Search in sources :

Example 16 with FormField

use of org.activityinfo.model.form.FormField in project activityinfo by bedatadriven.

the class FeatureSourceStorage method toField.

private FormField toField(int i, PropertyDescriptor descriptor) {
    FormField field = new FormField(ResourceId.valueOf(FIELD_ID_PREFIX + i));
    field.setLabel(descriptor.getName().getLocalPart());
    field.setCode(descriptor.getName().getLocalPart());
    field.setType(AttributeTypeAdapters.of(descriptor).createType());
    return field;
}
Also used : FormField(org.activityinfo.model.form.FormField)

Example 17 with FormField

use of org.activityinfo.model.form.FormField in project activityinfo by bedatadriven.

the class OldGetSitesHandler method joinCalculatedIndicatorValues.

private void joinCalculatedIndicatorValues(final Promise<Void> complete, SqlTransaction tx, final Multimap<Integer, SiteDTO> siteMap) {
    Log.trace("Starting joinIndicatorValues()");
    final Set<Integer> activityIds = Sets.newHashSet();
    for (SiteDTO siteDTO : siteMap.values()) {
        activityIds.add(siteDTO.getActivityId());
    }
    SqlQuery query = SqlQuery.select().appendColumn("I.IndicatorId", "indicatorId").appendColumn("I.Name", "indicatorName").appendColumn("I.ActivityId", "activityId").appendColumn("I.Type", "type").appendColumn("I.Expression", "expression").appendColumn("I.nameInExpression", "code").appendColumn("I.calculatedAutomatically", "calculatedAutomatically").from(Tables.INDICATOR, "I").where("I.ActivityId").in(activityIds).and("I.dateDeleted IS NULL").orderBy("I.SortOrder");
    Log.info(query.toString());
    query.execute(tx, new SqlResultCallback() {

        @Override
        public void onSuccess(SqlTransaction tx, final SqlResultSet results) {
            Multimap<Integer, FormField> fields = HashMultimap.create();
            for (SqlResultSetRow row : results.getRows()) {
                fields.put(row.getInt("activityId"), createField(row));
            }
            // Have to resolve symbols on a per-form basis
            for (Integer activityId : fields.keySet()) {
                Collection<FormField> activityFields = fields.get(activityId);
                FormSymbolTable symbolTable = new FormSymbolTable(activityFields);
                PartialEvaluator<SiteDTO> evaluator = new PartialEvaluator<>(symbolTable, new SiteFieldReaderFactory());
                List<CalculatedIndicatorReader> readers = Lists.newArrayList();
                for (FormField field : activityFields) {
                    if (field.getType() instanceof CalculatedFieldType) {
                        try {
                            FieldReader<SiteDTO> reader = evaluator.partiallyEvaluate(field);
                            if (reader.getType() instanceof QuantityType) {
                                readers.add(new CalculatedIndicatorReader(field, reader));
                            }
                        } catch (Exception e) {
                            // we don't want to fail whole GetSites command due to invalid expression.
                            Log.error("Failed to evaluate calculated field: " + field + ", expression: " + ((CalculatedFieldType) field.getType()).getExpression(), e);
                        }
                    }
                }
                for (SiteDTO site : siteMap.values()) {
                    for (CalculatedIndicatorReader reader : readers) {
                        reader.read(site);
                    }
                }
            }
            complete.onSuccess(null);
        }
    });
}
Also used : CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) SqlQuery(com.bedatadriven.rebar.sql.client.query.SqlQuery) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) PartialEvaluator(org.activityinfo.model.formula.eval.PartialEvaluator) SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) QuantityType(org.activityinfo.model.type.number.QuantityType) FormSymbolTable(org.activityinfo.model.formula.eval.FormSymbolTable) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback) FormField(org.activityinfo.model.form.FormField) FieldReader(org.activityinfo.model.formula.eval.FieldReader)

Example 18 with FormField

use of org.activityinfo.model.form.FormField in project activityinfo by bedatadriven.

the class AttributeGroupDTO method asFormField.

@Override
public FormField asFormField() {
    Cardinality cardinality = isMultipleAllowed() ? Cardinality.MULTIPLE : Cardinality.SINGLE;
    List<EnumItem> values = Lists.newArrayList();
    for (AttributeDTO attribute : getAttributes()) {
        values.add(new EnumItem(CuidAdapter.attributeId(attribute.getId()), attribute.getName()));
    }
    return new FormField(CuidAdapter.attributeGroupField(getId())).setLabel(getName()).setType(new EnumType(cardinality, values)).setRequired(isMandatory());
}
Also used : Cardinality(org.activityinfo.model.type.Cardinality) EnumType(org.activityinfo.model.type.enumerated.EnumType) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) FormField(org.activityinfo.model.form.FormField)

