Search in sources :

Example 16 with Session

use of org.geotoolkit.storage.feature.session.Session in project geotoolkit by Geomatys.

the class PostgresComplexTypeTest method testComplex2Insert.

/**
 * 3 level depths feature test.
 */
@Test
public void testComplex2Insert() throws DataStoreException, VersioningException {
    reload(false);
    store.createFeatureType(FTYPE_COMPLEX2);
    final FeatureType soundingType = store.getFeatureType(store.getNames().iterator().next().toString());
    final FeatureType recordType = ((FeatureAssociationRole) soundingType.getProperty("records")).getValueType();
    final FeatureType dataType = ((FeatureAssociationRole) recordType.getProperty("datas")).getValueType();
    final Feature sounding = soundingType.newInstance();
    sounding.setPropertyValue("identifier", 120l);
    final Feature record1 = recordType.newInstance();
    record1.setPropertyValue("time", new Date(5000000));
    final Feature data11 = dataType.newInstance();
    data11.setPropertyValue("values", new Float[] { 1f, 2f, 3f });
    final Feature data12 = dataType.newInstance();
    data12.setPropertyValue("values", new Float[] { 4f, 5f, 6f });
    record1.setPropertyValue("datas", Arrays.asList(data11, data12));
    final Feature record2 = recordType.newInstance();
    record2.setPropertyValue("time", new Date(6000000));
    final Feature data21 = dataType.newInstance();
    data21.setPropertyValue("values", new Float[] { 7f, 8f, 9f });
    record2.setPropertyValue("datas", Arrays.asList(data21));
    sounding.setPropertyValue("records", Arrays.asList(record1, record2));
    List<ResourceId> addedIds = store.addFeatures(soundingType.getName().toString(), Collections.singleton(sounding));
    assertEquals(1, addedIds.size());
    assertEquals(FF.resourceId("Sounding.1"), addedIds.get(0));
    final Session session = store.createSession(false);
    final FeatureCollection col = session.getFeatureCollection(new Query(soundingType.getName()));
    assertEquals(1, col.size());
    final FeatureIterator ite = store.getFeatureReader(new Query(soundingType.getName()));
    try {
        final Feature resFeature = ite.next();
        assertNotNull(resFeature);
        assertEquals(120l, resFeature.getPropertyValue("identifier"));
        final Collection<Feature> records = (Collection<Feature>) resFeature.getPropertyValue("records");
        assertEquals(2, records.size());
        final boolean[] found = new boolean[2];
        for (Feature record : records) {
            final Timestamp time = (Timestamp) record.getPropertyValue("time");
            if (time.getTime() == 5000000) {
                found[0] = true;
                final Collection<Feature> datas = (Collection<Feature>) record.getPropertyValue("datas");
                assertEquals(2, datas.size());
                final boolean[] dfound = new boolean[2];
                for (Feature data : datas) {
                    final Float[] values = (Float[]) data.getPropertyValue("values");
                    if (Arrays.equals(values, new Float[] { 1f, 2f, 3f })) {
                        dfound[0] = true;
                    } else if (Arrays.equals(values, new Float[] { 4f, 5f, 6f })) {
                        dfound[1] = true;
                    } else {
                        fail("Unexpected property \n" + data);
                    }
                }
                for (boolean b : dfound) assertTrue(b);
            } else if (time.getTime() == 6000000) {
                found[1] = true;
                final Collection<Feature> datas = (Collection<Feature>) record.getPropertyValue("datas");
                assertEquals(1, datas.size());
                final boolean[] dfound = new boolean[1];
                for (Feature data : datas) {
                    final Float[] values = (Float[]) data.getPropertyValue("values");
                    if (Arrays.equals(values, new Float[] { 7f, 8f, 9f })) {
                        dfound[0] = true;
                    } else {
                        fail("Unexpected property \n" + data);
                    }
                }
                for (boolean b : dfound) assertTrue(b);
            } else {
                fail("Unexpected property \n" + record);
            }
        }
        for (boolean b : found) assertTrue(b);
    } finally {
        ite.close();
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) SQLQuery(org.geotoolkit.storage.feature.query.SQLQuery) Feature(org.opengis.feature.Feature) Timestamp(java.sql.Timestamp) Date(java.util.Date) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) ResourceId(org.opengis.filter.ResourceId) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) Collection(java.util.Collection) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole) Session(org.geotoolkit.storage.feature.session.Session) Test(org.junit.Test)

