Search in sources :

Example 71 with FormClass

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

the class HrdCatalogTest method subFormTest.

@Test
public void subFormTest() {
    // Typical scenario with a household interview form
    // and a repeating househould member form
    FormClass hhForm = new FormClass(ResourceId.generateId());
    FormClass memberForm = new FormClass(ResourceId.generateId());
    memberForm.setParentFormId(hhForm.getId());
    hhForm.setParentFormId(ResourceId.valueOf("foo"));
    hhForm.setLabel("Household interview");
    FormField hhIdField = hhForm.addField().setLabel("Household ID").setType(TextType.SIMPLE);
    hhForm.addField().setLabel("Household Memmbers").setType(new SubFormReferenceType(memberForm.getId()));
    memberForm.setLabel("Household Members");
    FormField nameField = memberForm.addField().setLabel("Name").setType(TextType.SIMPLE);
    FormField ageField = memberForm.addField().setLabel("Age").setType(new QuantityType("years"));
    HrdStorageProvider catalog = new HrdStorageProvider();
    catalog.create(hhForm);
    catalog.create(memberForm);
    TypedRecordUpdate hh1 = new TypedRecordUpdate();
    hh1.setUserId(userId);
    hh1.setRecordId(ResourceId.generateSubmissionId(hhForm));
    hh1.set(hhIdField.getId(), TextValue.valueOf("HH1"));
    TypedRecordUpdate hh2 = new TypedRecordUpdate();
    hh2.setUserId(userId);
    hh2.setRecordId(ResourceId.generateSubmissionId(hhForm));
    hh2.set(hhIdField.getId(), TextValue.valueOf("HH2"));
    TypedRecordUpdate father1 = new TypedRecordUpdate();
    father1.setUserId(userId);
    father1.setRecordId(ResourceId.generateSubmissionId(memberForm));
    father1.setParentId(hh1.getRecordId());
    father1.set(nameField.getId(), TextValue.valueOf("Homer"));
    father1.set(ageField.getId(), new Quantity(40));
    TypedRecordUpdate father2 = new TypedRecordUpdate();
    father2.setUserId(userId);
    father2.setRecordId(ResourceId.generateSubmissionId(memberForm));
    father2.setParentId(hh2.getRecordId());
    father2.set(nameField.getId(), TextValue.valueOf("Ned"));
    father2.set(ageField.getId(), new Quantity(41));
    Optional<FormStorage> hhCollection = catalog.getForm(hhForm.getId());
    assertTrue(hhCollection.isPresent());
    hhCollection.get().add(hh1);
    hhCollection.get().add(hh2);
    Optional<FormStorage> memberCollection = catalog.getForm(memberForm.getId());
    assertTrue(memberCollection.isPresent());
    memberCollection.get().add(father1);
    memberCollection.get().add(father2);
    QueryModel queryModel = new QueryModel(memberForm.getId());
    queryModel.selectResourceId().as("id");
    queryModel.selectField("Household ID").as("hh_id");
    queryModel.selectField("Name").as("member_name");
    queryModel.selectField("Age").as("member_age");
    ColumnSetBuilder builder = new ColumnSetBuilder(catalog, new NullFormSupervisor());
    ColumnSet columnSet = builder.build(queryModel);
    System.out.println(columnSet);
    assertThat(columnSet.getNumRows(), equalTo(2));
}
Also used : Quantity(org.activityinfo.model.type.number.Quantity) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) NullFormSupervisor(org.activityinfo.store.query.shared.NullFormSupervisor) FormField(org.activityinfo.model.form.FormField) Test(org.junit.Test)

Example 72 with FormClass

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

the class HrdCatalogTest method enumWithNoChoices.

