Search in sources :

Example 1 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class MdsDummyDataGeneratorImpl method makeDummyInstance.

@Override
public Object makeDummyInstance(Long entityId) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
    String className = entityService.getEntity(entityId).getClassName();
    Class objectClass = MDSClassLoader.getInstance().loadClass(className);
    Object obj = objectClass.newInstance();
    for (FieldDto fieldDto : entityService.getEntityFields(entityId)) {
        String fieldName = fieldDto.getBasic().getName();
        if (!ArrayUtils.contains(GENERATED_FIELD_NAMES, fieldName)) {
            PropertyUtil.safeSetProperty(obj, fieldName, randomValueFor(fieldDto.getType()));
        }
    }
    return obj;
}
Also used : FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 2 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class MdsDummyDataGeneratorImpl method prepareDummyEntity.

private void prepareDummyEntity(int number, int fieldsPerEntity, int lookupsPerEntity) throws IOException {
    EntityDto entityDto = new EntityDto(Long.valueOf(number), entityPrefix.concat(String.valueOf(number)));
    entityDto = entityService.createEntity(entityDto);
    List<FieldDto> fields = new ArrayList<>();
    for (int i = 0; i < fieldsPerEntity; i++) {
        TypeDto type = pickRandomFieldType();
        fields.add(new FieldDto(null, entityDto.getId(), type, new FieldBasicDto(fieldPrefix.concat(String.valueOf(i)), fieldPrefix.concat(String.valueOf(i))), false, null, null, settingsFor(type), null));
    }
    entityService.addFields(entityDto, fields);
    List<LookupDto> lookups = new ArrayList<>();
    for (int i = 0; i < lookupsPerEntity; i++) {
        List<LookupFieldDto> lookupFields = new ArrayList<>();
        List<FieldDto> entityFields = entityService.getFields(entityDto.getId());
        int amountOfFields = RAND.nextInt(entityFields.size());
        for (int j = 0; j < amountOfFields; j++) {
            lookupFields.add(new LookupFieldDto(null, entityFields.get(j).getBasic().getName(), LookupFieldType.VALUE));
        }
        lookups.add(new LookupDto(lookupPrefix.concat(String.valueOf(i)), RAND.nextBoolean(), RAND.nextBoolean(), lookupFields, false));
    }
    entityService.addLookups(entityDto.getId(), lookups);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) TypeDto(org.motechproject.mds.dto.TypeDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 3 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class FieldRecordTest method shouldHandleSingleSelectComboBoxes.

@Test
public void shouldHandleSingleSelectComboBoxes() {
    FieldDto fieldDto = FieldTestHelper.fieldDto(1L, "name", List.class.getName(), "disp", "[one, two]");
    fieldDto.setSettings(asList(new SettingDto(COMBOBOX_VALUES, asList("one", "two", "three"), null, null)));
    FieldRecord fieldRecord = new FieldRecord(fieldDto);
    fieldRecord.setValue("[two]");
    assertEquals("two", fieldRecord.getValue());
    fieldRecord.setValue("test");
    assertEquals("test", fieldRecord.getValue());
    // test with enum
    fieldRecord.setValue(TestEnum.TWO);
    assertEquals("TWO", fieldRecord.getValue());
    fieldRecord.setValue(asList(TestEnum.ONE));
    assertEquals("ONE", fieldRecord.getValue());
    fieldRecord.setValue(null);
    assertNull(fieldRecord.getValue());
}
Also used : List(java.util.List) Arrays.asList(java.util.Arrays.asList) SettingDto(org.motechproject.mds.dto.SettingDto) FieldRecord(org.motechproject.mds.web.domain.FieldRecord) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 4 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class EntityControllerTest method shouldSaveTemporaryChange.

@Test
public void shouldSaveTemporaryChange() throws Exception {
    DraftData data = new DraftData();
    data.setEdit(true);
    data.setValues(new HashMap<>());
    data.getValues().put(DraftData.PATH, "basic.displayName");
    data.getValues().put(DraftData.FIELD_ID, "2");
    data.getValues().put(DraftData.VALUE, singletonList("test"));
    ResultActions actions = controller.perform(get("/entities/9007")).andExpect(status().isOk());
    EntityDto entity = new ObjectMapper().readValue(actions.andReturn().getResponse().getContentAsByteArray(), EntityDto.class);
    actions = controller.perform(get("/entities/9007/fields")).andExpect(status().isOk());
    List<FieldDto> fields = new ObjectMapper().readValue(actions.andReturn().getResponse().getContentAsByteArray(), new TypeReference<List<FieldDto>>() {
    });
    FieldDto fieldDto = findFieldById(fields, 12L);
    // before change
    assertFalse(entity.isModified());
    assertNotNull(fieldDto);
    assertEquals("ID", fieldDto.getBasic().getDisplayName());
    // change
    controller.perform(post("/entities/9007/draft").body(new ObjectMapper().writeValueAsBytes(data)).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
    verify(entityService).saveDraftEntityChanges(eq(9007L), draftDataCaptor.capture());
    DraftData captured = draftDataCaptor.getValue();
    assertTrue(captured.isEdit());
    assertEquals("basic.displayName", captured.getPath());
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) DraftData(org.motechproject.mds.dto.DraftData) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ResultActions(org.springframework.test.web.server.ResultActions) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 5 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class InstanceServiceTest method shouldNotAutoPopulateOwnerAndCreatorForNonEditableFields.

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

Aggregations

FieldDto (org.motechproject.mds.dto.FieldDto)158 Test (org.junit.Test)61 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)60 EntityDto (org.motechproject.mds.dto.EntityDto)53 ArrayList (java.util.ArrayList)47 LookupDto (org.motechproject.mds.dto.LookupDto)29 List (java.util.List)23 MetadataDto (org.motechproject.mds.dto.MetadataDto)22 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)19 TypeDto (org.motechproject.mds.dto.TypeDto)17 Method (java.lang.reflect.Method)14 MotechDataService (org.motechproject.mds.service.MotechDataService)14 HashMap (java.util.HashMap)12 Field (org.motechproject.mds.domain.Field)12 FieldTestHelper.lookupFieldDto (org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto)12 Arrays.asList (java.util.Arrays.asList)11 SettingDto (org.motechproject.mds.dto.SettingDto)11 Before (org.junit.Before)7 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)7 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)7