Search in sources :

Example 1 with IMetadataQueryExec

use of org.pentaho.metadata.model.IMetadataQueryExec in project pentaho-platform by pentaho.

the class MetadataQueryComponent method execute.

public boolean execute() {
    // get the xml parser
    QueryXmlHelper helper = null;
    try {
        helper = createQueryXmlHelper();
    } catch (Exception e) {
        // $NON-NLS-1$
        logger.error("error", e);
        return false;
    }
    // parse the metadata query
    IMetadataDomainRepository repo = PentahoSystem.get(IMetadataDomainRepository.class, null);
    if (queryObject == null) {
        // there is no query model, so create one from the query string
        // apply templates to the query
        String templatedQuery = null;
        if (inputs != null) {
            Properties properties = new Properties();
            for (String name : inputs.keySet()) {
                if (!(inputs.get(name) == null)) {
                    properties.put(name, inputs.get(name).toString());
                }
            }
            templatedQuery = TemplateUtil.applyTemplate(query, properties, null);
        } else {
            templatedQuery = query;
        }
        try {
            queryObject = helper.fromXML(repo, templatedQuery);
        } catch (Exception e) {
            // $NON-NLS-1$
            logger.error("error", e);
            return false;
        }
    }
    if (queryObject == null) {
        // $NON-NLS-1$
        logger.error("error query object null");
        return false;
    }
    // Can still be overridden in the action sequence
    if (timeout == null) {
        // $NON-NLS-1$
        Object timeoutProperty = queryObject.getLogicalModel().getProperty("timeout");
        if (timeoutProperty != null && timeoutProperty instanceof Number) {
            int timeoutVal = ((Number) timeoutProperty).intValue();
            this.setTimeout(timeoutVal);
        }
    }
    if (maxRows == null) {
        // $NON-NLS-1$
        Object maxRowsProperty = queryObject.getLogicalModel().getProperty("max_rows");
        if (maxRowsProperty != null && maxRowsProperty instanceof Number) {
            int maxRowsVal = ((Number) maxRowsProperty).intValue();
            this.setMaxRows(maxRowsVal);
        }
    }
    String queryExecName = queryObject.getLogicalModel().getPhysicalModel().getQueryExecName();
    String queryExecDefault = queryObject.getLogicalModel().getPhysicalModel().getDefaultQueryClassname();
    // String modelType = (String) inputs.get("modeltype");
    IMetadataQueryExec executor = PentahoSystem.get(IMetadataQueryExec.class, queryExecName, session);
    if (executor == null) {
        // get the executor from a plugin possibly?
        Class clazz;
        try {
            clazz = Class.forName(queryExecDefault, true, queryObject.getLogicalModel().getPhysicalModel().getClass().getClassLoader());
            executor = (IMetadataQueryExec) clazz.getConstructor(new Class[] {}).newInstance(new Object[] {});
        } catch (Exception e) {
            logger.warn(Messages.getInstance().getErrorString("MetadataQueryComponent.ERROR_0002_NO_EXECUTOR", // $NON-NLS-1$
            queryExecName));
        }
    }
    if (executor == null) {
        // the query exec class is not defined thru configuration, go with the default
        Class clazz;
        try {
            clazz = Class.forName(queryExecDefault);
            executor = (IMetadataQueryExec) clazz.getConstructor(new Class[] {}).newInstance(new Object[] {});
        } catch (Exception e) {
            logger.error(Messages.getInstance().getErrorString("MetadataQueryComponent.ERROR_0002_NO_EXECUTOR", // $NON-NLS-1$
            queryExecName));
            return false;
        }
    }
    // determine parameter values
    if (queryObject.getParameters() != null) {
        for (Parameter param : queryObject.getParameters()) {
            Object value = null;
            if (inputs != null) {
                value = inputs.get(param.getName());
            }
            executor.setParameter(param, value);
        }
    }
    try {
        executor.setDoQueryLog(logSql);
        executor.setForwardOnly(this.useForwardOnlyResultSet);
        executor.setMaxRows(this.maxRows);
        executor.setMetadataDomainRepository(repo);
        executor.setReadOnly(this.readOnly);
        executor.setTimeout(this.timeout);
        if (this.inputs != null) {
            executor.setInputs(this.inputs);
        }
        resultSet = executor.executeQuery(queryObject);
        if (resultSet != null && !live && executor.isLive()) {
            // read the results and cache them
            IPentahoResultSet cachedResultSet = resultSet.memoryCopy();
            resultSet.close();
            resultSet.closeConnection();
            resultSet = cachedResultSet;
        }
        return resultSet != null;
    } catch (Exception e) {
        // $NON-NLS-1$
        logger.error("error", e);
        throw new RuntimeException(e.getLocalizedMessage(), e);
    }
}
Also used : QueryXmlHelper(org.pentaho.metadata.query.model.util.QueryXmlHelper) IMetadataDomainRepository(org.pentaho.metadata.repository.IMetadataDomainRepository) Properties(java.util.Properties) IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) Parameter(org.pentaho.metadata.query.model.Parameter) IMetadataQueryExec(org.pentaho.metadata.model.IMetadataQueryExec)

Aggregations

Properties (java.util.Properties)1 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)1 IMetadataQueryExec (org.pentaho.metadata.model.IMetadataQueryExec)1 Parameter (org.pentaho.metadata.query.model.Parameter)1 QueryXmlHelper (org.pentaho.metadata.query.model.util.QueryXmlHelper)1 IMetadataDomainRepository (org.pentaho.metadata.repository.IMetadataDomainRepository)1