use of org.activityinfo.model.resource.RecordTransaction in project activityinfo by bedatadriven.
the class FormInputView method save.
public void save(CloseEvent.CloseHandler closeHandler) {
// If the view model is still loading, ignore this click
if (viewModel == null) {
return;
}
if (!viewModel.isValid()) {
MessageBox box = new MessageBox(I18N.CONSTANTS.error(), I18N.CONSTANTS.pleaseFillInAllRequiredFields());
box.setModal(true);
box.show();
return;
}
// Good to go...
RecordTransaction tx = viewModel.buildTransaction();
LOGGER.info("Submitting transaction: " + Json.stringify(tx));
formStore.updateRecords(tx).then(new AsyncCallback<Void>() {
@Override
public void onFailure(Throwable caught) {
MessageBox box = new MessageBox(I18N.CONSTANTS.serverError(), I18N.CONSTANTS.errorOnServer());
box.setModal(true);
box.show();
}
@Override
public void onSuccess(Void result) {
closeHandler.onClose(new CloseEvent(null));
}
});
}
use of org.activityinfo.model.resource.RecordTransaction in project activityinfo by bedatadriven.
the class FormInputViewModelTest method testSubFormInput.
@Test
public void testSubFormInput() {
FormInputViewModelBuilder builder = builderFor(setup.getCatalog().getIncidentForm());
// Start with empty input
FormInputModel inputModel = new FormInputModel(new RecordRef(IncidentForm.FORM_ID, ResourceId.generateId()));
// Should see one (empty) sub form record
FormInputViewModel viewModel = builder.build(inputModel);
SubFormViewModel referralSubForm = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
assertThat(referralSubForm.getSubRecords(), hasSize(1));
// We can update this sub record
FormInputViewModel subRecord = referralSubForm.getSubRecords().get(0);
inputModel = inputModel.update(subRecord.getRecordRef(), ReferralSubForm.ORGANIZATION_FIELD_ID, new FieldInput(TextValue.valueOf("CRS")));
viewModel = builder.build(inputModel);
referralSubForm = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
assertThat(referralSubForm.getSubRecords(), hasSize(1));
// Now add a second record
inputModel = inputModel.addSubRecord(new RecordRef(ReferralSubForm.FORM_ID, ResourceId.generateId()));
viewModel = builder.build(inputModel);
referralSubForm = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
assertThat(referralSubForm.getSubRecords(), hasSize(2));
// Verify that the transaction is built is correctly
RecordTransaction tx = viewModel.buildTransaction();
RecordUpdate[] changes = tx.getChangeArray();
assertThat(changes.length, equalTo(3));
RecordUpdate parentChange = changes[0];
RecordUpdate subFormChange = changes[1];
assertThat(parentChange.getRecordRef(), equalTo(inputModel.getRecordRef()));
assertThat(subFormChange.getParentRecordId(), equalTo(parentChange.getRecordId().asString()));
}
use of org.activityinfo.model.resource.RecordTransaction in project activityinfo by bedatadriven.
the class FormInputViewModelTest method editRecordWithSubRecords.
@Test
public void editRecordWithSubRecords() {
IncidentForm incidentForm = setup.getCatalog().getIncidentForm();
RecordRef rootRecordRef = incidentForm.getRecordRef(0);
FormStructure structure = fetchStructure(rootRecordRef);
FormInputViewModelBuilder builder = builderFor(structure);
FormInputModel inputModel = new FormInputModel(rootRecordRef);
FormInputViewModel viewModel = builder.build(inputModel, structure.getExistingRecord());
SubFormViewModel subFormField = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
assertThat(subFormField.getSubRecords(), hasSize(4));
// Try deleting the first one...
RecordRef deletedRef = subFormField.getSubRecords().get(0).getRecordRef();
inputModel = inputModel.deleteSubRecord(deletedRef);
viewModel = builder.build(inputModel, structure.getExistingRecord());
subFormField = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
assertThat(subFormField.getSubRecords(), hasSize(3));
// Should show up in transaction
RecordTransaction tx = viewModel.buildTransaction();
assertThat(tx.getChanges(), hasItem(hasProperty("deleted", equalTo(true))));
}
use of org.activityinfo.model.resource.RecordTransaction in project activityinfo by bedatadriven.
the class Updater method execute.
public void execute(FormInstance formInstance) {
RecordTransaction tx = new RecordTransactionBuilder().create(formInstance).build();
execute(tx);
}
use of org.activityinfo.model.resource.RecordTransaction in project activityinfo by bedatadriven.
the class FormInputViewModelTest method testNewlyIrrelvantFieldSetToEmpty.
@Test
public void testNewlyIrrelvantFieldSetToEmpty() {
TestingFormStore store = new TestingFormStore();
Survey survey = store.getCatalog().getSurvey();
RecordRef recordRef = survey.getRecordRef(8);
FormStructure structure = fetchStructure(recordRef);
FormInputViewModelBuilder builder = new FormInputViewModelBuilder(store, structure.getFormTree(), new TestingActivePeriodMemory());
FormInputModel inputModel = new FormInputModel(new RecordRef(survey.getFormId(), ResourceId.generateId()));
// The record was saved as GENDER=Female, and PREGNANT=No
FormInputViewModel viewModel = builder.build(inputModel, structure.getExistingRecord());
assertThat(viewModel.getField(survey.getGenderFieldId()), equalTo(new EnumValue(survey.getFemaleId())));
assertThat(viewModel.isRelevant(survey.getPregnantFieldId()), equalTo(true));
assertThat(viewModel.getField(survey.getPregnantFieldId()), equalTo(new EnumValue(survey.getPregnantNo())));
// When we change the Gender to Male, then PREGNANT should be set to empty
inputModel = inputModel.update(survey.getGenderFieldId(), new EnumValue(survey.getMaleId()));
viewModel = builder.build(inputModel, structure.getExistingRecord());
assertThat(viewModel.isRelevant(survey.getPregnantFieldId()), equalTo(false));
RecordTransaction tx = viewModel.buildTransaction();
assertThat(tx.getChangeArray(), arrayWithSize(1));
RecordUpdate update = tx.getChanges().iterator().next();
assertTrue(update.getFields().get(survey.getPregnantFieldId().asString()).isJsonNull());
}
Aggregations