Search in sources :

Example 31 with FormInstance

use of org.activityinfo.model.form.FormInstance 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 32 with FormInstance

use of org.activityinfo.model.form.FormInstance in project activityinfo by bedatadriven.

the class ResourceLocatorAdaptorTest method updateLocation.

@Test
public void updateLocation() {
    // <location locationId="1" name="Penekusu Kivu" locationTypeId="1"
    // X="1.532" Y="27.323" timeEdited="1"/>
    // <locationAdminLink locationId="1" adminEntityId="2"/>
    // <locationAdminLink locationId="1" adminEntityId="12"/>
    FormInstance instance = assertResolves(locator.getFormInstance(HEALTH_CENTER_CLASS, locationInstanceId(1)));
    instance.set(field(HEALTH_CENTER_CLASS, NAME_FIELD), "New Penekusu");
    assertResolves(locator.persist(instance));
    GetLocations query = new GetLocations(1);
    LocationResult result = execute(query);
    LocationDTO location = result.getData().get(0);
    assertThat(location.getName(), equalTo("New Penekusu"));
    assertThat(location.getLocationTypeId(), equalTo(1));
    assertThat(location.getLatitude(), equalTo(27.323));
    assertThat(location.getLongitude(), equalTo(1.532));
    // 12 admin level is not returned because we eliminate redundant information org.activityinfo.store.mysql.side.AdminColumnBuilder.emit(int[])
    assertThat(location.getAdminEntity(1).getId(), equalTo(2));
}
Also used : GetLocations(org.activityinfo.legacy.shared.command.GetLocations) FormInstance(org.activityinfo.model.form.FormInstance) LocationDTO(org.activityinfo.legacy.shared.model.LocationDTO) LocationResult(org.activityinfo.legacy.shared.command.result.LocationResult) Test(org.junit.Test)

Example 33 with FormInstance

use of org.activityinfo.model.form.FormInstance 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 34 with FormInstance

use of org.activityinfo.model.form.FormInstance in project activityinfo by bedatadriven.

the class ResourceLocatorAdaptorTest method getLocation.

@Test
@OnDataSet("/dbunit/jordan-locations.db.xml")
public void getLocation() {
    ResourceId classId = locationFormClass(50512);
    FormInstance instance = assertResolves(locator.getFormInstance(classId, locationInstanceId(1590565828)));
    ReferenceValue adminUnits = (ReferenceValue) instance.get(field(classId, ADMIN_FIELD));
    System.out.println(adminUnits);
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) ReferenceValue(org.activityinfo.model.type.ReferenceValue) FormInstance(org.activityinfo.model.form.FormInstance) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Example 35 with FormInstance

use of org.activityinfo.model.form.FormInstance 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

FormInstance (org.activityinfo.model.form.FormInstance)46 Test (org.junit.Test)26 ResourceId (org.activityinfo.model.resource.ResourceId)15 FormClass (org.activityinfo.model.form.FormClass)14 FieldValue (org.activityinfo.model.type.FieldValue)10 FormField (org.activityinfo.model.form.FormField)9 RecordRef (org.activityinfo.model.type.RecordRef)8 ReferenceValue (org.activityinfo.model.type.ReferenceValue)7 LocalDate (org.activityinfo.model.type.time.LocalDate)7 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)6 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)6 OnDataSet (org.activityinfo.server.database.OnDataSet)6 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)5 SerialNumberType (org.activityinfo.model.type.SerialNumberType)5 FormRecord (org.activityinfo.model.form.FormRecord)4 SerialNumber (org.activityinfo.model.type.SerialNumber)4 Map (java.util.Map)3 FormTree (org.activityinfo.model.formTree.FormTree)3 Maybe (org.activityinfo.promise.Maybe)3 ArrayList (java.util.ArrayList)2