Search in sources :

Example 61 with ResourceId

use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.

the class MySqlStorageProvider method getFormClasses.

@Override
public Map<ResourceId, FormClass> getFormClasses(Collection<ResourceId> formIds) {
    Map<ResourceId, FormClass> resultMap = new HashMap<>();
    Set<ResourceId> toFetch = new HashSet<>();
    // First check sessionCache for any collections which are already loaded
    for (ResourceId collectionId : formIds) {
        Optional<FormStorage> collection = sessionCache.getIfPresent(collectionId);
        if (collection != null && collection.isPresent()) {
            resultMap.put(collectionId, collection.get().getFormClass());
        } else {
            toFetch.add(collectionId);
        }
    }
    // Now consult each of our providers for collections
    try {
        for (FormProvider provider : providers) {
            Map<ResourceId, FormStorage> fetched = provider.openForms(executor, toFetch);
            for (Map.Entry<ResourceId, FormStorage> entry : fetched.entrySet()) {
                if (resultMap.containsKey(entry.getKey())) {
                    throw new IllegalStateException("Collection " + entry.getKey() + " returned by multiple providers");
                }
                sessionCache.put(entry.getKey(), Optional.of(entry.getValue()));
                resultMap.put(entry.getKey(), entry.getValue().getFormClass());
            }
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
    // Finally, verify that all collections were loaded
    for (ResourceId collectionId : formIds) {
        if (!resultMap.containsKey(collectionId)) {
            throw new FormNotFoundException(collectionId);
        }
    }
    return resultMap;
}
Also used : SQLException(java.sql.SQLException) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass)

Example 62 with ResourceId

use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.

the class HrdCatalogTest method simpleFormTest.

@Test
public void simpleFormTest() {
    ResourceId collectionId = ResourceId.generateId();
    ResourceId villageField = ResourceId.valueOf("FV");
    ResourceId countField = ResourceId.valueOf("FC");
    FormClass formClass = new FormClass(collectionId);
    formClass.setParentFormId(ResourceId.valueOf("foo"));
    formClass.setLabel("NFI Distributions");
    formClass.addField(villageField).setLabel("Village name").setCode("VILLAGE").setType(TextType.SIMPLE);
    formClass.addField(countField).setLabel("Number of Beneficiaries").setCode("BENE").setType(new QuantityType("Beneficiaries"));
    HrdStorageProvider catalog = new HrdStorageProvider();
    catalog.create(formClass);
    Optional<FormStorage> storage = catalog.getForm(collectionId);
    assertTrue(storage.isPresent());
    TypedRecordUpdate village1 = new TypedRecordUpdate();
    village1.setUserId(userId);
    village1.setRecordId(ResourceId.generateSubmissionId(formClass));
    village1.set(villageField, TextValue.valueOf("Rutshuru"));
    village1.set(countField, new Quantity(1000));
    TypedRecordUpdate village2 = new TypedRecordUpdate();
    village2.setUserId(userId);
    village2.setRecordId(ResourceId.generateSubmissionId(formClass));
    village2.set(villageField, TextValue.valueOf("Beni"));
    village2.set(countField, new Quantity(230));
    storage.get().add(village1);
    storage.get().add(village2);
    QueryModel queryModel = new QueryModel(collectionId);
    queryModel.selectResourceId().as("id");
    queryModel.selectField("VILLAGE").as("village");
    queryModel.selectField("BENE").as("family_count");
    queryModel.selectExpr("BENE*5").as("individual_count");
    ColumnSetBuilder builder = new ColumnSetBuilder(catalog, new NullFormSupervisor());
    ColumnSet columnSet = builder.build(queryModel);
    System.out.println(columnSet);
    assertThat(columnSet.getNumRows(), equalTo(2));
    List<RecordVersion> versions1 = ((VersionedFormStorage) storage.get()).getVersions(village1.getRecordId());
    assertThat(versions1, hasSize(1));
    RecordVersion version = versions1.get(0);
    assertThat(version.getRecordId(), equalTo(village1.getRecordId()));
    assertThat(version.getUserId(), equalTo((long) userId));
    assertThat(version.getType(), equalTo(RecordChangeType.CREATED));
}
Also used : Quantity(org.activityinfo.model.type.number.Quantity) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) ResourceId(org.activityinfo.model.resource.ResourceId) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) NullFormSupervisor(org.activityinfo.store.query.shared.NullFormSupervisor) Test(org.junit.Test)

