Search in sources :

Example 21 with ResourceId

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

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

the class FormMappingBuilder method buildReferenceMapping.

/**
 * Builds a reference mapping for a given target node.
 *
 * <p>A reference field takes the value of a single {@code ResourceId}, but
 * most of the time we don't have the actual id of the field in the dataset to import:
 * we have to obtain the id by performing a look up against text fields in the
 * </p>
 *
 * @param targetField the reference field to look up
 */
private void buildReferenceMapping(final FormField targetField) {
    // In order to match against the ReferenceField, we need the actual data
    // from the form that is being referenced.
    // 
    // Example: If we have a "Location" field that references the "Province" form class,
    // then we need then names and/or codes of the Province form class in order to lookup the
    // ids, assuming that our source dataset has a "province name" column with the names of the provinces.
    ReferenceType type = (ReferenceType) targetField.getType();
    // Currently this only supports reference fields that reference exactly one form class.
    if (type.getCardinality() == Cardinality.SINGLE && type.getRange().size() == 1) {
        ResourceId referenceFormId = Iterables.getOnlyElement(type.getRange());
        Observable<FormProfile> lookupForm = FormProfile.profile(resourceStore, referenceFormId);
        Observable<FieldMapping> mapping = lookupForm.transform(new Function<FormProfile, FieldMapping>() {

            @Override
            public FieldMapping apply(FormProfile lookupForm) {
                return new ReferenceFieldMapping(targetField, KeyFieldPairSet.matchKeys(source, lookupForm), referenceMatches);
            }
        });
        mappings.add(mapping);
    }
}
Also used : FormProfile(org.activityinfo.geoadmin.merge2.view.profile.FormProfile) ResourceId(org.activityinfo.model.resource.ResourceId) ReferenceType(org.activityinfo.model.type.ReferenceType)

Example 23 with ResourceId

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

the class ReferenceFieldMapping method mapFieldValue.

public FieldValue mapFieldValue(int sourceIndex) {
    int keyIndex = sourceKeySet.getKeyIndexOfSourceRow(sourceIndex);
    ResourceId targetId = lookupTable.get().getTargetMatchId(keyIndex);
    ResourceId targetFormId = Iterables.getOnlyElement(((ReferenceType) targetReferenceField.getType()).getRange());
    return new ReferenceValue(new RecordRef(targetFormId, targetId));
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) ReferenceValue(org.activityinfo.model.type.ReferenceValue) RecordRef(org.activityinfo.model.type.RecordRef)

Example 24 with ResourceId

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

the class FeatureQueryBuilder method execute.

@Override
public void execute() {
    SimpleFeatureIterator it;
    try {
        it = featureSource.getFeatures().features();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    while (it.hasNext()) {
        SimpleFeature feature = it.next();
        ResourceId id = ResourceId.valueOf(feature.getID());
        for (CursorObserver<ResourceId> idObserver : idObservers) {
            idObserver.onNext(id);
        }
        for (QueryField field : fields) {
            Object value = feature.getAttribute(field.attributeIndex);
            if (value == null) {
                field.observer.onNext(null);
            } else {
                field.observer.onNext(field.converter.apply(value));
            }
        }
    }
    for (CursorObserver<ResourceId> idObserver : idObservers) {
        idObserver.done();
    }
    for (QueryField field : fields) {
        field.observer.done();
    }
}
Also used : SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) ResourceId(org.activityinfo.model.resource.ResourceId) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature)

Example 25 with ResourceId

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

the class MatchTableTest method resolveMatch.

@Test
public void resolveMatch() {
    // Amoron'i mania
    ResourceId sourceId = ResourceId.valueOf("MDG_adm2.7");
    // Amoron i Mania
    ResourceId targetId = ResourceId.valueOf("z0000041751");
    assertThat(waitFor(matchTable.getUnresolvedCount()), equalTo(2));
    model.getInstanceMatchSet().add(new InstanceMatch(sourceId, targetId));
    assertThat(matchTable.getUnresolvedCount().get(), equalTo(1));
    dump(matchTable);
}
Also used : InstanceMatch(org.activityinfo.geoadmin.merge2.model.InstanceMatch) ResourceId(org.activityinfo.model.resource.ResourceId) Test(org.junit.Test)

Aggregations

ResourceId (org.activityinfo.model.resource.ResourceId)198 Test (org.junit.Test)42 FormClass (org.activityinfo.model.form.FormClass)41 FormField (org.activityinfo.model.form.FormField)34 RecordRef (org.activityinfo.model.type.RecordRef)30 JsonValue (org.activityinfo.json.JsonValue)21 FormTree (org.activityinfo.model.formTree.FormTree)18 ReferenceValue (org.activityinfo.model.type.ReferenceValue)17 FieldValue (org.activityinfo.model.type.FieldValue)16 FormInstance (org.activityinfo.model.form.FormInstance)15 HashMap (java.util.HashMap)14 QuantityType (org.activityinfo.model.type.number.QuantityType)13 ColumnSet (org.activityinfo.model.query.ColumnSet)12 QueryModel (org.activityinfo.model.query.QueryModel)12 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)11 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)11 ReferenceType (org.activityinfo.model.type.ReferenceType)10 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)10 FormStorage (org.activityinfo.store.spi.FormStorage)10 ArrayList (java.util.ArrayList)9