KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > xquery > XQueryBaseComponent


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * Created Sep 21, 2005
14  * @author wseyler
15  */

16 package org.pentaho.plugin.xquery;
17
18 import java.io.BufferedWriter JavaDoc;
19 import java.io.File JavaDoc;
20 import java.io.FileWriter JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
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 JavaDoc FILENAME_PREFIX = "tmp"; //$NON-NLS-1$
48

49     private static final String JavaDoc EXTENSION = ".xml"; //$NON-NLS-1$
50

51     private static final String JavaDoc DOCUMENT = "document"; //$NON-NLS-1$
52

53     private static final String JavaDoc TEMP_DIRECTORY = "system/tmp/"; //$NON-NLS-1$
54

55     private static final String JavaDoc XML_DOCUMENT_TAG = "XML_DOCUMENT"; //$NON-NLS-1$
56

57     public abstract boolean validateSystemSettings();
58
59     public abstract String JavaDoc 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())); //$NON-NLS-1$
71
return false;
72             }
73             if (!isDefinedInput(StandardSettings.SQL_QUERY)) {
74                 error(Messages.getErrorString("XQueryBaseComponent.ERROR_0001_QUERY_NOT_SPECIFIED", getActionName())); //$NON-NLS-1$
75
return false;
76             }
77             String JavaDoc outputName = getResultOutputName();
78             if (outputName != null) {
79                 if (!isDefinedOutput(getResultOutputName())) {
80                     error(Messages.getErrorString("XQueryBaseComponent.ERROR_0003_OUTPUT_NOT_SPECIFIED", getActionName())); //$NON-NLS-1$
81
return false;
82                 }
83             }
84             return true;
85         } catch (Exception JavaDoc e) {
86             error(Messages.getErrorString("XQueryBaseComponent.ERROR_0004_VALIDATION_FAILED", getActionName()), e); //$NON-NLS-1$
87
}
88         return false;
89     }
90
91     public void done() {
92         // TODO Auto-generated method stub
93
}
94
95     protected boolean executeAction() {
96         try {
97             IPentahoConnection connection = getConnection();
98             if (connection == null) {
99                 return false;
100             }
101             String JavaDoc query = getInputStringValue(StandardSettings.SQL_QUERY);
102             return runQuery(connection, query);
103         } catch (Exception JavaDoc e) {
104             error(Messages.getErrorString("XQueryBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName()), e); //$NON-NLS-1$
105
}
106         return false;
107     }
108
109     protected boolean runQuery(IPentahoConnection connection, String JavaDoc rawQuery) {
110         try {
111             String JavaDoc columnTypes[] = null;
112             if (connection == null) {
113                 return false;
114             }
115             if (debug) {
116                 debug(Messages.getString("XQueryBaseComponent.DEBUG_RUNNING_QUERY", rawQuery)); //$NON-NLS-1$
117
}
118             String JavaDoc 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                 // we have a local document to use as the data source
125
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 JavaDoc documentFile = null;
136             if (resourceType != IActionResource.URL_RESOURCE) {
137               // check that the document exists
138
documentFile = new File JavaDoc(documentPath);
139               if (!documentFile.exists()) {
140                   error(Messages.getString("XQueryBaseComponent.ERROR_0007_FILE_NOT_FOUND", documentPath)); //$NON-NLS-1$
141
return false;
142               }
143               // convert any '\' to '/'
144
documentPath = documentFile.getCanonicalPath();
145               documentPath = documentPath.replaceAll("\\\\", "/"); //$NON-NLS-1$ //$NON-NLS-2$
146
}
147             
148             SAXReader reader = new SAXReader();
149             try {
150                 Document document;
151                 if (resourceType == IActionResource.URL_RESOURCE) {
152                   document = reader.read(new URL JavaDoc(documentPath));
153                 } else {
154                   document = reader.read(documentFile);
155                 }
156                 Node commentNode = document.selectSingleNode("/result-set/comment()"); //$NON-NLS-1$
157
if (commentNode != null) {
158                     String JavaDoc commentString = commentNode.getText();
159                     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(commentString, ","); //$NON-NLS-1$
160
List JavaDoc columnTypesList = new LinkedList JavaDoc();
161                     while (st.hasMoreTokens()) {
162                         String JavaDoc token = st.nextToken();
163                         columnTypesList.add(token);
164                     }
165                     columnTypes = (String JavaDoc[]) columnTypesList.toArray(new String JavaDoc[0]);
166                 }
167             } catch (Exception JavaDoc e) {
168                 e.printStackTrace();
169             }
170             if (rawQuery.indexOf("{"+XML_DOCUMENT_TAG+"}") >= 0) { //$NON-NLS-1$//$NON-NLS-2$
171
rawQuery = TemplateUtil.applyTemplate(rawQuery, XML_DOCUMENT_TAG, documentPath);
172             } else {
173                 rawQuery = "doc(\"" + documentPath + "\")" + rawQuery; //$NON-NLS-1$ //$NON-NLS-2$
174
}
175         
176             return runFinalQuery(connection, rawQuery, columnTypes);
177         } catch (Exception JavaDoc e) {
178             e.printStackTrace();
179             return false;
180         }
181     }
182
183     private boolean runFinalQuery(IPentahoConnection connection, String JavaDoc rawQuery, String JavaDoc[] columnTypes) {
184         boolean success = false;
185         String JavaDoc finalQuery = applyInputsToFormat(rawQuery);
186         // execute the query, read the results and cache them
187
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             // TODO Auto-generated catch block
205
e.printStackTrace();
206         } finally {
207             connection.close();
208         }
209         return success;
210     }
211
212     private String JavaDoc createTempXMLFile(String JavaDoc xmlString) {
213         // Save it to a temporary file
214
File JavaDoc file;
215         String JavaDoc documentPath = null;
216         try {
217             file = File.createTempFile(FILENAME_PREFIX , EXTENSION , new File JavaDoc(PentahoSystem.getApplicationContext().getFileOutputPath(TEMP_DIRECTORY)));
218             file.deleteOnExit();
219             documentPath = file.getCanonicalPath();
220             
221             BufferedWriter JavaDoc out = new BufferedWriter JavaDoc(new FileWriter JavaDoc(file));
222             out.write(xmlString);
223             out.close();
224         } catch (IOException JavaDoc e) {
225             e.printStackTrace();
226         }
227         
228         documentPath = documentPath.replaceAll("\\\\", "/"); //$NON-NLS-1$ //$NON-NLS-2$
229
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")); //$NON-NLS-1$
240
return null;
241             }
242             return connection;
243         } catch (Exception JavaDoc e) {
244             error(Messages.getErrorString("XQueryBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName()), e); //$NON-NLS-1$
245
}
246         return null;
247     }
248
249     public boolean init() {
250         return true;
251     }
252 }
253
Popular Tags