Search in sources :

Example 26 with CommandFailedException

use of org.locationtech.geogig.cli.CommandFailedException in project GeoGig by boundlessgeo.

the class AbstractSLCommand method getDataStore.

/**
     * Constructs a new SpatiaLite data store using connection parameters from {@link SLCommonArgs}.
     * 
     * @return the constructed data store
     * @throws CommandFailedException
     * @see DataStore
     */
protected DataStore getDataStore() {
    Map<String, Serializable> params = Maps.newHashMap();
    params.put(SpatiaLiteDataStoreFactory.DBTYPE.key, "spatialite");
    params.put(SpatiaLiteDataStoreFactory.DATABASE.key, commonArgs.database);
    params.put(SpatiaLiteDataStoreFactory.USER.key, commonArgs.username);
    try {
        DataStore dataStore = dataStoreFactory.createDataStore(params);
        if (dataStore == null) {
            throw new CommandFailedException("Unable to connect using the specified database parameters.");
        }
        if (dataStore instanceof JDBCDataStore) {
            Connection con = null;
            con = ((JDBCDataStore) dataStore).getDataSource().getConnection();
            ((JDBCDataStore) dataStore).closeSafe(con);
        }
        return dataStore;
    } catch (IOException e) {
        throw new CommandFailedException("Unable to connect using the specified database parameters.", e);
    } catch (SQLException e) {
        throw new CommandFailedException("Unable to validate the database connection.", e);
    }
}
Also used : Serializable(java.io.Serializable) JDBCDataStore(org.geotools.jdbc.JDBCDataStore) SQLException(java.sql.SQLException) JDBCDataStore(org.geotools.jdbc.JDBCDataStore) DataStore(org.geotools.data.DataStore) Connection(java.sql.Connection) IOException(java.io.IOException) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException)

Example 27 with CommandFailedException

use of org.locationtech.geogig.cli.CommandFailedException in project GeoGig by boundlessgeo.

the class AbstractSQLServerCommand method getDataStore.

/**
     * Constructs a new SQL Server data store using connection parameters from
     * {@link SQLServerCommonArgs}.
     * 
     * @return the constructed data store
     * @throws CommandFailedException
     * @see DataStore
     */
protected DataStore getDataStore() {
    Map<String, Serializable> params = Maps.newHashMap();
    params.put(SQLServerDataStoreFactory.DBTYPE.key, "sqlserver");
    params.put(SQLServerDataStoreFactory.HOST.key, commonArgs.host);
    params.put(SQLServerDataStoreFactory.PORT.key, commonArgs.port.toString());
    params.put(SQLServerDataStoreFactory.INTSEC.key, commonArgs.intsec);
    params.put(SQLServerDataStoreFactory.SCHEMA.key, commonArgs.schema);
    params.put(SQLServerDataStoreFactory.DATABASE.key, commonArgs.database);
    params.put(SQLServerDataStoreFactory.USER.key, commonArgs.username);
    params.put(SQLServerDataStoreFactory.PASSWD.key, commonArgs.password);
    params.put(SQLServerDataStoreFactory.FETCHSIZE.key, 1000);
    if (!commonArgs.geometryMetadataTable.equals(""))
        params.put(SQLServerDataStoreFactory.GEOMETRY_METADATA_TABLE.key, commonArgs.geometryMetadataTable);
    DataStore dataStore;
    try {
        dataStore = dataStoreFactory.createDataStore(params);
    } catch (IOException e) {
        throw new CommandFailedException("Unable to connect using the specified database parameters.", e);
    }
    if (dataStore == null) {
        throw new CommandFailedException("No suitable data store found for the provided parameters");
    }
    if (dataStore instanceof JDBCDataStore) {
        Connection con = null;
        try {
            con = ((JDBCDataStore) dataStore).getDataSource().getConnection();
        } catch (Exception e) {
            throw new CommandFailedException("Error validating the database connection", e);
        }
        ((JDBCDataStore) dataStore).closeSafe(con);
    }
    return dataStore;
}
Also used : Serializable(java.io.Serializable) JDBCDataStore(org.geotools.jdbc.JDBCDataStore) JDBCDataStore(org.geotools.jdbc.JDBCDataStore) DataStore(org.geotools.data.DataStore) Connection(java.sql.Connection) IOException(java.io.IOException) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) IOException(java.io.IOException) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException)

