Search in sources :

Example 1 with RecordUpdate

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()));
}
Also used : RecordUpdate(org.activityinfo.model.resource.RecordUpdate) RecordRef(org.activityinfo.model.type.RecordRef) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) FieldInput(org.activityinfo.ui.client.input.model.FieldInput) RecordTransaction(org.activityinfo.model.resource.RecordTransaction) Test(org.junit.Test)

Example 2 with RecordUpdate

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;
}
Also used : RecordUpdate(org.activityinfo.model.resource.RecordUpdate)

Example 3 with RecordUpdate

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;
}
Also used : RecordUpdate(org.activityinfo.model.resource.RecordUpdate) Date(java.util.Date) JsOverlay(jsinterop.annotations.JsOverlay)

Example 4 with RecordUpdate

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");
    }
}
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 5 with RecordUpdate

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;
    }
}
Also used : RecordUpdate(org.activityinfo.model.resource.RecordUpdate) JsonMappingException(org.activityinfo.json.JsonMappingException)

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