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")));
}
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());
}
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());
}
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);
}
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;
}
Aggregations