Search in sources :

Example 1 with FormMetadata

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"))));
}
Also used : FormMetadataProvider(org.activityinfo.model.formTree.FormMetadataProvider) RecordRef(org.activityinfo.model.type.RecordRef) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) FormTreeBuilder(org.activityinfo.model.formTree.FormTreeBuilder) FormMetadata(org.activityinfo.model.form.FormMetadata) SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) FormTree(org.activityinfo.model.formTree.FormTree) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) TestingFormStore(org.activityinfo.ui.client.store.TestingFormStore) FormStore(org.activityinfo.ui.client.store.FormStore) Test(org.junit.Test)

Example 2 with FormMetadata

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));
    });
}
Also used : HashSet(java.util.HashSet) FormSyncSet(org.activityinfo.model.form.FormSyncSet) Set(java.util.Set) ArrayList(java.util.ArrayList) Observable(org.activityinfo.observable.Observable) FormMetadata(org.activityinfo.model.form.FormMetadata) FormTree(org.activityinfo.model.formTree.FormTree) ResourceId(org.activityinfo.model.resource.ResourceId) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 3 with FormMetadata

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;
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) FormField(org.activityinfo.model.form.FormField) RecordFieldType(org.activityinfo.model.type.RecordFieldType) FormMetadata(org.activityinfo.model.form.FormMetadata)

Example 4 with FormMetadata

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));
}
Also used : FormInstance(org.activityinfo.model.form.FormInstance) FormMetadata(org.activityinfo.model.form.FormMetadata)

Aggregations

FormMetadata (org.activityinfo.model.form.FormMetadata)4 ResourceId (org.activityinfo.model.resource.ResourceId)3 FormClass (org.activityinfo.model.form.FormClass)2 FormTree (org.activityinfo.model.formTree.FormTree)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 FormField (org.activityinfo.model.form.FormField)1 FormInstance (org.activityinfo.model.form.FormInstance)1 FormSyncSet (org.activityinfo.model.form.FormSyncSet)1 FormMetadataProvider (org.activityinfo.model.formTree.FormMetadataProvider)1 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)1 RecordFieldType (org.activityinfo.model.type.RecordFieldType)1 RecordRef (org.activityinfo.model.type.RecordRef)1 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)1 Observable (org.activityinfo.observable.Observable)1 FormInputModel (org.activityinfo.ui.client.input.model.FormInputModel)1 FormStore (org.activityinfo.ui.client.store.FormStore)1 TestingFormStore (org.activityinfo.ui.client.store.TestingFormStore)1