Example 17 with Session

use of org.geotoolkit.storage.feature.session.Session in project geotoolkit by Geomatys.

the class PostgresComplexTypeTest method testComplex3Insert.

/**
 * multiple complex properties of same type
 * @throws org.apache.sis.storage.DataStoreException
 * @throws org.geotoolkit.version.VersioningException
 */
@Test
public void testComplex3Insert() throws DataStoreException, VersioningException {
    reload(false);
    store.createFeatureType(FTYPE_COMPLEX3);
    final FeatureType recordType = store.getFeatureType(store.getNames().iterator().next().toString());
    final Feature record = FTYPE_RECORD.newInstance();
    record.setPropertyValue("identifier", 120);
    final Feature data1 = FTYPE_DATA.newInstance();
    data1.setPropertyValue("value", 5f);
    final Feature data2 = FTYPE_DATA.newInstance();
    data2.setPropertyValue("value", 10f);
    final Feature data3 = FTYPE_DATA.newInstance();
    data3.setPropertyValue("value", 15f);
    record.setPropertyValue("datas", Arrays.asList(data1, data2, data3));
    List<ResourceId> addedIds = store.addFeatures(recordType.getName().toString(), Collections.singleton(record));
    assertEquals(1, addedIds.size());
    assertEquals(FF.resourceId("Record.1"), addedIds.get(0));
    final Session session = store.createSession(false);
    final FeatureCollection col = session.getFeatureCollection(new Query(recordType.getName()));
    assertEquals(1, col.size());
    final FeatureIterator ite = store.getFeatureReader(new Query(recordType.getName()));
    try {
        final Feature resFeature = ite.next();
        assertNotNull(resFeature);
        assertEquals(120l, resFeature.getPropertyValue("identifier"));
        assertNotNull(resFeature.getProperty("data1"));
        assertNotNull(resFeature.getProperty("data2"));
        assertNotNull(resFeature.getProperty("data3"));
        assertEquals(5f, ((Feature) resFeature.getPropertyValue("data1")).getPropertyValue("value"));
        assertEquals(10f, ((Feature) resFeature.getPropertyValue("data2")).getPropertyValue("value"));
        assertEquals(15f, ((Feature) resFeature.getPropertyValue("data3")).getPropertyValue("value"));
    } finally {
        ite.close();
    }
}
Also used : FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) SQLQuery(org.geotoolkit.storage.feature.query.SQLQuery) ResourceId(org.opengis.filter.ResourceId) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) Feature(org.opengis.feature.Feature) Session(org.geotoolkit.storage.feature.session.Session) Test(org.junit.Test)

Example 18 with Session

use of org.geotoolkit.storage.feature.session.Session in project geotoolkit by Geomatys.

the class PostgresVersioningTest method testVersioningSynchrone.

/**
 * Check versions are created on each call on the session.
 */