Example 63 with ResourceId

use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.

the class HrdCatalogTest method versionRangeTest.

@Test
public void versionRangeTest() {
    ResourceId collectionId = ResourceId.generateId();
    ResourceId villageField = ResourceId.valueOf("FV");
    ResourceId countField = ResourceId.valueOf("FC");
    FormClass formClass = new FormClass(collectionId);
    formClass.setParentFormId(ResourceId.valueOf("foo"));
    formClass.setLabel("NFI Distributions");
    formClass.addField(villageField).setLabel("Village name").setCode("VILLAGE").setType(TextType.SIMPLE);
    formClass.addField(countField).setLabel("Number of Beneficiaries").setCode("BENE").setType(new QuantityType("Beneficiaries"));
    HrdStorageProvider catalog = new HrdStorageProvider();
    catalog.create(formClass);
    VersionedFormStorage formStorage = (VersionedFormStorage) catalog.getForm(collectionId).get();
    // Initially, with no records added, the form should be at version 1
    // and the version range (0, 1] should be empty.
    assertThat(formStorage.cacheVersion(), equalTo(1L));
    FormSyncSet updatedRecords = formStorage.getVersionRange(0, 1L, resourceId -> true);
    assertTrue(updatedRecords.isEmpty());
    // Add a new record
    TypedRecordUpdate firstUpdate = new TypedRecordUpdate();
    firstUpdate.setUserId(1);
    firstUpdate.setRecordId(ResourceId.generateId());
    firstUpdate.set(villageField, TextValue.valueOf("Goma"));
    catalog.getForm(collectionId).get().add(firstUpdate);
    // Verify that the version is incremented and the version range
    // (0, 2] includes our new record
    assertThat(formStorage.cacheVersion(), equalTo(2L));
    FormSyncSet updated = formStorage.getVersionRange(0, 2L, resourceId -> true);
    assertThat(updated.getUpdatedRecordCount(), equalTo(1));
    // Update the first record and add a new one
    TypedRecordUpdate secondUpdate = new TypedRecordUpdate();
    secondUpdate.setUserId(1);
    secondUpdate.setRecordId(ResourceId.generateId());
    secondUpdate.set(villageField, TextValue.valueOf("Rutshuru"));
    catalog.getForm(collectionId).get().add(firstUpdate);
    // Verify that the version is incremented and the version range
    // (1, 2] includes our new record
    assertThat(formStorage.cacheVersion(), equalTo(3L));
    updated = formStorage.getVersionRange(2L, 3L, resourceId -> true);
    assertThat(updated.getUpdatedRecordCount(), equalTo(1));
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) org.activityinfo.store.spi(org.activityinfo.store.spi) BeforeClass(org.junit.BeforeClass) Cardinality(org.activityinfo.model.type.Cardinality) QuantityType(org.activityinfo.model.type.number.QuantityType) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) EnumType(org.activityinfo.model.type.enumerated.EnumType) Quantity(org.activityinfo.model.type.number.Quantity) Assert.assertThat(org.junit.Assert.assertThat) LocalDatastoreServiceTestConfig(com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) NullFormSupervisor(org.activityinfo.store.query.shared.NullFormSupervisor) Optional(com.google.common.base.Optional) After(org.junit.After) TextValue(org.activityinfo.model.type.primitive.TextValue) Matchers.hasSize(org.hamcrest.Matchers.hasSize) VoidWork(com.googlecode.objectify.VoidWork) LocalServiceTestHelper(com.google.appengine.tools.development.testing.LocalServiceTestHelper) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) Before(org.junit.Before) ObjectifyService(com.googlecode.objectify.ObjectifyService) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Updater(org.activityinfo.store.query.server.Updater) FormSyncSet(org.activityinfo.model.form.FormSyncSet) List(java.util.List) FormField(org.activityinfo.model.form.FormField) TextType(org.activityinfo.model.type.primitive.TextType) LocaleProxy(net.lightoze.gwt.i18n.server.LocaleProxy) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) Closeable(com.googlecode.objectify.util.Closeable) Collections(java.util.Collections) ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) ResourceId(org.activityinfo.model.resource.ResourceId) QuantityType(org.activityinfo.model.type.number.QuantityType) FormSyncSet(org.activityinfo.model.form.FormSyncSet) FormClass(org.activityinfo.model.form.FormClass) Test(org.junit.Test)

