use of org.pentaho.platform.plugin.services.connections.javascript.JavaScriptResultSet in project pentaho-platform by pentaho.
the class JavaScriptResultSetIT method testPeek.
public void testPeek() {
MemoryMetaData metadata = new MemoryMetaData(new String[][] { { "col1", "col2" } }, null);
MemoryResultSet dataSet = new MemoryResultSet(metadata);
JavaScriptResultSet data = new JavaScriptResultSet();
data.setResultSet(dataSet);
data.addRow(new Object[] { "a", new Integer(1) });
data.addRow(new Object[] { "b", new Integer(2) });
data.addRow(new Object[] { "c", new Integer(3) });
Object[] row = data.peek();
assertEquals("a", row[0]);
assertEquals(1, row[1]);
row = data.peek();
assertEquals("a", row[0]);
assertEquals(1, row[1]);
row = data.peek();
assertEquals("a", row[0]);
assertEquals(1, row[1]);
row = data.next();
assertEquals("a", row[0]);
assertEquals(1, row[1]);
row = data.peek();
assertEquals("b", row[0]);
assertEquals(2, row[1]);
row = data.peek();
assertEquals("b", row[0]);
assertEquals(2, row[1]);
row = data.next();
assertEquals("b", row[0]);
assertEquals(2, row[1]);
row = data.peek();
assertEquals("c", row[0]);
assertEquals(3, row[1]);
row = data.peek();
assertEquals("c", row[0]);
assertEquals(3, row[1]);
row = data.next();
assertEquals("c", row[0]);
assertEquals(3, row[1]);
row = data.peek();
assertNull(row);
row = data.peek();
assertNull(row);
row = data.peek();
assertNull(row);
row = data.next();
assertNull(row);
data.beforeFirst();
row = data.peek();
assertEquals("a", row[0]);
assertEquals(1, row[1]);
}
use of org.pentaho.platform.plugin.services.connections.javascript.JavaScriptResultSet in project pentaho-platform by pentaho.
the class JavaScriptResultSetIT method testAddRow.
public void testAddRow() {
MemoryMetaData metadata = new MemoryMetaData(new String[][] { { "col1", "col2" } }, null);
MemoryResultSet dataSet = new MemoryResultSet(metadata);
JavaScriptResultSet data = new JavaScriptResultSet();
data.setResultSet(dataSet);
data.addRow(new Object[] { "a", new Integer(1) });
data.addRow(new Object[] { "b", new Integer(2) });
data.addRow(new Object[] { "c", new Integer(3) });
assertEquals(3, data.getRowCount());
assertEquals("a", data.getValueAt(0, 0));
assertEquals(1, data.getValueAt(0, 1));
assertEquals("b", data.getValueAt(1, 0));
assertEquals(2, data.getValueAt(1, 1));
assertEquals("c", data.getValueAt(2, 0));
assertEquals(3, data.getValueAt(2, 1));
}
use of org.pentaho.platform.plugin.services.connections.javascript.JavaScriptResultSet in project pentaho-platform by pentaho.
the class ConditionalExecution method shouldExecute.
public boolean shouldExecute(final Map currentInputs, final Log logger) throws Exception {
boolean shouldExecute = true;
Context cx = ContextFactory.getGlobal().enterContext();
try {
ScriptableObject scriptable = new RhinoScriptable();
// initialize the standard javascript objects
Scriptable scope = cx.initStandardObjects(scriptable);
ScriptableObject.defineClass(scope, JavaScriptResultSet.class);
Object inputValue;
IActionParameter inputParameter;
String inputName;
Iterator inputs = currentInputs.entrySet().iterator();
Map.Entry mapEntry;
while (inputs.hasNext()) {
mapEntry = (Map.Entry) inputs.next();
inputName = (String) mapEntry.getKey();
if (inputName.indexOf('-') >= 0) {
// $NON-NLS-1$
logger.info("Ignoring Input: " + inputName);
continue;
}
inputParameter = (IActionParameter) mapEntry.getValue();
inputValue = inputParameter.getValue();
Object wrapper;
if (inputValue instanceof IPentahoResultSet) {
JavaScriptResultSet results = new JavaScriptResultSet();
// Required as of Rhino 1.7R1 to resolve caching, base object
// inheritance and property tree
results.setPrototype(scriptable);
results.setResultSet((IPentahoResultSet) inputValue);
wrapper = Context.javaToJS(inputValue, results);
} else {
wrapper = Context.javaToJS(inputValue, scope);
}
ScriptableObject.putProperty(scope, inputName, wrapper);
}
Object wrappedOut = Context.javaToJS(System.out, scope);
Object wrappedThis = Context.javaToJS(this, scope);
// $NON-NLS-1$
ScriptableObject.putProperty(scope, "out", wrappedOut);
// $NON-NLS-1$
ScriptableObject.putProperty(scope, "rule", wrappedThis);
// evaluate the script
// $NON-NLS-1$
Object resultObject = cx.evaluateString(scope, script, "<cmd>", 1, null);
Object actualObject = null;
if (resultObject instanceof org.mozilla.javascript.NativeJavaObject) {
actualObject = ((org.mozilla.javascript.NativeJavaObject) resultObject).unwrap();
} else {
actualObject = resultObject;
}
if (actualObject instanceof Boolean) {
return ((Boolean) actualObject).booleanValue();
} else if (actualObject instanceof String) {
return ("true".equalsIgnoreCase(actualObject.toString())) || ("yes".equalsIgnoreCase(// $NON-NLS-1$ //$NON-NLS-2$
actualObject.toString()));
} else if (actualObject instanceof Number) {
return ((Number) actualObject).intValue() > 0;
} else if (actualObject instanceof IPentahoResultSet) {
return ((IPentahoResultSet) actualObject).getRowCount() > 0;
}
// } catch (Exception e) {
// logger.error("Error executing conditional execution script.", e);
} finally {
Context.exit();
}
return shouldExecute;
}
use of org.pentaho.platform.plugin.services.connections.javascript.JavaScriptResultSet in project pentaho-platform by pentaho.
the class XQueryIT method testAddRow.
public void testAddRow() {
MemoryMetaData metadata = new MemoryMetaData(new String[][] { { "col1", "col2" } }, null);
MemoryResultSet dataSet = new MemoryResultSet(metadata);
JavaScriptResultSet data = new JavaScriptResultSet();
data.setResultSet(dataSet);
data.addRow(new Object[] { "a", new Integer(1) });
data.addRow(new Object[] { "b", new Integer(2) });
data.addRow(new Object[] { "c", new Integer(3) });
assertEquals(3, data.getRowCount());
assertEquals("a", data.getValueAt(0, 0));
assertEquals(1, data.getValueAt(0, 1));
assertEquals("b", data.getValueAt(1, 0));
assertEquals(2, data.getValueAt(1, 1));
assertEquals("c", data.getValueAt(2, 0));
assertEquals(3, data.getValueAt(2, 1));
}
use of org.pentaho.platform.plugin.services.connections.javascript.JavaScriptResultSet in project pentaho-platform by pentaho.
the class XQueryIT method testIterators.
public void testIterators() {
MemoryMetaData metadata = new MemoryMetaData(new String[][] { { "col1", "col2" } }, null);
MemoryResultSet dataSet = new MemoryResultSet(metadata);
JavaScriptResultSet data = new JavaScriptResultSet();
data.setResultSet(dataSet);
data.addRow(new Object[] { "a", new Integer(1) });
data.addRow(new Object[] { "b", new Integer(2) });
data.addRow(new Object[] { "c", new Integer(3) });
assertEquals(3, data.getRowCount());
assertEquals("a", data.getValueAt(0, 0));
assertEquals(1, data.getValueAt(0, 1));
assertEquals("b", data.getValueAt(1, 0));
assertEquals(2, data.getValueAt(1, 1));
assertEquals("c", data.getValueAt(2, 0));
assertEquals(3, data.getValueAt(2, 1));
assertEquals("a", data.next()[0]);
assertEquals("b", data.next()[0]);
assertEquals("c", data.next()[0]);
assertNull(data.next());
data.beforeFirst();
assertEquals("a", data.next()[0]);
assertEquals("b", data.next()[0]);
assertEquals("c", data.next()[0]);
assertNull(data.next());
data.close();
assertEquals("a", data.next()[0]);
assertEquals("b", data.next()[0]);
assertEquals("c", data.next()[0]);
assertNull(data.next());
data.closeConnection();
assertEquals("a", data.next()[0]);
assertEquals("b", data.next()[0]);
assertEquals("c", data.next()[0]);
assertNull(data.next());
data.dispose();
assertEquals("a", data.next()[0]);
assertEquals("b", data.next()[0]);
assertEquals("c", data.next()[0]);
assertNull(data.next());
}
Aggregations