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