KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > web > talk > jspbeans > AceCannedMessageServer


1 /*
2  * AceCannedMessageServer.java
3  *
4  * Created on June 6, 2003, 11:36 AM
5  */

6
7 package com.quikj.application.web.talk.jspbeans;
8
9 import java.sql.*;
10 import java.util.*;
11
12 /**
13  *
14  * @author amit
15  */

16 public class AceCannedMessageServer implements CannedMessageServiceInterface
17 {
18     /** Holds value of property jdbcClass. */
19     private String JavaDoc jdbcClass;
20     
21     /** Holds value of property url. */
22     private String JavaDoc url;
23     
24     /** Holds value of property user. */
25     private String JavaDoc user;
26     
27     /** Holds value of property password. */
28     private String JavaDoc password;
29     
30     private Connection connection = null;
31     
32     /** Creates a new instance of AceCannedMessageServer */
33     public AceCannedMessageServer()
34     {
35     }
36     
37     public String JavaDoc init()
38     {
39         if (getConnection() == null)
40         {
41             return "Could not obtain database connection";
42         }
43         
44         return null;
45     }
46     
47     public CannedMessageSummaryElement[] list(String JavaDoc[] groups)
48     {
49         StringBuffer JavaDoc query = new StringBuffer JavaDoc("select id, description, grp from canned_message_tbl");
50         
51         if (groups.length > 0)
52         {
53             query.append (" where");
54         }
55         
56         for (int i = 0; i < groups.length; i++)
57         {
58             if (i > 0)
59             {
60                 query.append(" or");
61             }
62             
63             query.append(" grp = ?");
64         }
65         
66         query.append (" order by grp, description");
67         try
68         {
69             Connection c = getConnection();
70             if (c == null)
71             {
72                 return null;
73             }
74             
75             PreparedStatement pstmt = c.prepareStatement(query.toString());
76             for (int i = 0; i < groups.length; i++)
77             {
78                 pstmt.setString(i+1, groups[i]);
79             }
80             
81             ResultSet rslt = pstmt.executeQuery();
82             ArrayList lst = new ArrayList();
83             while (rslt.next() == true)
84             {
85                 CannedMessageSummaryElement el = new CannedMessageSummaryElement();
86                 el.setId(rslt.getString(1));
87                 el.setDescription(rslt.getString(2));
88                 el.setGroup(rslt.getString(3));
89                 
90                 lst.add(el);
91             }
92             
93             CannedMessageSummaryElement[] els = new CannedMessageSummaryElement[lst.size()];
94             lst.toArray(els);
95             return els;
96         }
97         catch (SQLException ex)
98         {
99             return null;
100         }
101     }
102     
103     public String JavaDoc query(String JavaDoc id, String JavaDoc group)
104     {
105         String JavaDoc query = "select message from canned_message_tbl where id = ? and grp = ?";
106         
107         try
108         {
109             Connection c = getConnection();
110             if (c == null)
111             {
112                 return null;
113             }
114             
115             PreparedStatement pstmt = c.prepareStatement(query);
116             pstmt.setString(1, id);
117             pstmt.setString(2, group);
118             ResultSet rslt = pstmt.executeQuery();
119
120             if (rslt.next() == true)
121             {
122                 Blob blob = rslt.getBlob(1);
123                 return new String JavaDoc(blob.getBytes(1, (int)blob.length()));
124             }
125         }
126         catch (SQLException ex)
127         {
128             return null;
129         }
130         
131         return null;
132     }
133     
134     public void setParam(String JavaDoc name, String JavaDoc value)
135     {
136         if (name.equals("jdbc") == true)
137         {
138             jdbcClass = value;
139         }
140         else if (name.equals("url") == true)
141         {
142             url = value;
143         }
144         else if (name.equals("user") == true)
145         {
146             user = value;
147         }
148         else if (name.equals("password") == true)
149         {
150             password = value;
151         }
152     }
153     
154     private void exerciseConnection()
155     {
156         Statement st;
157         try
158         {
159             st = connection.createStatement();
160             st.executeQuery("SELECT 1");
161             st.close();
162         }
163         catch (SQLException e)
164         {
165             // do nothing, this should set the state of the connection
166
// where connection.isClosed() is going to return true.
167
return;
168         }
169     }
170     
171     private Connection getConnection()
172     {
173         try
174         {
175             boolean new_connection = false;
176             if (connection == null)
177             {
178                 new_connection = true;
179             }
180             else
181             {
182                 exerciseConnection();
183                 if (connection.isClosed() == true)
184                 {
185                     new_connection = true;
186                 }
187             }
188             
189             if (new_connection == true)
190             {
191                 Class.forName(jdbcClass).newInstance();
192                 connection = DriverManager.getConnection(url, user, password);
193                 return connection;
194             }
195             else
196             {
197                 return connection;
198             }
199         }
200         catch (Exception JavaDoc ex)
201         {
202             return null;
203         }
204     }
205     
206     /** Getter for property jdbcClass.
207      * @return Value of property jdbcClass.
208      *
209      */

210     public String JavaDoc getJdbcClass()
211     {
212         return this.jdbcClass;
213     }
214     
215     /** Setter for property jdbcClass.
216      * @param jdbcClass New value of property jdbcClass.
217      *
218      */

219     public void setJdbcClass(String JavaDoc jdbcClass)
220     {
221         this.jdbcClass = jdbcClass;
222     }
223     
224     /** Getter for property url.
225      * @return Value of property url.
226      *
227      */

228     public String JavaDoc getUrl()
229     {
230         return this.url;
231     }
232     
233     /** Setter for property url.
234      * @param url New value of property url.
235      *
236      */

237     public void setUrl(String JavaDoc url)
238     {
239         this.url = url;
240     }
241     
242     /** Getter for property user.
243      * @return Value of property user.
244      *
245      */

246     public String JavaDoc getUser()
247     {
248         return this.user;
249     }
250     
251     /** Setter for property user.
252      * @param user New value of property user.
253      *
254      */

255     public void setUser(String JavaDoc user)
256     {
257         this.user = user;
258     }
259     
260     /** Getter for property password.
261      * @return Value of property password.
262      *
263      */

264     public String JavaDoc getPassword()
265     {
266         return this.password;
267     }
268     
269     /** Setter for property password.
270      * @param password New value of property password.
271      *
272      */

273     public void setPassword(String JavaDoc password)
274     {
275         this.password = password;
276     }
277     
278     public void destroy()
279     {
280         if (getConnection() != null)
281         {
282             try
283             {
284                 getConnection().close();
285             }
286             catch (SQLException ex)
287             {
288             }
289         }
290     }
291     
292 }
293
Popular Tags