Example 28 with CommandFailedException

use of org.locationtech.geogig.cli.CommandFailedException in project GeoGig by boundlessgeo.

the class AbstractGeoJsonCommand method getDataStore.

protected DataStore getDataStore(String geoJSON) throws FileNotFoundException, IOException {
    try {
        File geoJsonfile = new File(geoJSON);
        checkParameter(geoJsonfile.exists(), "File does not exist '%s'", geoJsonfile);
        InputStream in = new FileInputStream(geoJsonfile);
        FeatureJSON fjson = new FeatureJSON();
        @SuppressWarnings("rawtypes") FeatureCollection fc = fjson.readFeatureCollection(in);
        @SuppressWarnings("unchecked") DataStore dataStore = new MemoryDataStore(fc);
        return dataStore;
    } catch (IOException ioe) {
        throw new CommandFailedException("Error opening GeoJSON: " + ioe.getMessage(), ioe);
    }
}
Also used : FeatureJSON(org.geotools.geojson.feature.FeatureJSON) FeatureCollection(org.geotools.feature.FeatureCollection) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DataStore(org.geotools.data.DataStore) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException)

Example 29 with CommandFailedException

use of org.locationtech.geogig.cli.CommandFailedException in project GeoGig by boundlessgeo.

the class AbstractPGCommand method getDataStore.

/**
     * Constructs a new PostGIS data store using connection parameters from {@link PGCommonArgs}.
     * 
     * @return the constructed data store
     * @throws Exception
     * @see DataStore
     */
protected DataStore getDataStore() {
    Map<String, Serializable> params = Maps.newHashMap();
    params.put(PostgisNGDataStoreFactory.DBTYPE.key, "postgis");
    params.put(PostgisNGDataStoreFactory.HOST.key, commonArgs.host);
    params.put(PostgisNGDataStoreFactory.PORT.key, commonArgs.port.toString());
    params.put(PostgisNGDataStoreFactory.SCHEMA.key, commonArgs.schema);
    params.put(PostgisNGDataStoreFactory.DATABASE.key, commonArgs.database);
    params.put(PostgisNGDataStoreFactory.USER.key, commonArgs.username);
    params.put(PostgisNGDataStoreFactory.PASSWD.key, commonArgs.password);
    params.put(PostgisNGDataStoreFactory.FETCHSIZE.key, 1000);
    params.put(PostgisNGDataStoreFactory.EXPOSE_PK.key, true);
    DataStore dataStore;
    try {
        dataStore = dataStoreFactory.createDataStore(params);
    } catch (IOException e) {
        throw new CommandFailedException("Unable to connect using the specified database parameters.", e);
    }
    if (dataStore == null) {
        throw new CommandFailedException("Unable to connect using the specified database parameters.");
    }
    if (dataStore instanceof JDBCDataStore) {
        Connection con = null;
        try {
            con = ((JDBCDataStore) dataStore).getDataSource().getConnection();
        } catch (SQLException e) {
            throw new CommandFailedException(e.getMessage(), e);
        }
        ((JDBCDataStore) dataStore).closeSafe(con);
    }
    return dataStore;
}
Also used : Serializable(java.io.Serializable) JDBCDataStore(org.geotools.jdbc.JDBCDataStore) SQLException(java.sql.SQLException) JDBCDataStore(org.geotools.jdbc.JDBCDataStore) DataStore(org.geotools.data.DataStore) Connection(java.sql.Connection) IOException(java.io.IOException) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException)

Example 30 with CommandFailedException

use of org.locationtech.geogig.cli.CommandFailedException in project GeoGig by boundlessgeo.

the class GeoJsonImport method runInternal.

