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());
}
}
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;
}
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;
}
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);
}
}
}
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;
}
Aggregations