Search in sources :

Example 1 with QuantityType

use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.

the class FormFieldWidgetFactory method createWidget.

public Promise<? extends FormFieldWidget> createWidget(FormClass formClass, FormField field, FieldUpdater valueUpdater) {
    FieldType type = field.getType();
    if (type instanceof QuantityType) {
        return Promise.resolved(new QuantityFieldWidget((QuantityType) type, valueUpdater));
    } else if (type instanceof SerialNumberType) {
        return Promise.resolved(new SerialNumberFieldWidget((SerialNumberType) type));
    } else if (type instanceof NarrativeType) {
        return Promise.resolved(new NarrativeFieldWidget(valueUpdater));
    } else if (type instanceof TextType) {
        return Promise.resolved(new TextFieldWidget((TextType) type, valueUpdater));
    } else if (type instanceof CalculatedFieldType) {
        return Promise.resolved(new CalculatedFieldWidget(valueUpdater));
    } else if (type instanceof LocalDateType) {
        return Promise.resolved(new DateFieldWidget(valueUpdater));
    } else if (type instanceof LocalDateIntervalType) {
        return Promise.resolved(new DateIntervalFieldWidget(valueUpdater));
    } else if (type instanceof GeoPointType) {
        return Promise.resolved(new GeographicPointWidget(valueUpdater));
    } else if (type instanceof EnumType) {
        return Promise.resolved(new EnumFieldWidget((EnumType) field.getType(), valueUpdater, fieldWidgetMode));
    } else if (type instanceof BooleanType) {
        return Promise.resolved(new BooleanFieldWidget(valueUpdater));
    } else if (type instanceof AttachmentType) {
        AttachmentType attachmentType = (AttachmentType) type;
        if (attachmentType.getKind() == AttachmentType.Kind.IMAGE) {
            return Promise.resolved(new ImageUploadFieldWidget(formClass.getId(), valueUpdater, fieldWidgetMode));
        } else {
            return Promise.resolved(new AttachmentUploadFieldWidget(formClass.getId(), valueUpdater, fieldWidgetMode));
        }
    } else if (type instanceof ReferenceType) {
        return createReferenceWidget(field, valueUpdater);
    } else if (type instanceof BarcodeType) {
        return Promise.resolved(new BarcodeFieldWidget(valueUpdater));
    }
    Log.error("Unexpected field type " + type.getTypeClass());
    throw new UnsupportedOperationException();
}
Also used : SerialNumberType(org.activityinfo.model.type.SerialNumberType) ReferenceType(org.activityinfo.model.type.ReferenceType) AttachmentUploadFieldWidget(org.activityinfo.ui.client.component.form.field.attachment.AttachmentUploadFieldWidget) ImageUploadFieldWidget(org.activityinfo.ui.client.component.form.field.attachment.ImageUploadFieldWidget) NarrativeType(org.activityinfo.model.type.NarrativeType) EnumType(org.activityinfo.model.type.enumerated.EnumType) CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) AttachmentType(org.activityinfo.model.type.attachment.AttachmentType) BooleanType(org.activityinfo.model.type.primitive.BooleanType) LocalDateIntervalType(org.activityinfo.model.type.time.LocalDateIntervalType) FieldType(org.activityinfo.model.type.FieldType) CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) TextType(org.activityinfo.model.type.primitive.TextType) GeoPointType(org.activityinfo.model.type.geo.GeoPointType) QuantityType(org.activityinfo.model.type.number.QuantityType) LocalDateType(org.activityinfo.model.type.time.LocalDateType) BarcodeType(org.activityinfo.model.type.barcode.BarcodeType)

Example 2 with QuantityType

use of org.activityinfo.model.type.number.QuantityType 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 3 with QuantityType

use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.

the class CurlExamplesGenerator method getFormSchema.

