Search in sources :

Example 1 with ICreateFeedbackParameterCallback

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

the class SolutionEngineInteractivityService method handleActionRequest.

protected void handleActionRequest(final HttpServletRequest request, final HttpServletResponse response, final HttpOutputHandler outputHandler, final HttpServletRequestHandler requestHandler, IParameterProvider requestParameters, ByteArrayOutputStream outputStream, final IContentItem contentItem) throws ServletException, IOException {
    IRuntimeContext runtime = null;
    try {
        DocumentBuilderFactory dbf = XMLParserFactoryProducer.createSecureDocBuilderFactory();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.newDocument();
        Element root = document.createElement("action_sequence_info");
        document.appendChild(root);
        requestHandler.setCreateFeedbackParameterCallback(new ICreateFeedbackParameterCallback() {

            public void createFeedbackParameter(IRuntimeContext runtimeContext, String fieldName, String displayName, String hint, Object defaultValues, List values, Map dispNames, String displayStyle, boolean optional, boolean visible) {
                Element parameterElement = document.createElement("parameter");
                parameterElement.setAttribute("name", fieldName);
                parameterElement.setAttribute("display-name", displayName);
                parameterElement.setAttribute("display-style", displayStyle);
                parameterElement.setAttribute("hint", "" + hint);
                parameterElement.setAttribute("optional", "" + optional);
                parameterElement.setAttribute("visible", "" + visible);
                try {
                    IActionParameter actionParameter = runtimeContext.getInputParameter(fieldName);
                    if (actionParameter != null) {
                        List variables = actionParameter.getVariables();
                        for (int i = 0; variables != null && i < variables.size(); i++) {
                            Object var = variables.get(i);
                            if (var instanceof ActionParameterSource) {
                                String sourceName = ((ActionParameterSource) var).getSourceName();
                                String sourceValue = ((ActionParameterSource) var).getValue();
                                parameterElement.setAttribute("source-name", "" + sourceName);
                                parameterElement.setAttribute("source-value", "" + sourceValue);
                            } else {
                                System.out.println(var);
                            }
                        }
                    }
                } catch (Exception npe) {
                // ignore
                }
                root.appendChild(parameterElement);
                if (values != null) {
                    Element valuesElement = document.createElement("values");
                    for (Object value : values) {
                        Element valueElement = document.createElement("value");
                        valueElement.setAttribute("value", "" + value);
                        if (dispNames != null && dispNames.containsKey(value)) {
                            valueElement.setAttribute("display-name", "" + dispNames.get(value));
                        }
                        valuesElement.appendChild(valueElement);
                    }
                    parameterElement.appendChild(valuesElement);
                }
                if (defaultValues != null) {
                    Element valuesElement = document.createElement("selected-values");
                    if (defaultValues instanceof List) {
                        for (Object value : (List) defaultValues) {
                            Element valueElement = document.createElement("value");
                            valueElement.setAttribute("value", "" + value);
                            valuesElement.appendChild(valueElement);
                        }
                    } else {
                        Element valueElement = document.createElement("value");
                        valueElement.setAttribute("value", "" + defaultValues);
                        valuesElement.appendChild(valueElement);
                    }
                    parameterElement.appendChild(valuesElement);
                }
            }
        });
        runtime = requestHandler.handleActionRequest(0, 0);
        root.setAttribute("is-prompt-pending", "" + runtime.isPromptPending());
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(new StringWriter());
        TransformerFactory.newInstance().newTransformer().transform(source, result);
        String theXML = result.getWriter().toString();
        response.setContentType("text/xml");
        response.getOutputStream().write(theXML.getBytes());
        response.getOutputStream().close();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        e.printStackTrace();
    } finally {
        if (runtime != null) {
            runtime.dispose();
        }
    }
    if (contentItem != null) {
        contentItem.closeOutputStream();
    }
}
Also used : TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ICreateFeedbackParameterCallback(org.pentaho.platform.api.engine.ICreateFeedbackParameterCallback) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException) ServletException(javax.servlet.ServletException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ActionParameterSource(org.pentaho.platform.engine.services.actionsequence.ActionParameterSource) StringWriter(java.io.StringWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) List(java.util.List) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IRuntimeContext(org.pentaho.platform.api.engine.IRuntimeContext) HashMap(java.util.HashMap) Map(java.util.Map) IActionParameter(org.pentaho.platform.api.engine.IActionParameter) TransformerException(javax.xml.transform.TransformerException)

Aggregations

IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ServletException (javax.servlet.ServletException)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 TransformerFactoryConfigurationError (javax.xml.transform.TransformerFactoryConfigurationError)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 IActionParameter (org.pentaho.platform.api.engine.IActionParameter)1 ICreateFeedbackParameterCallback (org.pentaho.platform.api.engine.ICreateFeedbackParameterCallback)1 IRuntimeContext (org.pentaho.platform.api.engine.IRuntimeContext)1 ActionParameterSource (org.pentaho.platform.engine.services.actionsequence.ActionParameterSource)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1