001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package dlog4j;
017:
018: import java.util.Date;
019: import java.util.List;
020:
021: import dlog4j.formbean.BookMarkBean;
022: import dlog4j.formbean.LogForm;
023: import dlog4j.formbean.ReplyForm;
024: import dlog4j.formbean.SiteForm;
025: import dlog4j.formbean.UserForm;
026:
027: import net.sf.hibernate.Criteria;
028: import net.sf.hibernate.HibernateException;
029: import net.sf.hibernate.Query;
030: import net.sf.hibernate.Session;
031: import net.sf.hibernate.expression.Expression;
032: import net.sf.hibernate.expression.Order;
033:
034: /**
035: * @author Liudong
036: * 网站注册用户管理器
037: */
038: public class UserManager {
039:
040: public final static String PASSWORD_CRYPT_KEY = "_jdlog_des_key_";
041:
042: /**
043: * 用户资料修改
044: * @param ssn
045: * @param user
046: * @return
047: * @throws HibernateException
048: */
049: public static UserForm updateUser(Session ssn, UserForm user)
050: throws HibernateException {
051: ssn.update(user);
052: return user;
053: }
054:
055: /**
056: * 创建用户
057: * @param ssn
058: * @param user
059: * @return
060: * @throws HibernateException
061: */
062: public static UserForm createUser(Session ssn, UserForm user)
063: throws HibernateException {
064: if (user.getLogCount() < 1)
065: user.setLoginCount(1);
066: if (user.getRegTime() == null)
067: user.setRegTime(new Date());
068: if (user.getLastTime() == null)
069: user.setLastTime(user.getRegTime());
070: ssn.save(user);
071: return user;
072: }
073:
074: /**
075: * 通过用户名来获取用户资料,用于注册用户是判断是否重名
076: * @param ssn
077: * @param site
078: * @param nickname
079: * @return
080: * @throws HibernateException
081: */
082: public static UserForm getUserByName(Session ssn, SiteForm site,
083: String nickname) throws HibernateException {
084: UserForm u = null;
085: Criteria crit = ssn.createCriteria(UserForm.class);
086: if (site != null)
087: crit = crit.add(Expression.eq("site.id", new Integer(site
088: .getId())));
089: crit = crit.add(Expression.eq("displayName", nickname));
090: List users = crit.list();
091: if (users.size() > 0)
092: return (UserForm) users.get(0);
093:
094: return null;
095: }
096:
097: /**
098: * 读取用户资料
099: * @param ssn
100: * @param site
101: * @param loginName
102: * @return
103: * @throws HibernateException
104: */
105: public static UserForm getUser(Session ssn, SiteForm site,
106: String loginName) throws HibernateException {
107: UserForm u = null;
108: Criteria crit = ssn.createCriteria(UserForm.class);
109: if (site != null)
110: crit = crit.add(Expression.eq("site.id", new Integer(site
111: .getId())));
112: crit = crit.add(Expression.eq("loginName", loginName));
113: List users = crit.list();
114: if (users.size() > 0)
115: return (UserForm) users.get(0);
116:
117: return null;
118: }
119:
120: /**
121: * 子网站注册用户信息查询(该方法用于members.jsp用于显示最近N个注册用户)
122: * @param ssn
123: * @param site
124: * @param count
125: * @return
126: * @throws SQLException
127: */
128: public static List listUsers(Session ssn, SiteForm site, int from,
129: int count, String username) throws HibernateException {
130: Criteria crit = ssn.createCriteria(UserForm.class);
131: crit = crit.add(Expression.eq("site.id", new Integer(site
132: .getId())));
133: if (username != null)
134: crit = crit.add(Expression.like("displayName",
135: '%' + username + '%'));
136: crit = crit.addOrder(Order.desc("regTime"));
137: if (from > 0)
138: crit = crit.setFirstResult(from);
139: if (count > 0)
140: crit = crit.setMaxResults(count);
141: return crit.list();
142: }
143:
144: /**
145: * 子网站注册用户信息查询(该方法用于members.jsp用于显示最近N个注册用户)
146: * @param ssn
147: * @param site
148: * @param count
149: * @return
150: * @throws SQLException
151: */
152: public static List listUsers(Session ssn, SiteForm site, int count,
153: String username) throws HibernateException {
154: return listUsers(ssn, site, 0, count, username);
155: }
156:
157: /**
158: * 获取指定站点某个类型用户的数量(该方法用于日记后台用户维护user_list.jsp)
159: * @param ssn
160: * @param site
161: * @param role
162: * @return
163: * @throws HibernateException
164: */
165: public static int getUserCount(Session ssn, SiteForm site,
166: int role, String username) throws HibernateException {
167: Query query = null;
168: String hsql = " SELECT COUNT(user.id) FROM "
169: + UserForm.class.getName()
170: + " AS user WHERE user.site.id=?";
171: if (role != -2)
172: hsql += " AND user.userRole=?";
173: if (username != null)
174: hsql += " AND user.displayName LIKE ?";
175:
176: query = ssn.createQuery(hsql);
177: query.setInteger(0, site.getId());
178: if (role != -2) {
179: query.setInteger(1, role);
180: if (username != null)
181: query.setString(2, '%' + username + '%');
182: } else if (username != null)
183: query.setString(1, '%' + username + '%');
184: List res = query.list();
185: int uc = (res.size() > 0) ? ((Integer) res.get(0)).intValue()
186: : 0;
187: return uc;
188: }
189:
190: /**
191: * 获取某个用户的详细资料信息
192: * @param ssn
193: * @param userid
194: * @param withDetails
195: * @return
196: * @throws HibernateException
197: */
198: public static UserForm getUser(Session ssn, int userid,
199: boolean withDetails) throws HibernateException {
200: UserForm user = (UserForm) ssn.load(UserForm.class,
201: new Integer(userid));
202: fillUserWithLogAndReplyCount(ssn, user, withDetails);
203: return user;
204: }
205:
206: /**
207: * 填充用户的日记和评论信息数
208: * @param ssn
209: * @param user
210: * @param withLogsAndReplyies
211: * @throws HibernateException
212: */
213: public static void fillUserWithLogAndReplyCount(Session ssn,
214: UserForm user, boolean withLogsAndReplyies)
215: throws HibernateException {
216: if (user != null) {
217: //由于logs与replies属性设置了lazy=true所以必须手工去读取该信息
218: //读取日志数
219: Query query = ssn.createQuery("SELECT COUNT(log.id) FROM "
220: + LogForm.class.getName()
221: + " AS log WHERE log.owner.id=? AND log.status<>?");
222: query.setInteger(0, user.getId());
223: query.setInteger(1, LogForm.STATUS_DELETED);
224: List res = query.list();
225: int logCount = (res.size() > 0) ? ((Integer) res.get(0))
226: .intValue() : 0;
227: user.setLogCount(logCount);
228: //读取评论数
229: Query query2 = ssn
230: .createQuery("SELECT COUNT(reply.id) FROM "
231: + ReplyForm.class.getName()
232: + " AS reply WHERE reply.author.id=? AND reply.log.status<>?");
233: query2.setInteger(0, user.getId());
234: query2.setInteger(1, LogForm.STATUS_DELETED);
235: List res2 = query2.list();
236: int replyCount = (res2.size() > 0) ? ((Integer) res2.get(0))
237: .intValue()
238: : 0;
239: user.setReplyCount(replyCount);
240: //读取书签数
241: Query query3 = ssn.createQuery("SELECT COUNT(bm.id) FROM "
242: + BookMarkBean.class.getName()
243: + " AS bm WHERE bm.user.id=?");
244: query2.setInteger(0, user.getId());
245: res2 = query2.list();
246: int bmCount = (res2.size() > 0) ? ((Integer) res2.get(0))
247: .intValue() : 0;
248: user.setBookMarkCount(bmCount);
249:
250: if (withLogsAndReplyies) {
251: user.setLogs(ssn.createCriteria(LogForm.class).add(
252: Expression.eq("owner.id", new Integer(user
253: .getId()))).addOrder(
254: Order.desc("logTime")).list());
255: user.setLogs(ssn.createCriteria(ReplyForm.class).add(
256: Expression.eq("author.id", new Integer(user
257: .getId()))).addOrder(
258: Order.desc("writeTime")).list());
259: }
260: }
261: }
262:
263: }
|