Search in sources :

Example 6 with IActionSequenceResource

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

the class JFreeReportLoadComponent method getReportFromJar.

private MasterReport getReportFromJar() throws Exception {
    MasterReport report;
    final IActionSequenceResource resource = getResource(AbstractJFreeReportComponent.DATACOMPONENT_JARINPUT);
    final ClassLoader loader = ReportUtils.createJarLoader(getSession(), resource);
    if (loader == null) {
        throw new Exception(Messages.getInstance().getString(// $NON-NLS-1$
        "JFreeReportLoadComponent.ERROR_0035_COULD_NOT_CREATE_CLASSLOADER"));
    }
    String reportLocation = getInputStringValue(AbstractJFreeReportComponent.REPORTLOAD_REPORTLOC);
    URL resourceUrl = loader.getResource(reportLocation);
    if (resourceUrl == null) {
        throw new Exception(// $NON-NLS-1$
        Messages.getInstance().getErrorString(// $NON-NLS-1$
        "JFreeReport.ERROR_0016_REPORT_RESOURCE_INVALID", reportLocation, resource.getAddress()));
    }
    try {
        ReportGenerator generator = ReportGenerator.getInstance();
        report = generator.parseReport(resourceUrl, getDefinedResourceURL(resourceUrl));
    } catch (Exception ex) {
        throw new Exception(Messages.getInstance().getErrorString("JFreeReport.ERROR_0007_COULD_NOT_PARSE", reportLocation), // $NON-NLS-1$
        ex);
    }
    return report;
}
Also used : MasterReport(org.pentaho.reporting.engine.classic.core.MasterReport) ReportGenerator(org.pentaho.reporting.engine.classic.core.modules.parser.base.ReportGenerator) ResourceException(org.pentaho.reporting.libraries.resourceloader.ResourceException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) URL(java.net.URL) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource)

Example 7 with IActionSequenceResource

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

the class TemplateComponent method executeAction.

@Override
protected boolean executeAction() {
    try {
        TemplateMsgAction actionDefinition = (TemplateMsgAction) getActionDefinition();
        String template = null;
        template = actionDefinition.getTemplate().getStringValue();
        if ((null == template) && isDefinedResource(TemplateComponent.TEMPLATE)) {
            // $NON-NLS-1$
            IActionSequenceResource resource = getResource("template");
            template = getResourceAsString(resource);
        }
        String outputName = (String) getOutputNames().iterator().next();
        IActionParameter outputParam = getOutputItem(outputName);
        if (outputParam.getType().equals(IActionParameter.TYPE_CONTENT)) {
            String mimeType = actionDefinition.getMimeType().getStringValue();
            String extension = actionDefinition.getExtension().getStringValue();
            // This would prevent null values being passed as parameters to getOutputItem
            if (mimeType == null) {
                // $NON-NLS-1$
                mimeType = "";
            }
            if (extension == null) {
                // $NON-NLS-1$
                extension = "";
            }
            // Removing the null check here because if we avoid null exception it gives misleading hibernate
            // stale data exception which has nothing to do with a report that simply reads data.
            IContentItem outputItem = getOutputContentItem(outputName, mimeType);
            // IContentItem outputItem = getOutputItem(outputName, mimeType, extension);
            OutputStream outputStream = outputItem.getOutputStream(getActionName());
            outputStream.write(applyInputsToFormat(template).getBytes(LocaleHelper.getSystemEncoding()));
            outputItem.closeOutputStream();
            return true;
        } else {
            setOutputValue(outputName, applyInputsToFormat(template));
        }
        return true;
    } catch (Exception e) {
        // $NON-NLS-1$
        error(Messages.getInstance().getString("Template.ERROR_0004_COULD_NOT_FORMAT_TEMPLATE"), e);
        return false;
    }
}
Also used : OutputStream(java.io.OutputStream) IContentItem(org.pentaho.platform.api.repository.IContentItem) TemplateMsgAction(org.pentaho.actionsequence.dom.actions.TemplateMsgAction) IActionParameter(org.pentaho.platform.api.engine.IActionParameter) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource)

