use of org.geotoolkit.storage.feature.FeatureWriter in project geotoolkit by Geomatys.
the class DefaultJDBCFeatureStore method getFeatureWriterInternal.
private FeatureWriter getFeatureWriterInternal(final String typeName, Filter baseFilter, final EditMode mode, Connection cnx, final Hints hints) throws DataStoreException, IOException {
if (!isWritable(typeName)) {
throw new DataStoreException("Type " + typeName + " is not writeable.");
}
final FeatureType baseType = getFeatureType(typeName);
final PrimaryKey pkey = dbmodel.getPrimaryKey(typeName);
// replace any PropertyEqualsTo in true ID filters
baseFilter = (Filter) FIDFixVisitor.INSTANCE.visit(baseFilter);
// split the filter between what can be send and must be handle by code
final Filter[] divided = getDialect().splitFilter(baseFilter, baseType);
Filter preFilter = divided[0];
Filter postFilter = divided[1];
// ensure spatial filters are in featuretype geometry crs
preFilter = (Filter) new CRSAdaptorVisitor(baseType).visit(preFilter);
// we gave him the connection, he must not release it
final boolean release = (cnx == null);
if (cnx == null) {
try {
cnx = getDataSource().getConnection();
} catch (SQLException ex) {
throw new DataStoreException(ex.getMessage(), ex);
}
}
FeatureWriter writer;
try {
// check for insert only
if (EditMode.INSERT == mode) {
// build up a statement for the content, inserting only so we dont want
// the query to return any data ==> Filter.exclude()
final org.geotoolkit.storage.feature.query.Query queryNone = org.geotoolkit.storage.feature.query.Query.filtered(typeName, Filter.exclude());
final String sql = getQueryBuilder().selectSQL(baseType, queryNone);
getLogger().fine(sql);
return new JDBCFeatureWriterInsert(this, sql, baseType, cnx, release, hints);
}
// build up a statement for the content
final org.geotoolkit.storage.feature.query.Query preQuery = org.geotoolkit.storage.feature.query.Query.filtered(typeName, preFilter);
final String sql = getQueryBuilder().selectSQL(baseType, preQuery);
getLogger().fine(sql);
if (EditMode.UPDATE == mode) {
writer = new JDBCFeatureWriterUpdate(this, sql, baseType, cnx, release, hints);
} else {
// update insert case
writer = new JDBCFeatureWriterUpdateInsert(this, sql, baseType, cnx, release, hints);
}
} catch (SQLException e) {
// close the connection
JDBCFeatureStoreUtilities.closeSafe(getLogger(), (release) ? cnx : null);
// now we can safely rethrow the exception
throw new DataStoreException(e);
}
// check for post filter and wrap accordingly
if (postFilter != null && postFilter != Filter.include()) {
writer = FeatureStreams.filter(writer, postFilter);
}
return writer;
}
use of org.geotoolkit.storage.feature.FeatureWriter in project geotoolkit by Geomatys.
the class IndexedShapefileDataStoreTest method testRemoveFromBackAndClose.
/**
* Create a test file, then continue removing the last entry until there are
* no features left.
*/
@Test
public void testRemoveFromBackAndClose() throws Throwable {
IndexedShapefileFeatureStore sds = createDataStore();
int idx = loadFeatures(sds).size();
while (idx > 0) {
FeatureWriter writer = null;
try {
writer = sds.getFeatureWriter(new Query(sds.getName()));
while (writer.hasNext()) {
writer.next();
}
writer.remove();
} finally {
if (writer != null) {
writer.close();
writer = null;
}
}
assertEquals(--idx, loadFeatures(sds).size());
}
sds.close();
}
use of org.geotoolkit.storage.feature.FeatureWriter in project geotoolkit by Geomatys.
the class FidQueryTest method testAddFeature.
@Test
@Ignore
public void testAddFeature() throws Exception {
final FeatureType schema = ds.getFeatureType(ds.getName().toString());
final Feature newFeature = schema.newInstance();
final GeometryFactory gf = org.geotoolkit.geometry.jts.JTS.getFactory();
newFeature.setPropertyValue("a", gf.createPoint((new Coordinate(0, 0))));
newFeature.setPropertyValue("b", new Long(0));
newFeature.setPropertyValue("c", new Long(0));
newFeature.setPropertyValue("d", "Hey");
final Collection<Feature> collection = new ArrayList<>();
collection.add(newFeature);
final List<ResourceId> newFids;
try (FeatureWriter writer = ds.getFeatureWriter(Query.filtered(name.toString(), Filter.exclude()))) {
newFids = FeatureStoreUtilities.write(writer, collection);
}
assertEquals(1, newFids.size());
// this.assertFidsMatch();
final Filter filter = newFids.iterator().next();
final Query query = new Query();
query.setTypeName(schema.getName());
query.setSelection(filter);
final FeatureIterator features = ds.getFeatureReader(query);
try {
final Feature feature = features.next();
for (PropertyType desc : schema.getProperties(true)) {
final Object value = feature.getPropertyValue(desc.getName().toString());
final Object newValue = newFeature.getPropertyValue(desc.getName().toString());
if (value instanceof Geometry) {
assertTrue(((Geometry) newValue).equals((Geometry) value));
} else {
assertEquals(newValue, value);
}
}
assertFalse(features.hasNext());
} finally {
if (features != null)
features.close();
}
}
use of org.geotoolkit.storage.feature.FeatureWriter in project geotoolkit by Geomatys.
the class FeatureStreamsTest method testEmptyWriter.
@Test
public void testEmptyWriter() {
FeatureCollection collection = buildSimpleFeatureCollection();
final FeatureWriter iterator = FeatureStreams.emptyWriter(collection.getType());
assertEquals(iterator.getFeatureType(), collection.getType());
assertFalse(iterator.hasNext());
try {
iterator.next();
fail("Next on empty iterator should have raised a no such element exception.");
} catch (NoSuchElementException ex) {
// ok
}
try {
iterator.remove();
fail("Remove should have raise an error.");
} catch (Exception ex) {
// ok
}
iterator.close();
}
use of org.geotoolkit.storage.feature.FeatureWriter in project geotoolkit by Geomatys.
the class DefaultJDBCFeatureStore method handleUpdateWithFeatureWriter.
/**
* Convinient method to handle adding features operation by using the
* FeatureWriter.
*/
protected void handleUpdateWithFeatureWriter(final String groupName, final Filter filter, final Map<String, ? extends Object> values, Connection cnx) throws DataStoreException {
try (FeatureWriter writer = getFeatureWriterInternal(groupName, filter, EditMode.UPDATE, cnx, null)) {
while (writer.hasNext()) {
final Feature f = writer.next();
for (final Map.Entry<String, ? extends Object> entry : values.entrySet()) {
f.setPropertyValue(entry.getKey(), entry.getValue());
}
writer.write();
}
} catch (FeatureStoreRuntimeException | IOException ex) {
throw new DataStoreException(ex);
}
}
Aggregations