Search in sources :

Example 11 with IOProviderConfigurationException

use of eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException in project hale by halestudio.

the class StreamGmlWriter method checkCompatibility.

/**
 * @see AbstractInstanceWriter#checkCompatibility()
 */
@Override
public void checkCompatibility() throws IOProviderConfigurationException {
    super.checkCompatibility();
    XmlIndex xmlIndex = getXMLIndex();
    if (xmlIndex == null) {
        fail("No XML target schema");
    }
    if (requiresDefaultContainer()) {
        XmlElement element;
        try {
            element = findDefaultContainter(xmlIndex, null);
        } catch (Exception e) {
            // ignore
            element = null;
        }
        if (element == null) {
            fail("Cannot find container element in schema.");
        }
    }
}
Also used : XmlIndex(eu.esdihumboldt.hale.io.xsd.model.XmlIndex) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) URISyntaxException(java.net.URISyntaxException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException)

Example 12 with IOProviderConfigurationException

use of eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException in project hale by halestudio.

the class JDBCInstanceWriter method execute.

@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    InstanceCollection instances = getInstances();
    Connection connection = null;
    try {
        // connect to the database
        try {
            connection = getConnection();
        } catch (Exception e) {
            reporter.error(new IOMessageImpl(e.getLocalizedMessage(), e));
            reporter.setSuccess(false);
            reporter.setSummary("Failed to connect to database.");
            return reporter;
        }
        if (isWriteUnordered()) {
            // write instances as they come in
            writeInstances(connection, instances, progress, reporter);
        } else {
            // write instances based on type order needed for insert
            // (to avoid violating constraints)
            Set<TypeDefinition> sortedSet = getSortedSchemas(getTargetSchema().getMappingRelevantTypes());
            for (TypeDefinition td : sortedSet) {
                writeInstances(connection, instances.select(new TypeFilter(td)), progress, reporter);
            }
        }
        reporter.setSuccess(true);
    } catch (Exception e) {
        reporter.error(new IOMessageImpl(e.getLocalizedMessage(), e));
        reporter.setSuccess(false);
        reporter.setSummary("Saving instances to database failed.");
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
            // ignore
            }
        }
        progress.end();
    }
    return reporter;
}
Also used : SQLException(java.sql.SQLException) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) Connection(java.sql.Connection) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) TypeFilter(eu.esdihumboldt.hale.common.instance.model.TypeFilter) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) SQLException(java.sql.SQLException) IOException(java.io.IOException) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 13 with IOProviderConfigurationException

use of eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException in project hale by halestudio.

the class SQLSchemaReader method loadFromSource.

