Search in sources :

Example 11 with RelationshipHolder

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

the class EntityMetadataBuilderImpl method fixRelationMetadata.

private void fixRelationMetadata(PackageMetadata pmd, EntityDto entity, FieldDto field) {
    RelationshipHolder holder = new RelationshipHolder(field);
    // this must be set here cause we can't get related class metadata before metadata enhancement
    if (shouldSetCascadeDelete(holder, EntityType.STANDARD)) {
        String relatedClass = ClassName.getSimpleName(holder.getRelatedClass());
        MemberMetadata rfmd = getFieldMetadata(getClassMetadata(pmd, relatedClass), holder.getRelatedField());
        ForeignKeyMetadata rfkmd = getOrCreateRelFkMetadata(rfmd, entity, field);
        rfkmd.setDeleteAction(ForeignKeyAction.CASCADE);
    }
}
Also used : RelationshipHolder(org.motechproject.mds.domain.RelationshipHolder) ForeignKeyMetadata(javax.jdo.metadata.ForeignKeyMetadata) MemberMetadata(javax.jdo.metadata.MemberMetadata)

Example 12 with RelationshipHolder

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

the class LookupBuilder method body.

private String body() {
    StringBuilder body = new StringBuilder();
    body.append("java.util.List properties = new java.util.ArrayList();");
    Map<String, String> jdoQueryVariables = new HashMap<>();
    for (String lookupFieldName : lookup.getFieldsOrder()) {
        boolean appendJdoVariableName = false;
        FieldDto field = getLookupField(lookupFieldName);
        // don't use lookupFieldName for fetching fields, as it can contain dots, etc.
        LookupFieldDto lookupField = lookup.getLookupField(field.getBasic().getName());
        FieldDto relationField = null;
        if (lookupFieldName.contains(".")) {
            EntityDto relatedEntity = schemaHolder.getEntityByClassName(new RelationshipHolder(field).getRelatedClass());
            relationField = schemaHolder.getFieldByName(relatedEntity, LookupName.getRelatedFieldName(lookupFieldName));
        }
        TypeDto type = field.getType();
        String typeClassName = resolveClassName(type, relationField);
        body.append("properties.add(");
        if (type.isCombobox()) {
            ComboboxHolder holder = new ComboboxHolder(entity, field);
            typeClassName = holder.getUnderlyingType();
            body.append(resolvePropertyForCombobox(holder, type));
        } else if (relationField != null && relationField.getType().isCombobox()) {
            RelationshipHolder relationshipHolder = new RelationshipHolder(field);
            EntityDto relatedEntity = schemaHolder.getEntityByClassName(relationshipHolder.getRelatedClass());
            ComboboxHolder holder = new ComboboxHolder(relatedEntity, relationField);
            typeClassName = holder.getUnderlyingType();
            body.append(resolvePropertyForCombobox(holder, type));
            addJdoVariableName(jdoQueryVariables, lookupFieldName);
            appendJdoVariableName = needsJdoVariable(type);
        } else if (relationField != null && (type.getTypeClass().equals(TypeDto.MANY_TO_MANY_RELATIONSHIP.getTypeClass()) || type.getTypeClass().equals(TypeDto.ONE_TO_MANY_RELATIONSHIP.getTypeClass()))) {
            // related class collection
            body.append(PropertyBuilder.class.getName());
            body.append(".createRelationProperty");
            addJdoVariableName(jdoQueryVariables, lookupFieldName);
            appendJdoVariableName = true;
        } else {
            body.append(PropertyBuilder.class.getName());
            body.append(".create");
        }
        body.append(buildPropertyParameters(appendJdoVariableName, lookupFieldName, lookupField, typeClassName, jdoQueryVariables));
    }
    body.append(buildReturn());
    return body.toString();
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) RelationshipHolder(org.motechproject.mds.domain.RelationshipHolder) HashMap(java.util.HashMap) 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) PropertyBuilder(org.motechproject.mds.query.PropertyBuilder)

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