Search in sources :

Example 6 with RelationshipHolder

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

the class CsvImporterExporter method parseRelationshipValue.

private Object parseRelationshipValue(String csvValue, FieldDto field) {
    RelationshipHolder relationshipHolder = new RelationshipHolder(field);
    if (relationshipHolder.isManyToMany() || relationshipHolder.isOneToMany()) {
        List<Long> ids = (List<Long>) TypeHelper.parse(csvValue, List.class.getName(), Long.class.getName());
        Collection<Object> relatedObjects = buildRelationshipCollection(relationshipHolder);
        if (ids != null) {
            for (Long id : ids) {
                Object relatedObj = getRelatedObject(id, relationshipHolder.getRelatedClass());
                if (relatedObj != null) {
                    relatedObjects.add(relatedObj);
                }
            }
        }
        return relatedObjects;
    } else {
        Long id = (Long) TypeHelper.parse(csvValue, Long.class);
        return getRelatedObject(id, relationshipHolder.getRelatedClass());
    }
}
Also used : RelationshipHolder(org.motechproject.mds.domain.RelationshipHolder) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with RelationshipHolder

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

the class MdsRestFacadeImpl method getLookupFieldsMapping.

private Map<String, FieldDto> getLookupFieldsMapping(EntityInfo entity, LookupDto lookup) {
    Map<String, FieldDto> fieldMap = new HashMap<>();
    for (LookupFieldDto lookupField : lookup.getLookupFields()) {
        FieldDto field;
        if (StringUtils.isNotBlank(lookupField.getRelatedName())) {
            RelationshipHolder relHolder = new RelationshipHolder(entity.getField(lookupField.getName()).getField());
            EntityInfo relatedEntity = entityInfoReader.getEntityInfo(relHolder.getRelatedClass());
            field = relatedEntity.getField(lookupField.getRelatedName()).getField();
        } else {
            field = entity.getField(lookupField.getName()).getField();
        }
        fieldMap.put(lookupField.getLookupFieldName(), field);
    }
    return fieldMap;
}
Also used : RelationshipHolder(org.motechproject.mds.domain.RelationshipHolder) HashMap(java.util.HashMap) EntityInfo(org.motechproject.mds.entityinfo.EntityInfo) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 8 with RelationshipHolder

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

the class EntitySorter method sortByHasARelation.

/**
 * Takes a list of entities and sorts them, according to relationships they have. The entities
 * that have uni-directional relationship with another entity, will be moved to the position
 * behind the entity they are related with. The bi-directional relationships are not sorted,
 * moreover if invalid bi-directional relationship is found, an exception is thrown.
 *
 * @param schemaHolder the holder of the current MDS schema
 * @return List of entities, sorted by relationship
 */
public static List<EntityDto> sortByHasARelation(List<EntityDto> allEntities, SchemaHolder schemaHolder) {
    List<EntityDto> sorted = new ArrayList<>(allEntities);
    MultiValueMap<String, String> unresolvedRelations = new LinkedMultiValueMap<>();
    // we do that after all entities will be added to sorted list
    for (int i = 0; i < sorted.size(); ++i) {
        EntityDto entity = sorted.get(i);
        List<FieldDto> fields = (List<FieldDto>) CollectionUtils.select(schemaHolder.getFields(entity), new Predicate() {

            @Override
            public boolean evaluate(Object object) {
                return object instanceof FieldDto && ((FieldDto) object).getType().isRelationship();
            }
        });
        if (CollectionUtils.isNotEmpty(fields)) {
            int max = i;
            for (FieldDto field : fields) {
                final RelationshipHolder holder = new RelationshipHolder(field);
                // For each field we perform a validation to spot circular, unresolvable relations,
                // which means the data model is incorrect
                unresolvedRelations = validateRelationship(unresolvedRelations, entity, holder);
                assertRelationshipIsHistoryCompatible(entity, holder, allEntities);
                EntityDto relation = (EntityDto) CollectionUtils.find(sorted, new Predicate() {

                    @Override
                    public boolean evaluate(Object object) {
                        return object instanceof EntityDto && ((EntityDto) object).getClassName().equalsIgnoreCase(holder.getRelatedClass());
                    }
                });
                // In case the relation is bidirectional, we shouldn't move the class,
                // in order to avoid infinite loop
                boolean biDirectional = field.getMetadata(RELATED_FIELD) != null;
                max = Math.max(max, biDirectional ? -1 : sorted.indexOf(relation));
            }
            if (max != i) {
                sorted.remove(i);
                --i;
                if (max < sorted.size()) {
                    sorted.add(max, entity);
                } else {
                    sorted.add(entity);
                }
            }
        }
    }
    return sorted;
}
Also used : RelationshipHolder(org.motechproject.mds.domain.RelationshipHolder) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ArrayList(java.util.ArrayList) Predicate(org.apache.commons.collections.Predicate) EntityDto(org.motechproject.mds.dto.EntityDto) ArrayList(java.util.ArrayList) List(java.util.List) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 9 with RelationshipHolder

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

