Search in sources :

Example 1 with VersioningException

use of org.geotoolkit.version.VersioningException in project geotoolkit by Geomatys.

the class Copy method execute.

/**
 *  {@inheritDoc }
 */
@Override
protected void execute() throws ProcessException {
    final FeatureStore sourceDS = inputParameters.getValue(SOURCE_STORE);
    final FeatureStore targetDS = inputParameters.getValue(TARGET_STORE);
    Session targetSS = inputParameters.getValue(TARGET_SESSION);
    final Boolean eraseParam = inputParameters.getValue(ERASE);
    final Boolean newVersion = inputParameters.getValue(NEW_VERSION);
    // Type name can be removed, it's embedded in the query param.
    final String typenameParam = inputParameters.getValue(TYPE_NAME);
    final Query queryParam = inputParameters.getValue(QUERY);
    final boolean doCommit = targetSS == null;
    final Session sourceSS = sourceDS.createSession(false);
    if (targetSS == null) {
        if (targetDS != null) {
            targetSS = targetDS.createSession(true);
        } else {
            throw new ProcessException("Input target_session or target_datastore missing.", this, null);
        }
    }
    boolean reBuildQuery = false;
    final String queryName;
    if (queryParam != null) {
        queryName = queryParam.getTypeName();
        reBuildQuery = true;
    } else if (typenameParam != null) {
        queryName = typenameParam;
    } else {
        queryName = "*";
    }
    final Set<GenericName> names;
    if ("*".equals(queryName)) {
        // all values
        try {
            names = sourceDS.getNames();
        } catch (DataStoreException ex) {
            throw new ProcessException(ex.getMessage(), this, ex);
        }
    } else {
        // pick only the wanted names
        names = new HashSet<>();
        final List<String> wanted = UnmodifiableArrayList.wrap(queryName.split(","));
        for (String s : wanted) {
            try {
                final FeatureType type = sourceDS.getFeatureType(s);
                names.add(type.getName());
            } catch (DataStoreException ex) {
                throw new ProcessException(ex.getMessage(), this, ex);
            }
        }
    }
    final float size = names.size();
    int inc = 0;
    for (GenericName n : names) {
        fireProgressing("Copying " + n + ".", (int) ((inc * 100f) / size), false);
        try {
            Query query;
            if (reBuildQuery) {
                Query builder = new Query();
                builder.copy(queryParam);
                builder.setTypeName(n);
                query = builder;
            } else {
                query = queryParam != null ? queryParam : new Query(n);
            }
            insert(n, sourceSS, targetSS, query, eraseParam, newVersion);
        } catch (DataStoreException ex) {
            throw new ProcessException(ex.getMessage(), this, ex);
        }
        inc++;
    }
    try {
        Date lastVersionDate = null;
        if (doCommit) {
            LOGGER.log(Level.INFO, "Commit all changes");
            targetSS.commit();
            // find last version
            for (GenericName n : names) {
                if (targetSS.getFeatureStore().getQueryCapabilities().handleVersioning()) {
                    final List<Version> versions = targetSS.getFeatureStore().getVersioning(n.toString()).list();
                    if (!versions.isEmpty()) {
                        if (lastVersionDate == null || versions.get(versions.size() - 1).getDate().getTime() > lastVersionDate.getTime()) {
                            lastVersionDate = versions.get(versions.size() - 1).getDate();
                        }
                    }
                }
            }
        }
        if (lastVersionDate != null) {
            outputParameters.getOrCreate(VERSION).setValue(lastVersionDate);
        }
    } catch (DataStoreException ex) {
        throw new ProcessException(ex.getMessage(), this, ex);
    } catch (VersioningException ex) {
        throw new ProcessException(ex.getMessage(), this, ex);
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) DataStoreException(org.apache.sis.storage.DataStoreException) Query(org.geotoolkit.storage.feature.query.Query) Date(java.util.Date) ProcessException(org.geotoolkit.process.ProcessException) GenericName(org.opengis.util.GenericName) Version(org.geotoolkit.version.Version) VersioningException(org.geotoolkit.version.VersioningException) FeatureStore(org.geotoolkit.storage.feature.FeatureStore) Session(org.geotoolkit.storage.feature.session.Session)

Example 2 with VersioningException

use of org.geotoolkit.version.VersioningException in project geotoolkit by Geomatys.

the class PostgresVersionControl method trim.

private void trim(final String schemaName, final FeatureType type, final Date date, final Set<FeatureType> visited) throws VersioningException {
    if (visited.contains(type))
        return;
    visited.add(type);
    final String tableName = type.getName().tip().toString();
    // trim complex properties versioning
    for (PropertyType desc : type.getProperties(true)) {
        if (desc instanceof FeatureAssociationRole) {
            // complex type, trim sub table history
            FeatureAssociationRole far = (FeatureAssociationRole) desc;
            trim(schemaName, far.getValueType(), date, visited);
        }
    }
    Connection cnx = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        cnx = featureStore.getDataSource().getConnection();
        stmt = cnx.createStatement();
        final StringBuilder sb = new StringBuilder("SELECT \"HSX_TrimHistory\"(");
        sb.append('\'');
        if (schemaName != null && !schemaName.isEmpty()) {
            sb.append(schemaName).append('.');
        }
        sb.append(tableName);
        sb.append('\'');
        sb.append(", TIMESTAMP '");
        sb.append(new Timestamp(date.getTime()).toString());
        sb.append("');");
        rs = stmt.executeQuery(sb.toString());
    } catch (SQLException ex) {
        throw new VersioningException(ex.getMessage(), ex);
    } finally {
        JDBCFeatureStoreUtilities.closeSafe(featureStore.getLogger(), cnx, stmt, null);
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) VersioningException(org.geotoolkit.version.VersioningException) PropertyType(org.opengis.feature.PropertyType) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole) Timestamp(java.sql.Timestamp)

