Search in sources :

Example 1 with QueryXmlHelper

use of org.pentaho.metadata.query.model.util.QueryXmlHelper 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)

Example 2 with QueryXmlHelper

use of org.pentaho.metadata.query.model.util.QueryXmlHelper in project data-access by pentaho.

the class MetadataService method doQuery.

/**
 * Executes a query model and returns a serializable result set
 *
 * @param query
 * @param rowLimit An optional row limit, -1 or null means all rows
 * @return
 */
public MarshallableResultSet doQuery(Query query, Integer rowLimit) {
    MetadataServiceUtil util = getMetadataServiceUtil();
    org.pentaho.metadata.query.model.Query fullQuery = util.convertQuery(query);
    QueryXmlHelper helper = new QueryXmlHelper();
    String xml = helper.toXML(fullQuery);
    return doXmlQuery(xml, rowLimit);
}
Also used : QueryXmlHelper(org.pentaho.metadata.query.model.util.QueryXmlHelper)

Example 3 with QueryXmlHelper

use of org.pentaho.metadata.query.model.util.QueryXmlHelper in project data-access by pentaho.

the class MetadataServiceUtil method getDomainObject.

/**
 * Returns the full domain obejct for a XML MQL query
 *
 * @param query
 * @return
 * @throws PentahoMetadataException
 */
public org.pentaho.metadata.model.Domain getDomainObject(String query) throws PentahoMetadataException {
    QueryXmlHelper helper = new QueryXmlHelper();
    IMetadataDomainRepository domainRepository = getMetadataRepository();
    org.pentaho.metadata.query.model.Query fatQuery = helper.fromXML(domainRepository, query);
    return fatQuery.getDomain();
}
Also used : QueryXmlHelper(org.pentaho.metadata.query.model.util.QueryXmlHelper) IMetadataDomainRepository(org.pentaho.metadata.repository.IMetadataDomainRepository)

Example 4 with QueryXmlHelper

use of org.pentaho.metadata.query.model.util.QueryXmlHelper in project data-access by pentaho.

the class MetadataService method getQueryXmlFromJson.

/**
 * Converts a JSON query into a full Query object by going via a thin Query object
 *
 * @param json
 * @return
 */
protected String getQueryXmlFromJson(String json) {
    MetadataServiceUtil util = getMetadataServiceUtil();
    Query query = util.deserializeJsonQuery(json);
    try {
        // convert the thin query model into a full one
        org.pentaho.metadata.query.model.Query fullQuery = util.convertQuery(query);
        // get the XML for the query
        QueryXmlHelper helper = new QueryXmlHelper();
        String xml = helper.toXML(fullQuery);
        return xml;
    } catch (Exception e) {
        // $NON-NLS-1$
        error(Messages.getErrorString("MetadataService.ERROR_0008_BAD_QUERY"), e);
    }
    return null;
}
Also used : Query(org.pentaho.platform.dataaccess.metadata.model.impl.Query) QueryXmlHelper(org.pentaho.metadata.query.model.util.QueryXmlHelper) JSONException(org.json.JSONException) PentahoMetadataException(org.pentaho.pms.core.exception.PentahoMetadataException) IOException(java.io.IOException)

Aggregations

QueryXmlHelper (org.pentaho.metadata.query.model.util.QueryXmlHelper)4 IMetadataDomainRepository (org.pentaho.metadata.repository.IMetadataDomainRepository)2 IOException (java.io.IOException)1 Properties (java.util.Properties)1 JSONException (org.json.JSONException)1 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)1 IMetadataQueryExec (org.pentaho.metadata.model.IMetadataQueryExec)1 Parameter (org.pentaho.metadata.query.model.Parameter)1 Query (org.pentaho.platform.dataaccess.metadata.model.impl.Query)1 PentahoMetadataException (org.pentaho.pms.core.exception.PentahoMetadataException)1