Example 8 with IActionSequenceResource

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

the class DashboardWidgetComponent method getXmlContent.

/**
 * Create a dial image.
 * <ul>
 * <li>Load the specified XML document describing the dial definition</li>
 * <li>Create a dial definition object from the XML definition</li>
 * <li>Use the JFreeChartEngine to create a dial image</li>
 * <li>Create an XML document describing the dial</li>
 * <li>Return the XML document</li>
 * </ul>
 *
 * @return The XML document describing this dial
 */
@Override
public Document getXmlContent() {
    WidgetDefinition widget = null;
    if (type == DashboardWidgetComponent.TYPE_DIAL) {
        // load the XML document that defines the dial
        IActionSequenceResource resource = new // $NON-NLS-1$
        ActionSequenceResource(// $NON-NLS-1$
        title, // $NON-NLS-1$
        IActionSequenceResource.SOLUTION_FILE_RESOURCE, // $NON-NLS-1$
        "text/xml", definitionPath);
        Document dialDefinition = null;
        try {
            org.dom4j.io.SAXReader reader = XMLParserFactoryProducer.getSAXReader(new SolutionURIResolver());
            dialDefinition = reader.read(resource.getInputStream(RepositoryFilePermission.READ, LocaleHelper.getLocale()));
        } catch (Throwable t) {
        // XML document can't be read. We'll just return a null document.
        }
        if (dialDefinition == null) {
            // $NON-NLS-1$
            error(Messages.getInstance().getErrorString("Widget.ERROR_0002_INVALID_RESOURCE", definitionPath));
            return null;
        }
        // create a dial definition from the XML definition
        widget = new DialWidgetDefinition(dialDefinition, 0, width, height, getSession());
        if (widget != null) {
            // set the value to be displayed on the dial
            widget.setValue(new Double(value));
        }
    }
    /*
     * else if( type == TYPE_THERMOMETER ) { // load the XML document that defines the thermometer
     * 
     * ActionResource resource = new ActionResource( title, IActionResource.SOLUTION_FILE_RESOURCE, "text/xml",
     * //$NON-NLS-1$ PentahoSystem.getApplicationContext().getSolutionPath( definitionPath ) ); //$NON-NLS-1$
     * Document thermometerDefinition = null; try { thermometerDefinition = PentahoSystem.getResourceAsDocument(
     * resource ); } catch (IOException e) {} // create a dial definition from the XML definition widget =
     * createThermometer( thermometerDefinition );
     * 
     * if( widget != null ) { // set the value to be displayed on the dial widget.setValue( new Double(value) ); //
     * Set the XSL file to be used to generate the HTML setXsl( "text/html", "DialWidget.xsl" ); //$NON-NLS-1$
     * //$NON-NLS-2$ } else { error( Messages.getInstance().getString("Widget.ERROR_0001_COULD_NOT_CREATE") );
     * //$NON-NLS-1$ return null; } }
     */
    if (widget == null) {
        // $NON-NLS-1$
        error(Messages.getInstance().getString("Widget.ERROR_0001_COULD_NOT_CREATE"));
        return null;
    }
    // create an image for the dial using the JFreeChart engine
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    // create temporary file names
    // $NON-NLS-1$
    String solutionDir = "system/tmp/";
    // $NON-NLS-1$
    String fileNamePrefix = "tmp_pie_";
    // $NON-NLS-1$
    String extension = ".png";
    String fileName = null;
    String filePathWithoutExtension = null;
    try {
        File file = PentahoSystem.getApplicationContext().createTempFile(getSession(), fileNamePrefix, extension, true);
        fileName = file.getName();
        filePathWithoutExtension = solutionDir + fileName.substring(0, fileName.indexOf('.'));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // $NON-NLS-1$
    String dialTitle = "";
    JFreeChartEngine.saveChart(widget, dialTitle, units, filePathWithoutExtension, width, height, JFreeChartEngine.OUTPUT_PNG, printWriter, this);
    // Create a document that describes the result
    Document result = DocumentHelper.createDocument();
    IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    setXslProperty("baseUrl", requestContext.getContextPath());
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    setXslProperty("fullyQualifiedServerUrl", PentahoSystem.getApplicationContext().getFullyQualifiedServerURL());
    // $NON-NLS-1$
    Element root = result.addElement("widget");
    // $NON-NLS-1$
    root.addElement("title").setText(title);
    // $NON-NLS-1$
    root.addElement("units").setText(units);
    // $NON-NLS-1$
    root.addElement("width").setText(Integer.toString(width));
    // $NON-NLS-1$
    root.addElement("height").setText(Integer.toString(height));
    // $NON-NLS-1$
    Element valueNode = root.addElement("value");
    valueNode.setText(Double.toString(value));
    // $NON-NLS-1$
    valueNode.addAttribute("in-image", Boolean.toString(widget.getValueFont() != null));
    // $NON-NLS-1$
    root.addElement("image").setText(fileName);
    return result;
}
Also used : Element(org.dom4j.Element) IOException(java.io.IOException) Document(org.dom4j.Document) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource) SolutionURIResolver(org.pentaho.platform.engine.services.SolutionURIResolver) IPentahoRequestContext(org.pentaho.platform.api.engine.IPentahoRequestContext) StringWriter(java.io.StringWriter) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource) ActionSequenceResource(org.pentaho.platform.engine.services.actionsequence.ActionSequenceResource) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 9 with IActionSequenceResource

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

