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;
}
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();
}
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;
}
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);
}
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);
}
Aggregations