KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > jfreereport > components > JFreeReportGenerateDefinitionComponent


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 package org.pentaho.plugin.jfreereport.components;
14
15 import java.io.ByteArrayOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Set JavaDoc;
20 import java.util.zip.ZipEntry JavaDoc;
21 import java.util.zip.ZipInputStream JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.pentaho.core.admin.datasources.DataSourceInfo;
25 import org.pentaho.core.admin.datasources.jboss.JBossDatasourceAdmin;
26 import org.pentaho.core.connection.IPentahoConnection;
27 import org.pentaho.core.connection.IPentahoResultSet;
28 import org.pentaho.core.repository.ISolutionRepository;
29 import org.pentaho.core.runtime.IActionParameter;
30 import org.pentaho.core.solution.IActionResource;
31 import org.pentaho.core.system.PentahoSystem;
32 import org.pentaho.data.PentahoConnectionFactory;
33 import org.pentaho.jfreereport.castormodel.reportspec.ReportSpec;
34 import org.pentaho.jfreereport.wizard.utility.CastorUtility;
35 import org.pentaho.jfreereport.wizard.utility.ReportGenerationUtility;
36 import org.pentaho.jfreereport.wizard.utility.connection.ConnectionUtility;
37 import org.pentaho.plugin.jfreereport.AbstractJFreeReportComponent;
38
39 /**
40  * This component generates report definitions. It uses the report wizard for
41  * the magical generation task.
42  *
43  * @author Thomas Morgner
44  */

45 public class JFreeReportGenerateDefinitionComponent extends
46     AbstractJFreeReportComponent {
47   private static final long serialVersionUID = -6364796568478754207L;
48
49
50   public JFreeReportGenerateDefinitionComponent() {
51   }
52
53   protected boolean validateAction() {
54     if (isDefinedResource(REPORTGENERATEDEFN_REPORTSPECINPUT) == false) {
55       return false;
56     }
57     return true;
58   }
59
60   protected boolean validateSystemSettings() {
61     return true;
62   }
63
64   public void done() {
65
66   }
67
68   /**
69      * Look through the Zip stream and find an entry whose name is .xreportspec.
70      * If the entry is found, return it, otherwise return null.
71      *
72      * @param zStrm
73      * @return If the entry is found, return it, otherwise return null.
74      * @throws IOException
75      */

76     private ZipEntry JavaDoc findReportSpec(ZipInputStream JavaDoc zStrm) throws IOException JavaDoc {
77         ZipEntry JavaDoc reportSpecEntry = null;
78         // for loop has no body
79
for (reportSpecEntry = zStrm.getNextEntry(); null != reportSpecEntry
80                 && !reportSpecEntry.getName().endsWith(".xreportspec"); reportSpecEntry = zStrm
81                 .getNextEntry())
82             ;
83
84         return reportSpecEntry;
85     }
86
87   private ReportSpec loadFromZip(IActionResource resource) {
88     final ISolutionRepository solutionRepository = PentahoSystem
89         .getSolutionRepository(getSession());
90     InputStream JavaDoc reportSpecInputStream = solutionRepository
91         .getResourceInputStream(resource);
92
93     try {
94       ZipInputStream JavaDoc zis = new ZipInputStream JavaDoc(reportSpecInputStream);
95       ZipEntry JavaDoc reportSpecEntry = findReportSpec( zis );
96
97       if (reportSpecEntry == null) {
98         return null;
99       }
100
101       // is this really sane? Blindly using the first zip entry is ... argh!
102
// maybe you should use GZipped streams instead...
103
return (ReportSpec) CastorUtility.getInstance().readCastorObject(zis,
104           ReportSpec.class);
105     } catch (Exception JavaDoc e) {
106       return null;
107     }
108   }
109
110   protected boolean executeAction() {
111     if (isDefinedResource(REPORTGENERATEDEFN_REPORTSPECINPUT) == false) {
112       return false;
113     }
114
115     IActionResource resource = getResource(REPORTGENERATEDEFN_REPORTSPECINPUT);
116     ReportSpec reportSpec = loadFromZip(resource);
117     if (reportSpec == null) {
118       InputStream JavaDoc reportSpecInputStream = PentahoSystem.getSolutionRepository(
119           getSession()).getResourceInputStream(resource);
120       reportSpec = (ReportSpec) CastorUtility.getInstance().readCastorObject(
121           reportSpecInputStream, ReportSpec.class);
122     }
123     if (reportSpec == null) {
124       return false;
125     }
126
127     ByteArrayOutputStream JavaDoc outputStream = new ByteArrayOutputStream JavaDoc();
128     ReportGenerationUtility.createJFreeReportXML(reportSpec, outputStream, 0, 0, false,
129         "", 0, 0); //$NON-NLS-1$
130
String JavaDoc reportDefinition = new String JavaDoc(outputStream.toByteArray());
131     addTempParameterObject(REPORTGENERATEDEFN_REPORTDEFN, reportDefinition);
132
133     // if that parameter is not defined, we do query for backward compatibility.
134
if (isDefinedInput(REPORTGENERATEDEFN_REPORTTEMP_PERFQRY)) {
135       if ("true".equals(getInputParameter(REPORTGENERATEDEFN_REPORTTEMP_PERFQRY)) == false) { //$NON-NLS-1$
136
return true;
137       }
138     }
139
140     JBossDatasourceAdmin admin = new JBossDatasourceAdmin();
141     if (reportSpec.getReportSpecChoice().getXqueryUrl() != null) {
142       // handle xquery
143
} else {
144       try {
145         DataSourceInfo info = admin.getDataSourceInfo(reportSpec
146             .getReportSpecChoice().getJndiSource());
147         IPentahoConnection connection = null;
148         if (reportSpec.getIsMDX()) {
149           connection = PentahoConnectionFactory.getConnection(
150               PentahoConnectionFactory.MDX_DATASOURCE, info.getDriver(), info
151                   .getUrl(), info.getUserId(), info.getPassword(), null);
152         } else {
153           connection = PentahoConnectionFactory.getConnection(
154               PentahoConnectionFactory.SQL_DATASOURCE, info.getDriver(), info
155                   .getUrl(), info.getUserId(), info.getPassword(), null);
156         }
157         String JavaDoc query = ConnectionUtility.setupParametersForActionSequence(reportSpec
158             .getQuery());
159         query = setupQueryParameters(query);
160         IPentahoResultSet res = connection.executeQuery(query);
161         addTempParameterObject(DATACOMPONENT_DATAINPUT, res);
162       } catch (Exception JavaDoc e) {
163         return false;
164       }
165     }
166
167     return true;
168   }
169
170   public boolean init() {
171     return true;
172   }
173
174   public Log getLogger() {
175     return null;
176   }
177
178   public String JavaDoc setupQueryParameters(String JavaDoc query) {
179     Set JavaDoc inputNames = getInputNames();
180     Iterator JavaDoc iter = inputNames.iterator();
181     while (iter.hasNext()) {
182       String JavaDoc inputName = (String JavaDoc) iter.next();
183       final IActionParameter inputParameter = getInputParameter(inputName);
184       final Object JavaDoc value = inputParameter.getValue();
185       if (value instanceof String JavaDoc == false) {
186         continue;
187       }
188       String JavaDoc paramValue = (String JavaDoc) value;
189       String JavaDoc param = "\\{" + inputName + "\\}"; //$NON-NLS-1$ //$NON-NLS-2$
190
query = query.replaceAll(param, paramValue);
191     }
192     return query;
193   }
194
195 }
196
Popular Tags