use of org.activityinfo.model.form.FormInstance in project activityinfo by bedatadriven.
the class SerialNumberTest method testWithPrefix.
@Test
public void testWithPrefix() {
TestDatabase database = harness.createDatabase();
ActivityInfoClient client = harness.client();
FormClass formClass = new FormClass(harness.newFormId());
formClass.setLabel("Test Prefix");
formClass.setDatabaseId(database.getId());
FormField partnerField = CuidAdapter.partnerField(formClass);
formClass.addElement(partnerField);
FormField provinceField = formClass.addField(harness.newFieldId()).setType(TextType.SIMPLE).setLabel("Province Code").setCode("PROVINCE").setRequired(true);
FormField fileNumberField = formClass.addField(harness.newFieldId()).setCode("SN").setType(new SerialNumberType("PROVINCE", 5)).setLabel("Record Number");
client.createForm(formClass);
FormInstance kunduz1 = new FormInstance(harness.newRecordId(), formClass.getId());
kunduz1.set(provinceField.getId(), "KUNDUZ");
kunduz1.set(partnerField.getId(), database.getDefaultPartner());
client.createRecord(kunduz1);
FormInstance kunduz2 = new FormInstance(harness.newRecordId(), formClass.getId());
kunduz2.set(provinceField.getId(), "KUNDUZ");
kunduz2.set(partnerField.getId(), database.getDefaultPartner());
client.createRecord(kunduz2);
FormInstance takhar = new FormInstance(harness.newRecordId(), formClass.getId());
takhar.set(provinceField.getId(), "TAKHAR");
takhar.set(partnerField.getId(), database.getDefaultPartner());
client.createRecord(takhar);
// Verify that the serial numbers have been assigned
kunduz1 = client.getTypedRecord(formClass, kunduz1.getId());
kunduz2 = client.getTypedRecord(formClass, kunduz2.getId());
takhar = client.getTypedRecord(formClass, takhar.getId());
assertThat(kunduz1.get(fileNumberField.getId()), equalTo((FieldValue) new SerialNumber("KUNDUZ", 1)));
assertThat(kunduz2.get(fileNumberField.getId()), equalTo((FieldValue) new SerialNumber("KUNDUZ", 2)));
assertThat(takhar.get(fileNumberField.getId()), equalTo((FieldValue) new SerialNumber("TAKHAR", 1)));
// Now verify that we can query a table with serial numbers
QueryModel queryModel = new QueryModel(formClass.getId());
queryModel.selectResourceId().as("id");
queryModel.selectField("SN");
ColumnSet columnSet = client.queryTable(queryModel);
ColumnView sn = columnSet.getColumnView("SN");
}
use of org.activityinfo.model.form.FormInstance in project activityinfo by bedatadriven.
the class RecordCursor method execute.
public Iterator<FormInstance> execute() {
Cursor cursor = builder.open();
while (cursor.next()) {
}
return new Iterator<FormInstance>() {
private int row = 0;
@Override
public boolean hasNext() {
return row < id.values.size();
}
@Override
public FormInstance next() {
FormInstance record = new FormInstance(id.values.get(row), formId);
for (FieldCollector field : fields) {
record.set(field.fieldId, field.values.get(row));
}
row++;
return record;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
use of org.activityinfo.model.form.FormInstance in project activityinfo by bedatadriven.
the class SelectionViewModel method computeSelection.
private static Optional<SelectionViewModel> computeSelection(FormTree formTree, Maybe<FormRecord> record) {
if (!record.isVisible()) {
return Optional.absent();
}
FormMetadata form = formTree.getFormMetadata(ResourceId.valueOf(record.get().getFormId()));
if (!form.isVisible()) {
return Optional.absent();
}
FormInstance typedRecord = FormInstance.toFormInstance(form.getSchema(), record.get());
boolean editAllowed = evalPermission(form, typedRecord, Operation.EDIT_RECORD);
return Optional.of(new SelectionViewModel(typedRecord, editAllowed, editAllowed));
}
use of org.activityinfo.model.form.FormInstance 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.FormInstance in project activityinfo by bedatadriven.
the class ImportWithMultiClassRangeTest method school.
private Set<ResourceId> school(String name) {
// Find id of the school with this name
ResourceId id = null;
ColumnView nameColumn = resultSet.getColumnView("name");
ColumnView idColumn = resultSet.getColumnView("id");
for (int i = 0; i < resultSet.getNumRows(); i++) {
if (name.equals(nameColumn.getString(i))) {
id = ResourceId.valueOf(idColumn.getString(i));
break;
}
}
if (id == null) {
throw new AssertionError("No school named '" + name + "'");
}
Promise<FormInstance> record = locator.getFormInstance(SCHOOL_FORM_CLASS, id);
ReferenceValue value = (ReferenceValue) record.get().get(CuidAdapter.field(SCHOOL_FORM_CLASS, CuidAdapter.ADMIN_FIELD));
Set<ResourceId> recordIds = new HashSet<>();
for (RecordRef recordRef : value.getReferences()) {
recordIds.add(recordRef.getRecordId());
}
return recordIds;
}
Aggregations