001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * JRXMLDataSourceConnection.java
028: *
029: * Created on 4 giugno 2003, 18.15
030: *
031: */
032:
033: package it.businesslogic.ireport.connection;
034:
035: import it.businesslogic.ireport.IReportConnectionEditor;
036: import it.businesslogic.ireport.connection.gui.JRXMLDataSourceConnectionEditor;
037: import it.businesslogic.ireport.gui.MainFrame;
038: import it.businesslogic.ireport.util.*;
039: import java.awt.Component;
040: import javax.swing.*;
041:
042: import net.sf.jasperreports.engine.query.JRXPathQueryExecuterFactory;
043: import java.io.File;
044: import org.w3c.dom.Document;
045: import net.sf.jasperreports.engine.util.JRXmlUtils;
046: import java.util.Locale;
047: import java.util.TimeZone;
048:
049: /**
050: *
051: * @author Administrator
052: */
053: public class JRXMLDataSourceConnection extends
054: it.businesslogic.ireport.IReportConnection {
055:
056: private String name;
057:
058: private String filename;
059:
060: private String selectExpression;
061:
062: private boolean useConnection = false;
063:
064: private Locale locale = null;
065: private String datePattern = null;
066: private String numberPattern = null;
067: private TimeZone timeZone = null;
068:
069: /** Creates a new instance of JDBCConnection */
070: public JRXMLDataSourceConnection() {
071: }
072:
073: /** This method return an instanced connection to the database.
074: * If isJDBCConnection() return false => getConnection() return null
075: *
076: */
077: public java.sql.Connection getConnection() {
078: return null;
079: }
080:
081: public boolean isJDBCConnection() {
082: return false;
083: }
084:
085: public boolean isJRDataSource() {
086: return !isUseConnection();
087: }
088:
089: /*
090: * This method return all properties used by this connection
091: */
092: public java.util.HashMap getProperties() {
093: java.util.HashMap map = new java.util.HashMap();
094: map.put("Filename", Misc.nvl(this .getFilename(), ""));
095: map.put("SelectExpression", Misc.nvl(
096: this .getSelectExpression(), ""));
097: map.put("UseConnection", Misc.nvl("" + this .isUseConnection(),
098: "false"));
099:
100: if (getLocale() != null) {
101: map.put("Locale_language", Misc.nvl(getLocale()
102: .getLanguage(), ""));
103: map.put("Locale_country", Misc.nvl(
104: getLocale().getCountry(), ""));
105: map.put("Locale_variant", Misc.nvl(
106: getLocale().getVariant(), ""));
107: }
108:
109: map.put("DatePattern", Misc.nvl(this .getDatePattern(), ""));
110: map.put("NumberPattern", Misc.nvl(this .getNumberPattern(), ""));
111:
112: if (getTimeZone() != null) {
113: map.put("timeZone", Misc
114: .nvl(this .getTimeZone().getID(), ""));
115: }
116:
117: return map;
118: }
119:
120: public void loadProperties(java.util.HashMap map) {
121: this .setFilename((String) map.get("Filename"));
122: this .setSelectExpression((String) map.get("SelectExpression"));
123: this .setUseConnection(Boolean.valueOf(
124: Misc.nvl(map.get("UseConnection"), "false"))
125: .booleanValue());
126:
127: String language = (String) map.get("Locale_language");
128: String country = (String) map.get("Locale_country");
129: String variant = (String) map.get("Locale_variant");
130:
131: if (language != null && language.trim().length() > 0) {
132: if (country != null && country.trim().length() > 0) {
133: if (variant != null && variant.trim().length() > 0) {
134: setLocale(new Locale(language, country, variant));
135: } else {
136: setLocale(new Locale(language, country));
137: }
138: } else {
139: setLocale(new Locale(language));
140: }
141: }
142:
143: String datePatternValue = (String) map.get("DatePattern");
144: if (datePatternValue != null
145: && datePatternValue.trim().length() > 0) {
146: this .setDatePattern(datePatternValue);
147: }
148:
149: String numberPatternValue = (String) map.get("NumberPattern");
150: if (numberPatternValue != null
151: && numberPatternValue.trim().length() > 0) {
152: this .setNumberPattern(numberPatternValue);
153: }
154:
155: String timezoneId = (String) map.get("timeZone");
156: if (timezoneId != null && timezoneId.trim().length() > 0) {
157: this .setTimeZone(TimeZone.getTimeZone(timezoneId));
158: }
159:
160: }
161:
162: /**
163: * Getter for property filename.
164: * @return Value of property filename.
165: */
166: public java.lang.String getFilename() {
167: return filename;
168: }
169:
170: /**
171: * Setter for property filename.
172: * @param filename New value of property filename.
173: */
174: public void setFilename(java.lang.String filename) {
175: this .filename = filename;
176: }
177:
178: /**
179: * Getter for property name.
180: * @return Value of property name.
181: */
182: public java.lang.String getName() {
183: return name;
184: }
185:
186: /**
187: * Setter for property name.
188: * @param name New value of property name.
189: */
190: public void setName(java.lang.String name) {
191: this .name = name;
192: }
193:
194: /**
195: * This method return an instanced JRDataDource to the database.
196: * If isJDBCConnection() return true => getJRDataSource() return false
197: */
198: public net.sf.jasperreports.engine.JRDataSource getJRDataSource() {
199: try {
200:
201: net.sf.jasperreports.engine.data.JRXmlDataSource ds = new net.sf.jasperreports.engine.data.JRXmlDataSource(
202: filename, getSelectExpression());
203:
204: if (getLocale() != null) {
205: ds.setLocale(getLocale());
206: }
207:
208: if (getTimeZone() != null) {
209: ds.setTimeZone(getTimeZone());
210: }
211:
212: if (getDatePattern() != null
213: && getDatePattern().trim().length() > 0) {
214: ds.setDatePattern(getDatePattern());
215: }
216:
217: if (getNumberPattern() != null
218: && getNumberPattern().trim().length() > 0) {
219: ds.setNumberPattern(getNumberPattern());
220: }
221:
222: return ds;
223: } catch (Exception ex) {
224: }
225: return null;
226: }
227:
228: public String getSelectExpression() {
229: return selectExpression;
230: }
231:
232: public void setSelectExpression(String selectExpression) {
233: this .selectExpression = selectExpression;
234: }
235:
236: public boolean isUseConnection() {
237: return useConnection;
238: }
239:
240: public void setUseConnection(boolean useConnection) {
241: this .useConnection = useConnection;
242: }
243:
244: /**
245: * This method is call before the datasource is used and permit to add special parameters to the map
246: *
247: */
248: public java.util.Map getSpecialParameters(java.util.Map map)
249: throws net.sf.jasperreports.engine.JRException {
250: if (isUseConnection()) {
251:
252: /*
253: if (this.getFilename().toLowerCase().startsWith("https://") ||
254: this.getFilename().toLowerCase().startsWith("http://") ||
255: this.getFilename().toLowerCase().startsWith("file:"))
256: {
257: map.put(JRXPathQueryExecuterFactory.XML_URL, this.getFilename());
258: }
259: else
260: {
261: */
262: Document document = JRXmlUtils.parse(new File(this
263: .getFilename()));
264: map
265: .put(
266: JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT,
267: document);
268: //}
269:
270: if (getLocale() != null) {
271: map.put(JRXPathQueryExecuterFactory.XML_LOCALE,
272: getLocale());
273: }
274:
275: if (getTimeZone() != null) {
276: map.put(JRXPathQueryExecuterFactory.XML_TIME_ZONE,
277: getTimeZone());
278: }
279:
280: if (getDatePattern() != null
281: && getDatePattern().trim().length() > 0) {
282: map.put(JRXPathQueryExecuterFactory.XML_DATE_PATTERN,
283: getDatePattern());
284: }
285:
286: if (getNumberPattern() != null
287: && getNumberPattern().trim().length() > 0) {
288: map.put(JRXPathQueryExecuterFactory.XML_NUMBER_PATTERN,
289: getNumberPattern());
290: }
291:
292: }
293: return map;
294: }
295:
296: public Locale getLocale() {
297: return locale;
298: }
299:
300: public void setLocale(Locale locale) {
301: this .locale = locale;
302: }
303:
304: public String getDatePattern() {
305: return datePattern;
306: }
307:
308: public void setDatePattern(String datePattern) {
309: this .datePattern = datePattern;
310: }
311:
312: public String getNumberPattern() {
313: return numberPattern;
314: }
315:
316: public void setNumberPattern(String numberPattern) {
317: this .numberPattern = numberPattern;
318: }
319:
320: public TimeZone getTimeZone() {
321: return timeZone;
322: }
323:
324: public void setTimeZone(TimeZone timeZone) {
325: this .timeZone = timeZone;
326: }
327:
328: public String getDescription() {
329: return I18n.getString("connectionType.xml",
330: "XML file datasource");
331: }
332:
333: public IReportConnectionEditor getIReportConnectionEditor() {
334: return new JRXMLDataSourceConnectionEditor();
335: }
336:
337: public void test() throws Exception {
338:
339: try {
340:
341: java.io.File f = new java.io.File(this .getFilename());
342: if (!f.exists()) {
343: JOptionPane.showMessageDialog(MainFrame
344: .getMainInstance(), I18n.getFormattedString(
345: "messages.connectionDialog.fileNotFound",
346: "File {0} not found", new Object[] { this
347: .getFilename() }), I18n.getString(
348: "message.title.error", "Error"),
349: JOptionPane.ERROR_MESSAGE);
350: throw new Exception();
351: }
352:
353: JOptionPane
354: .showMessageDialog(
355: MainFrame.getMainInstance(),
356: I18n
357: .getString(
358: "messages.connectionDialog.connectionTestSuccessful",
359: "Connection test successful!"),
360: "", JOptionPane.INFORMATION_MESSAGE);
361: return;
362: } catch (Exception ex) {
363: //JOptionPane.showMessageDialog(MainFrame.getMainInstance(),ex.getMessage(),I18n.getString("message.title.error","Error"),JOptionPane.ERROR_MESSAGE);
364: //ex.printStackTrace();
365: throw ex;
366: }
367: }
368: }
|