Search in sources :

Example 31 with IActionParameter

use of org.pentaho.platform.api.engine.IActionParameter in project pentaho-platform by pentaho.

the class XactionUtil method doParameter.

@SuppressWarnings({ "unchecked", "rawtypes" })
public static String doParameter(final RepositoryFile file, IParameterProvider parameterProvider, final IPentahoSession userSession) throws IOException {
    ActionSequenceJCRHelper helper = new ActionSequenceJCRHelper();
    final IActionSequence actionSequence = helper.getActionSequence(file.getPath(), PentahoSystem.loggingLevel, RepositoryFilePermission.READ);
    final Document document = DocumentHelper.createDocument();
    try {
        final Element parametersElement = document.addElement("parameters");
        // noinspection unchecked
        final Map<String, IActionParameter> params = actionSequence.getInputDefinitionsForParameterProvider(IParameterProvider.SCOPE_REQUEST);
        for (final Map.Entry<String, IActionParameter> entry : params.entrySet()) {
            final String paramName = entry.getKey();
            final IActionParameter paramDef = entry.getValue();
            final String value = paramDef.getStringValue();
            final Class type;
            // defined as constant (string)
            if (IActionParameter.TYPE_LIST.equalsIgnoreCase(paramDef.getType())) {
                type = String[].class;
            } else {
                type = String.class;
            }
            final String label = paramDef.getSelectionDisplayName();
            final String[] values;
            if (StringUtils.isEmpty(value)) {
                values = new String[0];
            } else {
                values = new String[] { value };
            }
            createParameterElement(parametersElement, paramName, type, label, "user", "parameters", values);
        }
        createParameterElement(parametersElement, "path", String.class, null, "system", "system", new String[] { file.getPath() });
        createParameterElement(parametersElement, "prompt", String.class, null, "system", "system", new String[] { "yes", "no" });
        createParameterElement(parametersElement, "instance-id", String.class, null, "system", "system", new String[] { parameterProvider.getStringParameter("instance-id", null) });
        // no close, as far as I know tomcat does not like it that much ..
        OutputFormat format = OutputFormat.createCompactFormat();
        format.setSuppressDeclaration(true);
        // $NON-NLS-1$
        format.setEncoding("utf-8");
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        XMLWriter writer = new XMLWriter(outputStream, format);
        writer.write(document);
        writer.flush();
        return outputStream.toString("utf-8");
    } catch (Exception e) {
        logger.warn(Messages.getInstance().getString("HttpWebService.ERROR_0003_UNEXPECTED"), e);
        return null;
    }
}
Also used : IActionSequence(org.pentaho.platform.api.engine.IActionSequence) Element(org.dom4j.Element) OutputFormat(org.dom4j.io.OutputFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter) ServletException(javax.servlet.ServletException) ObjectFactoryException(org.pentaho.platform.api.engine.ObjectFactoryException) IOException(java.io.IOException) ActionSequenceJCRHelper(org.pentaho.platform.engine.services.ActionSequenceJCRHelper) IActionParameter(org.pentaho.platform.api.engine.IActionParameter) Map(java.util.Map) HashMap(java.util.HashMap)

Example 32 with IActionParameter

use of org.pentaho.platform.api.engine.IActionParameter in project pentaho-platform by pentaho.

the class WidgetGridComponent method getActionData.

