use of org.activityinfo.model.form.FormRecord in project activityinfo by bedatadriven.
the class MySqlUpdateTest method updateLocation.
@Test
public void updateLocation() throws SQLException {
ResourceId recordId = CuidAdapter.cuid(LOCATION_DOMAIN, 1);
ResourceId formId = CuidAdapter.locationFormClass(1);
RecordUpdate update = new RecordUpdate();
update.setFormId(formId);
update.setRecordId(recordId);
update.setFieldValue(CuidAdapter.field(formId, CuidAdapter.NAME_FIELD), TextValue.valueOf("New Name"));
Updater updater = updater();
updater.executeChange(update);
newRequest();
FormStorage formStorage = catalog.getForm(formId).get();
FormRecord record = formStorage.get(recordId).get();
FormInstance typedRecord = FormInstance.toFormInstance(formStorage.getFormClass(), record);
GeoPoint point = (GeoPoint) typedRecord.get(CuidAdapter.field(formId, CuidAdapter.GEOMETRY_FIELD));
assertThat(point, not(nullValue()));
}
use of org.activityinfo.model.form.FormRecord in project activityinfo by bedatadriven.
the class MySqlUpdateTest method addNewAttributes.
@Test
public void addNewAttributes() {
KeyGenerator generator = new KeyGenerator();
int activityId = generator.generateInt();
EnumType enumType = new EnumType(Cardinality.SINGLE, new EnumItem(EnumItem.generateId(), "A"), new EnumItem(EnumItem.generateId(), "B"));
FormField selectField = new FormField(ResourceId.generateFieldId(EnumType.TYPE_CLASS)).setType(enumType).setLabel("Select");
FormClass formClass = new FormClass(CuidAdapter.activityFormClass(activityId));
formClass.setDatabaseId(1);
formClass.setLabel("New Form");
formClass.addElement(selectField);
catalog.createOrUpdateFormSchema(formClass);
System.out.println("Created activity " + activityId);
// Now change the enum items
EnumType updatedType = new EnumType(Cardinality.SINGLE, new EnumItem(EnumItem.generateId(), "C"), new EnumItem(EnumItem.generateId(), "D"));
selectField.setType(updatedType);
newRequest();
catalog.createOrUpdateFormSchema(formClass);
newRequest();
// Now try to save a new instance with the value
FieldValue valueC = new EnumValue(updatedType.getValues().get(0).getId());
FormInstance newRecord = new FormInstance(CuidAdapter.generateSiteCuid(), formClass.getId());
newRecord.set(selectField.getId(), new EnumValue(updatedType.getValues().get(0).getId()));
newRecord.set(CuidAdapter.partnerField(activityId), CuidAdapter.partnerRef(1, 1));
executeUpdate(newRecord);
// Ensure that the select field has been saved
FormRecord saved = catalog.getForm(formClass.getId()).get().get(newRecord.getId()).get();
FormInstance savedInstance = FormInstance.toFormInstance(formClass, saved);
assertThat(savedInstance.get(selectField.getId()), equalTo(valueC));
}
use of org.activityinfo.model.form.FormRecord in project activityinfo by bedatadriven.
the class FormStoreTest method newRecordOffline.
@Test
public void newRecordOffline() {
TestSetup setup = new TestSetup();
Survey survey = setup.getSurveyForm();
// Synchronize the survey form
setup.setConnected(true);
setup.getFormStore().setFormOffline(survey.getFormId(), true);
setup.runScheduled();
// Go offline...
setup.setConnected(false);
// Monitor the pending queue status
Connection<PendingStatus> pendingStatus = setup.connect(setup.getOfflineStore().getPendingStatus());
assertThat(pendingStatus.assertLoaded().isEmpty(), equalTo(true));
// Create a new survey record
FormInstance newRecordTyped = survey.getGenerator().get();
RecordTransaction tx = RecordTransaction.builder().create(newRecordTyped).build();
// Update a record...
Promise<Void> updateResult = setup.getFormStore().updateRecords(tx);
assertThat(updateResult.getState(), equalTo(Promise.State.FULFILLED));
// Now query offline...
Connection<Maybe<FormRecord>> recordView = setup.connect(setup.getFormStore().getRecord(newRecordTyped.getRef()));
// It should be listed as a pending change...
assertThat(pendingStatus.assertLoaded().getCount(), equalTo(1));
Maybe<FormRecord> record = recordView.assertLoaded();
assertThat(record.getState(), equalTo(Maybe.State.VISIBLE));
assertThat(record.get().getRecordId(), equalTo(newRecordTyped.getId().asString()));
// Finally go online and ensure that results are sent to the server
setup.setConnected(true);
setup.getOfflineStore().syncChanges();
// Our queue should be empty again
assertThat(pendingStatus.assertLoaded().isEmpty(), equalTo(true));
}
Aggregations