Search in sources :

Example 31 with EnumType

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

the class EffectiveMapping method createMultiDimSet.

public MultiDim createMultiDimSet(ColumnSet columnSet) {
    EnumType enumType = (EnumType) this.formula.getResultType();
    int numItems = enumType.getValues().size();
    int numCategories;
    if (model.getMissingIncluded()) {
        // If we are included cases with no values set, then consider it
        // a category on its own
        numCategories = numItems + 1;
    } else {
        numCategories = numItems;
    }
    String[] labels = new String[numCategories];
    BitSet[] bitSets = new BitSet[numCategories];
    List<EnumItem> values = enumType.getValues();
    for (int j = 0; j < numItems; j++) {
        BitSet bitSet = new BitSet();
        EnumItem enumItem = values.get(j);
        ColumnView columnView = columnSet.getColumnView(getColumnId(enumItem));
        assert columnView != null;
        assert columnView.getType() == ColumnType.BOOLEAN;
        for (int i = 0; i < columnView.numRows(); i++) {
            if (columnView.getBoolean(i) == ColumnView.TRUE) {
                bitSet.set(i, true);
            }
        }
        labels[j] = enumItem.getLabel();
        bitSets[j] = bitSet;
    }
    if (model.getMissingIncluded()) {
        labels[numCategories - 1] = getMissingLabel();
        bitSets[numCategories - 1] = computeMissingBitSet(columnSet.getNumRows(), numItems, bitSets);
    }
    return new MultiDim(this.index, labels, bitSets);
}
Also used : EnumType(org.activityinfo.model.type.enumerated.EnumType) ColumnView(org.activityinfo.model.query.ColumnView) EnumItem(org.activityinfo.model.type.enumerated.EnumItem)

Example 32 with EnumType

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

the class EffectiveMapping method getRequiredColumns.

public List<ColumnModel> getRequiredColumns() {
    if (multiValued) {
        List<ColumnModel> columns = new ArrayList<>();
        EnumType enumType = (EnumType) this.formula.getResultType();
        for (EnumItem enumItem : enumType.getValues()) {
            ColumnModel columnModel = new ColumnModel();
            columnModel.setId(getColumnId(enumItem));
            columnModel.setFormula(new CompoundExpr(this.formula.getRootNode(), new SymbolNode(enumItem.getId())));
            columns.add(columnModel);
        }
        return columns;
    }
    if (this.mapping != null && this.formula.isValid()) {
        ColumnModel columnModel = new ColumnModel();
        columnModel.setId(getColumnId());
        columnModel.setFormula(this.formula.getFormula());
        return Collections.singletonList(columnModel);
    }
    return Collections.emptyList();
}
Also used : CompoundExpr(org.activityinfo.model.formula.CompoundExpr) SymbolNode(org.activityinfo.model.formula.SymbolNode) EnumType(org.activityinfo.model.type.enumerated.EnumType) ColumnModel(org.activityinfo.model.query.ColumnModel) EnumItem(org.activityinfo.model.type.enumerated.EnumItem)

Example 33 with EnumType

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

the class PivotAdapter method findAttributeFilterNames.

/**
 * Maps attribute filter ids to their attribute group id and name.
 *
 * Attribute filters are _SERIALIZED_ as only the integer ids of the required attributes,
 * but they are actually applied by _NAME_ to all forms in the query.
 */
private void findAttributeFilterNames() {
    Set<Integer> attributeIds = filter.getRestrictions(DimensionType.Attribute);
    if (attributeIds.isEmpty()) {
        return;
    }
    for (FormTree formTree : formTrees.values()) {
        for (FormTree.Node node : formTree.getLeaves()) {
            if (node.isEnum()) {
                EnumType type = (EnumType) node.getType();
                for (EnumItem enumItem : type.getValues()) {
                    int attributeId = CuidAdapter.getLegacyIdFromCuid(enumItem.getId());
                    if (attributeIds.contains(attributeId)) {
                        attributeFilters.put(node.getField().getLabel(), enumItem.getLabel());
                        attributeIds.remove(attributeId);
                        if (attributeIds.isEmpty()) {
                            return;
                        }
                    }
                }
            }
        }
    }
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) EnumType(org.activityinfo.model.type.enumerated.EnumType) EnumItem(org.activityinfo.model.type.enumerated.EnumItem)

Example 34 with EnumType

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

the class MySqlUpdateTest method addNewAttributes.

@Test
public void addNewAttributes() {
    KeyGenerator generator = new KeyGenerator();
    int activityId = generator.generateInt();
    EnumType enumType = new EnumType(Cardinality.SINGLE, new EnumItem(EnumItem.generateId(), "A"), new EnumItem(EnumItem.generateId(), "B"));
    FormField selectField = new FormField(ResourceId.generateFieldId(EnumType.TYPE_CLASS)).setType(enumType).setLabel("Select");
    FormClass formClass = new FormClass(CuidAdapter.activityFormClass(activityId));
    formClass.setDatabaseId(1);
    formClass.setLabel("New Form");
    formClass.addElement(selectField);
    catalog.createOrUpdateFormSchema(formClass);
    System.out.println("Created activity " + activityId);
    // Now change the enum items
    EnumType updatedType = new EnumType(Cardinality.SINGLE, new EnumItem(EnumItem.generateId(), "C"), new EnumItem(EnumItem.generateId(), "D"));
    selectField.setType(updatedType);
    newRequest();
    catalog.createOrUpdateFormSchema(formClass);
    newRequest();
    // Now try to save a new instance with the value
    FieldValue valueC = new EnumValue(updatedType.getValues().get(0).getId());
    FormInstance newRecord = new FormInstance(CuidAdapter.generateSiteCuid(), formClass.getId());
    newRecord.set(selectField.getId(), new EnumValue(updatedType.getValues().get(0).getId()));
    newRecord.set(CuidAdapter.partnerField(activityId), CuidAdapter.partnerRef(1, 1));
    executeUpdate(newRecord);
    // Ensure that the select field has been saved
    FormRecord saved = catalog.getForm(formClass.getId()).get().get(newRecord.getId()).get();
    FormInstance savedInstance = FormInstance.toFormInstance(formClass, saved);
    assertThat(savedInstance.get(selectField.getId()), equalTo(valueC));
}
Also used : EnumType(org.activityinfo.model.type.enumerated.EnumType) FormClass(org.activityinfo.model.form.FormClass) EnumValue(org.activityinfo.model.type.enumerated.EnumValue) FieldValue(org.activityinfo.model.type.FieldValue) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) FormInstance(org.activityinfo.model.form.FormInstance) FormRecord(org.activityinfo.model.form.FormRecord) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) FormField(org.activityinfo.model.form.FormField) GeoPoint(org.activityinfo.model.type.geo.GeoPoint) Test(org.junit.Test)

Example 35 with EnumType

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

the class ActivityLoader method createEnumType.

private EnumType createEnumType(ResultSet rs, Map<Integer, List<EnumItem>> attributes) throws SQLException {
    Cardinality cardinality;
    if (rs.getBoolean("multipleAllowed")) {
        cardinality = Cardinality.MULTIPLE;
    } else {
        cardinality = Cardinality.SINGLE;
    }
    List<EnumItem> enumValues = attributes.get(rs.getInt("id"));
    if (enumValues == null) {
        enumValues = Lists.newArrayList();
    }
    return new EnumType(cardinality, enumValues);
}
Also used : Cardinality(org.activityinfo.model.type.Cardinality) EnumType(org.activityinfo.model.type.enumerated.EnumType) EnumItem(org.activityinfo.model.type.enumerated.EnumItem)

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