Search in sources :

Example 11 with DefaultTransaction

use of org.geotools.data.DefaultTransaction in project GeoGig by boundlessgeo.

the class GeoGigDataStoreTest method testFeatureWriterAppend.

@Test
public void testFeatureWriterAppend() throws Exception {
    dataStore.createSchema(linesType);
    Transaction tx = new DefaultTransaction();
    FeatureWriter<SimpleFeatureType, SimpleFeature> fw = dataStore.getFeatureWriterAppend(linesTypeName.getLocalPart(), tx);
    LineString line = new GeometryBuilder().lineString(0, 0, 1, 1);
    SimpleFeature f = (SimpleFeature) fw.next();
    f.setAttribute("sp", "foo");
    f.setAttribute("ip", 10);
    f.setAttribute("pp", line);
    fw.write();
    fw.close();
    tx.commit();
    FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(linesTypeName);
    assertEquals(1, source.getCount(null));
    FeatureReader<SimpleFeatureType, SimpleFeature> r = dataStore.getFeatureReader(new Query(linesTypeName.getLocalPart()), Transaction.AUTO_COMMIT);
    assertTrue(r.hasNext());
    f = r.next();
    assertEquals("foo", f.getAttribute("sp"));
    assertEquals(10, f.getAttribute("ip"));
    assertTrue(line.equals((Geometry) f.getAttribute("pp")));
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Query(org.geotools.data.Query) LineString(com.vividsolutions.jts.geom.LineString) GeometryBuilder(org.geotools.geometry.jts.GeometryBuilder) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Test(org.junit.Test)

Example 12 with DefaultTransaction

use of org.geotools.data.DefaultTransaction 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 13 with DefaultTransaction

use of org.geotools.data.DefaultTransaction in project GeoGig by boundlessgeo.

the class GeoGigFeatureStoreTest method testTransactionCommitAuthorAndEmail.

@Test
public void testTransactionCommitAuthorAndEmail() throws Exception {
    FeatureCollection<SimpleFeatureType, SimpleFeature> collection;
    collection = DataUtilities.collection(Arrays.asList((SimpleFeature) points1, (SimpleFeature) points2, (SimpleFeature) points3));
    DefaultTransaction tx = new DefaultTransaction();
    points.setTransaction(tx);
    assertSame(tx, points.getTransaction());
    try {
        points.addFeatures(collection);
        tx.putProperty(GeogigTransactionState.VERSIONING_COMMIT_AUTHOR, "john");
        tx.putProperty(GeogigTransactionState.VERSIONING_COMMIT_MESSAGE, "test message");
        tx.putProperty("fullname", "John Doe");
        tx.putProperty("email", "jd@example.com");
        tx.commit();
        assertEquals(3, dataStore.getFeatureSource(pointsTypeName).getFeatures().size());
    } catch (Exception e) {
        tx.rollback();
        throw e;
    } finally {
        tx.close();
    }
    List<RevCommit> commits = toList(geogig.command(LogOp.class).call());
    assertFalse(commits.isEmpty());
    assertTrue(commits.get(0).getAuthor().getName().isPresent());
    assertEquals("John Doe", commits.get(0).getAuthor().getName().orNull());
    assertEquals("jd@example.com", commits.get(0).getAuthor().getEmail().orNull());
}
Also used : SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) SimpleFeature(org.opengis.feature.simple.SimpleFeature) DefaultTransaction(org.geotools.data.DefaultTransaction) RevCommit(org.locationtech.geogig.api.RevCommit) Test(org.junit.Test)

Example 14 with DefaultTransaction

use of org.geotools.data.DefaultTransaction in project GeoGig by boundlessgeo.

the class GeoGigFeatureStoreTest method testAddFeaturesWhileNotOnABranch.

@Test
public void testAddFeaturesWhileNotOnABranch() throws Exception {
    boolean gotIllegalStateException = false;
    final ObjectId head = geogig.command(RevParse.class).setRefSpec("HEAD").call().get();
    dataStore.setHead(head.toString());
    FeatureCollection<SimpleFeatureType, SimpleFeature> collection;
    collection = DataUtilities.collection(Arrays.asList((SimpleFeature) points1, (SimpleFeature) points2, (SimpleFeature) points3));
    Transaction tx = new DefaultTransaction();
    points.setTransaction(tx);
    assertSame(tx, points.getTransaction());
    try {
        List<FeatureId> addedFeatures = points.addFeatures(collection);
        assertNotNull(addedFeatures);
        assertEquals(3, addedFeatures.size());
        // assert transaction isolation
        assertEquals(3, points.getFeatures().size());
        assertEquals(0, dataStore.getFeatureSource(pointsTypeName).getFeatures().size());
        tx.commit();
        assertEquals(3, dataStore.getFeatureSource(pointsTypeName).getFeatures().size());
    } catch (IllegalStateException e) {
        tx.rollback();
        gotIllegalStateException = true;
    } catch (Exception e) {
        tx.rollback();
        throw e;
    } finally {
        tx.close();
    }
    assertTrue("Should throw IllegalStateException when trying to modify data in geogig datastore when it is not configured with a branch.", gotIllegalStateException);
}
Also used : FeatureId(org.opengis.filter.identity.FeatureId) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) ObjectId(org.locationtech.geogig.api.ObjectId) RevParse(org.locationtech.geogig.api.plumbing.RevParse) SimpleFeature(org.opengis.feature.simple.SimpleFeature) DefaultTransaction(org.geotools.data.DefaultTransaction) Test(org.junit.Test)

