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();
}
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);
}
});
}
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());
}
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;
}
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);
}
Aggregations