Search in sources :

Example 1 with XQueryAction

use of org.pentaho.actionsequence.dom.actions.XQueryAction in project pentaho-platform by pentaho.

the class XQueryBaseComponent method executeAction.

@Override
protected boolean executeAction() {
    boolean result = false;
    IActionDefinition actionDefinition = getActionDefinition();
    // int queryTimeout = -1;
    if (actionDefinition instanceof XQueryAction) {
        XQueryAction xQueryAction = (XQueryAction) actionDefinition;
        // Not implemented yet
        // IActionInput queryTimeoutInput = xQueryAction.getQueryTimeout();
        IActionInput maxRowsInput = xQueryAction.getMaxRows();
        if (maxRowsInput != ActionInputConstant.NULL_INPUT) {
            this.setMaxRows(maxRowsInput.getIntValue());
        }
        IPreparedComponent sharedConnection = (IPreparedComponent) xQueryAction.getSharedConnection().getValue();
        if (sharedConnection != null) {
            connectionOwner = false;
            connection = sharedConnection.shareConnection();
        } else {
            connection = getConnection();
        }
        if (connection == null) {
            error(Messages.getInstance().getErrorString("IPreparedComponent.ERROR_0002_CONNECTION_NOT_AVAILABLE", // $NON-NLS-1$
            getActionName()));
        } else if (connection.getDatasourceType() != IPentahoConnection.XML_DATASOURCE) {
            error(Messages.getInstance().getErrorString("IPreparedComponent.ERROR_0001_INVALID_CONNECTION_TYPE", // $NON-NLS-1$
            getActionName()));
        } else {
            result = runQuery(connection, xQueryAction.getQuery().getStringValue());
        }
    } else if (actionDefinition instanceof XQueryConnectionAction) {
        XQueryConnectionAction xQueryConnectionAction = (XQueryConnectionAction) getActionDefinition();
        connection = getConnection();
        if (connection == null) {
            error(Messages.getInstance().getErrorString("IPreparedComponent.ERROR_0002_CONNECTION_NOT_AVAILABLE", // $NON-NLS-1$
            getActionName()));
        } else if (connection.getDatasourceType() != IPentahoConnection.XML_DATASOURCE) {
            error(Messages.getInstance().getErrorString("IPreparedComponent.ERROR_0001_INVALID_CONNECTION_TYPE", // $NON-NLS-1$
            getActionName()));
        } else {
            xQueryConnectionAction.getOutputConnection().setValue(this);
            result = true;
        }
    }
    return result;
}
Also used : IActionInput(org.pentaho.actionsequence.dom.IActionInput) IActionDefinition(org.pentaho.actionsequence.dom.IActionDefinition) XQueryConnectionAction(org.pentaho.actionsequence.dom.actions.XQueryConnectionAction) XQueryAction(org.pentaho.actionsequence.dom.actions.XQueryAction) IPreparedComponent(org.pentaho.platform.api.data.IPreparedComponent)

Example 2 with XQueryAction

use of org.pentaho.actionsequence.dom.actions.XQueryAction in project pentaho-platform by pentaho.

the class XQueryBaseComponent method runFinalQuery.

protected boolean runFinalQuery(final IPentahoConnection localConnection, final String rawQuery, final String[] columnTypes) {
    XQueryAction xQueryAction = (XQueryAction) getActionDefinition();
    boolean success = false;
    String finalQuery = applyInputsToFormat(rawQuery);
    // execute the query, read the results and cache them
    try {
        IPentahoResultSet resultSet = ((XQConnection) localConnection).executeQuery(finalQuery, columnTypes);
        if (resultSet != null) {
            if (!xQueryAction.getLive().getBooleanValue(true)) {
                resultSet = resultSet.memoryCopy();
            }
            try {
                IActionOutput resultSetOutput = xQueryAction.getOutputResultSet();
                if (resultSetOutput != null) {
                    resultSetOutput.setValue(resultSet);
                }
                success = true;
            } finally {
                resultSet.close();
            }
        }
    } catch (XPathException e) {
        error(Messages.getInstance().getErrorString("XQueryBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName()), // $NON-NLS-1$
        e);
    }
    return success;
}
Also used : IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) XQConnection(org.pentaho.platform.plugin.services.connections.xquery.XQConnection) XPathException(net.sf.saxon.trans.XPathException) IActionOutput(org.pentaho.actionsequence.dom.IActionOutput) XQueryAction(org.pentaho.actionsequence.dom.actions.XQueryAction)

