Search in sources :

Example 31 with FormClass

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

the class JaxRsJsonReaderTest method modelClass.

@Test
public void modelClass() throws IOException {
    FormClass formClass = new FormClass(ResourceId.valueOf("FORM"));
    formClass.setLabel("My Form");
    formClass.addField(ResourceId.valueOf("F1")).setLabel("My field").setRequired(true).setType(TextType.SIMPLE).setCode("MY");
    JaxRsJsonReader reader = new JaxRsJsonReader();
    assertTrue(reader.isReadable(FormClass.class, FormClass.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE));
    assertTrue(reader.isWriteable(FormClass.class, FormClass.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE));
    ByteArrayOutputStream entity = new ByteArrayOutputStream();
    reader.writeTo(formClass, formClass.getClass(), formClass.getClass(), new Annotation[0], MediaType.APPLICATION_JSON_TYPE, EMPTY_HEADERS, entity);
    JsonValue jsonObject = Json.parse(new String(entity.toByteArray(), Charsets.UTF_8));
    System.out.println(Json.stringify(jsonObject));
    FormClass reformClass = (FormClass) reader.readFrom((Class) FormClass.class, FormClass.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE, EMPTY_HEADERS, new ByteArrayInputStream(entity.toByteArray()));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FormClass(org.activityinfo.model.form.FormClass) JsonValue(org.activityinfo.json.JsonValue) JaxRsJsonReader(org.activityinfo.server.endpoint.rest.JaxRsJsonReader) FormClass(org.activityinfo.model.form.FormClass) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Annotation(java.lang.annotation.Annotation) Test(org.junit.Test)

Example 32 with FormClass

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

the class FormModelTest method doNotPersistFormClassWithStaleSubformReference.

@Ignore
@Test
public void doNotPersistFormClassWithStaleSubformReference() {
    setupForms();
    FormClass formClass = new FormClass(ResourceId.generateId());
    formClass.setDatabaseId(DATABASE_ID);
    FormClass subform = new FormClass(ResourceId.generateId());
    subform.setDatabaseId(DATABASE_ID);
    FormField subformOwnerField = formClass.addField(CuidAdapter.generateIndicatorId());
    subformOwnerField.setType(new SubFormReferenceType(subform.getId()));
    locator.persist(formClass).then(new AsyncCallback<Void>() {

        @Override
        public void onFailure(Throwable caught) {
        // expected result
        }

        @Override
        public void onSuccess(Void result) {
            throw new RuntimeException("FormClass is persisted with stale (non-existent) SubFormClass reference.");
        }
    });
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) FormClass(org.activityinfo.model.form.FormClass) FormField(org.activityinfo.model.form.FormField) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 33 with FormClass

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

the class FormModelTest method setupForms.

/**
 * Sets up test fixtures. Must be called by each test to ensure that it runs AFTER
 * the dbunit setup
 */
public void setupForms() {
    ResourceId masterFormId = CuidAdapter.activityFormClass(3);
    masterFormClass = new FormClass(masterFormId);
    masterFormClass.setDatabaseId(CuidAdapter.databaseId(1));
    FormField partnerField = new FormField(CuidAdapter.partnerField(3));
    partnerField.setLabel("Partner");
    partnerField.setType(ReferenceType.single(CuidAdapter.partnerFormId(1)));
    masterFormClass.addElement(partnerField);
    FormField labelField = masterFormClass.addField(CuidAdapter.generateIndicatorId());
    labelField.setLabel("label1");
    labelField.setType(TextType.SIMPLE);
    subFormClass = new FormClass(ResourceId.generateId());
    subFormClass.setDatabaseId(masterFormClass.getDatabaseId());
    subFormClass.setParentFormId(masterFormId);
    subFormClass.setSubFormKind(SubFormKind.MONTHLY);
    subFormChildField = subFormClass.addField();
    subFormChildField.setType(TextType.SIMPLE);
    subFormField = masterFormClass.addField(CuidAdapter.generateIndicatorId());
    subFormField.setType(new SubFormReferenceType(subFormClass.getId()));
    assertResolves(locator.persist(subFormClass));
    assertResolves(locator.persist(masterFormClass));
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) FormField(org.activityinfo.model.form.FormField)

Example 34 with FormClass

use of org.activityinfo.model.form.FormClass 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)

Example 35 with FormClass

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

the class MySqlStorageProvider method getFormClasses.

@Override
public Map<ResourceId, FormClass> getFormClasses(Collection<ResourceId> formIds) {
    Map<ResourceId, FormClass> resultMap = new HashMap<>();
    Set<ResourceId> toFetch = new HashSet<>();
    // First check sessionCache for any collections which are already loaded
    for (ResourceId collectionId : formIds) {
        Optional<FormStorage> collection = sessionCache.getIfPresent(collectionId);
        if (collection != null && collection.isPresent()) {
            resultMap.put(collectionId, collection.get().getFormClass());
        } else {
            toFetch.add(collectionId);
        }
    }
    // Now consult each of our providers for collections
    try {
        for (FormProvider provider : providers) {
            Map<ResourceId, FormStorage> fetched = provider.openForms(executor, toFetch);
            for (Map.Entry<ResourceId, FormStorage> entry : fetched.entrySet()) {
                if (resultMap.containsKey(entry.getKey())) {
                    throw new IllegalStateException("Collection " + entry.getKey() + " returned by multiple providers");
                }
                sessionCache.put(entry.getKey(), Optional.of(entry.getValue()));
                resultMap.put(entry.getKey(), entry.getValue().getFormClass());
            }
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
    // Finally, verify that all collections were loaded
    for (ResourceId collectionId : formIds) {
        if (!resultMap.containsKey(collectionId)) {
            throw new FormNotFoundException(collectionId);
        }
    }
    return resultMap;
}
Also used : SQLException(java.sql.SQLException) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass)

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