use of org.motechproject.mds.web.domain.BasicFieldRecord 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.BasicFieldRecord in project motech by motech.
the class InstanceControllerTest method testRecordAsEntityRecord.
private BasicEntityRecord testRecordAsEntityRecord(String name, int val, long id) {
BasicFieldRecord nameField = new FieldRecord("name", name, TypeDto.STRING);
BasicFieldRecord valField = new FieldRecord("val", val, TypeDto.INTEGER);
return new BasicEntityRecord(id, asList(nameField, valField));
}
use of org.motechproject.mds.web.domain.BasicFieldRecord in project motech by motech.
the class InstanceServiceTest method shouldHandleNullParamsForLookups.
@Test
public void shouldHandleNullParamsForLookups() {
mockSampleFields();
mockEntity();
mockLookups();
mockLookupService();
Map<String, Object> lookupMap = new HashMap<>();
lookupMap.put("dtField", null);
List<BasicEntityRecord> result = instanceService.getEntityRecordsFromLookup(ENTITY_ID, TestDataService.NULL_EXPECTING_LOOKUP_NAME, lookupMap, queryParams());
assertNotNull(result);
assertEquals(2, result.size());
BasicEntityRecord entityRecord = result.get(0);
List<? extends BasicFieldRecord> fieldRecords = entityRecord.getFields();
assertCommonBasicFieldRecordFields(fieldRecords);
assertEquals(asList("three", 3, null, null, null), extract(fieldRecords, on(FieldRecord.class).getValue()));
entityRecord = result.get(1);
fieldRecords = entityRecord.getFields();
assertCommonBasicFieldRecordFields(fieldRecords);
assertEquals(asList("four", 4, null, null, null), extract(fieldRecords, on(FieldRecord.class).getValue()));
}
use of org.motechproject.mds.web.domain.BasicFieldRecord in project motech by motech.
the class InstanceServiceTest method shouldRetrieveInstancesBasedOnASingleReturnLookup.
@Test
public void shouldRetrieveInstancesBasedOnASingleReturnLookup() {
mockSampleFields();
mockEntity();
mockLookups();
mockLookupService();
Map<String, Object> lookupMap = new HashMap<>();
lookupMap.put("strField", TestDataService.LOOKUP_1_EXPECTED_PARAM);
List<BasicEntityRecord> result = instanceService.getEntityRecordsFromLookup(ENTITY_ID, TestDataService.LOOKUP_1_NAME, lookupMap, queryParams());
assertNotNull(result);
assertEquals(1, result.size());
BasicEntityRecord entityRecord = result.get(0);
List<? extends BasicFieldRecord> fieldRecords = entityRecord.getFields();
assertCommonBasicFieldRecordFields(fieldRecords);
assertEquals(asList("strField", 6, null, null, null), extract(fieldRecords, on(FieldRecord.class).getValue()));
}
use of org.motechproject.mds.web.domain.BasicFieldRecord in project motech by motech.
the class InstanceServiceTest method shouldRetrieveInstancesBasedOnAMultiReturnLookup.
@Test
public void shouldRetrieveInstancesBasedOnAMultiReturnLookup() {
mockSampleFields();
mockEntity();
mockLookups();
mockLookupService();
Map<String, Object> lookupMap = new HashMap<>();
lookupMap.put("strField", TestDataService.LOOKUP_2_EXPECTED_PARAM);
List<BasicEntityRecord> result = instanceService.getEntityRecordsFromLookup(ENTITY_ID, TestDataService.LOOKUP_2_NAME, lookupMap, queryParams());
assertNotNull(result);
assertEquals(2, result.size());
BasicEntityRecord entityRecord = result.get(0);
List<? extends BasicFieldRecord> fieldRecords = entityRecord.getFields();
assertCommonBasicFieldRecordFields(fieldRecords);
assertEquals(asList("one", 1, null, null, null), extract(fieldRecords, on(FieldRecord.class).getValue()));
entityRecord = result.get(1);
fieldRecords = entityRecord.getFields();
assertCommonBasicFieldRecordFields(fieldRecords);
assertEquals(asList("two", 2, null, null, null), extract(fieldRecords, on(FieldRecord.class).getValue()));
}
Aggregations