Example 3 with XQueryAction

use of org.pentaho.actionsequence.dom.actions.XQueryAction in project pentaho-platform by pentaho.

the class XQueryBaseComponent method runQuery.

protected boolean runQuery(final IPentahoConnection localConnection, String rawQuery) {
    XQueryAction xQueryAction = (XQueryAction) getActionDefinition();
    try {
        if (localConnection == null) {
            return false;
        }
        if (ComponentBase.debug) {
            // $NON-NLS-1$
            debug(Messages.getInstance().getString("XQueryBaseComponent.DEBUG_RUNNING_QUERY", rawQuery));
        }
        String documentPath = null;
        int resourceType = -1;
        String srcXml = xQueryAction.getSourceXml().getStringValue();
        org.pentaho.actionsequence.dom.IActionResource xmlResource = xQueryAction.getXmlDocument();
        InputStream inputStream = null;
        URL url = null;
        if (srcXml != null) {
            inputStream = new FileInputStream(new File(createTempXMLFile(srcXml)));
        } else if (xmlResource != null) {
            // we have a local document to use as the data source
            IActionSequenceResource resource = getResource(xmlResource.getName());
            resourceType = resource.getSourceType();
            if ((resourceType == IActionSequenceResource.SOLUTION_FILE_RESOURCE) || (resourceType == IActionSequenceResource.FILE_RESOURCE)) {
                inputStream = resource.getInputStream(RepositoryFilePermission.READ);
            } else if (resourceType == IActionSequenceResource.XML) {
                inputStream = new FileInputStream(new File(createTempXMLFile(resource.getAddress())));
            } else {
                url = new URL(documentPath);
            }
        }
        // Retrieve the column types
        String[] columnTypes = null;
        if (retrieveColumnTypes()) {
            try {
                SAXReader reader = XMLParserFactoryProducer.getSAXReader(null);
                Document document;
                if (url != null) {
                    document = reader.read(url);
                } else {
                    document = reader.read(inputStream);
                }
                // $NON-NLS-1$
                Node commentNode = document.selectSingleNode("/result-set/comment()");
                if (commentNode != null) {
                    String commentString = commentNode.getText();
                    // $NON-NLS-1$
                    StringTokenizer st = new StringTokenizer(commentString, ",");
                    List columnTypesList = new LinkedList();
                    while (st.hasMoreTokens()) {
                        String token = st.nextToken().trim();
                        columnTypesList.add(token);
                    }
                    columnTypes = (String[]) columnTypesList.toArray(new String[0]);
                }
            } catch (Exception e) {
                getLogger().warn(Messages.getInstance().getString("XQueryBaseComponent.ERROR_0009_ERROR_BUILDING_COLUMN_TYPES"), // $NON-NLS-1$
                e);
            }
        }
        if (rawQuery != null) {
            if (rawQuery.indexOf("{" + XQueryBaseComponent.XML_DOCUMENT_TAG + "}") >= 0) {
                // $NON-NLS-1$//$NON-NLS-2$
                rawQuery = TemplateUtil.applyTemplate(rawQuery, XQueryBaseComponent.XML_DOCUMENT_TAG, documentPath);
            } else {
                Calendar now = Calendar.getInstance();
                File temp = File.createTempFile("tempXQuery" + now.getTimeInMillis(), ".xml");
                temp.deleteOnExit();
                OutputStream out = new FileOutputStream(temp);
                IActionSequenceResource resource = getResource(xmlResource.getName());
                inputStream = resource.getInputStream(RepositoryFilePermission.READ);
                byte[] buf = new byte[1024];
                int len;
                while ((len = inputStream.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.close();
                inputStream.close();
                documentPath = temp.getAbsolutePath();
                documentPath = FilenameUtils.separatorsToUnix(documentPath);
                // $NON-NLS-1$ //$NON-NLS-2$
                rawQuery = "doc(\"" + documentPath + "\")" + rawQuery;
            }
        }
        if (xQueryAction.getOutputPreparedStatement() != null) {
            return prepareFinalQuery(rawQuery, columnTypes);
        } else {
            return runFinalQuery(localConnection, rawQuery, columnTypes);
        }
    } catch (Exception e) {
        getLogger().error(Messages.getInstance().getString("XQueryBaseComponent.ERROR_0010_ERROR_RUNNING_QUERY"), // $NON-NLS-1$
        e);
        return false;
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SAXReader(org.dom4j.io.SAXReader) Node(org.dom4j.Node) Calendar(java.util.Calendar) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) XQueryAction(org.pentaho.actionsequence.dom.actions.XQueryAction) Document(org.dom4j.Document) URL(java.net.URL) FileInputStream(java.io.FileInputStream) LinkedList(java.util.LinkedList) XPathException(net.sf.saxon.trans.XPathException) IOException(java.io.IOException) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource) StringTokenizer(java.util.StringTokenizer) FileOutputStream(java.io.FileOutputStream) LinkedList(java.util.LinkedList) List(java.util.List) File(java.io.File)

Example 4 with XQueryAction

use of org.pentaho.actionsequence.dom.actions.XQueryAction in project pentaho-platform by pentaho.

the class XQueryBaseComponent method validateAction.

@Override
protected boolean validateAction() {
    boolean result = false;
    IActionDefinition actionDefinition = getActionDefinition();
    if (actionDefinition instanceof XQueryAction) {
        XQueryAction xQueryAction = (XQueryAction) actionDefinition;
        if ((xQueryAction.getSourceXml() == ActionInputConstant.NULL_INPUT) && (xQueryAction.getXmlDocument() == null)) {
            error(Messages.getInstance().getString("XQueryBaseComponent.ERROR_0008_SOURCE_NOT_DEFINED", // $NON-NLS-1$
            getActionName()));
        } else if (xQueryAction.getQuery() == ActionInputConstant.NULL_INPUT) {
            error(Messages.getInstance().getErrorString("XQueryBaseComponent.ERROR_0001_QUERY_NOT_SPECIFIED", // $NON-NLS-1$
            getActionName()));
        } else if ((xQueryAction.getOutputPreparedStatement() == null) && (xQueryAction.getOutputResultSet() == null)) {
            error(Messages.getInstance().getErrorString("XQueryBaseComponent.ERROR_0003_OUTPUT_NOT_SPECIFIED", // $NON-NLS-1$
            getActionName()));
        } else {
            result = true;
        }
    } else if (actionDefinition instanceof XQueryConnectionAction) {
        XQueryConnectionAction xQueryConnectionAction = (XQueryConnectionAction) actionDefinition;
        if (xQueryConnectionAction.getOutputConnection() == null) {
            error(Messages.getInstance().getErrorString("XQueryBaseComponent.ERROR_0003_OUTPUT_NOT_SPECIFIED", // $NON-NLS-1$
            getActionName()));
        } else {
            result = true;
        }
    } else {
        error(Messages.getInstance().getErrorString("ComponentBase.ERROR_0001_UNKNOWN_ACTION_TYPE", // $NON-NLS-1$
        actionDefinition.getElement().asXML()));
    }
    return result;
}
Also used : IActionDefinition(org.pentaho.actionsequence.dom.IActionDefinition) XQueryConnectionAction(org.pentaho.actionsequence.dom.actions.XQueryConnectionAction) XQueryAction(org.pentaho.actionsequence.dom.actions.XQueryAction)

Aggregations

XQueryAction (org.pentaho.actionsequence.dom.actions.XQueryAction)4 XPathException (net.sf.saxon.trans.XPathException)2 IActionDefinition (org.pentaho.actionsequence.dom.IActionDefinition)2 XQueryConnectionAction (org.pentaho.actionsequence.dom.actions.XQueryConnectionAction)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 URL (java.net.URL)1 Calendar (java.util.Calendar)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 StringTokenizer (java.util.StringTokenizer)1 Document (org.dom4j.Document)1 Node (org.dom4j.Node)1 SAXReader (org.dom4j.io.SAXReader)1 IActionInput (org.pentaho.actionsequence.dom.IActionInput)1 IActionOutput (org.pentaho.actionsequence.dom.IActionOutput)1