@Test
public void testVersioningSynchrone() throws DataStoreException, VersioningException {
    reload(true);
    List<Version> versions;
    Version version;
    Feature feature;
    ResourceId fid;
    FeatureIterator ite;
    Query qb = new Query();
    final FeatureType refType = FTYPE_SIMPLE;
    store.createFeatureType(refType);
    final VersionControl vc = store.getVersioning(refType.getName().toString());
    // //////////////////////////////////////////////////////////////////////
    // start versioning /////////////////////////////////////////////////////
    vc.startVersioning();
    versions = vc.list();
    assertTrue(versions.isEmpty());
    final Session session = store.createSession(false);
    // //////////////////////////////////////////////////////////////////////
    // make an insert ///////////////////////////////////////////////////////
    final Point firstPoint = GF.createPoint(new Coordinate(56, 45));
    feature = refType.newInstance();
    feature.setPropertyValue("id", "0");
    feature.setPropertyValue("boolean", Boolean.TRUE);
    feature.setPropertyValue("integer", 14);
    feature.setPropertyValue("point", firstPoint);
    feature.setPropertyValue("string", "someteststring");
    session.addFeatures(refType.getName().toString(), Collections.singleton(feature));
    // we should have one version
    versions = vc.list();
    assertEquals(1, versions.size());
    version = versions.get(0);
    Date date = version.getDate();
    // ensure normal reading is correct without version----------------------
    qb = new Query();
    qb.setTypeName(refType.getName().toString());
    ite = session.getFeatureCollection(qb).iterator();
    try {
        feature = ite.next();
        fid = FeatureExt.getId(feature);
    } finally {
        ite.close();
    }
    try {
        // wait a bit just to have some space between version dates
        Thread.sleep(1000);
    } catch (InterruptedException ex) {
        fail(ex.getMessage());
    }
    // //////////////////////////////////////////////////////////////////////
    // make an update 1 /////////////////////////////////////////////////////
    final Point secondPoint = GF.createPoint(new Coordinate(-12, 21));
    Map<String, Object> updates = new HashMap<>();
    updates.put("boolean", Boolean.FALSE);
    updates.put("integer", -3);
    updates.put("point", secondPoint);
    updates.put("string", "anothertextupdated");
    session.updateFeatures(refType.getName().toString(), fid, updates);
    // we should have two versions
    versions = vc.list();
    assertEquals(2, versions.size());
    // //////////////////////////////////////////////////////////////////////
    // make an update 2 /////////////////////////////////////////////////////
    final Point thirdPoint = GF.createPoint(new Coordinate(48, -51));
    updates = new HashMap<>();
    updates.put("boolean", Boolean.TRUE);
    updates.put("integer", -89);
    updates.put("point", thirdPoint);
    updates.put("string", "thridupdatetext");
    session.updateFeatures(refType.getName().toString(), fid, updates);
    // we should have three versions
    versions = vc.list();
    assertEquals(3, versions.size());
    // //////////////////////////////////////////////////////////////////////
    // delete record ////////////////////////////////////////////////////////
    session.removeFeatures(refType.getName().toString(), fid);
    qb = new Query();
    qb.setTypeName(refType.getName().toString());
    assertEquals(0, session.getCount(qb));
    // we should have four versions
    versions = vc.list();
    assertEquals(4, versions.size());
    // //////////////////////////////////////////////////////////////////////
    // make an insert ///////////////////////////////////////////////////////
    Point fourthPoint = GF.createPoint(new Coordinate(66, 11));
    feature = refType.newInstance();
    feature.setPropertyValue("id", "0");
    feature.setPropertyValue("boolean", Boolean.FALSE);
    feature.setPropertyValue("integer", 22);
    feature.setPropertyValue("point", fourthPoint);
    feature.setPropertyValue("string", "fourthupdateString");
    session.addFeatures(refType.getName().toString(), Collections.singleton(feature));
    // we should have five versions
    versions = vc.list();
    assertEquals(5, versions.size());
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) HashMap(java.util.HashMap) Point(org.locationtech.jts.geom.Point) Feature(org.opengis.feature.Feature) VersionControl(org.geotoolkit.version.VersionControl) Date(java.util.Date) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) Version(org.geotoolkit.version.Version) ResourceId(org.opengis.filter.ResourceId) Coordinate(org.locationtech.jts.geom.Coordinate) Session(org.geotoolkit.storage.feature.session.Session) Test(org.junit.Test)

Example 19 with Session

use of org.geotoolkit.storage.feature.session.Session in project geotoolkit by Geomatys.

the class PostgresVersioningTest method testVersioningASynchrone.

/**
 * Check versions are created only on session commit calls.
 */
