Search in sources :

Example 21 with RecordRef

use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.

the class Presenter method choices.

private Supplier<Promise<List<Choice>>> choices(final Level level) {
    final QueryModel queryModel = new QueryModel(level.getFormId());
    queryModel.selectResourceId().as("id");
    queryModel.selectExpr("label").as("label");
    if (!level.isRoot()) {
        Choice selectedParent = getSelection(level.getParent());
        queryModel.selectExpr("parent").as("parent");
        queryModel.setFilter(Formulas.equals(new SymbolNode("parent"), Formulas.idConstant(selectedParent.getRef().getRecordId())));
    }
    return new Supplier<Promise<List<Choice>>>() {

        @Override
        public Promise<List<Choice>> get() {
            return locator.queryTable(queryModel).then(new Function<ColumnSet, List<Choice>>() {

                @Nullable
                @Override
                public List<Choice> apply(ColumnSet input) {
                    ColumnView id = input.getColumnView("id");
                    ColumnView label = input.getColumnView("label");
                    ColumnView parent = input.getColumnView("parent");
                    List<Choice> choices = new ArrayList<>();
                    for (int i = 0; i < input.getNumRows(); i++) {
                        if (parent == null) {
                            choices.add(new Choice(level.getFormId(), ResourceId.valueOf(id.getString(i)), label.getString(i)));
                        } else {
                            choices.add(new Choice(level.getFormId(), ResourceId.valueOf(id.getString(i)), label.getString(i), new RecordRef(level.getParent().getFormId(), ResourceId.valueOf(parent.getString(i)))));
                        }
                    }
                    return choices;
                }
            });
        }
    };
}
Also used : SymbolNode(org.activityinfo.model.formula.SymbolNode) ColumnView(org.activityinfo.model.query.ColumnView) RecordRef(org.activityinfo.model.type.RecordRef) Supplier(com.google.common.base.Supplier) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) Nullable(javax.annotation.Nullable)

Example 22 with RecordRef

use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.

the class ReferenceFieldMapping method mapFieldValue.

public FieldValue mapFieldValue(int sourceIndex) {
    int keyIndex = sourceKeySet.getKeyIndexOfSourceRow(sourceIndex);
    ResourceId targetId = lookupTable.get().getTargetMatchId(keyIndex);
    ResourceId targetFormId = Iterables.getOnlyElement(((ReferenceType) targetReferenceField.getType()).getRange());
    return new ReferenceValue(new RecordRef(targetFormId, targetId));
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) ReferenceValue(org.activityinfo.model.type.ReferenceValue) RecordRef(org.activityinfo.model.type.RecordRef)

Example 23 with RecordRef

use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.

the class ResourceLocatorSyncImpl method getReferenceChoices.

@Override
public List<ReferenceChoice> getReferenceChoices(Collection<ResourceId> range) {
    ResourceId formId = Iterables.getOnlyElement(range);
    QueryModel queryModel = new QueryModel(formId);
    queryModel.selectResourceId().as("id");
    queryModel.selectExpr("label").as("label");
    ColumnSetBuilder builder = new ColumnSetBuilder(catalog.get(), new NullFormSupervisor());
    ColumnSet columnSet = builder.build(queryModel);
    ColumnView id = columnSet.getColumnView("id");
    ColumnView label = columnSet.getColumnView("label");
    List<ReferenceChoice> choices = Lists.newArrayList();
    for (int i = 0; i < columnSet.getNumRows(); i++) {
        ResourceId choiceId = ResourceId.valueOf(id.getString(i));
        String choiceLabel = label.getString(i);
        choices.add(new ReferenceChoice(new RecordRef(formId, choiceId), choiceLabel));
    }
    return choices;
}
Also used : ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) ResourceId(org.activityinfo.model.resource.ResourceId) ColumnView(org.activityinfo.model.query.ColumnView) RecordRef(org.activityinfo.model.type.RecordRef) ColumnSet(org.activityinfo.model.query.ColumnSet) NullFormSupervisor(org.activityinfo.store.query.shared.NullFormSupervisor) QueryModel(org.activityinfo.model.query.QueryModel)

Example 24 with RecordRef

