KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > UserManager


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j;
17
18 import java.util.Date JavaDoc;
19 import java.util.List JavaDoc;
20
21 import dlog4j.formbean.BookMarkBean;
22 import dlog4j.formbean.LogForm;
23 import dlog4j.formbean.ReplyForm;
24 import dlog4j.formbean.SiteForm;
25 import dlog4j.formbean.UserForm;
26
27 import net.sf.hibernate.Criteria;
28 import net.sf.hibernate.HibernateException;
29 import net.sf.hibernate.Query;
30 import net.sf.hibernate.Session;
31 import net.sf.hibernate.expression.Expression;
32 import net.sf.hibernate.expression.Order;
33
34 /**
35  * @author Liudong
36  * 网站注册用户管理器
37  */

38 public class UserManager {
39
40     public final static String JavaDoc PASSWORD_CRYPT_KEY = "_jdlog_des_key_";
41     /**
42      * 用户资料修改
43      * @param ssn
44      * @param user
45      * @return
46      * @throws HibernateException
47      */

48     public static UserForm updateUser(Session ssn, UserForm user) throws HibernateException{
49         ssn.update(user);
50         return user;
51     }
52     /**
53      * 创建用户
54      * @param ssn
55      * @param user
56      * @return
57      * @throws HibernateException
58      */

59     public static UserForm createUser(Session ssn, UserForm user) throws HibernateException{
60         if(user.getLogCount()<1)
61             user.setLoginCount(1);
62         if(user.getRegTime()==null)
63             user.setRegTime(new Date JavaDoc());
64         if(user.getLastTime()==null)
65             user.setLastTime(user.getRegTime());
66         ssn.save(user);
67         return user;
68     }
69     
70     /**
71      * 通过用户名来获取用户资料,用于注册用户是判断是否重名
72      * @param ssn
73      * @param site
74      * @param nickname
75      * @return
76      * @throws HibernateException
77      */

78     public static UserForm getUserByName(Session ssn, SiteForm site, String JavaDoc nickname) throws HibernateException
79     {
80         UserForm u = null;
81         Criteria crit = ssn.createCriteria(UserForm.class);
82         if(site!=null)
83             crit = crit.add(Expression.eq("site.id", new Integer JavaDoc(site.getId())));
84         crit = crit.add(Expression.eq("displayName",nickname));
85         List JavaDoc users = crit.list();
86         if(users.size()>0)
87             return (UserForm)users.get(0);
88         
89         return null;
90     }
91     
92     /**
93      * 读取用户资料
94      * @param ssn
95      * @param site
96      * @param loginName
97      * @return
98      * @throws HibernateException
99      */

100     public static UserForm getUser(Session ssn, SiteForm site, String JavaDoc loginName) throws HibernateException
101     {
102         UserForm u = null;
103         Criteria crit = ssn.createCriteria(UserForm.class);
104         if(site!=null)
105             crit = crit.add(Expression.eq("site.id", new Integer JavaDoc(site.getId())));
106         crit = crit.add(Expression.eq("loginName",loginName));
107         List JavaDoc users = crit.list();
108         if(users.size()>0)
109             return (UserForm)users.get(0);
110         
111         return null;
112     }
113     /**
114      * 子网站注册用户信息查询(该方法用于members.jsp用于显示最近N个注册用户)
115      * @param ssn
116      * @param site
117      * @param count
118      * @return
119      * @throws SQLException
120      */

121     public static List JavaDoc listUsers(Session ssn, SiteForm site, int from, int count, String JavaDoc username)
122         throws HibernateException {
123         Criteria crit = ssn.createCriteria(UserForm.class);
124         crit = crit.add(Expression.eq("site.id", new Integer JavaDoc(site.getId())));
125         if(username!=null)
126             crit = crit.add(Expression.like("displayName",'%'+username+'%'));
127         crit = crit.addOrder(Order.desc("regTime"));
128         if(from>0)
129             crit = crit.setFirstResult(from);
130         if (count > 0)
131             crit = crit.setMaxResults(count);
132         return crit.list();
133     }
134     /**
135      * 子网站注册用户信息查询(该方法用于members.jsp用于显示最近N个注册用户)
136      * @param ssn
137      * @param site
138      * @param count
139      * @return
140      * @throws SQLException
141      */

142     public static List JavaDoc listUsers(Session ssn, SiteForm site, int count, String JavaDoc username)
143         throws HibernateException {
144         return listUsers(ssn,site,0,count,username);
145     }
146     /**
147      * 获取指定站点某个类型用户的数量(该方法用于日记后台用户维护user_list.jsp)
148      * @param ssn
149      * @param site
150      * @param role
151      * @return
152      * @throws HibernateException
153      */

154     public static int getUserCount(Session ssn, SiteForm site, int role, String JavaDoc username) throws HibernateException
155     {
156         Query query = null;
157         String JavaDoc hsql = " SELECT COUNT(user.id) FROM " + UserForm.class.getName() + " AS user WHERE user.site.id=?";
158         if(role!=-2)
159             hsql += " AND user.userRole=?";
160         if(username!=null)
161             hsql += " AND user.displayName LIKE ?";
162         
163         query = ssn.createQuery(hsql);
164         query.setInteger(0, site.getId());
165         if(role!=-2){
166             query.setInteger(1,role);
167             if(username!=null)
168                 query.setString(2, '%'+username+'%');
169         }
170         else if(username!=null)
171             query.setString(1, '%'+username+'%');
172         List JavaDoc res = query.list();
173         int uc = (res.size() > 0) ? ((Integer JavaDoc) res.get(0)).intValue() : 0;
174         return uc;
175     }
176     /**
177      * 获取某个用户的详细资料信息
178      * @param ssn
179      * @param userid
180      * @param withDetails
181      * @return
182      * @throws HibernateException
183      */

184     public static UserForm getUser(Session ssn,int userid,boolean withDetails) throws HibernateException
185     {
186         UserForm user = (UserForm) ssn.load(UserForm.class, new Integer JavaDoc(userid));
187         fillUserWithLogAndReplyCount(ssn, user, withDetails);
188         return user;
189     }
190     
191     /**
192      * 填充用户的日记和评论信息数
193      * @param ssn
194      * @param user
195      * @param withLogsAndReplyies
196      * @throws HibernateException
197      */

198     public static void fillUserWithLogAndReplyCount(Session ssn, UserForm user, boolean withLogsAndReplyies) throws HibernateException{
199         if (user != null) {
200             //由于logs与replies属性设置了lazy=true所以必须手工去读取该信息
201
//读取日志数
202
Query query = ssn.createQuery("SELECT COUNT(log.id) FROM "
203                         + LogForm.class.getName()
204                         + " AS log WHERE log.owner.id=? AND log.status<>?");
205             query.setInteger(0, user.getId());
206             query.setInteger(1, LogForm.STATUS_DELETED);
207             List JavaDoc res = query.list();
208             int logCount = (res.size()>0)?((Integer JavaDoc)res.get(0)).intValue():0;
209             user.setLogCount(logCount);
210             //读取评论数
211
Query query2 =ssn.createQuery("SELECT COUNT(reply.id) FROM "
212                         + ReplyForm.class.getName()
213                         + " AS reply WHERE reply.author.id=? AND reply.log.status<>?");
214             query2.setInteger(0, user.getId());
215             query2.setInteger(1, LogForm.STATUS_DELETED);
216             List JavaDoc res2 = query2.list();
217             int replyCount = (res2.size()>0)?((Integer JavaDoc)res2.get(0)).intValue():0;
218             user.setReplyCount(replyCount);
219             //读取书签数
220
Query query3 =ssn.createQuery("SELECT COUNT(bm.id) FROM "
221                         + BookMarkBean.class.getName()
222                         + " AS bm WHERE bm.user.id=?");
223             query2.setInteger(0, user.getId());
224             res2 = query2.list();
225             int bmCount = (res2.size()>0)?((Integer JavaDoc)res2.get(0)).intValue():0;
226             user.setBookMarkCount(bmCount);
227
228             if (withLogsAndReplyies) {
229                 user.setLogs(ssn.createCriteria(LogForm.class).add(
230                     Expression.eq("owner.id",new Integer JavaDoc(user.getId()))).addOrder(Order.desc("logTime")).list());
231                 user.setLogs(ssn.createCriteria(ReplyForm.class).add(
232                     Expression.eq("author.id",new Integer JavaDoc(user.getId()))).addOrder(Order.desc("writeTime")).list());
233             }
234         }
235     }
236
237 }
238
Popular Tags