Search in sources :

Example 26 with IOProviderConfigurationException

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

the class MappingExporter method execute.

@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    progress.begin("Generate mapping documentation", ProgressIndicator.UNKNOWN);
    // retrieve template URL
    URL templateUrl = getClass().getResource("mapping.html");
    // generate Json representation
    CellJsonExtension ext = new ExtendedCellRepresentation(getAlignment(), getServiceProvider());
    ValueRepresentation rep = new JsonValueRepresentation();
    StringWriter jsonWriter = new StringWriter();
    JsonStreamBuilder json = new JsonStreamBuilder(jsonWriter, true);
    Set<Locale> locales = AlignmentJson.alignmentInfoJSON(getAlignment(), json, getServiceProvider(), getProjectInfo(), ext, rep, Locale.getDefault(), getSourceSchema(), getTargetSchema());
    // create language binding
    String languageJson = getLanguageJson(locales);
    // create template binding
    Map<String, Object> binding = new HashMap<>();
    binding.put("json", jsonWriter.toString());
    String title = (getProjectInfo() != null && getProjectInfo().getName() != null) ? getProjectInfo().getName() : "Mapping documentation";
    binding.put("title", title);
    binding.put("languages", languageJson);
    binding.put("halejsVersion", HALEJS_VERSION);
    // initialize template engine
    GStringTemplateEngine engine = new GStringTemplateEngine();
    // bind and write template
    try (Writer out = new OutputStreamWriter(getTarget().getOutput(), StandardCharsets.UTF_8)) {
        Writable template = engine.createTemplate(templateUrl).make(binding);
        template.writeTo(out);
        reporter.setSuccess(true);
    } catch (Exception e) {
        reporter.error(new IOMessageImpl("Error creating mapping documentation", e));
        reporter.setSuccess(false);
    } finally {
        progress.end();
    }
    return reporter;
}
Also used : Locale(java.util.Locale) JsonValueRepresentation(eu.esdihumboldt.hale.io.html.svg.mapping.json.JsonValueRepresentation) HashMap(java.util.HashMap) ExtendedCellRepresentation(eu.esdihumboldt.hale.io.html.svg.mapping.json.ExtendedCellRepresentation) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) Writable(groovy.lang.Writable) GStringTemplateEngine(groovy.text.GStringTemplateEngine) CellJsonExtension(eu.esdihumboldt.hale.io.html.svg.mapping.json.CellJsonExtension) URL(java.net.URL) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) ValueRepresentation(eu.esdihumboldt.hale.io.html.svg.mapping.json.ValueRepresentation) JsonValueRepresentation(eu.esdihumboldt.hale.io.html.svg.mapping.json.JsonValueRepresentation) StringWriter(java.io.StringWriter) JsonStreamBuilder(eu.esdihumboldt.util.groovy.json.JsonStreamBuilder) OutputStreamWriter(java.io.OutputStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) AbstractAlignmentWriter(eu.esdihumboldt.hale.common.align.io.impl.AbstractAlignmentWriter)

Example 27 with IOProviderConfigurationException

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

the class HtmlMappingExporter method execute.

@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    this.reporter = reporter;
    context = new VelocityContext();
    cellIds = new Identifiers<Cell>(Cell.class, false);
    alignment = getAlignment();
    // $NON-NLS-1$
    URL headlinePath = this.getClass().getResource("bg-headline.png");
    // $NON-NLS-1$
    URL cssPath = this.getClass().getResource("style.css");
    // $NON-NLS-1$
    URL linkPath = this.getClass().getResource("int_link.png");
    // $NON-NLS-1$
    URL tooltipIcon = this.getClass().getResource("tooltip.gif");
    final String filesSubDir = FilenameUtils.removeExtension(FilenameUtils.getName(getTarget().getLocation().getPath())) + // $NON-NLS-1$
    "_files";
    final File filesDir = new File(FilenameUtils.getFullPath(getTarget().getLocation().getPath()), filesSubDir);
    filesDir.mkdirs();
    context.put(FILE_DIRECTORY, filesSubDir);
    try {
        init();
    } catch (Exception e) {
        return reportError(reporter, "Initializing error", e);
    }
    File cssOutputFile = new File(filesDir, "style.css");
    FileUtils.copyFile(getInputFile(cssPath), cssOutputFile);
    // create headline picture
    // $NON-NLS-1$
    File headlineOutputFile = new File(filesDir, "bg-headline.png");
    FileUtils.copyFile(getInputFile(headlinePath), headlineOutputFile);
    // $NON-NLS-1$
    File linkOutputFile = new File(filesDir, "int_link.png");
    FileUtils.copyFile(getInputFile(linkPath), linkOutputFile);
    // $NON-NLS-1$
    File tooltipIconFile = new File(filesDir, "tooltip.png");
    FileUtils.copyFile(getInputFile(tooltipIcon), tooltipIconFile);
    File htmlExportFile = new File(getTarget().getLocation().getPath());
    if (projectInfo != null) {
        Date date = new Date();
        DateFormat dfm = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
        // associate variables with information data
        String exportDate = dfm.format(date);
        context.put(EXPORT_DATE, exportDate);
        if (projectInfo.getCreated() != null) {
            String created = dfm.format(projectInfo.getCreated());
            context.put(CREATED_DATE, created);
        }
        context.put(PROJECT_INFO, projectInfo);
    }
    if (alignment != null) {
        Collection<TypeCellInfo> typeCellInfos = new ArrayList<TypeCellInfo>();
        Collection<? extends Cell> cells = alignment.getTypeCells();
        Iterator<? extends Cell> it = cells.iterator();
        while (it.hasNext()) {
            final Cell cell = it.next();
            // this is the collection of type cell info
            TypeCellInfo typeCellInfo = new TypeCellInfo(cell, alignment, cellIds, filesSubDir);
            typeCellInfos.add(typeCellInfo);
        }
        // put the full collection of type cell info to the context (for the
        // template)
        context.put(TYPE_CELL_INFOS, typeCellInfos);
        createImages(filesDir);
    }
    context.put(TOOLTIP, getParameter(TOOLTIP).as(boolean.class));
    Template template;
    try {
        template = velocityEngine.getTemplate(templateFile.getName(), "UTF-8");
    } catch (Exception e) {
        return reportError(reporter, "Could not load template", e);
    }
    FileWriter fileWriter = new FileWriter(htmlExportFile);
    template.merge(context, fileWriter);
    fileWriter.close();
    reporter.setSuccess(true);
    return reporter;
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) URL(java.net.URL) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Date(java.util.Date) Template(org.apache.velocity.Template) DateFormat(java.text.DateFormat) Cell(eu.esdihumboldt.hale.common.align.model.Cell) File(java.io.File)