@Test
public void testVersioningASynchrone() throws DataStoreException, VersioningException {
    reload(true);
    List<Version> versions;
    Version version;
    Feature feature;
    ResourceId fid;
    FeatureIterator ite;
    Query qb = new Query();
    final FeatureType refType = FTYPE_SIMPLE;
    store.createFeatureType(refType);
    final VersionControl vc = store.getVersioning(refType.getName().toString());
    // //////////////////////////////////////////////////////////////////////
    // start versioning /////////////////////////////////////////////////////
    vc.startVersioning();
    versions = vc.list();
    assertTrue(versions.isEmpty());
    final Session session = store.createSession(true);
    // //////////////////////////////////////////////////////////////////////
    // make an insert ///////////////////////////////////////////////////////
    final Point firstPoint = GF.createPoint(new Coordinate(56, 45));
    feature = refType.newInstance();
    feature.setPropertyValue("id", "0");
    feature.setPropertyValue("boolean", Boolean.TRUE);
    feature.setPropertyValue("integer", 14);
    feature.setPropertyValue("point", firstPoint);
    feature.setPropertyValue("string", "someteststring");
    session.addFeatures(refType.getName().toString(), Collections.singleton(feature));
    // we should have 0 version
    versions = vc.list();
    assertEquals(0, versions.size());
    // <-- creates a version
    session.commit();
    // we should have 1 version
    versions = vc.list();
    assertEquals(1, versions.size());
    version = versions.get(0);
    Date date = version.getDate();
    // ensure normal reading is correct without version----------------------
    qb = new Query();
    qb.setTypeName(refType.getName().toString());
    ite = session.getFeatureCollection(qb).iterator();
    try {
        feature = ite.next();
        fid = FeatureExt.getId(feature);
    } finally {
        ite.close();
    }
    try {
        // wait a bit just to have some space between version dates
        Thread.sleep(1000);
    } catch (InterruptedException ex) {
        fail(ex.getMessage());
    }
    // //////////////////////////////////////////////////////////////////////
    // make 2 updates at the time ///////////////////////////////////////////
    final Point secondPoint = GF.createPoint(new Coordinate(-12, 21));
    Map<String, Object> updates = new HashMap<>();
    updates.put("boolean", Boolean.FALSE);
    updates.put("integer", -3);
    updates.put("point", secondPoint);
    updates.put("string", "anothertextupdated");
    session.updateFeatures(refType.getName().toString(), fid, updates);
    // we should have 1 version
    versions = vc.list();
    assertEquals(1, versions.size());
    final Point thirdPoint = GF.createPoint(new Coordinate(48, -51));
    updates = new HashMap<>();
    updates.put("boolean", Boolean.TRUE);
    updates.put("integer", -89);
    updates.put("point", thirdPoint);
    updates.put("string", "thridupdatetext");
    session.updateFeatures(refType.getName().toString(), fid, updates);
    // we should have 1 version
    versions = vc.list();
    assertEquals(1, versions.size());
    // <-- creates a version
    session.commit();
    // we should have two versions
    versions = vc.list();
    assertEquals(2, versions.size());
    // ensure we read the latest --------------------------------------------
    qb = new Query();
    qb.setTypeName(refType.getName());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        feature = ite.next();
        assertEquals(Boolean.TRUE, feature.getProperty("boolean").getValue());
        assertEquals(-89, feature.getProperty("integer").getValue());
        assertEquals(thirdPoint, feature.getProperty("point").getValue());
        assertEquals("thridupdatetext", feature.getProperty("string").getValue());
    } finally {
        ite.close();
    }
    // //////////////////////////////////////////////////////////////////////
    // make delete + insert at the same time ///////////////////////////////
    session.removeFeatures(refType.getName().toString(), fid);
    qb = new Query();
    qb.setTypeName(refType.getName().toString());
    assertEquals(0, session.getCount(qb));
    // we should have two versions
    versions = vc.list();
    assertEquals(2, versions.size());
    // //////////////////////////////////////////////////////////////////////
    // delete record ////////////////////////////////////////////////////////
    session.removeFeatures(refType.getName().toString(), fid);
    qb = new Query();
    qb.setTypeName(refType.getName().toString());
    assertEquals(0, session.getCount(qb));
    // we should have two versions
    versions = vc.list();
    assertEquals(2, versions.size());
    Point fourthPoint = GF.createPoint(new Coordinate(66, 11));
    feature = refType.newInstance();
    feature.setPropertyValue("id", "0");
    feature.setPropertyValue("boolean", Boolean.FALSE);
    feature.setPropertyValue("integer", 22);
    feature.setPropertyValue("point", fourthPoint);
    feature.setPropertyValue("string", "fourthupdateString");
    session.addFeatures(refType.getName().toString(), Collections.singleton(feature));
    // we should have two versions
    versions = vc.list();
    assertEquals(2, versions.size());
    // <-- creates a version
    session.commit();
    // we should have three versions
    versions = vc.list();
    assertEquals(3, versions.size());
    // ensure we read the latest --------------------------------------------
    qb = new Query();
    qb.setTypeName(refType.getName().toString());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        feature = ite.next();
        assertEquals(Boolean.FALSE, feature.getProperty("boolean").getValue());
        assertEquals(22, feature.getProperty("integer").getValue());
        assertEquals(fourthPoint, feature.getProperty("point").getValue());
        assertEquals("fourthupdateString", feature.getProperty("string").getValue());
    } finally {
        ite.close();
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) HashMap(java.util.HashMap) Point(org.locationtech.jts.geom.Point) Feature(org.opengis.feature.Feature) VersionControl(org.geotoolkit.version.VersionControl) Date(java.util.Date) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) Version(org.geotoolkit.version.Version) ResourceId(org.opengis.filter.ResourceId) Coordinate(org.locationtech.jts.geom.Coordinate) Session(org.geotoolkit.storage.feature.session.Session) Test(org.junit.Test)

