use of org.pentaho.platform.api.engine.IActionParameter in project pentaho-platform by pentaho.
the class PojoComponent method executeAction.
@SuppressWarnings({ "unchecked" })
@Override
protected boolean executeAction() throws Throwable {
Set<?> inputNames = getInputNames();
Element defnNode = (Element) getComponentDefinition();
// if( pojo instanceof IConfiguredPojo ) {
if (getMethods.containsKey("CONFIGSETTINGSPATHS") && configureMethod != null) {
// $NON-NLS-1$
// $NON-NLS-1$
Method method = getMethods.get("CONFIGSETTINGSPATHS");
Set<String> settingsPaths = (Set<String>) method.invoke(pojo, new Object[] {});
Iterator<String> keys = settingsPaths.iterator();
Map<String, String> settings = new HashMap<String, String>();
SystemSettingsParameterProvider params = new SystemSettingsParameterProvider();
while (keys.hasNext()) {
String path = keys.next();
String value = params.getStringParameter(path, null);
if (value != null) {
settings.put(path, value);
}
}
configureMethod.invoke(pojo, new Object[] { settings });
}
// set the PentahoSession
if (sessionMethod != null) {
callMethods(Arrays.asList(new Method[] { sessionMethod }), getSession());
}
// set the logger
if (loggerMethod != null) {
callMethods(Arrays.asList(new Method[] { loggerMethod }), getLogger());
}
Map<String, Object> inputMap = new HashMap<String, Object>();
// look at the component settings
// $NON-NLS-1$
List<?> nodes = defnNode.selectNodes("*");
for (int idx = 0; idx < nodes.size(); idx++) {
Element node = (Element) nodes.get(idx);
// inputs may typically contain a dash in them, such as
// something like "report-definition" and we should expect
// a setter as setReportDefinition, so we will remove the
// dashes and everything should proceed as expected
// $NON-NLS-1$ //$NON-NLS-2$
String name = node.getName().replace("-", "").toUpperCase();
if (!name.equals("CLASS") && !name.equals("OUTPUTSTREAM")) {
// $NON-NLS-1$ //$NON-NLS-2$
String value = node.getText();
List<Method> method = setMethods.get(name);
if (method != null) {
callMethodWithString(method, value);
} else if (runtimeInputsMethod != null) {
inputMap.put(name, value);
} else {
// Supress error (For string/value replacement)
// $NON-NLS-1$
getLogger().warn(Messages.getInstance().getString("PojoComponent.UNUSED_INPUT", name));
}
}
}
Iterator<?> it = null;
// now process all of the resources and see if we can call them as setters
Set<?> resourceNames = getResourceNames();
Map<String, IActionSequenceResource> resourceMap = new HashMap<String, IActionSequenceResource>();
if (resourceNames != null && resourceNames.size() > 0) {
it = resourceNames.iterator();
while (it.hasNext()) {
String name = (String) it.next();
IActionSequenceResource resource = getResource(name);
// $NON-NLS-1$ //$NON-NLS-2$
name = name.replace("-", "");
resourceMap.put(name, resource);
List<Method> methods = setMethods.get(name.toUpperCase());
if (methods != null) {
for (Method method : methods) {
Class<?>[] paramTypes = method.getParameterTypes();
if (paramTypes.length == 1) {
Object value = null;
if (paramTypes[0] == InputStream.class) {
value = resource.getInputStream(RepositoryFilePermission.READ, LocaleHelper.getLocale());
} else if (paramTypes[0] == IActionSequenceResource.class) {
value = resource;
} else if (paramTypes[0] == String.class) {
value = getRuntimeContext().getResourceAsString(resource);
} else if (paramTypes[0] == Document.class) {
value = getRuntimeContext().getResourceAsDocument(resource);
}
callMethod(method, value);
}
}
// CHECKSTYLE IGNORE EmptyBlock FOR NEXT 3 LINES
} else {
// BISERVER-2715 we should ignore this as the resource might be meant for another component
}
}
}
// now process all of the inputs, overriding the component settings
it = inputNames.iterator();
while (it.hasNext()) {
String name = (String) it.next();
Object value = getInputValue(name);
// now that we have the value, we can fix the name
// $NON-NLS-1$ //$NON-NLS-2$
name = name.replace("-", "");
List<Method> methods = setMethods.get(name.toUpperCase());
if (methods != null) {
callMethods(methods, value);
} else if (runtimeInputsMethod != null) {
inputMap.put(name, value);
} else {
// Supress error (For string/value replacement)
// $NON-NLS-1$
getLogger().warn(Messages.getInstance().getString("PojoComponent.UNUSED_INPUT", name));
}
}
if (resourceMap.size() > 0 && resourcesMethod != null) {
// call the resources setter
resourcesMethod.invoke(pojo, new Object[] { resourceMap });
}
if (inputMap.size() > 0 && runtimeInputsMethod != null) {
// call the generic input setter
runtimeInputsMethod.invoke(pojo, new Object[] { inputMap });
}
if (// $NON-NLS-1$ //$NON-NLS-2$
getOutputNames().contains("outputstream") && setMethods.containsKey("OUTPUTSTREAM") && getMethods.containsKey("MIMETYPE")) {
// $NON-NLS-1$
// get the mime-type
// Get the first method to match
// $NON-NLS-1$
Method method = getMethods.get("MIMETYPE");
String mimeType = (String) method.invoke(pojo, new Object[] {});
// $NON-NLS-1$
String mappedOutputName = "outputstream";
if ((getActionDefinition() != null) && (getActionDefinition().getOutput("outputstream") != null)) {
// $NON-NLS-1$
// $NON-NLS-1$
mappedOutputName = getActionDefinition().getOutput("outputstream").getPublicName();
}
// this marks the HttpOutputHandler as contentDone=true, causing the MessageFormatter to not print an error
IContentItem contentItem = getOutputContentItem(mappedOutputName, mimeType);
if (!(contentItem instanceof SimpleContentItem)) {
// SimpleContentItem can't handle being added to outputs because it
// doesn't have a getInputStream(), and the path used to return
// null.
// $NON-NLS-1$
setOutputValue("outputstream", contentItem);
}
// set the output stream
OutputStream out = contentItem.getOutputStream(getActionName());
// $NON-NLS-1$
method = setMethods.get("OUTPUTSTREAM").get(0);
method.invoke(pojo, new Object[] { out });
}
if (validateMethod != null) {
Object obj = validateMethod.invoke(pojo, (Object[]) null);
if (obj instanceof Boolean) {
Boolean ok = (Boolean) obj;
if (!ok) {
return false;
}
}
}
// now execute the pojo
Boolean result = Boolean.FALSE;
if (executeMethod != null) {
result = (Boolean) executeMethod.invoke(pojo, new Object[] {});
} else {
// we can only assume we are ok so far
result = Boolean.TRUE;
}
// now handle outputs
Set<?> outputNames = getOutputNames();
// first get the runtime outputs
Map<String, Object> outputMap = new HashMap<String, Object>();
if (runtimeOutputsMethod != null) {
outputMap = (Map<String, Object>) runtimeOutputsMethod.invoke(pojo, new Object[] {});
}
it = outputNames.iterator();
while (it.hasNext()) {
String name = (String) it.next();
// CHECKSTYLE IGNORE EmptyBlock FOR NEXT 3 LINES
if (name.equals("outputstream")) {
// $NON-NLS-1$
// we should be done
} else {
IActionParameter param = getOutputItem(name);
Method method = getMethods.get(name.toUpperCase());
if (method != null) {
Object value = method.invoke(pojo, new Object[] {});
param.setValue(value);
} else {
Object value = outputMap.get(name);
if (value != null) {
param.setValue(value);
} else {
throw new NoSuchMethodException(name);
}
}
}
}
return result.booleanValue();
}
use of org.pentaho.platform.api.engine.IActionParameter in project pentaho-platform by pentaho.
the class ConditionalExecutionTest method createParameterWithResult.
private static IActionParameter createParameterWithResult(int rowsCount) {
IActionParameter parameter = mock(IActionParameter.class);
IPentahoResultSet resultSet = mock(IPentahoResultSet.class);
when(resultSet.getRowCount()).thenReturn(rowsCount);
when(parameter.getValue()).thenReturn(resultSet);
return parameter;
}
use of org.pentaho.platform.api.engine.IActionParameter in project pentaho-platform by pentaho.
the class ScriptableConditionTest method shouldExecute_returns_true_for_result_with_rows.
@Test
public void shouldExecute_returns_true_for_result_with_rows() throws Exception {
ScriptableCondition conditionalExecution = new ScriptableCondition();
conditionalExecution.setScript(RESULT_ELEMENT_SCRIPT);
IActionParameter parameter = createParameterWithResult(1);
boolean actualResult = conditionalExecution.shouldExecute(Collections.singletonMap(RESULT_ELEMENT, parameter), logger);
assertTrue(actualResult);
}
use of org.pentaho.platform.api.engine.IActionParameter in project pentaho-platform by pentaho.
the class IsOutputParameterTest method testIsOutputParameter.
/**
* Assert parameters with is-output-parameter=false don't appear in output
*
* @throws XmlParseException
*/
public void testIsOutputParameter() throws XmlParseException {
startTest();
ISolutionEngine solutionEngine = ServiceTestHelper.getSolutionEngine();
String xactionStr = ServiceTestHelper.getXAction(SOLUTION_PATH, "services/" + xactionName);
Document actionSequenceDocument = XmlDom4JHelper.getDocFromString(xactionStr, null);
IActionSequence actionSequence = SequenceDefinition.ActionSequenceFactory(actionSequenceDocument, "", this, // $NON-NLS-1$
PentahoSystem.getApplicationContext(), DEBUG);
Map allParameters = actionSequence.getOutputDefinitions();
Set<String> outParameters = new HashSet<String>();
Set<String> nonOutParameters = new HashSet<String>();
for (Object key : allParameters.keySet()) {
IActionParameter param = (IActionParameter) allParameters.get(key);
if (param.isOutputParameter()) {
outParameters.add(param.getName());
} else {
nonOutParameters.add(param.getName());
}
}
Assert.assertEquals("expected 2 outputable parameters in xaction", 2, outParameters.size());
Assert.assertEquals("expected 1 paramater with is-output-parameter=false", 1, nonOutParameters.size());
IRuntimeContext runtimeContext = // $NON-NLS-1$
solutionEngine.execute(// $NON-NLS-1$
xactionStr, // $NON-NLS-1$
xactionName, // $NON-NLS-1$
"simple output test", // $NON-NLS-1$
false, // $NON-NLS-1$
true, // $NON-NLS-1$
null, // $NON-NLS-1$
false, // $NON-NLS-1$
new HashMap(), null, null, new SimpleUrlFactory(""), // $NON-NLS-1$
new ArrayList());
IParameterManager paramManager = runtimeContext.getParameterManager();
Assert.assertEquals(outParameters.size(), paramManager.getCurrentOutputNames().size());
for (Object key : paramManager.getCurrentOutputNames()) {
Assert.assertTrue("output parameter not found in definition", outParameters.contains(key));
Assert.assertFalse("non-output parameter in output", nonOutParameters.contains(key));
}
finishTest();
}
use of org.pentaho.platform.api.engine.IActionParameter in project pentaho-platform by pentaho.
the class MessageFormatterTest method formatSuccessMessage.
@Test
public void formatSuccessMessage() throws Exception {
Set inputNames = new HashSet<String>();
inputNames.add("Test");
IActionParameter actionParameter = new ActionParameter("Test", "Test", "<img%20src=\"http://www.pentaho" + ".com/sites/all/themes/pentaho_resp/logo.svg\"%20/>", null, "");
when(runtimeCtx.getOutputNames()).thenReturn(inputNames);
doReturn(actionParameter).when(runtimeCtx).getOutputParameter(anyString());
MessageFormatter mf = new MessageFormatter();
StringBuffer messageBuffer = new StringBuffer();
mf.formatSuccessMessage(MessageFormatter.HTML_MIME_TYPE, runtimeCtx, messageBuffer, false);
assertEquals("<html><head><title>Pentaho BI Platform - Start Action</title><link rel=\"stylesheet\" " + "type=\"text/css\" href=\"/pentaho-style/active/default.css\"></head><body dir=\"LTR\"><table " + "cellspacing=\"10\"><tr><td class=\"portlet-section\" colspan=\"3\">Action Successful<hr " + "size=\"1\"/></td></tr><tr><td class=\"portlet-font\" valign=\"top\">Test=<img%20src=\"http://www" + ".pentaho.com/sites/all/themes/pentaho_resp/logo.svg\"%20/><br/></td></tr></table></body></html>", messageBuffer.toString());
}
Aggregations