Search in sources :

Example 11 with Field

use of org.motechproject.mds.domain.Field in project motech by motech.

the class InstancesReader method importInstance.

public void importInstance() throws IOException {
    try {
        Object instance = dataService.getClassType().newInstance();
        Long refId = null;
        jsonReader.beginObject();
        while (jsonReader.hasNext()) {
            String fieldName = objectReader.readName();
            if ("refId".equals(fieldName)) {
                refId = jsonReader.nextLong();
            } else {
                Field field = entity.getField(fieldName);
                readProperty(instance, field);
            }
        }
        jsonReader.endObject();
        if (null != refId) {
            importContext.putInstanceOfEntity(entity.getClassName(), refId, dataService.create(instance));
        }
    } catch (InstantiationException | IllegalAccessException e) {
        LOGGER.error("Exception occurred during importing instances", e);
    }
}
Also used : Field(org.motechproject.mds.domain.Field)

Example 12 with Field

use of org.motechproject.mds.domain.Field in project motech by motech.

the class EntityReader method readDataBrowsingDisplayFields.

private void readDataBrowsingDisplayFields() throws IOException {
    List<String> displayFieldsNames = objectReader.readStringArray("fields");
    for (Field field : entity.getFields()) {
        long uiDisplayPosition = displayFieldsNames.indexOf(field.getName());
        field.setUIDisplayable(uiDisplayPosition >= 0);
        field.setUIDisplayPosition(uiDisplayPosition);
    }
}
Also used : Field(org.motechproject.mds.domain.Field)

Example 13 with Field

use of org.motechproject.mds.domain.Field in project motech by motech.

the class SwaggerGenerator method buildDefinitionProperties.

private void buildDefinitionProperties(Map<String, Property> properties, List<String> required, Entity entity, boolean includeAuto, boolean includeId) {
    if (includeId) {
        properties.put(Constants.Util.ID_FIELD_NAME, new Property(INTEGER_TYPE, INT64_FORMAT));
    }
    for (Field field : entity.getFields()) {
        final String fieldName = field.getName();
        if (field.isExposedViaRest()) {
            // auto generated fields included only in responses
            if (!field.isAutoGenerated() || includeAuto) {
                Property property = SwaggerFieldConverter.fieldToProperty(field);
                properties.put(fieldName, property);
                if (field.isRequired()) {
                    required.add(fieldName);
                }
            }
        }
    }
}
Also used : Field(org.motechproject.mds.domain.Field) Property(org.motechproject.mds.docs.swagger.model.Property)

Example 14 with Field

use of org.motechproject.mds.domain.Field in project motech by motech.

the class EntityServiceImpl method getLookupFieldsMapping.

@Override
@Transactional
public Map<String, FieldDto> getLookupFieldsMapping(Long entityId, String lookupName) {
    Entity entity = allEntities.retrieveById(entityId);
    assertEntityExists(entity, entityId);
    Lookup lookup = entity.getLookupByName(lookupName);
    if (lookup == null) {
        throw new LookupNotFoundException(entity.getName(), lookupName);
    }
    Map<String, FieldDto> fieldMap = new HashMap<>();
    for (String lookupFieldName : lookup.getFieldsOrder()) {
        Field field = lookup.getLookupFieldByName(LookupName.getFieldName(lookupFieldName));
        if (lookupFieldName.contains(".")) {
            Entity relatedEntity = allEntities.retrieveByClassName(field.getMetadata(Constants.MetadataKeys.RELATED_CLASS).getValue());
            field = relatedEntity.getField(LookupName.getRelatedFieldName(lookupFieldName));
        }
        fieldMap.put(lookupFieldName, field.toDto());
    }
    return fieldMap;
}
Also used : MdsEntity(org.motechproject.mds.domain.MdsEntity) Entity(org.motechproject.mds.domain.Entity) MdsVersionedEntity(org.motechproject.mds.domain.MdsVersionedEntity) LookupNotFoundException(org.motechproject.mds.exception.lookup.LookupNotFoundException) Field(org.motechproject.mds.domain.Field) HashMap(java.util.HashMap) Lookup(org.motechproject.mds.domain.Lookup) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) Transactional(org.springframework.transaction.annotation.Transactional)

Example 15 with Field

use of org.motechproject.mds.domain.Field in project motech by motech.

the class EntityServiceImpl method createFieldForDraft.

private void createFieldForDraft(EntityDraft draft, DraftData draftData) {
    String typeClass = draftData.getValue(DraftData.TYPE_CLASS).toString();
    String displayName = draftData.getValue(DraftData.DISPLAY_NAME).toString();
    String name = draftData.getValue(DraftData.NAME).toString();
    Type type = ("textArea".equalsIgnoreCase(typeClass)) ? allTypes.retrieveByClassName("java.lang.String") : allTypes.retrieveByClassName(typeClass);
    if (type == null) {
        throw new NoSuchTypeException(typeClass);
    }
    Set<Lookup> fieldLookups = new HashSet<>();
    Field field = new Field(draft, name, displayName, fieldLookups);
    field.setType(type);
    if (type.hasSettings()) {
        for (TypeSetting setting : type.getSettings()) {
            field.addSetting(new FieldSetting(field, setting));
        }
    }
    if (type.hasValidation()) {
        for (TypeValidation validation : type.getValidations()) {
            field.addValidation(new FieldValidation(field, validation));
        }
    }
    if (TypeDto.BLOB.getTypeClass().equals(typeClass)) {
        field.setUIDisplayable(false);
    } else {
        field.setUIDisplayable(true);
        field.setUIDisplayPosition((long) draft.getFields().size());
    }
    if ("textArea".equalsIgnoreCase(typeClass)) {
        setSettingForTextArea(field);
    }
    FieldHelper.addMetadataForRelationship(typeClass, field);
    FieldHelper.addOrUpdateMetadataForCombobox(field);
    draft.addField(field);
    allEntityDrafts.update(draft);
}
Also used : FieldSetting(org.motechproject.mds.domain.FieldSetting) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) TypeValidation(org.motechproject.mds.domain.TypeValidation) NoSuchTypeException(org.motechproject.mds.exception.type.NoSuchTypeException) TypeSetting(org.motechproject.mds.domain.TypeSetting) Lookup(org.motechproject.mds.domain.Lookup) FieldValidation(org.motechproject.mds.domain.FieldValidation) HashSet(java.util.HashSet)

Aggregations

Field (org.motechproject.mds.domain.Field)73 Entity (org.motechproject.mds.domain.Entity)33 Test (org.junit.Test)24 Lookup (org.motechproject.mds.domain.Lookup)16 MdsEntity (org.motechproject.mds.domain.MdsEntity)15 MdsVersionedEntity (org.motechproject.mds.domain.MdsVersionedEntity)15 Type (org.motechproject.mds.domain.Type)14 ArrayList (java.util.ArrayList)13 Transactional (org.springframework.transaction.annotation.Transactional)13 FieldDto (org.motechproject.mds.dto.FieldDto)12 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)12 HashSet (java.util.HashSet)8 Matchers.anyString (org.mockito.Matchers.anyString)6 FieldSetting (org.motechproject.mds.domain.FieldSetting)6 TypeSetting (org.motechproject.mds.domain.TypeSetting)6 UserPreferences (org.motechproject.mds.domain.UserPreferences)5 EntityDto (org.motechproject.mds.dto.EntityDto)5 AllUserPreferences (org.motechproject.mds.repository.internal.AllUserPreferences)5 FieldMetadata (org.motechproject.mds.domain.FieldMetadata)4 LookupDto (org.motechproject.mds.dto.LookupDto)4