Search in sources :

Example 41 with FormInstance

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

the class MySqlUpdateTest method updateLocation.

@Test
public void updateLocation() throws SQLException {
    ResourceId recordId = CuidAdapter.cuid(LOCATION_DOMAIN, 1);
    ResourceId formId = CuidAdapter.locationFormClass(1);
    RecordUpdate update = new RecordUpdate();
    update.setFormId(formId);
    update.setRecordId(recordId);
    update.setFieldValue(CuidAdapter.field(formId, CuidAdapter.NAME_FIELD), TextValue.valueOf("New Name"));
    Updater updater = updater();
    updater.executeChange(update);
    newRequest();
    FormStorage formStorage = catalog.getForm(formId).get();
    FormRecord record = formStorage.get(recordId).get();
    FormInstance typedRecord = FormInstance.toFormInstance(formStorage.getFormClass(), record);
    GeoPoint point = (GeoPoint) typedRecord.get(CuidAdapter.field(formId, CuidAdapter.GEOMETRY_FIELD));
    assertThat(point, not(nullValue()));
}
Also used : GeoPoint(org.activityinfo.model.type.geo.GeoPoint) RecordUpdate(org.activityinfo.model.resource.RecordUpdate) TypedRecordUpdate(org.activityinfo.store.spi.TypedRecordUpdate) FormStorage(org.activityinfo.store.spi.FormStorage) ResourceId(org.activityinfo.model.resource.ResourceId) Updater(org.activityinfo.store.query.server.Updater) FormRecord(org.activityinfo.model.form.FormRecord) FormInstance(org.activityinfo.model.form.FormInstance) Test(org.junit.Test)

Example 42 with FormInstance

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

the class MySqlUpdateTest method addNewAttributes.

@Test
public void addNewAttributes() {
    KeyGenerator generator = new KeyGenerator();
    int activityId = generator.generateInt();
    EnumType enumType = new EnumType(Cardinality.SINGLE, new EnumItem(EnumItem.generateId(), "A"), new EnumItem(EnumItem.generateId(), "B"));
    FormField selectField = new FormField(ResourceId.generateFieldId(EnumType.TYPE_CLASS)).setType(enumType).setLabel("Select");
    FormClass formClass = new FormClass(CuidAdapter.activityFormClass(activityId));
    formClass.setDatabaseId(1);
    formClass.setLabel("New Form");
    formClass.addElement(selectField);
    catalog.createOrUpdateFormSchema(formClass);
    System.out.println("Created activity " + activityId);
    // Now change the enum items
    EnumType updatedType = new EnumType(Cardinality.SINGLE, new EnumItem(EnumItem.generateId(), "C"), new EnumItem(EnumItem.generateId(), "D"));
    selectField.setType(updatedType);
    newRequest();
    catalog.createOrUpdateFormSchema(formClass);
    newRequest();
    // Now try to save a new instance with the value
    FieldValue valueC = new EnumValue(updatedType.getValues().get(0).getId());
    FormInstance newRecord = new FormInstance(CuidAdapter.generateSiteCuid(), formClass.getId());
    newRecord.set(selectField.getId(), new EnumValue(updatedType.getValues().get(0).getId()));
    newRecord.set(CuidAdapter.partnerField(activityId), CuidAdapter.partnerRef(1, 1));
    executeUpdate(newRecord);
    // Ensure that the select field has been saved
    FormRecord saved = catalog.getForm(formClass.getId()).get().get(newRecord.getId()).get();
    FormInstance savedInstance = FormInstance.toFormInstance(formClass, saved);
    assertThat(savedInstance.get(selectField.getId()), equalTo(valueC));
}
Also used : EnumType(org.activityinfo.model.type.enumerated.EnumType) FormClass(org.activityinfo.model.form.FormClass) EnumValue(org.activityinfo.model.type.enumerated.EnumValue) FieldValue(org.activityinfo.model.type.FieldValue) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) FormInstance(org.activityinfo.model.form.FormInstance) FormRecord(org.activityinfo.model.form.FormRecord) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) FormField(org.activityinfo.model.form.FormField) GeoPoint(org.activityinfo.model.type.geo.GeoPoint) Test(org.junit.Test)

Example 43 with FormInstance

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

the class FormInputViewModelBuilder method build.

