/** * Returns the a list of property names of the target bean that have * references to the bean id. */ private static boolean hasReferences(Bean target, BeanId reference) { for (String name : target.getReferenceNames()) { List<BeanId> refs = target.getReference(name); if (refs != null) { for (BeanId ref : target.getReference(name)) { if (ref.equals(reference)) { return true; } } } } return false; }
/** * A helper method for getting the value of single referenced property. Returns * null if the refrences does not exist. * * @param propertyName name of the property as defined by the bean's schema. * @return the value. */ public BeanId getFirstReference(final String propertyName) { List<BeanId> refrences = getReference(propertyName); if (refrences == null || refrences.size() < 1) { return null; } return refrences.get(0); }
/** * Returns the a list of property names of the target bean that have * references to the bean id. */ private static boolean hasReferences(Bean target, BeanId reference) { for (String name : target.getReferenceNames()) { for (BeanId ref : target.getReference(name)) { if (ref.equals(reference)) { return true; } } } return false; }
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(forName(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(forName(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 static void checkReferencesExist(final Bean bean, final Map<BeanId, Bean> storage) { ArrayList<BeanId> allRefs = new ArrayList<BeanId>(); for (String name : bean.getReferenceNames()) { if (bean.getReference(name) == null) { // the reference is about to be removed. continue; } for (BeanId beanId : bean.getReference(name)) { allRefs.add(beanId); } } Collection<BeanId> missingReferences = new ArrayList<BeanId>(); for (BeanId beanId : allRefs) { if (beanId.getInstanceId() == null) { continue; } Bean b = storage.get(beanId); if (b == null) { missingReferences.add(beanId); } } if (missingReferences.size() > 0) { throw CFG301_MISSING_RUNTIME_REF(bean.getId(), missingReferences); } }
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(forName(prop.getMapType())); for (BeanId beanId : beans) { Bean b = beanId.getBean(); if (b != null) { Object beanInstance = instanceCache.get(beanId); if (beanInstance == null) { try { beanInstance = newInstance(forName(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 initReferences(Collection<Bean> beans) { Map<BeanId, Bean> indexed = BeanUtils.uniqueIndex(beans); for (Bean bean : beans) { for (String name : bean.getReferenceNames()) { List<BeanId> ids = bean.getReference(name); if (ids == null) { continue; } for (BeanId id : ids) { Bean ref = indexed.get(id); if (ref == null) { // TODO: investigate if eager is really needed Optional<Bean> optionalRef = beanManager.getEager(id); if (optionalRef.isPresent()) { core.setSchema(optionalRef.get(), schemaManager.getSchemas()); ref = optionalRef.get(); } } id.setBean(ref); } } } }
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 initReferences(Collection<Bean> beans) { Map<BeanId, Bean> indexed = BeanUtils.uniqueIndex(beans); for (Bean bean : beans) { for (String name : bean.getReferenceNames()) { List<BeanId> ids = bean.getReference(name); for (BeanId id : ids) { Bean ref = indexed.get(id); if (ref == null) { // TODO: investigate if eager is really needed ref = beanManager.getEager(id); setSchema(ref, schemaManager.getSchemas()); } id.setBean(ref); } } } }
private void mergeReferences(Map<BeanId, Bean> mergeBeans, List<Bean> stored) { for (Bean store : stored) { Bean mergeBean = mergeBeans.get(store.getId()); for (String name : mergeBean.getReferenceNames()) { deleteReference(store.getId(), name); List<BeanId> beanIds = mergeBean.getReference(name); if (beanIds == null) { continue; } for (BeanId ref : beanIds) { getEmOrFail().persist(new JpaRef(mergeBean.getId(), ref, name)); } } } }
/** * Used for setting or creating a single bean. */ @SuppressWarnings("unused") private void initalizeReferences(Bean bean) { for (String name : bean.getReferenceNames()) { List<BeanId> values = bean.getReference(name); if (values == null) { continue; } for (BeanId beanId : values) { Optional<Bean> ref = beanManager.getEager(beanId); if (ref.isPresent()) { beanId.setBean(ref.get()); } core.setSchema(beanId.getBean(), schemaManager.getSchemas()); } } }
/** * Used for setting or creating a single bean. */ private void initalizeReferences(Bean bean) { for (String name : bean.getReferenceNames()) { List<BeanId> values = bean.getReference(name); if (values == null) { continue; } for (BeanId beanId : values) { Bean ref = beanManager.getEager(beanId); beanId.setBean(ref); setSchema(beanId.getBean(), schemaManager.getSchemas()); } } }
private void createJpaRefs(Collection<Bean> beans) { int i = 0; for (Bean bean : beans) { for (String name : bean.getReferenceNames()) { List<BeanId> refs = bean.getReference(name); if (refs == null) { continue; } for (BeanId id : refs) { if (!exists(id)) { throw CFG301_MISSING_RUNTIME_REF(bean.getId(), id); } getEmOrFail().persist(new JpaRef(bean.getId(), id, name)); if (i++ % BATCH_SIZE == 0) { getEmOrFail().flush(); getEmOrFail().clear(); } } } } }
private void merge(List<Bean> sources, Bean mergeBean) { HashMap<BeanId, Bean> cache = new HashMap<BeanId, Bean>(); for (Bean source : sources) { for (String name : mergeBean.getPropertyNames()) { List<String> values = mergeBean.getValues(name); if (values == null || values.size() == 0) { continue; } source.setProperty(name, values); } for (String name : mergeBean.getReferenceNames()) { List<BeanId> refs = mergeBean.getReference(name); if (refs == null || refs.size() == 0) { source.setReferences(name, refs); continue; } for (BeanId beanId : refs) { Bean bean = cache.get(beanId); if (bean == null) { bean = beanManager.getLazy(beanId); setSchema(bean, schemaManager.getSchemas()); cache.put(beanId, bean); } beanId.setBean(bean); } source.setReferences(name, refs); } } }
/** * Used for setting or creating a multiple beans. * * We must consider that the operation may include beans that have * references betewen eachother. User provided beans are * prioritized and the storage is secondary for looking up references. */ private void initalizeReferences(Collection<Bean> beans) { Map<BeanId, Bean> userProvided = BeanUtils.uniqueIndex(beans); for (Bean bean : beans) { for (String name : bean.getReferenceNames()) { List<BeanId> values = bean.getReference(name); if (values == null) { continue; } for (BeanId beanId : values) { // the does not exist in storage, but may exist in the // set of beans provided by the user. Bean ref = userProvided.get(beanId); if (ref == null) { ref = beanManager.getEager(beanId); } beanId.setBean(ref); setSchema(beanId.getBean(), schemaManager.getSchemas()); } } } }
List<BeanId> refs = mergeBean.getReference(name); if (refs == null || refs.size() == 0) { source.setReferences(name, refs);
public XmlBean(Bean bean) { this.id = bean.getId().getInstanceId(); this.name = bean.getId().getSchemaName(); this.singleton = bean.getId().isSingleton(); for (String name : bean.getPropertyNames()) { List<String> values = bean.getValues(name); if (values == null || values.size() == 0) { continue; } properties.add(new XmlBeanPropertyList(name, values)); } for (String name : bean.getReferenceNames()) { List<BeanId> ids = bean.getReference(name); if (ids == null || ids.size() == 0) { continue; } String schemaName = ids.get(0).getSchemaName(); references.add(new XmlBeanPropertyRefList(name, schemaName, ids)); } } }
private void replace(Bean target, Bean replace) { if (target == null) { // bean did not exist in storage, create it. target = replace; } checkReferencesExist(replace, new ArrayList<Bean>()); for (String name : replace.getPropertyNames()) { List<String> values = replace.getValues(name); if (values == null || values.size() == 0) { // null/empty indicates a remove/reset-to-default op target.remove(name); } else { target.setProperty(name, replace.getValues(name)); } } for (String name : replace.getReferenceNames()) { List<BeanId> values = replace.getReference(name); if (values == null || values.size() == 0) { // null/empty indicates a remove/reset-to-default op target.remove(name); } else { target.setReferences(name, values); } } }
private void replace(Bean target, Bean replace, Map<BeanId, Bean> all) { if (target == null) { // bean did not exist in storage, create it. target = replace; } checkReferencesExist(replace, all); for (String name : replace.getPropertyNames()) { List<String> values = replace.getValues(name); if (values == null || values.size() == 0) { // null/empty indicates a remove/reset-to-default op target.remove(name); } else { target.setProperty(name, replace.getValues(name)); } } for (String name : replace.getReferenceNames()) { List<BeanId> values = replace.getReference(name); if (values == null || values.size() == 0) { // null/empty indicates a remove/reset-to-default op target.remove(name); } else { target.setReferences(name, values); } } }
private Bean copy(Bean bean) { if (bean == null) { return null; } Bean copy = Bean.create(bean.getId()); for (String property : bean.getPropertyNames()) { copy.setProperty(property, bean.getValues(property)); } for (String property : bean.getReferenceNames()) { List<BeanId> ids = bean.getReference(property); for (BeanId id : ids) { copy.addReference(property, BeanId.create(id.getInstanceId(), id.getSchemaName())); } } return copy; } }