@Override
protected void runInternal(GeogigCLI cli) throws InvalidParameterException, CommandFailedException, IOException {
    checkParameter(geoJSONList != null && !geoJSONList.isEmpty(), "No GeoJSON specified");
    checkParameter(geomName == null || !geomNameAuto, "Cannot use --geom-name and --geom-name-auto at the same time");
    for (String geoJSON : geoJSONList) {
        DataStore dataStore = null;
        try {
            dataStore = getDataStore(geoJSON);
        } catch (InvalidParameterException e) {
            cli.getConsole().println("The GeoJSON file '" + geoJSON + "' could not be found, skipping...");
            continue;
        }
        if (fidAttribute != null) {
            AttributeDescriptor attrib = dataStore.getSchema(dataStore.getNames().get(0)).getDescriptor(fidAttribute);
            if (attrib == null) {
                throw new InvalidParameterException("The specified attribute does not exist in the selected GeoJSON file");
            }
        }
        if (geomNameAuto) {
            String destPath = destTable;
            if (destPath == null) {
                destPath = dataStore.getSchema(dataStore.getNames().get(0)).getTypeName();
            }
            Optional<RevFeatureType> ft = cli.getGeogig().command(RevObjectParse.class).setRefSpec("WORK_HEAD:" + destPath).call(RevFeatureType.class);
            // attribute
            if (ft.isPresent()) {
                GeometryDescriptor geomDescriptor = ft.get().type().getGeometryDescriptor();
                if (geomDescriptor != null) {
                    geomName = geomDescriptor.getLocalName();
                }
            }
        }
        try {
            cli.getConsole().println("Importing from GeoJSON " + geoJSON);
            ProgressListener progressListener = cli.getProgressListener();
            cli.getGeogig().command(ImportOp.class).setAll(true).setTable(null).setAlter(alter).setOverwrite(!add).setDestinationPath(destTable).setDataStore(dataStore).setFidAttribute(fidAttribute).setGeometryNameOverride(geomName).setAdaptToDefaultFeatureType(!forceFeatureType).setProgressListener(progressListener).call();
            cli.getConsole().println(geoJSON + " imported successfully.");
        } catch (GeoToolsOpException e) {
            switch(e.statusCode) {
                case NO_FEATURES_FOUND:
                    throw new CommandFailedException("No features were found in the GeoJSON file.", e);
                case UNABLE_TO_GET_NAMES:
                    throw new CommandFailedException("Unable to get feature types from the GeoJSON file.", e);
                case UNABLE_TO_GET_FEATURES:
                    throw new CommandFailedException("Unable to get features from the GeoJSON file.", e);
                case UNABLE_TO_INSERT:
                    throw new CommandFailedException("Unable to insert features into the working tree.", e);
                case INCOMPATIBLE_FEATURE_TYPE:
                    throw new CommandFailedException("The feature type of the data to import does not match the feature type of the destination tree and cannot be imported\n" + "USe the --force-featuretype switch to import using the original featuretype and crete a mixed type tree", e);
                default:
                    throw new CommandFailedException("Import failed with exception: " + e.statusCode.name(), e);
            }
        }
    }
}
Also used : InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) GeometryDescriptor(org.opengis.feature.type.GeometryDescriptor) ProgressListener(org.locationtech.geogig.api.ProgressListener) DataStore(org.geotools.data.DataStore) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) ImportOp(org.locationtech.geogig.geotools.plumbing.ImportOp) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)

Aggregations

CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)58 DataStore (org.geotools.data.DataStore)28 GeoToolsOpException (org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)24 File (java.io.File)16 GeoGIG (org.locationtech.geogig.api.GeoGIG)16 ObjectId (org.locationtech.geogig.api.ObjectId)15 IOException (java.io.IOException)14 InvalidParameterException (org.locationtech.geogig.cli.InvalidParameterException)12 ConsoleReader (jline.console.ConsoleReader)10 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)10 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)10 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)10 ExportOp (org.locationtech.geogig.geotools.plumbing.ExportOp)9 Serializable (java.io.Serializable)8 Optional (com.google.common.base.Optional)7 TYPE (org.locationtech.geogig.api.RevObject.TYPE)7 Map (java.util.Map)6 ProgressListener (org.locationtech.geogig.api.ProgressListener)6 Connection (java.sql.Connection)4 Feature (org.opengis.feature.Feature)4