Search in sources :

Example 26 with EntityDto

use of org.motechproject.mds.dto.EntityDto in project motech by motech.

the class InstanceServiceImpl method getSingleTrashRecord.

@Override
public EntityRecord getSingleTrashRecord(Long entityId, Long instanceId) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);
    MotechDataService service = getServiceForEntity(entity);
    List<FieldDto> fields = entityService.getEntityFieldsForUI(entityId);
    Object instance = trashService.findTrashById(instanceId, entity.getClassName());
    Map<String, List<FieldDto>> relatedEntitiesFields = getRelatedEntitiesFields(fields);
    return instanceToRecord(instance, entity, fields, service, EntityType.TRASH, relatedEntitiesFields);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) ArrayList(java.util.ArrayList) List(java.util.List) MotechDataService(org.motechproject.mds.service.MotechDataService) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 27 with EntityDto

use of org.motechproject.mds.dto.EntityDto in project motech by motech.

the class LookupProcessor method process.

@Override
protected void process(AnnotatedElement annotatedElement) {
    Method method = (Method) annotatedElement;
    Class returnType = method.getReturnType();
    String returnClassName = returnType.getName();
    boolean singleObjectReturn = true;
    if (returnType.isArray() || Collection.class.isAssignableFrom(returnType)) {
        singleObjectReturn = false;
        returnClassName = determineGenericClass(method.getGenericReturnType().toString());
    }
    EntityDto entity = findEntityByClassName(returnClassName);
    if (entity == null) {
        LOGGER.error("There's no matching entity for the resolved return type of the lookup" + "method: {}; Resolved return type: {}", method.getName(), returnClassName);
        return;
    }
    LOGGER.debug("Found entity class by the return type of lookup method: {}", entity.getName());
    Lookup annotation = ReflectionsUtil.findAnnotation(method, Lookup.class);
    String lookupName = generateLookupName(annotation.name(), method.getName());
    List<LookupFieldDto> lookupFields = findLookupFields(method, entity);
    boolean restExposed = processRestExposed(method);
    boolean indexRequired = annotation.indexRequired();
    verifyLookupParameters(method, returnClassName, lookupName, lookupFields, method.getParameterTypes());
    LookupDto lookup = new LookupDto();
    lookup.setSingleObjectReturn(singleObjectReturn);
    lookup.setLookupName(lookupName);
    lookup.setLookupFields(lookupFields);
    lookup.setReadOnly(true);
    lookup.setMethodName(method.getName());
    lookup.setIndexRequired(indexRequired);
    if (!restOptionsModifiedByUser(entity)) {
        lookup.setExposedViaRest(restExposed);
    }
    if (!getElements().containsKey(returnClassName)) {
        put(returnClassName, new ArrayList<>());
    }
    getElement(returnClassName).add(lookup);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) LookupDto(org.motechproject.mds.dto.LookupDto) Collection(java.util.Collection) Lookup(org.motechproject.mds.annotations.Lookup) Method(java.lang.reflect.Method) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 28 with EntityDto

use of org.motechproject.mds.dto.EntityDto in project motech by motech.

the class EntitySorter method sortByInheritance.

/**
 * Takes a list of entities and sorts them by the inheritance tree. The entities that extend
 * the Object class or MdsEntity class will be moved to the beggining of the list. After that,
 * the entites that are already present on the list will be added, up the inheritance tree.
 *
 * @param list Initial list of entities to sort
 * @return List of entities, sorted by inheritance tree
 */
public static List<EntityDto> sortByInheritance(List<EntityDto> list) {
    List<EntityDto> sorted = new ArrayList<>(list.size());
    // firstly we add entities with base class equal to Object class or MdsEntity class
    for (Iterator<EntityDto> iterator = list.iterator(); iterator.hasNext(); ) {
        EntityDto entity = iterator.next();
        if (entity.isBaseEntity()) {
            sorted.add(entity);
            iterator.remove();
        }
    }
    // we do that after all entities will be added to sorted list
    while (!list.isEmpty()) {
        for (Iterator<EntityDto> iterator = list.iterator(); iterator.hasNext(); ) {
            final EntityDto entity = iterator.next();
            EntityDto superClass = (EntityDto) CollectionUtils.find(sorted, new Predicate() {

                @Override
                public boolean evaluate(Object object) {
                    return object instanceof EntityDto && ((EntityDto) object).getClassName().equals(entity.getSuperClass());
                }
            });
            if (null != superClass) {
                sorted.add(entity);
                iterator.remove();
            }
        }
    }
    return sorted;
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) ArrayList(java.util.ArrayList) Predicate(org.apache.commons.collections.Predicate)

Example 29 with EntityDto

use of org.motechproject.mds.dto.EntityDto in project motech by motech.

the class LookupBuilder method build.

