Search in sources :

Example 16 with FieldBasicDto

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

the class EntityControllerTest method shouldGetEntityFields.

@Test
public void shouldGetEntityFields() throws Exception {
    List<MetadataDto> exampleMetadata = new LinkedList<>();
    exampleMetadata.add(new MetadataDto("key1", "value1"));
    exampleMetadata.add(new MetadataDto("key2", "value2"));
    List<FieldDto> expected = new LinkedList<>();
    expected.add(new FieldDto(14L, 9005L, STRING, new FieldBasicDto("Other", "Other", false, false, "test", null, null), false, exampleMetadata, FieldValidationDto.STRING, null, null));
    ResultActions actions = controller.perform(get("/entities/9005/fields")).andExpect(status().isOk());
    List<FieldDto> fields = new ObjectMapper().readValue(actions.andReturn().getResponse().getContentAsByteArray(), new TypeReference<List<FieldDto>>() {
    });
    assertEquals(expected, fields);
}
Also used : FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) 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) MetadataDto(org.motechproject.mds.dto.MetadataDto) LinkedList(java.util.LinkedList) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 17 with FieldBasicDto

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

the class EntityControllerTest method shouldFindFieldByName.

@Test
public void shouldFindFieldByName() throws Exception {
    List<MetadataDto> exampleMetadata = new LinkedList<>();
    exampleMetadata.add(new MetadataDto("key1", "value1"));
    exampleMetadata.add(new MetadataDto("key2", "value2"));
    FieldDto expected = new FieldDto(14L, 9005L, STRING, new FieldBasicDto("Other", "Other", false, false, "test", null, null), false, exampleMetadata, FieldValidationDto.STRING, null, null);
    ResultActions actions = controller.perform(get("/entities/9005/fields/Other")).andExpect(status().isOk());
    FieldDto field = new ObjectMapper().readValue(actions.andReturn().getResponse().getContentAsByteArray(), FieldDto.class);
    assertEquals(expected, field);
}
Also used : FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) ResultActions(org.springframework.test.web.server.ResultActions) MetadataDto(org.motechproject.mds.dto.MetadataDto) LinkedList(java.util.LinkedList) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 18 with FieldBasicDto

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

the class MdsRestBundleIT method prepareEntity.