the class RevertConverter method convert.

@Override
public Object convert(Object value, PropertyDescriptor descriptor) {
    FieldInfo field = entityInfo.getField(descriptor.getName());
    FieldDto fieldDto = field.getField();
    if (value == null || field == null || !fieldDto.getType().isRelationship()) {
        return value;
    } else {
        RelationshipHolder relHolder = new RelationshipHolder(fieldDto);
        String relatedClass = relHolder.getRelatedClass();
        MotechDataService relatedDataService = ServiceUtil.getServiceFromAppContext(applicationContext, relatedClass);
        if (value instanceof Collection) {
            Collection<Long> idColl = (Collection<Long>) value;
            return idCollToRelColl(idColl, relatedDataService);
        } else {
            Long id = (Long) value;
            return relatedDataService.findById(id);
        }
    }
}
Also used : RelationshipHolder(org.motechproject.mds.domain.RelationshipHolder) Collection(java.util.Collection) FieldInfo(org.motechproject.mds.entityinfo.FieldInfo) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 10 with RelationshipHolder

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

the class EntityMetadataBuilderImpl method setRelationshipMetadata.

private FieldMetadata setRelationshipMetadata(ClassMetadata cmd, ClassData classData, EntityDto entity, FieldDto field, EntityType entityType, Class<?> definition) {
    RelationshipHolder holder = new RelationshipHolder(classData, field);
    FieldMetadata fmd = cmd.newFieldMetadata(getNameForMetadata(field));
    addDefaultFetchGroupMetadata(fmd, definition);
    if (entityType == EntityType.STANDARD) {
        processRelationship(fmd, holder, entity, field, definition);
    } else {
        processHistoryTrashRelationship(cmd, fmd, holder);
    }
    return fmd;
}
Also used : FieldMetadata(javax.jdo.metadata.FieldMetadata) RelationshipHolder(org.motechproject.mds.domain.RelationshipHolder)

Aggregations

RelationshipHolder (org.motechproject.mds.domain.RelationshipHolder)12 FieldDto (org.motechproject.mds.dto.FieldDto)6 ArrayList (java.util.ArrayList)4 List (java.util.List)4 EntityDto (org.motechproject.mds.dto.EntityDto)4 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)4 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 ComboboxHolder (org.motechproject.mds.domain.ComboboxHolder)2 TypeDto (org.motechproject.mds.dto.TypeDto)2 CtMethod (javassist.CtMethod)1 FieldMetadata (javax.jdo.metadata.FieldMetadata)1 ForeignKeyMetadata (javax.jdo.metadata.ForeignKeyMetadata)1 MemberMetadata (javax.jdo.metadata.MemberMetadata)1 Predicate (org.apache.commons.collections.Predicate)1 EntityInfo (org.motechproject.mds.entityinfo.EntityInfo)1 FieldInfo (org.motechproject.mds.entityinfo.FieldInfo)1 ActionHandlerException (org.motechproject.mds.exception.action.ActionHandlerException)1 PropertyBuilder (org.motechproject.mds.query.PropertyBuilder)1 QueryParams (org.motechproject.mds.query.QueryParams)1