Search in sources :

Example 6 with JavaScriptResultSet

use of org.pentaho.platform.plugin.services.connections.javascript.JavaScriptResultSet in project pentaho-platform by pentaho.

the class OpenFlashChartComponentTest method testAutoRange.

@Test
public void testAutoRange() throws Exception {
    // Line Chart
    JavaScriptResultSet query_result = new JavaScriptResultSet();
    List<String> cols = new ArrayList<String>();
    cols.add("MESES");
    cols.add("ACTUAL");
    cols.add("ANTERIOR");
    query_result.setMetaData(new MemoryMetaData(cols));
    query_result.addRow(new Object[] { "Enero", 0, 0 });
    query_result.addRow(new Object[] { "Febrero", 0, 0 });
    query_result.addRow(new Object[] { "Marzo", 0, 0 });
    query_result.addRow(new Object[] { "Abril", 0, 0 });
    query_result.addRow(new Object[] { "Mayo", 0, 0 });
    query_result.addRow(new Object[] { "Junio", 0, 0 });
    query_result.addRow(new Object[] { "Julio", 6, 3 });
    Document chartDocument = XmlDom4JHelper.getDocFromString("<chart><chart-type>LineChart</chart-type></chart>", new PentahoEntityResolver());
    Node chartNode = chartDocument.selectSingleNode("chart");
    String json = PentahoOFC4JChartHelper.generateChartJson(chartNode, query_result, false, null);
    System.out.println(json);
    Assert.assertTrue(json.indexOf("\"min\":0") >= 0 && json.indexOf("\"max\":6") >= 0);
}
Also used : JavaScriptResultSet(org.pentaho.platform.plugin.services.connections.javascript.JavaScriptResultSet) Node(org.dom4j.Node) ArrayList(java.util.ArrayList) MemoryMetaData(org.pentaho.commons.connection.memory.MemoryMetaData) Document(org.dom4j.Document) PentahoEntityResolver(org.pentaho.platform.engine.services.solution.PentahoEntityResolver) Test(org.junit.Test)

Example 7 with JavaScriptResultSet

use of org.pentaho.platform.plugin.services.connections.javascript.JavaScriptResultSet in project pentaho-platform by pentaho.

the class JavascriptRule method executeScript.

protected Object executeScript(final ScriptableObject scriptable, final Scriptable scope, final String script, final Context cx) throws Exception {
    ScriptableObject.defineClass(scope, JavaScriptResultSet.class);
    @SuppressWarnings("unchecked") Set<String> inputNames = getInputNames();
    Iterator<String> inputNamesIterator = inputNames.iterator();
    String inputName;
    Object inputValue;
    while (inputNamesIterator.hasNext()) {
        inputName = (String) inputNamesIterator.next();
        if (inputName.indexOf('-') >= 0) {
            throw new IllegalArgumentException(Messages.getInstance().getErrorString("JSRULE.ERROR_0006_INVALID_JS_VARIABLE", // $NON-NLS-1$
            inputName));
        }
        inputValue = getInputValue(inputName);
        if (inputValue instanceof String) {
            inputValue = StringEscapeUtils.escapeHtml((String) inputValue);
        }
        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);
    }
    // Add system out and this object to the scope
    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);
    // $NON-NLS-1$
    return cx.evaluateString(scope, script, "<cmd>", 1, null);
}
Also used : IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) JavaScriptResultSet(org.pentaho.platform.plugin.services.connections.javascript.JavaScriptResultSet) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 8 with JavaScriptResultSet

use of org.pentaho.platform.plugin.services.connections.javascript.JavaScriptResultSet in project pentaho-platform by pentaho.

the class JavaScriptResultSetIT 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());
}
Also used : JavaScriptResultSet(org.pentaho.platform.plugin.services.connections.javascript.JavaScriptResultSet) MemoryMetaData(org.pentaho.commons.connection.memory.MemoryMetaData) MemoryResultSet(org.pentaho.commons.connection.memory.MemoryResultSet)

Example 9 with JavaScriptResultSet

use of org.pentaho.platform.plugin.services.connections.javascript.JavaScriptResultSet in project pentaho-platform by pentaho.

the class JavaScriptResultSetIT method testGetDataRow.

public void testGetDataRow() {
    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.getDataRow(0);
    assertEquals(2, row.length);
    assertEquals("a", row[0]);
    assertEquals(1, row[1]);
    row = data.getDataRow(1);
    assertEquals("b", row[0]);
    assertEquals(2, row[1]);
    row = data.getDataRow(2);
    assertEquals("c", row[0]);
    assertEquals(3, row[1]);
    assertNull(data.getDataRow(99));
}
Also used : JavaScriptResultSet(org.pentaho.platform.plugin.services.connections.javascript.JavaScriptResultSet) MemoryMetaData(org.pentaho.commons.connection.memory.MemoryMetaData) MemoryResultSet(org.pentaho.commons.connection.memory.MemoryResultSet)

Example 10 with JavaScriptResultSet

use of org.pentaho.platform.plugin.services.connections.javascript.JavaScriptResultSet in project pentaho-platform by pentaho.

the class JavaScriptResultSetIT method testGetDataColumn.

public void testGetDataColumn() {
    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[] col = data.getDataColumn(0);
    assertEquals(3, col.length);
    assertEquals("a", col[0]);
    assertEquals("b", col[1]);
    assertEquals("c", col[2]);
    col = data.getDataColumn(1);
    assertEquals(3, col.length);
    assertEquals(1, col[0]);
    assertEquals(2, col[1]);
    assertEquals(3, col[2]);
}
Also used : JavaScriptResultSet(org.pentaho.platform.plugin.services.connections.javascript.JavaScriptResultSet) MemoryMetaData(org.pentaho.commons.connection.memory.MemoryMetaData) MemoryResultSet(org.pentaho.commons.connection.memory.MemoryResultSet)

Aggregations

JavaScriptResultSet (org.pentaho.platform.plugin.services.connections.javascript.JavaScriptResultSet)10 MemoryMetaData (org.pentaho.commons.connection.memory.MemoryMetaData)8 MemoryResultSet (org.pentaho.commons.connection.memory.MemoryResultSet)7 ScriptableObject (org.mozilla.javascript.ScriptableObject)2 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Document (org.dom4j.Document)1 Node (org.dom4j.Node)1 Test (org.junit.Test)1 Context (org.mozilla.javascript.Context)1 Scriptable (org.mozilla.javascript.Scriptable)1 IActionParameter (org.pentaho.platform.api.engine.IActionParameter)1 PentahoEntityResolver (org.pentaho.platform.engine.services.solution.PentahoEntityResolver)1