Search in sources :

Example 6 with CalculatedFieldType

use of org.activityinfo.model.type.expr.CalculatedFieldType in project activityinfo by bedatadriven.

the class ActivityUpdater method insertIndicatorRow.

private void insertIndicatorRow(FormField formField, int sortOrder) {
    SqlInsert insert = SqlInsert.insertInto("indicator");
    insert.value("indicatorId", CuidAdapter.getLegacyIdFromCuid(formField.getId()));
    insert.value("activityId", activityId);
    insert.value("name", formField.getLabel());
    insert.value("nameInExpression", formField.getCode());
    insert.value("description", formField.getDescription());
    if (formField.getType() instanceof QuantityType) {
        insert.value("aggregation", ((QuantityType) formField.getType()).getAggregation().ordinal());
    } else {
        insert.value("aggregation", 0);
    }
    insert.value("sortOrder", sortOrder);
    insert.value("type", formField.getType().getTypeClass().getId());
    insert.value("mandatory", formField.isRequired());
    if (formField.getType() instanceof QuantityType) {
        QuantityType quantityType = (QuantityType) formField.getType();
        insert.value("units", quantityType.getUnits());
    }
    if (formField.getType() instanceof CalculatedFieldType) {
        CalculatedFieldType type = (CalculatedFieldType) formField.getType();
        insert.value("calculatedAutomatically", true);
        insert.value("expression", type.getExpression());
    }
    insert.execute(executor);
}
Also used : CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) QuantityType(org.activityinfo.model.type.number.QuantityType)

Example 7 with CalculatedFieldType

use of org.activityinfo.model.type.expr.CalculatedFieldType in project activityinfo by bedatadriven.

the class ActivityUpdater method updateIndicatorRow.

private void updateIndicatorRow(ActivityField existingField, FormField formField, int sortOrder) {
    SqlUpdate update = SqlUpdate.update("indicator");
    update.where("indicatorId", existingField.getId());
    update.setIfChanged("name", existingField.getFormField().getLabel(), formField.getLabel());
    update.setIfChanged("nameInExpression", existingField.getFormField().getCode(), formField.getCode(), 255);
    update.setIfChanged("sortOrder", existingField.getSortOrder(), sortOrder);
    update.setIfChanged("description", existingField.getFormField().getDescription(), formField.getDescription());
    update.setIfChanged("mandatory", existingField.getFormField().isRequired(), formField.isRequired());
    if (existingField.getFormField().getType() instanceof QuantityType) {
        QuantityType existingType = (QuantityType) existingField.getFormField().getType();
        QuantityType updatedType = (QuantityType) formField.getType();
        update.setIfChanged("units", existingType.getUnits(), updatedType.getUnits(), 255);
        update.setIfChanged("aggregation", existingType.getAggregation(), updatedType.getAggregation());
    }
    if (existingField.getFormField().getType() instanceof CalculatedFieldType) {
        CalculatedFieldType existingType = (CalculatedFieldType) existingField.getFormField().getType();
        CalculatedFieldType updatedType = (CalculatedFieldType) formField.getType();
        update.setIfChanged("expression", existingType.getExpression(), updatedType.getExpression());
    }
    update.execute(executor);
}
Also used : CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) QuantityType(org.activityinfo.model.type.number.QuantityType)

Example 8 with CalculatedFieldType

use of org.activityinfo.model.type.expr.CalculatedFieldType 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 9 with CalculatedFieldType

use of org.activityinfo.model.type.expr.CalculatedFieldType 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 10 with CalculatedFieldType

use of org.activityinfo.model.type.expr.CalculatedFieldType 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

CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)16 QuantityType (org.activityinfo.model.type.number.QuantityType)12 FormClass (org.activityinfo.model.form.FormClass)7 FormField (org.activityinfo.model.form.FormField)7 Test (org.junit.Test)6 EnumType (org.activityinfo.model.type.enumerated.EnumType)4 FieldType (org.activityinfo.model.type.FieldType)3 ColumnView (org.activityinfo.model.query.ColumnView)2 QueryModel (org.activityinfo.model.query.QueryModel)2 ResourceId (org.activityinfo.model.resource.ResourceId)2 NarrativeType (org.activityinfo.model.type.NarrativeType)2 ReferenceType (org.activityinfo.model.type.ReferenceType)2 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)2 Quantity (org.activityinfo.model.type.number.Quantity)2 TextType (org.activityinfo.model.type.primitive.TextType)2 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)2 LocalDateType (org.activityinfo.model.type.time.LocalDateType)2 ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)2 TypedRecordUpdate (org.activityinfo.store.spi.TypedRecordUpdate)2 SqlResultCallback (com.bedatadriven.rebar.sql.client.SqlResultCallback)1