Example 28 with IOProviderConfigurationException

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

the class InstanceBuilderReader method execute.

@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    progress.begin("Run instance builder", ProgressIndicator.UNKNOWN);
    try {
        CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
        compilerConfiguration.setScriptBaseClass(DelegatingScript.class.getName());
        // Configure the GroovyShell and pass the compiler configuration.
        GroovyShell shell = new GroovyShell(getClass().getClassLoader(), new Binding(), compilerConfiguration);
        DelegatingScript script;
        try (InputStream in = getSource().getInput();
            InputStreamReader reader = new InputStreamReader(in, getCharset())) {
            script = (DelegatingScript) shell.parse(reader);
        }
        InstanceBuilder builder = new InstanceBuilder();
        // apply schema
        builder.setTypes(getSourceSchema());
        script.setDelegate(builder);
        Object res = script.run();
        if (res == null) {
            throw new IllegalStateException("Null returned by script");
        } else if (res instanceof InstanceCollection) {
            instances = (InstanceCollection) res;
        } else if (res instanceof Instance) {
            instances = new DefaultInstanceCollection(Collections.singleton((Instance) res));
        } else {
            throw new IllegalStateException("Unrecognised return type: " + res.getClass().getName());
        }
        reporter.setSuccess(true);
    } catch (Exception e) {
        reporter.setSuccess(false);
        reporter.error("Error running instance builder", e);
    } finally {
        progress.end();
    }
    return reporter;
}
Also used : Binding(groovy.lang.Binding) InputStreamReader(java.io.InputStreamReader) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) InputStream(java.io.InputStream) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) GroovyShell(groovy.lang.GroovyShell) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) DelegatingScript(groovy.util.DelegatingScript) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) InstanceBuilder(eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)

Example 29 with IOProviderConfigurationException

use of eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException 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 30 with IOProviderConfigurationException

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

the class JDBCSchemaReader method loadFromSource.

