001: /*
002: * Copyright 2006-2007 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.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.crm.report;
015:
016: import org.gjt.xpp.XmlPullNode;
017: import org.gjt.xpp.XmlPullParser;
018: import org.gjt.xpp.XmlPullParserFactory;
019: import org.jetbrains.annotations.NonNls;
020: import org.jetbrains.annotations.NotNull;
021: import org.jetbrains.annotations.Nullable;
022: import org.pentaho.reportdesigner.crm.report.datasetplugin.multidataset.MultiDataSetReportElement;
023: import org.pentaho.reportdesigner.crm.report.model.BandToplevelReportElement;
024: import org.pentaho.reportdesigner.crm.report.model.FilePath;
025: import org.pentaho.reportdesigner.crm.report.model.Report;
026: import org.pentaho.reportdesigner.crm.report.model.ReportElement;
027: import org.pentaho.reportdesigner.crm.report.model.ReportFactory;
028: import org.pentaho.reportdesigner.crm.report.model.SubReport;
029: import org.pentaho.reportdesigner.crm.report.model.SubReportElement;
030: import org.pentaho.reportdesigner.crm.report.model.dataset.DataSetsReportElement;
031: import org.pentaho.reportdesigner.crm.report.settings.WorkspaceSettings;
032: import org.pentaho.reportdesigner.crm.report.util.JOptionPaneHelper;
033: import org.pentaho.reportdesigner.crm.report.util.XMLContextKeys;
034: import org.pentaho.reportdesigner.lib.client.components.CenterPanelDialog;
035: import org.pentaho.reportdesigner.lib.client.components.ProgressDialog;
036: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
037: import org.pentaho.reportdesigner.lib.client.util.ExceptionUtils;
038: import org.pentaho.reportdesigner.lib.client.util.IOUtil;
039: import org.pentaho.reportdesigner.lib.client.util.UncaughtExcpetionsModel;
040: import org.pentaho.reportdesigner.lib.common.xml.XMLConstants;
041: import org.pentaho.reportdesigner.lib.common.xml.XMLContext;
042:
043: import java.awt.*;
044: import java.io.BufferedReader;
045: import java.io.File;
046: import java.io.FileInputStream;
047: import java.io.IOException;
048: import java.io.InputStreamReader;
049: import java.util.ArrayList;
050: import java.util.LinkedList;
051: import java.util.logging.Level;
052: import java.util.logging.Logger;
053:
054: /**
055: * User: Martin
056: * Date: 11.02.2007
057: * Time: 12:00:29
058: */
059: public class ReportDesignerUtils {
060: @NonNls
061: @NotNull
062: private static final Logger LOG = Logger
063: .getLogger(ReportDesignerUtils.class.getName());
064:
065: private ReportDesignerUtils() {
066: }
067:
068: public static boolean isModifiedOrNotEmpty(@NotNull
069: ReportDialog reportDialog) {
070: //noinspection ConstantConditions
071: if (reportDialog == null) {
072: throw new IllegalArgumentException(
073: "reportDialog must not be null");
074: }
075:
076: if (reportDialog.isModified()) {
077: return true;
078: }
079:
080: Report report = reportDialog.getReport();
081: if (report != null) {
082: ArrayList<ReportElement> children = report.getChildren();
083:
084: for (ReportElement child : children) {
085: if (child instanceof BandToplevelReportElement) {
086: BandToplevelReportElement bandToplevelReportElement = (BandToplevelReportElement) child;
087: if (!bandToplevelReportElement.getChildren()
088: .isEmpty()) {
089: return true;
090: }
091: }
092: }
093: }
094:
095: return false;
096: }
097:
098: @Nullable
099: public static ReportDialog getOpenReportDialog(@NotNull
100: File f) {
101: //noinspection ConstantConditions
102: if (f == null) {
103: throw new IllegalArgumentException("f must not be null");
104: }
105:
106: //test if this file is already open
107: LinkedList<ReportDialog> reportDialogs = ReportDesignerWindows
108: .getInstance().getReportDialogs();
109: for (ReportDialog dialog : reportDialogs) {
110: try {
111: File file = dialog.getCurrentReportFile();
112: if (file != null
113: && file.getCanonicalPath().equals(
114: f.getCanonicalPath())) {
115: return dialog;
116: }
117: } catch (IOException e) {
118: if (LOG.isLoggable(Level.FINE))
119: LOG
120: .log(Level.FINE,
121: "OpenReportCommand.execute ", e);
122: }
123: }
124: return null;
125: }
126:
127: public static void openReport(@NotNull
128: File currentReportFile, @NotNull
129: final ReportDialog reportDialog, @Nullable
130: final File mainReportFile) {
131: try {
132: long n1 = System.nanoTime();
133:
134: final File currentReportFile1 = currentReportFile;
135:
136: final ProgressDialog progressDialog = ProgressDialog
137: .createProgressDialog(
138: reportDialog,
139: TranslationManager
140: .getInstance()
141: .getTranslation("R",
142: "OpenReportCommand.ProgressDialog.Title"),
143: "");
144:
145: //final InternalProgressDialog progressDialog = new InternalProgressDialog(reportDialog,
146: // TranslationManager.getInstance().getTranslation("R", "OpenReportCommand.ProgressDialog.Title"),
147: // "");
148:
149: Thread t = new Thread() {
150: public void run() {
151: BufferedReader bufferedReader = null;
152: try {
153: XmlPullParserFactory xmlPullParserFactory = XmlPullParserFactory
154: .newInstance();
155: XmlPullParser xmlPullParser = xmlPullParserFactory
156: .newPullParser();
157: //noinspection IOResourceOpenedButNotSafelyClosed
158: bufferedReader = new BufferedReader(
159: new InputStreamReader(
160: new FileInputStream(
161: currentReportFile1),
162: XMLConstants.ENCODING));
163: xmlPullParser.setInput(bufferedReader);
164:
165: XMLContext xmlContext = new XMLContext();
166: XMLContextKeys.CONTEXT_PATH.putObject(
167: xmlContext, currentReportFile1
168: .getParentFile());
169:
170: XmlPullNode node;
171: Report report = null;
172: try {
173: xmlPullParser.next(); // get first start tag
174: node = xmlPullParserFactory
175: .newPullNode(xmlPullParser);
176:
177: if (XMLConstants.REPORT.equals(node
178: .getRawName())) {
179: report = ReportFactory
180: .createEmptyReport();
181: report.readObject(node, xmlContext);
182: } else if (XMLConstants.SUBREPORT
183: .equals(node.getRawName())) {
184: report = ReportFactory
185: .createEmptySubReport();
186: report.readObject(node, xmlContext);
187: }
188:
189: node.resetPullNode();
190: } catch (Exception e) {
191: if (LOG.isLoggable(Level.WARNING))
192: LOG.log(Level.WARNING,
193: "OpenReportCommand.run ", e);
194: ExceptionUtils
195: .disposeDialogInEDT(progressDialog);
196: EventQueue.invokeLater(new Runnable() {
197: public void run() {
198: JOptionPaneHelper
199: .showMessageDialog(
200: reportDialog,
201: TranslationManager
202: .getInstance()
203: .getTranslation(
204: "R",
205: "OpenReportCommand.InvalidFile.Message"),
206: TranslationManager
207: .getInstance()
208: .getTranslation(
209: "R",
210: "OpenReportCommand.InvalidFile.Title"),
211: JOptionPaneHelper.ERROR_MESSAGE,
212: CenterPanelDialog.ModalityType.DOCUMENT_MODAL);
213: }
214: });
215:
216: return;
217: } catch (Throwable e) {
218: if (LOG.isLoggable(Level.SEVERE))
219: LOG.log(Level.SEVERE,
220: "OpenReportCommand.run ", e);
221: ExceptionUtils
222: .disposeDialogInEDT(progressDialog);
223: UncaughtExcpetionsModel.getInstance()
224: .addException(e);
225: return;
226: }
227:
228: final String canonicalPath = currentReportFile1
229: .getCanonicalPath();
230:
231: final Report report1 = report;
232: EventQueue.invokeAndWait(new Runnable() {
233: public void run() {
234: reportDialog.setReport(report1);
235: reportDialog
236: .getWorkspaceSettings()
237: .put(
238: WorkspaceSettings.LAST_ACCESSED_REPORT_FILE,
239: canonicalPath);
240: reportDialog
241: .setCurrentReportFile(currentReportFile1);
242: reportDialog.setModified(false);
243:
244: if (report1 instanceof SubReport
245: && mainReportFile != null) {
246: SubReport subReport = (SubReport) report1;
247: subReport.getSubReportDataElement()
248: .setCurrentMainReport(
249: mainReportFile);
250: }
251:
252: progressDialog.dispose();
253: }
254: });
255: } catch (Exception e) {
256: if (LOG.isLoggable(Level.FINE))
257: LOG.log(Level.FINE,
258: "OpenReportCommand.run ", e);
259: ExceptionUtils
260: .disposeDialogInEDT(progressDialog);
261: UncaughtExcpetionsModel.getInstance()
262: .addException(e);
263: } finally {
264: IOUtil.closeStream(bufferedReader);
265: }
266: }
267: };
268:
269: t.setDaemon(true);
270: t.setPriority(Thread.NORM_PRIORITY - 1);
271: t.start();
272:
273: if (t.isAlive()) {
274: progressDialog.setVisible(true);
275: }
276:
277: long n2 = System.nanoTime();
278: if (LOG.isLoggable(Level.FINE))
279: LOG.log(Level.FINE, "OpenReportCommand.openReport "
280: + (n2 - n1) / (1000. * 1000.) + " ms");
281: } catch (Exception e) {
282: UncaughtExcpetionsModel.getInstance().addException(e);
283: }
284: }
285:
286: @Nullable
287: public static Report openReport(@NotNull
288: File file) throws Exception {
289: BufferedReader bufferedReader = null;
290: try {
291: XmlPullParserFactory xmlPullParserFactory = XmlPullParserFactory
292: .newInstance();
293: XmlPullParser xmlPullParser = xmlPullParserFactory
294: .newPullParser();
295: //noinspection IOResourceOpenedButNotSafelyClosed
296: bufferedReader = new BufferedReader(new InputStreamReader(
297: new FileInputStream(file), XMLConstants.ENCODING));
298: xmlPullParser.setInput(bufferedReader);
299:
300: XMLContext xmlContext = new XMLContext();
301: XMLContextKeys.CONTEXT_PATH.putObject(xmlContext, file
302: .getParentFile());
303:
304: XmlPullNode node;
305: Report report = null;
306:
307: xmlPullParser.next(); // get first start tag
308: node = xmlPullParserFactory.newPullNode(xmlPullParser);
309:
310: if (XMLConstants.REPORT.equals(node.getRawName())) {
311: report = ReportFactory.createEmptyReport();
312: report.readObject(node, xmlContext);
313: } else if (XMLConstants.SUBREPORT.equals(node.getRawName())) {
314: report = ReportFactory.createEmptySubReport();
315: report.readObject(node, xmlContext);
316: }
317:
318: node.resetPullNode();
319:
320: return report;
321: } finally {
322: IOUtil.closeStream(bufferedReader);
323: }
324: }
325:
326: @Nullable
327: public static SubReport getSubReport(@NotNull
328: SubReportElement subReportElement) throws Exception {
329: //noinspection ConstantConditions
330: if (subReportElement == null) {
331: throw new IllegalArgumentException(
332: "subReportElement must not be null");
333: }
334:
335: //perhaps it is already open
336: FilePath filePath = subReportElement.getFilePath();
337: ReportDialog subReportDialog = getOpenReportDialog(new File(
338: filePath.getPath()));
339: if (subReportDialog != null) {
340: Report report = subReportDialog.getReport();
341: if (report instanceof SubReport) {
342: return (SubReport) report;
343: }
344: }
345:
346: //try to load from disk
347: XmlPullParserFactory xmlPullParserFactory = XmlPullParserFactory
348: .newInstance();
349: XmlPullParser xmlPullParser = xmlPullParserFactory
350: .newPullParser();
351:
352: BufferedReader bufferedReader = null;
353: try {
354: //noinspection IOResourceOpenedButNotSafelyClosed
355: bufferedReader = new BufferedReader(new InputStreamReader(
356: new FileInputStream(filePath.getPath()),
357: XMLConstants.ENCODING));
358: xmlPullParser.setInput(bufferedReader);
359:
360: XmlPullNode node;
361:
362: XMLContext xmlContext = new XMLContext();
363: XMLContextKeys.CONTEXT_PATH.putObject(xmlContext, new File(
364: filePath.getPath()).getParentFile());
365:
366: xmlPullParser.next();//get first start tag
367: node = xmlPullParserFactory.newPullNode(xmlPullParser);
368:
369: if (XMLConstants.SUBREPORT.equals(node.getRawName())) {
370: SubReport subReport = ReportFactory
371: .createEmptySubReport();
372: subReport.readObject(node, xmlContext);
373: return subReport;
374: }
375: } finally {
376: IOUtil.closeStream(bufferedReader);
377: }
378:
379: return null;
380: }
381:
382: @Nullable
383: public static Report getReport(@NotNull
384: File file) {
385: ReportDialog dialog = getOpenReportDialog(file);
386: if (dialog != null) {
387: return dialog.getReport();
388: } else {
389: try {
390: return openReport(file);
391: } catch (Exception e) {
392: if (LOG.isLoggable(Level.FINE))
393: LOG.log(Level.FINE,
394: "ReportDesignerUtils.getReport ", e);
395: return null;
396: }
397: }
398: }
399:
400: @NotNull
401: public static String[] getPossibleQueriesForSubReport(@Nullable
402: Report mainReport) {
403: ArrayList<String> possibleQueries = new ArrayList<String>();
404: if (mainReport != null) {
405: DataSetsReportElement dataSetsReportElement = mainReport
406: .getDataSetsReportElement();
407: for (ReportElement reportElement : dataSetsReportElement
408: .getChildren()) {
409: if (reportElement instanceof MultiDataSetReportElement) {
410: MultiDataSetReportElement multiDataSetReportElement = (MultiDataSetReportElement) reportElement;
411: String queryName = multiDataSetReportElement
412: .getQueryName();
413: possibleQueries.add(queryName);
414: }
415: }
416: }
417:
418: return possibleQueries.toArray(new String[possibleQueries
419: .size()]);
420: }
421: }
|