Search in sources :

Example 21 with Field

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

the class EntityServiceImpl method getFields.

private List<FieldDto> getFields(Long entityId, boolean forDraft, boolean forUi) {
    Entity entity = (forDraft) ? getEntityDraft(entityId) : allEntities.retrieveById(entityId);
    assertEntityExists(entity, entityId);
    // the returned collection is unmodifiable
    List<Field> fields = new ArrayList<>(entity.getFields());
    // for data browser purposes, we sort the fields by their ui display order
    if (!forDraft) {
        Collections.sort(fields, new UIDisplayFieldComparator());
    }
    // if it's for the UI, then we add combobox options
    List<FieldDto> fieldDtos = toFieldDtos(entity, fields, forUi);
    return addNonPersistentFieldsData(fieldDtos, entity);
}
Also used : MdsEntity(org.motechproject.mds.domain.MdsEntity) Entity(org.motechproject.mds.domain.Entity) MdsVersionedEntity(org.motechproject.mds.domain.MdsVersionedEntity) Field(org.motechproject.mds.domain.Field) ArrayList(java.util.ArrayList) UIDisplayFieldComparator(org.motechproject.mds.domain.UIDisplayFieldComparator) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 22 with Field

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

the class EntityServiceImpl method editFieldForDraft.

private void editFieldForDraft(EntityDraft draft, DraftData draftData) {
    String fieldIdStr = draftData.getValue(DraftData.FIELD_ID).toString();
    if (StringUtils.isNotBlank(fieldIdStr)) {
        Long fieldId = Long.valueOf(fieldIdStr);
        Field field = draft.getField(fieldId);
        if (field != null) {
            String path = draftData.getPath();
            List value = (List) draftData.getValue(DraftData.VALUE);
            // Convert to dto for UI updates
            FieldDto dto = field.toDto();
            dto.setUiChanged(true);
            FieldHelper.setField(dto, path, value);
            // If field name was changed add this change to map
            if (NAME_PATH.equals(path)) {
                draft.addFieldNameChange(field.getName(), value.get(0).toString());
                List<LookupDto> lookups = draft.advancedSettingsDto().getIndexes();
                // Perform update
                field.update(dto);
                // we need update fields name in lookup fieldsOrder
                draft.updateIndexes(lookups);
                FieldHelper.addOrUpdateMetadataForCombobox(field);
            } else if (UNIQUE_PATH.equals(path)) {
                // check if unique was removed on this field
                boolean originalUnique = false;
                Field originalField = draft.getParentEntity().getField(field.getName());
                if (originalField != null) {
                    originalUnique = originalField.isUnique();
                }
                boolean newVal = (boolean) value.get(0);
                if (originalUnique && !newVal) {
                    // we will be dropping the unique constraint for this field
                    draft.addUniqueToRemove(field.getName());
                }
                // Perform update
                field.update(dto);
            } else if (REQUIRED_PATH.equals(path)) {
                Field originalField = draft.getParentEntity().getField(field.getName());
                handleRequiredChanges(originalField, draft, value, field);
                // Perform update
                field.update(dto);
            } else {
                // Perform update
                field.update(dto);
            }
            allEntityDrafts.update(draft);
        }
    }
}
Also used : Field(org.motechproject.mds.domain.Field) LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) List(java.util.List) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 23 with Field

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

the class EntityServiceImpl method findFieldByName.

@Override
@Transactional
public FieldDto findFieldByName(Long entityId, String name) {
    Entity entity = getEntityDraft(entityId);
    Field field = entity.getField(name);
    if (field == null) {
        throw new FieldNotFoundException(entity.getClassName(), name);
    }
    return field.toDto();
}
Also used : MdsEntity(org.motechproject.mds.domain.MdsEntity) Entity(org.motechproject.mds.domain.Entity) MdsVersionedEntity(org.motechproject.mds.domain.MdsVersionedEntity) Field(org.motechproject.mds.domain.Field) FieldNotFoundException(org.motechproject.mds.exception.field.FieldNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 24 with Field

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

the class EntityServiceImpl method addNonEditableFields.

@Override
@Transactional
public void addNonEditableFields(EntityDto entityDto, Map<String, Boolean> nonEditableFields) {
    Entity entity = allEntities.retrieveById(entityDto.getId());
    assertEntityExists(entity, entityDto.getId());
    List<Field> fields = entity.getFields();
    for (Field field : fields) {
        boolean isNonEditable = nonEditableFields.containsKey(field.getName());
        Boolean display = nonEditableFields.get(field.getName());
        field.setNonEditable(isNonEditable);
        if (display != null) {
            field.setNonDisplayable(!display);
        } else {
            field.setNonDisplayable(false);
        }
    }
}
Also used : MdsEntity(org.motechproject.mds.domain.MdsEntity) Entity(org.motechproject.mds.domain.Entity) MdsVersionedEntity(org.motechproject.mds.domain.MdsVersionedEntity) Field(org.motechproject.mds.domain.Field) Transactional(org.springframework.transaction.annotation.Transactional)

Example 25 with Field

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

the class EntityServiceImpl method getDisplayFields.

@Override
@Transactional
public List<FieldDto> getDisplayFields(Long entityId) {
    Entity entity = allEntities.retrieveById(entityId);
    assertEntityExists(entity, entityId);
    List<Field> displayFields = new ArrayList<>(entity.getFields());
    CollectionUtils.filter(displayFields, new Predicate() {

        @Override
        public boolean evaluate(Object object) {
            Field field = (Field) object;
            return field.isUIDisplayable() && !field.isNonDisplayable();
        }
    });
    return toFieldDtos(entity, displayFields, true);
}
Also used : MdsEntity(org.motechproject.mds.domain.MdsEntity) Entity(org.motechproject.mds.domain.Entity) MdsVersionedEntity(org.motechproject.mds.domain.MdsVersionedEntity) Field(org.motechproject.mds.domain.Field) ArrayList(java.util.ArrayList) Predicate(org.apache.commons.collections.Predicate) Transactional(org.springframework.transaction.annotation.Transactional)

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