Search in sources :

Example 1 with RecordTransaction

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));
        }
    });
}
Also used : CloseEvent(com.sencha.gxt.widget.core.client.event.CloseEvent) MessageBox(com.sencha.gxt.widget.core.client.box.MessageBox) RecordTransaction(org.activityinfo.model.resource.RecordTransaction)

Example 2 with RecordTransaction

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()));
}
Also used : RecordUpdate(org.activityinfo.model.resource.RecordUpdate) RecordRef(org.activityinfo.model.type.RecordRef) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) FieldInput(org.activityinfo.ui.client.input.model.FieldInput) RecordTransaction(org.activityinfo.model.resource.RecordTransaction) Test(org.junit.Test)

Example 3 with RecordTransaction

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))));
}
Also used : RecordRef(org.activityinfo.model.type.RecordRef) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) RecordTransaction(org.activityinfo.model.resource.RecordTransaction) Test(org.junit.Test)

Example 4 with RecordTransaction

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);
}
Also used : RecordTransactionBuilder(org.activityinfo.model.resource.RecordTransactionBuilder) RecordTransaction(org.activityinfo.model.resource.RecordTransaction)

Example 5 with RecordTransaction

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());
}
Also used : RecordUpdate(org.activityinfo.model.resource.RecordUpdate) EnumValue(org.activityinfo.model.type.enumerated.EnumValue) RecordRef(org.activityinfo.model.type.RecordRef) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) TestingFormStore(org.activityinfo.ui.client.store.TestingFormStore) RecordTransaction(org.activityinfo.model.resource.RecordTransaction) Test(org.junit.Test)

Aggregations

RecordTransaction (org.activityinfo.model.resource.RecordTransaction)6 Test (org.junit.Test)4 RecordRef (org.activityinfo.model.type.RecordRef)3 FormInputModel (org.activityinfo.ui.client.input.model.FormInputModel)3 RecordUpdate (org.activityinfo.model.resource.RecordUpdate)2 MessageBox (com.sencha.gxt.widget.core.client.box.MessageBox)1 CloseEvent (com.sencha.gxt.widget.core.client.event.CloseEvent)1 FormInstance (org.activityinfo.model.form.FormInstance)1 FormRecord (org.activityinfo.model.form.FormRecord)1 RecordTransactionBuilder (org.activityinfo.model.resource.RecordTransactionBuilder)1 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)1 Maybe (org.activityinfo.promise.Maybe)1 FieldInput (org.activityinfo.ui.client.input.model.FieldInput)1 TestingFormStore (org.activityinfo.ui.client.store.TestingFormStore)1