Search in sources :

Example 1 with ComboboxHolder

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

the class EntityBuilderImpl method createInitializer.

private CtField.Initializer createInitializer(EntityDto entity, FieldDto field) {
    TypeDto type = field.getType();
    CtField.Initializer initializer = null;
    if (type.isCombobox()) {
        ComboboxHolder holder = new ComboboxHolder(entity, field);
        if (holder.isStringCollection()) {
            initializer = JavassistBuilder.createCollectionInitializer(holder.getUnderlyingType(), field.getBasic().getDefaultValue());
        } else if (holder.isEnumCollection()) {
            Object defaultValue = field.getBasic().getDefaultValue();
            initializer = JavassistBuilder.createCollectionInitializer(holder.getEnumName(), EnumHelper.prefixEnumValues((Collection) defaultValue));
        } else if (holder.isString()) {
            initializer = JavassistBuilder.createInitializer(holder.getUnderlyingType(), field.getBasic().getDefaultValue().toString());
        } else if (holder.isEnum()) {
            initializer = JavassistBuilder.createEnumInitializer(holder.getEnumName(), EnumHelper.prefixEnumValue(field.getBasic().getDefaultValue().toString()));
        }
    } else if (!type.isRelationship()) {
        initializer = JavassistBuilder.createInitializer(type.getTypeClass(), field.getBasic().getDefaultValue());
    }
    return initializer;
}
Also used : CtField(javassist.CtField) ComboboxHolder(org.motechproject.mds.domain.ComboboxHolder) Collection(java.util.Collection) TypeDto(org.motechproject.mds.dto.TypeDto)

Example 2 with ComboboxHolder

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

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

the class JarGeneratorServiceImpl method getFieldsInfo.

private List<FieldInfo> getFieldsInfo(EntityDto entity, List<FieldDto> fields, AdvancedSettingsDto advancedSettings) {
    List<FieldInfo> fieldsInfo = new ArrayList<>();
    Collections.sort(fields, new UIDisplayFieldComparator(advancedSettings.getBrowsing().getDisplayedFields()));
    for (FieldDto field : fields) {
        FieldInfo fieldInfo = new FieldInfo();
        fieldInfo.setField(field);
        boolean exposedByRest = advancedSettings.isFieldExposedByRest(field.getBasic().getName());
        fieldInfo.setRestExposed(exposedByRest);
        FieldInfo.TypeInfo typeInfo = fieldInfo.getTypeInfo();
        typeInfo.setType(field.getType().getTypeClass());
        typeInfo.setTaskType(ActionParameterTypeResolver.resolveType(entity, field));
        // combobox values
        typeInfo.setCombobox(field.getType().isCombobox());
        if (field.getType().isCombobox()) {
            ComboboxHolder cbHolder = new ComboboxHolder(entity, field);
            String[] items = cbHolder.getValues();
            if (ArrayUtils.isNotEmpty(items)) {
                typeInfo.setItems(Arrays.asList(items));
            }
            typeInfo.setAllowsMultipleSelection(cbHolder.isCollection());
            typeInfo.setAllowUserSupplied(cbHolder.isAllowUserSupplied());
        }
        fieldsInfo.add(fieldInfo);
    }
    return fieldsInfo;
}
Also used : ComboboxHolder(org.motechproject.mds.domain.ComboboxHolder) ArrayList(java.util.ArrayList) FieldInfo(org.motechproject.mds.entityinfo.FieldInfo) UIDisplayFieldComparator(org.motechproject.mds.dto.UIDisplayFieldComparator) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 4 with ComboboxHolder

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

the class EnumBuilderImplTest method testBuild.

@Test
public void testBuild() throws Exception {
    String[] expectedValues = { "A", "B", "C", "D", "E" };
    TypeSetting typeSetting = new TypeSetting();
    typeSetting.setName(Constants.Settings.COMBOBOX_VALUES);
    FieldSetting fieldSetting = new FieldSetting(null, typeSetting, StringUtils.join(expectedValues, "\n"));
    Type type = new Type("mds.field.combobox", null, null);
    Field field = new Field(null, "swag", "swag", false, false, false, false, false, null, null, null, null);
    field.setType(type);
    field.addSetting(fieldSetting);
    Entity entity = new Entity(CLASS_NAME);
    entity.addField(field);
    ComboboxHolder holder = new ComboboxHolder(entity, field);
    ClassData data = new EnumBuilderImpl().build(holder);
    MDSClassLoader.getInstance().safeDefineClass(data.getClassName(), data.getBytecode());
    Class<?> enumClass = MDSClassLoader.getInstance().loadClass(data.getClassName());
    assertTrue("The class definition should be enum", enumClass.isEnum());
    Object[] enumConstants = enumClass.getEnumConstants();
    String[] actualValues = new String[enumConstants.length];
    for (int i = 0; i < enumConstants.length; i++) {
        actualValues[i] = enumConstants[i].toString();
    }
    Arrays.sort(expectedValues, String.CASE_INSENSITIVE_ORDER);
    Arrays.sort(actualValues, String.CASE_INSENSITIVE_ORDER);
    assertArrayEquals(expectedValues, actualValues);
}
Also used : Entity(org.motechproject.mds.domain.Entity) ComboboxHolder(org.motechproject.mds.domain.ComboboxHolder) FieldSetting(org.motechproject.mds.domain.FieldSetting) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) ClassData(org.motechproject.mds.domain.ClassData) TypeSetting(org.motechproject.mds.domain.TypeSetting) Test(org.junit.Test)

Example 5 with ComboboxHolder

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

the class ComboboxValueServiceImpl method getAllValuesForCombobox.

@Override
@Transactional
public List<String> getAllValuesForCombobox(EntityDto entityDto, FieldDto fieldDto) {
    if (entityDto == null || fieldDto == null || !fieldDto.getType().isCombobox()) {
        throw new IllegalArgumentException("An existing entity and a combobox field are required");
    }
    ComboboxHolder cbHolder = new ComboboxHolder(entityDto, fieldDto);
    Set<String> options = new LinkedHashSet<>();
    String[] values = cbHolder.getValues();
    options.addAll(Arrays.asList(values));
    // as options
    if (cbHolder.isAllowUserSupplied()) {
        try {
            List<String> optionsFromDb;
            if (cbHolder.isAllowMultipleSelections()) {
                String cbTableName = metadataService.getComboboxTableName(entityDto.getClassName(), fieldDto.getBasic().getName());
                optionsFromDb = cbValueRepository.getComboboxValuesForCollection(cbTableName);
            } else {
                optionsFromDb = cbValueRepository.getComboboxValuesForStringField(entityDto, fieldDto);
            }
            options.addAll(optionsFromDb);
        } catch (RuntimeException e) {
            // we don't want to break in this case, so we just return the predefined values
            // after logging the exception
            LOGGER.error("Unable to retrieve combobox values from the database for field {} in entity {}", fieldDto.getBasic().getName(), entityDto.getClassName(), e);
        }
    }
    return new ArrayList<>(options);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ComboboxHolder(org.motechproject.mds.domain.ComboboxHolder) ArrayList(java.util.ArrayList) Transactional(org.springframework.transaction.annotation.Transactional)

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