use of org.activityinfo.model.type.RecordRef 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);
}
Also used : SubFormInstanceLoader(org.activityinfo.ui.client.component.form.subform.SubFormInstanceLoader) Set(java.util.Set) OnDataSet(org.activityinfo.server.database.OnDataSet) ReferenceValue(org.activityinfo.model.type.ReferenceValue) RecordRef(org.activityinfo.model.type.RecordRef) GxtStateProvider(org.activityinfo.ui.client.dispatch.state.GxtStateProvider) LocalDate(org.activityinfo.model.type.time.LocalDate) Month(org.activityinfo.model.type.time.Month) FormInstance(org.activityinfo.model.form.FormInstance) Test(org.junit.Test)

Example 25 with RecordRef

use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.

the class HierarchyTest method buildViewModelTest.

@Test
public void buildViewModelTest() {
    FormClass campForm = assertResolves(locator.getFormClass(CAMP_CLASS));
    FormField adminField = campForm.getField(CuidAdapter.field(CAMP_CLASS, CuidAdapter.ADMIN_FIELD));
    Set<RecordRef> fieldValue = Collections.singleton(new RecordRef(CAMP_DISTRICT_CLASS, entity(325703)));
    Hierarchy tree = assertResolves(Hierarchy.get(locator, (ReferenceType) adminField.getType()));
    prettyPrintTree(tree);
    assertThat(tree.getRoots(), hasSize(1));
    createWidgets(tree);
    Presenter presenter = new Presenter(locator, tree, widgets, new ValueUpdater() {

        @Override
        public void update(Object value) {
            System.out.println("VALUE = " + value);
        }
    });
    assertResolves(presenter.setInitialSelection(fieldValue));
    assertThat(presenter.getSelectionLabel(CAMP_DISTRICT_CLASS), equalTo("District 5"));
    // now try to get options for the root level
    List<Choice> choices = assertResolves(widgets.get(REGION).choices.get());
    System.out.println(choices);
    assertThat(choices, hasSize(3));
    // if we change the root item, then all descendants should be cleared
    widgets.get(REGION).setSelection("South");
    prettyPrintWidgets(tree);
    assertThat(widgets.get(CAMP_DISTRICT_CLASS).selection, isEmptyOrNullString());
    assertThat(widgets.get(GOVERNORATE_ID).choices, Matchers.notNullValue());
    List<Choice> governorateChoices = assertResolves(widgets.get(GOVERNORATE_ID).choices.get());
    System.out.println(governorateChoices);
    assertThat(governorateChoices, hasSize(4));
}
Also used : ValueUpdater(com.google.gwt.cell.client.ValueUpdater) FormClass(org.activityinfo.model.form.FormClass) RecordRef(org.activityinfo.model.type.RecordRef) FormField(org.activityinfo.model.form.FormField) ReferenceType(org.activityinfo.model.type.ReferenceType) Test(org.junit.Test)

Aggregations

RecordRef (org.activityinfo.model.type.RecordRef)66 Test (org.junit.Test)31 ResourceId (org.activityinfo.model.resource.ResourceId)26 ReferenceValue (org.activityinfo.model.type.ReferenceValue)26 FormInputModel (org.activityinfo.ui.client.input.model.FormInputModel)14 FormInstance (org.activityinfo.model.form.FormInstance)7 FormTree (org.activityinfo.model.formTree.FormTree)7 LocalDate (org.activityinfo.model.type.time.LocalDate)7 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)6 Optional (com.google.common.base.Optional)5 FieldValue (org.activityinfo.model.type.FieldValue)5 FieldInput (org.activityinfo.ui.client.input.model.FieldInput)5 FormClass (org.activityinfo.model.form.FormClass)4 ResultSet (java.sql.ResultSet)3 FormField (org.activityinfo.model.form.FormField)3 RecordTree (org.activityinfo.model.formTree.RecordTree)3 ColumnSet (org.activityinfo.model.query.ColumnSet)3 ColumnView (org.activityinfo.model.query.ColumnView)3 RecordTransaction (org.activityinfo.model.resource.RecordTransaction)3 RecordUpdate (org.activityinfo.model.resource.RecordUpdate)3