CompareUtils { public static List<String> compare(Object oldEntity, Object newEntity, EntityModel<?> model, EntityModelFactory entityModelFactory, MessageService messageService, String... ignore) { List<String> results = new ArrayList<>(); Set<String> toIgnore = new HashSet<>(); if (ignore != null) { toIgnore = Sets.newHashSet(ignore); } toIgnore.addAll(EntityModelUtils.ALWAYS_IGNORE); String noValue = messageService.getMessage("ocs.no.value", VaadinUtils.getLocale()); for (AttributeModel am : model.getAttributeModels()) { if ((AttributeType.BASIC.equals(am.getAttributeType()) || AttributeType.MASTER.equals(am.getAttributeType())) && !toIgnore.contains(am.getName())) { Object oldValue = ClassUtils.getFieldValue(oldEntity, am.getName()); Object newValue = ClassUtils.getFieldValue(newEntity, am.getName()); if (!Objects.equals(oldValue, newValue)) { String oldValueStr = FormatUtils.formatPropertyValue(entityModelFactory, am, oldValue, ", "); String newValueStr = FormatUtils.formatPropertyValue(entityModelFactory, am, newValue, ", "); results.add(messageService.getMessage("ocs.value.changed", VaadinUtils.getLocale(), am.getDisplayName(VaadinUtils.getLocale()), oldValue == null ? noValue : oldValueStr, newValue == null ? noValue : newValueStr)); } } else if (AttributeType.DETAIL.equals(am.getAttributeType())) { Collection<?> ocol = (Collection<?>) ClassUtils.getFieldValue(oldEntity, am.getName()); Collection<?> ncol = (Collection<?>) ClassUtils.getFieldValue(newEntity, am.getName()); for (Object o : ncol) { if (!ocol.contains(o)) { results.add(messageService.getMessage("ocs.value.added", VaadinUtils.getLocale(), getDescription(o, am.getNestedEntityModel()), am.getDisplayName(VaadinUtils.getLocale()))); } } for (Object o : ocol) { if (!ncol.contains(o)) { results.add(messageService.getMessage("ocs.value.removed", VaadinUtils.getLocale(), getDescription(o, am.getNestedEntityModel()), am.getDisplayName(VaadinUtils.getLocale()))); } } for (Object o : ocol) { for (Object o2 : ncol) { if (o.equals(o2)) { List<String> nested = compare(o, o2, am.getNestedEntityModel(), entityModelFactory, messageService, ignore); results.addAll(nested); } } } } } return results; } private  CompareUtils(); static List<String> compare(Object oldEntity, Object newEntity, EntityModel<?> model,
			EntityModelFactory entityModelFactory, MessageService messageService, String... ignore);  }
@Test public void testCompare() { TestEntity e1 = new TestEntity(); TestEntity e2 = new TestEntity(); List<String> changes = CompareUtils.compare(e1, e2, factory.getModel(TestEntity.class), factory, messageService); assertEquals(0, changes.size()); e1.setAge(12L); changes = CompareUtils.compare(e1, e2, factory.getModel(TestEntity.class), factory, messageService); assertEquals(1, changes.size()); assertEquals("ocs.value.changed", changes.get(0)); e1.setAge(null); e2.setName("Kevin"); changes = CompareUtils.compare(e1, e2, factory.getModel(TestEntity.class), factory, messageService); assertEquals(1, changes.size()); assertEquals("ocs.value.changed", changes.get(0)); }
@Test public void testCompare_Ignore() { TestEntity e1 = new TestEntity(); TestEntity e2 = new TestEntity(); e1.setName("Bob"); List<String> changes = CompareUtils.compare(e1, e2, factory.getModel(TestEntity.class), factory, messageService); assertEquals(1, changes.size()); changes = CompareUtils.compare(e1, e2, factory.getModel(TestEntity.class), factory, messageService, "name"); assertEquals(0, changes.size()); }
@Test public void testCompare_CollectionRemove() { TestEntity e1 = new TestEntity(); TestEntity e2 = new TestEntity(); e1.addTestEntity2(new TestEntity2()); List<String> changes = CompareUtils.compare(e1, e2, factory.getModel(TestEntity.class), factory, messageService); assertEquals(1, changes.size()); assertEquals("ocs.value.removed", changes.get(0)); }
@Test public void testCompare_CollectionAdd() { TestEntity e1 = new TestEntity(); TestEntity e2 = new TestEntity(); e2.addTestEntity2(new TestEntity2()); List<String> changes = CompareUtils.compare(e1, e2, factory.getModel(TestEntity.class), factory, messageService); assertEquals(1, changes.size()); assertEquals("ocs.value.added", changes.get(0)); }