Search in sources :

Example 21 with IPentahoConnection

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

the class MDXBaseComponentTest method testGetConnectionOrig_nullConnectionProps.

@Test
public void testGetConnectionOrig_nullConnectionProps() throws Exception {
    doReturn(connAction).when(mdxBaseComponent).getActionDefinition();
    when(connAction.getMdxConnectionString()).thenReturn(connectionStringAction);
    when(connectionStringAction.getStringValue()).thenReturn("mdx:localhost:8080");
    when(connAction.getConnectionProps()).thenReturn(actionInput);
    when(actionInput.getValue()).thenReturn(null);
    when(connAction.getConnection()).thenReturn(jdbcAction);
    when(jdbcAction.getStringValue()).thenReturn("psql://localhost:5432");
    when(connAction.getJndi()).thenReturn(jndiAction);
    when(jndiAction.getStringValue()).thenReturn("jndi string");
    when(connAction.getLocation()).thenReturn(locationAction);
    when(locationAction.getStringValue()).thenReturn("location string");
    when(connAction.getRole()).thenReturn(roleAction);
    when(roleAction.getStringValue()).thenReturn("role string");
    when(connAction.getCatalog()).thenReturn(catalog);
    // when( catalog.getStringValue() ).thenReturn( "<catalog></catalog>" );
    when(catalog.getStringValue()).thenReturn(null);
    when(connAction.getCatalogResource()).thenReturn(catalogResource);
    when(catalogResource.getName()).thenReturn("catalog name");
    when(connAction.getUserId()).thenReturn(userAction);
    when(connAction.getPassword()).thenReturn(userAction);
    when(userAction.getStringValue()).thenReturn("user/pass");
    PentahoSystem.registerObject(mdxConnection);
    PentahoSystem.registerPrimaryObjectFactory(objFactory);
    PentahoSessionHolder.setSession(session);
    when(objFactory.get(any(Class.class), anyString(), any(IPentahoSession.class))).thenReturn(mdxConnection);
    mdxBaseComponent.setRuntimeContext(runtimeContext);
    when(runtimeContext.getResourceDefintion("catalog name")).thenReturn(catalogActionSeqRes);
    when(catalogActionSeqRes.getSourceType()).thenReturn(IActionSequenceResource.URL_RESOURCE);
    when(catalogActionSeqRes.getAddress()).thenReturn("sampledata");
    when(connAction.getExtendedColumnNames()).thenReturn(ActionInputConstant.NULL_INPUT);
    IPentahoConnection connectionOrig = mdxBaseComponent.getConnectionOrig();
    assertNotNull(connectionOrig);
    assertEquals(mdxConnection, connectionOrig);
}
Also used : IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Test(org.junit.Test)

Example 22 with IPentahoConnection

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

the class MDXBaseComponent 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) {
            // ignored
            }
            return con;
        } catch (Exception ex) {
        // ignored
        }
        waitFor(element);
    }
    con = getConnection();
    try {
        con.clearWarnings();
    } catch (Exception ex) {
    // ignore
    }
    return con;
}
Also used : IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException)

Example 23 with IPentahoConnection

use of org.pentaho.commons.connection.IPentahoConnection 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 24 with IPentahoConnection

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

the class ReportWizardSpecComponent method getResultSet.

public IPentahoResultSet getResultSet(final ReportSpec reportSpec) throws Exception {
    String jndiName = reportSpec.getReportSpecChoice().getJndiSource();
    IPentahoConnection connection = null;
    if (reportSpec.getIsMDX()) {
        // did this ever work??
        // $NON-NLS-1$
        String connectStr = "";
        IDBDatasourceService datasourceService = PentahoSystem.getObjectFactory().get(IDBDatasourceService.class, null);
        String dsName = datasourceService.getDSBoundName(jndiName);
        if (dsName != null) {
            // $NON-NLS-1$ //$NON-NLS-2$
            connectStr = "dataSource=" + dsName + "; Catalog=mondrian";
        } else {
            // $NON-NLS-1$
            error(Messages.getInstance().getErrorString("MDXBaseComponent.ERROR_0005_INVALID_CONNECTION"));
            return null;
        }
        Properties props = new Properties();
        props.setProperty(IPentahoConnection.CONNECTION, connectStr);
        props.setProperty(IPentahoConnection.PROVIDER, reportSpec.getMondrianCubeDefinitionPath());
        connection = PentahoConnectionFactory.getConnection(IPentahoConnection.MDX_DATASOURCE, props, getSession(), this);
    } else {
        connection = PentahoConnectionFactory.getConnection(IPentahoConnection.SQL_DATASOURCE, jndiName, getSession(), this);
    }
    String query = ReportParameterUtility.setupParametersForActionSequence(reportSpec.getQuery());
    query = setupQueryParameters(query);
    IPentahoResultSet res = connection.executeQuery(query);
    return res;
}
Also used : IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) Properties(java.util.Properties) IDBDatasourceService(org.pentaho.platform.api.data.IDBDatasourceService)

Example 25 with IPentahoConnection

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

the class JFreeReportGenerateDefinitionComponent method getResultSet.

public IPentahoResultSet getResultSet(final ReportSpec reportSpec) throws Exception {
    String jndiName = reportSpec.getReportSpecChoice().getJndiSource();
    IPentahoConnection connection = null;
    if (reportSpec.getIsMDX()) {
        // did this ever work??
        // $NON-NLS-1$
        String connectStr = "";
        IDBDatasourceService datasourceService = PentahoSystem.getObjectFactory().get(IDBDatasourceService.class, null);
        String dsName = datasourceService.getDSBoundName(jndiName);
        if (dsName != null) {
            // $NON-NLS-1$ //$NON-NLS-2$
            connectStr = "dataSource=" + dsName + "; Catalog=mondrian";
        } else {
            // $NON-NLS-1$
            error(Messages.getInstance().getErrorString("MDXBaseComponent.ERROR_0005_INVALID_CONNECTION"));
            return null;
        }
        Properties props = new Properties();
        props.setProperty(IPentahoConnection.CONNECTION, connectStr);
        props.setProperty(IPentahoConnection.PROVIDER, reportSpec.getMondrianCubeDefinitionPath());
        connection = PentahoConnectionFactory.getConnection(IPentahoConnection.MDX_DATASOURCE, props, getSession(), this);
    } else {
        connection = PentahoConnectionFactory.getConnection(IPentahoConnection.SQL_DATASOURCE, jndiName, getSession(), this);
    }
    String query = ReportParameterUtility.setupParametersForActionSequence(reportSpec.getQuery());
    query = setupQueryParameters(query);
    IPentahoResultSet res = connection.executeQuery(query);
    return res;
}
Also used : IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) Properties(java.util.Properties) IDBDatasourceService(org.pentaho.platform.api.data.IDBDatasourceService)

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