@Override
protected Schema loadFromSource(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    DefaultSchema typeIndex = null;
    String query = null;
    Text text = getParameter(PARAM_SQL).as(Text.class);
    if (text != null) {
        query = text.getText();
    }
    if (query == null) {
        query = getParameter(PARAM_SQL).as(String.class);
    }
    if (query == null) {
        reporter.setSuccess(false);
        reporter.setSummary("No SQL query specified");
        return null;
    }
    String typename = getParameter(PARAM_TYPE_NAME).as(String.class);
    if (typename == null) {
        reporter.setSuccess(false);
        reporter.setSummary("Name of the type that the SQL query should be represented as must be specified");
        return null;
    }
    progress.begin("Read SQL query schema", ProgressIndicator.UNKNOWN);
    Connection connection = null;
    try {
        // connect to the database
        try {
            connection = getConnection();
        } catch (Exception e) {
            reporter.error(new IOMessageImpl(e.getLocalizedMessage(), e));
            reporter.setSuccess(false);
            reporter.setSummary("Failed to connect to database.");
            return null;
        }
        // connection has been created), report a warning message instead
        try {
            connection.setReadOnly(true);
        } catch (SQLException e) {
        // ignore
        // reporter.warn(new IOMessageImpl(e.getLocalizedMessage(), e));
        }
        connection.setAutoCommit(false);
        // get advisor
        JDBCSchemaReaderAdvisor advisor = SchemaReaderAdvisorExtension.getInstance().getAdvisor(connection);
        // determine quotes character
        @SuppressWarnings("unused") String quotes = determineQuoteString(connection);
        // FIXME not actually used here or in JDBC schema reader
        URI jdbcURI = getSource().getLocation();
        String dbNamespace = determineNamespace(jdbcURI, advisor);
        String namespace = NAMESPACE;
        SchemaCrawlerOptions options = new SchemaCrawlerOptions();
        SchemaInfoLevel level = new SchemaInfoLevel();
        level.setTag("hale");
        // these are enabled by default, we don't need them (yet)
        level.setRetrieveSchemaCrawlerInfo(false);
        level.setRetrieveJdbcDriverInfo(false);
        level.setRetrieveDatabaseInfo(false);
        level.setRetrieveTables(false);
        level.setRetrieveTableColumns(false);
        level.setRetrieveForeignKeys(false);
        // set what we need
        level.setRetrieveColumnDataTypes(true);
        level.setRetrieveUserDefinedColumnDataTypes(true);
        options.setSchemaInfoLevel(level);
        if (advisor != null) {
            advisor.configureSchemaCrawler(options);
        }
        final Catalog database = SchemaCrawlerUtility.getCatalog(connection, options);
        // create the type index
        typeIndex = new DefaultSchema(dbNamespace, jdbcURI);
        Statement st = null;
        try {
            st = JDBCUtil.createReadStatement(connection, 1);
            // support project variables
            String processedQuery = JDBCUtil.replaceVariables(query, getServiceProvider());
            ResultSet result = st.executeQuery(processedQuery);
            // the query represents a type
            // get the type definition
            TypeDefinition type = addTableType(query, namespace, typeIndex, connection, reporter, typename);
            ResultsColumns additionalInfo = SchemaCrawlerUtility.getResultColumns(result);
            for (final ResultsColumn column : additionalInfo.getColumns()) {
                getOrCreateProperty(type, column, namespace, typeIndex, connection, reporter, database);
            }
        } finally {
            if (st != null) {
                st.close();
            }
        }
        reporter.setSuccess(true);
    } catch (Exception e) {
        throw new IOProviderConfigurationException("Failed to read database schema", e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
            // ignore
            }
        }
        progress.end();
    }
    return typeIndex;
}
Also used : SchemaInfoLevel(schemacrawler.schemacrawler.SchemaInfoLevel) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) JDBCSchemaReaderAdvisor(eu.esdihumboldt.hale.io.jdbc.extension.JDBCSchemaReaderAdvisor) Text(eu.esdihumboldt.hale.common.core.io.Text) JDBCUtil.determineQuoteString(eu.esdihumboldt.hale.io.jdbc.JDBCUtil.determineQuoteString) SchemaCrawlerOptions(schemacrawler.schemacrawler.SchemaCrawlerOptions) URI(java.net.URI) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) SQLException(java.sql.SQLException) IOException(java.io.IOException) Catalog(schemacrawler.schema.Catalog) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) ResultsColumns(schemacrawler.schema.ResultsColumns) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) DefaultSchema(eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema) ResultSet(java.sql.ResultSet) ResultsColumn(schemacrawler.schema.ResultsColumn)

Example 14 with IOProviderConfigurationException

use of eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException in project hale by halestudio.

the class IOWizard method performFinish.

/**
 * @see Wizard#performFinish()
 *
 * @return <code>true</code> if executing the I/O provider was successful
 */
