Search in sources :

Example 1 with ReferenceType

use of org.activityinfo.model.type.ReferenceType in project activityinfo by bedatadriven.

the class FormForest method collectReferenced.

private void collectReferenced(FormTree tree, Iterable<FormTree.Node> parentFields, Set<ResourceId> visited, List<FormClass> list) {
    for (FormTree.Node parentField : parentFields) {
        if (parentField.getType() instanceof ReferenceType) {
            for (ResourceId formId : parentField.getRange()) {
                if (!visited.contains(formId)) {
                    visited.add(formId);
                    // depth first...
                    collectReferenced(tree, parentField.getChildren(formId), visited, list);
                    // now ourselves
                    list.add(tree.getFormClass(formId));
                }
            }
        }
    }
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) ResourceId(org.activityinfo.model.resource.ResourceId) ReferenceType(org.activityinfo.model.type.ReferenceType)

Example 2 with ReferenceType

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

use of org.activityinfo.model.type.ReferenceType in project activityinfo by bedatadriven.

the class FormMappingBuilder method buildReferenceMapping.

/**
 * Builds a reference mapping for a given target node.
 *
 * <p>A reference field takes the value of a single {@code ResourceId}, but
 * most of the time we don't have the actual id of the field in the dataset to import:
 * we have to obtain the id by performing a look up against text fields in the
 * </p>
 *
 * @param targetField the reference field to look up
 */
private void buildReferenceMapping(final FormField targetField) {
    // In order to match against the ReferenceField, we need the actual data
    // from the form that is being referenced.
    // 
    // Example: If we have a "Location" field that references the "Province" form class,
    // then we need then names and/or codes of the Province form class in order to lookup the
    // ids, assuming that our source dataset has a "province name" column with the names of the provinces.
    ReferenceType type = (ReferenceType) targetField.getType();
    // Currently this only supports reference fields that reference exactly one form class.
    if (type.getCardinality() == Cardinality.SINGLE && type.getRange().size() == 1) {
        ResourceId referenceFormId = Iterables.getOnlyElement(type.getRange());
        Observable<FormProfile> lookupForm = FormProfile.profile(resourceStore, referenceFormId);
        Observable<FieldMapping> mapping = lookupForm.transform(new Function<FormProfile, FieldMapping>() {

            @Override
            public FieldMapping apply(FormProfile lookupForm) {
                return new ReferenceFieldMapping(targetField, KeyFieldPairSet.matchKeys(source, lookupForm), referenceMatches);
            }
        });
        mappings.add(mapping);
    }
}
Also used : FormProfile(org.activityinfo.geoadmin.merge2.view.profile.FormProfile) ResourceId(org.activityinfo.model.resource.ResourceId) ReferenceType(org.activityinfo.model.type.ReferenceType)

Example 4 with ReferenceType

use of org.activityinfo.model.type.ReferenceType 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)

Example 5 with ReferenceType

use of org.activityinfo.model.type.ReferenceType in project activityinfo by bedatadriven.

the class FormClassTest method oldSerialization.

@Test
public void oldSerialization() throws IOException {
    FormClass formClass = parseResource();
    assertThat(formClass.getId(), equalTo(ResourceId.valueOf("a0000000728")));
    assertThat(formClass.getLabel(), equalTo("Youth Group Formation"));
    FormField partnerField = formClass.getFields().get(0);
    assertThat(partnerField.getLabel(), equalTo("Partner"));
    assertThat(partnerField.getType(), instanceOf(ReferenceType.class));
    ReferenceType partnerFieldType = (ReferenceType) partnerField.getType();
    assertThat(partnerFieldType.getCardinality(), equalTo(Cardinality.SINGLE));
    assertThat(partnerFieldType.getRange(), hasItem(ResourceId.valueOf("P0000000326")));
    FormField calcField = formClass.getFields().get(5);
    assertThat(calcField.getLabel(), equalTo("Number of attendees"));
    assertThat(calcField.getType(), instanceOf(CalculatedFieldType.class));
    CalculatedFieldType calcFieldType = (CalculatedFieldType) calcField.getType();
    assertThat(calcFieldType.getExpression(), equalTo("3250+3249"));
}
Also used : CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) ReferenceType(org.activityinfo.model.type.ReferenceType) Test(org.junit.Test)

Aggregations

ReferenceType (org.activityinfo.model.type.ReferenceType)21 FormField (org.activityinfo.model.form.FormField)16 ResourceId (org.activityinfo.model.resource.ResourceId)10 FormClass (org.activityinfo.model.form.FormClass)6 FormTree (org.activityinfo.model.formTree.FormTree)6 Test (org.junit.Test)6 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)4 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)2 RecordRef (org.activityinfo.model.type.RecordRef)2 EnumType (org.activityinfo.model.type.enumerated.EnumType)2 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)2 QuantityType (org.activityinfo.model.type.number.QuantityType)2 Optional (com.google.common.base.Optional)1 ValueUpdater (com.google.gwt.cell.client.ValueUpdater)1 LinkedList (java.util.LinkedList)1 FormProfile (org.activityinfo.geoadmin.merge2.view.profile.FormProfile)1 FormSection (org.activityinfo.model.form.FormSection)1 FormClassProvider (org.activityinfo.model.formTree.FormClassProvider)1 FormMetadataProviderStub (org.activityinfo.model.formTree.FormMetadataProviderStub)1 LookupKeySet (org.activityinfo.model.formTree.LookupKeySet)1