@Test
public void enumWithNoChoices() {
    final ResourceId formId = ResourceId.generateId();
    ResourceId villageField = ResourceId.valueOf("FV");
    final ResourceId selectField = ResourceId.valueOf("FC");
    FormClass formClass = new FormClass(formId);
    formClass.setParentFormId(ResourceId.valueOf("foo"));
    formClass.setLabel("NFI Distributions");
    formClass.addField(villageField).setLabel("Village name").setCode("VILLAGE").setType(TextType.SIMPLE);
    formClass.addField(selectField).setLabel("Favorite color").setType(new EnumType(Cardinality.SINGLE, EnumType.Presentation.AUTOMATIC, Collections.<EnumItem>emptyList()));
    HrdStorageProvider catalog = new HrdStorageProvider();
    catalog.create(formClass);
    // Avoid cache
    // objectifyCloseable.close();
    ObjectifyService.run(new VoidWork() {

        @Override
        public void vrun() {
            HrdStorageProvider catalog = new HrdStorageProvider();
            Optional<FormStorage> storage = catalog.getForm(formId);
            FormClass deserializedSchema = storage.get().getFormClass();
        }
    });
}
Also used : Optional(com.google.common.base.Optional) ResourceId(org.activityinfo.model.resource.ResourceId) VoidWork(com.googlecode.objectify.VoidWork) FormClass(org.activityinfo.model.form.FormClass) EnumType(org.activityinfo.model.type.enumerated.EnumType) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) Test(org.junit.Test)

Example 73 with FormClass

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

the class HrdCatalogTest method createResource.

@Test
public void createResource() {
    FormClass formClass = new FormClass(ResourceId.generateId());
    formClass.setParentFormId(ResourceId.valueOf("foo"));
    formClass.setLabel("NFI Distributions");
    FormField nameField = formClass.addField(ResourceId.generateId()).setLabel("Village name").setCode("VILLAGE").setType(TextType.SIMPLE);
    HrdStorageProvider catalog = new HrdStorageProvider();
    Updater updater = new Updater(catalog, userId, new BlobAuthorizerStub(), new HrdSerialNumberProvider());
    catalog.create(formClass);
    String[] villageNames = new String[] { "Rutshuru", "Beni", "Goma" };
    for (String villageName : villageNames) {
        TypedRecordUpdate update = new TypedRecordUpdate();
        update.setUserId(userId);
        update.setFormId(formClass.getId());
        update.setRecordId(ResourceId.generateSubmissionId(formClass));
        update.set(nameField.getId(), TextValue.valueOf(villageName));
        updater.execute(update);
    }
    QueryModel queryModel = new QueryModel(formClass.getId());
    queryModel.selectResourceId().as("id");
    queryModel.selectField("VILLAGE").as("village");
    ColumnSetBuilder builder = new ColumnSetBuilder(catalog, new NullFormSupervisor());
    ColumnSet columnSet = builder.build(queryModel);
    System.out.println(columnSet);
}
Also used : ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) FormClass(org.activityinfo.model.form.FormClass) Updater(org.activityinfo.store.query.server.Updater) NullFormSupervisor(org.activityinfo.store.query.shared.NullFormSupervisor) FormField(org.activityinfo.model.form.FormField) Test(org.junit.Test)

Example 74 with FormClass

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

the class SubFormAggregationTest method subFormAggregationTest.