@Override
public boolean performFinish() {
    if (getProvider() == null) {
        return false;
    }
    if (!applyConfiguration()) {
        return false;
    }
    // create default report
    IOReporter defReport = provider.createReporter();
    // validate and execute provider
    try {
        // validate configuration
        provider.validate();
        ProjectService ps = PlatformUI.getWorkbench().getService(ProjectService.class);
        URI projectLoc = ps.getLoadLocation() == null ? null : ps.getLoadLocation();
        boolean isProjectResource = false;
        if (actionId != null) {
            // XXX instead move project resource to action?
            ActionUI factory = ActionUIExtension.getInstance().findActionUI(actionId);
            isProjectResource = factory.isProjectResource();
        }
        // prevent loading of duplicate resources
        if (isProjectResource && provider instanceof ImportProvider && !getProviderFactory().allowDuplicateResource()) {
            String currentResource = ((ImportProvider) provider).getSource().getLocation().toString();
            URI currentAbsolute = URI.create(currentResource);
            if (projectLoc != null && !currentAbsolute.isAbsolute()) {
                currentAbsolute = projectLoc.resolve(currentAbsolute);
            }
            for (IOConfiguration conf : ((Project) ps.getProjectInfo()).getResources()) {
                Value otherResourceValue = conf.getProviderConfiguration().get(ImportProvider.PARAM_SOURCE);
                if (otherResourceValue == null) {
                    continue;
                }
                String otherResource = otherResourceValue.as(String.class);
                URI otherAbsolute = URI.create(otherResource);
                if (projectLoc != null && !otherAbsolute.isAbsolute()) {
                    otherAbsolute = projectLoc.resolve(otherAbsolute);
                }
                String action = conf.getActionId();
                // resource is already loaded into the project
                if (currentAbsolute.equals(otherAbsolute) && Objects.equal(actionId, action)) {
                    // check if the resource is loaded with a provider that
                    // allows duplicates
                    boolean allowDuplicate = false;
                    IOProviderDescriptor providerFactory = IOProviderExtension.getInstance().getFactory(conf.getProviderId());
                    if (providerFactory != null) {
                        allowDuplicate = providerFactory.allowDuplicateResource();
                    }
                    if (!allowDuplicate) {
                        log.userError("Resource is already loaded. Loading duplicate resources is aborted!");
                        return false;
                    }
                }
            }
        }
        // enable provider internal caching
        if (isProjectResource && provider instanceof CachingImportProvider) {
            ((CachingImportProvider) provider).setProvideCache();
        }
        IOReport report = execute(provider, defReport);
        if (report != null) {
            // add report to report server
            ReportService repService = PlatformUI.getWorkbench().getService(ReportService.class);
            repService.addReport(report);
            // show message to user
            if (report.isSuccess()) {
                // let advisor handle results
                try {
                    getContainer().run(true, false, new IRunnableWithProgress() {

                        @Override
                        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                            monitor.beginTask("Completing operation...", IProgressMonitor.UNKNOWN);
                            try {
                                advisor.handleResults(getProvider());
                            } finally {
                                monitor.done();
                            }
                        }
                    });
                } catch (InvocationTargetException e) {
                    log.userError("Error processing results:\n" + e.getCause().getLocalizedMessage(), e.getCause());
                    return false;
                } catch (Exception e) {
                    log.userError("Error processing results:\n" + e.getLocalizedMessage(), e);
                    return false;
                }
                // add to project service if necessary
                if (isProjectResource)
                    ps.rememberIO(actionId, getProviderFactory().getIdentifier(), provider);
                return true;
            } else {
                // error message
                log.userError(report.getSummary() + "\nPlease see the report for details.");
                return false;
            }
        } else
            return true;
    } catch (IOProviderConfigurationException e) {
        // user feedback
        log.userError("Validation of the provider configuration failed:\n" + e.getLocalizedMessage(), e);
        return false;
    }
}
Also used : IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) ProjectService(eu.esdihumboldt.hale.ui.service.project.ProjectService) ActionUI(eu.esdihumboldt.hale.ui.io.action.ActionUI) CachingImportProvider(eu.esdihumboldt.hale.common.core.io.CachingImportProvider) ImportProvider(eu.esdihumboldt.hale.common.core.io.ImportProvider) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) URI(java.net.URI) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) Project(eu.esdihumboldt.hale.common.core.io.project.model.Project) IOReporter(eu.esdihumboldt.hale.common.core.io.report.IOReporter) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) ReportService(eu.esdihumboldt.hale.ui.service.report.ReportService) CachingImportProvider(eu.esdihumboldt.hale.common.core.io.CachingImportProvider) Value(eu.esdihumboldt.hale.common.core.io.Value)

Example 15 with IOProviderConfigurationException

use of eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException in project hale by halestudio.

the class CityGMLXsltExport method init.

@Override
protected void init(XmlIndex sourceIndex, XmlIndex targetIndex) throws IOProviderConfigurationException {
    super.init(sourceIndex, targetIndex);
    // scan target schema for CityModel
    targetCityModel = findCityModel(targetIndex);
    if (targetCityModel != null) {
        QName name = targetCityModel.getName();
        setParameter(PARAM_ROOT_ELEMENT_NAMESPACE, new ParameterValue(name.getNamespaceURI()));
        setParameter(PARAM_ROOT_ELEMENT_NAME, new ParameterValue(name.getLocalPart()));
    } else {
        throw new IOProviderConfigurationException(MessageFormat.format("Element {0} not found in the target schema.", CITY_MODEL_ELEMENT));
    }
    // scan source schema for CityModel
    sourceCityModel = findCityModel(sourceIndex);
    if (sourceCityModel != null) {
        // create a custom source context
        sourceContext = new CityGMLSourceContext(sourceCityModel);
    // TODO copy envelope?
    }
}
Also used : IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) QName(javax.xml.namespace.QName)

Aggregations

IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)44 IOException (java.io.IOException)38 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)27 InputStream (java.io.InputStream)14 URI (java.net.URI)13 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)7 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)6 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)6 DefaultSchema (eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema)6 File (java.io.File)6 OutputStream (java.io.OutputStream)6 ArrayList (java.util.ArrayList)6 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)5 QName (javax.xml.namespace.QName)5 IOProviderDescriptor (eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor)4 DefaultPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition)4 InputStreamReader (java.io.InputStreamReader)4 Connection (java.sql.Connection)4 SQLException (java.sql.SQLException)4 PathUpdate (eu.esdihumboldt.hale.common.core.io.PathUpdate)3