@Override @Transactional public Object saveInstance(EntityRecord entityRecord, Long deleteValueFieldId) { EntityDto entity = getEntity(entityRecord.getEntitySchemaId()); validateCredentials(entity); validateNonEditableProperty(entity); List<FieldDto> entityFields = getEntityFields(entityRecord.getEntitySchemaId()); try { MotechDataService service = getServiceForEntity(entity); Class<?> entityClass = getEntityClass(entity); boolean newObject = entityRecord.getId() == null; Object instance; if (newObject) { instance = entityClass.newInstance(); for (FieldDto entityField : entityFields) { if (entityField.getType().isMap() && entityField.getBasic().getDefaultValue() != null) { setInstanceFieldMap(instance, entityField); } } } else { instance = service.retrieve(ID_FIELD_NAME, entityRecord.getId()); if (instance == null) { throw new ObjectNotFoundException(entity.getName(), entityRecord.getId()); } } updateFields(instance, entityRecord.getFields(), service, deleteValueFieldId, !newObject); if (newObject) { return service.create(instance); } else { return service.update(instance); } } catch (Exception e) { if (entityRecord.getId() == null) { throw new ObjectCreateException(entity.getName(), e); } else { throw new ObjectUpdateException(entity.getName(), entityRecord.getId(), e); } } }
@Test public void shouldAutoPopulateOwnerAndCreator() { when(entityService.getEntityFieldsForUI(ENTITY_ID)) .thenReturn( asList( FieldTestHelper.fieldDto(1L, "owner", String.class.getName(), "String field", null), FieldTestHelper.fieldDto( 1L, "creator", String.class.getName(), "String field", null))); mockEntity(); setUpSecurityContext(); EntityRecord record = instanceService.newInstance(ENTITY_ID); List<FieldRecord> fieldRecords = record.getFields(); assertEquals( asList("motech", "motech"), extract(fieldRecords, on(FieldRecord.class).getValue())); }
@Test public void shouldReturnNewInstances() { mockSampleFields(); mockEntity(); EntityRecord record = instanceService.newInstance(ENTITY_ID); assertNotNull(record); assertEquals(Long.valueOf(ENTITY_ID), record.getEntitySchemaId()); assertNull(record.getId()); List<FieldRecord> fieldRecords = record.getFields(); assertCommonFieldRecordFields(fieldRecords); assertEquals( asList("Default", 7, null, null, null), extract(fieldRecords, on(FieldRecord.class).getValue())); }
private HistoryRecord convertToHistoryRecord( Object object, EntityDto entity, Long instanceId, MotechDataService service) { Long entityId = entity.getId(); EntityRecord entityRecord = instanceToRecord(object, entity, entityService.getEntityFields(entityId), service); Long historyInstanceSchemaVersion = (Long) PropertyUtil.safeGetProperty( object, HistoryTrashClassHelper.schemaVersion(object.getClass())); Long currentSchemaVersion = entityService.getCurrentSchemaVersion(entity.getClassName()); return new HistoryRecord( entityRecord.getId(), instanceId, historyInstanceSchemaVersion.equals(currentSchemaVersion), entityRecord.getFields()); }
@Test public void shouldNotAutoPopulateOwnerAndCreatorForNonEditableFields() { FieldDto ownerField = FieldTestHelper.fieldDto(1L, "owner", String.class.getName(), "String field", null); ownerField.setNonEditable(true); FieldDto creatorField = FieldTestHelper.fieldDto(1L, "creator", String.class.getName(), "String field", null); creatorField.setNonEditable(true); when(entityService.getEntityFieldsForUI(ENTITY_ID)) .thenReturn(asList(ownerField, creatorField)); mockEntity(); setUpSecurityContext(); EntityRecord record = instanceService.newInstance(ENTITY_ID); List<FieldRecord> fieldRecords = record.getFields(); assertEquals(asList(null, null), extract(fieldRecords, on(FieldRecord.class).getValue())); }
@Test public void shouldReturnEntityInstance() { mockDataService(); mockSampleFields(); mockEntity(); when(motechDataService.retrieve("id", INSTANCE_ID)) .thenReturn(new TestSample("Hello world", 99)); EntityRecord record = instanceService.getEntityInstance(ENTITY_ID, INSTANCE_ID); assertNotNull(record); assertEquals(Long.valueOf(ENTITY_ID), record.getEntitySchemaId()); assertEquals(Long.valueOf(INSTANCE_ID), record.getId()); List<FieldRecord> fieldRecords = record.getFields(); assertCommonFieldRecordFields(fieldRecords); assertEquals( asList("Hello world", 99, null, null, null), extract(fieldRecords, on(FieldRecord.class).getValue())); }