use of org.activityinfo.model.form.FormInstance 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.FormInstance 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.FormInstance in project activityinfo by bedatadriven.
the class FormInputViewModelBuilder method build.
public FormInputViewModel build(FormInputModel inputModel, Maybe<RecordTree> existingRecord, boolean placeholder) {
// Combine the original values of the record with the newly entered data
Map<ResourceId, FieldValue> existingValues = existingRecord.transform(r -> r.getRoot().getFieldValueMap()).or(emptyMap());
FormInstance record = computeUpdatedRecord(existingValues, inputModel);
// Now construct the viewModel that includes everything about
// the current state of data entry.
FormInputViewModel viewModel = new FormInputViewModel();
viewModel.formTree = this.formTree;
viewModel.inputModel = inputModel;
viewModel.fieldValueMap = record.getFieldValueMap();
viewModel.subFormMap = computeSubViewModels(inputModel, existingRecord);
viewModel.existingValues = existingValues;
viewModel.placeholder = placeholder;
viewModel.relevant = computeRelevance(record);
viewModel.missing = computeMissing(record, viewModel.relevant);
viewModel.validationErrors = validateFieldValues(record);
viewModel.dirty = computeDirty(placeholder, existingValues, record);
viewModel.locked = checkLocks(record);
viewModel.valid = allInputValid(inputModel) && viewModel.missing.isEmpty() && viewModel.validationErrors.isEmpty() && viewModel.subFormMap.values().stream().allMatch(SubFormViewModel::isValid);
LOGGER.info("Valid = " + viewModel.valid);
return viewModel;
}
use of org.activityinfo.model.form.FormInstance in project activityinfo by bedatadriven.
the class FormInputViewModelBuilder method computeUpdatedRecord.
/**
* Computes the effective record, based
* @param existingValues
* @param inputModel
* @return
*/
private FormInstance computeUpdatedRecord(Map<ResourceId, FieldValue> existingValues, FormInputModel inputModel) {
// We inherit all the existing values...
FormInstance record = new FormInstance(ResourceId.generateId(), formTree.getRootFormId());
record.setAll(existingValues);
// Now apply changes...
for (FormTree.Node node : formTree.getRootFields()) {
FieldInput fieldInput = inputModel.get(node.getFieldId());
switch(fieldInput.getState()) {
case UNTOUCHED:
// No changes
break;
case EMPTY:
record.set(node.getFieldId(), (FieldValue) null);
break;
case VALID:
record.set(node.getFieldId(), fieldInput.getValue());
break;
case INVALID:
LOGGER.info("Field with invalid input = " + node.getFieldId());
record.set(node.getFieldId(), (FieldValue) null);
break;
}
}
LOGGER.info("fieldValues = " + record.getFieldValueMap());
return record;
}
use of org.activityinfo.model.form.FormInstance in project activityinfo by bedatadriven.
the class FormStoreTest method serialNumberOffline.
@Test
public void serialNumberOffline() {
TestSetup setup = new TestSetup();
IntakeForm intakeForm = setup.getCatalog().getIntakeForm();
// Synchronize the intake form
setup.setConnected(true);
setup.getFormStore().setFormOffline(intakeForm.getFormId(), true);
setup.runScheduled();
// Go offline
setup.setConnected(false);
// Create a new intake record
FormInstance newRecord = intakeForm.getGenerator().get();
newRecord.set(intakeForm.getProtectionCodeFieldId(), (FieldValue) null);
Promise<Void> update = setup.getFormStore().updateRecords(new RecordTransactionBuilder().create(newRecord).build());
assertThat(update.getState(), equalTo(Promise.State.FULFILLED));
// Verify that we can read the new record offline
QueryModel queryModel = new QueryModel(intakeForm.getFormId());
queryModel.selectResourceId().as("id");
queryModel.selectField(intakeForm.getProtectionCodeFieldId()).as("serial");
queryModel.setFilter(Formulas.equals(new SymbolNode("_id"), new ConstantNode(newRecord.getId().asString())));
Connection<ColumnSet> view = setup.connect(setup.getFormStore().query(queryModel));
assertThat(view.assertLoaded().getNumRows(), equalTo(1));
assertThat(view.assertLoaded().getColumnView("serial").getString(0), nullValue());
// Now go online...
setup.setConnected(true);
setup.getOfflineStore().syncChanges();
;
setup.runScheduled();
// Check that the serial number has been updated with the value from the server
assertThat(view.assertLoaded().getColumnView("serial").getString(0), not(nullValue()));
}
Aggregations