public static Example getFormSchema() {
    FormClass exampleForm = new FormClass(ResourceId.generateId()).setDatabaseId(54).setLabel("NFI Distribution").setDescription("Form for collecting results of NFI distributions in North Kivu");
    exampleForm.addField(ResourceId.generateId()).setLabel("Date of Distribution").setType(LocalDateType.INSTANCE).setRequired(true);
    exampleForm.addField(CuidAdapter.partnerField(33)).setLabel("Partner").setType(ReferenceType.single(CuidAdapter.partnerFormId(54))).setRequired(true).setDescription("The implementing partner who conducted the distribution");
    exampleForm.addField(ResourceId.generateId()).setLabel("Donor").setType(new EnumType(Cardinality.SINGLE, new EnumItem(ResourceId.generateId(), "USAID"), new EnumItem(ResourceId.generateId(), "DFID"), new EnumItem(ResourceId.generateId(), "ECHO"))).setRequired(true);
    exampleForm.addField(ResourceId.generateId()).setLabel("Number of households receiving a kit").setType(new QuantityType("households")).setRequired(true);
    return new Example("curl https://www.activityinfo.org/form/" + exampleForm.getId() + "/schema", exampleForm.toJson());
}
Also used : QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) EnumType(org.activityinfo.model.type.enumerated.EnumType) EnumItem(org.activityinfo.model.type.enumerated.EnumItem)

Example 4 with QuantityType

use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.

the class GeoPointType method getFormClass.

@Override
public FormClass getFormClass() {
    FormClass formClass = new FormClass(ResourceId.valueOf("geoPoint"));
    formClass.setLabel(I18N.CONSTANTS.geographicCoordinatesFieldLabel());
    formClass.addField(ResourceId.valueOf(LATITUDE)).setCode(LATITUDE).setLabel(I18N.CONSTANTS.latitude()).setType(new QuantityType("degrees")).setRequired(true);
    formClass.addField(ResourceId.valueOf(LONGITUDE)).setCode(LONGITUDE).setLabel(I18N.CONSTANTS.longitude()).setType(new QuantityType("degrees")).setRequired(true);
    return formClass;
}
Also used : QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass)

Example 5 with QuantityType

use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.

the class ActivityTest method orderIndicatorsActivities.

@Test
public void orderIndicatorsActivities() {
    SchemaDTO schema = execute(new GetSchema());
    UserDatabaseDTO db = schema.getDatabaseById(1);
    LocationTypeDTO locType = schema.getCountryById(1).getLocationTypes().get(0);
    ActivityFormDTO act = new ActivityFormDTO();
    act.setName("Household Survey");
    act.setLocationType(locType);
    act.setReportingFrequency(ActivityFormDTO.REPORT_ONCE);
    CreateResult createResult = execute(CreateEntity.Activity(db, act));
    ResourceId classId = activityFormClass(createResult.getNewId());
    FormClass formClass = assertResolves(locator.getFormClass(classId));
    // create three new fields with an order that mixes "attributes" and "indicators"
    FormField newField = new FormField(ResourceId.generateFieldId(QuantityType.TYPE_CLASS));
    newField.setLabel("How old are you?");
    newField.setType(new QuantityType().setUnits("years"));
    formClass.addElement(newField);
    FormField newGenderField = new FormField(ResourceId.generateFieldId(EnumType.TYPE_CLASS));
    newGenderField.setLabel("Gender");
    EnumItem male = new EnumItem(EnumItem.generateId(), "Male");
    EnumItem female = new EnumItem(EnumItem.generateId(), "Female");
    newGenderField.setType(new EnumType(Cardinality.SINGLE, Arrays.asList(male, female)));
    formClass.addElement(newGenderField);
    FormField newTextField = new FormField(ResourceId.generateFieldId(TextType.TYPE_CLASS));
    newTextField.setLabel("What is your name?");
    newTextField.setType(TextType.SIMPLE);
    formClass.addElement(newTextField);
    assertResolves(locator.persist(formClass));
    TFormClass reform = new TFormClass(assertResolves(locator.getFormClass(formClass.getId())));
    System.out.println(Joiner.on("\n").join(reform.getFormClass().getFields()));
    int a = reform.indexOfField("How old are you?");
    int b = reform.indexOfField("Gender");
    int c = reform.indexOfField("What is your name?");
    assertTrue(a < b && b < c);
}
Also used : CreateResult(org.activityinfo.legacy.shared.command.result.CreateResult) ResourceId(org.activityinfo.model.resource.ResourceId) QuantityType(org.activityinfo.model.type.number.QuantityType) CuidAdapter.activityFormClass(org.activityinfo.model.legacy.CuidAdapter.activityFormClass) EnumType(org.activityinfo.model.type.enumerated.EnumType) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) Test(org.junit.Test)

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