private void prepareEntity() throws IOException {
    EntityDto entityDto = new EntityDto(ENTITY_NAME);
    entityDto = entityService.createEntity(entityDto);
    FieldDto strField = new FieldDto(null, entityDto.getId(), TypeDto.STRING, new FieldBasicDto("strFieldDisp", "strField", true, false), false, null);
    FieldDto intField = new FieldDto(null, entityDto.getId(), TypeDto.INTEGER, new FieldBasicDto("intFieldDisp", "intField"), false, null);
    entityService.addFields(entityDto, asList(strField, intField));
    RestOptionsDto restOptions = new RestOptionsDto(true, true, true, true, false);
    restOptions.setFieldNames(prepareAllRestFieldNames(entityService.getEntityFields(entityDto.getId())));
    entityService.updateRestOptions(entityDto.getId(), restOptions);
    // a set based lookup for our convenience
    LookupFieldDto intSetLookupField = new LookupFieldDto(null, "intField", LookupFieldType.SET);
    LookupDto setLookup = new LookupDto("byIntSet", false, true, asList(intSetLookupField), false);
    // list return REST lookup
    LookupFieldDto intLookupField = new LookupFieldDto(null, "intField", LookupFieldType.VALUE);
    LookupDto listLookup = new LookupDto("byInt", false, true, asList(intLookupField), false);
    // single return REST lookup
    LookupFieldDto strLookupField = new LookupFieldDto(null, "strField", LookupFieldType.VALUE);
    LookupDto singleLookup = new LookupDto("byStr", true, true, asList(strLookupField), false);
    entityService.addLookups(entityDto.getId(), asList(setLookup, listLookup, singleLookup));
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) LookupDto(org.motechproject.mds.dto.LookupDto) RestOptionsDto(org.motechproject.mds.dto.RestOptionsDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 19 with FieldBasicDto

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

the class MdsRestBundleIT method prepareFilteredEntity.

private void prepareFilteredEntity() throws IOException {
    EntityDto entityDto = new EntityDto(FILTERED_ENTITY_NAME);
    entityDto = entityService.createEntity(entityDto);
    FieldDto strField = new FieldDto(null, entityDto.getId(), TypeDto.STRING, new FieldBasicDto("strFieldDisp", "strField"), false, null);
    FieldDto intField = new FieldDto(null, entityDto.getId(), TypeDto.INTEGER, new FieldBasicDto("intFieldDisp", "intField"), false, null);
    entityService.addFields(entityDto, asList(strField, intField));
    RestOptionsDto restOptions = new RestOptionsDto(true, true, true, true, false);
    restOptions.setFieldNames(prepareFilteredRestFieldNames(entityService.getEntityFields(entityDto.getId())));
    entityService.updateRestOptions(entityDto.getId(), restOptions);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) RestOptionsDto(org.motechproject.mds.dto.RestOptionsDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 20 with FieldBasicDto

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

the class FieldProcessor method process.

@Override
protected void process(AnnotatedElement element) {
    AccessibleObject ac = (AccessibleObject) element;
    Class<?> classType = MemberUtil.getCorrectType(ac);
    Class<?> genericType = MemberUtil.getGenericType(element);
    Class<?> declaringClass = MemberUtil.getDeclaringClass(ac);
    Class<?> valueType = null;
    if (null != classType) {
        if (Map.class.isAssignableFrom(classType)) {
            valueType = MemberUtil.getGenericType(element, 1);
        }
        String fieldName = MemberUtil.getFieldName(ac);
        FieldDto currentField = getFieldByName(declaringClass.getName(), fieldName);
        boolean isRelationship = ReflectionsUtil.hasAnnotationClassLoaderSafe(genericType, genericType, Entity.class);
        boolean isOwningSide = isRelationship && getMappedBy(ac) == null;
        boolean isCollection = Collection.class.isAssignableFrom(classType);
        Field annotation = getAnnotationClassLoaderSafe(ac, classType, Field.class);
        String relatedFieldName = getRelatedFieldName(ac, classType, genericType, declaringClass, isRelationship);
        java.lang.reflect.Field relatedField = (relatedFieldName != null) ? ReflectionUtils.findField(genericType, relatedFieldName) : null;
        boolean relatedFieldIsCollection = isRelatedFieldCollection(relatedField);
        boolean isTextArea = getAnnotationValue(annotation, TYPE, EMPTY).equalsIgnoreCase("text");
        TypeDto type = getCorrectType(classType, isCollection, isRelationship, relatedFieldIsCollection);
        FieldBasicDto basic = new FieldBasicDto();
        basic.setDisplayName(isUiChanged(currentField) ? currentField.getBasic().getDisplayName() : getAnnotationValue(annotation, DISPLAY_NAME, convertFromCamelCase(fieldName)));
        basic.setName(fieldName);
        basic.setDefaultValue(getDefaultValueForField(annotation, classType));
        basic.setRequired(isFieldRequired(annotation, classType));
        basic.setUnique(isFieldUnique(ac));
        FieldDto field = new FieldDto();
        if (null != annotation) {
            basic.setTooltip(isUiChanged(currentField) ? currentField.getBasic().getTooltip() : annotation.tooltip());
            basic.setPlaceholder(annotation.placeholder());
            String fn = getAnnotationValue(annotation, NAME, EMPTY);
            if (!fn.equals(EMPTY)) {
                field.addMetadata(new MetadataDto(DATABASE_COLUMN_NAME, fn));
            }
        }
        field.setEntityId(entity.getId());
        field.setType(type);
        field.setBasic(basic);
        field.setValidation(createValidation(ac, type));
        field.setReadOnly(true);
        field.setNonEditable(false);
        field.setNonDisplayable(false);
        field.setUiChanged(isUiChanged(currentField));
        field.setUiFilterable(isUiFilterable(currentField));
        setFieldSettings(ac, classType, isRelationship, isTextArea, field);
        setFieldMetadata(classType, genericType, valueType, isCollection, isRelationship, relatedFieldIsCollection, isOwningSide, field, relatedFieldName);
        add(field);
    } else {
        LOGGER.warn("Field type is unknown in: {}", ac);
    }
}
Also used : PredicateUtil.entityField(org.motechproject.mds.annotations.internal.PredicateUtil.entityField) Field(org.motechproject.mds.annotations.Field) FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) AccessibleObject(java.lang.reflect.AccessibleObject) TypeDto(org.motechproject.mds.dto.TypeDto) MetadataDto(org.motechproject.mds.dto.MetadataDto) FieldDto(org.motechproject.mds.dto.FieldDto)

Aggregations

FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)20 FieldDto (org.motechproject.mds.dto.FieldDto)19 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)11 EntityDto (org.motechproject.mds.dto.EntityDto)9 TypeDto (org.motechproject.mds.dto.TypeDto)9 MetadataDto (org.motechproject.mds.dto.MetadataDto)8 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)6 LookupDto (org.motechproject.mds.dto.LookupDto)5 LinkedList (java.util.LinkedList)4 SettingDto (org.motechproject.mds.dto.SettingDto)4 Field (org.motechproject.mds.domain.Field)3 FieldValidationDto (org.motechproject.mds.dto.FieldValidationDto)3 SchemaHolder (org.motechproject.mds.dto.SchemaHolder)3 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 Type (org.motechproject.mds.domain.Type)2 RestOptionsDto (org.motechproject.mds.dto.RestOptionsDto)2 ValidationCriterionDto (org.motechproject.mds.dto.ValidationCriterionDto)2 FieldTestHelper.lookupFieldDto (org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto)2 ResultActions (org.springframework.test.web.server.ResultActions)2