KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > connection > JavaBeanDataSourceConnection


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * JavaBeanDataSourceConnection.java
28  *
29  * Created on 4 giugno 2003, 18.15
30  *
31  */

32
33 package it.businesslogic.ireport.connection;
34 import it.businesslogic.ireport.*;
35 import it.businesslogic.ireport.util.*;
36 import java.sql.*;
37 import javax.swing.*;
38 import net.sf.jasperreports.engine.data.JRAbstractBeanDataSource;
39 /**
40  *
41  * @author Administrator
42  */

43 public class JavaBeanDataSourceConnection extends it.businesslogic.ireport.IReportConnection {
44     
45     public static String JavaDoc BEAN_ARRAY = "BEAN_ARRAY";
46     public static String JavaDoc BEAN_COLLECTION = "BEAN_COLLECTION";
47     
48     private String JavaDoc name;
49     
50     private String JavaDoc factoryClass;
51     
52     private String JavaDoc methodToCall;
53     
54     private boolean useFieldDescription;
55     
56     private String JavaDoc type = "BEAN_COLLECTION";
57     
58     /** Creates a new instance of JDBCConnection */
59     
60     
61     public JavaBeanDataSourceConnection() {
62     }
63     
64     /** This method return an instanced connection to the database.
65      * If isJDBCConnection() return false => getConnection() return null
66      *
67      */

68     public java.sql.Connection JavaDoc getConnection() {
69             return null;
70     }
71     
72     public boolean isJDBCConnection() {
73         return false;
74     }
75     
76     /*
77      * This method return all properties used by this connection
78      */

79     public java.util.HashMap JavaDoc getProperties()
80     {
81         java.util.HashMap JavaDoc map = new java.util.HashMap JavaDoc();
82         map.put("FactoryClass", Misc.nvl(this.getFactoryClass() ,"") );
83         map.put("MethodToCall", Misc.nvl(this.getMethodToCall(),""));
84         map.put("Type", Misc.nvl(this.getType(),""));
85         map.put("UseFieldDescription", ""+this.isUseFieldDescription());
86        
87         return map;
88     }
89     
90     public void loadProperties(java.util.HashMap JavaDoc map)
91     {
92         this.setFactoryClass( (String JavaDoc)map.get("FactoryClass"));
93         this.setMethodToCall( (String JavaDoc)map.get("MethodToCall"));
94         if (map.containsKey("UseFieldDescription"))
95         {
96             this.setUseFieldDescription( ((String JavaDoc)map.get("UseFieldDescription")).equals("true") );
97         }
98         if (map.containsKey("Type"))
99         {
100             this.setType( (String JavaDoc)map.get("Type"));
101         }
102     }
103     
104     public String JavaDoc getDescription(){ return "JavaBean set datasource"; }
105     
106     /** Getter for property methodToCall.
107      * @return Value of property methodToCall.
108      *
109      */

110     public java.lang.String JavaDoc getMethodToCall() {
111         return methodToCall;
112     }
113     
114     /** Setter for property methodToCall.
115      * @param methodToCall New value of property methodToCall.
116      *
117      */

118     public void setMethodToCall(java.lang.String JavaDoc methodToCall) {
119         this.methodToCall = methodToCall;
120     }
121     
122     /** Getter for property factoryClass.
123      * @return Value of property factoryClass.
124      *
125      */

126     public java.lang.String JavaDoc getFactoryClass() {
127         return factoryClass;
128     }
129     
130     /** Setter for property factoryClass.
131      * @param factoryClass New value of property factoryClass.
132      *
133      */

134     public void setFactoryClass(java.lang.String JavaDoc factoryClass) {
135         this.factoryClass = factoryClass;
136     }
137     
138     /**
139      * Getter for property type.
140      * @return Value of property type.
141      */

142     public java.lang.String JavaDoc getType() {
143         return type;
144     }
145    
146     /**
147      * Setter for property type.
148      * @param type New value of property type.
149      */

150     public void setType(java.lang.String JavaDoc type) {
151         this.type = type;
152     }
153     
154         /**
155      * This method return an instanced JRDataDource to the database.
156      * If isJDBCConnection() return true => getJRDataSource() return false
157      */

158     public net.sf.jasperreports.engine.JRDataSource getJRDataSource()
159     {
160         try {
161             
162                 Class JavaDoc clazz = Thread.currentThread().getContextClassLoader().loadClass(factoryClass);
163             Object JavaDoc obj = clazz.newInstance();
164             Object JavaDoc return_obj = obj.getClass().getMethod( methodToCall, new Class JavaDoc[0]).invoke(null,new Object JavaDoc[0]);
165             
166             if (return_obj != null)
167             {
168                 if (Misc.nvl(this.getType(),"").equals(BEAN_ARRAY) )
169                 {
170                     return new net.sf.jasperreports.engine.data.JRBeanArrayDataSource((Object JavaDoc[]) return_obj, isUseFieldDescription());
171                         }
172                 else if (Misc.nvl(this.getType(),"").equals(BEAN_COLLECTION) )
173                 {
174                     return new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource((java.util.Collection JavaDoc) return_obj, isUseFieldDescription());
175                 }
176             }
177             return new net.sf.jasperreports.engine.JREmptyDataSource();
178                          
179         } catch (Exception JavaDoc ex)
180         {
181             ex.printStackTrace();
182             return super.getJRDataSource();
183         }
184     }
185
186     public boolean isUseFieldDescription() {
187         return useFieldDescription;
188     }
189
190     public void setUseFieldDescription(boolean useFieldDescription) {
191         this.useFieldDescription = useFieldDescription;
192     }
193 }
194
Popular Tags