1 16 package org.pentaho.plugin.xquery; 17 18 import java.io.BufferedWriter ; 19 import java.io.File ; 20 import java.io.FileWriter ; 21 import java.io.IOException ; 22 import java.net.URL ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 import java.util.StringTokenizer ; 26 27 import net.sf.saxon.trans.XPathException; 28 29 import org.apache.commons.logging.Log; 30 import org.dom4j.Document; 31 import org.dom4j.Node; 32 import org.dom4j.io.SAXReader; 33 import org.pentaho.core.connection.IPentahoConnection; 34 import org.pentaho.core.connection.IPentahoResultSet; 35 import org.pentaho.core.solution.IActionResource; 36 import org.pentaho.core.system.PentahoSystem; 37 import org.pentaho.core.util.TemplateUtil; 38 import org.pentaho.data.PentahoConnectionFactory; 39 import org.pentaho.data.connection.xquery.XQConnection; 40 import org.pentaho.messages.Messages; 41 import org.pentaho.plugin.ComponentBase; 42 import org.pentaho.plugin.core.StandardSettings; 43 44 public abstract class XQueryBaseComponent extends ComponentBase { 45 private IPentahoResultSet rSet; 46 47 private static final String FILENAME_PREFIX = "tmp"; 49 private static final String EXTENSION = ".xml"; 51 private static final String DOCUMENT = "document"; 53 private static final String TEMP_DIRECTORY = "system/tmp/"; 55 private static final String XML_DOCUMENT_TAG = "XML_DOCUMENT"; 57 public abstract boolean validateSystemSettings(); 58 59 public abstract String getResultOutputName(); 60 61 public abstract Log getLogger(); 62 63 public IPentahoResultSet getResultSet() { 64 return rSet; 65 } 66 67 protected boolean validateAction() { 68 try { 69 if (!isDefinedResource(DOCUMENT) && !isDefinedInput(DOCUMENT)) { 70 error(Messages.getString("XQueryBaseComponent.ERROR_0008_SOURCE_NOT_DEFINED", getActionName())); return false; 72 } 73 if (!isDefinedInput(StandardSettings.SQL_QUERY)) { 74 error(Messages.getErrorString("XQueryBaseComponent.ERROR_0001_QUERY_NOT_SPECIFIED", getActionName())); return false; 76 } 77 String outputName = getResultOutputName(); 78 if (outputName != null) { 79 if (!isDefinedOutput(getResultOutputName())) { 80 error(Messages.getErrorString("XQueryBaseComponent.ERROR_0003_OUTPUT_NOT_SPECIFIED", getActionName())); return false; 82 } 83 } 84 return true; 85 } catch (Exception e) { 86 error(Messages.getErrorString("XQueryBaseComponent.ERROR_0004_VALIDATION_FAILED", getActionName()), e); } 88 return false; 89 } 90 91 public void done() { 92 } 94 95 protected boolean executeAction() { 96 try { 97 IPentahoConnection connection = getConnection(); 98 if (connection == null) { 99 return false; 100 } 101 String query = getInputStringValue(StandardSettings.SQL_QUERY); 102 return runQuery(connection, query); 103 } catch (Exception e) { 104 error(Messages.getErrorString("XQueryBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName()), e); } 106 return false; 107 } 108 109 protected boolean runQuery(IPentahoConnection connection, String rawQuery) { 110 try { 111 String columnTypes[] = null; 112 if (connection == null) { 113 return false; 114 } 115 if (debug) { 116 debug(Messages.getString("XQueryBaseComponent.DEBUG_RUNNING_QUERY", rawQuery)); } 118 String documentPath = null; 119 int resourceType = -1; 120 if (isDefinedInput(DOCUMENT)) { 121 documentPath = createTempXMLFile(getInputValue(DOCUMENT).toString()); 122 resourceType = IActionResource.FILE_RESOURCE; 123 } else if (isDefinedResource(DOCUMENT)) { 124 IActionResource resource = getResource(DOCUMENT); 126 resourceType = getResource(DOCUMENT).getSourceType(); 127 if (resourceType == IActionResource.SOLUTION_FILE_RESOURCE) { 128 documentPath = PentahoSystem.getApplicationContext().getSolutionPath(resource.getAddress()); 129 } else if (resourceType == IActionResource.XML) { 130 documentPath = createTempXMLFile(resource.getAddress()); 131 } else { 132 documentPath = resource.getAddress(); 133 } 134 } 135 File documentFile = null; 136 if (resourceType != IActionResource.URL_RESOURCE) { 137 documentFile = new File (documentPath); 139 if (!documentFile.exists()) { 140 error(Messages.getString("XQueryBaseComponent.ERROR_0007_FILE_NOT_FOUND", documentPath)); return false; 142 } 143 documentPath = documentFile.getCanonicalPath(); 145 documentPath = documentPath.replaceAll("\\\\", "/"); } 147 148 SAXReader reader = new SAXReader(); 149 try { 150 Document document; 151 if (resourceType == IActionResource.URL_RESOURCE) { 152 document = reader.read(new URL (documentPath)); 153 } else { 154 document = reader.read(documentFile); 155 } 156 Node commentNode = document.selectSingleNode("/result-set/comment()"); if (commentNode != null) { 158 String commentString = commentNode.getText(); 159 StringTokenizer st = new StringTokenizer (commentString, ","); List columnTypesList = new LinkedList (); 161 while (st.hasMoreTokens()) { 162 String token = st.nextToken(); 163 columnTypesList.add(token); 164 } 165 columnTypes = (String []) columnTypesList.toArray(new String [0]); 166 } 167 } catch (Exception e) { 168 e.printStackTrace(); 169 } 170 if (rawQuery.indexOf("{"+XML_DOCUMENT_TAG+"}") >= 0) { rawQuery = TemplateUtil.applyTemplate(rawQuery, XML_DOCUMENT_TAG, documentPath); 172 } else { 173 rawQuery = "doc(\"" + documentPath + "\")" + rawQuery; } 175 176 return runFinalQuery(connection, rawQuery, columnTypes); 177 } catch (Exception e) { 178 e.printStackTrace(); 179 return false; 180 } 181 } 182 183 private boolean runFinalQuery(IPentahoConnection connection, String rawQuery, String [] columnTypes) { 184 boolean success = false; 185 String finalQuery = applyInputsToFormat(rawQuery); 186 try { 188 IPentahoResultSet resultSet = ((XQConnection) connection).executeQuery(finalQuery, columnTypes); 189 if (resultSet != null) { 190 boolean live = getInputBooleanValue(StandardSettings.LIVE, true); 191 if (!live) { 192 resultSet = resultSet.memoryCopy(); 193 } 194 try { 195 if (this.getResultOutputName() != null) { 196 setOutputValue(this.getResultOutputName(), resultSet); 197 } 198 success = true; 199 } finally { 200 resultSet.close(); 201 } 202 } 203 } catch (XPathException e) { 204 e.printStackTrace(); 206 } finally { 207 connection.close(); 208 } 209 return success; 210 } 211 212 private String createTempXMLFile(String xmlString) { 213 File file; 215 String documentPath = null; 216 try { 217 file = File.createTempFile(FILENAME_PREFIX , EXTENSION , new File (PentahoSystem.getApplicationContext().getFileOutputPath(TEMP_DIRECTORY))); 218 file.deleteOnExit(); 219 documentPath = file.getCanonicalPath(); 220 221 BufferedWriter out = new BufferedWriter (new FileWriter (file)); 222 out.write(xmlString); 223 out.close(); 224 } catch (IOException e) { 225 e.printStackTrace(); 226 } 227 228 documentPath = documentPath.replaceAll("\\\\", "/"); return documentPath; 230 } 231 232 233 234 protected IPentahoConnection getConnection() { 235 IPentahoConnection connection = null; 236 try { 237 connection = PentahoConnectionFactory.getConnection(PentahoConnectionFactory.XML_DATASOURCE, this); 238 if (connection == null) { 239 error(Messages.getErrorString("XQueryBaseComponent.ERROR_0005_INVALID_CONNECTION")); return null; 241 } 242 return connection; 243 } catch (Exception e) { 244 error(Messages.getErrorString("XQueryBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName()), e); } 246 return null; 247 } 248 249 public boolean init() { 250 return true; 251 } 252 } 253 | Popular Tags |