public JpaBeanQuery(Schema schema, EntityManager em, Jpa20BeanManager manager) { this.schema = schema; this.schemaName = schema.getName(); this.em = em; this.manager = manager; }
public XmlSchema(Schema bean) { this.id = new XmlSchemaId(bean.getId()); this.name = bean.getName(); this.type = bean.getType(); this.desc = bean.getDesc(); for (SchemaProperty p : bean.get(SchemaProperty.class)) { properties.add(new XmlSchemaProperty(p)); } for (SchemaPropertyList p : bean.get(SchemaPropertyList.class)) { collection.add(new XmlSchemaCollection(p)); } for (SchemaPropertyRef p : bean.get(SchemaPropertyRef.class)) { ref.add(new XmlSchemaRef(p)); } for (SchemaPropertyRefList p : bean.get(SchemaPropertyRefList.class)) { refCollection.add(new XmlSchemaRefCollection(p)); } for (SchemaPropertyRefMap p : bean.get(SchemaPropertyRefMap.class)) { refMap.add(new XmlSchemaRefMap(p)); } } }
public Map<String, Schema> getSchemas() { Map<String, Schema> result = new HashMap<String, Schema>(); for (XmlSchema b : schemas) { Schema schema = Schema.create( SchemaId.create(b.id.name, b.id.desc, b.id.singleton), b.type, b.name, b.desc); for (XmlSchemaProperty p : b.properties) { schema.add(SchemaProperty.create(p.name, p.fieldName, p.type, p.desc, p.isImmutable, p.isEnum, p.defaultValue)); } for (XmlSchemaCollection p : b.collection) { schema.add(SchemaPropertyList.create(p.name, p.fieldName, p.parameterizedType, p.desc, p.isImmutable, p.isEnum, p.defaultValues, p.collectionType)); } for (XmlSchemaRef p : b.ref) { schema.add(SchemaPropertyRef.create(p.name, p.fieldName, p.schemaName, p.desc, p.isImmutable, p.isSingleton)); } for (XmlSchemaRefCollection p : b.refCollection) { schema.add(SchemaPropertyRefList.create(p.name, p.fieldName, p.schemaName, p.desc, p.isImmutable, p.collectionType)); } for (XmlSchemaRefMap p : b.refMap) { schema.add(SchemaPropertyRefMap.create(p.name, p.fieldName, p.schemaName, p.desc, p.isImmutable, p.mapType)); } result.put(schema.getName(), schema); } return result; }
private void convertPropertyRef(Bean source, Map<String, Object> values, Map<BeanId, Object> instanceCache) { for (SchemaPropertyRef prop : source.getSchema().get(SchemaPropertyRef.class)) { BeanId id = source.getFirstReference(prop.getName()); if (id == null) { continue; } Bean ref = id.getBean(); if (ref == null) { continue; } Schema refSchema = ref.getSchema(); SchemaPropertyRef schemaRef = source.getSchema().get(SchemaPropertyRef.class, prop.getName()); Object beanInstance = instanceCache.get(id); if (beanInstance == null) { try { beanInstance = newInstance(loadClass(refSchema.getType())); } catch (Exception e) { throw new UnsupportedOperationException(e); } beanInstance = convert(ref, beanInstance, new HashMap<String, Object>(), instanceCache); } values.put(schemaRef.getFieldName(), beanInstance); } }
private void constructBean(Map<String, Object> properties, Schema schema, Bean bean) { bean.set(schemas.get(bean.getId().getSchemaName())); for (String propertyName : schema.getPropertyNames()) { Object value = properties.get(propertyName); if (value == null) { for (String propertyName : schema.getReferenceNames()) { Object value = properties.get(propertyName); Schema refSchema = schemas.get(schema.getReferenceSchemaName(propertyName)); if (refSchema.getId().isSingleton()) { BeanId id = BeanId.createSingleton(refSchema.getName()); Optional<Bean> optional = get(id); if (!optional.isPresent()) { List<BeanId> beanIds = new ArrayList<>(); for (String id : stringValues) { BeanId beanId = BeanId.create(id, refSchema.getName()); Optional<Bean> ref = get(beanId); if (!ref.isPresent()) { BeanId id = BeanId.create(value.toString(), refSchema.getName()); Optional<Bean> idBean = get(id); if (!idBean.isPresent()) {
@SuppressWarnings("unused") private static void validatePropertyRefList(Bean bean) { Schema schema = bean.getSchema(); for (SchemaPropertyRefList prop : schema.get(SchemaPropertyRefList.class)) { } }
public List<Bean> list(Schema schema) { List<Bean> beans = new ArrayList<>(); Map<String, Map<String, Object>> instances; try { instances = (Map) config.getAnyRef(schema.getName()); if (instances == null) { return beans; } } catch (ConfigException e) { return beans; } if (schema.getId().isSingleton()) { final Bean bean = Bean.create(BeanId.createSingleton(schema.getName())); Map<String, Object> properties = (Map) instances; beans.add(bean); constructBean(properties, schema, bean); } else { for (String instanceId : instances.keySet()) { final Map<String, Object> properties = instances.get(instanceId); final Bean bean = Bean.create(BeanId.create(instanceId, schema.getName())); bean.set(schema); beans.add(bean); constructBean(properties, schema, bean); } } return beans; }
String desc = "configDesc" + i; SchemaId id = SchemaId.create("configId" + i, "configDesc" + i, false); Schema schema = Schema.create(id, type, name, desc); for (int j = 0; j < numProps; j++) { String _name = "propName" + j; SchemaProperty prop = SchemaProperty.create(_name, _fieldName, _classType, _desc, true, new ArrayList<String>(), _defaultValue, false); schema.add(prop); _name = "collPropName" + j; _fieldName = "collpropFieldName" + j; _desc, true, new ArrayList<String>(), _colDefault, _colDefault.getClass() .getName(), false); schema.add(col);
/** * This is the fully qualified classname of the configurable class that this schema originates from. * * @return Class of this schema. */ public Class<?> getClassType() { try { return Class.forName(getType()); } catch (ClassNotFoundException e) { throw new IllegalStateException(e); } }
private Schema getSchema(Class<?> clazz) { Schema schema = schemas.get(clazz); if(schema != null) { return schema; } ClassIntrospector introspector = new ClassIntrospector(clazz); Config config = introspector.getAnnotation(Config.class); if (config == null) { throw CFG102_NOT_CONFIGURABLE(clazz); } String schemaName = config.name(); if (schemaName == null || "".equals(schemaName)) { schemaName = clazz.getName(); } schema = conversion.convert(clazz, Schema.class); for (Class<?> cls : schema.getReferenceSchemaTypes()) { if (schemas.get(clazz) != null) { getSchema(cls); } } properties.registerSchema(schema); schemas.put(schema.getName(), schema); schemaManager.registerSchema(schema); core.putIndex(schema); return schema; } }
public boolean isReference(String property) { return getReferenceNames().contains(property); }
public boolean isProperty(String property) { return getPropertyNames().contains(property); }
private Object convert(Bean source, Object instance, Map<String, Object> valuesToInject, Map<BeanId, Object> instanceCache) { instanceCache.put(source.getId(), instance); convertProperty(source, valuesToInject); convertPropertyList(source, valuesToInject); convertPropertyRef(source, valuesToInject, instanceCache); convertPropertyRefList(source, valuesToInject, instanceCache); convertPropertyRefMap(source, valuesToInject, instanceCache); Schema schema = source.getSchema(); if (!schema.getId().isSingleton()) { // do not try to inject get id: the field is static final valuesToInject.put(getIdField(instance.getClass()), source.getId().getInstanceId()); } inject(instance, valuesToInject); return instance; }
private void convertPropertyRef(Bean source, Map<String, Object> values, Map<BeanId, Object> instanceCache) { for (SchemaPropertyRef prop : source.getSchema().get(SchemaPropertyRef.class)) { BeanId id = source.getFirstReference(prop.getName()); if (id == null) { continue; } Bean ref = id.getBean(); if (ref == null) { continue; } Schema refSchema = ref.getSchema(); SchemaPropertyRef schemaRef = source.getSchema().get(SchemaPropertyRef.class, prop.getName()); Object beanInstance = instanceCache.get(id); if (beanInstance == null) { try { beanInstance = newInstance(forName(refSchema.getType())); } catch (Exception e) { throw new UnsupportedOperationException(e); } beanInstance = convert(ref, beanInstance, new HashMap<String, Object>(), instanceCache); } values.put(schemaRef.getFieldName(), beanInstance); } }
private static void validatePropertyRefMap(Bean bean) { Schema schema = bean.getSchema(); for (SchemaPropertyRefMap prop : schema.get(SchemaPropertyRefMap.class)) { } }
private Optional<Bean> get(BeanId beanId) { Schema schema = schemas.get(beanId.getSchemaName()); if (schema == null) { throw Events.CFG101_SCHEMA_NOT_EXIST(beanId.getSchemaName()); } Map<String, Map<String, Object>> instances = (Map) config.getAnyRef(schema.getName()); try { final Bean bean; if (schema.getId().isSingleton()) { bean = Bean.create(BeanId.createSingleton(schema.getName())); constructBean((Map) instances, schema, bean); } else { if (instances == null) { return Optional.absent(); } bean = Bean.create(beanId); Map<String, Object> properties = instances.get(beanId.getInstanceId()); constructBean(properties, schema, bean); } return Optional.of(bean); } catch (ConfigException e) { return Optional.absent(); } }
@Override public Schema convert(Class<?> source, Class<? extends Schema> specificType) { ClassIntrospector introspector = new ClassIntrospector(source); Config config = introspector.getAnnotation(Config.class); if (config == null) { throw CFG102_NOT_CONFIGURABLE(source); } SchemaId schemaId = getId(introspector); String schemaName = config.name(); if (schemaName == null || "".equals(schemaName)) { schemaName = source.getName(); } Schema schema = Schema.create(schemaId, introspector.getName(), schemaName, config.desc()); Collection<Object> fields = new ArrayList<Object>(); fields.addAll(introspector.getFieldList(Config.class)); Collection<AbstractSchemaProperty> props = conversion.convert(fields, AbstractSchemaProperty.class); for (AbstractSchemaProperty abstractProp : props) { schema.add(abstractProp); } return schema; }