Search in sources :

Example 16 with LocalDate

use of org.activityinfo.model.type.time.LocalDate 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)

Example 17 with LocalDate

use of org.activityinfo.model.type.time.LocalDate in project activityinfo by bedatadriven.

the class CurlExamplesGenerator method getRecord.

public static Example getRecord() {
    ResourceId formId = ResourceId.generateId();
    ResourceId recordId = ResourceId.generateId();
    FormRecord.Builder record = new FormRecord.Builder();
    record.setFormId(formId);
    record.setRecordId(recordId);
    record.setFieldValue(ResourceId.generateId(), TextValue.valueOf("Text Value"));
    record.setFieldValue(ResourceId.generateId(), new Quantity(1500));
    record.setFieldValue(ResourceId.generateId(), new LocalDate(2016, 10, 5));
    record.setFieldValue(ResourceId.generateId(), new GeoPoint(52.078663, 4.288788));
    record.setFieldValue(ResourceId.generateId(), new EnumValue(ResourceId.generateId()));
    return new Example(String.format("curl https://www.activityinfo.org/resources/form/%s/record/%s", formId, recordId), record.build().toJson());
}
Also used : GeoPoint(org.activityinfo.model.type.geo.GeoPoint) ResourceId(org.activityinfo.model.resource.ResourceId) EnumValue(org.activityinfo.model.type.enumerated.EnumValue) Quantity(org.activityinfo.model.type.number.Quantity) FormRecord(org.activityinfo.model.form.FormRecord) LocalDate(org.activityinfo.model.type.time.LocalDate)

Example 18 with LocalDate

use of org.activityinfo.model.type.time.LocalDate in project activityinfo by bedatadriven.

the class CreateSiteTest method persistSite.

@Test
public void persistSite() {
    ResourceId locationClassId = CuidAdapter.locationFormClass(1);
    FormInstance location = new FormInstance(CuidAdapter.generateLocationCuid(), locationClassId);
    location.set(field(locationClassId, CuidAdapter.NAME_FIELD), "Virunga");
    location.set(field(locationClassId, CuidAdapter.AXE_FIELD), "Goma - Rutshuru");
    location.set(field(locationClassId, CuidAdapter.GEOMETRY_FIELD), new GeoPoint(27.432, 1.23));
    assertResolves(locator.persist(location));
    int databaseId = 1;
    ResourceId formClassId = CuidAdapter.activityFormClass(1);
    FormInstance instance = new FormInstance(CuidAdapter.generateSiteCuid(), formClassId);
    instance.set(field(formClassId, LOCATION_FIELD), new ReferenceValue(new RecordRef(locationClassId, location.getId())));
    instance.set(field(formClassId, PARTNER_FIELD), CuidAdapter.partnerRef(databaseId, 1));
    instance.set(field(formClassId, START_DATE_FIELD), new LocalDate(2008, 12, 1));
    instance.set(field(formClassId, END_DATE_FIELD), new LocalDate(2009, 1, 3));
    instance.set(indicatorField(1), 996.0);
    instance.set(attributeField(1), new EnumValue(CuidAdapter.attributeId(1), CuidAdapter.attributeField(2)));
    instance.set(commentsField(1), "objection!");
    instance.set(field(formClassId, PROJECT_FIELD), CuidAdapter.projectRef(PROJECT_DOMAIN, 1));
    assertResolves(locator.persist(instance));
}
Also used : GeoPoint(org.activityinfo.model.type.geo.GeoPoint) ResourceId(org.activityinfo.model.resource.ResourceId) ReferenceValue(org.activityinfo.model.type.ReferenceValue) EnumValue(org.activityinfo.model.type.enumerated.EnumValue) RecordRef(org.activityinfo.model.type.RecordRef) FormInstance(org.activityinfo.model.form.FormInstance) LocalDate(org.activityinfo.model.type.time.LocalDate) GeoPoint(org.activityinfo.model.type.geo.GeoPoint) Test(org.junit.Test)

Example 19 with LocalDate

use of org.activityinfo.model.type.time.LocalDate in project activityinfo by bedatadriven.

the class ResourceLocatorAdaptorTest method persistSiteWithCalculatedIndicators.

