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.components;
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 org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.pentaho.core.admin.datasources.DataSourceInfo;
027: import org.pentaho.core.admin.datasources.jboss.JBossDatasourceAdmin;
028: import org.pentaho.commons.connection.IPentahoConnection;
029: import org.pentaho.commons.connection.IPentahoResultSet;
030: import org.pentaho.core.repository.ISolutionRepository;
031: import org.pentaho.core.runtime.IActionParameter;
032: import org.pentaho.core.solution.IActionResource;
033: import org.pentaho.core.system.PentahoSystem;
034: import org.pentaho.data.PentahoConnectionFactory;
035: import org.pentaho.jfreereport.castormodel.reportspec.ReportSpec;
036: import org.pentaho.jfreereport.wizard.utility.CastorUtility;
037: import org.pentaho.jfreereport.wizard.utility.report.ReportGenerationUtility;
038: import org.pentaho.jfreereport.wizard.utility.report.ReportParameterUtility;
039: import org.pentaho.plugin.jfreereport.AbstractJFreeReportComponent;
040:
041: /**
042: * This component generates report definitions. It uses the report wizard for
043: * the magical generation task.
044: *
045: * @author Thomas Morgner
046: */
047: public class JFreeReportGenerateDefinitionComponent extends
048: AbstractJFreeReportComponent {
049: private static final long serialVersionUID = -6364796568478754207L;
050:
051: public JFreeReportGenerateDefinitionComponent() {
052: }
053:
054: protected boolean validateAction() {
055: if (isDefinedResource(REPORTGENERATEDEFN_REPORTSPECINPUT) == false) {
056: return false;
057: }
058: return true;
059: }
060:
061: protected boolean validateSystemSettings() {
062: return true;
063: }
064:
065: public void done() {
066:
067: }
068:
069: /**
070: * Look through the Zip stream and find an entry whose name is .xreportspec.
071: * If the entry is found, return it, otherwise return null.
072: *
073: * @param zStrm
074: * @return If the entry is found, return it, otherwise return null.
075: * @throws IOException
076: */
077: private ZipEntry findReportSpec(ZipInputStream zStrm)
078: throws IOException {
079: ZipEntry reportSpecEntry = null;
080: // for loop has no body
081: for (reportSpecEntry = zStrm.getNextEntry(); null != reportSpecEntry
082: && !reportSpecEntry.getName().endsWith(".xreportspec"); reportSpecEntry = zStrm //$NON-NLS-1$
083: .getNextEntry())
084: ;
085:
086: return reportSpecEntry;
087: }
088:
089: private ReportSpec loadFromZip(IActionResource resource) {
090: final ISolutionRepository solutionRepository = PentahoSystem
091: .getSolutionRepository(getSession());
092:
093: try {
094: InputStream reportSpecInputStream = solutionRepository
095: .getResourceInputStream(resource, true);
096: ZipInputStream zis = new ZipInputStream(
097: reportSpecInputStream);
098:
099: ZipEntry reportSpecEntry = findReportSpec(zis);
100:
101: if (reportSpecEntry == null) {
102: return null;
103: }
104:
105: // is this really sane? Blindly using the first zip entry is ... argh!
106: // maybe you should use GZipped streams instead...
107: return (ReportSpec) CastorUtility.getInstance()
108: .readCastorObject(zis, ReportSpec.class);
109: } catch (Exception e) {
110: return null;
111: }
112: }
113:
114: public ReportSpec getReportSpec() throws FileNotFoundException {
115: ReportSpec reportSpec = null;
116: if (isDefinedResource(REPORTGENERATEDEFN_REPORTSPECINPUT)) {
117: IActionResource resource = getResource(REPORTGENERATEDEFN_REPORTSPECINPUT);
118: reportSpec = loadFromZip(resource);
119: if (reportSpec == null) {
120: InputStream reportSpecInputStream = PentahoSystem
121: .getSolutionRepository(getSession())
122: .getResourceInputStream(resource, true);
123: reportSpec = (ReportSpec) CastorUtility.getInstance()
124: .readCastorObject(reportSpecInputStream,
125: ReportSpec.class);
126: }
127: }
128: return reportSpec;
129: }
130:
131: public IPentahoResultSet getResultSet(ReportSpec reportSpec)
132: throws Exception {
133: JBossDatasourceAdmin admin = new JBossDatasourceAdmin();
134: DataSourceInfo info = admin.getDataSourceInfo(reportSpec
135: .getReportSpecChoice().getJndiSource());
136: IPentahoConnection connection = null;
137: if (reportSpec.getIsMDX()) {
138: connection = PentahoConnectionFactory.getConnection(
139: IPentahoConnection.MDX_DATASOURCE,
140: info.getDriver(), info.getUrl(), info.getUserId(),
141: info.getPassword(), null);
142: } else {
143: connection = PentahoConnectionFactory.getConnection(
144: IPentahoConnection.SQL_DATASOURCE,
145: info.getDriver(), info.getUrl(), info.getUserId(),
146: info.getPassword(), null);
147: }
148: String query = ReportParameterUtility
149: .setupParametersForActionSequence(reportSpec.getQuery());
150: query = setupQueryParameters(query);
151: IPentahoResultSet res = connection.executeQuery(query);
152: return res;
153: }
154:
155: protected boolean executeAction() {
156: boolean result = true;
157: try {
158: ReportSpec reportSpec = getReportSpec();
159: if (reportSpec != null) {
160: ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
161: ReportGenerationUtility
162: .createJFreeReportXML(reportSpec, outputStream,
163: 0, 0, false, "", 0, 0); //$NON-NLS-1$
164: String reportDefinition = new String(outputStream
165: .toByteArray());
166: addTempParameterObject(REPORTGENERATEDEFN_REPORTDEFN,
167: reportDefinition);
168:
169: // if that parameter is not defined, we do query for backward compatibility.
170: if (!isDefinedInput(REPORTGENERATEDEFN_REPORTTEMP_PERFQRY)
171: || "true"
172: .equals(getInputParameter(REPORTGENERATEDEFN_REPORTTEMP_PERFQRY))) {
173: if (reportSpec.getReportSpecChoice().getXqueryUrl() != null) {
174: // handle xquery
175: } else {
176: try {
177: addTempParameterObject(
178: DATACOMPONENT_DATAINPUT,
179: getResultSet(reportSpec));
180: } catch (Exception e) {
181: result = false;
182: }
183: }
184: }
185: }
186: } catch (FileNotFoundException ex) {
187: error(ex.getLocalizedMessage());
188: result = false;
189: }
190: return result;
191: }
192:
193: public boolean init() {
194: return true;
195: }
196:
197: public Log getLogger() {
198: return LogFactory
199: .getLog(JFreeReportGenerateDefinitionComponent.class);
200: }
201:
202: public String setupQueryParameters(String query) {
203: Set inputNames = getInputNames();
204: Iterator iter = inputNames.iterator();
205: while (iter.hasNext()) {
206: String inputName = (String) iter.next();
207: final IActionParameter inputParameter = getInputParameter(inputName);
208: final Object value = inputParameter.getValue();
209: if (value instanceof String == false) {
210: continue;
211: }
212: String paramValue = (String) value;
213: String param = "\\{" + inputName + "\\}"; //$NON-NLS-1$ //$NON-NLS-2$
214: query = query.replaceAll(param, paramValue);
215: }
216: return query;
217: }
218:
219: }
|