Search in sources :

Example 11 with FieldRecord

use of org.motechproject.mds.web.domain.FieldRecord 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 12 with FieldRecord

use of org.motechproject.mds.web.domain.FieldRecord in project motech by motech.

the class InstanceServiceImpl method getInstanceValueAsRelatedField.

@Override
public FieldRecord getInstanceValueAsRelatedField(Long entityId, Long fieldId, Long instanceId) {
    validateCredentialsForReading(getEntity(entityId));
    try {
        FieldRecord fieldRecord;
        FieldDto field = entityService.getEntityFieldById(entityId, fieldId);
        MotechDataService service = DataServiceHelper.getDataService(bundleContext, field.getMetadata(RELATED_CLASS).getValue());
        Object instance = service.findById(instanceId);
        if (instance == null) {
            throw new ObjectNotFoundException(service.getClassType().getName(), instanceId);
        }
        List<FieldDto> relatedEntityFields = getEntityFieldsByClassName(field.getMetadata(RELATED_CLASS).getValue());
        fieldRecord = new FieldRecord(field);
        fieldRecord.setValue(parseValueForDisplay(instance, relatedEntityFields));
        fieldRecord.setDisplayValue(instance.toString());
        return fieldRecord;
    } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
        throw new ObjectReadException(entityId, e);
    }
}
Also used : ObjectNotFoundException(org.motechproject.mds.exception.object.ObjectNotFoundException) BasicFieldRecord(org.motechproject.mds.web.domain.BasicFieldRecord) FieldRecord(org.motechproject.mds.web.domain.FieldRecord) ObjectReadException(org.motechproject.mds.exception.object.ObjectReadException) MotechDataService(org.motechproject.mds.service.MotechDataService) InvocationTargetException(java.lang.reflect.InvocationTargetException) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 13 with FieldRecord

use of org.motechproject.mds.web.domain.FieldRecord 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 14 with FieldRecord

use of org.motechproject.mds.web.domain.FieldRecord in project motech by motech.

the class FieldTestHelper method fieldRecord.

public static FieldRecord fieldRecord(TypeDto type, String name, String displayName, Object value) {
    FieldRecord record = new FieldRecord(name, value, type);
    record.setDisplayName(displayName);
    return record;
}
Also used : FieldRecord(org.motechproject.mds.web.domain.FieldRecord)

Example 15 with FieldRecord

use of org.motechproject.mds.web.domain.FieldRecord 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));
}
Also used : BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) EntityRecord(org.motechproject.mds.web.domain.EntityRecord) EntityDto(org.motechproject.mds.dto.EntityDto) RelationshipsUpdate(org.motechproject.mds.web.domain.RelationshipsUpdate) FieldRecord(org.motechproject.mds.web.domain.FieldRecord) BasicFieldRecord(org.motechproject.mds.web.domain.BasicFieldRecord) Matchers.anyString(org.mockito.Matchers.anyString) DefaultMotechDataService(org.motechproject.mds.service.DefaultMotechDataService) MotechDataService(org.motechproject.mds.service.MotechDataService) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

FieldRecord (org.motechproject.mds.web.domain.FieldRecord)24 BasicFieldRecord (org.motechproject.mds.web.domain.BasicFieldRecord)19 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)16 EntityRecord (org.motechproject.mds.web.domain.EntityRecord)15 Test (org.junit.Test)14 EntityDto (org.motechproject.mds.dto.EntityDto)7 FieldDto (org.motechproject.mds.dto.FieldDto)6 List (java.util.List)3 Matchers.anyString (org.mockito.Matchers.anyString)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 Arrays.asList (java.util.Arrays.asList)2 SettingDto (org.motechproject.mds.dto.SettingDto)2 ObjectNotFoundException (org.motechproject.mds.exception.object.ObjectNotFoundException)2 ObjectReadException (org.motechproject.mds.exception.object.ObjectReadException)2 MotechDataService (org.motechproject.mds.service.MotechDataService)2 DateFormat (java.text.DateFormat)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1