Example 15 with DefaultTransaction

use of org.geotools.data.DefaultTransaction in project GeoGig by boundlessgeo.

the class ExportOp method _call.

/**
     * Executes the export operation using the parameters that have been specified.
     * 
     * @return a FeatureCollection with the specified features
     */
@Override
protected SimpleFeatureStore _call() {
    final StagingDatabase database = stagingDatabase();
    if (filterFeatureTypeId != null) {
        RevObject filterType = database.getIfPresent(filterFeatureTypeId);
        checkArgument(filterType instanceof RevFeatureType, "Provided filter feature type is does not exist");
    }
    final SimpleFeatureStore targetStore = getTargetStore();
    final String refspec = resolveRefSpec();
    final String treePath = refspec.substring(refspec.indexOf(':') + 1);
    final RevTree rootTree = resolveRootTree(refspec);
    final NodeRef typeTreeRef = resolTypeTreeRef(refspec, treePath, rootTree);
    final ObjectId defaultMetadataId = typeTreeRef.getMetadataId();
    final RevTree typeTree = database.getTree(typeTreeRef.objectId());
    final ProgressListener progressListener = getProgressListener();
    progressListener.started();
    progressListener.setDescription("Exporting from " + path + " to " + targetStore.getName().getLocalPart() + "... ");
    FeatureCollection<SimpleFeatureType, SimpleFeature> asFeatureCollection = new BaseFeatureCollection<SimpleFeatureType, SimpleFeature>() {

        @Override
        public FeatureIterator<SimpleFeature> features() {
            final Iterator<SimpleFeature> plainFeatures = getFeatures(typeTree, database, defaultMetadataId, progressListener);
            Iterator<SimpleFeature> adaptedFeatures = adaptToArguments(plainFeatures, defaultMetadataId);
            Iterator<Optional<Feature>> transformed = Iterators.transform(adaptedFeatures, ExportOp.this.function);
            Iterator<SimpleFeature> filtered = Iterators.filter(Iterators.transform(transformed, new Function<Optional<Feature>, SimpleFeature>() {

                @Override
                public SimpleFeature apply(Optional<Feature> input) {
                    return (SimpleFeature) (input.isPresent() ? input.get() : null);
                }
            }), Predicates.notNull());
            return new DelegateFeatureIterator<SimpleFeature>(filtered);
        }
    };
    // add the feature collection to the feature store
    final Transaction transaction;
    if (transactional) {
        transaction = new DefaultTransaction("create");
    } else {
        transaction = Transaction.AUTO_COMMIT;
    }
    try {
        targetStore.setTransaction(transaction);
        try {
            targetStore.addFeatures(asFeatureCollection);
            transaction.commit();
        } catch (final Exception e) {
            if (transactional) {
                transaction.rollback();
            }
            Throwables.propagateIfInstanceOf(e, GeoToolsOpException.class);
            throw new GeoToolsOpException(e, StatusCode.UNABLE_TO_ADD);
        } finally {
            transaction.close();
        }
    } catch (IOException e) {
        throw new GeoToolsOpException(e, StatusCode.UNABLE_TO_ADD);
    }
    progressListener.complete();
    return targetStore;
}
Also used : DelegateFeatureIterator(org.geotools.feature.collection.DelegateFeatureIterator) Optional(com.google.common.base.Optional) RevObject(org.locationtech.geogig.api.RevObject) ObjectId(org.locationtech.geogig.api.ObjectId) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) RevFeature(org.locationtech.geogig.api.RevFeature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) DefaultTransaction(org.geotools.data.DefaultTransaction) IOException(java.io.IOException) NodeRef(org.locationtech.geogig.api.NodeRef) Function(com.google.common.base.Function) ProgressListener(org.locationtech.geogig.api.ProgressListener) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) BaseFeatureCollection(org.geotools.feature.collection.BaseFeatureCollection) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevTree(org.locationtech.geogig.api.RevTree) StagingDatabase(org.locationtech.geogig.storage.StagingDatabase)

Aggregations

DefaultTransaction (org.geotools.data.DefaultTransaction)20 Transaction (org.geotools.data.Transaction)17 SimpleFeature (org.opengis.feature.simple.SimpleFeature)15 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)14 Test (org.junit.Test)12 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)9 ShapefileDataStore (org.geotools.data.shapefile.ShapefileDataStore)6 FeatureId (org.opengis.filter.identity.FeatureId)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Query (org.geotools.data.Query)5 ObjectId (org.locationtech.geogig.api.ObjectId)5 IOException (java.io.IOException)4 FileDataStoreFactorySpi (org.geotools.data.FileDataStoreFactorySpi)4 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)4 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)4 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)4 AttributeDescriptor (org.opengis.feature.type.AttributeDescriptor)4 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)3 Ignore (org.junit.Ignore)3