Search in sources :

Example 11 with Transaction

use of org.geotools.data.Transaction 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

Transaction (org.geotools.data.Transaction)11 DefaultTransaction (org.geotools.data.DefaultTransaction)9 SimpleFeature (org.opengis.feature.simple.SimpleFeature)8 Test (org.junit.Test)7 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)7 FeatureId (org.opengis.filter.identity.FeatureId)6 ObjectId (org.locationtech.geogig.api.ObjectId)5 ResourceId (org.opengis.filter.identity.ResourceId)3 Function (com.google.common.base.Function)2 Optional (com.google.common.base.Optional)2 IOException (java.io.IOException)2 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)2 BaseFeatureCollection (org.geotools.feature.collection.BaseFeatureCollection)2 DelegateFeatureIterator (org.geotools.feature.collection.DelegateFeatureIterator)2 NodeRef (org.locationtech.geogig.api.NodeRef)2 ProgressListener (org.locationtech.geogig.api.ProgressListener)2 RevFeature (org.locationtech.geogig.api.RevFeature)2 RevTree (org.locationtech.geogig.api.RevTree)2 CommitOp (org.locationtech.geogig.api.porcelain.CommitOp)2 Feature (org.opengis.feature.Feature)2