public SchemaImpl(SchemaImpl schema) { this(); this.name = schema.getId(); this.parent = schema.getParent(); this.children = new ArrayList<String>(schema.getChildren()); this.pluralName = schema.getPluralName(); this.load(schema); }
public void load(SchemaImpl schema) { this.create = schema.isCreate(); this.update = schema.isUpdate(); this.list = schema.isList(); this.deletable = schema.isDeletable(); this.byId = schema.isById(); this.includeableLinks = new ArrayList<String>(schema.getIncludeableLinks()); this.resourceFields = copyFields(schema.getResourceFields()); this.collectionFilters = copyFilters(schema.getCollectionFilters()); this.resourceActions = copyActions(schema.getResourceActions()); this.collectionActions = copyActions(schema.getCollectionActions()); this.collectionFields = copyFields(schema.getCollectionFields()); }
protected SchemaImpl schemaFromObject(Object obj) { Class<?> clz = obj instanceof Class<?> ? (Class<?>)obj : obj.getClass(); SchemaImpl schema = new SchemaImpl(); SchemaType schemaType = obj instanceof String ? schemaTypeFromString((String)obj) : schemaTypeFromClass(clz); schema.setName(schemaType.name); schema.setPluralName(schemaType.pluralName); schema.setParent(schemaType.parent); if (schemaType.parent == null && schemaType.parentClass != null) { parentClasses.put(schema, schemaType.parentClass); } return schema; }
newSchema.setId(dynamicSchema.getName()); return newSchema; newSchema.setName(dynamicSchema.getName()); SchemaImpl mergedSchema = new SchemaImpl((SchemaImpl)parentSchema); mergedSchema.setPluralName(newSchema.getPluralName()); mergedSchema.setId(dynamicSchema.getName()); mergedSchema.setParent(dynamicSchema.getParent()); mergedSchema.setCollectionMethods(newSchema.getCollectionMethods()); mergedSchema.setResourceMethods(newSchema.getResourceMethods()); if (mergedSchema.getParent().equals(mergedSchema.getId())) { mergedSchema.setParent(parentSchema.getParent()); Map<String, Field> existingFields = mergedSchema.getResourceFields(); for (Map.Entry<String, Field> entry : newSchema.getResourceFields().entrySet()) { Field oldField = existingFields.put(entry.getKey(), entry.getValue()); if (oldField instanceof FieldImpl) {
@Override public SchemaImpl postProcess(SchemaImpl schema, SchemaFactory factory) { Perm perm = getPerm(schema.getId()); schema.setCreate(perm.isCreate()); schema.setUpdate(perm.isUpdate()); schema.setDeletable(perm.isDelete()); Iterator<Map.Entry<String, Field>> fieldIter = schema.getResourceFields().entrySet().iterator(); fieldImpl.setReadOnCreateOnly(perm.isReadOnCreateOnly()); Iterator<Map.Entry<String, Action>> actionIter = schema.getResourceActions().entrySet().iterator();
addTransitioningFields(schema, factory); Map<String, List<Relationship>> relationships = this.relationships.get(factory.getSchemaClass(schema.getId())); schema.getIncludeableLinks().add(linkName); continue; Field field = schema.getResourceFields().get(relationship.getPropertyName()); if (!(field instanceof FieldImpl)) { continue; fieldImpl.setType(FieldType.toString(FieldType.REFERENCE, factory.getSchema(relationship.getObjectType()).getId())); schema.getIncludeableLinks().add(linkName); Map<String, Filter> filters = schema.getCollectionFilters(); for (Map.Entry<String, Field> entry : schema.getResourceFields().entrySet()) { String name = entry.getKey(); Field field = entry.getValue(); TableField<?, ?> tableField = getTableFieldFor(schema.getId(), name);
protected void processParent(SchemaImpl schema) { SchemaImpl parent = null; Class<?> parentClass = parentClasses.get(schema); String parentName = schema.getParent(); if (parentClass == null && parentName != null) { parent = schemasByName.get(parentName); if (parent == null) { throw new IllegalArgumentException("Failed to find parent schema for [" + parentName + "] for type [" + schema.getId() + "]"); } } else if (parentClass != null) { parent = schemasByClass.get(parentClass); if (parent == null) { throw new IllegalArgumentException("Failed to find parent schema for class [" + parentClass + "] for type [" + schema.getId() + "]"); } } if (parent != null) { schema.setParent(parent.getId()); parent.getChildren().add(schema.getId()); schema.load(parent); } }
@Override public SchemaImpl postProcess(SchemaImpl schema, SchemaFactory factory) { Class<?> clz = factory.getSchemaClass(schema.getId()); if (clz == null || clz.getAnnotation(Entity.class) == null) { return schema; } schema.setCreate(true); schema.setUpdate(true); schema.setDeletable(true); for (PropertyDescriptor prop : PropertyUtils.getPropertyDescriptors(clz)) { processProperty(schema, prop); } return schema; }
@Override public SchemaImpl postProcessRegister(SchemaImpl schema, SchemaFactory factory) { schema.setCreate(false); schema.setUpdate(false); schema.setDeletable(false); for (Field field : schema.getResourceFields().values()) { if (field instanceof FieldImpl) { ((FieldImpl) field).setCreate(false); ((FieldImpl) field).setUpdate(false); } } return schema; }
@Override public SchemaImpl postProcessRegister(SchemaImpl schema, SchemaFactory factory) { if (ignoreTypes.contains(schema.getId())) { return schema; } try { List<URL> resources = lookUpResource(schema.getId()); if (whiteList && resources.size() == 0) { return null; } } catch (IOException e) { throw new IllegalStateException("Failed to lookup schema for [" + schema.getId() + "] at [" + path + "]"); } return super.postProcessRegister(schema, factory); }
@Override public Schema parseSchema(String name) { SchemaImpl schema = readSchema(name); Class<?> clz = typeToClass.get(name); processParent(schema); List<Field> fields = getFields(clz); for (Map.Entry<String, Field> entry : schema.getResourceFields().entrySet()) { Field field = entry.getValue(); if (field instanceof FieldImpl) { ((FieldImpl)field).setName(entry.getKey()); } fields.add(field); } Map<String, Field> resourceFields = sortFields(fields); schema.setResourceFields(resourceFields); schema.getResourceActions().putAll(getResourceActions(clz)); schema.getCollectionActions().putAll(getCollectionActions(clz)); for (SchemaPostProcessor processor : postProcessors) { schema = processor.postProcess(schema, this); } addToMap(schemasByName, schema, schema); if (clz == null && schema.getParent() != null) { clz = typeToClass.get(schema.getParent()); if (clz != null) { addToMap(typeToClass, schema, clz); } } return schema; }
protected void addStates(SchemaImpl schema, SchemaFactory factory) { String type = factory.getBaseType(schema.getId()); Set<String> validStates = this.validStates.get(type); Field stateField = schema.getResourceFields().get(ObjectMetaDataManager.STATE_FIELD); if (validStates != null && stateField instanceof FieldImpl) { FieldImpl field = (FieldImpl) stateField; field.setOptions(new ArrayList<String>(validStates)); field.setTypeEnum(FieldType.ENUM); } }
protected void addActions(SchemaImpl schema, SchemaFactory factory) { Set<String> actions = this.actions.get(schema.getId()); if (actions == null || actions.size() == 0) { return; } Map<String, Action> resourceActions = schema.getResourceActions(); if (resourceActions == null) { resourceActions = new LinkedHashMap<String, Action>(); schema.setResourceActions(resourceActions); } for (String action : actions) { if (!resourceActions.containsKey(action)) { Action newAction = new Action(); newAction.setOutput(schema.getId()); resourceActions.put(action, newAction); } } }
@Override protected void prune(SchemaImpl schema) { super.prune(schema); schema.getIncludeableLinks().clear(); Map<String, Relationship> rels = metaDataManager.getLinkRelationships(this, schema.getId()); if (rels == null || rels.size() == 0) { return; } else { for (Relationship rel : rels.values()) { schema.getIncludeableLinks().add(rel.getName()); } } }
protected SchemaImpl readSchema(String name) { Class<?> clz = typeToClass.get(name); if (clz == null) clz = Object.class; SchemaImpl schema = schemasByName.get(name); if (schema == null) schema = schemaFromObject(clz); io.github.ibuildthecloud.gdapi.annotation.Type type = clz.getAnnotation(io.github.ibuildthecloud.gdapi.annotation.Type.class); if (type == null) type = defaultType; if (type == defaultType) { schema.setCreate(writableByDefault); schema.setUpdate(writableByDefault); schema.setDeletable(writableByDefault); } else { schema.setCreate(type.create()); schema.setUpdate(type.update()); schema.setDeletable(type.delete()); } schema.setById(type.byId()); schema.setList(type.list()); return schema; }
protected <T> void addToMap(Map<String, T> map, SchemaImpl key, T value) { if (key == null || value == null) return; map.put(key.getId(), value); map.put(key.getId().toLowerCase(), value); map.put(key.getPluralName(), value); map.put(key.getPluralName().toLowerCase(), value); }
protected void prune(SchemaImpl schema) { Map<String, Field> fields = schema.getResourceFields(); Map<String, Filter> filters = schema.getCollectionFilters(); for (String name : new HashSet<String>(fields.keySet())) { Field field = fields.get(name); List<FieldType> subTypeEnums = field.getSubTypeEnums(); List<String> subTypes = field.getSubTypes(); for (int i = 0; i < subTypeEnums.size(); i++) { if (subTypeEnums.get(i) == FieldType.TYPE && !schemaMap.containsKey(subTypes.get(i)) && !"type".equals(subTypes.get(i))) { fields.remove(name); filters.remove(name); break; } } } Iterator<String> childrenIter = schema.getChildren().iterator(); while (childrenIter.hasNext()) { if (!schemaMap.containsKey(childrenIter.next())) { childrenIter.remove(); } } }
if (schema instanceof SchemaImpl) { SchemaImpl impl = new SchemaImpl((SchemaImpl)schema); schemaMap.put(impl.getId(), impl);
protected FieldImpl getField(SchemaImpl schema, String name) { Field field = schema.getResourceFields().get(name); if (field instanceof FieldImpl) { return (FieldImpl) field; } return null; } }