Example 3 with VersioningException

use of org.geotoolkit.version.VersioningException in project geotoolkit by Geomatys.

the class PostgresVersionControl method list.

@Override
public synchronized List<Version> list() throws VersioningException {
    if (!isVersioned()) {
        return Collections.EMPTY_LIST;
    }
    final List<Version> versions = new ArrayList<Version>();
    final String schemaName = featureStore.getDatabaseSchema();
    final String tableName = getHSTableName();
    Connection cnx = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        cnx = featureStore.getDataSource().getConnection();
        stmt = cnx.createStatement();
        final StringBuilder sb = new StringBuilder("SELECT distinct(sub.date) as date FROM (");
        sb.append("SELECT \"HS_Begin\" AS date from ");
        dialect.encodeSchemaAndTableName(sb, schemaName, tableName);
        sb.append(" UNION ");
        sb.append("SELECT \"HS_End\" AS date from ");
        dialect.encodeSchemaAndTableName(sb, schemaName, tableName);
        sb.append(" WHERE \"HS_End\" IS NOT NULL");
        sb.append(") AS sub ORDER BY date ASC");
        rs = stmt.executeQuery(sb.toString());
        while (rs.next()) {
            final Timestamp ts = rs.getTimestamp(1);
            final Version v = new Version(this, ts.toString(), ts);
            versions.add(v);
        }
    } catch (SQLException ex) {
        throw new VersioningException(ex.getMessage(), ex);
    } finally {
        JDBCFeatureStoreUtilities.closeSafe(featureStore.getLogger(), cnx, stmt, null);
    }
    return versions;
}
Also used : Version(org.geotoolkit.version.Version) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) VersioningException(org.geotoolkit.version.VersioningException) Timestamp(java.sql.Timestamp)

Example 4 with VersioningException

use of org.geotoolkit.version.VersioningException in project geotoolkit by Geomatys.

the class PostgresVersionControl method revert.

