KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > impl > DbProfileManager


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

25 package org.nemesis.forum.impl;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.nemesis.forum.Forum;
37 import org.nemesis.forum.Group;
38 import org.nemesis.forum.ProfileManager;
39 import org.nemesis.forum.User;
40 import org.nemesis.forum.exception.GroupAlreadyExistsException;
41 import org.nemesis.forum.exception.GroupNotFoundException;
42 import org.nemesis.forum.exception.UnauthorizedException;
43 import org.nemesis.forum.exception.UserAlreadyExistsException;
44 import org.nemesis.forum.exception.UserNotFoundException;
45 import org.nemesis.forum.util.cache.CacheableInteger;
46 import org.nemesis.forum.util.jdbc.DbConnectionManager;
47 /**
48  * Database implementation of the ProfileManager interface.
49  */

50 public class DbProfileManager implements ProfileManager {
51     static protected Log log = LogFactory.getLog(DbProfileManager.class);
52     /** DATABASE QUERIES **/
53     private static final String JavaDoc USER_GROUPS = "SELECT groupID from yazdGroupUser WHERE userID=?";
54     private static final String JavaDoc USER_MESSAGE_COUNT =
55         "SELECT count(*) FROM yazdMessage,yazdForum,yazdThread WHERE "
56             + "yazdMessage.userID=? AND yazdForum.forumID=? AND "
57             + "yazdThread.forumID=yazdForum.forumID AND "
58             + "yazdMessage.threadID=yazdThread.threadID";
59     private static final String JavaDoc USER_COUNT = "SELECT count(*) FROM yazdUser";
60     private static final String JavaDoc ALL_USER_MESSAGES = "SELECT messageID FROM yazdMessage WHERE userID=?";
61     private static final String JavaDoc DELETE_USER_MESSAGES = "UPDATE yazdMessage set userID=-1 WHERE userID=?";
62     private static final String JavaDoc DELETE_USER_PERMS = "DELETE FROM yazdUserPerm WHERE userID=?";
63     private static final String JavaDoc DELETE_USER_GROUPS = "DELETE FROM yazdGroupUser WHERE userID=?";
64     private static final String JavaDoc DELETE_USER_PROPS = "DELETE FROM yazdUserProp WHERE userID=?";
65     private static final String JavaDoc DELETE_USER = "DELETE FROM yazdUser WHERE userID=?";
66     private static final String JavaDoc GROUP_COUNT = "SELECT count(*) FROM yazdGroup";
67     private static final String JavaDoc DELETE_GROUP_USERS = "DELETE FROM yazdGroupUser WHERE groupID=?";
68     private static final String JavaDoc DELETE_GROUP = "DELETE FROM yazdGroup WHERE groupID=?";
69
70     private User anonymousUser = null;
71     private User specialUser = null;
72     private DbForumFactory factory;
73
74     /**
75      * Creates a new ProfileManager.
76      */

77     public DbProfileManager(DbForumFactory factory) {
78         this.factory = factory;
79         try {
80             anonymousUser = getUser(-1);
81             specialUser = getUser(0);
82         } catch (UserNotFoundException unfe) {
83         }
84     }
85
86     //FROM THE PROFILEMANAGER INTERFACE//
87

88     public User createUser(String JavaDoc username, String JavaDoc password, String JavaDoc email) throws UserAlreadyExistsException {
89         User newUser = null;
90         try {
91             User existingUser = getUser(username);
92
93             //The user already exists since now exception, so:
94
throw new UserAlreadyExistsException();
95         } catch (UserNotFoundException unfe) {
96             //The user doesn't already exist so we can create a new user
97
newUser = new DbUser(username, password, email);
98         }
99         return newUser;
100     }
101
102     public User getUser(int userID) throws UserNotFoundException {
103         DbCacheManager cacheManager = factory.getCacheManager();
104         //If cache is not enabled, do a new lookup of object
105
if (!cacheManager.isCacheEnabled()) {
106             return new DbUser(userID);
107         }
108         //Cache is enabled.
109
Integer JavaDoc userIDInteger = new Integer JavaDoc(userID);
110         DbUser user = (DbUser) cacheManager.get(DbCacheManager.USER_CACHE, userIDInteger);
111         if (user == null) {
112             user = new DbUser(userID);
113             cacheManager.add(DbCacheManager.USER_CACHE, userIDInteger, user);
114         }
115         return user;
116     }
117
118     public User getUser(String JavaDoc username) throws UserNotFoundException {
119         DbCacheManager cacheManager = factory.getCacheManager();
120         //If cache is not enabled, do a new lookup of object
121
if (!cacheManager.isCacheEnabled()) {
122             User user = new DbUser(username);
123             return getUser(user.getID());
124         }
125         //Cache is enabled.
126
CacheableInteger userIDInteger = (CacheableInteger) cacheManager.get(DbCacheManager.USER_ID_CACHE, username);
127         //if id wan't found in cache, load it up and put it there.
128
if (userIDInteger == null) {
129             User user = new DbUser(username);
130             userIDInteger = new CacheableInteger(new Integer JavaDoc(user.getID()));
131             cacheManager.add(DbCacheManager.USER_ID_CACHE, username, userIDInteger);
132         }
133         return getUser(userIDInteger.getInteger().intValue());
134     }
135
136     public User getAnonymousUser() {
137         return anonymousUser;
138     }
139
140     public User getSpecialUser() {
141         return specialUser;
142     }
143
144     public void deleteUser(User user) throws UnauthorizedException {
145         int userID = user.getID();
146         int[] messages;
147         //Get array of all user's messages in the system so that
148
//we can expire them from cache.
149
ArrayList JavaDoc tempMessages = new ArrayList JavaDoc();
150         Connection JavaDoc con = null;
151         PreparedStatement JavaDoc pstmt = null;
152         try {
153             con = DbConnectionManager.getConnection();
154             pstmt = con.prepareStatement(ALL_USER_MESSAGES);
155             pstmt.setInt(1, user.getID());
156             ResultSet JavaDoc rs = pstmt.executeQuery();
157             while (rs.next()) {
158                 tempMessages.add(new Integer JavaDoc(rs.getInt("messageID")));
159             }
160         } catch (SQLException JavaDoc sqle) {
161             log.error("Error in DbProfileManager:deleteUser()-" , sqle);
162             
163         } finally {
164             try {
165                 pstmt.close();
166             } catch (Exception JavaDoc e) {
167                 log.error("" , e);
168             }
169             try {
170                 con.close();
171             } catch (Exception JavaDoc e) {
172                 log.error("" , e);
173             }
174         }
175         //Now copy into an array.
176
messages = new int[tempMessages.size()];
177         for (int i = 0; i < messages.length; i++) {
178             messages[i] = ((Integer JavaDoc) tempMessages.get(i)).intValue();
179         }
180
181         con = null;
182         pstmt = null;
183         try {
184             con = DbConnectionManager.getConnection();
185             //mark all message by user as anonymous
186
pstmt = con.prepareStatement(DELETE_USER_MESSAGES);
187             pstmt.setInt(1, userID);
188             pstmt.execute();
189             pstmt.close();
190             //remove all permissions given to user
191
pstmt = con.prepareStatement(DELETE_USER_PERMS);
192             pstmt.setInt(1, userID);
193             pstmt.execute();
194             pstmt.close();
195             //remove user from all groups
196
pstmt = con.prepareStatement(DELETE_USER_GROUPS);
197             pstmt.setInt(1, userID);
198             pstmt.execute();
199             pstmt.close();
200             //delete all of the users's extended properties
201
pstmt = con.prepareStatement(DELETE_USER_PROPS);
202             pstmt.setInt(1, userID);
203             pstmt.execute();
204             pstmt.close();
205             //delete the actual user entry
206
pstmt = con.prepareStatement(DELETE_USER);
207             pstmt.setInt(1, userID);
208             pstmt.execute();
209         } catch (SQLException JavaDoc sqle) {
210             log.error("" , sqle);
211         } finally {
212             try {
213                 pstmt.close();
214             } catch (Exception JavaDoc e) {
215                 log.error("" , e);
216             }
217             try {
218                 con.close();
219             } catch (Exception JavaDoc e) {
220                 log.error("" , e);
221             }
222         }
223
224         //Finally, expire all relevant caches
225
//all of users's messages
226
DbCacheManager cacheManager = factory.getCacheManager();
227         for (int i = 0; i < messages.length; i++) {
228             cacheManager.remove(DbCacheManager.MESSAGE_CACHE, new Integer JavaDoc(messages[i]));
229         }
230         //user cache
231
cacheManager.remove(DbCacheManager.USER_ID_CACHE, user.getUsername());
232         cacheManager.remove(DbCacheManager.USER_CACHE, new Integer JavaDoc(userID));
233     }
234
235     public Group createGroup(String JavaDoc name) throws UnauthorizedException, GroupAlreadyExistsException {
236         Group newGroup = null;
237         try {
238             Group existingGroup = getGroup(name);
239
240             //The group already exists since now exception, so:
241
throw new GroupAlreadyExistsException();
242         } catch (GroupNotFoundException unfe) {
243             //The group doesn't already exist so we can create a new group
244
newGroup = new DbGroup(name, factory);
245         }
246         return newGroup;
247     }
248
249     public Group getGroup(int groupID) throws GroupNotFoundException {
250         DbCacheManager cacheManager = factory.getCacheManager();
251         //If cache is not enabled, do a new lookup of object
252
if (!cacheManager.isCacheEnabled()) {
253             return new DbGroup(groupID, factory);
254         }
255         //Cache is enabled.
256
Integer JavaDoc groupIDInteger = new Integer JavaDoc(groupID);
257         DbGroup group = (DbGroup) cacheManager.get(DbCacheManager.GROUP_CACHE, groupIDInteger);
258         if (group == null) {
259             group = new DbGroup(groupID, factory);
260             cacheManager.add(DbCacheManager.GROUP_CACHE, groupIDInteger, group);
261         }
262         return group;
263     }
264
265     public Group getGroup(String JavaDoc name) throws GroupNotFoundException {
266         DbCacheManager cacheManager = factory.getCacheManager();
267         //If cache is not enabled, do a new lookup of object
268
if (!cacheManager.isCacheEnabled()) {
269             Group group = new DbGroup(name, null, factory);
270             return getGroup(group.getID());
271         }
272         //Cache is enabled.
273
CacheableInteger groupIDInteger = (CacheableInteger) cacheManager.get(DbCacheManager.GROUP_ID_CACHE, name);
274         //if id wan't found in cache, load it up and put it there.
275
if (groupIDInteger == null) {
276             Group group = new DbGroup(name, null, factory);
277             groupIDInteger = new CacheableInteger(new Integer JavaDoc(group.getID()));
278             cacheManager.add(DbCacheManager.GROUP_ID_CACHE, name, groupIDInteger);
279         }
280         return getGroup(groupIDInteger.getInteger().intValue());
281     }
282
283     public void deleteGroup(Group group) throws UnauthorizedException {
284         int groupID = group.getID();
285         int[] members = new int[group.getMemberCount()];
286         Iterator JavaDoc iter = group.members();
287         for (int i = 0; i < members.length; i++) {
288             User user = (User) iter.next();
289             members[i] = user.getID();
290         }
291
292         Connection JavaDoc con = null;
293         PreparedStatement JavaDoc pstmt = null;
294         try {
295             con = DbConnectionManager.getConnection();
296             //mark all message by user as anonymous
297
pstmt = con.prepareStatement(DELETE_GROUP_USERS);
298             pstmt.setInt(1, groupID);
299             pstmt.execute();
300             pstmt.close();
301             //remove all permissions given to user
302
pstmt = con.prepareStatement(DELETE_GROUP);
303             pstmt.setInt(1, groupID);
304             pstmt.execute();
305             pstmt.close();
306         } catch (SQLException JavaDoc sqle) {
307             log.error("" , sqle);
308         } finally {
309             try {
310                 pstmt.close();
311             } catch (Exception JavaDoc e) {
312                 log.error("" , e);
313             }
314             try {
315                 con.close();
316             } catch (Exception JavaDoc e) {
317                 log.error("" , e);
318             }
319         }
320
321         //Finally, expire all relevant caches
322
DbCacheManager cacheManager = factory.getCacheManager();
323         cacheManager.remove(DbCacheManager.GROUP_ID_CACHE, group.getName());
324         cacheManager.remove(DbCacheManager.GROUP_CACHE, new Integer JavaDoc(groupID));
325         //Removing a group can change the permissions of all the users in that
326
//group. Therefore, remove each user from the user perms cache.
327
for (int i = 0; i < members.length; i++) {
328             cacheManager.removeUserPerm(new Integer JavaDoc(members[i]));
329         }
330     }
331
332     public int getUserCount() {
333         int count = 0;
334         Connection JavaDoc con = null;
335         PreparedStatement JavaDoc pstmt = null;
336         try {
337             con = DbConnectionManager.getConnection();
338             pstmt = con.prepareStatement(USER_COUNT);
339             ResultSet JavaDoc rs = pstmt.executeQuery();
340             if (rs.next()) {
341                 count = rs.getInt(1);
342             }
343         } catch (SQLException JavaDoc sqle) {
344             log.error("" , sqle);
345         } finally {
346             try {
347                 pstmt.close();
348             } catch (Exception JavaDoc e) {
349                 log.error("" , e);
350             }
351             try {
352                 con.close();
353             } catch (Exception JavaDoc e) {
354                 log.error("" , e);
355             }
356         }
357         return count;
358     }
359
360     public int getGroupCount() {
361         int count = 0;
362         Connection JavaDoc con = null;
363         PreparedStatement JavaDoc pstmt = null;
364         try {
365             con = DbConnectionManager.getConnection();
366             pstmt = con.prepareStatement(GROUP_COUNT);
367             ResultSet JavaDoc rs = pstmt.executeQuery();
368             if (rs.next()) {
369                 count = rs.getInt(1);
370             }
371         } catch (SQLException JavaDoc sqle) {
372             log.error("" , sqle);
373         } finally {
374             try {
375                 pstmt.close();
376             } catch (Exception JavaDoc e) {
377                 log.error("" , e);
378             }
379             try {
380                 con.close();
381             } catch (Exception JavaDoc e) {
382                 log.error("" , e);
383             }
384         }
385         return count;
386     }
387
388     public Iterator JavaDoc users() {
389         return new DbUserIterator(this);
390     }
391
392     public Iterator JavaDoc users(int startIndex, int numResults) {
393         return new DbUserIterator(this, startIndex, numResults);
394     }
395
396     public Iterator JavaDoc groups() {
397         return new DbGroupIterator(this);
398     }
399
400     public Iterator JavaDoc groups(int startIndex, int numResults) {
401         return new DbGroupIterator(this, startIndex, numResults);
402     }
403
404     public int userMessageCount(User user, Forum forum) {
405         int count = 0;
406         Connection JavaDoc con = null;
407         PreparedStatement JavaDoc pstmt = null;
408         try {
409             con = DbConnectionManager.getConnection();
410             pstmt = con.prepareStatement(USER_MESSAGE_COUNT);
411             pstmt.setInt(1, user.getID());
412             pstmt.setInt(2, forum.getID());
413             ResultSet JavaDoc rs = pstmt.executeQuery();
414             if (rs.next()) {
415                 count = rs.getInt(1);
416             }
417         } catch (SQLException JavaDoc sqle) {
418             log.error("" , sqle);
419         } finally {
420             try {
421                 pstmt.close();
422             } catch (Exception JavaDoc e) {
423                 log.error("" , e);
424             }
425             try {
426                 con.close();
427             } catch (Exception JavaDoc e) {
428                 log.error("" , e);
429             }
430         }
431         return count;
432     }
433
434     public Iterator JavaDoc userMessages(User user, Forum forum) {
435         return new DbUserMessagesIterator(factory, user, forum);
436     }
437
438     /**
439      * Returns an array of all the groups that the user belongs to.
440      */

441     protected int[] getUserGroups(int userID) {
442         Connection JavaDoc con = null;
443         PreparedStatement JavaDoc pstmt = null;
444         int[] groups = new int[0];
445         try {
446             con = DbConnectionManager.getConnection();
447             pstmt = con.prepareStatement(USER_GROUPS);
448             pstmt.setInt(1, userID);
449             ResultSet JavaDoc rs = pstmt.executeQuery();
450             ArrayList JavaDoc groupList = new ArrayList JavaDoc();
451             while (rs.next()) {
452                 groupList.add(new Integer JavaDoc(rs.getInt("groupID")));
453             }
454             groups = new int[groupList.size()];
455             for (int i = 0; i < groups.length; i++) {
456                 groups[i] = ((Integer JavaDoc) groupList.get(i)).intValue();
457             }
458         } catch (SQLException JavaDoc sqle) {
459             log.error("" , sqle);
460         } finally {
461             try {
462                 pstmt.close();
463             } catch (Exception JavaDoc e) {
464                 log.error("" , e);
465             }
466             try {
467                 con.close();
468             } catch (Exception JavaDoc e) {
469                 log.error("" , e);
470             }
471         }
472         return groups;
473     }
474 }
475
Popular Tags