use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.
the class ActivityLoaderTest method serializationTest.
@Test
public void serializationTest() throws IOException, ClassNotFoundException {
// Large form class
FormClass formClass = new FormClass(ResourceId.generateId());
formClass.setLabel("Widget survey");
for (int i = 0; i < 1000; i++) {
formClass.addField(ResourceId.generateId()).setLabel("How many widgets?").setType(new QuantityType("people"));
}
assertTrue(formClass.toJsonString().length() > 0xFFFF);
Activity activity = new Activity();
activity.serializedFormClass = new Activity.FormClassHolder();
activity.serializedFormClass.value = formClass;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(activity);
// Re-read
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
Activity reread = (Activity) ois.readObject();
assertThat(reread.getSerializedFormClass().toJsonString(), equalTo(formClass.toJsonString()));
}
use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.
the class DatabaseTargetForm method addIndicator.
public void addIndicator(int id, String name, String units) {
FormField field = new FormField(CuidAdapter.cuid(CuidAdapter.TARGET_INDICATOR_FIELD_DOMAIN, id));
field.setLabel(name);
field.setSuperProperty(CuidAdapter.indicatorField(id));
field.setType(new QuantityType(units));
indicatorFields.add(field);
fieldMap.put(id, field);
}
use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.
the class IndicatorDTO method asFormField.
@Override
public FormField asFormField() {
FormField field = new FormField(CuidAdapter.indicatorField(getId()));
field.setLabel(getName());
field.setDescription(getDescription());
field.setRelevanceConditionExpression(getRelevanceExpression());
field.setRequired(isMandatory());
field.setVisible(isVisible());
String code = getNameInExpression();
if (!Strings.isNullOrEmpty(code)) {
field.setCode(code);
}
if (isCalculated()) {
field.setType(new CalculatedFieldType(getExpression()));
} else if (Strings.isNullOrEmpty(getTypeId()) || getTypeId().equals(QuantityType.TYPE_CLASS.getId())) {
String units = getUnits();
if (units == null) {
units = "";
}
field.setType(new QuantityType().setUnits(units).setAggregation(getAggregation()));
} else {
field.setType(getType().createType());
}
return field;
}
use of org.activityinfo.model.type.number.QuantityType 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.number.QuantityType in project activityinfo by bedatadriven.
the class XlsFormBuilder method writeSimpleField.
private void writeSimpleField(FormField field) {
HSSFRow fieldRow = surveySheet.createRow(nextFieldRow++);
String name = field.getCode();
if (Strings.isNullOrEmpty(name)) {
name = field.getId().asString();
}
fieldRow.createCell(NAME_COLUMN).setCellValue(name);
fieldRow.createCell(LABEL_COLUMN).setCellValue(field.getLabel());
fieldRow.createCell(REQUIRED_COLUMN).setCellValue(field.isRequired() ? "yes" : "no");
FieldType type = field.getType();
if (type instanceof QuantityType) {
QuantityType quantityType = (QuantityType) field.getType();
fieldRow.createCell(TYPE_COLUMN).setCellValue(XlsFormTypes.DECIMAL);
fieldRow.createCell(UNITS_COLUMN).setCellValue(quantityType.getUnits());
} else if (type instanceof TextType) {
fieldRow.createCell(TYPE_COLUMN).setCellValue(XlsFormTypes.TEXT);
} else if (type instanceof NarrativeType) {
fieldRow.createCell(TYPE_COLUMN).setCellValue("text");
} else if (type instanceof CalculatedFieldType) {
CalculatedFieldType calculatedType = (CalculatedFieldType) field.getType();
fieldRow.createCell(TYPE_COLUMN).setCellValue(XlsFormTypes.CALCULATE);
fieldRow.createCell(CALCULATION_FIELD).setCellValue(calculatedType.getExpression());
} else if (type instanceof LocalDateType) {
fieldRow.createCell(TYPE_COLUMN).setCellValue(XlsFormTypes.DATE);
} else if (type instanceof EnumType) {
EnumType enumType = (EnumType) type;
String typeName;
if (enumType.getCardinality() == Cardinality.SINGLE) {
typeName = XlsFormTypes.SELECT_ONE;
} else {
typeName = XlsFormTypes.SELECT_MULTIPLE;
}
String listName = name;
fieldRow.createCell(TYPE_COLUMN).setCellValue(typeName + " " + listName);
addChoices(listName, enumType);
}
if (field.getRelevanceConditionExpression() != null) {
String xpathRelevanceCondition = xPathBuilder.build(field.getRelevanceConditionExpression());
fieldRow.createCell(RELEVANT_COLUMN).setCellValue(xpathRelevanceCondition);
}
}
Aggregations