public FormInputViewModel build(FormInputModel inputModel, Maybe<RecordTree> existingRecord, boolean placeholder) {
    // Combine the original values of the record with the newly entered data
    Map<ResourceId, FieldValue> existingValues = existingRecord.transform(r -> r.getRoot().getFieldValueMap()).or(emptyMap());
    FormInstance record = computeUpdatedRecord(existingValues, inputModel);
    // Now construct the viewModel that includes everything about
    // the current state of data entry.
    FormInputViewModel viewModel = new FormInputViewModel();
    viewModel.formTree = this.formTree;
    viewModel.inputModel = inputModel;
    viewModel.fieldValueMap = record.getFieldValueMap();
    viewModel.subFormMap = computeSubViewModels(inputModel, existingRecord);
    viewModel.existingValues = existingValues;
    viewModel.placeholder = placeholder;
    viewModel.relevant = computeRelevance(record);
    viewModel.missing = computeMissing(record, viewModel.relevant);
    viewModel.validationErrors = validateFieldValues(record);
    viewModel.dirty = computeDirty(placeholder, existingValues, record);
    viewModel.locked = checkLocks(record);
    viewModel.valid = allInputValid(inputModel) && viewModel.missing.isEmpty() && viewModel.validationErrors.isEmpty() && viewModel.subFormMap.values().stream().allMatch(SubFormViewModel::isValid);
    LOGGER.info("Valid = " + viewModel.valid);
    return viewModel;
}
Also used : java.util(java.util) FormulaParser(org.activityinfo.model.formula.FormulaParser) PeriodValue(org.activityinfo.model.type.time.PeriodValue) Multimap(com.google.common.collect.Multimap) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) Level(java.util.logging.Level) SerialNumberType(org.activityinfo.model.type.SerialNumberType) HashMultimap(com.google.common.collect.HashMultimap) ResourceId(org.activityinfo.model.resource.ResourceId) RecordTree(org.activityinfo.model.formTree.RecordTree) FormEvalContext(org.activityinfo.model.form.FormEvalContext) RecordRef(org.activityinfo.model.type.RecordRef) FormStore(org.activityinfo.ui.client.store.FormStore) FieldValue(org.activityinfo.model.type.FieldValue) RecordLockSet(org.activityinfo.model.database.RecordLockSet) Collections.emptyMap(java.util.Collections.emptyMap) FormInstance(org.activityinfo.model.form.FormInstance) Logger(java.util.logging.Logger) FormulaNode(org.activityinfo.model.formula.FormulaNode) FieldInput(org.activityinfo.ui.client.input.model.FieldInput) FormField(org.activityinfo.model.form.FormField) TextType(org.activityinfo.model.type.primitive.TextType) Predicate(com.google.common.base.Predicate) Maybe(org.activityinfo.promise.Maybe) FormTree(org.activityinfo.model.formTree.FormTree) ResourceId(org.activityinfo.model.resource.ResourceId) FieldValue(org.activityinfo.model.type.FieldValue) FormInstance(org.activityinfo.model.form.FormInstance)

Example 44 with FormInstance

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

the class FormInputViewModelBuilder method computeUpdatedRecord.

/**
 * Computes the effective record, based
 * @param existingValues
 * @param inputModel
 * @return
 */
private FormInstance computeUpdatedRecord(Map<ResourceId, FieldValue> existingValues, FormInputModel inputModel) {
    // We inherit all the existing values...
    FormInstance record = new FormInstance(ResourceId.generateId(), formTree.getRootFormId());
    record.setAll(existingValues);
    // Now apply changes...
    for (FormTree.Node node : formTree.getRootFields()) {
        FieldInput fieldInput = inputModel.get(node.getFieldId());
        switch(fieldInput.getState()) {
            case UNTOUCHED:
                // No changes
                break;
            case EMPTY:
                record.set(node.getFieldId(), (FieldValue) null);
                break;
            case VALID:
                record.set(node.getFieldId(), fieldInput.getValue());
                break;
            case INVALID:
                LOGGER.info("Field with invalid input = " + node.getFieldId());
                record.set(node.getFieldId(), (FieldValue) null);
                break;
        }
    }
    LOGGER.info("fieldValues = " + record.getFieldValueMap());
    return record;
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) FieldInput(org.activityinfo.ui.client.input.model.FieldInput) FormInstance(org.activityinfo.model.form.FormInstance)

Example 45 with FormInstance

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

the class FormStoreTest method serialNumberOffline.

@Test
public void serialNumberOffline() {
    TestSetup setup = new TestSetup();
    IntakeForm intakeForm = setup.getCatalog().getIntakeForm();
    // Synchronize the intake form
    setup.setConnected(true);
    setup.getFormStore().setFormOffline(intakeForm.getFormId(), true);
    setup.runScheduled();
    // Go offline
    setup.setConnected(false);
    // Create a new intake record
    FormInstance newRecord = intakeForm.getGenerator().get();
    newRecord.set(intakeForm.getProtectionCodeFieldId(), (FieldValue) null);
    Promise<Void> update = setup.getFormStore().updateRecords(new RecordTransactionBuilder().create(newRecord).build());
    assertThat(update.getState(), equalTo(Promise.State.FULFILLED));
    // Verify that we can read the new record offline
    QueryModel queryModel = new QueryModel(intakeForm.getFormId());
    queryModel.selectResourceId().as("id");
    queryModel.selectField(intakeForm.getProtectionCodeFieldId()).as("serial");
    queryModel.setFilter(Formulas.equals(new SymbolNode("_id"), new ConstantNode(newRecord.getId().asString())));
    Connection<ColumnSet> view = setup.connect(setup.getFormStore().query(queryModel));
    assertThat(view.assertLoaded().getNumRows(), equalTo(1));
    assertThat(view.assertLoaded().getColumnView("serial").getString(0), nullValue());
    // Now go online...
    setup.setConnected(true);
    setup.getOfflineStore().syncChanges();
    ;
    setup.runScheduled();
    // Check that the serial number has been updated with the value from the server
    assertThat(view.assertLoaded().getColumnView("serial").getString(0), not(nullValue()));
}
Also used : RecordTransactionBuilder(org.activityinfo.model.resource.RecordTransactionBuilder) SymbolNode(org.activityinfo.model.formula.SymbolNode) ConstantNode(org.activityinfo.model.formula.ConstantNode) ColumnSet(org.activityinfo.model.query.ColumnSet) FormInstance(org.activityinfo.model.form.FormInstance) QueryModel(org.activityinfo.model.query.QueryModel) 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