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; } }
public List<Bean> getBeans() { ArrayList<Bean> result = new ArrayList<Bean>(); for (XmlBean b : beans) { Bean bean = null; if (b.singleton) { bean = Bean.create(BeanId.createSingleton(b.id, b.name)); } else { bean = Bean.create(BeanId.create(b.id, b.name)); } for (XmlBeanPropertyList p : b.properties) { if (p.values != null) bean.addProperty(p.name, p.values); } for (XmlBeanPropertyRefList p : b.references) { if (p.refs != null) bean.addReference(p.name, p.getReferences()); } result.add(bean); } return result; }
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 Map<BeanId, Bean> getDirectSuccessors(Bean bean, Map<BeanId, Bean> all) { Map<BeanId, Bean> successors = new HashMap<BeanId, Bean>(); for (Bean b : all.values()) { List<BeanId> refs = b.getReferences(); if (refs.contains(bean.getId())) { successors.put(b.getId(), b); } } return successors; }
Bean child = Bean.create(BeanId.create("child", "java.lang.String")); child.addProperty("property1", "true"); child.addProperty("property2", "false"); manager.create(child); Bean parent = Bean.create(BeanId.create("parent", "java.lang.String")); parent.addReference("refName", BeanId.create("child", "java.lang.String")); parent.addProperty("property1", "prop1"); parent.addProperty("property2", "prop2"); manager.create(parent); Bean grandparent = Bean.create(grandParentId); grandparent.addReference("refName", BeanId.create("parent", "java.lang.String")); manager.create(grandparent); List<String> childs = grandpa.get().getReferenceNames(); assertThat(childs.size(), is(1)); BeanId childRef = grandpa.get().getReference(childs.get(0)).get(0); Bean childBean = childRef.getBean(); assertThat(childBean.getId(), is(childBean.getId())); assertEquals(childBean.getSingleValue("property1"), "prop1"); assertEquals(childBean.getSingleValue("property2"), "prop2"); childs = childBean.getReferenceNames(); assertThat(childs.size(), is(1)); childRef = childBean.getReference(childs.get(0)).get(0); childBean = childRef.getBean(); assertThat(childBean.getId(), is(childBean.getId()));
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)); } } } }
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)); } } }
/** * 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; }
private void createJpaProperties(Bean bean) { for (String name : bean.getPropertyNames()) { List<String> values = bean.getValues(name); if (values == null) { continue; } for (String value : values) { getEmOrFail().persist(new JpaProperty(bean.getId(), name, value)); } } }
public Bean traverse(Set<Bean> beans, Bean parent) { Bean current = Bean.create(BeanId.create(value + "", "binarytree")); current.setProperty("value", value + ""); if (parent != null) { current.setReference("parent", parent.getId()); } if (left != null) { Bean leftBean = left.traverse(beans, current); current.setReference("left", leftBean.getId()); } if (right != null) { Bean rightBean = right.traverse(beans, current); current.setReference("right", rightBean.getId()); } beans.add(current); return current; }
private void setSingletonReferences(Bean bean, Map<String, Schema> schemas) { Schema s = bean.getSchema(); for (SchemaPropertyRef ref : s.get(SchemaPropertyRef.class)) { if (ref.isSingleton()) { Schema singletonSchema = schemas.get(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(), schemas); bean.setReference(ref.getName(), singletonId); } } }
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); } } } }
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); bean.addProperty(propertyName, toStrings((Collection) value)); } else { bean.addProperty(propertyName, value.toString()); b.set(schemas.get(b.getId().getSchemaName())); id.setBean(b); bean.addReference(propertyName, id); b.set(schemas.get(b.getId().getSchemaName())); beanId.setBean(b); beanIds.add(beanId); bean.addReference(propertyName, beanIds); } else { BeanId id = BeanId.create(value.toString(), refSchema.getName()); b.set(schemas.get(b.getId().getSchemaName())); id.setBean(b); bean.addReference(propertyName, id);
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); } }
BeanId dId = BeanId.create("d", personSchema); Bean a = Bean.create(aId); Bean b = Bean.create(bId); Bean c = Bean.create(cId); Bean d = Bean.create(dId); a.setReference("bestFriend", bId); b.setReference("bestFriend", aId); c.setReference("bestFriend", dId); d.setReference("bestFriend", cId); a.addReference("closeFriends", Arrays.asList(bId, cId, dId)); b.addReference("closeFriends", Arrays.asList(aId, cId, dId)); c.addReference("closeFriends", Arrays.asList(aId, bId, dId)); d.addReference("closeFriends", Arrays.asList(aId, bId, cId)); a.addReference("colleauges", Arrays.asList(bId, cId, dId)); b.addReference("colleauges", Arrays.asList(aId, cId, dId)); c.addReference("colleauges", Arrays.asList(aId, bId, dId)); d.addReference("colleauges", Arrays.asList(aId, bId, cId));
createDefault(); observer.clear(); Bean bean = Bean.create(c1.getId()); bean.addProperty("non_existing", "bogus"); try { admin.create(bean); bean = Bean.create(BeanId.create("c5", CHILD_SCHEMA_NAME)); bean.setReference("non_existing", c1.getId()); admin.create(bean); fail("Not possible to set property names that does not exist in schema"); assertThat(observer.getChanges().size(), is(0)); bean = Bean.create(c1.getId()); bean.addProperty("non_existing", "bogus"); try { admin.set(bean);
Bean child = Bean.create(BeanId.create("c1", ConfigTestData.CHILD_SCHEMA_NAME)); child.setProperty("prop8", "100000"); admin.set(child); fail("10000 does not fit java.lang.Byte"); child.addProperty("prop3", "2.2"); admin.merge(child); fail("2.2 does not fit a collection of java.lang.Integer"); Bean parent = Bean.create(BeanId.create("g1", ConfigTestData.GRANDFATHER_SCHEMA_NAME)); try { parent.setProperty("prop14", "not_a_enum"); admin.set(parent); fail("not_a_enum is not a value of TimeUnit"); parent = Bean.create(BeanId.create("p1", ConfigTestData.PARENT_SCHEMA_NAME)); try { parent.addProperty("prop19", "not_a_enum"); admin.merge(parent); fail("not_a_enum is not a value of TimeUnit"); Bean grandfather = Bean.create(BeanId.create("g1", ConfigTestData.GRANDFATHER_SCHEMA_NAME)); try { grandfather.addProperty("prop1", Arrays.asList("1", "2")); admin.merge(grandfather); fail("Cannot add mutiple values to a single valued property."); grandfather = Bean.create(BeanId.create("p1", ConfigTestData.PARENT_SCHEMA_NAME));
@Test public void test_set_empty_properties() { createThenGet(c1); Bean b = Bean.create(BeanId.create("c1", CHILD_SCHEMA_NAME)); admin.set(b); Bean result = admin.get(b.getId()).get(); assertThat(result.getPropertyNames().size(), is(0)); }