Search in sources :

Example 36 with FormClass

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

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

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

the class FixSubForm method maybeFixForm.

private boolean maybeFixForm(final PrintWriter logger, MySqlQueryExecutor executor, ResourceId parentFormId) {
    logger.println("Fixing " + parentFormId + "...");
    final MySqlStorageProvider catalog = new MySqlStorageProvider(executor);
    FormStorage parentForm = catalog.getForm(parentFormId).get();
    final FormClass formClass = parentForm.getFormClass();
    logger.println("Loaded activity " + parentForm.getFormClass().getLabel());
    final List<FormField> updated = new ArrayList<>();
    ObjectifyService.run(new VoidWork() {

        @Override
        public void vrun() {
            Hrd.ofy().transact(new VoidWork() {

                @Override
                public void vrun() {
                    for (FormField formField : formClass.getFields()) {
                        if (formField.getType() instanceof SubFormReferenceType) {
                            if (maybeFixForm(logger, formClass, formField)) {
                                updated.add(formField);
                            }
                        }
                    }
                }
            });
        }
    });
    logger.println("TX COMPLETED!");
    if (!updated.isEmpty()) {
        logger.println("Updating parent form schema...");
        catalog.createOrUpdateFormSchema(formClass);
        return true;
    } else {
        return false;
    }
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) FormStorage(org.activityinfo.store.spi.FormStorage) VoidWork(com.googlecode.objectify.VoidWork) FormClass(org.activityinfo.model.form.FormClass) ArrayList(java.util.ArrayList) MySqlStorageProvider(org.activityinfo.store.mysql.MySqlStorageProvider) FormField(org.activityinfo.model.form.FormField)

Example 39 with FormClass

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

the class FormFolder method getChildren.

public List<CatalogEntry> getChildren(ResourceId formId) {
    Optional<FormStorage> storage = catalog.getForm(formId);
    if (!storage.isPresent()) {
        return Collections.emptyList();
    }
    List<CatalogEntry> entries = new ArrayList<>();
    FormClass formClass = storage.get().getFormClass();
    for (FormField formField : formClass.getFields()) {
        if (formField.getType() instanceof SubFormReferenceType) {
            SubFormReferenceType subFormType = (SubFormReferenceType) formField.getType();
            ResourceId subFormId = subFormType.getClassId();
            CatalogEntry catalogEntry = new CatalogEntry(subFormId.asString(), formField.getLabel(), CatalogEntryType.FORM);
            catalogEntry.setLeaf(true);
            entries.add(catalogEntry);
        }
    }
    return entries;
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) FormStorage(org.activityinfo.store.spi.FormStorage) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) CatalogEntry(org.activityinfo.model.form.CatalogEntry) ArrayList(java.util.ArrayList) FormField(org.activityinfo.model.form.FormField)

Example 40 with FormClass

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

the class ExprQueryBuilder method computeFormClass.

private FormSymbolTable computeFormClass(FormSymbolTable parent, FormulaNode formulaNode) {
    if (formulaNode instanceof SymbolNode) {
        FormField field = parent.resolveSymbol((SymbolNode) formulaNode);
        if (field.getType() instanceof RecordFieldType) {
            RecordFieldType recordFieldType = (RecordFieldType) field.getType();
            FormClass recordFormClass = recordFieldType.getFormClass();
            return new FormSymbolTable(recordFormClass);
        } else {
            throw new IllegalStateException(field.getName() + " is not a record type field.");
        }
    } else if (formulaNode instanceof CompoundExpr) {
        CompoundExpr compoundExpr = (CompoundExpr) formulaNode;
        FormSymbolTable child = computeFormClass(parent, compoundExpr.getValue());
        return computeFormClass(child, compoundExpr.getValue());
    } else {
        throw new UnsupportedOperationException("exprNode: " + formulaNode);
    }
}
Also used : FormClass(org.activityinfo.model.form.FormClass) FormSymbolTable(org.activityinfo.model.formula.eval.FormSymbolTable) FormField(org.activityinfo.model.form.FormField)

Aggregations

FormClass (org.activityinfo.model.form.FormClass)109 FormField (org.activityinfo.model.form.FormField)49 ResourceId (org.activityinfo.model.resource.ResourceId)41 Test (org.junit.Test)38 QuantityType (org.activityinfo.model.type.number.QuantityType)20 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)19 FormInstance (org.activityinfo.model.form.FormInstance)14 EnumType (org.activityinfo.model.type.enumerated.EnumType)12 JsonValue (org.activityinfo.json.JsonValue)11 FormTree (org.activityinfo.model.formTree.FormTree)10 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)10 ColumnSet (org.activityinfo.model.query.ColumnSet)8 QueryModel (org.activityinfo.model.query.QueryModel)8 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)8 FieldValue (org.activityinfo.model.type.FieldValue)7 Quantity (org.activityinfo.model.type.number.Quantity)7 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)6 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)6 ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)6 ColumnView (org.activityinfo.model.query.ColumnView)5