the class SolutionURIResolver method loadXsl.

public InputStream loadXsl(final String name) {
    InputStream xslIS = null;
    IActionSequenceResource resource = new // $NON-NLS-1$ //$NON-NLS-2$
    ActionSequenceResource(// $NON-NLS-1$ //$NON-NLS-2$
    "", // $NON-NLS-1$ //$NON-NLS-2$
    IActionSequenceResource.SOLUTION_FILE_RESOURCE, // $NON-NLS-1$ //$NON-NLS-2$
    "text/xml", name);
    xslIS = resource.getInputStream(RepositoryFilePermission.READ, LocaleHelper.getLocale());
    return xslIS;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource) ActionSequenceResource(org.pentaho.platform.engine.services.actionsequence.ActionSequenceResource) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource)

Example 10 with IActionSequenceResource

use of org.pentaho.platform.api.engine.IActionSequenceResource 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();
}
Also used : Set(java.util.Set) IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) HashMap(java.util.HashMap) Element(org.dom4j.Element) OutputStream(java.io.OutputStream) Document(org.dom4j.Document) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource) SimpleContentItem(org.pentaho.platform.engine.core.output.SimpleContentItem) Method(java.lang.reflect.Method) IContentItem(org.pentaho.platform.api.repository.IContentItem) SystemSettingsParameterProvider(org.pentaho.platform.engine.core.solution.SystemSettingsParameterProvider) IActionParameter(org.pentaho.platform.api.engine.IActionParameter)

Aggregations

IActionSequenceResource (org.pentaho.platform.api.engine.IActionSequenceResource)28 InputStream (java.io.InputStream)10 IOException (java.io.IOException)9 ActionSequenceResource (org.pentaho.platform.engine.services.actionsequence.ActionSequenceResource)7 OutputStream (java.io.OutputStream)5 FileInputStream (java.io.FileInputStream)4 Set (java.util.Set)4 Document (org.dom4j.Document)4 Node (org.dom4j.Node)4 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)4 IActionParameter (org.pentaho.platform.api.engine.IActionParameter)4 MasterReport (org.pentaho.reporting.engine.classic.core.MasterReport)4 ReportGenerator (org.pentaho.reporting.engine.classic.core.modules.parser.base.ReportGenerator)4 BufferedInputStream (java.io.BufferedInputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 URL (java.net.URL)3 Iterator (java.util.Iterator)3