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;
}
}
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;
}
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;
}
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;
}
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();
}
Aggregations