@SuppressWarnings("unused") private static void validatePropertyRefMap(Bean bean) { Schema schema = bean.getSchema(); for (SchemaPropertyRefMap prop : schema.get(SchemaPropertyRefMap.class)) { } }
@SuppressWarnings("unused") private static void validatePropertyRefList(Bean bean) { Schema schema = bean.getSchema(); for (SchemaPropertyRefList prop : schema.get(SchemaPropertyRefList.class)) { } }
private static void validatePropertyReferences(Bean bean) { Schema schema = bean.getSchema(); for (SchemaPropertyRef prop : schema.get(SchemaPropertyRef.class)) { validateSingle(bean, prop); } }
private static void validateReferences(Bean bean) { Schema schema = bean.getSchema(); Set<String> schemaReferenceNames = schema.getReferenceNames(); for (String name : bean.getReferenceNames()) { if (!schemaReferenceNames.contains(name)) { throw Events.CFG111_REF_NOT_EXIST_IN_SCHEMA(name); } } }
private static void validatePropertyNames(Bean bean) { Schema schema = bean.getSchema(); Set<String> schemaPropertyNames = schema.getPropertyNames(); for (String name : bean.getPropertyNames()) { if (!schemaPropertyNames.contains(name)) { throw Events.CFG110_PROP_NOT_EXIST_IN_SCHEMA(name); } } }
private void setSingletonReferences(Bean bean) { Schema s = bean.getSchema(); for (SchemaPropertyRef ref : s.get(SchemaPropertyRef.class)) { if (ref.isSingleton()) { Schema singletonSchema = schemaManager.getSchema(ref.getSchemaName()); Optional<Bean> singleton = beanManager.getSingleton(ref.getSchemaName()); if (singleton.isPresent()) { singleton.get().set(singletonSchema); BeanId singletonId = singleton.get().getId(); singletonId.setBean(singleton.get()); // recursive call. setSingletonReferences(singleton.get()); bean.setReference(ref.getName(), singletonId); } } } }
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(); if (refSchema == null) { throw Events.CFG101_SCHEMA_NOT_EXIST(ref.getId().getSchemaName()); } 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 convertPropertyRefList(Bean source, Map<String, Object> values, Map<BeanId, Object> instanceCache) { for (SchemaPropertyRefList prop : source.getSchema().get(SchemaPropertyRefList.class)) { List<BeanId> references = source.getReference(prop.getName()); if (references == null) { continue; } Collection<Object> c = newCollection(loadClass(prop.getCollectionType())); for (BeanId beanId : references) { Bean b = beanId.getBean(); if (b != null) { Object beanInstance = instanceCache.get(beanId); if (beanInstance == null) { String type = b.getSchema().getType(); try { beanInstance = newInstance(loadClass(type)); } catch (Exception e) { throw new UnsupportedOperationException(e); } beanInstance = convert(b, beanInstance, new HashMap<String, Object>(), instanceCache); } c.add(beanInstance); } } values.put(prop.getFieldName(), c); } }
private void convertPropertyRefMap(Bean source, Map<String, Object> values, Map<BeanId, Object> instanceCache) { for (SchemaPropertyRefMap prop : source.getSchema().get(SchemaPropertyRefMap.class)) { List<BeanId> beans = source.getReference(prop.getName()); if (beans == null) { continue; } Map<Object, Object> c = newMap(loadClass(prop.getMapType())); for (BeanId beanId : beans) { Bean b = beanId.getBean(); if (b != null) { Object beanInstance = instanceCache.get(beanId); if (beanInstance == null) { try { beanInstance = newInstance(loadClass(b.getSchema().getType())); } catch (Exception e) { throw new UnsupportedOperationException(e); } beanInstance = convert(b, beanInstance, new HashMap<String, Object>(), instanceCache); } c.put(beanId.getInstanceId(), beanInstance); } } values.put(prop.getFieldName(), c); } }
private void convertProperty(Bean source, Map<String, Object> values) { for (SchemaProperty prop : source.getSchema().get(SchemaProperty.class)) { String value = source.getSingleValue(prop.getName()); String field = prop.getFieldName(); Object converted = conversion.convert(value, loadClass(prop.getType())); values.put(field, converted); } }
@Override public void setSchema(Collection<Bean> beans) { for (Bean bean : beans) { Schema schema = NAME_TO_SCHEMA.get(bean.getId().getSchemaName()); if (schema == null) { throw CFG101_SCHEMA_NOT_EXIST(bean.getId().getSchemaName()); } bean.set(schema); for (BeanId id : bean.getReferences()) { Bean refBean = id.getBean(); if (refBean != null && refBean.getSchema() == null) { setSchema(Arrays.asList(refBean)); } } } }
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 lookup id: the field is static final valuesToInject.put(getIdField(instance.getClass()), source.getId().getInstanceId()); } inject(instance, valuesToInject); return instance; }
private void setSingletonReferences(Bean bean) { Schema s = bean.getSchema(); for (SchemaPropertyRef ref : s.get(SchemaPropertyRef.class)) { if (ref.isSingleton()) { Schema singletonSchema = schemaManager.getSchema(ref.getSchemaName()); Optional<Bean> singleton = beanManager.getSingleton(ref.getSchemaName()); if (!singleton.isPresent()) { initFile(null); Bean fileBean = FILE_CONFIG.get(BeanId.createSingleton(ref.getSchemaName())); if (fileBean != null) { singleton = Optional.of(fileBean); } } if (!singleton.isPresent()) { singleton = Optional.of(Bean.create(BeanId.createSingleton(ref.getSchemaName()))); } singleton.get().set(singletonSchema); BeanId singletonId = singleton.get().getId(); singletonId.setBean(singleton.get()); // recursive call. setSingletonReferences(singleton.get()); bean.setReference(ref.getName(), singletonId); } } }
private void convertPropertyList(Bean source, Map<String, Object> values) { for (SchemaPropertyList prop : source.getSchema().get(SchemaPropertyList.class)) { List<String> vals = source.getValues(prop.getName()); String field = prop.getFieldName(); if (vals == null) { continue; } Collection<Object> c = newCollection(loadClass(prop.getCollectionType())); for (String val : vals) { Object converted = conversion.convert(val, loadClass(prop.getType())); c.add(converted); } values.put(field, c); } }
private static void validateProperties(Bean bean) { Schema schema = bean.getSchema(); for (SchemaProperty prop : schema.get(SchemaProperty.class)) { String value = validateSingle(bean, prop); if (value == null) { continue; } try { conversion.convert(value, forName(prop.getType())); } catch (Exception e) { throw Events.CFG105_WRONG_PROPERTY_TYPE(bean.getId(), prop.getName(), prop.getType(), value); } } }
private static void validatePropertyList(Bean bean) { Schema schema = bean.getSchema(); for (SchemaPropertyList prop : schema.get(SchemaPropertyList.class)) { List<String> values = bean.getValues(prop.getName()); if (values == null) { continue; } for (String value : values) { try { conversion.convert(value, forName(prop.getType())); } catch (Exception e) { throw Events.CFG105_WRONG_PROPERTY_TYPE(bean.getId(), prop.getName(), prop.getType(), value); } } } }