KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > group > DefaultGroupProvider


1 /**
2  * $RCSfile: DefaultGroupProvider.java,v $
3  * $Revision: 1.6 $
4  * $Date: 2005/07/08 20:21:58 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.group;
13
14 import org.jivesoftware.database.DbConnectionManager;
15 import org.jivesoftware.util.*;
16 import org.jivesoftware.messenger.user.User;
17
18 import java.sql.Connection JavaDoc;
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Collection JavaDoc;
25
26 /**
27  * Database implementation of the GroupManager interface.
28  *
29  * @author Matt Tucker
30  */

31 public class DefaultGroupProvider implements GroupProvider {
32
33     private static final String JavaDoc INSERT_GROUP =
34         "INSERT INTO jiveGroup (groupName, description) VALUES (?, ?)";
35     private static final String JavaDoc SAVE_GROUP =
36         "UPDATE jiveGroup SET description=? WHERE groupName=?";
37     private static final String JavaDoc SET_GROUP_NAME_1 =
38         "UPDATE jiveGroup SET groupName=? WHERE groupName=?";
39     private static final String JavaDoc SET_GROUP_NAME_2 =
40         "UPDATE jiveGroupProp SET groupName=? WHERE groupName=?";
41     private static final String JavaDoc SET_GROUP_NAME_3 =
42         "UPDATE jiveGroupUser SET groupName=? WHERE groupName=?";
43     private static final String JavaDoc DELETE_GROUP_USERS =
44         "DELETE FROM jiveGroupUser WHERE groupName=?";
45     private static final String JavaDoc DELETE_PROPERTIES =
46         "DELETE FROM jiveGroupProp WHERE groupName=?";
47     private static final String JavaDoc DELETE_GROUP =
48         "DELETE FROM jiveGroup WHERE groupName=?";
49     private static final String JavaDoc GROUP_COUNT = "SELECT count(*) FROM jiveGroup";
50      private static final String JavaDoc LOAD_ADMINS =
51         "SELECT username FROM jiveGroupUser WHERE administrator=1 AND groupName=?";
52     private static final String JavaDoc LOAD_MEMBERS =
53         "SELECT username FROM jiveGroupUser WHERE administrator=0 AND groupName=?";
54     private static final String JavaDoc LOAD_GROUP =
55         "SELECT description FROM jiveGroup WHERE groupName=?";
56     private static final String JavaDoc REMOVE_USER =
57         "DELETE FROM jiveGroupUser WHERE groupName=? AND username=?";
58     private static final String JavaDoc ADD_USER =
59         "INSERT INTO jiveGroupUser (groupName, username, administrator) VALUES (?, ?, ?)";
60     private static final String JavaDoc UPDATE_USER =
61         "UPDATE jiveGroupUser SET administrator=? WHERE groupName=? AND username=?";
62     private static final String JavaDoc USER_GROUPS =
63         "SELECT groupName FROM jiveGroupUser WHERE username=?";
64     private static final String JavaDoc ALL_GROUPS = "SELECT groupName FROM jiveGroup ORDER BY groupName";
65
66     public Group createGroup(String JavaDoc name) throws GroupAlreadyExistsException {
67         Connection JavaDoc con = null;
68         PreparedStatement JavaDoc pstmt = null;
69         try {
70             con = DbConnectionManager.getConnection();
71             pstmt = con.prepareStatement(INSERT_GROUP);
72             pstmt.setString(1, name);
73             pstmt.setString(2, "");
74             pstmt.executeUpdate();
75         }
76         catch (SQLException JavaDoc e) {
77             Log.error(e);
78         }
79         finally {
80             try { if (pstmt != null) pstmt.close(); }
81             catch (Exception JavaDoc e) { Log.error(e); }
82             try { if (con != null) con.close(); }
83             catch (Exception JavaDoc e) { Log.error(e); }
84         }
85         Collection JavaDoc<String JavaDoc> members = getMembers(name, false);
86         Collection JavaDoc<String JavaDoc> administrators = getMembers(name, true);
87         return new Group(this, name, "", members, administrators);
88     }
89
90     public Group getGroup(String JavaDoc name) throws GroupNotFoundException {
91         String JavaDoc description = null;
92
93         Connection JavaDoc con = null;
94         PreparedStatement JavaDoc pstmt = null;
95         try {
96             con = DbConnectionManager.getConnection();
97             pstmt = con.prepareStatement(LOAD_GROUP);
98             pstmt.setString(1, name);
99             ResultSet JavaDoc rs = pstmt.executeQuery();
100             if (!rs.next()) {
101                 throw new GroupNotFoundException("Group with name "
102                     + name + " not found.");
103             }
104             description = rs.getString(1);
105         }
106         catch (SQLException JavaDoc e) {
107             Log.error(e);
108         }
109         finally {
110             try { if (pstmt != null) pstmt.close(); }
111             catch (Exception JavaDoc e) { Log.error(e); }
112             try { if (con != null) con.close(); }
113             catch (Exception JavaDoc e) { Log.error(e); }
114         }
115         Collection JavaDoc<String JavaDoc> members = getMembers(name, false);
116         Collection JavaDoc<String JavaDoc> administrators = getMembers(name, true);
117         return new Group(this, name, description, members, administrators);
118     }
119
120     public void setDescription(String JavaDoc name, String JavaDoc description)
121             throws GroupNotFoundException
122     {
123         Connection JavaDoc con = null;
124         PreparedStatement JavaDoc pstmt = null;
125         try {
126             con = DbConnectionManager.getConnection();
127             pstmt = con.prepareStatement(SAVE_GROUP);
128             pstmt.setString(1, description);
129             pstmt.setString(2, name);
130             pstmt.executeUpdate();
131         }
132         catch (SQLException JavaDoc e) {
133             Log.error(e);
134             throw new GroupNotFoundException();
135         }
136         finally {
137             try { if (pstmt != null) pstmt.close(); }
138             catch (Exception JavaDoc e) { Log.error(e); }
139             try { if (con != null) con.close(); }
140             catch (Exception JavaDoc e) { Log.error(e); }
141         }
142     }
143
144     public void setName(String JavaDoc oldName, String JavaDoc newName) throws UnsupportedOperationException JavaDoc,
145             GroupAlreadyExistsException
146     {
147         Connection JavaDoc con = null;
148         PreparedStatement JavaDoc pstmt = null;
149         boolean abortTransaction = false;
150         try {
151             con = DbConnectionManager.getTransactionConnection();
152             pstmt = con.prepareStatement(SET_GROUP_NAME_1);
153             pstmt.setString(1, newName);
154             pstmt.setString(2, oldName);
155             pstmt.executeUpdate();
156             pstmt.close();
157             pstmt = con.prepareStatement(SET_GROUP_NAME_2);
158             pstmt.setString(1, newName);
159             pstmt.setString(2, oldName);
160             pstmt.executeUpdate();
161             pstmt.close();
162             pstmt = con.prepareStatement(SET_GROUP_NAME_3);
163             pstmt.setString(1, newName);
164             pstmt.setString(2, oldName);
165             pstmt.executeUpdate();
166         }
167         catch (SQLException JavaDoc e) {
168             Log.error(e);
169             abortTransaction = true;
170         }
171         finally {
172             try { if (pstmt != null) pstmt.close(); }
173             catch (Exception JavaDoc e) { Log.error(e); }
174             DbConnectionManager.closeTransactionConnection(con, abortTransaction);
175         }
176     }
177
178     public void deleteGroup(String JavaDoc groupName) {
179         Connection JavaDoc con = null;
180         PreparedStatement JavaDoc pstmt = null;
181         boolean abortTransaction = false;
182         try {
183             con = DbConnectionManager.getTransactionConnection();
184             // Remove all users in the group.
185
pstmt = con.prepareStatement(DELETE_GROUP_USERS);
186             pstmt.setString(1, groupName);
187             pstmt.executeUpdate();
188             pstmt.close();
189             // Remove all properties of the group.
190
pstmt = con.prepareStatement(DELETE_PROPERTIES);
191             pstmt.setString(1, groupName);
192             pstmt.executeUpdate();
193             pstmt.close();
194             // Remove the group entry.
195
pstmt = con.prepareStatement(DELETE_GROUP);
196             pstmt.setString(1, groupName);
197             pstmt.executeUpdate();
198             pstmt.close();
199         }
200         catch (SQLException JavaDoc e) {
201             Log.error(e);
202             abortTransaction = true;
203         }
204         finally {
205             try { if (pstmt != null) pstmt.close(); }
206             catch (Exception JavaDoc e) { Log.error(e); }
207             DbConnectionManager.closeTransactionConnection(con, abortTransaction);
208         }
209     }
210
211     public int getGroupCount() {
212         int count = 0;
213         Connection JavaDoc con = null;
214         PreparedStatement JavaDoc pstmt = null;
215         try {
216             con = DbConnectionManager.getConnection();
217             pstmt = con.prepareStatement(GROUP_COUNT);
218             ResultSet JavaDoc rs = pstmt.executeQuery();
219             if (rs.next()) {
220                 count = rs.getInt(1);
221             }
222             rs.close();
223         }
224         catch (SQLException JavaDoc e) {
225             Log.error(e);
226         }
227         finally {
228             try { if (pstmt != null) pstmt.close(); }
229             catch (Exception JavaDoc e) { Log.error(e); }
230             try { if (con != null) con.close(); }
231             catch (Exception JavaDoc e) { Log.error(e); }
232         }
233         return count;
234     }
235
236     public Collection JavaDoc<Group> getGroups() {
237         List JavaDoc<String JavaDoc> groupNames = new ArrayList JavaDoc<String JavaDoc>();
238         Connection JavaDoc con = null;
239         PreparedStatement JavaDoc pstmt = null;
240         try {
241             con = DbConnectionManager.getConnection();
242             pstmt = con.prepareStatement(ALL_GROUPS);
243             ResultSet JavaDoc rs = pstmt.executeQuery();
244             while (rs.next()) {
245                 groupNames.add(rs.getString(1));
246             }
247             rs.close();
248         }
249         catch (SQLException JavaDoc e) {
250             Log.error(e);
251         }
252         finally {
253             try { if (pstmt != null) pstmt.close(); }
254             catch (Exception JavaDoc e) { Log.error(e); }
255             try { if (con != null) con.close(); }
256             catch (Exception JavaDoc e) { Log.error(e); }
257         }
258         List JavaDoc<Group> groups = new ArrayList JavaDoc<Group>(groupNames.size());
259         GroupManager manager = GroupManager.getInstance();
260         for (String JavaDoc groupName : groupNames) {
261             try {
262                 groups.add(manager.getGroup(groupName));
263             }
264             catch (GroupNotFoundException e) {
265                 Log.error(e);
266             }
267         }
268         return groups;
269     }
270
271     public Collection JavaDoc<Group> getGroups(int startIndex, int numResults) {
272         List JavaDoc<String JavaDoc> groupNames = new ArrayList JavaDoc<String JavaDoc>();
273         Connection JavaDoc con = null;
274         PreparedStatement JavaDoc pstmt = null;
275         try {
276             con = DbConnectionManager.getConnection();
277             pstmt = con.prepareStatement(ALL_GROUPS);
278             ResultSet JavaDoc rs = pstmt.executeQuery();
279             DbConnectionManager.scrollResultSet(rs, startIndex);
280             int count = 0;
281             while (rs.next() && count < numResults) {
282                 groupNames.add(rs.getString(1));
283                 count++;
284             }
285             rs.close();
286         }
287         catch (SQLException JavaDoc e) {
288             Log.error(e);
289         }
290         finally {
291             try { if (pstmt != null) pstmt.close(); }
292             catch (Exception JavaDoc e) { Log.error(e); }
293             try { if (con != null) con.close(); }
294             catch (Exception JavaDoc e) { Log.error(e); }
295         }
296         List JavaDoc<Group> groups = new ArrayList JavaDoc<Group>(groupNames.size());
297         GroupManager manager = GroupManager.getInstance();
298         for (String JavaDoc groupName : groupNames) {
299             try {
300                 groups.add(manager.getGroup(groupName));
301             }
302             catch (GroupNotFoundException e) {
303                 Log.error(e);
304             }
305         }
306         return groups;
307     }
308
309     public Collection JavaDoc<Group> getGroups(User user) {
310         List JavaDoc<String JavaDoc> groupNames = new ArrayList JavaDoc<String JavaDoc>();
311         Connection JavaDoc con = null;
312         PreparedStatement JavaDoc pstmt = null;
313         try {
314             con = DbConnectionManager.getConnection();
315             pstmt = con.prepareStatement(USER_GROUPS);
316             pstmt.setString(1, user.getUsername());
317             ResultSet JavaDoc rs = pstmt.executeQuery();
318             while (rs.next()) {
319                 groupNames.add(rs.getString(1));
320             }
321             rs.close();
322         }
323         catch (SQLException JavaDoc e) {
324             Log.error(e);
325         }
326         finally {
327             try { if (pstmt != null) pstmt.close(); }
328             catch (Exception JavaDoc e) { Log.error(e); }
329             try { if (con != null) con.close(); }
330             catch (Exception JavaDoc e) { Log.error(e); }
331         }
332         List JavaDoc<Group> groups = new ArrayList JavaDoc<Group>(groupNames.size());
333         GroupManager manager = GroupManager.getInstance();
334         for (String JavaDoc groupName : groupNames) {
335             try {
336                 groups.add(manager.getGroup(groupName));
337             }
338             catch (GroupNotFoundException e) {
339                 Log.error(e);
340             }
341         }
342         return groups;
343     }
344
345     public void addMember(String JavaDoc groupName, String JavaDoc username, boolean administrator) {
346         Connection JavaDoc con = null;
347         PreparedStatement JavaDoc pstmt = null;
348         try {
349             con = DbConnectionManager.getConnection();
350             pstmt = con.prepareStatement(ADD_USER);
351             pstmt.setString(1, groupName);
352             pstmt.setString(2, username);
353             pstmt.setInt(3, administrator ? 1 : 0);
354             pstmt.executeUpdate();
355         }
356         catch (SQLException JavaDoc e) {
357             Log.error(e);
358         }
359         finally {
360             try { if (pstmt != null) pstmt.close(); }
361             catch (Exception JavaDoc e) { Log.error(e); }
362             try { if (con != null) con.close(); }
363             catch (Exception JavaDoc e) { Log.error(e); }
364         }
365     }
366
367     public void updateMember(String JavaDoc groupName, String JavaDoc username, boolean administrator) {
368         Connection JavaDoc con = null;
369         PreparedStatement JavaDoc pstmt = null;
370         try {
371             con = DbConnectionManager.getConnection();
372             pstmt = con.prepareStatement(UPDATE_USER);
373             pstmt.setInt(1, administrator ? 1 : 0);
374             pstmt.setString(2, groupName);
375             pstmt.setString(3, username);
376             pstmt.executeUpdate();
377         }
378         catch (SQLException JavaDoc e) {
379             Log.error(e);
380         }
381         finally {
382             try { if (pstmt != null) pstmt.close(); }
383             catch (Exception JavaDoc e) { Log.error(e); }
384             try { if (con != null) con.close(); }
385             catch (Exception JavaDoc e) { Log.error(e); }
386         }
387     }
388
389     public void deleteMember(String JavaDoc groupName, String JavaDoc username) {
390         Connection JavaDoc con = null;
391         PreparedStatement JavaDoc pstmt = null;
392         try {
393             con = DbConnectionManager.getConnection();
394             pstmt = con.prepareStatement(REMOVE_USER);
395             pstmt.setString(1, groupName);
396             pstmt.setString(2, username);
397             pstmt.executeUpdate();
398         }
399         catch (SQLException JavaDoc e) {
400             Log.error(e);
401         }
402         finally {
403             try { if (pstmt != null) pstmt.close(); }
404             catch (Exception JavaDoc e) { Log.error(e); }
405             try { if (con != null) con.close(); }
406             catch (Exception JavaDoc e) { Log.error(e); }
407         }
408     }
409
410     public boolean isReadOnly() {
411         return false;
412     }
413
414     private Collection JavaDoc<String JavaDoc> getMembers(String JavaDoc groupName, boolean adminsOnly) {
415         List JavaDoc<String JavaDoc> members = new ArrayList JavaDoc<String JavaDoc>();
416         Connection JavaDoc con = null;
417         PreparedStatement JavaDoc pstmt = null;
418         try {
419             con = DbConnectionManager.getConnection();
420             if (adminsOnly) {
421                 pstmt = con.prepareStatement(LOAD_ADMINS);
422             }
423             else {
424                 pstmt = con.prepareStatement(LOAD_MEMBERS);
425             }
426             pstmt.setString(1, groupName);
427             ResultSet JavaDoc rs = pstmt.executeQuery();
428             while (rs.next()) {
429                 members.add(rs.getString(1));
430             }
431             rs.close();
432         }
433         catch (SQLException JavaDoc e) {
434             Log.error(e);
435         }
436         finally {
437             try { if (pstmt != null) pstmt.close(); }
438             catch (Exception JavaDoc e) { Log.error(e); }
439             try { if (con != null) con.close(); }
440             catch (Exception JavaDoc e) { Log.error(e); }
441         }
442         return members;
443     }
444 }
Popular Tags