use of org.activityinfo.model.type.enumerated.EnumItem in project activityinfo by bedatadriven.
the class XlsFormBuilderTest method test.
@Test
public void test() throws IOException {
final FormClass formClass = new FormClass(ResourceId.valueOf("F1"));
formClass.addField(ResourceId.valueOf("X1")).setLabel("What is your name?").setRequired(true).setCode("RespName").setType(TextType.SIMPLE);
formClass.addField(ResourceId.valueOf("X2")).setLabel("How old are you?").setRequired(true).setCode("Age").setType(new QuantityType("years"));
formClass.addField(ResourceId.valueOf("X3")).setLabel("Age in dog years").setRequired(true).setCode("AgeDogYears").setType(new CalculatedFieldType("Age*7"));
formClass.addField(ResourceId.valueOf("X4")).setLabel("Date of birth").setRequired(false).setCode("DOB").setType(LocalDateType.INSTANCE);
formClass.addField(ResourceId.valueOf("X5")).setLabel("What is your favorite color?").setRequired(false).setCode("Color").setType(new EnumType(Cardinality.SINGLE, Lists.newArrayList(new EnumItem(ResourceId.valueOf("Z1"), "Blue"), new EnumItem(ResourceId.valueOf("Z2"), "Red"), new EnumItem(ResourceId.valueOf("Z3"), "Green"))));
formClass.addField(ResourceId.valueOf("X6")).setLabel("Who are your brothers?").setCode("Brothers").setType(new SubFormReferenceType(ResourceId.valueOf("F2")));
formClass.addField(ResourceId.valueOf("X7")).setLabel("Likes any color?").setCode("Colorful").setType(BooleanType.INSTANCE).setRelevanceConditionExpression("containsAny(X5,Z1,Z2,Z3)");
formClass.addField(ResourceId.valueOf("X8")).setLabel("Likes the color blue?").setCode("BlueLover").setType(BooleanType.INSTANCE).setRelevanceConditionExpression("X5==Z1");
formClass.addField(ResourceId.valueOf("X9")).setLabel("Likes the color red?").setCode("RedLover").setType(BooleanType.INSTANCE).setRelevanceConditionExpression("X5==\'Z2\'");
final FormClass subFormClass = new FormClass(ResourceId.valueOf("F2"));
subFormClass.setLabel("Sub Form");
subFormClass.addField(ResourceId.valueOf("Y1")).setLabel("What is his name?").setType(TextType.SIMPLE).setCode("Name");
FormClassProvider provider = new FormClassProvider() {
@Override
public FormClass getFormClass(ResourceId formId) {
if (formId.equals(formClass.getId())) {
return formClass;
} else if (formId.equals(subFormClass.getId())) {
return subFormClass;
} else {
throw new IllegalArgumentException();
}
}
};
XlsFormBuilder writer = new XlsFormBuilder(provider);
writer.build(formClass.getId());
try (FileOutputStream fos = new FileOutputStream("test.xls")) {
writer.write(fos);
}
}
use of org.activityinfo.model.type.enumerated.EnumItem in project activityinfo by bedatadriven.
the class XlsFormBuilder method addChoices.
private void addChoices(String listName, EnumType enumType) {
for (EnumItem enumItem : enumType.getValues()) {
HSSFRow row = choicesSheet.createRow(nextChoiceRow++);
row.createCell(CHOICES_LIST_NAME_COLUMN).setCellValue(listName);
row.createCell(CHOICES_NAME_COLUMN).setCellValue(enumItem.getId().asString());
row.createCell(CHOICES_LABEL).setCellValue(enumItem.getLabel());
}
}
use of org.activityinfo.model.type.enumerated.EnumItem in project activityinfo by bedatadriven.
the class HrdCatalogTest method enumWithNoChoices.
@Test
public void enumWithNoChoices() {
final ResourceId formId = ResourceId.generateId();
ResourceId villageField = ResourceId.valueOf("FV");
final ResourceId selectField = ResourceId.valueOf("FC");
FormClass formClass = new FormClass(formId);
formClass.setParentFormId(ResourceId.valueOf("foo"));
formClass.setLabel("NFI Distributions");
formClass.addField(villageField).setLabel("Village name").setCode("VILLAGE").setType(TextType.SIMPLE);
formClass.addField(selectField).setLabel("Favorite color").setType(new EnumType(Cardinality.SINGLE, EnumType.Presentation.AUTOMATIC, Collections.<EnumItem>emptyList()));
HrdStorageProvider catalog = new HrdStorageProvider();
catalog.create(formClass);
// Avoid cache
// objectifyCloseable.close();
ObjectifyService.run(new VoidWork() {
@Override
public void vrun() {
HrdStorageProvider catalog = new HrdStorageProvider();
Optional<FormStorage> storage = catalog.getForm(formId);
FormClass deserializedSchema = storage.get().getFormClass();
}
});
}
use of org.activityinfo.model.type.enumerated.EnumItem in project activityinfo by bedatadriven.
the class MySqlRecordHistoryBuilder method render.
private String render(EnumType type, EnumValue value) {
StringBuilder sb = new StringBuilder();
boolean needsComma = false;
for (EnumItem enumItem : value.getValuesAsItems(type)) {
if (needsComma) {
sb.append(", ");
}
sb.append(enumItem.getLabel());
needsComma = true;
}
return sb.toString();
}
use of org.activityinfo.model.type.enumerated.EnumItem in project activityinfo by bedatadriven.
the class CloneDatabaseHandler method cloneEnumType.
/**
* Copies an {@link EnumType}, generating a new unique identifier each
* enumerated item, since the MySQL backend requires them to be globally unique.
*/
private FieldType cloneEnumType(EnumType sourceEnumType) {
List<EnumItem> targetValues = Lists.newArrayList();
for (EnumItem sourceValue : sourceEnumType.getValues()) {
ResourceId targetValueId = newEnumId(sourceValue);
targetValues.add(new EnumItem(targetValueId, sourceValue.getLabel()));
typeIdMapping.put(sourceValue.getId(), targetValueId);
}
return new EnumType(sourceEnumType.getCardinality(), targetValues);
}
Aggregations