private void revert(final String schemaName, final FeatureType type, final Date date, final Set<FeatureType> visited) throws VersioningException {
    if (visited.contains(type))
        return;
    visited.add(type);
    final String tableName = type.getName().tip().toString();
    // revert complex properties versioning
    for (PropertyType desc : type.getProperties(true)) {
        if (desc instanceof FeatureAssociationRole) {
            // complex type, revert sub table history
            FeatureAssociationRole far = (FeatureAssociationRole) desc;
            revert(schemaName, far.getValueType(), date, visited);
        }
    }
    Connection cnx = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        cnx = featureStore.getDataSource().getConnection();
        stmt = cnx.createStatement();
        final StringBuilder sb = new StringBuilder("SELECT \"HSX_RevertHistory\"(");
        sb.append('\'');
        if (schemaName != null && !schemaName.isEmpty()) {
            sb.append(schemaName).append('.');
        }
        sb.append(tableName);
        sb.append('\'');
        sb.append(", TIMESTAMP '");
        sb.append(new Timestamp(date.getTime()).toString());
        sb.append("');");
        rs = stmt.executeQuery(sb.toString());
    } catch (SQLException ex) {
        throw new VersioningException(ex.getMessage(), ex);
    } finally {
        JDBCFeatureStoreUtilities.closeSafe(featureStore.getLogger(), cnx, stmt, null);
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) VersioningException(org.geotoolkit.version.VersioningException) PropertyType(org.opengis.feature.PropertyType) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole) Timestamp(java.sql.Timestamp)

Example 5 with VersioningException

use of org.geotoolkit.version.VersioningException in project geotoolkit by Geomatys.

the class PostgresVersionControl method createVersioningTable.

/**
 * Create versioning table for given table.
 *
 * @param schemaName
 * @param tableName
 * @param visited set of already visited types, there might be recursion
 *                or multiple properties with the same type.
 */
private void createVersioningTable(final String schemaName, final FeatureType type, final Set<FeatureType> visited) throws VersioningException {
    if (visited.contains(type))
        return;
    visited.add(type);
    final String tableName = type.getName().tip().toString();
    final StringBuilder sb = new StringBuilder("SELECT \"HS_CreateHistory\"(");
    sb.append('\'');
    if (schemaName != null && !schemaName.isEmpty()) {
        sb.append(schemaName).append('.');
    }
    sb.append(tableName);
    sb.append('\'');
    sb.append(',');
    final List<String> hsColumnNames = new ArrayList<>();
    for (PropertyType desc : type.getProperties(true)) {
        if (AttributeConvention.contains(desc.getName()))
            continue;
        if (desc instanceof FeatureAssociationRole) {
            // complex type, create sub table history
            FeatureAssociationRole far = (FeatureAssociationRole) desc;
            createVersioningTable(schemaName, far.getValueType(), visited);
        } else if (desc instanceof AttributeType) {
            hsColumnNames.add("'" + desc.getName().tip() + '\'');
        }
    }
    Connection cnx = null;
    Statement stmt = null;
    try {
        cnx = featureStore.getDataSource().getConnection();
        stmt = cnx.createStatement();
        sb.append("array[");
        for (int i = 0, n = hsColumnNames.size(); i < n; i++) {
            if (i != 0)
                sb.append(',');
            sb.append(hsColumnNames.get(i));
        }
        sb.append("]);");
        stmt.executeQuery(sb.toString());
    } catch (SQLException ex) {
        throw new VersioningException(ex.getMessage(), ex);
    } finally {
        JDBCFeatureStoreUtilities.closeSafe(featureStore.getLogger(), cnx, stmt, null);
    }
}
Also used : SQLException(java.sql.SQLException) AttributeType(org.opengis.feature.AttributeType) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) VersioningException(org.geotoolkit.version.VersioningException) PropertyType(org.opengis.feature.PropertyType) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole)

Aggregations

VersioningException (org.geotoolkit.version.VersioningException)16 Connection (java.sql.Connection)10 SQLException (java.sql.SQLException)10 Statement (java.sql.Statement)8 ResultSet (java.sql.ResultSet)6 DataStoreException (org.apache.sis.storage.DataStoreException)6 Version (org.geotoolkit.version.Version)5 FeatureAssociationRole (org.opengis.feature.FeatureAssociationRole)4 PropertyType (org.opengis.feature.PropertyType)4 IOException (java.io.IOException)3 Timestamp (java.sql.Timestamp)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 Query (org.geotoolkit.storage.feature.query.Query)3 FeatureType (org.opengis.feature.FeatureType)3 ScriptRunner (org.geotoolkit.internal.sql.ScriptRunner)2 VersionControl (org.geotoolkit.version.VersionControl)2 Filter (org.opengis.filter.Filter)2 GenericName (org.opengis.util.GenericName)2 FileNotFoundException (java.io.FileNotFoundException)1