Example 64 with ResourceId

use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.

the class FixSubForm method copySnapshots.

private void copySnapshots(PrintWriter logger, ResourceId parentFormId, ResourceId subFormId, ResourceId newSubFormId) {
    QueryResultIterable<FormRecordSnapshotEntity> subRecords = ofy().load().type(FormRecordSnapshotEntity.class).ancestor(FormEntity.key(subFormId)).iterable();
    for (FormRecordSnapshotEntity subRecord : subRecords) {
        ResourceId parentRecordId = ResourceId.valueOf(subRecord.getParentRecordId());
        String name = "sub record snapshot " + subRecord.getRecordId() + "@" + subRecord.getVersion();
        if (isSubRecordOf(parentFormId, parentRecordId)) {
            logger.println(name + " belongs to " + parentRecordId + " in " + parentFormId);
            copySnapshot(subRecord, newSubFormId);
        } else {
            logger.println(name + " does not belong");
        }
    }
}
Also used : FormRecordSnapshotEntity(org.activityinfo.store.hrd.entity.FormRecordSnapshotEntity) ResourceId(org.activityinfo.model.resource.ResourceId)

Example 65 with ResourceId

use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.

the class FixSubForm method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    final PrintWriter logger = new PrintWriter(resp.getOutputStream());
    resp.setContentType("text/plain");
    try (MySqlQueryExecutor executor = new MySqlQueryExecutor()) {
        try (ResultSet rs = executor.query("select distinct i.activityid from indicator i " + "left join activity a on (a.activityid=i.activityid) " + "left join userdatabase db on (a.databaseid=db.databaseid) " + "where type='subform' and " + "i.datedeleted is null and " + "a.datedeleted is null and " + "db.datedeleted is null")) {
            while (rs.next()) {
                final ResourceId parentFormId = CuidAdapter.activityFormClass(rs.getInt(1));
                if (maybeFixForm(logger, executor, parentFormId)) {
                    return;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace(logger);
    } finally {
        logger.flush();
    }
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) ResultSet(java.sql.ResultSet) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Aggregations

ResourceId (org.activityinfo.model.resource.ResourceId)198 Test (org.junit.Test)42 FormClass (org.activityinfo.model.form.FormClass)41 FormField (org.activityinfo.model.form.FormField)34 RecordRef (org.activityinfo.model.type.RecordRef)30 JsonValue (org.activityinfo.json.JsonValue)21 FormTree (org.activityinfo.model.formTree.FormTree)18 ReferenceValue (org.activityinfo.model.type.ReferenceValue)17 FieldValue (org.activityinfo.model.type.FieldValue)16 FormInstance (org.activityinfo.model.form.FormInstance)15 HashMap (java.util.HashMap)14 QuantityType (org.activityinfo.model.type.number.QuantityType)13 ColumnSet (org.activityinfo.model.query.ColumnSet)12 QueryModel (org.activityinfo.model.query.QueryModel)12 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)11 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)11 ReferenceType (org.activityinfo.model.type.ReferenceType)10 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)10 FormStorage (org.activityinfo.store.spi.FormStorage)10 ArrayList (java.util.ArrayList)9