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;
}
});
}
};
}
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));
}
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;
}
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);
}
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));
}
Aggregations