use of org.activityinfo.model.resource.RecordUpdate in project activityinfo by bedatadriven.
the class FormInputViewModelTest method testSubFormInput.
@Test
public void testSubFormInput() {
FormInputViewModelBuilder builder = builderFor(setup.getCatalog().getIncidentForm());
// Start with empty input
FormInputModel inputModel = new FormInputModel(new RecordRef(IncidentForm.FORM_ID, ResourceId.generateId()));
// Should see one (empty) sub form record
FormInputViewModel viewModel = builder.build(inputModel);
SubFormViewModel referralSubForm = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
assertThat(referralSubForm.getSubRecords(), hasSize(1));
// We can update this sub record
FormInputViewModel subRecord = referralSubForm.getSubRecords().get(0);
inputModel = inputModel.update(subRecord.getRecordRef(), ReferralSubForm.ORGANIZATION_FIELD_ID, new FieldInput(TextValue.valueOf("CRS")));
viewModel = builder.build(inputModel);
referralSubForm = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
assertThat(referralSubForm.getSubRecords(), hasSize(1));
// Now add a second record
inputModel = inputModel.addSubRecord(new RecordRef(ReferralSubForm.FORM_ID, ResourceId.generateId()));
viewModel = builder.build(inputModel);
referralSubForm = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
assertThat(referralSubForm.getSubRecords(), hasSize(2));
// Verify that the transaction is built is correctly
RecordTransaction tx = viewModel.buildTransaction();
RecordUpdate[] changes = tx.getChangeArray();
assertThat(changes.length, equalTo(3));
RecordUpdate parentChange = changes[0];
RecordUpdate subFormChange = changes[1];
assertThat(parentChange.getRecordRef(), equalTo(inputModel.getRecordRef()));
assertThat(subFormChange.getParentRecordId(), equalTo(parentChange.getRecordId().asString()));
}
use of org.activityinfo.model.resource.RecordUpdate in project activityinfo by bedatadriven.
the class OfflineStore method inverse.
private RecordUpdate inverse(Optional<RecordObject> existingRecord, RecordUpdate update) {
RecordUpdate inverse = new RecordUpdate();
inverse.setFormId(update.getFormId());
inverse.setRecordId(update.getRecordId());
if (existingRecord.isPresent()) {
if (update.isDeleted()) {
// Restore the old values
inverse.setFields(existingRecord.get().getFields());
} else {
// Remember only the changed the old values so we can roll them back
for (String updatedField : update.getFields().keys()) {
inverse.setFieldValue(updatedField, existingRecord.get().getField(updatedField));
}
}
} else {
inverse.setDeleted(true);
}
return inverse;
}
use of org.activityinfo.model.resource.RecordUpdate in project activityinfo by bedatadriven.
the class PendingTransaction method create.
@JsOverlay
public static PendingTransaction create(RecordTransaction transaction, List<RecordUpdate> rollbacks) {
PendingTransaction p = new PendingTransaction();
p.id = transaction.getId();
p.status = READY;
p.time = new Date();
p.transaction = transaction;
p.rollbacks = rollbacks.toArray(new RecordUpdate[rollbacks.size()]);
return p;
}
use of org.activityinfo.model.resource.RecordUpdate 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.RecordUpdate in project activityinfo by bedatadriven.
the class Updater method execute.
public void execute(RecordTransaction tx) {
TransactionalStorageProvider txStorageProvider = (TransactionalStorageProvider) catalog;
txStorageProvider.begin();
try {
for (RecordUpdate change : tx.getChanges()) {
executeChange(change);
}
txStorageProvider.commit();
} catch (Exception e) {
txStorageProvider.rollback();
throw e;
}
}
Aggregations