EntityModelUtils { public static <T> void copySimpleAttributes(T source, T target, EntityModel<T> model, String... ignore) { Set<String> toIgnore = new HashSet<>(); if (ignore != null) { toIgnore = Sets.newHashSet(ignore); } toIgnore.addAll(ALWAYS_IGNORE); for (AttributeModel am : model.getAttributeModels()) { if ((AttributeType.BASIC.equals(am.getAttributeType()) || AttributeType.LOB.equals(am.getAttributeType())) && !toIgnore.contains(am.getName())) { if (!DynamoConstants.ID.equals(am.getName())) { Object value = ClassUtils.getFieldValue(source, am.getName()); if (ClassUtils.canSetProperty(target, am.getName())) { if (value != null) { ClassUtils.setFieldValue(target, am.getName(), value); } else { ClassUtils.clearFieldValue(target, am.getName(), am.getType()); } } } } } } private  EntityModelUtils(); static void copySimpleAttributes(T source, T target, EntityModel<T> model, String... ignore); static String getDisplayPropertyValue(Collection<T> entities, EntityModel<T> model, int maxItems,
			MessageService messageService, Locale locale); static String getDisplayPropertyValue(T entity, EntityModel<T> model); static final Set<String> ALWAYS_IGNORE; }
@Test public void testCopySimpleAttributes_Ignore() { TestEntity source = new TestEntity(); source.setAge(12L); source.setName("Name"); source.setDiscount(BigDecimal.valueOf(12.0)); source.setSomeBoolean(Boolean.TRUE); source.setId(24); TestEntity target = new TestEntity(); EntityModelUtils.copySimpleAttributes(source, target, factory.getModel(TestEntity.class), "name"); assertNull(target.getName()); }
@Test public void testCopySimpleAttributes() { TestEntity2 source = new TestEntity2(); source.setId(1); source.setName("Name"); source.setTestEntity(new TestEntity()); TestEntity2 target = new TestEntity2(); EntityModelUtils.copySimpleAttributes(source, target, factory.getModel(TestEntity2.class)); assertEquals("Name", target.getName()); assertNull(target.getId()); assertNull(target.getTestEntity()); }
@Test public void testCopySimpleAttributes2() { TestEntity source = new TestEntity(); source.setAge(12L); source.setName("Name"); source.setDiscount(BigDecimal.valueOf(12.0)); source.setSomeBoolean(Boolean.TRUE); source.setId(24); TestEntity target = new TestEntity(); EntityModelUtils.copySimpleAttributes(source, target, factory.getModel(TestEntity.class)); assertEquals("Name", target.getName()); assertEquals(12L, target.getAge().longValue()); assertEquals(12.0, target.getDiscount().longValue(), 0.001); assertEquals(Boolean.TRUE, target.getSomeBoolean()); assertNull(target.getId()); }