Search in sources :

Example 16 with QuantityType

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()));
}
Also used : QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) Test(org.junit.Test)

Example 17 with QuantityType

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);
}
Also used : QuantityType(org.activityinfo.model.type.number.QuantityType) FormField(org.activityinfo.model.form.FormField)

Example 18 with QuantityType

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;
}
Also used : CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) QuantityType(org.activityinfo.model.type.number.QuantityType) FormField(org.activityinfo.model.form.FormField)

Example 19 with QuantityType

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);
    }
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) FormClassProvider(org.activityinfo.model.formTree.FormClassProvider) CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) QuantityType(org.activityinfo.model.type.number.QuantityType) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) EnumType(org.activityinfo.model.type.enumerated.EnumType) FileOutputStream(java.io.FileOutputStream) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) Test(org.junit.Test)

Example 20 with QuantityType

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);
    }
}
Also used : CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) NarrativeType(org.activityinfo.model.type.NarrativeType) QuantityType(org.activityinfo.model.type.number.QuantityType) LocalDateType(org.activityinfo.model.type.time.LocalDateType) EnumType(org.activityinfo.model.type.enumerated.EnumType) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) FieldType(org.activityinfo.model.type.FieldType) TextType(org.activityinfo.model.type.primitive.TextType)

Aggregations

QuantityType (org.activityinfo.model.type.number.QuantityType)34 FormClass (org.activityinfo.model.form.FormClass)20 FormField (org.activityinfo.model.form.FormField)19 Test (org.junit.Test)18 ResourceId (org.activityinfo.model.resource.ResourceId)13 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)12 EnumType (org.activityinfo.model.type.enumerated.EnumType)8 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)7 Quantity (org.activityinfo.model.type.number.Quantity)7 QueryModel (org.activityinfo.model.query.QueryModel)6 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)6 JsonValue (org.activityinfo.json.JsonValue)5 CuidAdapter.activityFormClass (org.activityinfo.model.legacy.CuidAdapter.activityFormClass)5 ColumnSet (org.activityinfo.model.query.ColumnSet)5 ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)4 NullFormSupervisor (org.activityinfo.store.query.shared.NullFormSupervisor)4 TypedRecordUpdate (org.activityinfo.store.spi.TypedRecordUpdate)4 CreateResult (org.activityinfo.legacy.shared.command.result.CreateResult)3 FieldType (org.activityinfo.model.type.FieldType)3 TextType (org.activityinfo.model.type.primitive.TextType)3