@Override
protected Schema loadFromSource(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    DefaultSchema typeIndex = null;
    progress.begin("Read database 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));
        }
        URI jdbcURI = getSource().getLocation();
        final 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);
        // set what we need
        level.setRetrieveTables(true);
        level.setRetrieveColumnDataTypes(true);
        level.setRetrieveUserDefinedColumnDataTypes(true);
        // to get table columns
        level.setRetrieveTableColumns(true);
        // information, also
        // includes primary key
        // to get linking information
        level.setRetrieveForeignKeys(true);
        // level.setRetrieveIndices(true); // to get info about UNIQUE indices for validation
        // XXX For some advanced info / DBMS specific info we'll need a
        // properties file. See Config & InformationSchemaViews.
        level.setTag("hale");
        if (getParameter(SCHEMAS).as(String.class) != null) {
            String schemas = getParameter(SCHEMAS).as(String.class).replace(',', '|');
            options.setSchemaInclusionRule(new RegularExpressionInclusionRule(schemas));
        }
        if (SchemaSpaceID.SOURCE.equals(getSchemaSpace())) {
            // show views and tables
            final List<String> tableTypesWanted = Arrays.asList("TABLE", "VIEW", "MATERIALIZED VIEW");
            // try to determine table types supported by the JDBC connection
            final List<String> tableTypeSupported = new ArrayList<>();
            try {
                ResultSet rs = connection.getMetaData().getTableTypes();
                while (rs.next()) {
                    String tableType = rs.getString(1);
                    tableTypeSupported.add(tableType);
                }
            } catch (Throwable t) {
                // Ignore, try with wanted list
                reporter.warn(new IOMessageImpl(MessageFormat.format("Could not determine supported table types for connection: {0}", t.getMessage()), t));
                tableTypeSupported.addAll(tableTypesWanted);
            }
            options.setTableTypes(tableTypesWanted.stream().filter(tt -> tableTypeSupported.contains(tt)).collect(Collectors.toList()));
        } else {
            // only show tables
            options.setTableTypes(Arrays.asList("TABLE"));
        }
        options.setSchemaInfoLevel(level);
        // get advisor
        // XXX should be created once, and used in other places if
        // applicable
        JDBCSchemaReaderAdvisor advisor = SchemaReaderAdvisorExtension.getInstance().getAdvisor(connection);
        if (advisor != null) {
            advisor.configureSchemaCrawler(options);
        }
        final Catalog database = SchemaCrawlerUtility.getCatalog(connection, options);
        @SuppressWarnings("unused") String quotes = JDBCUtil.determineQuoteString(connection);
        // FIXME not actually used here or in SQL schema reader
        String overallNamespace = JDBCUtil.determineNamespace(jdbcURI, advisor);
        // create the type index
        typeIndex = new DefaultSchema(overallNamespace, jdbcURI);
        for (final schemacrawler.schema.Schema schema : database.getSchemas()) {
            // each schema represents a namespace
            String namespace;
            if (overallNamespace.isEmpty()) {
                namespace = unquote(schema.getName());
            } else {
                namespace = overallNamespace;
                if (schema.getName() != null) {
                    namespace += ":" + unquote(schema.getName());
                }
            }
            for (final Table table : database.getTables(schema)) {
                // each table is a type
                // get the type definition
                TypeDefinition type = getOrCreateTableType(schema, table, overallNamespace, namespace, typeIndex, connection, reporter, database);
                // get ResultSetMetaData for extra info about columns (e. g.
                // auto increment)
                ResultsColumns additionalInfo = null;
                Statement stmt = null;
                try {
                    stmt = connection.createStatement();
                    // get if in table name, quotation required or not.
                    String fullTableName = getQuotedValue(table.getName());
                    if (schema.getName() != null) {
                        fullTableName = getQuotedValue(schema.getName()) + "." + fullTableName;
                    }
                    ResultSet rs = stmt.executeQuery("SELECT * FROM " + fullTableName + " WHERE 1 = 0");
                    additionalInfo = SchemaCrawlerUtility.getResultColumns(rs);
                } catch (SQLException sqle) {
                    reporter.warn(new IOMessageImpl("Couldn't retrieve additional column meta data.", sqle));
                } finally {
                    if (stmt != null)
                        try {
                            stmt.close();
                        } catch (SQLException e) {
                        // ignore
                        }
                }
                // create property definitions for each column
                for (final Column column : table.getColumns()) {
                    DefaultPropertyDefinition property = getOrCreateProperty(schema, type, column, overallNamespace, namespace, typeIndex, connection, reporter, database);
                    // XXX does not work for example for PostgreSQL
                    if (additionalInfo != null) {
                        // ResultColumns does not quote the column namen in
                        // contrast to every other place
                        ResultsColumn rc = additionalInfo.getColumn(unquote(column.getName()));
                        if (rc != null && rc.isAutoIncrement())
                            property.setConstraint(AutoIncrementFlag.get(true));
                    }
                }
            }
        }
        reporter.setSuccess(true);
    } catch (SchemaCrawlerException 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 : DefaultPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition) SchemaInfoLevel(schemacrawler.schemacrawler.SchemaInfoLevel) RegularExpressionInclusionRule(schemacrawler.schemacrawler.RegularExpressionInclusionRule) SQLException(java.sql.SQLException) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) ArrayList(java.util.ArrayList) URI(java.net.URI) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) ResultsColumn(schemacrawler.schema.ResultsColumn) Column(schemacrawler.schema.Column) BaseColumn(schemacrawler.schema.BaseColumn) IndexColumn(schemacrawler.schema.IndexColumn) SchemaCrawlerException(schemacrawler.schemacrawler.SchemaCrawlerException) DefaultSchema(eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema) ResultSet(java.sql.ResultSet) ResultsColumn(schemacrawler.schema.ResultsColumn) Table(schemacrawler.schema.Table) DatabaseTable(eu.esdihumboldt.hale.io.jdbc.constraints.DatabaseTable) Statement(java.sql.Statement) Connection(java.sql.Connection) JDBCSchemaReaderAdvisor(eu.esdihumboldt.hale.io.jdbc.extension.JDBCSchemaReaderAdvisor) SchemaCrawlerOptions(schemacrawler.schemacrawler.SchemaCrawlerOptions) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) SchemaCrawlerException(schemacrawler.schemacrawler.SchemaCrawlerException) SQLException(java.sql.SQLException) IOException(java.io.IOException) Catalog(schemacrawler.schema.Catalog) ResultsColumns(schemacrawler.schema.ResultsColumns) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)

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