protected IPentahoResultSet getActionData() {
    // create an instance of the solution engine to execute the specified
    // action
    ISolutionEngine solutionEngine = PentahoSystem.get(ISolutionEngine.class, getSession());
    solutionEngine.setLoggingLevel(ILogger.DEBUG);
    solutionEngine.init(getSession());
    HashMap parameterProviders = getParameterProviders();
    OutputStream outputStream = null;
    SimpleOutputHandler outputHandler = null;
    outputHandler = new SimpleOutputHandler(outputStream, false);
    ArrayList messages = new ArrayList();
    String processId = this.getClass().getName();
    String actionSeqPath = ActionInfo.buildSolutionPath(solution, actionPath, actionName);
    context = solutionEngine.execute(actionSeqPath, processId, false, true, instanceId, false, parameterProviders, outputHandler, null, urlFactory, messages);
    if (actionOutput != null) {
        if (context.getOutputNames().contains(actionOutput)) {
            IActionParameter output = context.getOutputParameter(actionOutput);
            IPentahoResultSet results = output.getValueAsResultSet();
            if (results != null) {
                results = results.memoryCopy();
            }
            return results;
        } else {
            // this is an error
            return null;
        }
    } else {
        // return the first list that we find...
        Iterator it = context.getOutputNames().iterator();
        while (it.hasNext()) {
            IActionParameter output = (IActionParameter) it.next();
            if (output.getType().equalsIgnoreCase(IActionParameter.TYPE_RESULT_SET)) {
                IPentahoResultSet results = output.getValueAsResultSet();
                if (results != null) {
                    results = results.memoryCopy();
                }
                return results;
            }
        }
    }
    return null;
}
Also used : IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) ISolutionEngine(org.pentaho.platform.api.engine.ISolutionEngine) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) SimpleOutputHandler(org.pentaho.platform.engine.core.output.SimpleOutputHandler) IActionParameter(org.pentaho.platform.api.engine.IActionParameter)

Example 33 with IActionParameter

use of org.pentaho.platform.api.engine.IActionParameter 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;
}
Also used : Context(org.mozilla.javascript.Context) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable) IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) JavaScriptResultSet(org.pentaho.platform.plugin.services.connections.javascript.JavaScriptResultSet) Iterator(java.util.Iterator) ScriptableObject(org.mozilla.javascript.ScriptableObject) IActionParameter(org.pentaho.platform.api.engine.IActionParameter) Map(java.util.Map)

Example 34 with IActionParameter

use of org.pentaho.platform.api.engine.IActionParameter in project pentaho-platform by pentaho.

the class JFreeReportValidateParametersComponent method executeAction.

@Override
protected boolean executeAction() throws Throwable {
    // $NON-NLS-1$
    final String defaultValue = "";
    // Get input parameters, and set them as properties in the report
    // object.
    final Set paramNames = getInputNames();
    boolean parameterUINeeded = false;
    final Iterator it = paramNames.iterator();
    while (it.hasNext()) {
        String paramName = (String) it.next();
        Object paramValue = getInputValue(paramName);
        if (// $NON-NLS-1$
        (paramValue == null) || ("".equals(paramValue))) {
            IActionParameter paramParameter = getInputParameter(paramName);
            if (paramParameter.getPromptStatus() == IActionParameter.PROMPT_PENDING) {
                parameterUINeeded = true;
                continue;
            }
            if (isParameterUIAvailable()) {
                // The parameter value was not provided, and we are allowed
                // to
                // create user interface forms
                // $NON-NLS-1$
                createFeedbackParameter(paramName, paramName, "", defaultValue, true);
                parameterUINeeded = true;
            } else {
                return false;
            }
        }
    }
    if (parameterUINeeded) {
        this.parameterUiNeeded = true;
    } else {
        this.parameterUiNeeded = false;
    }
    return true;
}
Also used : Set(java.util.Set) Iterator(java.util.Iterator) IActionParameter(org.pentaho.platform.api.engine.IActionParameter)

Example 35 with IActionParameter

use of org.pentaho.platform.api.engine.IActionParameter in project pentaho-platform by pentaho.

the class ContentOutputComponentIT method testSuccessPaths.

