Search in sources :

Example 16 with EnumType

use of org.activityinfo.model.type.enumerated.EnumType in project activityinfo by bedatadriven.

the class SiteHistoryReader method parseChanges.

private Map<ResourceId, FieldValue> parseChanges(JsonValue jsonObject) {
    Map<ResourceId, ResourceId> attributeToFieldMap = new HashMap<>();
    for (ActivityField activityField : activity.getAttributeAndIndicatorFields()) {
        if (activityField.getFormField().getType() instanceof EnumType) {
            EnumType type = (EnumType) activityField.getFormField().getType();
            for (EnumItem enumItem : type.getValues()) {
                attributeToFieldMap.put(enumItem.getId(), activityField.getResourceId());
            }
        }
    }
    Map<ResourceId, FieldValue> valueMap = new HashMap<>();
    Multimap<ResourceId, ResourceId> attributeValueMap = HashMultimap.create();
    for (Map.Entry<String, JsonValue> jsonEntry : jsonObject.entrySet()) {
        String fieldName = jsonEntry.getKey();
        if (fieldName.equals("comments")) {
            valueMap.put(fieldId(CuidAdapter.COMMENT_FIELD), NarrativeValue.valueOf(parseString(jsonEntry.getValue())));
        } else if (fieldName.equals("date1")) {
            valueMap.put(fieldId(CuidAdapter.START_DATE_FIELD), parseDate(jsonEntry.getValue()));
        } else if (fieldName.equals("date2")) {
            valueMap.put(fieldId(CuidAdapter.START_DATE_FIELD), parseDate(jsonEntry.getValue()));
        } else if (fieldName.equals("partnerId")) {
            valueMap.put(fieldId(CuidAdapter.PARTNER_FIELD), parseRef(jsonEntry.getValue(), activity.getPartnerFormClassId(), CuidAdapter.PARTNER_DOMAIN));
        } else if (fieldName.equals("projectId")) {
            valueMap.put(fieldId(CuidAdapter.PROJECT_FIELD), parseRef(jsonEntry.getValue(), activity.getProjectFormClassId(), CuidAdapter.PROJECT_DOMAIN));
        } else if (fieldName.equals("locationId")) {
            valueMap.put(fieldId(CuidAdapter.LOCATION_FIELD), parseRef(jsonEntry.getValue(), activity.getLocationFormClassId(), CuidAdapter.LOCATION_DOMAIN));
        } else if (fieldName.startsWith("I")) {
            int mIndex = fieldName.indexOf("M");
            if (mIndex == -1) {
                int indicatorId = Integer.parseInt(fieldName.substring(1));
                ResourceId fieldId = CuidAdapter.indicatorField(indicatorId);
                valueMap.put(fieldId, parseQuantity(jsonEntry.getValue()));
            } else {
                // old history
                valueMap.put(ResourceId.valueOf(fieldName), parseQuantity(jsonEntry.getValue()));
            }
        } else if (fieldName.startsWith("ATTRIB")) {
            if (parseBoolean(jsonEntry.getValue())) {
                int attributeId = Integer.parseInt(fieldName.substring("ATTRIB".length()));
                ResourceId attributeCuid = CuidAdapter.attributeId(attributeId);
                ResourceId fieldId = attributeToFieldMap.get(attributeCuid);
                if (fieldId != null) {
                    attributeValueMap.put(fieldId, attributeCuid);
                }
            }
        }
    }
    for (ResourceId fieldId : attributeValueMap.keySet()) {
        valueMap.put(fieldId, new EnumValue(attributeValueMap.get(fieldId)));
    }
    return valueMap;
}
Also used : HashMap(java.util.HashMap) EnumValue(org.activityinfo.model.type.enumerated.EnumValue) JsonValue(org.activityinfo.json.JsonValue) ResourceId(org.activityinfo.model.resource.ResourceId) EnumType(org.activityinfo.model.type.enumerated.EnumType) FieldValue(org.activityinfo.model.type.FieldValue) ActivityField(org.activityinfo.store.mysql.metadata.ActivityField) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) HashMap(java.util.HashMap) Map(java.util.Map)

Example 17 with EnumType

use of org.activityinfo.model.type.enumerated.EnumType in project activityinfo by bedatadriven.

the class SideColumnBuilder method add.

public void add(ActivityField field, final CursorObserver<FieldValue> observer) {
    ValueBuffer buffer = fieldMap.get(field.getId());
    if (buffer == null) {
        buffer = createBuffer(field.getFormField().getType(), observer);
        fieldMap.put(field.getId(), buffer);
        if (field.isAttributeGroup()) {
            // Map each individual attribute id to this buffer so we don't need to query
            // the attribute group from the database
            EnumType enumType = (EnumType) field.getFormField().getType();
            for (EnumItem enumItem : enumType.getValues()) {
                int attributeId = CuidAdapter.getLegacyIdFromCuid(enumItem.getId());
                attributeMap.put(attributeId, buffer);
            }
        }
    }
    buffer.add(observer);
}
Also used : EnumType(org.activityinfo.model.type.enumerated.EnumType) EnumItem(org.activityinfo.model.type.enumerated.EnumItem)

Example 18 with EnumType

use of org.activityinfo.model.type.enumerated.EnumType in project activityinfo by bedatadriven.

the class AttributeValueTableUpdater method update.

public void update(ResourceId fieldId, FieldValue value) {
    Preconditions.checkArgument(fieldId.getDomain() == CuidAdapter.ATTRIBUTE_GROUP_FIELD_DOMAIN);
    int attributeGroupId = CuidAdapter.getLegacyIdFromCuid(fieldId);
    ActivityField field = activity.getAttributeGroupField(attributeGroupId);
    EnumType enumType = (EnumType) field.getFormField().getType();
    for (EnumItem enumItem : enumType.getValues()) {
        attributesToClear.add(CuidAdapter.getLegacyIdFromCuid(enumItem.getId()));
    }
    add(value);
}
Also used : EnumType(org.activityinfo.model.type.enumerated.EnumType) ActivityField(org.activityinfo.store.mysql.metadata.ActivityField) EnumItem(org.activityinfo.model.type.enumerated.EnumItem)

Example 19 with EnumType

use of org.activityinfo.model.type.enumerated.EnumType 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 EnumType

use of org.activityinfo.model.type.enumerated.EnumType 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

EnumType (org.activityinfo.model.type.enumerated.EnumType)39 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)30 FormField (org.activityinfo.model.form.FormField)14 FormClass (org.activityinfo.model.form.FormClass)11 ResourceId (org.activityinfo.model.resource.ResourceId)7 QuantityType (org.activityinfo.model.type.number.QuantityType)7 Test (org.junit.Test)7 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)5 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)5 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)4 CuidAdapter.activityFormClass (org.activityinfo.model.legacy.CuidAdapter.activityFormClass)3 JsonValue (org.activityinfo.json.JsonValue)2 CreateResult (org.activityinfo.legacy.shared.command.result.CreateResult)2 ColumnView (org.activityinfo.model.query.ColumnView)2 Cardinality (org.activityinfo.model.type.Cardinality)2 FieldType (org.activityinfo.model.type.FieldType)2 FieldValue (org.activityinfo.model.type.FieldValue)2 NarrativeType (org.activityinfo.model.type.NarrativeType)2 ReferenceType (org.activityinfo.model.type.ReferenceType)2 TextType (org.activityinfo.model.type.primitive.TextType)2