Search in sources :

Example 56 with FormField

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

the class QuantityTypeTest method deserialization.

/**
 * Ensure QuantityType is parsed correctly from JSON.
 * In particular, ensure that the "aggregation" field is correctly initialised from a legacy schema with no
 * aggregation field within Quantity JSON element.
 *
 * @throws IOException
 */
@Test
public void deserialization() throws IOException {
    FormClass formClass = parseResource();
    ResourceId quantityFieldId = ResourceId.valueOf("i0000006090");
    FormField quantityField = formClass.getField(quantityFieldId);
    // Quantity Checks
    assertThat(quantityField.getLabel(), equalTo("Number of water points constructed"));
    assertThat(quantityField.isRequired(), is(false));
    assertThat(quantityField.isVisible(), is(true));
    // QuantityType Checks
    assertThat(quantityField.getType(), instanceOf(QuantityType.class));
    QuantityType quantityType = (QuantityType) quantityField.getType();
    assertThat(quantityType.getUnits(), equalTo("Waterpoints"));
    assertThat(quantityType.getAggregation(), is(notNullValue()));
    assertThat(quantityType.getAggregation(), equalTo(QuantityType.Aggregation.SUM));
    assertThat(quantityType.getAggregation().ordinal(), equalTo(0));
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) FormField(org.activityinfo.model.form.FormField) Test(org.junit.Test)

Example 57 with FormField

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

the class XFormSubmissionResource method submit.

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_XML)
public Response submit(byte[] bytes) {
    XFormInstance instance = new XFormInstanceImpl(bytes);
    AuthenticatedUser user = authenticationTokenService.authenticate(instance.getAuthenticationToken());
    FormClass formClass = locator.getFormClass(instance.getFormClassId());
    ResourceId formId = newLegacyFormInstanceId(formClass.getId());
    FormInstance formInstance = new FormInstance(formId, formClass.getId());
    String instanceId = instance.getId();
    LOGGER.log(Level.INFO, "Saving XForm " + instance.getId() + " as " + formId);
    for (FormField formField : formClass.getFields()) {
        if (formField.getType().isUpdatable()) {
            Optional<Element> element = instance.getFieldContent(formField.getId());
            if (element.isPresent()) {
                formInstance.set(formField.getId(), tryParse(formInstance, formField, element.get()));
            } else if (isLocation(formClass, formField)) {
                FieldType fieldType = formField.getType();
                Optional<Element> gpsField = instance.getFieldContent(field(formClass.getId(), GPS_FIELD));
                Optional<Element> nameField = instance.getFieldContent(field(formClass.getId(), LOCATION_NAME_FIELD));
                if (fieldType instanceof ReferenceType && gpsField.isPresent() && nameField.isPresent()) {
                    ResourceId locationFieldId = field(formClass.getId(), LOCATION_FIELD);
                    int newLocationId = new KeyGenerator().generateInt();
                    ReferenceType locationRefType = (ReferenceType) fieldType;
                    if (locationRefType.getRange().isEmpty()) {
                        throw new IllegalStateException("Location field has empty range");
                    }
                    ResourceId locationFormId = locationRefType.getRange().iterator().next();
                    int locationTypeId = getLegacyIdFromCuid(locationFormId);
                    FieldValue fieldValue = new ReferenceValue(new RecordRef(locationFormId, locationInstanceId(newLocationId)));
                    String name = OdkHelper.extractText(nameField.get());
                    if (Strings.isNullOrEmpty(name)) {
                        throw new WebApplicationException(Response.status(BAD_REQUEST).entity("Name value for location field is blank. ").build());
                    }
                    Optional<GeoPoint> geoPoint = parseLocation(gpsField);
                    formInstance.set(locationFieldId, fieldValue);
                    createLocation(newLocationId, locationTypeId, name, geoPoint);
                }
            }
        }
    }
    ensurePartnerIsSet(formClass, formInstance);
    if (!instanceIdService.exists(instanceId)) {
        for (FieldValue fieldValue : formInstance.getFieldValueMap().values()) {
            if (fieldValue instanceof AttachmentValue) {
                persist(user, instance, (AttachmentValue) fieldValue);
            }
        }
        locator.persist(formInstance);
        instanceIdService.submit(instanceId);
    }
    // Backup the original XForm in case something went wrong with processing
    submissionArchiver.backup(formClass.getId(), formId, ByteSource.wrap(bytes));
    return Response.status(CREATED).build();
}
Also used : AttachmentValue(org.activityinfo.model.type.attachment.AttachmentValue) Optional(com.google.common.base.Optional) Element(org.w3c.dom.Element) AuthenticatedUser(org.activityinfo.legacy.shared.AuthenticatedUser) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) XFormInstanceImpl(org.activityinfo.server.endpoint.odk.xform.XFormInstanceImpl) XFormInstance(org.activityinfo.server.endpoint.odk.xform.XFormInstance) FormInstance(org.activityinfo.model.form.FormInstance) FormField(org.activityinfo.model.form.FormField) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) XFormInstance(org.activityinfo.server.endpoint.odk.xform.XFormInstance)

Example 58 with FormField

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

the class XFormSubmissionResource method ensurePartnerIsSet.

private void ensurePartnerIsSet(FormClass formClass, FormInstance formInstance) {
    ResourceId partnerFieldId = CuidAdapter.field(formClass.getId(), CuidAdapter.PARTNER_FIELD);
    if (formInstance.get(partnerFieldId) != null) {
        return;
    }
    // Otherwise, find the default partner
    FormField partnerField = formClass.getField(partnerFieldId);
    ReferenceType partnerFieldType = (ReferenceType) partnerField.getType();
    List<ReferenceChoice> choices = locator.getReferenceChoices(partnerFieldType.getRange());
    if (choices.size() != 1) {
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("No partner selected").build());
    }
    formInstance.set(partnerFieldId, new ReferenceValue(choices.get(0).getRef()));
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) FormField(org.activityinfo.model.form.FormField)

Example 59 with FormField

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

the class XFormBuilder method locationName.

private OdkField locationName(FormField original) {
    FormField formField = new FormField(locationNameFieldId);
    formField.setType(TextType.SIMPLE);
    formField.setLabel(original.getLabel());
    formField.setRequired(original.isRequired());
    return new OdkField(formField, factory.get(formField.getType()));
}
Also used : OdkField(org.activityinfo.server.endpoint.odk.OdkField) FormField(org.activityinfo.model.form.FormField)

Example 60 with FormField

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

the class SchemaCsvWriterV3 method writeForm.

private void writeForm(UserDatabaseDTO db, FormClass formClass) throws IOException {
    FieldContext context = new FieldContext(db, formClass);
    List<FormField> fields = formClass.getFields();
    for (FormField field : fields) {
        if (field.getType() instanceof EnumType) {
            writeEnumItems(context, field, ((EnumType) field.getType()).getValues());
        } else if (field.getType() instanceof SubFormReferenceType) {
            writeSubForm(context, field);
        } else if (!isBuiltinField(formClass, field)) {
            writeField(context, field);
        }
    }
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) EnumType(org.activityinfo.model.type.enumerated.EnumType) FormField(org.activityinfo.model.form.FormField)

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