Example 19 with FormField

use of org.activityinfo.model.form.FormField in project activityinfo by bedatadriven.

the class BuiltinFields method getDateRange.

public static DateRange getDateRange(FormInstance instance, FormClass formClass) {
    Date startDate = null;
    Date endDate = null;
    for (FormField field : formClass.getFields()) {
        if (isStartDate(field.getId())) {
            LocalDate localDate = instance.getDate(field.getId());
            if (localDate != null) {
                startDate = localDate.atMidnightInMyTimezone();
            }
        }
        if (isEndDate(field.getId())) {
            LocalDate localDate = instance.getDate(field.getId());
            if (localDate != null) {
                endDate = localDate.atMidnightInMyTimezone();
            }
        }
    }
    return new DateRange(startDate, endDate);
}
Also used : DateRange(org.activityinfo.model.date.DateRange) FormField(org.activityinfo.model.form.FormField) LocalDate(org.activityinfo.model.type.time.LocalDate) LocalDate(org.activityinfo.model.type.time.LocalDate) Date(java.util.Date)

Example 20 with FormField

use of org.activityinfo.model.form.FormField in project activityinfo by bedatadriven.

the class LookupKeySet method addLevels.

private LookupKey addLevels(FormClass formClass) {
    ResourceId formId = formClass.getId();
    if (formMap.containsKey(formId)) {
        return formMap.get(formId);
    }
    // if serial number is present, we use that exclusively.
    Optional<FormField> serialNumberField = findSerialNumberField(formClass);
    if (serialNumberField.isPresent()) {
        LookupKey lookupKey = serialNumberLevel(formClass, serialNumberField.get());
        lookupKeys.add(lookupKey);
        return lookupKey;
    }
    LookupKey parentKey = null;
    String parentFieldId = null;
    // If there is a reference key, then we climb the reference tree recursively.
    Optional<FormField> referenceKey = findReferenceKey(formClass);
    if (referenceKey.isPresent()) {
        ReferenceType referenceType = (ReferenceType) referenceKey.get().getType();
        ResourceId referencedFormId = Iterables.getOnlyElement(referenceType.getRange());
        FormClass referencedFormClass = formTree.getFormClass(referencedFormId);
        parentMap.put(formId, referencedFormId);
        parentKey = addLevels(referencedFormClass);
        parentFieldId = referenceKey.get().getId().asString();
    }
    // Now check for text key fields
    for (FormField formField : formClass.getFields()) {
        if (isTextLikeKey(formField)) {
            LookupKey lookupKey = textKeyLevel(formClass, parentKey, parentFieldId, formField);
            lookupKeys.add(lookupKey);
            parentKey = lookupKey;
            parentFieldId = null;
        }
    }
    // If there is really no other key fields, then use the autogenerated id as a key
    if (parentKey == null) {
        parentKey = idLevel(formClass);
        lookupKeys.add(parentKey);
    }
    formMap.put(formId, parentKey);
    return parentKey;
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) FormField(org.activityinfo.model.form.FormField) ReferenceType(org.activityinfo.model.type.ReferenceType)

Aggregations

FormField (org.activityinfo.model.form.FormField)119 FormClass (org.activityinfo.model.form.FormClass)48 ResourceId (org.activityinfo.model.resource.ResourceId)32 Test (org.junit.Test)30 QuantityType (org.activityinfo.model.type.number.QuantityType)18 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)17 ReferenceType (org.activityinfo.model.type.ReferenceType)16 EnumType (org.activityinfo.model.type.enumerated.EnumType)14 FieldValue (org.activityinfo.model.type.FieldValue)12 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)11 JsonValue (org.activityinfo.json.JsonValue)9 FormTree (org.activityinfo.model.formTree.FormTree)9 FormInstance (org.activityinfo.model.form.FormInstance)8 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)8 ColumnSet (org.activityinfo.model.query.ColumnSet)6 QueryModel (org.activityinfo.model.query.QueryModel)6 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)5 Quantity (org.activityinfo.model.type.number.Quantity)5 TypedRecordUpdate (org.activityinfo.store.spi.TypedRecordUpdate)5 ArrayList (java.util.ArrayList)4