@Test
@OnDataSet("/dbunit/sites-calculated-indicators.db.xml")
public void persistSiteWithCalculatedIndicators() {
    int siteId = new KeyGenerator().generateInt();
    FormInstance instance = new FormInstance(CuidAdapter.cuid(SITE_DOMAIN, siteId), NFI_DIST_FORM_CLASS);
    instance.set(indicatorField(1), 1);
    instance.set(indicatorField(2), 2);
    instance.set(locationField(NFI_DIST_ID), locationRef(VILLAGE_CLASS, 1));
    instance.set(partnerField(NFI_DIST_ID), partnerRef(PEAR_DATABASE_ID, 1));
    instance.set(projectField(NFI_DIST_ID), projectRef(PEAR_DATABASE_ID, 1));
    instance.set(field(NFI_DIST_FORM_CLASS, START_DATE_FIELD), new LocalDate(2014, 1, 1));
    instance.set(field(NFI_DIST_FORM_CLASS, END_DATE_FIELD), new LocalDate(2014, 1, 1));
    instance.set(field(NFI_DIST_FORM_CLASS, COMMENT_FIELD), NarrativeValue.valueOf("My comment"));
    assertResolves(locator.persist(instance));
    FormInstance firstRead = assertResolves(locator.getFormInstance(NFI_DIST_FORM_CLASS, instance.getId()));
    assertThat(firstRead.get(indicatorField(1)), equalTo((FieldValue) new Quantity(1)));
    assertThat(firstRead.get(indicatorField(2)), equalTo((FieldValue) new Quantity(2)));
    // set indicators to null
    instance.set(indicatorField(1).asString(), null);
    instance.set(indicatorField(2).asString(), null);
    // persist it
    assertResolves(locator.persist(instance));
    // read from server
    FormInstance secondRead = assertResolves(locator.getFormInstance(NFI_DIST_FORM_CLASS, instance.getId()));
    // BENE
    assertThat(secondRead.get(indicatorField(1)), nullValue());
    // BACHE
    assertThat(secondRead.get(indicatorField(2)), nullValue());
// // Both BENE+BACHE and BENE/BACHE should be missing, because both
// // BENE and BACHE are missing
// assertEquals(null, secondRead.getValue(path(indicatorField(11))));
// assertEquals(null, secondRead.getValue(path(indicatorField(12))));
}
Also used : Quantity(org.activityinfo.model.type.number.Quantity) FieldValue(org.activityinfo.model.type.FieldValue) FormInstance(org.activityinfo.model.form.FormInstance) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) LocalDate(org.activityinfo.model.type.time.LocalDate) GeoPoint(org.activityinfo.model.type.geo.GeoPoint) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Example 20 with LocalDate

use of org.activityinfo.model.type.time.LocalDate in project activityinfo by bedatadriven.

the class SitePersisterTest method test.

@Test
public void test() {
    FormClass formClass = assertResolves(locator.getFormClass(PEAR_Activity));
    FormInstance siteFormInstance = new FormInstance(CuidAdapter.generateSiteCuid(), formClass.getId());
    siteFormInstance.set(CONTENU_DI_KIT_FIELD, new EnumValue(CONTENU_DI_KIT_FIELD_ATTR_VALUE));
    // built-in values
    siteFormInstance.set(CuidAdapter.field(formClass.getId(), CuidAdapter.PARTNER_FIELD), new ReferenceValue(new RecordRef(CuidAdapter.partnerFormId(DATABASE_ID), CuidAdapter.partnerRecordId(1))));
    siteFormInstance.set(CuidAdapter.field(formClass.getId(), CuidAdapter.START_DATE_FIELD), new LocalDate(2014, 1, 1));
    siteFormInstance.set(CuidAdapter.field(formClass.getId(), CuidAdapter.END_DATE_FIELD), new LocalDate(2014, 1, 2));
    siteFormInstance.set(CuidAdapter.field(formClass.getId(), CuidAdapter.LOCATION_FIELD), new ReferenceValue(new RecordRef(CuidAdapter.locationFormClass(1), CuidAdapter.locationInstanceId(1))));
    assertResolves(locator.persist(siteFormInstance));
    // query by id
    FormInstance fromServer = assertResolves(locator.getFormInstance(siteFormInstance.getFormId(), siteFormInstance.getId()));
    Assert.assertNotNull(fromServer);
    Assert.assertEquals(fromServer.get(CONTENU_DI_KIT_FIELD), new EnumValue(CONTENU_DI_KIT_FIELD_ATTR_VALUE));
    FormTree formTree = assertResolves(locator.getFormTree(formClass.getId()));
    final List<FieldPath> paths = Lists.newArrayList(formTree.getRootPaths());
    Assert.assertTrue(paths.contains(new FieldPath(CONTENU_DI_KIT_FIELD)));
    // query projection
    FormInstance instance = assertResolves(locator.getFormInstance(formClass.getId(), siteFormInstance.getId()));
    EnumValue fieldValue = (EnumValue) instance.get(CONTENU_DI_KIT_FIELD);
    assertThat(fieldValue.getValueId(), equalTo(CONTENU_DI_KIT_FIELD_ATTR_VALUE));
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) FormClass(org.activityinfo.model.form.FormClass) EnumValue(org.activityinfo.model.type.enumerated.EnumValue) ReferenceValue(org.activityinfo.model.type.ReferenceValue) FieldPath(org.activityinfo.model.formTree.FieldPath) RecordRef(org.activityinfo.model.type.RecordRef) FormInstance(org.activityinfo.model.form.FormInstance) LocalDate(org.activityinfo.model.type.time.LocalDate) Test(org.junit.Test)

Aggregations

LocalDate (org.activityinfo.model.type.time.LocalDate)24 Test (org.junit.Test)11 Quantity (org.activityinfo.model.type.number.Quantity)8 FormInstance (org.activityinfo.model.form.FormInstance)7 RecordRef (org.activityinfo.model.type.RecordRef)7 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)6 ResourceId (org.activityinfo.model.resource.ResourceId)5 OnDataSet (org.activityinfo.server.database.OnDataSet)5 FormClass (org.activityinfo.model.form.FormClass)4 FieldValue (org.activityinfo.model.type.FieldValue)4 ReferenceValue (org.activityinfo.model.type.ReferenceValue)4 Date (java.util.Date)3 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)3 FieldInput (org.activityinfo.ui.client.input.model.FieldInput)3 FormInputModel (org.activityinfo.ui.client.input.model.FormInputModel)3 FormField (org.activityinfo.model.form.FormField)2 FormulaSyntaxException (org.activityinfo.model.formula.diagnostic.FormulaSyntaxException)2 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)2 FilterConfig (com.sencha.gxt.data.shared.loader.FilterConfig)1 Calendar (java.util.Calendar)1