Search in sources :

Example 16 with RecordUpdate

use of org.activityinfo.model.resource.RecordUpdate 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 17 with RecordUpdate

use of org.activityinfo.model.resource.RecordUpdate in project activityinfo by bedatadriven.

the class MySqlUpdateTest method createFormWithCalculationAndRelevance.

@Test
public void createFormWithCalculationAndRelevance() {
    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 numberField = new FormField(CuidAdapter.generateIndicatorId()).setType(new QuantityType("widgets")).setLabel("NUM").setRequired(true);
    formClass.addElement(numberField);
    FormField calculatedField = new FormField(CuidAdapter.generateIndicatorId()).setType(new CalculatedFieldType("1")).setLabel("Calculation").setRelevanceConditionExpression("NUM>42").setRequired(true);
    formClass.addElement(calculatedField);
    catalog.createOrUpdateFormSchema(formClass);
    newRequest();
    // Create two records
    RecordTransactionBuilder tx = new RecordTransactionBuilder();
    RecordUpdate site1 = tx.update(formClass.getId(), CuidAdapter.generateSiteCuid());
    site1.setFieldValue(numberField.getId(), new Quantity(10));
    site1.setFieldValue(partnerField(activityId), CuidAdapter.partnerRef(1, 1));
    RecordUpdate site2 = tx.update(formClass.getId(), CuidAdapter.generateSiteCuid());
    site2.setFieldValue(numberField.getId(), new Quantity(60));
    site2.setFieldValue(partnerField(activityId), CuidAdapter.partnerRef(1, 1));
    updater().execute(tx.build());
    newRequest();
    // Query results
    QueryModel queryModel = new QueryModel(formClass.getId());
    queryModel.selectResourceId();
    queryModel.selectExpr("Num").as("num");
    queryModel.selectExpr("Calculation").as("calc");
    query(queryModel);
    ColumnView num = columnSet.getColumnView("num");
    ColumnView calculation = columnSet.getColumnView("calc");
    assertThat(calculation.isMissing(0), equalTo(num.getDouble(0) <= 42));
    assertThat(calculation.isMissing(1), equalTo(num.getDouble(1) <= 42));
}
Also used : RecordTransactionBuilder(org.activityinfo.model.resource.RecordTransactionBuilder) CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) RecordUpdate(org.activityinfo.model.resource.RecordUpdate) TypedRecordUpdate(org.activityinfo.store.spi.TypedRecordUpdate) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) ColumnView(org.activityinfo.model.query.ColumnView) Quantity(org.activityinfo.model.type.number.Quantity) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) FormField(org.activityinfo.model.form.FormField) QueryModel(org.activityinfo.model.query.QueryModel) GeoPoint(org.activityinfo.model.type.geo.GeoPoint) Test(org.junit.Test)

Example 18 with RecordUpdate

use of org.activityinfo.model.resource.RecordUpdate in project activityinfo by bedatadriven.

the class MySqlUpdateTest method deleteSite.

@Test
public void deleteSite() {
    RecordUpdate changeObject = new RecordUpdate();
    changeObject.setRecordId("s0000000001");
    changeObject.setFormId(activityFormClass(1).asString());
    changeObject.setDeleted(true);
    Updater updater = updater();
    updater.executeChange(changeObject);
    newRequest();
    query(activityFormClass(1), "_id");
    assertThat(column("_id"), hasValues("s0000000002", "s0000000003"));
}
Also used : RecordUpdate(org.activityinfo.model.resource.RecordUpdate) TypedRecordUpdate(org.activityinfo.store.spi.TypedRecordUpdate) Updater(org.activityinfo.store.query.server.Updater) Test(org.junit.Test)

Example 19 with RecordUpdate

use of org.activityinfo.model.resource.RecordUpdate in project activityinfo by bedatadriven.

the class FormInputViewModel method buildUpdate.

public RecordUpdate buildUpdate(Optional<RecordRef> parentRef) {
    RecordUpdate update = new RecordUpdate();
    update.setRecordId(inputModel.getRecordRef().getRecordId());
    update.setFormId(inputModel.getRecordRef().getFormId());
    if (parentRef.isPresent()) {
        update.setParentRecordId(parentRef.get().getRecordId().asString());
    }
    for (FormTree.Node node : formTree.getRootFields()) {
        FieldInput newInput = inputModel.get(node.getFieldId());
        if (newInput.getState() == FieldInput.State.VALID) {
            update.setFieldValue(node.getFieldId(), newInput.getValue());
        } else if (existingValues.containsKey(node.getFieldId()) && newInput.getState() == FieldInput.State.EMPTY) {
            update.setFieldValue(node.getFieldId().asString(), Json.createNull());
        } else if (existingValues.containsKey(node.getFieldId()) && !relevant.contains(node.getFieldId())) {
            update.setFieldValue(node.getFieldId().asString(), Json.createNull());
        }
    }
    return update;
}
Also used : RecordUpdate(org.activityinfo.model.resource.RecordUpdate) FormTree(org.activityinfo.model.formTree.FormTree) FieldInput(org.activityinfo.ui.client.input.model.FieldInput)

Example 20 with RecordUpdate

use of org.activityinfo.model.resource.RecordUpdate in project activityinfo by bedatadriven.

the class SubFormViewModel method buildUpdates.

public List<RecordUpdate> buildUpdates(RecordRef parentRef) {
    List<RecordUpdate> updates = new ArrayList<>();
    for (FormInputViewModel subRecord : subRecords) {
        if (!subRecord.isPlaceholder()) {
            updates.add(subRecord.buildUpdate(Optional.of(parentRef)));
        }
    }
    for (RecordRef deletedRecord : deletedRecords) {
        RecordUpdate update = new RecordUpdate();
        update.setFormId(deletedRecord.getFormId());
        update.setRecordId(deletedRecord.getRecordId());
        update.setDeleted(true);
        updates.add(update);
    }
    return updates;
}
Also used : RecordUpdate(org.activityinfo.model.resource.RecordUpdate) ArrayList(java.util.ArrayList) RecordRef(org.activityinfo.model.type.RecordRef)

Aggregations

RecordUpdate (org.activityinfo.model.resource.RecordUpdate)21 Test (org.junit.Test)14 TypedRecordUpdate (org.activityinfo.store.spi.TypedRecordUpdate)12 Updater (org.activityinfo.store.query.server.Updater)11 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)3 ResourceId (org.activityinfo.model.resource.ResourceId)3 RecordRef (org.activityinfo.model.type.RecordRef)3 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)3 HashMap (java.util.HashMap)2 FormInstance (org.activityinfo.model.form.FormInstance)2 RecordTransaction (org.activityinfo.model.resource.RecordTransaction)2 RecordTransactionBuilder (org.activityinfo.model.resource.RecordTransactionBuilder)2 FieldInput (org.activityinfo.ui.client.input.model.FieldInput)2 FormInputModel (org.activityinfo.ui.client.input.model.FormInputModel)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Map (java.util.Map)1 JsOverlay (jsinterop.annotations.JsOverlay)1 FieldMapping (org.activityinfo.geoadmin.merge2.view.mapping.FieldMapping)1