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);
}
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());
}
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));
}
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))));
}
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));
}
Aggregations