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