Search in sources :

Example 11 with IPentahoConnection

use of org.pentaho.commons.connection.IPentahoConnection in project pentaho-platform by pentaho.

the class ChartContentGenerator method getContent.

@Override
public String getContent() throws Exception {
    // $NON-NLS-1$
    int chartType = (int) requestParameters.getLongParameter("ChartType", JFreeChartEngine.UNDEFINED_CHART_TYPE);
    // $NON-NLS-1$
    String chartDefinitionPath = requestParameters.getStringParameter("ChartDefinitionPath", null);
    ArrayList messages = new ArrayList();
    CategoryDatasetChartComponent barChart = new CategoryDatasetChartComponent(chartType, chartDefinitionPath, 600, 400, urlFactory, messages);
    String content = null;
    IPentahoConnection connection = // $NON-NLS-1$
    PentahoConnectionFactory.getConnection(IPentahoConnection.SQL_DATASOURCE, "SampleData", userSession, this);
    try {
        // $NON-NLS-1$
        String query = "select department, actual, budget, variance from QUADRANT_ACTUALS";
        IPentahoResultSet results = connection.executeQuery(query);
        try {
            barChart.setValues(results);
            barChart.validate(userSession, null);
            barChart.setParameterProvider(IParameterProvider.SCOPE_REQUEST, requestParameters);
            barChart.setParameterProvider(IParameterProvider.SCOPE_SESSION, sessionParameters);
            // $NON-NLS-1$
            content = barChart.getContent("text/html");
        } finally {
            results.close();
        }
    } finally {
        connection.close();
    }
    return content;
}
Also used : CategoryDatasetChartComponent(org.pentaho.platform.uifoundation.chart.CategoryDatasetChartComponent) IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) ArrayList(java.util.ArrayList)

Example 12 with IPentahoConnection

use of org.pentaho.commons.connection.IPentahoConnection in project pentaho-platform by pentaho.

the class SQLBaseComponent method getDatasourceConnection.

/**
 * attempt to aquire a connection. if connection isn't available, wait a certain period of time before trying again.
 *
 * @return connection
 */
public IPentahoConnection getDatasourceConnection() {
    IPentahoConnection con;
    int[] timeouts = { 200, 500, 2000 };
    for (int element : timeouts) {
        try {
            con = getConnection();
            try {
                con.clearWarnings();
            } catch (Exception ex) {
            // ignore
            }
            return con;
        } catch (Exception ex) {
        // ignore
        }
        waitFor(element);
    }
    con = getConnection();
    try {
        con.clearWarnings();
    } catch (Exception ex) {
    // ignore
    }
    return con;
}
Also used : IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection)

Example 13 with IPentahoConnection

use of org.pentaho.commons.connection.IPentahoConnection in project pentaho-platform by pentaho.

the class SQLBaseComponent method getConnection.

/**
 * This method retrieves a connection based on the components inputs.
 *
 * @param defaultConnection
 *          a default connection to use if no other is available
 * @return new connection object
 */
protected IPentahoConnection getConnection(final IPentahoConnection defaultConnection) {
    IPentahoConnection localConnection = null;
    try {
        String jndiName = null;
        String driver = null;
        String userId = null;
        String password = null;
        String connectionInfo = null;
        if (getActionDefinition() instanceof SqlConnectionAction) {
            SqlConnectionAction sqlConnectionAction = (SqlConnectionAction) getActionDefinition();
            jndiName = sqlConnectionAction.getJndi().getStringValue();
            driver = sqlConnectionAction.getDriver().getStringValue();
            userId = sqlConnectionAction.getUserId().getStringValue();
            password = sqlConnectionAction.getPassword().getStringValue();
            connectionInfo = sqlConnectionAction.getDbUrl().getStringValue();
        } else if (getActionDefinition() instanceof AbstractRelationalDbAction) {
            AbstractRelationalDbAction relationalDbAction = (AbstractRelationalDbAction) getActionDefinition();
            jndiName = relationalDbAction.getJndi().getStringValue();
            driver = relationalDbAction.getDriver().getStringValue();
            userId = relationalDbAction.getUserId().getStringValue();
            password = relationalDbAction.getPassword().getStringValue();
            connectionInfo = relationalDbAction.getDbUrl().getStringValue();
        }
        if (jndiName != null) {
            localConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.SQL_DATASOURCE, jndiName, getSession(), this);
        }
        if (localConnection == null) {
            localConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.SQL_DATASOURCE, driver, connectionInfo, userId, password, getSession(), this);
        }
        if (localConnection == null) {
            if (defaultConnection == null) {
                // $NON-NLS-1$
                error(Messages.getInstance().getErrorString("SQLBaseComponent.ERROR_0005_INVALID_CONNECTION"));
                return null;
            } else {
                localConnection = defaultConnection;
            }
        }
        return localConnection;
    } catch (Exception e) {
        // $NON-NLS-1$
        error(Messages.getInstance().getErrorString("SQLBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName()), e);
    }
    return null;
}
Also used : IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) SqlConnectionAction(org.pentaho.actionsequence.dom.actions.SqlConnectionAction) AbstractRelationalDbAction(org.pentaho.actionsequence.dom.actions.AbstractRelationalDbAction)

