Search in sources :

Example 1 with PerTypeInstanceCollection

use of eu.esdihumboldt.hale.common.instance.model.ext.impl.PerTypeInstanceCollection in project hale by halestudio.

the class JDBCInstanceReader method execute.

@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    progress.begin("Configure database connection", ProgressIndicator.UNKNOWN);
    try {
        testConnection();
        String user = getParameter(PARAM_USER).as(String.class);
        String password = getParameter(PARAM_PASSWORD).as(String.class);
        Map<TypeDefinition, InstanceCollection> collections = new HashMap<>();
        // only load instances for mapping relevant types
        for (TypeDefinition type : getSourceSchema().getMappingRelevantTypes()) {
            // check constraint if a Database table or not
            if (type.getConstraint(DatabaseTable.class).isTable()) {
                collections.put(type, new JDBCTableCollection(type, getSource().getLocation(), user, password, getCrsProvider(), getServiceProvider()) {

                    // To provide extensibility for getting customized
                    // database connection for
                    // Instance reading.
                    @Override
                    protected Connection createConnection() throws SQLException {
                        return JDBCInstanceReader.this.getConnection();
                    }
                });
            } else // database?
            if (type.getConstraint(SQLQuery.class).hasQuery()) {
                collections.put(type, new JDBCTableCollection(type, getSource().getLocation(), user, password, getCrsProvider(), getServiceProvider()) {

                    // To provide extensibility for getting customized
                    // database connection for
                    // Instance reading.
                    @Override
                    protected Connection createConnection() throws SQLException {
                        return JDBCInstanceReader.this.getConnection();
                    }
                });
            }
        }
        collection = new PerTypeInstanceCollection(collections);
        reporter.setSuccess(true);
    } catch (Exception e) {
        reporter.error(new IOMessageImpl("Error configuring database connection", e));
        reporter.setSuccess(false);
    } finally {
        progress.end();
    }
    return reporter;
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) MultiInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.MultiInstanceCollection) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) PerTypeInstanceCollection(eu.esdihumboldt.hale.common.instance.model.ext.impl.PerTypeInstanceCollection) Connection(java.sql.Connection) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) IOException(java.io.IOException) SQLException(java.sql.SQLException) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) DatabaseTable(eu.esdihumboldt.hale.io.jdbc.constraints.DatabaseTable) PerTypeInstanceCollection(eu.esdihumboldt.hale.common.instance.model.ext.impl.PerTypeInstanceCollection)

Example 2 with PerTypeInstanceCollection

use of eu.esdihumboldt.hale.common.instance.model.ext.impl.PerTypeInstanceCollection in project hale by halestudio.

the class ShapeInstanceReader method execute.

/**
 * @see AbstractIOProvider#execute(ProgressIndicator, IOReporter)
 */
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    // $NON-NLS-1$
    progress.begin(Messages.getString("ShapeSchemaProvider.1"), ProgressIndicator.UNKNOWN);
    // DataStore store = new ShapefileDataStoreFactory().createDataStore(location.toURL());
    // DataStore store = FileDataStoreFinder.getDataStore(getSource().getLocation().toURL());
    ShapefileDataStore store = new ShapefileDataStore(getSource().getLocation().toURL());
    store.setCharset(getCharset());
    progress.setCurrentTask("Extracting shape instances");
    String typename = getParameter(PARAM_TYPENAME).as(String.class);
    TypeDefinition defaultType = null;
    if (typename != null && !typename.isEmpty()) {
        try {
            defaultType = getSourceSchema().getType(QName.valueOf(typename));
        } catch (Exception e) {
        // ignore
        }
    }
    if (defaultType == null) {
        // check if typename was supplied w/o namespace
        try {
            defaultType = getSourceSchema().getType(new QName(ShapefileConstants.SHAPEFILE_NS, typename));
        } catch (Exception e) {
        // ignore
        // TODO report?
        }
    }
    if (defaultType == null) {
        reporter.info(new IOMessageImpl("No type name supplied as parameter, trying to auto-detect the schema type.", null));
        TypeDefinition dataType = ShapeSchemaReader.readShapeType(getSource());
        if (dataType == null) {
            throw new IOException("Could not read shapefile structure information");
        }
        String preferredName = null;
        Name name = store.getNames().iterator().next();
        if (name != null) {
            preferredName = name.getLocalPart();
        }
        Pair<TypeDefinition, Integer> tp = getMostCompatibleShapeType(getSourceSchema(), dataType, preferredName);
        if (tp == null) {
            throw new IOProviderConfigurationException("No schema type specified and auto-detection failed");
        }
        defaultType = tp.getFirst();
        reporter.info(new IOMessageImpl(MessageFormat.format("Auto-deteted {0} as schema type, with a {1}% compatibility rating.", defaultType.getName(), tp.getSecond()), null));
    }
    Map<TypeDefinition, InstanceCollection> collections = new HashMap<>();
    // create a collection for each type
    for (Name name : store.getNames()) {
        SimpleFeatureSource features = store.getFeatureSource(name);
        TypeDefinition type = defaultType;
        if (type == null) {
            QName typeName = new QName(ShapefileConstants.SHAPEFILE_NS, name.getLocalPart());
            type = getSourceSchema().getType(typeName);
        }
        collections.put(type, new ShapesInstanceCollection(features, type, getCrsProvider(), name.getLocalPart()));
    }
    instances = new PerTypeInstanceCollection(collections);
    reporter.setSuccess(true);
    return reporter;
}
Also used : ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) PerTypeInstanceCollection(eu.esdihumboldt.hale.common.instance.model.ext.impl.PerTypeInstanceCollection) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOException(java.io.IOException) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Name(org.opengis.feature.type.Name) QName(javax.xml.namespace.QName) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) PerTypeInstanceCollection(eu.esdihumboldt.hale.common.instance.model.ext.impl.PerTypeInstanceCollection)

Aggregations

IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)2 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)2 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)2 PerTypeInstanceCollection (eu.esdihumboldt.hale.common.instance.model.ext.impl.PerTypeInstanceCollection)2 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 MultiInstanceCollection (eu.esdihumboldt.hale.common.instance.model.impl.MultiInstanceCollection)1 DatabaseTable (eu.esdihumboldt.hale.io.jdbc.constraints.DatabaseTable)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)1 QName (javax.xml.namespace.QName)1 ShapefileDataStore (org.geotools.data.shapefile.ShapefileDataStore)1 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)1 Name (org.opengis.feature.type.Name)1