/** * Creates a document ID extractor for the given entity */ public DocIdExtractor(EntityMetadata md) { this(md.getEntitySchema()); }
public SimpleFindImpl(EntityMetadata md, Factory factory) { this.controller = factory.getCRUDController(md); LOGGER.debug("Controller for {}:{}", md.getName(), controller.getClass().getName()); }
protected final void checkMetadataHasName(EntityMetadata md) { checkMetadataHasName(md.getEntityInfo()); }
public synchronized void put(EntityMetadata md) { cache.put(new EntityVersion(md.getName(), md.getVersion().getValue()), new WeakReference(md)); }
private EntityMetadata getMetadata(String entityName, String version) { EntityMetadata emd=metadataMap.get(entityName); if(emd!=null) { if (!emd.getVersion().getValue().equals(version)) { throw new IllegalArgumentException(CrudConstants.ERR_METADATA_APPEARS_TWICE + entityName + " " + version + " and " + emd.getVersion().getValue()); } } else { LOGGER.debug("Retrieving entity metadata {}:{}",entityName,version); emd=md.getEntityMetadata(entityName,version); if (emd == null || emd.getEntitySchema() == null) { throw new IllegalArgumentException("Unknown entity:" + entityName + ":" + version); } if (emd.getEntitySchema().getStatus() == MetadataStatus.DISABLED) { throw new IllegalArgumentException(CrudConstants.ERR_DISABLED_METADATA + " " + entityName + " " + version); } metadataMap.put(entityName,emd); } return emd; } }
/** * * @param root the root metadata * @param gmd the GetMetadata for resolving metadata * @param entityPath the path to the metadata * @param parentEntity the parent metadata * @param path relative path in processing, if not a recursive call it * should be a new empty MutablePath object * @return */ private static CompositeMetadata buildCompositeMetadata(EntityMetadata root, GetMetadata gmd, Path entityPath, CompositeMetadata parentEntity, MutablePath path) { // Recursively process and copy the fields, retrieving // metadata for references Error.push(root.getName()); try { CompositeSchema cschema = CompositeSchema.newSchemaWithEmptyFields(root.getEntitySchema()); CompositeMetadata cmd = new CompositeMetadata(root.getEntityInfo(), cschema, entityPath, parentEntity); // copy fields, resolve references copyFields(cschema.getFields(), root.getEntitySchema().getFields(), path, cmd, gmd); return cmd; } finally { Error.pop(); } }
audit.setEntityName(processedDocument.getEntityMetadata().getName()); audit.setVersionText(processedDocument.getEntityMetadata().getVersion().getValue()); audit.setLastUpdateDate(DateType.getDateFormat().format(processedDocument.getWhen())); audit.setLastUpdatedBy(processedDocument.getWho()); for (Field f : processedDocument.getEntityMetadata().getEntitySchema().getIdentityFields()) { Path p = f.getFullPath();
/** * Converts the entity metadata to T */ public T convert(EntityMetadata md) { Error.push("convert[metadata]"); try { T ret = newNode(); putObject(ret, STR_ENTITY_INFO, convert(md.getEntityInfo())); putObject(ret, STR_SCHEMA, convert(md.getEntitySchema())); return ret; } catch (Error e) { // rethrow lightblue error throw e; } catch (Exception e) { // throw new Error (preserves current error context) LOGGER.error(e.getMessage(), e); throw Error.get(MetadataConstants.ERR_ILL_FORMED_METADATA, e.getMessage()); } finally { Error.pop(); } }
/** * Returns a set of fields that are inaccessible to the user for the given * operation */ public Set<Path> getInaccessibleFields(Operation op) { FieldCursor cursor = md.getFieldCursor(); Set<Path> fields = new HashSet<>(); while (cursor.next()) { FieldTreeNode fn = cursor.getCurrentNode(); if (fn instanceof Field && !hasAccess((Field) fn, op)) { fields.add(cursor.getCurrentPath()); } } return fields; }
@Override protected EntityMetadata retrieveMetadata(Path injectionPath, String entityName, String entityVersion) { EntityMetadata metadata=metadataMap.get(entityName); if(metadata!=null) { if(entityVersion!=null&&!entityVersion.equals(metadata.getVersion().getValue())) { throw Error.get(CrudConstants.ERR_METADATA_APPEARS_TWICE, entityName + ":" + entityVersion + " and " + metadata.getVersion().getValue()); } } else if(entityVersion!=null) { metadata = md.getEntityMetadata(entityName, entityVersion); if (metadata == null) { throw Error.get(CrudConstants.ERR_UNKNOWN_ENTITY, entityName + ":"+ entityVersion); } metadataMap.put(entityName,metadata); } else { // Metadata is requested with default version, and we haven't loaded it yet metadata=md.getEntityMetadata(entityName,null); if (metadata == null) { throw Error.get(CrudConstants.ERR_UNKNOWN_ENTITY, entityName + ":"+ entityVersion); } metadataMap.put(entityName,metadata); } return metadata; } }
private void addMetadataRoles(Set<String> roles, EntityMetadata em) { EntityAccess a = em.getAccess(); a.getFind().addRolesTo(roles); a.getUpdate().addRolesTo(roles); a.getInsert().addRolesTo(roles); a.getDelete().addRolesTo(roles); }
/** * Updates all array size values in the given document */ public static void updateArraySizes(EntityMetadata md, JsonNodeFactory factory, JsonDoc doc) { FieldCursor cursor = md.getFieldCursor(); while (cursor.next()) { FieldTreeNode f = cursor.getCurrentNode(); if (doesFieldNameMatchArrayCountPattern(f.getName())) { Path lengthField = cursor.getCurrentPath(); String ls = lengthField.toString(); Path arrField = new Path(ls.substring(0, ls.length() - 1)); try { if (md.resolve(arrField) != null) { JsonNode arrNode = doc.get(arrField); if (arrNode == null || arrNode instanceof NullNode) { doc.modify(lengthField, factory.numberNode(0), false); } else { doc.modify(lengthField, factory.numberNode(arrNode.size()), false); } } } catch (Exception e) { } } } }
if (md.getAccess().getDelete().hasAccess(ctx.getCallerRoles())) { LOGGER.debug("Translating query {}", query); DBObject mongoQuery = xtranslator.translate(md, ExpressionTranslator.appendObjectType(query,ctx.getEntityName())); LOGGER.debug("Translated query {}", mongoQuery); DB db = dbResolver.get((MongoDataStore) md.getDataStore()); DBCollection coll = db.getCollection(((MongoDataStore) md.getDataStore()).getCollectionName()); DocDeleter deleter = new BasicDocDeleter(translator, MongoExecutionOptions. getWriteConcern(ctx.getExecutionOptions()), batchSize);
/** * Returns whether the current caller has access to the given field based on * the operation */ public boolean hasAccess(Path field, Operation op) { FieldTreeNode fn = md.resolve(field); if (fn != null) { if (fn instanceof Field) { return hasAccess((Field) fn, op); } else { return true; } } else { return false; } }
/** * Builds a projector using the given projection and entity metadata */ public static Projector getInstance(Projection projection, EntityMetadata md) { return getInstance(projection, Path.EMPTY, md.getFieldTreeRoot()); }
throw Error.get(MongoMetadataConstants.ERR_UNKNOWN_VERSION, entityName + ":" + version); return new EntityMetadata(info, schema); } catch (Error | IllegalArgumentException e) {
/** * Returns a CRUD controller for the given entity */ public CRUDController getCRUDController(EntityMetadata md) { return getCRUDController(md.getDataStore().getBackend()); }
/** * When EntityInfo is updated, we have to make sure any active/deprecated metadata is still valid */ protected void validateAllVersions(EntityInfo ei) { LOGGER.debug("Validating all versions of {}", ei.getName()); String version = null; try { DBCursor cursor = new FindCommand(collection, new BasicDBObject(LITERAL_NAME, ei.getName()). append(LITERAL_VERSION, new BasicDBObject("$exists", 1)). append(LITERAL_STATUS_VALUE, new BasicDBObject("$ne", MetadataParser.toString(MetadataStatus.DISABLED))), null).executeAndUnwrap(); while (cursor.hasNext()) { DBObject object = cursor.next(); EntitySchema schema = mdParser.parseEntitySchema(object); version = schema.getVersion().getValue(); LOGGER.debug("Validating {} {}", ei.getName(), version); EntityMetadata md = new EntityMetadata(ei, schema); md.validate(); } } catch (Exception e) { String msg = ei.getName() + ":" + version + e.toString(); throw analyzeException(e, MongoMetadataConstants.ERR_UPDATE_INVALIDATES_METADATA, msg); } }
@Override protected void translate(JsonNodeCursor cursor, Object target) { Path path = cursor.getCurrentPath(); FieldTreeNode fieldNode = entityMetadata.resolve(path); if(fieldNode instanceof Field){ String fieldName = ((Field) fieldNode).getName(); Fields fields = entityMetadata.getFields(); if(fieldNode instanceof ObjectField){ fields = ((ObjectField) fieldNode).getFields(); } if(PredefinedFields.isPredefinedField(fieldName, fields)){ /* * Indicates the field is auto-generated for lightblue purposes. These fields * should not be inserted into the data store. */ return; } } super.translate(cursor, target); }