private CtMethod build(boolean body) throws CannotCompileException, NotFoundException {
    Collection<String> paramCollection = new ArrayList<>();
    List<String> fieldOrder = lookup.getFieldsOrder();
    for (int i = 0; i < fieldOrder.size(); i++) {
        String fieldName = fieldOrder.get(i);
        FieldDto field = getLookupField(fieldName);
        // don't use fieldName for fetching fields, as it can contain dots, etc.
        LookupFieldDto lookupField = lookup.getLookupField(field.getBasic().getName());
        FieldDto relationField = null;
        EntityDto relatedEntity = null;
        if (fieldOrder.get(i).contains(".")) {
            relatedEntity = schemaHolder.getEntityByClassName(new RelationshipHolder(field).getRelatedClass());
            relationField = schemaHolder.getFieldByName(relatedEntity, LookupName.getRelatedFieldName(fieldOrder.get(i)));
        }
        String type = getTypeForParam(i, resolveEntity(entity, relatedEntity), resolveField(field, relationField), lookupField);
        String param = String.format("%s %s", type, fieldOrder.get(i).replace(".", ""));
        paramCollection.add(param);
    }
    // query params at the end for ordering/paging
    if (WITH_QUERY_PARAMS == lookupType) {
        String queryParam = String.format("%s queryParams", QueryParams.class.getName());
        paramCollection.add(queryParam);
    }
    String params = StringUtils.join(paramCollection, ", ");
    String signature = String.format("public %s %s(%s)", returnType(), lookupName, params);
    String methodAsString = body ? String.format("%s{%s}", signature, body()) : String.format("%s;", signature);
    String generic = buildGenericSignature();
    CtMethod method = CtNewMethod.make(methodAsString, definition);
    method.setGenericSignature(generic);
    return method;
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) RelationshipHolder(org.motechproject.mds.domain.RelationshipHolder) ArrayList(java.util.ArrayList) QueryParams(org.motechproject.mds.query.QueryParams) CtMethod(javassist.CtMethod) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 30 with EntityDto

use of org.motechproject.mds.dto.EntityDto in project motech by motech.

the class LookupBuilder method buildGenericSignature.

private String buildGenericSignature() throws NotFoundException {
    // we must build generic signatures for lookup methods
    // an example signature for the method signature
    // List<org.motechproject.mds.Test> method(String p1, Integer p2)
    // is
    // cmt -- (Ljava/lang/String;Ljava/lang/Integer;)Ljava/util/List<Lorg/motechproject/mds/Test;>;
    StringBuilder sb = new StringBuilder();
    sb.append('(');
    List<String> fieldsOrder = lookup.getFieldsOrder();
    for (int i = 0; i < fieldsOrder.size(); ++i) {
        String fieldName = fieldsOrder.get(i);
        FieldDto field = getLookupField(fieldName);
        // don't use fieldName for fetching fields, as it can contain dots, etc.
        LookupFieldDto lookupField = lookup.getLookupField(field.getBasic().getName());
        FieldDto relationField = null;
        EntityDto relatedEntity = null;
        if (fieldsOrder.get(i).contains(".")) {
            relatedEntity = schemaHolder.getEntityByClassName(new RelationshipHolder(field).getRelatedClass());
            relationField = schemaHolder.getFieldByName(relatedEntity, LookupName.getRelatedFieldName(fieldsOrder.get(i)));
        }
        String paramType = getTypeForParam(i, resolveEntity(entity, relatedEntity), resolveField(field, relationField), lookupField);
        String genericType;
        TypeDto type = resolveField(field, relationField).getType();
        if (type.isCombobox()) {
            ComboboxHolder holder = new ComboboxHolder(resolveEntity(entity, relatedEntity), resolveField(field, relationField));
            genericType = holder.getUnderlyingType();
        } else {
            genericType = type.getTypeClass();
        }
        if (StringUtils.equals(paramType, genericType) || TypeHelper.isPrimitive(paramType)) {
            // simple parameter
            sb.append(JavassistUtil.toGenericParam(paramType));
        } else {
            // we wrap in a range/set or a different wrapper
            sb.append(JavassistUtil.genericSignature(paramType, genericType));
        }
    }
    sb.append(')');
    if (lookup.isSingleObjectReturn()) {
        sb.append(JavassistUtil.toGenericParam(className));
    } else {
        sb.append(JavassistUtil.genericSignature(List.class.getName(), className));
    }
    return sb.toString();
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) RelationshipHolder(org.motechproject.mds.domain.RelationshipHolder) ComboboxHolder(org.motechproject.mds.domain.ComboboxHolder) TypeDto(org.motechproject.mds.dto.TypeDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Aggregations

EntityDto (org.motechproject.mds.dto.EntityDto)136 Test (org.junit.Test)61 FieldDto (org.motechproject.mds.dto.FieldDto)53 ArrayList (java.util.ArrayList)34 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)32 MotechDataService (org.motechproject.mds.service.MotechDataService)26 LookupDto (org.motechproject.mds.dto.LookupDto)24 List (java.util.List)19 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)12 Method (java.lang.reflect.Method)11 FieldTestHelper.lookupFieldDto (org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto)11 Arrays.asList (java.util.Arrays.asList)9 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)9 EntityRecord (org.motechproject.mds.web.domain.EntityRecord)9 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)7 TypeDto (org.motechproject.mds.dto.TypeDto)7 HashMap (java.util.HashMap)6 SchemaHolder (org.motechproject.mds.dto.SchemaHolder)6 BasicFieldRecord (org.motechproject.mds.web.domain.BasicFieldRecord)6 FieldRecord (org.motechproject.mds.web.domain.FieldRecord)6