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