Search in sources :

Example 11 with IPreparedComponent

use of org.pentaho.platform.api.data.IPreparedComponent in project pentaho-platform by pentaho.

the class MDXBaseComponent method executeAction.

@Override
protected boolean executeAction() {
    boolean value = false;
    /*
     * This is the query part. You would need a connection to execute the query. The connection will either come in as
     * an INPUT (prepared_component) or will be specified right there.
     * 
     * So check if a prepared component exists, if not create a new connection. If connection is not null, proceed to
     * work on the query part.
     * 
     * In the query section you can either execute the query right away or prepare it to be used later by a sub report.
     */
    try {
        if (getActionDefinition() instanceof MdxQueryAction) {
            MdxQueryAction queryAction = (MdxQueryAction) getActionDefinition();
            // instead of creating our own.
            if (queryAction.getMdxConnection() != ActionInputConstant.NULL_INPUT) {
                if (queryAction.getMdxConnection().getValue() != null) {
                    connectionOwner = false;
                    IPreparedComponent component = (IPreparedComponent) queryAction.getMdxConnection().getValue();
                    IPentahoConnection conn = component.shareConnection();
                    if (conn.getDatasourceType() == IPentahoConnection.MDX_DATASOURCE) {
                        connection = conn;
                    } else {
                        error(Messages.getInstance().getErrorString("IPreparedComponent.ERROR_0001_INVALID_CONNECTION_TYPE", // $NON-NLS-1$
                        getActionName()));
                    }
                } else {
                    error(Messages.getInstance().getErrorString("IPreparedComponent.ERROR_0002_CONNECTION_NOT_AVAILABLE", // $NON-NLS-1$
                    getActionName()));
                }
            } else {
                dispose();
                connection = getDatasourceConnection();
            }
            if (connection != null) {
                String query = queryAction.getQuery().getStringValue();
                if (queryAction.getOutputPreparedStatement() != null) {
                    // prepare the query for execution, but don't execute quite yet.
                    prepareQuery(query);
                    // set the output as self, which will be used later by another component.
                    setOutputValue(IPreparedComponent.PREPARED_COMPONENT_NAME, this);
                    value = true;
                } else {
                    value = runQuery(connection, query);
                }
            } else {
                error(Messages.getInstance().getErrorString("IPreparedComponent.ERROR_0004_NO_CONNECTION_INFO", // $NON-NLS-1$
                getActionName()));
            }
        } else if (getActionDefinition() instanceof MdxConnectionAction) {
            dispose();
            connection = getDatasourceConnection();
            if (connection != null) {
                setOutputValue(IPreparedComponent.PREPARED_COMPONENT_NAME, this);
                value = true;
            }
        } else {
            error(Messages.getInstance().getErrorString("MDXBaseComponent.ERROR_0004_VALIDATION_FAILED", // $NON-NLS-1$
            getActionName()));
        }
    } catch (Exception e) {
        // $NON-NLS-1$
        error(Messages.getInstance().getErrorString("MDXBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName()), e);
    }
    return value;
}
Also used : IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) MdxConnectionAction(org.pentaho.actionsequence.dom.actions.MdxConnectionAction) MdxQueryAction(org.pentaho.actionsequence.dom.actions.MdxQueryAction) IPreparedComponent(org.pentaho.platform.api.data.IPreparedComponent) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException)

Example 12 with IPreparedComponent

use of org.pentaho.platform.api.data.IPreparedComponent in project pentaho-platform by pentaho.

the class PentahoTableDataFactory method queryData.

/**
 * Queries a datasource. The string 'query' defines the name of the query. The Parameterset given here may contain
 * more data than actually needed.
 * <p/>
 * The dataset may change between two calls, do not assume anything!
 *
 * @param query
 *          the name of the table.
 * @param parameters
 *          are ignored for this factory.
 * @return the report data or null.
 */
public TableModel queryData(final String query, final DataRow parameters) {
    TableModel model = tables.get(query);
    if (model == null) {
        final IPreparedComponent component = components.get(query);
        if (component != null) {
            final HashMap<String, Object> map = new HashMap<String, Object>();
            if (parameters != null) {
                String[] columnNames = parameters.getColumnNames();
                for (String columnName : columnNames) {
                    map.put(columnName, parameters.get(columnName));
                }
            }
            final IPentahoResultSet rs = component.executePrepared(map);
            model = new PentahoTableModel(rs);
        }
    }
    return model;
}
Also used : IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) HashMap(java.util.HashMap) CloseableTableModel(org.pentaho.reporting.engine.classic.core.util.CloseableTableModel) TableModel(javax.swing.table.TableModel) IPreparedComponent(org.pentaho.platform.api.data.IPreparedComponent)

Aggregations

IPreparedComponent (org.pentaho.platform.api.data.IPreparedComponent)12 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)5 IActionParameter (org.pentaho.platform.api.engine.IActionParameter)5 IRuntimeContext (org.pentaho.platform.api.engine.IRuntimeContext)5 TableModel (javax.swing.table.TableModel)3 IActionInput (org.pentaho.actionsequence.dom.IActionInput)3 IPentahoConnection (org.pentaho.commons.connection.IPentahoConnection)3 HashMap (java.util.HashMap)2 IActionDefinition (org.pentaho.actionsequence.dom.IActionDefinition)2 IActionOutput (org.pentaho.actionsequence.dom.IActionOutput)2 PentahoTableDataFactory (org.pentaho.platform.plugin.action.jfreereport.helper.PentahoTableDataFactory)2 PentahoTableModel (org.pentaho.platform.plugin.action.jfreereport.helper.PentahoTableModel)2 Format (java.text.Format)1 Iterator (java.util.Iterator)1 ActionInput (org.pentaho.actionsequence.dom.ActionInput)1 AbstractRelationalDbAction (org.pentaho.actionsequence.dom.actions.AbstractRelationalDbAction)1 HQLConnectionAction (org.pentaho.actionsequence.dom.actions.HQLConnectionAction)1 HQLQueryAction (org.pentaho.actionsequence.dom.actions.HQLQueryAction)1 JFreeReportAction (org.pentaho.actionsequence.dom.actions.JFreeReportAction)1 MdxConnectionAction (org.pentaho.actionsequence.dom.actions.MdxConnectionAction)1