use of org.motechproject.mds.web.domain.EntityRecord in project motech by motech.
the class InstanceServiceImpl method instanceToRecord.
private <T extends BasicEntityRecord> T instanceToRecord(Object instance, EntityDto entityDto, List<FieldDto> fields, MotechDataService service, EntityType entityType, Class<T> clazz, Map<String, List<FieldDto>> relatedEntitiesFields) {
if (instance == null) {
return null;
}
try {
List fieldRecords = new ArrayList<>();
boolean basic = BasicEntityRecord.class.equals(clazz);
for (FieldDto field : fields) {
if (entityType != EntityType.STANDARD && field.isVersionField()) {
continue;
}
Object value = getProperty(instance, field, service);
Object displayValue = DisplayHelper.getDisplayValueForField(field, value, MAX_LENGTH);
value = parseValueForDisplay(value, relatedEntitiesFields.get(field.getMetadata(Constants.MetadataKeys.RELATED_CLASS)));
BasicFieldRecord fieldRecord = basic ? new BasicFieldRecord(field) : new FieldRecord(field);
fieldRecord.setValue(value);
fieldRecord.setDisplayValue(displayValue);
fieldRecords.add(fieldRecord);
}
Number id = (Number) PropertyUtil.safeGetProperty(instance, ID_FIELD_NAME);
Long parsedId = id == null ? null : id.longValue();
return (T) (basic ? new BasicEntityRecord(parsedId, fieldRecords) : new EntityRecord(parsedId, entityDto.getId(), fieldRecords));
} catch (Exception e) {
throw new ObjectReadException(entityDto.getName(), e);
}
}
use of org.motechproject.mds.web.domain.EntityRecord in project motech by motech.
the class InstanceController method getInstance.
@RequestMapping(value = "/instances/{entityId}/instance/{instanceId}", method = RequestMethod.GET)
@ResponseBody
public EntityRecord getInstance(@PathVariable Long entityId, @PathVariable Long instanceId) {
EntityRecord entityRecord = instanceService.getEntityInstance(entityId, instanceId);
processFieldsForUI(entityRecord);
return entityRecord;
}
use of org.motechproject.mds.web.domain.EntityRecord in project motech by motech.
the class InstanceController method deleteBlobContent.
@RequestMapping(value = "/instances/deleteBlob/{entityId}/{instanceId}/{fieldId}", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public void deleteBlobContent(@PathVariable Long entityId, @PathVariable Long instanceId, @PathVariable Long fieldId) {
EntityRecord record = instanceService.getEntityInstance(entityId, instanceId);
instanceService.saveInstance(record, fieldId);
}
use of org.motechproject.mds.web.domain.EntityRecord in project motech by motech.
the class InstanceController method getSingleTrashInstance.
@RequestMapping(value = "/entities/{entityId}/trash/{instanceId}", method = RequestMethod.GET)
@ResponseBody
public EntityRecord getSingleTrashInstance(@PathVariable Long entityId, @PathVariable Long instanceId) {
EntityRecord entityRecord = instanceService.getSingleTrashRecord(entityId, instanceId);
processFieldsForUI(entityRecord);
return entityRecord;
}
use of org.motechproject.mds.web.domain.EntityRecord in project motech by motech.
the class InstanceServiceTest method shouldUpdateRelatedFields.
@Test
public void shouldUpdateRelatedFields() {
TestSample test1 = new TestSample("someString", 4);
TestSample test2 = new TestSample("otherString", 5);
TestSample test3 = new TestSample("sample", 6);
RelationshipsUpdate oneToOneUpdate = new RelationshipsUpdate();
oneToOneUpdate.getAddedIds().add(6L);
RelationshipsUpdate oneToManyUpdate = buildRelationshipUpdate();
List<FieldRecord> fieldRecords = asList(FieldTestHelper.fieldRecord("title", String.class.getName(), "String field", "Default"), FieldTestHelper.fieldRecord(TypeDto.ONE_TO_MANY_RELATIONSHIP, "testSamples", "Related field", oneToManyUpdate), FieldTestHelper.fieldRecord(TypeDto.ONE_TO_ONE_RELATIONSHIP, "testSample", "Other Related field", oneToOneUpdate));
EntityRecord entityRecord = new EntityRecord(null, ANOTHER_ENTITY_ID, fieldRecords);
mockSampleFields();
mockDataService();
mockEntity();
EntityDto entityWithRelatedField = mock(EntityDto.class);
when(entityService.getEntity(ANOTHER_ENTITY_ID)).thenReturn(entityWithRelatedField);
when(entityWithRelatedField.getClassName()).thenReturn(AnotherSample.class.getName());
when(entityWithRelatedField.getId()).thenReturn(ENTITY_ID + 1);
ServiceReference serviceReferenceForClassWithRelatedField = mock(ServiceReference.class);
MotechDataService serviceForClassWithRelatedField = mock(MotechDataService.class);
when(bundleContext.getServiceReference(ClassName.getInterfaceName(AnotherSample.class.getName()))).thenReturn(serviceReferenceForClassWithRelatedField);
when(bundleContext.getService(serviceReferenceForClassWithRelatedField)).thenReturn(serviceForClassWithRelatedField);
when(motechDataService.findById(4L)).thenReturn(test1);
when(motechDataService.findById(5L)).thenReturn(test2);
when(motechDataService.findById(6L)).thenReturn(test3);
when(motechDataService.findByIds(oneToManyUpdate.getAddedIds())).thenReturn(Arrays.asList(test1, test2));
when(entityService.getEntityFieldsForUI(ANOTHER_ENTITY_ID)).thenReturn(asList(FieldTestHelper.fieldDto(5L, "title", String.class.getName(), "String field", "Default"), FieldTestHelper.fieldDto(6L, "testSamples", TypeDto.ONE_TO_MANY_RELATIONSHIP.getTypeClass(), "Related field", null)));
ArgumentCaptor<AnotherSample> captor = ArgumentCaptor.forClass(AnotherSample.class);
instanceService.saveInstance(entityRecord, null);
verify(serviceForClassWithRelatedField).create(captor.capture());
AnotherSample capturedValue = captor.getValue();
assertEquals(capturedValue.getTestSample(), test3);
assertEquals(capturedValue.getTestSamples().size(), 3);
assertEquals(capturedValue.getTitle(), "Default");
assertTrue(capturedValue.getTestSamples().contains(test1));
assertFalse(capturedValue.getTestSamples().contains(test3));
assertTrue(capturedValue.getTestSamples().contains(test2));
}
Aggregations