Example 20 with Session

use of org.geotoolkit.storage.feature.session.Session in project geotoolkit by Geomatys.

the class IndexedShapefileDataStoreTest method testWipesOutInvalidFidsFromFilters.

/**
 * Issueing a request, whether its a query, update or delete, with a fid filter where feature
 * ids match the {@code <typeName>.<number>} structure but the {@code <typeName>} part does not
 * match the actual typeName, shoud ensure the invalid fids are ignored
 *
 * @throws java.lang.Exception
 */
@Test
public void testWipesOutInvalidFidsFromFilters() throws Exception {
    final IndexedShapefileFeatureStore ds = createDataStore();
    final Session session = ds.createSession(true);
    final String validFid1, validFid2, invalidFid1, invalidFid2;
    try (FeatureIterator features = ds.getFeatureReader(new Query(ds.getName()))) {
        validFid1 = FeatureExt.getId(features.next()).getIdentifier();
        validFid2 = FeatureExt.getId(features.next()).getIdentifier();
        invalidFid1 = "_" + FeatureExt.getId(features.next()).getIdentifier();
        invalidFid2 = FeatureExt.getId(features.next()).getIdentifier() + "abc";
    }
    FilterFactory2 ff = FilterUtilities.FF;
    Set<Filter<Object>> ids = new HashSet<>();
    ids.add(ff.resourceId(validFid1));
    ids.add(ff.resourceId(validFid2));
    ids.add(ff.resourceId(invalidFid1));
    ids.add(ff.resourceId(invalidFid2));
    Filter fidFilter = ff.or(ids);
    final FeatureType schema = ds.getFeatureType();
    final String typeName = schema.getName().tip().toString();
    // get a property of type String to update its value by the given filter
    assertEquals(2, count(ds, typeName, fidFilter));
    session.updateFeatures(ds.getName().toString(), fidFilter, Collections.singletonMap("f", "modified"));
    session.commit();
    Filter modifiedFilter = ff.equal(ff.property("f"), ff.literal("modified"));
    assertEquals(2, count(ds, typeName, modifiedFilter));
    final long initialCount = ds.getCount(new Query(ds.getName()));
    session.removeFeatures(ds.getName().toString(), fidFilter);
    session.commit();
    final long afterCount = ds.getCount(new Query(ds.getName()));
    assertEquals(initialCount - 2, afterCount);
}
Also used : FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) Filter(org.opengis.filter.Filter) FilterFactory2(org.geotoolkit.filter.FilterFactory2) Session(org.geotoolkit.storage.feature.session.Session) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Session (org.geotoolkit.storage.feature.session.Session)21 Query (org.geotoolkit.storage.feature.query.Query)20 FeatureType (org.opengis.feature.FeatureType)14 Test (org.junit.Test)13 Feature (org.opengis.feature.Feature)13 FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)12 FeatureIterator (org.geotoolkit.storage.feature.FeatureIterator)10 ResourceId (org.opengis.filter.ResourceId)9 GenericName (org.opengis.util.GenericName)7 Date (java.util.Date)5 FeatureStore (org.geotoolkit.storage.feature.FeatureStore)5 Coordinate (org.locationtech.jts.geom.Coordinate)5 Point (org.locationtech.jts.geom.Point)4 ArrayList (java.util.ArrayList)3 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)3 SQLQuery (org.geotoolkit.storage.feature.query.SQLQuery)3 Version (org.geotoolkit.version.Version)3 GeometryCollection (org.locationtech.jts.geom.GeometryCollection)3 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)3 File (java.io.File)2