Search in sources :

Example 1 with IPeekable

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

the class RuntimeContext method executeLoop.

private void executeLoop(final IActionParameter loopParm, final IPentahoResultSet loopSet, final IActionSequence sequence, final IActionCompleteListener doneListener, final IExecutionListener execListener, final boolean async, boolean peekOnly) throws ActionSequenceException {
    // execute the actions
    int loopCount = -1;
    // to the first record. This is to resolve multiple levels of looping on resultset.
    if (loopSet.isScrollable()) {
        loopSet.beforeFirst();
    }
    if (peekOnly && !(loopSet instanceof IPeekable)) {
        throw new ActionExecutionException(Messages.getInstance().getErrorString(// $NON-NLS-1$
        "RuntimeContext.ERROR_0033_NOT_PEEKABLE"), session.getName(), instanceId, getActionSequence().getSequenceName(), null);
    }
    Object[] row = peekOnly ? ((IPeekable) loopSet).peek() : loopSet.next();
    Object[][] headerSet = loopSet.getMetaData().getColumnHeaders();
    // TODO handle OLAP result sets
    Object[] headers = headerSet[0];
    while (row != null) {
        loopCount++;
        if (RuntimeContext.debug) {
            debug(Messages.getInstance().getString("RuntimeContext.DEBUG_EXECUTING_ACTION", // $NON-NLS-1$
            Integer.toString(loopCount)));
        }
        if (execListener != null) {
            execListener.loop(this, loopCount);
        }
        if (loopParm != null) {
            IActionParameter ap;
            for (int columnNo = 0; columnNo < headers.length; columnNo++) {
                String name = headers[columnNo].toString();
                Object value = row[columnNo];
                String type = null;
                if (value instanceof String) {
                    type = IActionParameter.TYPE_STRING;
                } else if (value instanceof Date) {
                    type = IActionParameter.TYPE_DATE;
                } else if ((value instanceof Long) || (value instanceof Integer)) {
                    type = IActionParameter.TYPE_INTEGER;
                } else if ((value instanceof BigDecimal) || (value instanceof Double) || (value instanceof Float)) {
                    type = IActionParameter.TYPE_DECIMAL;
                } else if (value instanceof String[]) {
                    type = IActionParameter.TYPE_STRING;
                } else if (value == null) {
                    // $NON-NLS-1$
                    warn(Messages.getInstance().getString("RuntimeContext.WARN_VARIABLE_IN_LOOP_IS_NULL", name));
                } else {
                    type = IActionParameter.TYPE_OBJECT;
                    warn(Messages.getInstance().getString("RuntimeContext.WARN_VARIABLE_IN_LOOP_NOT_RECOGNIZED", name, // $NON-NLS-1$
                    value.getClass().toString()));
                }
                // TODO make sure any previous loop values are removed
                ap = paramManager.getInput(name);
                if (ap == null) {
                    ap = new ActionParameter(name, type, value, null, null);
                    addInputParameter(name, ap);
                } else {
                    ap.dispose();
                    ap.setValue(value);
                }
            }
        }
        try {
            performActions(sequence, doneListener, execListener, async);
        } catch (ActionSequenceException e) {
            e.setLoopIndex(loopCount);
            throw e;
        }
        row = peekOnly ? ((IPeekable) loopSet).peek() : loopSet.next();
    }
    status = IRuntimeContext.RUNTIME_STATUS_SUCCESS;
}
Also used : ActionSequenceException(org.pentaho.platform.api.engine.ActionSequenceException) ActionParameter(org.pentaho.platform.engine.services.actionsequence.ActionParameter) IActionParameter(org.pentaho.platform.api.engine.IActionParameter) Date(java.util.Date) BigDecimal(java.math.BigDecimal) ActionExecutionException(org.pentaho.platform.api.engine.ActionExecutionException) IActionParameter(org.pentaho.platform.api.engine.IActionParameter) IPeekable(org.pentaho.commons.connection.IPeekable)

Example 2 with IPeekable

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

the class XQueryIT method testPeek.

public void testPeek() throws Exception {
    XQConnection connection = new XQConnection();
    IPentahoResultSet data = connection.executeQuery(TEST_QUERY);
    assertNotNull("result set is null", data);
    assertTrue("result set is wrong type", data instanceof XQResultSet);
    assertEquals("row count is wrong", 4, data.getRowCount());
    assertEquals("column header is wrong", "title", data.getMetaData().getColumnHeaders()[0][0]);
    assertEquals("column header is wrong", "author", data.getMetaData().getColumnHeaders()[0][1]);
    assertEquals("column header is wrong", "year", data.getMetaData().getColumnHeaders()[0][2]);
    assertEquals("column header is wrong", "price", data.getMetaData().getColumnHeaders()[0][3]);
    assertTrue("result set is not peekable", data instanceof IPeekable);
    IPeekable peekable = (IPeekable) data;
    Object[] row = peekable.peek();
    assertEquals("Everyday Italian", row[0]);
    assertEquals("Giada De Laurentiis", row[1]);
    assertEquals("2005", row[2]);
    assertEquals("30.00", row[3]);
    row = peekable.peek();
    assertEquals("Everyday Italian", row[0]);
    row = peekable.peek();
    assertEquals("Everyday Italian", row[0]);
    row = peekable.peek();
    assertEquals("Everyday Italian", row[0]);
    row = data.next();
    assertEquals("Everyday Italian", row[0]);
    assertEquals("Giada De Laurentiis", row[1]);
    assertEquals("2005", row[2]);
    assertEquals("30.00", row[3]);
    row = peekable.peek();
    assertEquals("Harry Potter", row[0]);
    assertEquals("J K. Rowling", row[1]);
    assertEquals("2005", row[2]);
    assertEquals("29.99", row[3]);
    row = peekable.peek();
    assertEquals("Harry Potter", row[0]);
    assertEquals("J K. Rowling", row[1]);
    assertEquals("2005", row[2]);
    assertEquals("29.99", row[3]);
    row = data.next();
    assertEquals("Harry Potter", row[0]);
    assertEquals("J K. Rowling", row[1]);
    assertEquals("2005", row[2]);
    assertEquals("29.99", row[3]);
    row = peekable.peek();
    assertNotNull(row);
    row = data.next();
    assertNotNull(row);
    row = peekable.peek();
    assertNotNull(row);
    row = data.next();
    assertNotNull(row);
    row = peekable.peek();
    assertNull(row);
    row = data.next();
    assertNull(row);
    row = peekable.peek();
    assertNull(row);
}
Also used : IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) XQConnection(org.pentaho.platform.plugin.services.connections.xquery.XQConnection) XQResultSet(org.pentaho.platform.plugin.services.connections.xquery.XQResultSet) IPeekable(org.pentaho.commons.connection.IPeekable)

Aggregations

IPeekable (org.pentaho.commons.connection.IPeekable)2 BigDecimal (java.math.BigDecimal)1 Date (java.util.Date)1 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)1 ActionExecutionException (org.pentaho.platform.api.engine.ActionExecutionException)1 ActionSequenceException (org.pentaho.platform.api.engine.ActionSequenceException)1 IActionParameter (org.pentaho.platform.api.engine.IActionParameter)1 ActionParameter (org.pentaho.platform.engine.services.actionsequence.ActionParameter)1 XQConnection (org.pentaho.platform.plugin.services.connections.xquery.XQConnection)1 XQResultSet (org.pentaho.platform.plugin.services.connections.xquery.XQResultSet)1