Search in sources :

Example 1 with DataStoreFactory

use of org.geotoolkit.storage.DataStoreFactory in project geotoolkit by Geomatys.

the class PostgresVersioningTest method testDistinctSchema.

@Test
public void testDistinctSchema() throws DataStoreException, VersioningException, FileNotFoundException, IOException {
    reload(true);
    List<Version> versions;
    Feature feature;
    ResourceId fid;
    Version v0;
    Version v1;
    Version v2;
    FeatureIterator ite;
    Query qb = new Query();
    final FeatureType refType = FTYPE_SIMPLE;
    // ------------------- initialize public2 schema --------------------
    // / creation 2eme table
    PostgresStore store2;
    final ParameterValueGroup params2 = params.clone();
    params2.parameter("schema").setValue("public2");
    store2 = (PostgresStore) DataStores.open(params2);
    // -------------- create schema in public2 schema --------------------
    try {
        ((DataStoreFactory) store2.getProvider()).create(params2);
    } catch (Exception ex) {
    // schema public2 already exist
    }
    for (GenericName n : store2.getNames()) {
        VersionControl vc = store2.getVersioning(n.toString());
        vc.dropVersioning();
        store2.deleteFeatureType(n.toString());
    }
    assertTrue(store2.getNames().isEmpty());
    // delete historisation functions, he must create them himself
    store2.dropHSFunctions();
    // -------------- create table in public schema --------------------
    store.createFeatureType(refType);
    assertEquals(1, store.getNames().size());
    assertTrue(store2.getNames().isEmpty());
    // get version control
    final VersionControl vcP1 = store.getVersioning(refType.getName().toString());
    assertNotNull(vcP1);
    assertTrue(vcP1.isEditable());
    assertFalse(vcP1.isVersioned());
    // -------------------- start versioning in public schema ---------------
    vcP1.startVersioning();
    assertTrue(vcP1.isVersioned());
    versions = vcP1.list();
    assertTrue(versions.isEmpty());
    // --------------------- table creation in public2 schema ---------------
    store2.createFeatureType(refType);
    assertEquals(1, store2.getNames().size());
    // get version control
    final VersionControl vcP2 = store2.getVersioning(refType.getName().toString());
    assertNotNull(vcP2);
    assertTrue(vcP2.isEditable());
    assertFalse(vcP2.isVersioned());
    // -------------------- start versioning in public schema ---------------
    vcP2.startVersioning();
    assertTrue(vcP2.isVersioned());
    versions = vcP2.list();
    assertTrue(versions.isEmpty());
    /* insert, update and delete some elements in public schema and verify
         * public2 schema stay empty, to verify the 2th schema are actions distincts.*/
    // 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");
    store.addFeatures(refType.getName().toString(), Collections.singleton(feature));
    // ensure test table in public2 schema is empty
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertTrue(store2.createSession(true).getFeatureCollection(qb).isEmpty());
    // ensure history test table in public2 schema is empty
    assertTrue(vcP2.list().isEmpty());
    // make an update ///////////////////////////////////////////////////////
    // get feature to update
    // get identifier
    qb = new Query();
    qb.setTypeName(refType.getName());
    ite = store.createSession(true).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());
    }
    final Point secondPoint = GF.createPoint(new Coordinate(-12, 21));
    final Map<String, Object> updates = new HashMap<>();
    updates.put("boolean", Boolean.FALSE);
    updates.put("integer", -3);
    updates.put("point", secondPoint);
    updates.put("string", "anothertextupdated");
    store.updateFeatures(refType.getName().toString(), fid, updates);
    // ensure test table in public2 schema is empty
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertTrue(store2.createSession(true).getFeatureCollection(qb).isEmpty());
    // ensure history test table in public2 schema is empty
    assertTrue(vcP2.list().isEmpty());
    // make a remove ///////////////////////////////////////////////////////
    store.removeFeatures(refType.getName().toString(), fid);
    // ensure test table in public2 schema is empty
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertTrue(store2.createSession(true).getFeatureCollection(qb).isEmpty());
    // ensure history test table in public2 schema is empty
    assertTrue(vcP2.list().isEmpty());
    // get all versions organized in increase dates order.
    versions = vcP1.list();
    assertEquals(3, versions.size());
    v0 = versions.get(0);
    v1 = versions.get(1);
    v2 = versions.get(2);
    vcP1.revert(v1.getDate());
    // ensure test table in public2 schema is empty
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertTrue(store2.createSession(true).getFeatureCollection(qb).isEmpty());
    // ensure history test table in public2 schema is empty
    assertTrue(vcP2.list().isEmpty());
    vcP1.trim(v1.getDate());
    // ensure test table in public2 schema is empty
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertTrue(store2.createSession(true).getFeatureCollection(qb).isEmpty());
    // ensure history test table in public2 schema is empty
    assertTrue(vcP2.list().isEmpty());
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) HashMap(java.util.HashMap) Point(org.locationtech.jts.geom.Point) Feature(org.opengis.feature.Feature) VersionControl(org.geotoolkit.version.VersionControl) VersioningException(org.geotoolkit.version.VersioningException) DataStoreException(org.apache.sis.storage.DataStoreException) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) GenericName(org.opengis.util.GenericName) Version(org.geotoolkit.version.Version) ResourceId(org.opengis.filter.ResourceId) Coordinate(org.locationtech.jts.geom.Coordinate) DataStoreFactory(org.geotoolkit.storage.DataStoreFactory) Test(org.junit.Test)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 DataStoreException (org.apache.sis.storage.DataStoreException)1 DataStoreFactory (org.geotoolkit.storage.DataStoreFactory)1 FeatureIterator (org.geotoolkit.storage.feature.FeatureIterator)1 FeatureStoreRuntimeException (org.geotoolkit.storage.feature.FeatureStoreRuntimeException)1 Query (org.geotoolkit.storage.feature.query.Query)1 Version (org.geotoolkit.version.Version)1 VersionControl (org.geotoolkit.version.VersionControl)1 VersioningException (org.geotoolkit.version.VersioningException)1 Test (org.junit.Test)1 Coordinate (org.locationtech.jts.geom.Coordinate)1 Point (org.locationtech.jts.geom.Point)1 Feature (org.opengis.feature.Feature)1 FeatureType (org.opengis.feature.FeatureType)1 ResourceId (org.opengis.filter.ResourceId)1 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)1 GenericName (org.opengis.util.GenericName)1