use of org.activityinfo.model.form.FormMetadata in project activityinfo by bedatadriven.
the class FormInputViewModelTest method hiddenSubForm.
/**
* Test the ViewModel for when the user does not have access to a referenced sub form.
*/
@Test
public void hiddenSubForm() {
FormClass parentForm = new FormClass(ResourceId.valueOf("PARENT_FORM"));
parentForm.addField(ResourceId.valueOf("F1")).setLabel("What is your name?").setType(TextType.SIMPLE);
parentForm.addField(ResourceId.valueOf("F2")).setLabel("What are your secrets?").setType(new SubFormReferenceType(ResourceId.valueOf("SECRET_FORM")));
FormTreeBuilder treeBuilder = new FormTreeBuilder(new FormMetadataProvider() {
@Override
public FormMetadata getFormMetadata(ResourceId formId) {
if (formId.equals(parentForm.getId())) {
return FormMetadata.of(1, parentForm, FormPermissions.owner());
} else {
return FormMetadata.forbidden(formId);
}
}
});
FormTree formTree = treeBuilder.queryTree(parentForm.getId());
FormStore formStore = EasyMock.createMock(FormStore.class);
EasyMock.replay(formStore);
FormInputViewModelBuilder viewModelBuilder = new FormInputViewModelBuilder(formStore, formTree, new TestingActivePeriodMemory());
FormInputViewModel viewModel = viewModelBuilder.build(new FormInputModel(new RecordRef(parentForm.getId(), ResourceId.valueOf("R1"))));
}
use of org.activityinfo.model.form.FormMetadata in project activityinfo by bedatadriven.
the class Snapshot method compute.
public static Observable<Snapshot> compute(Observable<Set<ResourceId>> offlineForms, HttpStore httpStore) {
// We start with the "offlineForm" set which contains the set
// of forms the user has explicitly asked to cache.
// In order to find the related forms, we need the complete form trees of each of the
// selected forms.
Observable<List<FormTree>> formTrees = flatMap(offlineForms, httpStore::getFormTree);
// Together, all the related forms constitute the set of forms we need for
// a complete offline snapshot
Observable<Set<ResourceId>> completeSet = formTrees.transform(trees -> {
Set<ResourceId> set = new HashSet<>();
for (FormTree tree : trees) {
for (FormMetadata form : tree.getForms()) {
if (!isBuiltinForm(form.getId())) {
set.add(form.getId());
}
}
}
return set;
});
// Now need fetch the latest version numbers of each of these forms
Observable<List<FormMetadata>> metadata = flatMap(completeSet, httpStore::getFormMetadata);
// And finally fetch any difference between our current snapshot and the latest version of the new snapshot
return metadata.join(forms -> {
List<Observable<FormSyncSet>> recordSets = new ArrayList<>();
for (FormMetadata form : forms) {
recordSets.add(httpStore.getVersionRange(form.getId(), 0, form.getVersion()));
}
return Observable.flatten(recordSets).transform(x -> new Snapshot(forms, x));
});
}
use of org.activityinfo.model.form.FormMetadata in project activityinfo by bedatadriven.
the class FormTreeBuilder method queryTree.
public FormTree queryTree(ResourceId rootFormId) {
FormTree tree = new FormTree(rootFormId);
FormMetadata root = metadataProvider.getFormMetadata(rootFormId);
if (root.isDeleted()) {
tree.setRootState(FormTree.State.DELETED);
return tree;
}
if (!root.isVisible()) {
tree.setRootState(FormTree.State.FORBIDDEN);
return tree;
}
FormClass rootSchema = root.getSchema();
List<ResourceId> stack = Lists.newArrayList(rootFormId);
Optional<FormField> parentField = rootSchema.getParentField();
if (parentField.isPresent()) {
if (!stack.contains(parentField.get().getId())) {
tree.addFormMetadata(root);
FormTree.Node node = tree.addRootField(root, parentField.get());
fetchChildren(stack, tree, node, rootSchema.getParentFormId().asSet());
}
}
// Add fields defined by this FormClass
for (FormField field : rootSchema.getFields()) {
tree.addFormMetadata(root);
FormTree.Node node = tree.addRootField(root, field);
if (node.isReference()) {
fetchChildren(stack, tree, node, node.getRange());
} else if (field.getType() instanceof RecordFieldType) {
addChildren(stack, tree, node, embeddedForm(node));
}
}
return tree;
}
use of org.activityinfo.model.form.FormMetadata in project activityinfo by bedatadriven.
the class SelectionViewModel method computeSelection.
private static Optional<SelectionViewModel> computeSelection(FormTree formTree, Maybe<FormRecord> record) {
if (!record.isVisible()) {
return Optional.absent();
}
FormMetadata form = formTree.getFormMetadata(ResourceId.valueOf(record.get().getFormId()));
if (!form.isVisible()) {
return Optional.absent();
}
FormInstance typedRecord = FormInstance.toFormInstance(form.getSchema(), record.get());
boolean editAllowed = evalPermission(form, typedRecord, Operation.EDIT_RECORD);
return Optional.of(new SelectionViewModel(typedRecord, editAllowed, editAllowed));
}
Aggregations