Search in sources :

Example 1 with Id

use of org.opengis.filter.Id in project GeoGig by boundlessgeo.

the class GeoGigFeatureStoreTest method testModifyFeatures.

@Test
public void testModifyFeatures() throws Exception {
    // add features circumventing FeatureStore.addFeatures to keep the test
    // independent of the addFeatures functionality
    insertAndAdd(lines1, lines2, lines3, points1, points2, points3);
    geogig.command(CommitOp.class).call();
    Id filter = ff.id(Collections.singleton(ff.featureId(idP1)));
    Transaction tx = new DefaultTransaction();
    points.setTransaction(tx);
    try {
        // initial value
        SimpleFeature initial = points.getFeatures(filter).features().next();
        assertEquals("StringProp1_1", initial.getAttribute("sp"));
        // modify
        points.modifyFeatures("sp", "modified", filter);
        // modified value before commit
        SimpleFeature modified = points.getFeatures(filter).features().next();
        assertEquals("modified", modified.getAttribute("sp"));
        // unmodified value before commit on another store instance (tx isolation)
        assertEquals("StringProp1_1", dataStore.getFeatureSource(pointsTypeName).getFeatures(filter).features().next().getAttribute("sp"));
        tx.commit();
        // modified value after commit on another store instance
        assertEquals("modified", dataStore.getFeatureSource(pointsTypeName).getFeatures(filter).features().next().getAttribute("sp"));
    } catch (Exception e) {
        tx.rollback();
        throw e;
    } finally {
        tx.close();
    }
    points.setTransaction(Transaction.AUTO_COMMIT);
    SimpleFeature modified = points.getFeatures(filter).features().next();
    assertEquals("modified", modified.getAttribute("sp"));
}
Also used : Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) ResourceId(org.opengis.filter.identity.ResourceId) ObjectId(org.locationtech.geogig.api.ObjectId) Id(org.opengis.filter.Id) FeatureId(org.opengis.filter.identity.FeatureId) CommitOp(org.locationtech.geogig.api.porcelain.CommitOp) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Test(org.junit.Test)

Example 2 with Id

use of org.opengis.filter.Id in project GeoGig by boundlessgeo.

the class GeoGigFeatureStoreTest method testRemoveFeatures.

@Test
public void testRemoveFeatures() throws Exception {
    // add features circumventing FeatureStore.addFeatures to keep the test
    // independent of the
    // addFeatures functionality
    insertAndAdd(lines1, lines2, lines3);
    insertAndAdd(points1, points2, points3);
    geogig.command(CommitOp.class).call();
    Id filter = ff.id(Collections.singleton(ff.featureId(idP1)));
    Transaction tx = new DefaultTransaction();
    points.setTransaction(tx);
    try {
        // initial # of features
        assertEquals(3, points.getFeatures().size());
        // remove feature
        points.removeFeatures(filter);
        // #of features before commit on the same store
        assertEquals(2, points.getFeatures().size());
        // #of features before commit on a different store instance
        assertEquals(3, dataStore.getFeatureSource(pointsTypeName).getFeatures().size());
        tx.commit();
        // #of features after commit on a different store instance
        assertEquals(2, dataStore.getFeatureSource(pointsTypeName).getFeatures().size());
    } catch (Exception e) {
        tx.rollback();
        throw e;
    } finally {
        tx.close();
    }
    points.setTransaction(Transaction.AUTO_COMMIT);
    assertEquals(2, points.getFeatures().size());
    assertEquals(0, points.getFeatures(filter).size());
}
Also used : Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) ResourceId(org.opengis.filter.identity.ResourceId) ObjectId(org.locationtech.geogig.api.ObjectId) Id(org.opengis.filter.Id) FeatureId(org.opengis.filter.identity.FeatureId) CommitOp(org.locationtech.geogig.api.porcelain.CommitOp) DefaultTransaction(org.geotools.data.DefaultTransaction) Test(org.junit.Test)

Example 3 with Id

use of org.opengis.filter.Id in project polymap4-core by Polymap4.

the class UnitOfWork method checkSubmitModified.

// public void revert( IProgressMonitor monitor ) {
// revert( Filter.INCLUDE, monitor );
// }
// 
// 
// /**
// *
// * @param filter Specifies what features to revert. null for all features.
// * @param monitor
// */
// public void revert( Filter filter, IProgressMonitor monitor ) {
// assert filter != null;
// monitor.beginTask( getLabel() + " revert", modified.size() );
// 
// List<Feature> reverted = new ArrayList( modified.size() );
// 
// for (FeatureBufferState buffered : modified.values()) {
// if (filter.evaluate( buffered.original() )) {
// buffer.unregisterFeatures( Collections.singletonList( buffered.feature() ) );
// reverted.add( buffered.feature() );
// }
// monitor.worked( 1 );
// }
// fireFeatureChangeEvent( FeatureChangeEvent.Type.FLUSHED, reverted );
// 
// monitor.done();
// }
/**
 * Check concurrent modifications with the store.
 */
protected void checkSubmitModified(FeatureBufferState buffered) throws ConcurrentModificationException, IOException {
    FeatureId fid = buffered.feature().getIdentifier();
    Id fidFilter = ff.id(Collections.singleton(fid));
    // check concurrent modification
    Feature[] stored = (Feature[]) fs.getFeatures(fidFilter).toArray(new Feature[1]);
    if (stored.length == 0) {
        throw new ConcurrentModificationException("Feature has been removed concurrently: " + fid);
    } else if (stored.length > 1) {
        throw new IllegalStateException("More than one feature for id: " + fid + "!?");
    }
    if (isFeatureModified(stored[0], buffered.original())) {
        throw new ConcurrentModificationException("Objekt wurde von einem anderen Nutzer gleichzeitig ge�ndert: " + fid);
    }
    // write down
    AttributeDescriptor[] type = {};
    Object[] value = {};
    for (Property origProp : buffered.original().getProperties()) {
        if (origProp.getDescriptor() instanceof AttributeDescriptor) {
            Property newProp = buffered.feature().getProperty(origProp.getName());
            if (isPropertyModified(origProp.getValue(), newProp.getValue())) {
                type = (AttributeDescriptor[]) ArrayUtils.add(type, origProp.getDescriptor());
                value = ArrayUtils.add(value, newProp.getValue());
                log.info("Attribute modified: " + origProp.getDescriptor().getName() + " = " + newProp.getValue() + " (" + fid.getID() + ")");
            }
        }
    }
    fs.modifyFeatures(type, value, fidFilter);
}
Also used : FeatureId(org.opengis.filter.identity.FeatureId) ConcurrentModificationException(java.util.ConcurrentModificationException) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) FeatureId(org.opengis.filter.identity.FeatureId) Id(org.opengis.filter.Id) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) Property(org.opengis.feature.Property)

Aggregations

Id (org.opengis.filter.Id)3 FeatureId (org.opengis.filter.identity.FeatureId)3 DefaultTransaction (org.geotools.data.DefaultTransaction)2 Transaction (org.geotools.data.Transaction)2 Test (org.junit.Test)2 ObjectId (org.locationtech.geogig.api.ObjectId)2 CommitOp (org.locationtech.geogig.api.porcelain.CommitOp)2 SimpleFeature (org.opengis.feature.simple.SimpleFeature)2 ResourceId (org.opengis.filter.identity.ResourceId)2 ConcurrentModificationException (java.util.ConcurrentModificationException)1 Feature (org.opengis.feature.Feature)1 Property (org.opengis.feature.Property)1 AttributeDescriptor (org.opengis.feature.type.AttributeDescriptor)1