CollectionValueMerger implements ValueMerger { @Override public void mergeValue(FieldSpecification<?, ?> att, Object target, Object originalValue, Object mergedValue, Descriptor attributeDescriptor) { final Collection<?> mergedCol = (Collection<?>) mergedValue; if (mergedCol == null) { EntityPropertiesUtils.setFieldValue(att.getJavaField(), target, null); return; } final Collection<Object> newValue = CollectionFactory .createDefaultCollection(PluralAttribute.CollectionType.fromClass(att.getJavaType())); boolean elemTypeManaged = false; if (att instanceof PluralAttribute) { elemTypeManaged = uow.isEntityType(((PluralAttribute) att).getBindableJavaType()); } for (Object item : mergedCol) { newValue.add(elemTypeManaged ? managedTypeMerger.getValueToSet(item, attributeDescriptor) : item); } extendModuleExtractionSignature(att, newValue); EntityPropertiesUtils.setFieldValue(att.getJavaField(), target, newValue); } CollectionValueMerger(UnitOfWorkImpl uow, ManagedTypeValueMerger managedTypeMerger); @Override void mergeValue(FieldSpecification<?, ?> att, Object target, Object originalValue, Object mergedValue,
                           Descriptor attributeDescriptor);  }
@Test public void mergeMergesUpdatedValuesIntoOriginalSetOfNonManagedTypes() { final OWLClassA orig = Generators.generateOwlClassAInstance(); final OWLClassA merge = new OWLClassA(orig); final String addedType = Vocabulary.CLASS_BASE + "AddedType"; final String removedType = merge.getTypes().iterator().next(); merge.getTypes().remove(removedType); merge.getTypes().add(addedType); final EntityType<OWLClassA> et = metamodel.entity(OWLClassA.class); final FieldSpecification<? super OWLClassA, ?> typesSpec = et.getTypes(); merger.mergeValue(typesSpec, orig, orig.getTypes(), merge.getTypes(), descriptor); assertTrue(orig.getTypes().contains(addedType)); assertFalse(orig.getTypes().contains(removedType)); }
@Test public void mergeReplacesNullCollectionWithMergedOne() { final OWLClassA orig = Generators.generateOwlClassAInstance(); final OWLClassA merge = new OWLClassA(orig); orig.setTypes(null); final EntityType<OWLClassA> et = metamodel.entity(OWLClassA.class); final FieldSpecification<? super OWLClassA, ?> typesSpec = et.getTypes(); merger.mergeValue(typesSpec, orig, orig.getTypes(), merge.getTypes(), descriptor); assertNotNull(orig.getTypes()); assertTrue(orig.getTypes().equals(merge.getTypes())); }
@Test public void mergeReplacesOriginalCollectionWithNullWhenMergedHasNull() { final OWLClassA orig = Generators.generateOwlClassAInstance(); final OWLClassA merge = new OWLClassA(orig); merge.setTypes(null); final EntityType<OWLClassA> et = metamodel.entity(OWLClassA.class); final FieldSpecification<? super OWLClassA, ?> typesSpec = et.getTypes(); merger.mergeValue(typesSpec, orig, orig.getTypes(), merge.getTypes(), descriptor); assertNull(orig.getTypes()); }
@Test public void mergeLoadsReferencesUsedInMergedCollectionFromStorage() throws Exception { final OWLClassF orig = new OWLClassF(Generators.createIndividualIdentifier()); orig.setSimpleSet(IntStream.range(0, 10).mapToObj(i -> Generators.generateOwlClassAInstance()) .collect(Collectors.toSet())); orig.getSimpleSet() .forEach(a -> when(uow.readObject(OWLClassA.class, a.getUri(), descriptor)).thenReturn(new OWLClassA(a))); final OWLClassF merged = new OWLClassF(orig.getUri()); merged.setSimpleSet(new HashSet<>()); final Iterator<OWLClassA> it = orig.getSimpleSet().iterator(); int i = 0; while (it.hasNext()) { if (i % 2 == 0) { merged.getSimpleSet().add(it.next()); } else { it.next(); } i++; } for (int j = 0; j < Generators.randomPositiveInt(5); j++) { final OWLClassA a = Generators.generateOwlClassAInstance(); merged.getSimpleSet().add(a); when(uow.readObject(OWLClassA.class, a.getUri(), descriptor)).thenReturn(new OWLClassA(a)); } final EntityType<OWLClassF> et = metamodel.entity(OWLClassF.class); final FieldSpecification<? super OWLClassF, ?> att = et .getFieldSpecification(OWLClassF.getSimpleSetField().getName()); merger.mergeValue(att, orig, orig.getSimpleSet(), merged.getSimpleSet(), descriptor); assertEquals(merged.getSimpleSet().size(), orig.getSimpleSet().size()); merged.getSimpleSet().forEach(a -> verify(uow).readObject(OWLClassA.class, a.getUri(), descriptor)); }
@Test public void mergeReplacesOriginalCollectionWithEmptyOneWhenMergedCollectionIsEmpty() throws Exception { final OWLClassF orig = new OWLClassF(Generators.createIndividualIdentifier()); orig.setSimpleSet(IntStream.range(0, 10).mapToObj(i -> Generators.generateOwlClassAInstance()) .collect(Collectors.toSet())); final OWLClassF merged = new OWLClassF(orig.getUri()); merged.setSimpleSet(Collections.emptySet()); final EntityType<OWLClassF> et = metamodel.entity(OWLClassF.class); final FieldSpecification<? super OWLClassF, ?> att = et .getFieldSpecification(OWLClassF.getSimpleSetField().getName()); merger.mergeValue(att, orig, orig.getSimpleSet(), merged.getSimpleSet(), descriptor); assertNotNull(orig.getSimpleSet()); assertTrue(orig.getSimpleSet().isEmpty()); }
@Test public void mergeLoadsReferencesUsedInMergedListFromStorage() throws Exception { final OWLClassC orig = new OWLClassC(Generators.createIndividualIdentifier()); orig.setReferencedList(IntStream.range(0, 10).mapToObj(i -> Generators.generateOwlClassAInstance()) .collect(Collectors.toList())); orig.getReferencedList() .forEach(a -> when(uow.readObject(OWLClassA.class, a.getUri(), descriptor)).thenReturn(new OWLClassA(a))); final OWLClassC merged = new OWLClassC(orig.getUri()); merged.setReferencedList(new ArrayList<>()); for (int i = 0; i < orig.getReferencedList().size(); i++) { if (i % 3 == 0) { merged.getReferencedList().add(orig.getReferencedList().get(i)); } } for (int i = 0; i < Generators.randomPositiveInt(5); i++) { final OWLClassA a = Generators.generateOwlClassAInstance(); merged.getReferencedList().add(a); when(uow.readObject(OWLClassA.class, a.getUri(), descriptor)).thenReturn(new OWLClassA(a)); } final EntityType<OWLClassC> et = metamodel.entity(OWLClassC.class); final FieldSpecification<? super OWLClassC, ?> att = et .getFieldSpecification(OWLClassC.getRefListField().getName()); merger.mergeValue(att, orig, orig.getReferencedList(), merged.getReferencedList(), descriptor); assertEquals(merged.getReferencedList().size(), orig.getReferencedList().size()); merged.getReferencedList().forEach(a -> verify(uow).readObject(OWLClassA.class, a.getUri(), descriptor)); }