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");
}
}
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));
}
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());
}
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);
}
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));
}
Aggregations