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