Search in sources :

Example 1 with Text

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

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

the class DefaultCustomPropertyFunctionTypeTest method testWriteRead.

/**
 * Test if a simple lookup table containing only string values is the same
 * when converted to DOM and back again.
 */
@Test
public void testWriteRead() {
    DefaultCustomPropertyFunction f = new DefaultCustomPropertyFunction();
    f.setIdentifier("ident");
    f.setFunctionType("groovy");
    f.setName("My function");
    f.setFunctionDefinition(Value.of(new Text("a + b")));
    List<DefaultCustomPropertyFunctionEntity> sources = new ArrayList<>();
    sources.add(createEntity("a", 1, 1, false));
    sources.add(createEntity("b", 0, 1, false));
    f.setSources(sources);
    f.setTarget(createEntity(null, 1, 1, false));
    List<DefaultCustomPropertyFunctionParameter> parameters = new ArrayList<>();
    DefaultCustomPropertyFunctionParameter param1 = new DefaultCustomPropertyFunctionParameter();
    param1.setName("gender");
    Set<String> enumeration = new TreeSet<>();
    enumeration.add("male");
    enumeration.add("female");
    param1.setMinOccurrence(1);
    param1.setMaxOccurrence(1);
    param1.setEnumeration(enumeration);
    parameters.add(param1);
    DefaultCustomPropertyFunctionParameter param2 = new DefaultCustomPropertyFunctionParameter();
    param2.setName("name");
    param2.setBindingClass(String.class);
    param2.setMinOccurrence(0);
    param2.setMaxOccurrence(1);
    parameters.add(param2);
    DefaultCustomPropertyFunctionParameter param3 = new DefaultCustomPropertyFunctionParameter();
    param3.setName("flag");
    param3.setBindingClass(Boolean.class);
    param3.setMinOccurrence(1);
    param3.setMaxOccurrence(1);
    param3.setDefaultValue(Value.of(false));
    String p3display = "Awesome flag";
    param3.setDisplayName(p3display);
    String p3desc = "Awesome flag estimated in 2016.\nAll rights conserved.";
    param3.setDescription(p3desc);
    parameters.add(param3);
    f.setParameters(parameters);
    // explanation
    Map<Locale, Value> templates = new HashMap<>();
    templates.put(Locale.ROOT, Value.of(new Text("Hello")));
    templates.put(Locale.GERMAN, Value.of(new Text("Hallo")));
    templates.put(Locale.FRANCE, Value.of(new Text("Salut")));
    DefaultCustomFunctionExplanation explanation = new DefaultCustomFunctionExplanation(templates, null);
    f.setExplanation(explanation);
    // convert to DOM
    Element fragment = HaleIO.getComplexElement(f);
    // DEBUG
    System.out.println(XmlUtil.serialize(fragment));
    // convert back
    DefaultCustomPropertyFunction conv = HaleIO.getComplexValue(fragment, DefaultCustomPropertyFunction.class, null);
    // checks
    assertNotNull(conv);
    assertEquals(f.getIdentifier(), conv.getIdentifier());
    assertEquals(f.getName(), conv.getName());
    assertEquals(f.getFunctionType(), conv.getFunctionType());
    // function definition
    Text text = conv.getFunctionDefinition().as(Text.class);
    assertNotNull(text);
    assertEquals("a + b", text.getText());
    // sources
    assertEquals(2, conv.getSources().size());
    DefaultCustomPropertyFunctionEntity source1 = conv.getSources().get(0);
    assertEquals("a", source1.getName());
    assertEquals(1, source1.getMinOccurrence());
    assertEquals(1, source1.getMaxOccurrence());
    DefaultCustomPropertyFunctionEntity source2 = conv.getSources().get(1);
    assertEquals("b", source2.getName());
    assertEquals(0, source2.getMinOccurrence());
    // target
    assertNotNull(conv.getTarget());
    assertEquals(null, conv.getTarget().getName());
    // parameters
    assertEquals(3, conv.getParameters().size());
    DefaultCustomPropertyFunctionParameter cp1 = conv.getParameters().get(0);
    assertEquals("gender", cp1.getName());
    assertEquals(2, cp1.getEnumeration().size());
    DefaultCustomPropertyFunctionParameter cp2 = conv.getParameters().get(1);
    assertEquals("name", cp2.getName());
    assertEquals(0, cp2.getMinOccurrence());
    assertEquals(1, cp2.getMaxOccurrence());
    assertEquals(String.class, cp2.getBindingClass());
    DefaultCustomPropertyFunctionParameter cp3 = conv.getParameters().get(2);
    assertEquals("flag", cp3.getName());
    assertEquals(1, cp3.getMinOccurrence());
    assertEquals(1, cp3.getMaxOccurrence());
    assertEquals(Boolean.class, cp3.getBindingClass());
    assertEquals(false, cp3.getDefaultValue().as(Boolean.class));
    assertEquals(p3display, cp3.getDisplayName());
    assertEquals(p3desc, cp3.getDescription());
    // explanation
    assertNotNull(conv.getExplanation());
    Map<Locale, Value> tempConv = conv.getExplanation().getTemplates();
    assertEquals(3, tempConv.size());
    Value tempRoot = tempConv.get(Locale.ROOT);
    assertNotNull(tempRoot);
    assertEquals("Hello", tempRoot.as(Text.class).getText());
    Value tempGerman = tempConv.get(Locale.GERMAN);
    assertNotNull(tempGerman);
    assertEquals("Hallo", tempGerman.as(Text.class).getText());
    Value tempFrance = tempConv.get(Locale.FRANCE);
    assertNotNull(tempFrance);
    assertEquals("Salut", tempFrance.as(Text.class).getText());
}
Also used : Locale(java.util.Locale) HashMap(java.util.HashMap) DefaultCustomPropertyFunctionParameter(eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunctionParameter) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Text(eu.esdihumboldt.hale.common.core.io.Text) DefaultCustomPropertyFunctionEntity(eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunctionEntity) DefaultCustomPropertyFunction(eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunction) TreeSet(java.util.TreeSet) Value(eu.esdihumboldt.hale.common.core.io.Value) DefaultCustomFunctionExplanation(eu.esdihumboldt.hale.common.align.custom.DefaultCustomFunctionExplanation) Test(org.junit.Test)

