Search in sources :

Example 36 with FormInstance

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

the class ActivityFormClassBuilderTest method partnersFieldIsAlwaysVisible.

@Test
@OnDataSet("/dbunit/sites-simple1.db.xml")
public void partnersFieldIsAlwaysVisible() {
    setUser(BAVON_USER_ID);
    FormClass formClass = assertResolves(locator.getFormClass(CuidAdapter.activityFormClass(1)));
    int databaseId = 1;
    ResourceId partnerFieldId = CuidAdapter.field(formClass.getId(), CuidAdapter.PARTNER_FIELD);
    FormField partnerField = formClass.getField(partnerFieldId);
    // according to ai-1009 : partner field is always visible
    assertThat(partnerField, hasProperty("visible", equalTo(true)));
    // Make sure we can update if partner is not specified
    FormInstance instance = new FormInstance(CuidAdapter.newLegacyFormInstanceId(formClass.getId()), formClass.getId());
    instance.set(CuidAdapter.field(formClass.getId(), CuidAdapter.START_DATE_FIELD), new LocalDate(2014, 1, 1));
    instance.set(CuidAdapter.field(formClass.getId(), CuidAdapter.END_DATE_FIELD), new LocalDate(2014, 1, 2));
    instance.set(CuidAdapter.field(formClass.getId(), CuidAdapter.LOCATION_FIELD), new ReferenceValue(new RecordRef(CuidAdapter.locationFormClass(1), CuidAdapter.locationInstanceId(1))));
    instance.set(CuidAdapter.field(formClass.getId(), CuidAdapter.PARTNER_FIELD), CuidAdapter.partnerRef(databaseId, 1));
    assertResolves(locator.persist(instance));
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) ReferenceValue(org.activityinfo.model.type.ReferenceValue) RecordRef(org.activityinfo.model.type.RecordRef) FormInstance(org.activityinfo.model.form.FormInstance) FormField(org.activityinfo.model.form.FormField) LocalDate(org.activityinfo.model.type.time.LocalDate) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Example 37 with FormInstance

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

the class UpdaterTest method serialNumber.

@Test
public void serialNumber() throws JsonMappingException {
    FormClass formClass = new FormClass(ResourceId.valueOf("FORM1"));
    formClass.addField(ResourceId.valueOf("FIELD0")).setType(TextType.SIMPLE).setLabel("Province Code").setCode("PROVINCE").setRequired(true);
    FormField serialNumberField = formClass.addField(ResourceId.valueOf("FIELD1")).setType(new SerialNumberType("PROVINCE", 5)).setRequired(true).setLabel("File Number").setCode("SN");
    JsonValue fields = Json.createObject();
    fields.put("PROVINCE", "KUNDUZ");
    JsonValue change = createObject();
    change.put("recordId", "A");
    change.put("formId", "FORM1");
    change.put("fields", fields);
    TypedRecordUpdate update = Updater.parseChange(formClass, change, userId);
    FormInstance effectiveRecord = updater.computeEffectiveRecord(formClass, Optional.<FormRecord>absent(), update);
    updater.generateSerialNumber(formClass, serialNumberField, effectiveRecord, update);
    FieldValue serialValue = update.getChangedFieldValues().get(serialNumberField.getId());
    assertThat(serialValue, equalTo((FieldValue) new SerialNumber("KUNDUZ", 1)));
}
Also used : SerialNumber(org.activityinfo.model.type.SerialNumber) FormClass(org.activityinfo.model.form.FormClass) JsonValue(org.activityinfo.json.JsonValue) FieldValue(org.activityinfo.model.type.FieldValue) FormInstance(org.activityinfo.model.form.FormInstance) FormField(org.activityinfo.model.form.FormField) SerialNumberType(org.activityinfo.model.type.SerialNumberType) TypedRecordUpdate(org.activityinfo.store.spi.TypedRecordUpdate) Test(org.junit.Test)

Example 38 with FormInstance

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

the class FormActions method save.

public Promise<List<FormInstance>> save() {
    model.getEventBus().fireEvent(new BeforeSaveEvent());
    final List<FormInstance> instancesToPersist = getInstancesToPersist();
    Promise<Void> persist = locator.persist(instancesToPersist);
    Promise<Void> remove = remove();
    final Promise<List<FormInstance>> result = new Promise<>();
    Promise.waitAll(persist, remove).then(new AsyncCallback<Void>() {

        @Override
        public void onFailure(Throwable caught) {
            result.onFailure(caught);
        }

        @Override
        public void onSuccess(Void input) {
            model.getChangedInstances().clear();
            result.onSuccess(instancesToPersist);
        }
    });
    return result;
}
Also used : Promise(org.activityinfo.promise.Promise) BeforeSaveEvent(org.activityinfo.ui.client.component.form.event.BeforeSaveEvent) List(java.util.List) FormInstance(org.activityinfo.model.form.FormInstance)

