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