@Test
public void subFormAggregationTest() {
    // Typical scenario with a household interview form
    // and a repeating househould member form
    FormClass siteForm = new FormClass(ResourceId.generateId());
    siteForm.setParentFormId(ResourceId.ROOT_ID);
    FormClass monthlyForm = new FormClass(ResourceId.generateId());
    monthlyForm.setParentFormId(siteForm.getId());
    monthlyForm.setSubFormKind(SubFormKind.MONTHLY);
    siteForm.setLabel("Household interview");
    FormField villageField = siteForm.addField().setLabel("Village Name").setType(TextType.SIMPLE);
    siteForm.addField().setLabel("Maximum Beneficiaries").setCode("BENE").setType(new CalculatedFieldType("MAX(HH)"));
    siteForm.addField().setLabel("Monthly Activities").setType(new SubFormReferenceType(monthlyForm.getId()));
    monthlyForm.setLabel("Monthly Activities");
    FormField countField = monthlyForm.addField().setLabel("Number of Beneficiaries").setCode("HH").setType(new QuantityType("households"));
    HrdStorageProvider catalog = new HrdStorageProvider();
    catalog.create(siteForm);
    catalog.create(monthlyForm);
    TypedRecordUpdate v1 = new TypedRecordUpdate();
    v1.setUserId(userId);
    v1.setRecordId(ResourceId.generateSubmissionId(siteForm));
    v1.set(villageField.getId(), TextValue.valueOf("Rutshuru"));
    TypedRecordUpdate v2 = new TypedRecordUpdate();
    v2.setUserId(userId);
    v2.setRecordId(ResourceId.generateSubmissionId(siteForm));
    v2.set(villageField.getId(), TextValue.valueOf("Beni"));
    TypedRecordUpdate month1 = new TypedRecordUpdate();
    month1.setUserId(userId);
    month1.setRecordId(ResourceId.generateSubmissionId(monthlyForm));
    month1.setParentId(v1.getRecordId());
    month1.set(countField.getId(), new Quantity(40));
    TypedRecordUpdate month2 = new TypedRecordUpdate();
    month2.setUserId(userId);
    month2.setRecordId(ResourceId.generateSubmissionId(monthlyForm));
    month2.setParentId(v1.getRecordId());
    month2.set(countField.getId(), new Quantity(30));
    TypedRecordUpdate month3 = new TypedRecordUpdate();
    month3.setUserId(userId);
    month3.setRecordId(ResourceId.generateSubmissionId(monthlyForm));
    month3.setParentId(v2.getRecordId());
    month3.set(countField.getId(), new Quantity(47));
    FormStorage siteCollection = catalog.getForm(siteForm.getId()).get();
    siteCollection.add(v1);
    siteCollection.add(v2);
    Optional<FormStorage> monthCollection = catalog.getForm(monthlyForm.getId());
    assertTrue(monthCollection.isPresent());
    monthCollection.get().add(month1);
    monthCollection.get().add(month2);
    monthCollection.get().add(month3);
    QueryModel queryModel = new QueryModel(siteForm.getId());
    queryModel.selectResourceId().as("id");
    queryModel.selectField("Village Name").as("village");
    queryModel.selectField("BENE").as("max_hh");
    ColumnSetBuilder builder = new ColumnSetBuilder(catalog, new NullFormSupervisor());
    ColumnSet columnSet = builder.build(queryModel);
    System.out.println(columnSet);
    assertThat(columnSet.getNumRows(), equalTo(2));
}
Also used : CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) Quantity(org.activityinfo.model.type.number.Quantity) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) TypedRecordUpdate(org.activityinfo.store.spi.TypedRecordUpdate) SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) FormStorage(org.activityinfo.store.spi.FormStorage) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) NullFormSupervisor(org.activityinfo.store.query.shared.NullFormSupervisor) FormField(org.activityinfo.model.form.FormField) Test(org.junit.Test)

Example 75 with FormClass

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

the class FixSubForm method maybeFixForm.

private boolean maybeFixForm(PrintWriter logger, FormClass parentForm, FormField formField) {
    SubFormReferenceType type = (SubFormReferenceType) formField.getType();
    ResourceId subFormId = type.getClassId();
    logger.println("Found subform " + formField.getLabel() + "(" + subFormId + ")");
    ResourceId parentFormId = parentForm.getId();
    FormSchemaEntity schemaEntity = Hrd.ofy().load().key(FormSchemaEntity.key(subFormId)).now();
    if (schemaEntity == null) {
        logger.println("Subform " + subFormId + " does not exist!!");
        return false;
    }
    FormClass schema = schemaEntity.readFormClass();
    if (schema.getParentFormId().get().equals(parentFormId)) {
        logger.println("Parent is OK");
        return false;
    }
    FormEntity root = Hrd.ofy().load().key(FormEntity.key(subFormId)).now();
    logger.println("At version " + root.getVersion());
    // Generate new subform id..
    ResourceId newSubFormId = ResourceId.generateId();
    schema.setId(newSubFormId);
    schema.setParentFormId(parentFormId);
    logger.println("Creating copy of subform => " + newSubFormId);
    if (!DRY_RUN) {
        new CreateOrUpdateForm(schema).run();
    }
    // Now find all records and update their schema
    copyRecords(logger, parentFormId, subFormId, newSubFormId);
    copySnapshots(logger, parentFormId, subFormId, newSubFormId);
    // Update the root entity to match version numbers
    FormEntity newRoot = new FormEntity();
    newRoot.setId(newSubFormId);
    newRoot.setVersion(root.getVersion());
    newRoot.setSchemaVersion(root.getSchemaVersion());
    if (!DRY_RUN) {
        Hrd.ofy().save().entity(newRoot);
    }
    // Finally update the MySQL parent
    formField.setType(new SubFormReferenceType(newSubFormId));
    return true;
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) FormEntity(org.activityinfo.store.hrd.entity.FormEntity) CreateOrUpdateForm(org.activityinfo.store.hrd.op.CreateOrUpdateForm) FormSchemaEntity(org.activityinfo.store.hrd.entity.FormSchemaEntity)

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