public void testSuccessPaths() {
    startTest();
    // $NON-NLS-1$
    String testName = CO_TEST_NAME + "string_" + System.currentTimeMillis();
    SimpleParameterProvider parameterProvider = new SimpleParameterProvider();
    IRuntimeContext context = // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    run("/test/platform/ContentOutputTest.xaction", parameterProvider, testName, CO_TEST_EXTN);
    assertEquals(Messages.getInstance().getString("BaseTest.USER_RUNNING_ACTION_SEQUENCE"), IRuntimeContext.RUNTIME_STATUS_SUCCESS, // $NON-NLS-1$
    context.getStatus());
    // $NON-NLS-1$
    IActionParameter rtn = context.getOutputParameter("content");
    assertNotNull(rtn);
    InputStream is = this.getInputStreamFromOutput(testName, CO_TEST_EXTN);
    // Did the test execute properly...
    assertNotNull(is);
    // $NON-NLS-1$
    String lookingFor = "This is sample output from the content-output component.";
    String wasRead = FileHelper.getStringFromInputStream(is);
    assertTrue(wasRead.startsWith(lookingFor));
    // Test different path - Byte Array Output Stream
    // $NON-NLS-1$
    lookingFor = "This is as sample bytearray output stream";
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        baos.write(lookingFor.getBytes());
    } catch (Exception ex) {
        fail();
    }
    // $NON-NLS-1$
    testName = CO_TEST_NAME + "ByteArrayOutputStream_" + System.currentTimeMillis();
    // $NON-NLS-1$
    parameterProvider.setParameter("CONTENTOUTPUT", baos);
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    context = run("/test/platform/ContentOutputTest_Bytearray.xaction", parameterProvider, testName, CO_TEST_EXTN);
    assertEquals(IRuntimeContext.RUNTIME_STATUS_SUCCESS, context.getStatus());
    is = this.getInputStreamFromOutput(testName, CO_TEST_EXTN);
    // Did the test execute properly...
    assertNotNull(is);
    wasRead = FileHelper.getStringFromInputStream(is);
    FileHelper.getStringFromFile(new File(PentahoSystem.getApplicationContext().getSolutionPath(// $NON-NLS-1$
    "test/datasource/books.xml")));
    try {
        FileHelper.getBytesFromFile(new File(PentahoSystem.getApplicationContext().getSolutionPath(// $NON-NLS-1$
        "test/datasource/books.xml")));
    } catch (IOException io) {
    // do nothing
    }
    File f = null;
    FileHelper.getStringFromFile(f);
    assertTrue(wasRead.startsWith(lookingFor));
    // Test different path - InputStream
    // $NON-NLS-1$
    testName = CO_TEST_NAME + "ByteArrayInputStream_" + System.currentTimeMillis();
    // $NON-NLS-1$
    lookingFor = "This is as a simple bytearray input stream";
    ByteArrayInputStream bais = new ByteArrayInputStream(lookingFor.getBytes());
    // $NON-NLS-1$
    parameterProvider.setParameter("CONTENTOUTPUT", bais);
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    context = run("/test/platform/ContentOutputTest_Bytearray.xaction", parameterProvider, testName, CO_TEST_EXTN);
    assertEquals(IRuntimeContext.RUNTIME_STATUS_SUCCESS, context.getStatus());
    is = this.getInputStreamFromOutput(testName, CO_TEST_EXTN);
    // Did the test execute properly...
    assertNotNull(is);
    String newText = FileHelper.getStringFromInputStream(is);
    // $NON-NLS-1$
    System.out.println("Read Text from the input stream" + newText);
    String newTextFromIS = FileHelper.getStringFromInputStream(is);
    // $NON-NLS-1$
    System.out.println("Read Text from the input stream" + newTextFromIS);
    assertTrue(newText.startsWith(lookingFor));
    finishTest();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IRuntimeContext(org.pentaho.platform.api.engine.IRuntimeContext) IActionParameter(org.pentaho.platform.api.engine.IActionParameter) File(java.io.File) IOException(java.io.IOException) SimpleParameterProvider(org.pentaho.platform.engine.core.solution.SimpleParameterProvider)

Aggregations

IActionParameter (org.pentaho.platform.api.engine.IActionParameter)68 IRuntimeContext (org.pentaho.platform.api.engine.IRuntimeContext)24 ArrayList (java.util.ArrayList)22 HashMap (java.util.HashMap)22 Iterator (java.util.Iterator)19 Map (java.util.Map)14 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)14 ISolutionEngine (org.pentaho.platform.api.engine.ISolutionEngine)13 List (java.util.List)9 SimpleUrlFactory (org.pentaho.platform.util.web.SimpleUrlFactory)9 Set (java.util.Set)8 IContentItem (org.pentaho.platform.api.repository.IContentItem)8 IOException (java.io.IOException)7 OutputStream (java.io.OutputStream)6 Test (org.junit.Test)6 SimpleOutputHandler (org.pentaho.platform.engine.core.output.SimpleOutputHandler)6 ActionParameter (org.pentaho.platform.engine.services.actionsequence.ActionParameter)6 Document (org.dom4j.Document)5 IPreparedComponent (org.pentaho.platform.api.data.IPreparedComponent)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4