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.plugin.jrx;
034:
035: import it.businesslogic.ireport.IReportConnectionEditor;
036: import it.businesslogic.ireport.gui.MainFrame;
037: import it.businesslogic.ireport.util.*;
038: import java.awt.Component;
039: import javax.swing.*;
040:
041: import com.jaspersoft.jrx.query.JRXPathQueryExecuterFactory;
042: import java.io.File;
043: import org.w3c.dom.Document;
044: import net.sf.jasperreports.engine.util.JRXmlUtils;
045: import java.util.Locale;
046: import java.util.TimeZone;
047:
048: /**
049: *
050: * @author Administrator
051: */
052: public class JRXMLDataSourceConnection extends
053: it.businesslogic.ireport.IReportConnection {
054:
055: private String name;
056:
057: private String filename;
058:
059: private String selectExpression;
060:
061: private boolean useConnection = false;
062:
063: private Locale locale = null;
064: private String datePattern = null;
065: private String numberPattern = null;
066: private TimeZone timeZone = null;
067:
068: /** Creates a new instance of JDBCConnection */
069: public JRXMLDataSourceConnection() {
070: }
071:
072: /** This method return an instanced connection to the database.
073: * If isJDBCConnection() return false => getConnection() return null
074: *
075: */
076: public java.sql.Connection getConnection() {
077: return null;
078: }
079:
080: public boolean isJDBCConnection() {
081: return false;
082: }
083:
084: public boolean isJRDataSource() {
085: return !isUseConnection();
086: }
087:
088: /*
089: * This method return all properties used by this connection
090: */
091: public java.util.HashMap getProperties() {
092: java.util.HashMap map = new java.util.HashMap();
093: map.put("Filename", Misc.nvl(this .getFilename(), ""));
094: map.put("SelectExpression", Misc.nvl(
095: this .getSelectExpression(), ""));
096: map.put("UseConnection", Misc.nvl("" + this .isUseConnection(),
097: "false"));
098:
099: if (getLocale() != null) {
100: map.put("Locale_language", Misc.nvl(getLocale()
101: .getLanguage(), ""));
102: map.put("Locale_country", Misc.nvl(
103: getLocale().getCountry(), ""));
104: map.put("Locale_variant", Misc.nvl(
105: getLocale().getVariant(), ""));
106: }
107:
108: map.put("DatePattern", Misc.nvl(this .getDatePattern(), ""));
109: map.put("NumberPattern", Misc.nvl(this .getNumberPattern(), ""));
110:
111: if (getTimeZone() != null) {
112: map.put("timeZone", Misc
113: .nvl(this .getTimeZone().getID(), ""));
114: }
115:
116: return map;
117: }
118:
119: public void loadProperties(java.util.HashMap map) {
120: this .setFilename((String) map.get("Filename"));
121: this .setSelectExpression((String) map.get("SelectExpression"));
122: this .setUseConnection(Boolean.valueOf(
123: Misc.nvl(map.get("UseConnection"), "false"))
124: .booleanValue());
125:
126: String language = (String) map.get("Locale_language");
127: String country = (String) map.get("Locale_country");
128: String variant = (String) map.get("Locale_variant");
129:
130: if (language != null && language.trim().length() > 0) {
131: if (country != null && country.trim().length() > 0) {
132: if (variant != null && variant.trim().length() > 0) {
133: setLocale(new Locale(language, country, variant));
134: } else {
135: setLocale(new Locale(language, country));
136: }
137: } else {
138: setLocale(new Locale(language));
139: }
140: }
141:
142: String datePatternValue = (String) map.get("DatePattern");
143: if (datePatternValue != null
144: && datePatternValue.trim().length() > 0) {
145: this .setDatePattern(datePatternValue);
146: }
147:
148: String numberPatternValue = (String) map.get("NumberPattern");
149: if (numberPatternValue != null
150: && numberPatternValue.trim().length() > 0) {
151: this .setNumberPattern(numberPatternValue);
152: }
153:
154: String timezoneId = (String) map.get("timeZone");
155: if (timezoneId != null && timezoneId.trim().length() > 0) {
156: this .setTimeZone(TimeZone.getTimeZone(timezoneId));
157: }
158:
159: }
160:
161: /**
162: * Getter for property filename.
163: * @return Value of property filename.
164: */
165: public java.lang.String getFilename() {
166: return filename;
167: }
168:
169: /**
170: * Setter for property filename.
171: * @param filename New value of property filename.
172: */
173: public void setFilename(java.lang.String filename) {
174: this .filename = filename;
175: }
176:
177: /**
178: * Getter for property name.
179: * @return Value of property name.
180: */
181: public java.lang.String getName() {
182: return name;
183: }
184:
185: /**
186: * Setter for property name.
187: * @param name New value of property name.
188: */
189: public void setName(java.lang.String name) {
190: this .name = name;
191: }
192:
193: /**
194: * This method return an instanced JRDataDource to the database.
195: * If isJDBCConnection() return true => getJRDataSource() return false
196: */
197: public net.sf.jasperreports.engine.JRDataSource getJRDataSource() {
198: try {
199:
200: net.sf.jasperreports.engine.data.JRXmlDataSource ds = new net.sf.jasperreports.engine.data.JRXmlDataSource(
201: filename, getSelectExpression());
202:
203: if (getLocale() != null) {
204: ds.setLocale(getLocale());
205: }
206:
207: if (getTimeZone() != null) {
208: ds.setTimeZone(getTimeZone());
209: }
210:
211: if (getDatePattern() != null
212: && getDatePattern().trim().length() > 0) {
213: ds.setDatePattern(getDatePattern());
214: }
215:
216: if (getNumberPattern() != null
217: && getNumberPattern().trim().length() > 0) {
218: ds.setNumberPattern(getNumberPattern());
219: }
220:
221: return ds;
222: } catch (Exception ex) {
223: }
224: return null;
225: }
226:
227: public String getSelectExpression() {
228: return selectExpression;
229: }
230:
231: public void setSelectExpression(String selectExpression) {
232: this .selectExpression = selectExpression;
233: }
234:
235: public boolean isUseConnection() {
236: return useConnection;
237: }
238:
239: public void setUseConnection(boolean useConnection) {
240: this .useConnection = useConnection;
241: }
242:
243: /**
244: * This method is call before the datasource is used and permit to add special parameters to the map
245: *
246: */
247: public java.util.Map getSpecialParameters(java.util.Map map)
248: throws net.sf.jasperreports.engine.JRException {
249: if (isUseConnection()) {
250:
251: System.out
252: .println("Running against: " + this .getFilename());
253: System.out.flush();
254: if (this .getFilename().toLowerCase().startsWith("https://")
255: || this .getFilename().toLowerCase().startsWith(
256: "http://")
257: || this .getFilename().toLowerCase().startsWith(
258: "file:")) {
259:
260: map.put(JRXPathQueryExecuterFactory.XML_URL, this
261: .getFilename());
262: } else {
263:
264: Document document = JRXmlUtils.parse(new File(this
265: .getFilename()));
266: map
267: .put(
268: JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT,
269: document);
270: }
271:
272: if (getLocale() != null) {
273: map.put(JRXPathQueryExecuterFactory.XML_LOCALE,
274: getLocale());
275: }
276:
277: if (getTimeZone() != null) {
278: map.put(JRXPathQueryExecuterFactory.XML_TIME_ZONE,
279: getTimeZone());
280: }
281:
282: if (getDatePattern() != null
283: && getDatePattern().trim().length() > 0) {
284: map.put(JRXPathQueryExecuterFactory.XML_DATE_PATTERN,
285: getDatePattern());
286: }
287:
288: if (getNumberPattern() != null
289: && getNumberPattern().trim().length() > 0) {
290: map.put(JRXPathQueryExecuterFactory.XML_NUMBER_PATTERN,
291: getNumberPattern());
292: }
293:
294: }
295: return map;
296: }
297:
298: public Locale getLocale() {
299: return locale;
300: }
301:
302: public void setLocale(Locale locale) {
303: this .locale = locale;
304: }
305:
306: public String getDatePattern() {
307: return datePattern;
308: }
309:
310: public void setDatePattern(String datePattern) {
311: this .datePattern = datePattern;
312: }
313:
314: public String getNumberPattern() {
315: return numberPattern;
316: }
317:
318: public void setNumberPattern(String numberPattern) {
319: this .numberPattern = numberPattern;
320: }
321:
322: public TimeZone getTimeZone() {
323: return timeZone;
324: }
325:
326: public void setTimeZone(TimeZone timeZone) {
327: this .timeZone = timeZone;
328: }
329:
330: public String getDescription() {
331: return "Remote XML file datasource";
332: }
333:
334: public IReportConnectionEditor getIReportConnectionEditor() {
335: return new JRXMLDataSourceConnectionEditor();
336: }
337:
338: public void test() throws Exception {
339:
340: try {
341:
342: JOptionPane
343: .showMessageDialog(
344: MainFrame.getMainInstance(),
345: I18n
346: .getString(
347: "messages.connectionDialog.connectionTestSuccessful",
348: "Connection test successful!"),
349: "", JOptionPane.INFORMATION_MESSAGE);
350: return;
351: } catch (Exception ex) {
352: //JOptionPane.showMessageDialog(MainFrame.getMainInstance(),ex.getMessage(),I18n.getString("message.title.error","Error"),JOptionPane.ERROR_MESSAGE);
353: //ex.printStackTrace();
354: throw ex;
355: }
356: }
357: }
|