Search in sources :

Example 1 with BasicFieldRecord

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);
    }
}
Also used : ArrayList(java.util.ArrayList) BasicFieldRecord(org.motechproject.mds.web.domain.BasicFieldRecord) FieldRecord(org.motechproject.mds.web.domain.FieldRecord) ObjectReadException(org.motechproject.mds.exception.object.ObjectReadException) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) ObjectReadException(org.motechproject.mds.exception.object.ObjectReadException) FieldReadOnlyException(org.motechproject.mds.exception.field.FieldReadOnlyException) InvocationTargetException(java.lang.reflect.InvocationTargetException) LookupNotFoundException(org.motechproject.mds.exception.lookup.LookupNotFoundException) ObjectUpdateException(org.motechproject.mds.exception.object.ObjectUpdateException) LookupExecutorException(org.motechproject.mds.exception.lookup.LookupExecutorException) SecurityException(org.motechproject.mds.exception.object.SecurityException) EntityNotFoundException(org.motechproject.mds.exception.entity.EntityNotFoundException) LookupExecutionException(org.motechproject.mds.exception.lookup.LookupExecutionException) ObjectCreateException(org.motechproject.mds.exception.object.ObjectCreateException) FieldNotFoundException(org.motechproject.mds.exception.field.FieldNotFoundException) ObjectNotFoundException(org.motechproject.mds.exception.object.ObjectNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) EntityInstancesNonEditableException(org.motechproject.mds.exception.entity.EntityInstancesNonEditableException) CannotCompileException(javassist.CannotCompileException) BasicFieldRecord(org.motechproject.mds.web.domain.BasicFieldRecord) EntityRecord(org.motechproject.mds.web.domain.EntityRecord) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) ArrayList(java.util.ArrayList) List(java.util.List) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 2 with BasicFieldRecord

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));
}
Also used : FieldRecord(org.motechproject.mds.web.domain.FieldRecord) BasicFieldRecord(org.motechproject.mds.web.domain.BasicFieldRecord) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) BasicFieldRecord(org.motechproject.mds.web.domain.BasicFieldRecord)

Example 3 with BasicFieldRecord

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()));
}
Also used : HashMap(java.util.HashMap) FieldRecord(org.motechproject.mds.web.domain.FieldRecord) BasicFieldRecord(org.motechproject.mds.web.domain.BasicFieldRecord) Matchers.anyString(org.mockito.Matchers.anyString) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) Test(org.junit.Test)

Example 4 with BasicFieldRecord

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()));
}
Also used : HashMap(java.util.HashMap) FieldRecord(org.motechproject.mds.web.domain.FieldRecord) BasicFieldRecord(org.motechproject.mds.web.domain.BasicFieldRecord) Matchers.anyString(org.mockito.Matchers.anyString) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) Test(org.junit.Test)

Example 5 with BasicFieldRecord

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()));
}
Also used : HashMap(java.util.HashMap) FieldRecord(org.motechproject.mds.web.domain.FieldRecord) BasicFieldRecord(org.motechproject.mds.web.domain.BasicFieldRecord) Matchers.anyString(org.mockito.Matchers.anyString) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) Test(org.junit.Test)

Aggregations

BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)5 BasicFieldRecord (org.motechproject.mds.web.domain.BasicFieldRecord)5 FieldRecord (org.motechproject.mds.web.domain.FieldRecord)5 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 Matchers.anyString (org.mockito.Matchers.anyString)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CannotCompileException (javassist.CannotCompileException)1 InstanceNotFoundException (javax.management.InstanceNotFoundException)1 FieldDto (org.motechproject.mds.dto.FieldDto)1 EntityInstancesNonEditableException (org.motechproject.mds.exception.entity.EntityInstancesNonEditableException)1 EntityNotFoundException (org.motechproject.mds.exception.entity.EntityNotFoundException)1 FieldNotFoundException (org.motechproject.mds.exception.field.FieldNotFoundException)1 FieldReadOnlyException (org.motechproject.mds.exception.field.FieldReadOnlyException)1 LookupExecutionException (org.motechproject.mds.exception.lookup.LookupExecutionException)1 LookupExecutorException (org.motechproject.mds.exception.lookup.LookupExecutorException)1 LookupNotFoundException (org.motechproject.mds.exception.lookup.LookupNotFoundException)1 ObjectCreateException (org.motechproject.mds.exception.object.ObjectCreateException)1