Search in sources :

Example 11 with ComboboxHolder

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

the class SwaggerFieldConverter method toComboboxProperty.

private static Property toComboboxProperty(Field field) {
    if (field.getType().isCombobox()) {
        ComboboxHolder cbHolder = new ComboboxHolder(field);
        Property itemProperty = new Property(STRING_TYPE);
        // user-supplied comoboxes are actually strings or list of strings
        if (!cbHolder.isAllowUserSupplied()) {
            itemProperty.setEnumValues(Arrays.asList(cbHolder.getValues()));
        }
        if (cbHolder.isAllowMultipleSelections()) {
            return new Property(ARRAY_TYPE, itemProperty);
        } else {
            return itemProperty;
        }
    } else {
        return null;
    }
}
Also used : ComboboxHolder(org.motechproject.mds.domain.ComboboxHolder) Property(org.motechproject.mds.docs.swagger.model.Property)

Example 12 with ComboboxHolder

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

the class EntityServiceImpl method getAllComboboxValues.

private List<String> getAllComboboxValues(Entity entity, Field field) {
    ServiceReference<ComboboxValueService> ref = bundleContext.getServiceReference(ComboboxValueService.class);
    if (ref == null) {
        LOGGER.warn("Combobox value service unavailable, ignoring user supplied values");
        ComboboxHolder cbHolder = new ComboboxHolder(field);
        return Arrays.asList(cbHolder.getValues());
    } else {
        ComboboxValueService cbValueService = bundleContext.getService(ref);
        return cbValueService.getAllValuesForCombobox(entity.toDto(), field.toDto());
    }
}
Also used : ComboboxHolder(org.motechproject.mds.domain.ComboboxHolder) ComboboxValueService(org.motechproject.mds.service.ComboboxValueService)

Example 13 with ComboboxHolder

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

the class EntityBuilderImpl method createField.

private CtField createField(CtClass declaring, EntityDto entity, FieldDto field, EntityType entityType) throws IllegalAccessException, InstantiationException, CannotCompileException, ClassNotFoundException {
    TypeDto fieldType = field.getType();
    String genericSignature = null;
    CtClass type = null;
    if (fieldType.isCombobox()) {
        ComboboxHolder holder = new ComboboxHolder(entity, field);
        if (holder.isEnum() || holder.isEnumCollection()) {
            type = classPool.getOrNull(holder.getEnumName());
            if (holder.isEnumCollection()) {
                genericSignature = JavassistUtil.genericSignature(holder.getTypeClassName(), holder.getEnumName());
                type = classPool.getOrNull(holder.getTypeClassName());
            }
        } else if (holder.isStringCollection()) {
            genericSignature = JavassistUtil.genericSignature(holder.getTypeClassName(), holder.getUnderlyingType());
            type = classPool.getOrNull(holder.getTypeClassName());
        } else if (holder.isString()) {
            type = classPool.getOrNull(holder.getUnderlyingType());
        }
    } else if (fieldType.isRelationship()) {
        Class fieldClass = getClass().getClassLoader().loadClass(fieldType.getTypeClass());
        Relationship relationshipType = (Relationship) fieldClass.newInstance();
        genericSignature = relationshipType.getGenericSignature(field, entityType);
        type = classPool.getOrNull(relationshipType.getFieldType(field, entityType));
    } else {
        type = classPool.getOrNull(fieldType.getTypeClass());
    }
    return JavassistBuilder.createField(declaring, type, field.getBasic().getName(), genericSignature);
}
Also used : CtClass(javassist.CtClass) ComboboxHolder(org.motechproject.mds.domain.ComboboxHolder) Relationship(org.motechproject.mds.domain.Relationship) TypeDto(org.motechproject.mds.dto.TypeDto) CtClass(javassist.CtClass)

Example 14 with ComboboxHolder

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

the class EntityMetadataBuilderImpl method setComboboxMetadata.

private FieldMetadata setComboboxMetadata(ClassMetadata cmd, EntityDto entity, FieldDto field, Class<?> definition, EntityType entityType) {
    ComboboxHolder holder = new ComboboxHolder(entity, field);
    String fieldName = getNameForMetadata(field);
    FieldMetadata fmd = cmd.newFieldMetadata(fieldName);
    if (holder.isCollection()) {
        addDefaultFetchGroupMetadata(fmd, definition);
        fmd.setTable(ClassTableName.getTableName(cmd.getTable(), fieldName));
        JoinMetadata jm = fmd.newJoinMetadata();
        ForeignKeyMetadata fkmd = getOrCreateFkMetadata(jm);
        fkmd.setName(KeyNames.cbForeignKeyName(entity.getName(), entity.getId(), fieldName, entityType));
        jm.setDeleteAction(ForeignKeyAction.CASCADE);
        jm.newColumnMetadata().setName(fieldName + "_OID");
    }
    return fmd;
}
Also used : FieldMetadata(javax.jdo.metadata.FieldMetadata) ForeignKeyMetadata(javax.jdo.metadata.ForeignKeyMetadata) ComboboxHolder(org.motechproject.mds.domain.ComboboxHolder) JoinMetadata(javax.jdo.metadata.JoinMetadata)

Example 15 with ComboboxHolder

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

ComboboxHolder (org.motechproject.mds.domain.ComboboxHolder)16 TypeDto (org.motechproject.mds.dto.TypeDto)6 FieldDto (org.motechproject.mds.dto.FieldDto)5 ArrayList (java.util.ArrayList)3 EntityDto (org.motechproject.mds.dto.EntityDto)3 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)3 CtClass (javassist.CtClass)2 ClassData (org.motechproject.mds.domain.ClassData)2 RelationshipHolder (org.motechproject.mds.domain.RelationshipHolder)2 Type (org.motechproject.mds.domain.Type)2 IOException (java.io.IOException)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 CannotCompileException (javassist.CannotCompileException)1 CtField (javassist.CtField)1 FieldMetadata (javax.jdo.metadata.FieldMetadata)1 ForeignKeyMetadata (javax.jdo.metadata.ForeignKeyMetadata)1