Example 39 with FormInstance

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

the class FormActions method remove.

private Promise<Void> remove() {
    Promise<Void> remove = Promise.done();
    if (!model.getPersistedInstanceToRemoveByLocator().isEmpty()) {
        List<Promise<Void>> removePromises = Lists.newArrayList();
        for (FormInstance instance : model.getPersistedInstanceToRemoveByLocator()) {
            removePromises.add(locator.remove(instance.getFormId(), instance.getId()));
        }
        remove = Promise.waitAll(removePromises);
        remove.then(new Function<Void, Object>() {

            @Override
            public Object apply(Void input) {
                model.getPersistedInstanceToRemoveByLocator().clear();
                return null;
            }
        });
    }
    return remove;
}
Also used : Promise(org.activityinfo.promise.Promise) FormInstance(org.activityinfo.model.form.FormInstance)

Example 40 with FormInstance

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

the class MySqlUpdateTest method createFormWithSerialNumber.

@Test
public void createFormWithSerialNumber() {
    userId = 1;
    KeyGenerator generator = new KeyGenerator();
    int activityId = generator.generateInt();
    FormClass formClass = new FormClass(CuidAdapter.activityFormClass(activityId));
    formClass.setDatabaseId(1);
    formClass.setLabel("New Form");
    FormField serialNumField = new FormField(CuidAdapter.generateIndicatorId()).setType(new SerialNumberType()).setLabel("NUM").setRequired(true);
    formClass.addElement(serialNumField);
    FormField nameField = new FormField(CuidAdapter.generateIndicatorId()).setType(TextType.SIMPLE).setLabel("Name").setRequired(true);
    formClass.addElement(nameField);
    catalog.createOrUpdateFormSchema(formClass);
    newRequest();
    ResourceId siteId = CuidAdapter.generateSiteCuid();
    // Create the record
    FormInstance creation = new FormInstance(siteId, formClass.getId());
    creation.set(nameField.getId(), TextValue.valueOf("Bob"));
    creation.set(partnerField(activityId), CuidAdapter.partnerRef(1, 1));
    executeUpdate(creation);
    newRequest();
    // Verify that the record has been created
    FormInstance created = FormInstance.toFormInstance(formClass, catalog.getForm(formClass.getId()).get().get(siteId).get());
    assertThat(created.get(nameField.getId()), equalTo((FieldValue) TextValue.valueOf("Bob")));
    assertThat(created.get(serialNumField.getId()), equalTo((FieldValue) new SerialNumber(1)));
    newRequest();
    // Now update the record's name
    JsonValue fieldValues = createObject();
    fieldValues.put(nameField.getName(), TextValue.valueOf("Sue").toJson());
    // the UI may send null values
    fieldValues.put(serialNumField.getName(), Json.createNull());
    JsonValue update = createObject();
    update.put("fieldValues", fieldValues);
    updater().execute(formClass.getId(), siteId, update);
    newRequest();
    // Finally verify that the serial number is unchanged
    FormInstance updated = FormInstance.toFormInstance(formClass, catalog.getForm(formClass.getId()).get().get(siteId).get());
    assertThat(updated.get(nameField.getId()), equalTo((FieldValue) TextValue.valueOf("Sue")));
    assertThat(updated.get(serialNumField.getId()), equalTo((FieldValue) new SerialNumber(1)));
}
Also used : SerialNumber(org.activityinfo.model.type.SerialNumber) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) JsonValue(org.activityinfo.json.JsonValue) FieldValue(org.activityinfo.model.type.FieldValue) FormInstance(org.activityinfo.model.form.FormInstance) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) FormField(org.activityinfo.model.form.FormField) SerialNumberType(org.activityinfo.model.type.SerialNumberType) GeoPoint(org.activityinfo.model.type.geo.GeoPoint) Test(org.junit.Test)

Aggregations

FormInstance (org.activityinfo.model.form.FormInstance)46 Test (org.junit.Test)26 ResourceId (org.activityinfo.model.resource.ResourceId)15 FormClass (org.activityinfo.model.form.FormClass)14 FieldValue (org.activityinfo.model.type.FieldValue)10 FormField (org.activityinfo.model.form.FormField)9 RecordRef (org.activityinfo.model.type.RecordRef)8 ReferenceValue (org.activityinfo.model.type.ReferenceValue)7 LocalDate (org.activityinfo.model.type.time.LocalDate)7 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)6 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)6 OnDataSet (org.activityinfo.server.database.OnDataSet)6 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)5 SerialNumberType (org.activityinfo.model.type.SerialNumberType)5 FormRecord (org.activityinfo.model.form.FormRecord)4 SerialNumber (org.activityinfo.model.type.SerialNumber)4 Map (java.util.Map)3 FormTree (org.activityinfo.model.formTree.FormTree)3 Maybe (org.activityinfo.promise.Maybe)3 ArrayList (java.util.ArrayList)2