Search in sources :

Example 1 with RecordTransactionBuilder

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

the class ImportView method runUpdate.

/**
 * Based on the users explict choices and the automatic matching / mapping,
 * build a transaction to effect the import.
 * @param client
 */
public void runUpdate(GeoAdminClient client) {
    RecordTransactionBuilder tx = new RecordTransactionBuilder();
    ResourceId targetFormId = model.getTargetFormId().get();
    KeyGenerator generator = new KeyGenerator();
    Map<ResourceId, ResourceId> idMap = new HashMap<>();
    MatchTable matchTable = getMatchTable();
    int numRows = matchTable.getRowCount();
    for (int i = 0; i < numRows; i++) {
        MatchRow matchRow = matchTable.get(i);
        if (!matchRow.isMatched(MatchSide.SOURCE)) {
            // no corresponding row in the source:
            // delete unmatched target
            tx.delete(targetFormId, matchRow.getTargetId().get());
        } else {
            RecordUpdate update;
            ResourceId targetId;
            if (matchRow.isMatched(MatchSide.TARGET)) {
                // update target with properties from the source
                targetId = matchRow.getTargetId().get();
                update = tx.update(targetFormId, targetId);
            } else {
                // create a new instance with properties from the source
                targetId = CuidAdapter.entity(generator.generateInt());
                update = tx.create(targetFormId, targetId);
            }
            idMap.put(matchRow.getSourceId().get(), targetId);
            // apply properties from field mapping
            for (FieldMapping fieldMapping : mapping.get().getFieldMappings()) {
                update.setFieldValue(fieldMapping.getTargetFieldId(), fieldMapping.mapFieldValue(matchRow.getSourceRow()));
            }
        }
    }
    client.executeTransaction(tx);
    try {
        updateGeometry(client, idMap);
    } catch (IOException e) {
        throw new RuntimeException("Exception updating geometry");
    }
}
Also used : RecordTransactionBuilder(org.activityinfo.model.resource.RecordTransactionBuilder) RecordUpdate(org.activityinfo.model.resource.RecordUpdate) ResourceId(org.activityinfo.model.resource.ResourceId) HashMap(java.util.HashMap) FieldMapping(org.activityinfo.geoadmin.merge2.view.mapping.FieldMapping) IOException(java.io.IOException) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator)

Example 2 with RecordTransactionBuilder

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

the class SurveyTest method updateTransaction.

@Test
public void updateTransaction() {
    RecordTransactionBuilder tx = new RecordTransactionBuilder();
    tx.create(survey.getGenerator().get());
    tx.create(survey.getGenerator().get());
    client.update(tx.build());
    QueryModel queryModel = new QueryModel(survey.getFormId());
    queryModel.selectField(survey.getGenderFieldId()).as("gender");
    ColumnSet columnSet = client.queryTable(queryModel);
    assertThat(columnSet.getNumRows(), equalTo(2));
}
Also used : RecordTransactionBuilder(org.activityinfo.model.resource.RecordTransactionBuilder) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) Test(org.junit.Test)

Example 3 with RecordTransactionBuilder

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

the class SubFormDeletionTest method delete.

private void delete(ResourceId formId, String recordId) {
    RecordTransactionBuilder tx = new RecordTransactionBuilder();
    tx.delete(formId, ResourceId.valueOf(recordId));
    testingCatalog.updateRecords(tx.build());
}
Also used : RecordTransactionBuilder(org.activityinfo.model.resource.RecordTransactionBuilder)

Example 4 with RecordTransactionBuilder

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

the class Updater method execute.

public void execute(FormInstance formInstance) {
    RecordTransaction tx = new RecordTransactionBuilder().create(formInstance).build();
    execute(tx);
}
Also used : RecordTransactionBuilder(org.activityinfo.model.resource.RecordTransactionBuilder) RecordTransaction(org.activityinfo.model.resource.RecordTransaction)

Example 5 with RecordTransactionBuilder

use of org.activityinfo.model.resource.RecordTransactionBuilder 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)

Aggregations

RecordTransactionBuilder (org.activityinfo.model.resource.RecordTransactionBuilder)8 QueryModel (org.activityinfo.model.query.QueryModel)4 Test (org.junit.Test)4 ColumnSet (org.activityinfo.model.query.ColumnSet)3 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)2 RecordUpdate (org.activityinfo.model.resource.RecordUpdate)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 FieldMapping (org.activityinfo.geoadmin.merge2.view.mapping.FieldMapping)1 IDBFactoryStub (org.activityinfo.indexedb.IDBFactoryStub)1 FormClass (org.activityinfo.model.form.FormClass)1 FormField (org.activityinfo.model.form.FormField)1 FormInstance (org.activityinfo.model.form.FormInstance)1 ConstantNode (org.activityinfo.model.formula.ConstantNode)1 SymbolNode (org.activityinfo.model.formula.SymbolNode)1 ColumnView (org.activityinfo.model.query.ColumnView)1 RecordTransaction (org.activityinfo.model.resource.RecordTransaction)1 ResourceId (org.activityinfo.model.resource.ResourceId)1 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)1 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)1