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: package org.pentaho.plugin.jfreereport;
014:
015: import java.io.ByteArrayOutputStream;
016: import java.io.FileNotFoundException;
017: import java.io.IOException;
018: import java.io.InputStream;
019: import java.util.Iterator;
020: import java.util.Set;
021: import java.util.zip.ZipEntry;
022: import java.util.zip.ZipInputStream;
023:
024: import javax.activation.DataSource;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.jfree.report.JFreeReport;
029: import org.pentaho.actionsequence.dom.actions.JFreeReportAction;
030: import org.pentaho.core.admin.datasources.DataSourceInfo;
031: import org.pentaho.core.admin.datasources.jboss.JBossDatasourceAdmin;
032: import org.pentaho.commons.connection.IPentahoConnection;
033: import org.pentaho.commons.connection.IPentahoResultSet;
034: import org.pentaho.core.repository.ISolutionRepository;
035: import org.pentaho.core.runtime.IActionParameter;
036: import org.pentaho.core.solution.IActionResource;
037: import org.pentaho.core.system.PentahoSystem;
038: import org.pentaho.data.PentahoConnectionFactory;
039: import org.pentaho.jfreereport.castormodel.reportspec.ReportSpec;
040: import org.pentaho.jfreereport.wizard.utility.CastorUtility;
041: import org.pentaho.jfreereport.wizard.utility.report.ReportGenerationUtility;
042: import org.pentaho.jfreereport.wizard.utility.report.ReportParameterUtility;
043: import org.pentaho.plugin.jfreereport.components.JFreeReportGenerateDefinitionComponent;
044: import org.pentaho.plugin.jfreereport.helper.PentahoTableDataFactory;
045: import org.pentaho.plugin.jfreereport.helper.PentahoTableModel;
046:
047: /**
048: * The report-wizard component generates a report definition from a report-spec
049: * file. Use this component, if want to use the standard-report process only or
050: * if you have no need to tweak the processing.
051: * <p/>
052: * This class calls the {@link JFreeReportGenerateDefinitionComponent} to do
053: * the real work.
054: *
055: * @created May 15, 2006
056: * @author Michael D'Amour
057: */
058: public class ReportWizardSpecComponent extends JFreeReportComponent {
059: private static final long serialVersionUID = 3435921119638344882L;
060:
061: private JFreeReportGenerateDefinitionComponent generateDefinitionComponent;
062: private ReportSpec reportSpec;
063:
064: public ReportWizardSpecComponent() {
065: }
066:
067: public Log getLogger() {
068: return LogFactory.getLog(ReportWizardSpecComponent.class);
069: }
070:
071: public boolean validateAction() {
072: JFreeReportAction jFreeReportAction = (JFreeReportAction) getActionDefinition();
073: return (jFreeReportAction.getReportDefinition() != null)
074: && super .validateAction();
075: }
076:
077: protected boolean executeReportAction() {
078: boolean result = true;
079: try {
080: reportSpec = getReportSpec();
081: result = super .executeReportAction();
082: } catch (IOException ex) {
083: error(ex.getLocalizedMessage());
084: result = false;
085: }
086:
087: return result;
088: }
089:
090: public JFreeReport getReport() throws Exception {
091: JFreeReport report = null;
092: if (reportSpec != null) {
093: ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
094: ReportGenerationUtility.createJFreeReportXML(reportSpec,
095: outputStream, 0, 0, false, "", 0, 0); //$NON-NLS-1$
096: String reportDefinition = new String(outputStream
097: .toByteArray());
098: report = createReport(reportDefinition);
099: } else {
100: report = super .getReport();
101: }
102: return report;
103: }
104:
105: protected PentahoTableDataFactory getDataFactory()
106: throws ClassNotFoundException, InstantiationException,
107: IllegalAccessException, Exception {
108: PentahoTableDataFactory factory = null;
109: if (reportSpec != null) {
110: if (!isDefinedInput(REPORTGENERATEDEFN_REPORTTEMP_PERFQRY)
111: || "true"
112: .equals(getInputParameter(REPORTGENERATEDEFN_REPORTTEMP_PERFQRY))) {
113: if (reportSpec.getReportSpecChoice().getXqueryUrl() != null) {
114: // handle xquery
115: } else {
116: IPentahoResultSet pentahoResultSet = getResultSet(getReportSpec());
117: factory = new PentahoTableDataFactory();
118: pentahoResultSet.beforeFirst();
119: factory.addTable(DATACOMPONENT_DEFAULTINPUT,
120: new PentahoTableModel(pentahoResultSet));
121: }
122: } else {
123: factory = super .getDataFactory();
124: }
125: } else {
126: factory = super .getDataFactory();
127: }
128: return factory;
129: }
130:
131: public ReportSpec getReportSpec() throws IOException {
132: JFreeReportAction jFreeReportAction = (JFreeReportAction) getActionDefinition();
133: DataSource dataSource = jFreeReportAction
134: .getReportDefinitionDataSource();
135: ReportSpec reportSpec = null;
136: reportSpec = loadFromZip(dataSource.getInputStream());
137: if (reportSpec == null) {
138: dataSource = jFreeReportAction
139: .getReportDefinitionDataSource();
140: reportSpec = (ReportSpec) CastorUtility.getInstance()
141: .readCastorObject(dataSource.getInputStream(),
142: ReportSpec.class);
143: }
144: return reportSpec;
145: }
146:
147: private ReportSpec loadFromZip(InputStream reportSpecInputStream) {
148: try {
149: ZipInputStream zis = new ZipInputStream(
150: reportSpecInputStream);
151:
152: ZipEntry reportSpecEntry = findReportSpec(zis);
153:
154: if (reportSpecEntry == null) {
155: return null;
156: }
157:
158: // is this really sane? Blindly using the first zip entry is ... argh!
159: // maybe you should use GZipped streams instead...
160: return (ReportSpec) CastorUtility.getInstance()
161: .readCastorObject(zis, ReportSpec.class);
162: } catch (Exception e) {
163: return null;
164: }
165: }
166:
167: /**
168: * Look through the Zip stream and find an entry whose name is .xreportspec.
169: * If the entry is found, return it, otherwise return null.
170: *
171: * @param zStrm
172: * @return If the entry is found, return it, otherwise return null.
173: * @throws IOException
174: */
175: private ZipEntry findReportSpec(ZipInputStream zStrm)
176: throws IOException {
177: ZipEntry reportSpecEntry = null;
178: // for loop has no body
179: for (reportSpecEntry = zStrm.getNextEntry(); null != reportSpecEntry
180: && !reportSpecEntry.getName().endsWith(".xreportspec"); reportSpecEntry = zStrm //$NON-NLS-1$
181: .getNextEntry())
182: ;
183:
184: return reportSpecEntry;
185: }
186:
187: public IPentahoResultSet getResultSet(ReportSpec reportSpec)
188: throws Exception {
189: JBossDatasourceAdmin admin = new JBossDatasourceAdmin();
190: DataSourceInfo info = admin.getDataSourceInfo(reportSpec
191: .getReportSpecChoice().getJndiSource());
192: IPentahoConnection connection = null;
193: if (reportSpec.getIsMDX()) {
194: connection = PentahoConnectionFactory.getConnection(
195: IPentahoConnection.MDX_DATASOURCE,
196: info.getDriver(), info.getUrl(), info.getUserId(),
197: info.getPassword(), null);
198: } else {
199: connection = PentahoConnectionFactory.getConnection(
200: IPentahoConnection.SQL_DATASOURCE,
201: info.getDriver(), info.getUrl(), info.getUserId(),
202: info.getPassword(), null);
203: }
204: String query = ReportParameterUtility
205: .setupParametersForActionSequence(reportSpec.getQuery());
206: query = setupQueryParameters(query);
207: IPentahoResultSet res = connection.executeQuery(query);
208: return res;
209: }
210:
211: public String setupQueryParameters(String query) {
212: Set inputNames = getInputNames();
213: Iterator iter = inputNames.iterator();
214: while (iter.hasNext()) {
215: String inputName = (String) iter.next();
216: final IActionParameter inputParameter = getInputParameter(inputName);
217: final Object value = inputParameter.getValue();
218: if (value instanceof String == false) {
219: continue;
220: }
221: String paramValue = (String) value;
222: String param = "\\{" + inputName + "\\}"; //$NON-NLS-1$ //$NON-NLS-2$
223: query = query.replaceAll(param, paramValue);
224: }
225: return query;
226: }
227: }
|