Search in sources :

Example 1 with RelationshipHolder

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

the class InstancesReader method readRelationshipProperty.

private void readRelationshipProperty(Field field, Object instance) throws IOException {
    RelationshipHolder relationshipHolder = new RelationshipHolder(field);
    if (relationshipHolder.isManyToOne() || relationshipHolder.isOneToOne()) {
        Long refId = objectReader.readLong();
        if (null != refId) {
            Object relatedInstance = importContext.getInstanceOfEntity(relationshipHolder.getRelatedClass(), refId);
            PropertyUtil.safeSetProperty(instance, field.getName(), relatedInstance);
        }
    } else if (relationshipHolder.isManyToMany() || relationshipHolder.isOneToMany()) {
        List<Long> refIds = objectReader.readLongArray();
        if (null != refIds) {
            Collection<Object> relatedInstances = importContext.getInstancesOfEntity(relationshipHolder.getRelatedClass(), refIds);
            PropertyUtil.safeSetCollectionProperty(instance, field.getName(), relatedInstances);
        }
    }
}
Also used : RelationshipHolder(org.motechproject.mds.domain.RelationshipHolder) Collection(java.util.Collection) List(java.util.List)

Example 2 with RelationshipHolder

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

the class EntityMetadataBuilderImpl method fixCollectionMetadata.

private void fixCollectionMetadata(CollectionMetadata collMd, FieldDto field) {
    String elementType = collMd.getElementType();
    RelationshipHolder holder = new RelationshipHolder(field);
    if (null != MotechClassPool.getEnhancedClassData(elementType)) {
        collMd.setEmbeddedElement(false);
    }
    if (holder.isOneToMany() && holder.getRelatedField() == null) {
        collMd.setDependentElement(holder.isCascadeDelete());
    }
}
Also used : RelationshipHolder(org.motechproject.mds.domain.RelationshipHolder)

Example 3 with RelationshipHolder

use of org.motechproject.mds.domain.RelationshipHolder 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 4 with RelationshipHolder

use of org.motechproject.mds.domain.RelationshipHolder 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)

Example 5 with RelationshipHolder

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

the class ActionHandlerServiceImpl method setRelationshipInstanceProperty.

private void setRelationshipInstanceProperty(Object instance, Field field, Object value) throws ActionHandlerException {
    RelationshipHolder relationshipHolder = new RelationshipHolder(field);
    try {
        if (null != value) {
            String relatedClassName = relationshipHolder.getRelatedClass();
            MotechDataService relatedClassDataService = getEntityDataService(relatedClassName);
            if (relationshipHolder.isManyToOne() || relationshipHolder.isOneToOne()) {
                Object relatedInstance = getRelatedInstance(relatedClassDataService, value);
                PropertyUtil.safeSetProperty(instance, field.getName(), relatedInstance);
            } else if (relationshipHolder.isManyToMany() || relationshipHolder.isOneToMany()) {
                List<Object> relatedInstances = getRelatedInstances(relatedClassDataService, value);
                PropertyUtil.safeSetCollectionProperty(instance, field.getName(), relatedInstances);
            }
        } else {
            PropertyUtil.safeSetProperty(instance, field.getName(), null);
        }
    } catch (RuntimeException e) {
        throw new ActionHandlerException("Cannot set instance property " + field.getName() + " with value " + value, e);
    }
}
Also used : ActionHandlerException(org.motechproject.mds.exception.action.ActionHandlerException) RelationshipHolder(org.motechproject.mds.domain.RelationshipHolder) ArrayList(java.util.ArrayList) List(java.util.List) MotechDataService(org.motechproject.mds.service.MotechDataService)

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