Search in sources :

Example 1 with LookupNotFoundException

use of org.motechproject.mds.exception.lookup.LookupNotFoundException 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)

Aggregations

HashMap (java.util.HashMap)1 Entity (org.motechproject.mds.domain.Entity)1 Field (org.motechproject.mds.domain.Field)1 Lookup (org.motechproject.mds.domain.Lookup)1 MdsEntity (org.motechproject.mds.domain.MdsEntity)1 MdsVersionedEntity (org.motechproject.mds.domain.MdsVersionedEntity)1 FieldDto (org.motechproject.mds.dto.FieldDto)1 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)1 LookupNotFoundException (org.motechproject.mds.exception.lookup.LookupNotFoundException)1 Transactional (org.springframework.transaction.annotation.Transactional)1