Search in sources :

Example 16 with Field

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

the class EntityServiceImpl method removeFields.

private void removeFields(Entity entity, Collection<FieldDto> fields) {
    Iterator<Field> iterator = entity.getFields().iterator();
    while (iterator.hasNext()) {
        Field field = iterator.next();
        // don't remove user defined fields
        if (!field.isReadOnly() || field.getMetadata(AUTO_GENERATED) != null || field.getMetadata(AUTO_GENERATED_EDITABLE) != null) {
            continue;
        }
        boolean found = false;
        for (FieldDto fieldDto : fields) {
            if (field.getName().equalsIgnoreCase(fieldDto.getBasic().getName())) {
                found = true;
                break;
            }
        }
        if (!found) {
            iterator.remove();
        }
    }
}
Also used : Field(org.motechproject.mds.domain.Field) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 17 with Field

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

the class EntityServiceImpl method getFieldsForPreferences.

private Set<Field> getFieldsForPreferences(Entity parent, EntityDraft draft, Set<String> oldFields) {
    Set<Field> newFields = new HashSet<>();
    for (String field : oldFields) {
        String name = field;
        if (draft.getFieldNameChanges().containsKey(name)) {
            name = draft.getFieldNameChanges().get(name);
        }
        Field newfield = parent.getField(name);
        if (newfield != null) {
            newFields.add(newfield);
        }
    }
    return newFields;
}
Also used : Field(org.motechproject.mds.domain.Field) HashSet(java.util.HashSet)

Example 18 with Field

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

the class EntityServiceImpl method addNonPersistentDataForLookupFields.

private void addNonPersistentDataForLookupFields(Collection<LookupDto> lookupDtos, Entity entity) {
    for (LookupDto lookup : lookupDtos) {
        for (LookupFieldDto lookupField : lookup.getLookupFields()) {
            Field field = entity.getField(lookupField.getName());
            if (StringUtils.isNotBlank(lookupField.getRelatedName())) {
                Entity relatedEntity = allEntities.retrieveByClassName(field.getMetadata(Constants.MetadataKeys.RELATED_CLASS).getValue());
                Field relatedField = relatedEntity.getField(lookupField.getRelatedName());
                addNonPersistentDataForLookupField(relatedField, lookupField, field.getDisplayName(), relatedField.getDisplayName());
            } else {
                addNonPersistentDataForLookupField(field, lookupField, field.getDisplayName(), null);
            }
        }
    }
}
Also used : Field(org.motechproject.mds.domain.Field) MdsEntity(org.motechproject.mds.domain.MdsEntity) Entity(org.motechproject.mds.domain.Entity) MdsVersionedEntity(org.motechproject.mds.domain.MdsVersionedEntity) LookupDto(org.motechproject.mds.dto.LookupDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 19 with Field

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

the class EntityServiceImpl method getEntityFieldsByClassName.

private List<FieldDto> getEntityFieldsByClassName(String className, boolean forUI) {
    Entity entity = allEntities.retrieveByClassName(className);
    assertEntityExists(entity, className);
    List<Field> fields = new ArrayList<>(entity.getFields());
    Collections.sort(fields, new UIDisplayFieldComparator());
    return toFieldDtos(entity, fields, forUI);
}
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)

Example 20 with Field

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

the class EntityServiceImpl method updateRelatedField.

private void updateRelatedField(Field oldField, Field draftField, List<String> modulesToRefresh) {
    Entity relatedEntity = allEntities.retrieveByClassName(oldField.getMetadataValue(RELATED_CLASS));
    Field relatedField = relatedEntity.getField(oldField.getMetadataValue(RELATED_FIELD));
    boolean fieldChanged = false;
    boolean relatedEntityChanged = false;
    if (!StringUtils.equals(draftField.getMetadataValue(RELATED_CLASS), oldField.getMetadataValue(RELATED_CLASS))) {
        addRelatedField(draftField, modulesToRefresh);
        relatedEntity.removeField(relatedField.getId());
        relatedEntityChanged = true;
    }
    if (!relatedEntityChanged && !StringUtils.equals(draftField.getMetadataValue(RELATIONSHIP_COLLECTION_TYPE), oldField.getMetadataValue(RELATIONSHIP_COLLECTION_TYPE))) {
        relatedField.setMetadataValue(RELATIONSHIP_COLLECTION_TYPE, draftField.getMetadataValue(RELATIONSHIP_COLLECTION_TYPE));
        fieldChanged = true;
    }
    if (!relatedEntityChanged && !StringUtils.equals(draftField.getMetadataValue(RELATED_FIELD), oldField.getMetadataValue(RELATED_FIELD))) {
        relatedField.setName(draftField.getMetadataValue(RELATED_FIELD));
        fieldChanged = true;
    }
    if (!relatedEntityChanged && !oldField.getName().equals(draftField.getName())) {
        relatedField.setMetadataValue(RELATED_FIELD, draftField.getName());
        fieldChanged = true;
    }
    if (fieldChanged || relatedEntityChanged) {
        relatedEntity.incrementVersion();
    }
    addModuleToRefresh(relatedEntity, modulesToRefresh);
}
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)

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