Example 14 with IPentahoConnection

use of org.pentaho.commons.connection.IPentahoConnection in project pentaho-platform by pentaho.

the class SQLBaseComponent method executeAction.

/**
 * determines state of component, and executes accordingly.
 *
 * various inputs that impact the state include:
 *
 * live - returns a live result set vs. an in memory copy transform - transform a result set based on additional
 * inputs prepared_component - if available, use existing connection from prepared component max_rows - sets the
 * number of rows that should be returned in result sets
 *
 * The specified output also impacts the state of the execution. If prepared_component is defined as an output, setup
 * the query but delay execution.
 */
@Override
protected boolean executeAction() {
    IActionDefinition actionDefinition = getActionDefinition();
    try {
        if (actionDefinition instanceof AbstractRelationalDbAction) {
            AbstractRelationalDbAction relationalDbAction = (AbstractRelationalDbAction) actionDefinition;
            // Added by Arijit Chatterjee
            IActionInput queryTimeoutInput = relationalDbAction.getQueryTimeout();
            IActionInput maxRowsInput = relationalDbAction.getMaxRows();
            IActionInput readOnlyInput = relationalDbAction.getReadOnly();
            String baseQuery = getQuery();
            if (baseQuery == null) {
                error(Messages.getInstance().getErrorString("SQLBaseComponent.ERROR_0001_QUERY_NOT_SPECIFIED", // $NON-NLS-1$
                actionDefinition.getDescription()));
                return false;
            }
            IPreparedComponent sharedConnection = (IPreparedComponent) relationalDbAction.getSharedConnection().getValue();
            if (readOnlyInput != ActionInputConstant.NULL_INPUT) {
                this.setReadOnly(readOnlyInput.getBooleanValue());
            }
            if (sharedConnection != null) {
                connectionOwner = false;
                IPentahoConnection conn = sharedConnection.shareConnection();
                if (conn == null) {
                    error(Messages.getInstance().getErrorString("IPreparedComponent.ERROR_0002_CONNECTION_NOT_AVAILABLE", // $NON-NLS-1$
                    getActionName()));
                    return false;
                } else if (conn.getDatasourceType() == IPentahoConnection.SQL_DATASOURCE) {
                    connection = conn;
                } else {
                    error(Messages.getInstance().getErrorString("IPreparedComponent.ERROR_0001_INVALID_CONNECTION_TYPE", // $NON-NLS-1$
                    getActionName()));
                    return false;
                }
            } else {
                dispose();
                connection = getDatasourceConnection();
            }
            if (connection == null) {
                return false;
            }
            // query and set this component as the output. This query will be run later from a subreport.
            if (relationalDbAction.getOutputPreparedStatement() != null) {
                prepareQuery(baseQuery);
                IActionOutput actionOutput = relationalDbAction.getOutputPreparedStatement();
                if (actionOutput != null) {
                    actionOutput.setValue(this);
                }
                return true;
            }
            // int maxRows = relationalDbAction.getMaxRows().getIntValue(-1);
            if (maxRowsInput != ActionInputConstant.NULL_INPUT) {
                this.setMaxRows(maxRowsInput.getIntValue());
            }
            // Added by Arijit Chatterjee.Sets the value of timeout. Default is -1, if parameter not found.
            if (queryTimeoutInput != ActionInputConstant.NULL_INPUT) {
                this.setQueryTimeout(queryTimeoutInput.getIntValue());
            }
            if (relationalDbAction.getPerformTransform().getBooleanValue(false)) {
                // The side effect of
                runQuery(baseQuery, false);
                // transform rSet here
                rSet = PentahoDataTransmuter.crossTab(rSet, relationalDbAction.getTransformPivotColumn().getIntValue(-1) - 1, relationalDbAction.getTransformMeasuresColumn().getIntValue(-1) - 1, relationalDbAction.getTransformSortColumn().getIntValue(0) - 1, (Format) relationalDbAction.getTransformPivotDataFormat().getValue(), (Format) relationalDbAction.getTransformSortDataFormat().getValue(), relationalDbAction.getTransformOrderOutputColumns().getBooleanValue(false));
                IActionOutput actionOutput = relationalDbAction.getOutputResultSet();
                if (actionOutput != null) {
                    actionOutput.setValue(rSet);
                }
                return true;
            } else {
                return runQuery(baseQuery, relationalDbAction.getLive().getBooleanValue(false));
            }
        } else if (actionDefinition instanceof SqlConnectionAction) {
            SqlConnectionAction sqlConnectionAction = (SqlConnectionAction) actionDefinition;
            dispose();
            connection = getDatasourceConnection();
            if (connection == null) {
                return false;
            } else {
                IActionOutput actionOutput = sqlConnectionAction.getOutputConnection();
                if (actionOutput != null) {
                    actionOutput.setValue(this);
                    return true;
                } else {
                    return false;
                }
            }
        }
    } catch (Exception e) {
        // $NON-NLS-1$
        error(Messages.getInstance().getErrorString("SQLBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName()), e);
    }
    return false;
}
Also used : IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) Format(java.text.Format) IActionInput(org.pentaho.actionsequence.dom.IActionInput) IActionDefinition(org.pentaho.actionsequence.dom.IActionDefinition) IActionOutput(org.pentaho.actionsequence.dom.IActionOutput) SqlConnectionAction(org.pentaho.actionsequence.dom.actions.SqlConnectionAction) IPreparedComponent(org.pentaho.platform.api.data.IPreparedComponent) AbstractRelationalDbAction(org.pentaho.actionsequence.dom.actions.AbstractRelationalDbAction)

Example 15 with IPentahoConnection

use of org.pentaho.commons.connection.IPentahoConnection in project pentaho-platform by pentaho.

the class MDXBaseComponent method getConnectionOrig.

protected IPentahoConnection getConnectionOrig() {
    IPentahoConnection localConnection = null;
    MdxConnectionAction connAction = (MdxConnectionAction) getActionDefinition();
    try {
        String mdxConnectionStr = connAction.getMdxConnectionString().getStringValue();
        Properties mdxConnectionProps = (Properties) connAction.getConnectionProps().getValue();
        String jdbcStr = connAction.getConnection().getStringValue();
        String jndiStr = connAction.getJndi().getStringValue();
        String location = connAction.getLocation().getStringValue();
        String role = connAction.getRole().getStringValue();
        String catalog = connAction.getCatalog().getStringValue();
        if ((catalog == null) && (connAction.getCatalogResource() != null)) {
            IActionSequenceResource resource = getResource(connAction.getCatalogResource().getName());
            catalog = resource.getAddress();
            if (resource.getSourceType() == IActionSequenceResource.URL_RESOURCE) {
                if (!catalog.startsWith("solution:") && !catalog.startsWith("http:")) {
                    // $NON-NLS-1$
                    if (fileExistsInRepository(catalog)) {
                        // About allowed "solution:"
                        // Extra step to make sure that remote mondrian models
                        // fully qualified aren't munged
                        // MB
                        // $NON-NLS-1$
                        catalog = "solution:" + catalog;
                    }
                }
            } else if ((resource.getSourceType() == IActionSequenceResource.SOLUTION_FILE_RESOURCE) || (resource.getSourceType() == IActionSequenceResource.FILE_RESOURCE)) {
                if (!catalog.startsWith("solution:")) {
                    // $NON-NLS-1$
                    catalog = "solution:" + catalog;
                }
            }
        }
        if (catalog == null) {
            // $NON-NLS-1$
            warn(Messages.getInstance().getString("MDXBaseComponent.ERROR_0007_CATALOG_NOT_DEFINED", getActionName()));
        } else {
            if (mdxConnectionProps != null) {
                mdxConnectionProps.put(MdxConnectionAction.CATALOG_ELEMENT, catalog);
            }
        }
        String userId = connAction.getUserId().getStringValue();
        String password = connAction.getPassword().getStringValue();
        if (mdxConnectionProps != null) {
            localConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.MDX_DATASOURCE, mdxConnectionProps, getSession(), this);
        } else {
            if (mdxConnectionStr != null) {
                localConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.MDX_DATASOURCE, mdxConnectionStr, getSession(), this);
            } else {
                String connectStr = null;
                if (jdbcStr != null) {
                    // $NON-NLS-1$
                    connectStr = jdbcStr + "; Catalog=" + catalog;
                } else if (jndiStr != null) {
                    IDBDatasourceService datasourceService = PentahoSystem.getObjectFactory().get(IDBDatasourceService.class, null);
                    if (datasourceService.getDataSource(jndiStr) == null) {
                        // $NON-NLS-1$
                        error(Messages.getInstance().getErrorString("MDXBaseComponent.ERROR_0005_INVALID_CONNECTION"));
                        return null;
                    }
                    // $NON-NLS-1$ //$NON-NLS-2$
                    connectStr = "dataSource=" + jndiStr + "; Catalog=" + catalog;
                    // Add extra definitions from platform mondrian metadata
                    MondrianCatalog mc = getMondrianCatalog(catalog);
                    try {
                        connectStr += ";" + mc.getDataSourceInfo();
                    } catch (Exception e) {
                    // Just swallow the exception
                    }
                }
                if (role != null) {
                    // $NON-NLS-1$
                    connectStr += "; Role=" + role;
                }
                Properties props = new Properties();
                props.setProperty(IPentahoConnection.CONNECTION, connectStr);
                props.setProperty(IPentahoConnection.PROVIDER, location);
                if (userId != null) {
                    props.setProperty(IPentahoConnection.USERNAME_KEY, userId);
                }
                if (password != null) {
                    props.setProperty(IPentahoConnection.PASSWORD_KEY, password);
                }
                localConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.MDX_DATASOURCE, props, getSession(), this);
            }
            if (localConnection == null) {
                // $NON-NLS-1$
                error(Messages.getInstance().getErrorString("MDXBaseComponent.ERROR_0005_INVALID_CONNECTION"));
                return null;
            }
        }
        if (localConnection instanceof MDXConnection) {
            MDXConnection mdxConn = (MDXConnection) localConnection;
            if (connAction != null) {
                if ((connAction.getExtendedColumnNames() != ActionInputConstant.NULL_INPUT)) {
                    mdxConn.setUseExtendedColumnNames(connAction.getExtendedColumnNames().getBooleanValue());
                }
            }
        }
        return localConnection;
    } catch (Exception e) {
        // $NON-NLS-1$
        error(Messages.getInstance().getErrorString("MDXBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName()), e);
    }
    return null;
}
Also used : IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) MDXConnection(org.pentaho.platform.plugin.services.connections.mondrian.MDXConnection) MondrianCatalog(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog) MdxConnectionAction(org.pentaho.actionsequence.dom.actions.MdxConnectionAction) RolapConnectionProperties(mondrian.rolap.RolapConnectionProperties) Properties(java.util.Properties) IDBDatasourceService(org.pentaho.platform.api.data.IDBDatasourceService) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource)

Aggregations

IPentahoConnection (org.pentaho.commons.connection.IPentahoConnection)29 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)11 Test (org.junit.Test)9 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)9 Properties (java.util.Properties)6 OutputStream (java.io.OutputStream)5 StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)5 File (java.io.File)4 IDBDatasourceService (org.pentaho.platform.api.data.IDBDatasourceService)3 IPreparedComponent (org.pentaho.platform.api.data.IPreparedComponent)3 UnifiedRepositoryException (org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException)3 IActionOutput (org.pentaho.actionsequence.dom.IActionOutput)2 AbstractRelationalDbAction (org.pentaho.actionsequence.dom.actions.AbstractRelationalDbAction)2 MdxConnectionAction (org.pentaho.actionsequence.dom.actions.MdxConnectionAction)2 SqlConnectionAction (org.pentaho.actionsequence.dom.actions.SqlConnectionAction)2 DatabaseDialectException (org.pentaho.database.DatabaseDialectException)2 ConnectionServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException)2 FileInputStream (java.io.FileInputStream)1 Format (java.text.Format)1 ArrayList (java.util.ArrayList)1