Search in sources :

Example 1 with SubFormInstanceLoader

use of org.activityinfo.ui.client.component.form.subform.SubFormInstanceLoader in project activityinfo by bedatadriven.

the class FormModelTest method subformInstancesPersistence.

@Test
public void subformInstancesPersistence() {
    setupForms();
    FormInstance rootInstance = new FormInstance(ResourceId.generateSubmissionId(masterFormClass), masterFormClass.getId());
    rootInstance.set(CuidAdapter.field(masterFormClass.getId(), CuidAdapter.START_DATE_FIELD), new LocalDate(2016, 1, 1));
    rootInstance.set(CuidAdapter.field(masterFormClass.getId(), CuidAdapter.END_DATE_FIELD), new LocalDate(2016, 1, 1));
    rootInstance.set(CuidAdapter.field(masterFormClass.getId(), CuidAdapter.PARTNER_FIELD), new ReferenceValue(new RecordRef(CuidAdapter.partnerFormId(1), CuidAdapter.partnerRecordId(1))));
    rootInstance.set(CuidAdapter.field(masterFormClass.getId(), CuidAdapter.LOCATION_FIELD), new ReferenceValue(new RecordRef(CuidAdapter.locationFormClass(1), CuidAdapter.locationInstanceId(1))));
    FormModel formModel = newFormModel();
    formModel.setWorkingRootInstance(rootInstance);
    String tab1 = new Month(2015, 3).toString();
    String tab2 = new Month(2015, 8).toString();
    // Tab1
    FormInstance valueInstance1 = formModel.getWorkingInstance(subFormChildField.getId(), tab1).get();
    valueInstance1.set(subFormChildField.getId(), TextValue.valueOf("tab1"));
    // Tab2
    FormInstance valueInstance2 = formModel.getWorkingInstance(subFormChildField.getId(), tab2).get();
    valueInstance2.set(subFormChildField.getId(), TextValue.valueOf("tab2"));
    formModel.getChangedInstances().add(valueInstance1);
    formModel.getChangedInstances().add(valueInstance2);
    // persist all value and tab/key instances
    FormActions actions = new FormActions(locator, formModel);
    assertResolves(actions.save());
    // make sure instances are persisted
    FormInstance fetchedInstance1 = assertResolves(locator.getFormInstance(subFormClass.getId(), valueInstance1.getId()));
    FormInstance fetchedInstance2 = assertResolves(locator.getFormInstance(subFormClass.getId(), valueInstance2.getId()));
    assertEquals(fetchedInstance1.get(subFormChildField.getId()), TextValue.valueOf("tab1"));
    assertEquals(fetchedInstance2.get(subFormChildField.getId()), TextValue.valueOf("tab2"));
    // Update value instances
    // Tab1
    valueInstance1 = formModel.getWorkingInstance(subFormChildField.getId(), tab1).get();
    valueInstance1.set(subFormChildField.getId(), TextValue.valueOf("tab11"));
    // Tab2
    valueInstance2 = formModel.getWorkingInstance(subFormChildField.getId(), tab2).get();
    valueInstance2.set(subFormChildField.getId(), TextValue.valueOf("tab22"));
    formModel.getChangedInstances().add(valueInstance1);
    formModel.getChangedInstances().add(valueInstance2);
    // persist updates
    assertResolves(actions.save());
    // make sure instances are persisted
    fetchedInstance1 = assertResolves(locator.getFormInstance(subFormClass.getId(), valueInstance1.getId()));
    fetchedInstance2 = assertResolves(locator.getFormInstance(subFormClass.getId(), valueInstance2.getId()));
    assertEquals(fetchedInstance1.get(subFormChildField.getId()), TextValue.valueOf("tab11"));
    assertEquals(fetchedInstance2.get(subFormChildField.getId()), TextValue.valueOf("tab22"));
    // check subform loader
    FormModel emptyModel = new FormModel(locator, new GxtStateProvider());
    emptyModel.setWorkingRootInstance(rootInstance);
    // load subform instances into empty model
    assertResolves(new SubFormInstanceLoader(emptyModel).load(subFormClass));
    Map<FormModel.SubformValueKey, Set<FormInstance>> loadedInstances = emptyModel.getSubFormInstances();
    assertEquals(1, loadedInstances.size());
    assertEquals(emptyModel.getSubformValueInstance(subFormClass, rootInstance, tab1).get(), valueInstance1);
    assertEquals(emptyModel.getSubformValueInstance(subFormClass, rootInstance, tab2).get(), valueInstance2);
}
Also used : SubFormInstanceLoader(org.activityinfo.ui.client.component.form.subform.SubFormInstanceLoader) Set(java.util.Set) OnDataSet(org.activityinfo.server.database.OnDataSet) ReferenceValue(org.activityinfo.model.type.ReferenceValue) RecordRef(org.activityinfo.model.type.RecordRef) GxtStateProvider(org.activityinfo.ui.client.dispatch.state.GxtStateProvider) LocalDate(org.activityinfo.model.type.time.LocalDate) Month(org.activityinfo.model.type.time.Month) FormInstance(org.activityinfo.model.form.FormInstance) Test(org.junit.Test)

Aggregations

Set (java.util.Set)1 FormInstance (org.activityinfo.model.form.FormInstance)1 RecordRef (org.activityinfo.model.type.RecordRef)1 ReferenceValue (org.activityinfo.model.type.ReferenceValue)1 LocalDate (org.activityinfo.model.type.time.LocalDate)1 Month (org.activityinfo.model.type.time.Month)1 OnDataSet (org.activityinfo.server.database.OnDataSet)1 SubFormInstanceLoader (org.activityinfo.ui.client.component.form.subform.SubFormInstanceLoader)1 GxtStateProvider (org.activityinfo.ui.client.dispatch.state.GxtStateProvider)1 Test (org.junit.Test)1