Example 3 with Text

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

the class GroovyExplanation method getScript.

/**
 * Get the script stored in a Groovy function cell.
 *
 * @param cell the cell
 * @return the Groovy script string or <code>null</code>
 */
public static String getScript(Cell cell) {
    Value scriptValue = CellUtil.getFirstParameter(cell, PARAMETER_SCRIPT);
    String script;
    // try retrieving as text
    Text text = scriptValue.as(Text.class);
    if (text != null) {
        script = text.getText();
    } else {
        // fall back to string value
        script = scriptValue.as(String.class);
    }
    return script;
}
Also used : Value(eu.esdihumboldt.hale.common.core.io.Value) Text(eu.esdihumboldt.hale.common.core.io.Text)

Example 4 with Text

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

the class GroovyUtil method getScriptString.

/**
 * Get the script string.
 *
 * @param function the transformation function the script is associated to
 * @return the script string
 * @throws TransformationException if getting the script parameter from the
 *             function fails
 */
public static String getScriptString(AbstractTransformationFunction<?> function) throws TransformationException {
    ParameterValue scriptValue = function.getParameterChecked(PARAMETER_SCRIPT);
    String script;
    // try retrieving as text
    Text text = scriptValue.as(Text.class);
    if (text != null) {
        script = text.getText();
    } else {
        // fall back to string value
        script = scriptValue.as(String.class);
    }
    return script;
}
Also used : ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) Text(eu.esdihumboldt.hale.common.core.io.Text)

Example 5 with Text

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

the class SQLQueryFactory method restore.

@Override
public SQLQuery restore(Value value, Definition<?> definition, TypeResolver typeIndex, ClassResolver resolver) throws Exception {
    String query;
    Text text = value.as(Text.class);
    if (text != null) {
        query = text.getText();
    } else {
        query = value.as(String.class);
    }
    if (query != null) {
        return new SQLQuery(query);
    }
    return SQLQuery.NO_QUERY;
}
Also used : Text(eu.esdihumboldt.hale.common.core.io.Text) SQLQuery(eu.esdihumboldt.hale.io.jdbc.constraints.SQLQuery)

Aggregations

Text (eu.esdihumboldt.hale.common.core.io.Text)8 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)4 Value (eu.esdihumboldt.hale.common.core.io.Value)3 ArrayList (java.util.ArrayList)2 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 ListMultimap (com.google.common.collect.ListMultimap)1 GroovyConstants (eu.esdihumboldt.cst.functions.groovy.GroovyConstants)1 GroovyJoin (eu.esdihumboldt.cst.functions.groovy.GroovyJoin)1 DefaultCustomFunctionExplanation (eu.esdihumboldt.hale.common.align.custom.DefaultCustomFunctionExplanation)1 DefaultCustomPropertyFunction (eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunction)1 DefaultCustomPropertyFunctionEntity (eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunctionEntity)1 DefaultCustomPropertyFunctionParameter (eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunctionParameter)1 MergeUtil (eu.esdihumboldt.hale.common.align.merge.MergeUtil)1 AlignmentMigration (eu.esdihumboldt.hale.common.align.migrate.AlignmentMigration)1 AlignmentUtil (eu.esdihumboldt.hale.common.align.model.AlignmentUtil)1 Cell (eu.esdihumboldt.hale.common.align.model.Cell)1 CellUtil (eu.esdihumboldt.hale.common.align.model.CellUtil)1 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)1 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)1 JoinFunction (eu.esdihumboldt.hale.common.align.model.functions.JoinFunction)1