001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Feb 01, 2006
014: * @author Michael D'Amour
015: */
016: package org.pentaho.jfreereport.wizard.utility;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.File;
020: import java.io.FileOutputStream;
021: import java.io.OutputStream;
022: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.List;
025:
026: import org.dom4j.Document;
027: import org.dom4j.Element;
028: import org.dom4j.Node;
029: import org.dom4j.io.OutputFormat;
030: import org.dom4j.io.SAXReader;
031: import org.dom4j.io.XMLWriter;
032: import org.pentaho.core.connection.IPentahoResultSet;
033: import org.pentaho.core.runtime.IRuntimeContext;
034: import org.pentaho.core.session.StandaloneSession;
035: import org.pentaho.core.solution.IActionCompleteListener;
036: import org.pentaho.core.solution.ISolutionEngine;
037: import org.pentaho.core.solution.SimpleOutputHandler;
038: import org.pentaho.core.solution.SimpleParameterProvider;
039: import org.pentaho.core.system.PentahoSystem;
040: import org.pentaho.core.ui.IPentahoUrlFactory;
041: import org.pentaho.core.ui.SimpleUrlFactory;
042: import org.pentaho.jfreereport.wizard.utility.report.ReportParameterUtility;
043: import org.pentaho.util.logging.ILogger;
044:
045: public class ActionSequenceUtility implements IActionCompleteListener {
046: private static ActionSequenceUtility instance;
047:
048: public static ActionSequenceUtility getInstance() {
049: if (instance == null) {
050: instance = new ActionSequenceUtility();
051: }
052: return instance;
053: }
054:
055: public void createKettleActionSequence(String actionSeqDocInput,
056: String actionSeqDocOutput, String kettleTransformationFile,
057: String kettleStep) {
058: SAXReader reader = new SAXReader();
059: try {
060: File in = new File(actionSeqDocInput);
061: File out = new File(actionSeqDocOutput);
062: File ktrFile = new File(kettleTransformationFile);
063: Document document = reader.read(in);
064: document
065: .selectSingleNode("/action-sequence/name").setText(out.getName()); //$NON-NLS-1$
066: document
067: .selectSingleNode(
068: "/action-sequence/resources/transformation-file/solution-file/location").setText(ktrFile.getName()); //$NON-NLS-1$
069: List actionDefinitionList = document
070: .selectNodes("/action-sequence/actions/action-definition"); //$NON-NLS-1$
071: for (int i = 0; i < actionDefinitionList.size(); i++) {
072: Node actionDefinition = (Node) actionDefinitionList
073: .get(i);
074: String compDefName = actionDefinition.selectSingleNode(
075: "component-name").getText(); //$NON-NLS-1$
076: if (compDefName != null
077: && compDefName.equals("KettleComponent")) { //$NON-NLS-1$
078: Node kettleStepNode = actionDefinition
079: .selectSingleNode("component-definition/importstep"); //$NON-NLS-1$
080: kettleStepNode.setText(kettleStep);
081: break;
082: }
083: }
084: FileOutputStream outStream = new FileOutputStream(out);
085: OutputFormat format = OutputFormat.createPrettyPrint();
086: XMLWriter writer = new XMLWriter(outStream, format);
087: writer.write(document);
088: writer.close();
089: } catch (Exception e) {
090: e.printStackTrace(System.err);
091: }
092: }
093:
094: public void createXQueryActionSequence(String actionSeqDocInput,
095: String actionSeqDocOutput, String outputType,
096: String jfreeDoc, String xQuerySource, String query) {
097: SAXReader reader = new SAXReader();
098: try {
099: File in = new File(actionSeqDocInput);
100: File out = new File(actionSeqDocOutput);
101: File jfreeFile = new File(jfreeDoc);
102: Document document = reader.read(in);
103: document
104: .selectSingleNode("/action-sequence/name").setText(out.getName()); //$NON-NLS-1$
105: document
106: .selectSingleNode(
107: "/action-sequence/inputs/output-type/default-value").setText(outputType); //$NON-NLS-1$
108: document
109: .selectSingleNode(
110: "/action-sequence/resources/report-definition/solution-file/location").setText(jfreeFile.getName()); //$NON-NLS-1$
111: document
112: .selectSingleNode(
113: "/action-sequence/resources/document/solution-file/location").setText(xQuerySource); //$NON-NLS-1$
114: List actionDefinitionList = document
115: .selectNodes("/action-sequence/actions/action-definition"); //$NON-NLS-1$
116: for (int i = 0; i < actionDefinitionList.size(); i++) {
117: Node actionDefinition = (Node) actionDefinitionList
118: .get(i);
119: String compDefName = actionDefinition.selectSingleNode(
120: "component-name").getText(); //$NON-NLS-1$
121: if (compDefName != null
122: && compDefName.equals("XQueryLookupRule")) { //$NON-NLS-1$
123: Node queryNode = actionDefinition
124: .selectSingleNode("component-definition/query"); //$NON-NLS-1$
125: queryNode.setText(query);
126: }
127: }
128: FileOutputStream outStream = new FileOutputStream(out);
129: OutputFormat format = OutputFormat.createPrettyPrint();
130: XMLWriter writer = new XMLWriter(outStream, format);
131: writer.write(document);
132: writer.close();
133: } catch (Exception e) {
134: e.printStackTrace(System.err);
135: }
136: }
137:
138: public void createResultSetActionSequence(String actionSeqDocInput,
139: String actionSeqDocOutput, String outputType,
140: String jfreeDoc, IPentahoResultSet resultSet) {
141: SAXReader reader = new SAXReader();
142: try {
143: File in = new File(actionSeqDocInput);
144: File out = new File(actionSeqDocOutput);
145: File jfreeFile = new File(jfreeDoc);
146: Document document = reader.read(in);
147: document
148: .selectSingleNode("/action-sequence/name").setText(out.getName()); //$NON-NLS-1$
149: document
150: .selectSingleNode(
151: "/action-sequence/inputs/output-type/default-value").setText(outputType); //$NON-NLS-1$
152: document
153: .selectSingleNode(
154: "/action-sequence/resources/report-definition/solution-file/location").setText(jfreeFile.getName()); //$NON-NLS-1$
155: FileOutputStream outStream = new FileOutputStream(out);
156: OutputFormat format = OutputFormat.createPrettyPrint();
157: XMLWriter writer = new XMLWriter(outStream, format);
158: writer.write(document);
159: writer.close();
160: } catch (Exception e) {
161: e.printStackTrace(System.err);
162: }
163: }
164:
165: public void createActionSequence(String actionSeqDocInput,
166: String actionSeqDocOutput, String outputType,
167: String jfreeDoc, String mondrianCubeDef, String source,
168: String live, String jndi, String driver,
169: String connectInfo, String username, String password,
170: String query) {
171: SAXReader reader = new SAXReader();
172: try {
173: File in = new File(actionSeqDocInput);
174: File out = new File(actionSeqDocOutput);
175: File jfreeFile = new File(jfreeDoc);
176: Document document = reader.read(in);
177: List actionDefinitionList = document
178: .selectNodes("/action-sequence/actions/*"); //$NON-NLS-1$
179: Element lookupRuleElement = null;
180: for (int i = 0; i < actionDefinitionList.size(); i++) {
181: Element possibleActionDefinitionElement = (Element) actionDefinitionList
182: .get(i);
183: if (possibleActionDefinitionElement
184: .selectSingleNode("component-name").getText().equalsIgnoreCase("SQLLookupRule")) { //$NON-NLS-1$ //$NON-NLS-2$
185: lookupRuleElement = possibleActionDefinitionElement;
186: } else if (possibleActionDefinitionElement
187: .selectSingleNode("component-name").getText().equalsIgnoreCase("MDXLookupRule")) { //$NON-NLS-1$ //$NON-NLS-2$
188: lookupRuleElement = possibleActionDefinitionElement;
189: } else if (possibleActionDefinitionElement
190: .selectSingleNode("component-name").getText()
191: .equalsIgnoreCase("MQLRelationalDataComponent")) {
192: lookupRuleElement = possibleActionDefinitionElement;
193: }
194: }
195: if (source.equals("sql")) { //$NON-NLS-1$
196: // add inputs to the action sequence for each {PARAMETER} found in the query
197: Element seqInputsElement = (Element) document
198: .selectSingleNode("/action-sequence/inputs"); //$NON-NLS-1$
199: List parameterNames = ReportParameterUtility
200: .getParametersKeys(query);
201: for (int i = 0; i < parameterNames.size(); i++) {
202: String param = (String) parameterNames.get(i);
203: Element inputElement = seqInputsElement
204: .addElement(param);
205: inputElement.addAttribute("type", "string"); //$NON-NLS-1$ //$NON-NLS-2$
206: Element defaultValueElement = inputElement
207: .addElement("default-value"); //$NON-NLS-1$
208: defaultValueElement.setText(""); //$NON-NLS-1$
209: Element sourcesElement = inputElement
210: .addElement("sources"); //$NON-NLS-1$
211: Element requestElement = sourcesElement
212: .addElement("request"); //$NON-NLS-1$
213: requestElement.setText(param);
214: }
215: // add these inputs to the component inputs
216: Element componentInputs = (Element) lookupRuleElement
217: .selectSingleNode("action-inputs"); //$NON-NLS-1$
218: for (int i = 0; i < parameterNames.size(); i++) {
219: String param = (String) parameterNames.get(i);
220: Element inputElement = componentInputs
221: .addElement(param);
222: inputElement.addAttribute("type", "string"); //$NON-NLS-1$ //$NON-NLS-2$
223: }
224: // patch up query for use in action-sequence
225: query = ReportParameterUtility
226: .setupParametersForActionSequence(query);
227: }
228:
229: document
230: .selectSingleNode("/action-sequence/name").setText(out.getName()); //$NON-NLS-1$
231: document
232: .selectSingleNode(
233: "/action-sequence/inputs/output-type/default-value").setText(outputType); //$NON-NLS-1$
234: document
235: .selectSingleNode(
236: "/action-sequence/resources/report-definition/solution-file/location").setText(jfreeFile.getName()); //$NON-NLS-1$
237: // ((Element)compDefNode.selectSingleNode("query")).setText("<![CDATA[" + // query + "]]>"); //$NON-NLS-1$
238: // ((Element)compDefNode.selectSingleNode("query")).setText(query); //$NON-NLS-1$
239: if (lookupRuleElement != null && !source.equals("mql")) {
240: Element queryElement = (Element) lookupRuleElement
241: .selectSingleNode("component-definition/query"); //$NON-NLS-1$
242: queryElement.clearContent(); //$NON-NLS-1$
243: queryElement.addCDATA(query); //$NON-NLS-1$
244: } else if (source.equals("mql")) {
245:
246: reader = new SAXReader();
247: Document queryDocument = reader
248: .read(new ByteArrayInputStream(query.getBytes()));
249:
250: Element queryElement = (Element) lookupRuleElement
251: .selectSingleNode("component-definition/mql"); //$NON-NLS-1$
252: Element componentDefinitionElement = (Element) lookupRuleElement
253: .selectSingleNode("component-definition"); //$NON-NLS-1$
254: componentDefinitionElement.remove(queryElement);
255: componentDefinitionElement.add(queryDocument
256: .selectSingleNode("/mql"));
257: }
258: if (source.equals("mdx")) { //$NON-NLS-1$
259: String cubeDef = new File(mondrianCubeDef).getName();
260: document
261: .selectSingleNode(
262: "/action-sequence/resources/catalog/solution-file/location").setText(cubeDef); //$NON-NLS-1$
263: Element temp = (Element) lookupRuleElement
264: .selectSingleNode("component-definition/user-id"); //$NON-NLS-1$
265: // temp.setText(username);
266: temp.detach();
267: temp = (Element) lookupRuleElement
268: .selectSingleNode("component-definition/password"); //$NON-NLS-1$
269: // temp.setText(password);
270: temp.detach();
271: temp = (Element) lookupRuleElement
272: .selectSingleNode("component-definition/connection"); //$NON-NLS-1$
273: // temp.clearContent();
274: // temp.addCDATA(connectInfo);
275: temp.detach();
276: temp = (Element) lookupRuleElement
277: .selectSingleNode("component-definition/location"); //$NON-NLS-1$
278: temp.setText("mondrian");
279: Node jndiNode = lookupRuleElement
280: .selectSingleNode("component-definition/jndi"); //$NON-NLS-1$
281: jndiNode.setText(jndi);
282: } else if (source.equals("sql")) {
283: Node jndiNode = lookupRuleElement
284: .selectSingleNode("component-definition/jndi"); //$NON-NLS-1$
285: jndiNode.setText(jndi);
286: }
287: FileOutputStream outStream = new FileOutputStream(out);
288: OutputFormat format = OutputFormat.createPrettyPrint();
289: XMLWriter writer = new XMLWriter(outStream, format);
290: writer.write(document);
291: writer.close();
292: } catch (Exception e) {
293: e.printStackTrace(System.err);
294: }
295: }
296:
297: public IRuntimeContext executeKettleSequence(
298: String solutionRootPath, String solutionId, String path,
299: String actionName, IActionCompleteListener acl)
300: throws Exception {
301: IRuntimeContext runtimeContext = null;
302: try {
303: PentahoUtility.startup();
304: List messages = new ArrayList();
305: // PentahoSystem.systemEntryPoint();
306: String instanceId = null;
307: SimpleParameterProvider parameterProvider = new SimpleParameterProvider();
308: parameterProvider.setParameter("logging-level", "error"); //$NON-NLS-1$ //$NON-NLS-2$
309: parameterProvider.setParameter("level", "error"); //$NON-NLS-1$ //$NON-NLS-2$
310: StandaloneSession session = new StandaloneSession(
311: "Kettle-Session"); //$NON-NLS-1$
312: session.setLoggingLevel(ILogger.ERROR);
313: ISolutionEngine solutionEngine = PentahoSystem
314: .getSolutionEngineInstance(null);
315: solutionEngine.setLoggingLevel(ILogger.ERROR);
316: solutionEngine.init(session);
317: String baseUrl = ""; //$NON-NLS-1$
318: HashMap parameterProviderMap = new HashMap();
319: parameterProviderMap.put("request", parameterProvider); //$NON-NLS-1$
320: IPentahoUrlFactory urlFactory = new SimpleUrlFactory(
321: baseUrl);
322: SimpleOutputHandler outputHandler = null;
323: solutionEngine.setSession(session);
324: runtimeContext = solutionEngine
325: .execute(
326: solutionId,
327: path,
328: actionName,
329: "JFreeReport Wizard:Kettle", false, true, instanceId, false, parameterProviderMap, outputHandler, (acl == null ? this : acl), urlFactory, messages); //$NON-NLS-1$
330: } finally {
331: // PentahoSystem.systemExitPoint();
332: try {
333: } catch (Exception ex) {
334: ex.printStackTrace();
335: }
336: try {
337: } catch (Exception ex) {
338: ex.printStackTrace();
339: }
340: }
341: return runtimeContext;
342: }
343:
344: public File executeActionSequence(String solutionRootPath,
345: String solutionId, String path, String actionName,
346: String outputType, IPentahoResultSet resultSet,
347: IActionCompleteListener acl) throws Exception {
348: OutputStream outputStream = null;
349: File generatedReportFile = null;
350: try {
351: PentahoUtility.startup();
352: generatedReportFile = File.createTempFile(
353: "JFreeDesigner", "." + outputType); //$NON-NLS-1$ //$NON-NLS-2$
354: List messages = new ArrayList();
355: // PentahoSystem.systemEntryPoint();
356: String instanceId = null;
357: SimpleParameterProvider parameterProvider = new SimpleParameterProvider();
358: if (resultSet != null) {
359: parameterProvider.setParameter("result-set", resultSet); //$NON-NLS-1$
360: }
361: parameterProvider.setParameter("type", outputType); //$NON-NLS-1$
362: parameterProvider.setParameter("logging-level", "error"); //$NON-NLS-1$ //$NON-NLS-2$
363: parameterProvider.setParameter("level", "error"); //$NON-NLS-1$ //$NON-NLS-2$
364: StandaloneSession session = new StandaloneSession(
365: "JFreeReport-Session"); //$NON-NLS-1$
366: session.setLoggingLevel(ILogger.ERROR);
367: ISolutionEngine solutionEngine = PentahoSystem
368: .getSolutionEngineInstance(null);
369: solutionEngine.setLoggingLevel(ILogger.ERROR);
370: solutionEngine.init(session);
371: String baseUrl = ""; //$NON-NLS-1$
372: HashMap parameterProviderMap = new HashMap();
373: parameterProviderMap.put("request", parameterProvider); //$NON-NLS-1$
374: IPentahoUrlFactory urlFactory = new SimpleUrlFactory(
375: baseUrl);
376: SimpleOutputHandler outputHandler = null;
377: outputStream = new FileOutputStream(generatedReportFile);
378: if (outputStream != null) {
379: outputHandler = new SimpleOutputHandler(outputStream,
380: false);
381: }
382: solutionEngine.setSession(session);
383: solutionEngine
384: .execute(
385: solutionId,
386: path,
387: actionName,
388: "JFreeReport Designer", false, true, instanceId, false, parameterProviderMap, outputHandler, (acl == null ? this : acl), urlFactory, messages); //$NON-NLS-1$
389: } finally {
390: // PentahoSystem.systemExitPoint();
391: try {
392: outputStream.flush();
393: } catch (Exception ex) {
394: ex.printStackTrace();
395: }
396: try {
397: outputStream.close();
398: } catch (Exception ex) {
399: ex.printStackTrace();
400: }
401: }
402: return generatedReportFile;
403: }
404:
405: public void actionComplete(IRuntimeContext runtimeContext) {
406: }
407: }
|