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.io.IOException;
019: import java.util.ArrayList;
020: import java.util.Calendar;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import net.sf.hibernate.Criteria;
025: import net.sf.hibernate.HibernateException;
026: import net.sf.hibernate.Query;
027: import net.sf.hibernate.Session;
028: import net.sf.hibernate.expression.Expression;
029: import net.sf.hibernate.expression.Order;
030:
031: import org.apache.commons.lang.StringUtils;
032: import org.apache.lucene.queryParser.ParseException;
033:
034: import dlog4j.formbean.CategoryForm;
035: import dlog4j.formbean.LogForm;
036: import dlog4j.formbean.ReplyForm;
037: import dlog4j.formbean.SiteForm;
038: import dlog4j.formbean.UserForm;
039: import dlog4j.search.SearchProxy;
040:
041: /**
042: * @author Liudong 评论管理
043: */
044: public class ReplyManager {
045:
046: public final static String FIELD_WRITETIME = "writeTime";
047:
048: /**
049: * 读取指定的评论信息
050: * @param ssn
051: * @param site
052: * @param loginUser
053: * @param log_id
054: * @return
055: */
056: public static ReplyForm getLogForm(Session ssn, SiteForm site,
057: UserForm loginUser, int log_id) {
058: ReplyForm reply = null;
059: try {
060: reply = (ReplyForm) ssn.load(ReplyForm.class, new Integer(
061: log_id));
062: if (reply != null
063: && reply.getSite().getId() != site.getId())
064: reply = null;
065: if (reply != null)
066: reply = null;
067: } catch (HibernateException e) {
068: }
069: return reply;
070: }
071:
072: /**
073: * 统计出指定月份每天的评论篇数
074: * @param ssn
075: * @param site
076: * @param year
077: * @param month (1-12)
078: * @return
079: * @throws HibernateException
080: */
081: public static int[] statReplies(Session ssn, SiteForm site,
082: UserForm loginUser, int year, int month)
083: throws HibernateException {
084: Calendar cal = Calendar.getInstance();
085: cal.set(Calendar.YEAR, year);
086: cal.set(Calendar.MONTH, month - 1);
087: return statReplies(ssn, site, loginUser, cal);
088: }
089:
090: public static int[] statReplies(Session ssn, SiteForm site,
091: UserForm loginUser, Calendar month)
092: throws HibernateException {
093:
094: Calendar firstDate = (Calendar) month.clone();
095: firstDate.set(Calendar.DATE, 1);
096: resetCalendar(firstDate);
097: Calendar nextMonthFirstDate = (Calendar) firstDate.clone();
098: nextMonthFirstDate.add(Calendar.MONTH, 1);
099:
100: //计算指定月份有多少天
101: Calendar tempCal = (Calendar) nextMonthFirstDate.clone();
102: tempCal.add(Calendar.DATE, -1);
103: int dateCount = tempCal.get(Calendar.DATE);
104:
105: int[] replyCounts = new int[dateCount];
106:
107: //查询出当月的所有日记进行统计
108:
109: Criteria crit = ssn.createCriteria(ReplyForm.class).add(
110: Expression.eq("site.id", new Integer(site.getId())));
111: crit = crit.addOrder(Order.asc(FIELD_WRITETIME));
112: crit = crit.add(Expression.ge(FIELD_WRITETIME, firstDate
113: .getTime()));
114: crit = crit.add(Expression.lt(FIELD_WRITETIME,
115: nextMonthFirstDate.getTime()));
116:
117: Iterator replyies = crit.list().iterator();
118: while (replyies.hasNext()) {
119: ReplyForm reply = (ReplyForm) replyies.next();
120: if (loginUser == null)
121: continue;
122: tempCal.setTime(reply.getWriteTime());
123: int date = tempCal.get(Calendar.DATE) - 1;
124: replyCounts[date]++;
125: }
126:
127: return replyCounts;
128: }
129:
130: /**
131: * 清除日历的时间字段
132: * @param cal
133: */
134: protected static void resetCalendar(Calendar cal) {
135: cal.set(Calendar.HOUR_OF_DAY, 0);
136: cal.set(Calendar.MINUTE, 0);
137: cal.set(Calendar.SECOND, 0);
138: cal.set(Calendar.MILLISECOND, 0);
139: }
140:
141: /**
142: * 根据参数构建一个日历对象实例
143: * @param year
144: * @param month 1-12
145: * @param date
146: * @param clearTime 是否清除时间字段
147: * @return
148: */
149: protected static Calendar buildCalendar(int year, int month,
150: int date, boolean clearTime) {
151: Calendar cal = Calendar.getInstance();
152: if (clearTime)
153: resetCalendar(cal);
154: if (year != -1)
155: cal.set(Calendar.YEAR, year);
156: if (month != -1)
157: cal.set(Calendar.MONTH, month - 1);
158: if (date != -1)
159: cal.set(Calendar.DATE, date);
160: return cal;
161: }
162:
163: /**
164: * 搜索符合条件的所有评论
165: * @param ssn
166: * @param site
167: * @param loginUser
168: * @param cat_id
169: * @param search
170: * @return
171: * @throws HibernateException
172: * @throws IOException
173: * @throws ParseException
174: */
175: public static List searchAllReplies(Session ssn, SiteForm site,
176: UserForm loginUser, int cat_id, String search,
177: String orderField) throws HibernateException, IOException,
178: ParseException {
179: List logs = new ArrayList();
180: SearchProxy proxy = SearchProxy.getReplyQuery();
181: List ids = proxy.searchFor(site.getId(), cat_id, search, 0, -1);
182: if (ids.size() > 0) {
183: Criteria crit = ssn.createCriteria(ReplyForm.class)
184: .add(
185: Expression.eq("site.id", new Integer(site
186: .getId())));
187: crit = crit.add(Expression.in("id", ids));
188: if (StringUtils.isEmpty(orderField))
189: orderField = "writeTime";
190: crit = crit.addOrder(Order.desc(orderField));
191: logs = crit.list();
192: //过滤掉没有权限的日记分类
193: Iterator ls = logs.iterator();
194: while (ls.hasNext()) {
195: ReplyForm reply = (ReplyForm) ls.next();
196: if (reply.getLog().getStatus() != LogForm.STATUS_NORMAL)
197: ls.remove();
198: if (loginUser == null || !loginUser.isAdmin()) {
199: if (reply.getLog().getCategory().getType() == CategoryForm.TYPE_OWNER) {
200: ls.remove();
201: }
202: }
203: }
204: }
205: if (ids != null)
206: ids.clear();
207: return logs;
208: }
209:
210: /**
211: * 获取查询到的评论信息的总数,用于分页
212: * @deprecated 由searchAllReplies替换
213: * @param ssn
214: * @param site
215: * @param loginUser
216: * @param cat_id
217: * @param userid
218: * @param search
219: * @return
220: * @throws IOException
221: * @throws ParseException
222: * @throws HibernateException
223: */
224: public static int getSearchReplyCount(Session ssn, SiteForm site,
225: UserForm loginUser, int cat_id, int userid, String search)
226: throws IOException, ParseException, HibernateException {
227: SearchProxy proxy = SearchProxy.getReplyQuery();
228: List ids = proxy.searchFor(site.getId(), cat_id, search, 0, -1);
229: if (ids.size() == 0)
230: return 0;
231: Criteria crit = ssn.createCriteria(ReplyForm.class).add(
232: Expression.eq("site.id", new Integer(site.getId())));
233: crit = crit.add(Expression.in("id", ids));
234: List replies = crit.list();
235: //过滤掉没有权限的日记分类
236: Iterator ls = replies.iterator();
237: while (ls.hasNext()) {
238: ReplyForm reply = (ReplyForm) ls.next();
239: if (loginUser == null || !loginUser.isAdmin()) {
240: if (reply.getLog().getCategory().getType() == CategoryForm.TYPE_OWNER)
241: ls.remove();
242: }
243: }
244: int rc = replies.size();
245: replies.clear();
246: return rc;
247: }
248:
249: /**
250: * 搜索评论信息
251: * @deprecated 由searchAllReplies替换
252: * @param ssn
253: * @param site
254: * @param loginUser
255: * @param cat_id
256: * @param userid
257: * @param search
258: * @param orderField
259: * @param from
260: * @param count
261: * @return
262: * @throws IOException
263: * @throws ParseException
264: * @throws HibernateException
265: */
266: public static List searchReplies(Session ssn, SiteForm site,
267: UserForm loginUser, int cat_id, int userid, String search,
268: String orderField, int from, int count) throws IOException,
269: ParseException, HibernateException {
270: SearchProxy proxy = SearchProxy.getReplyQuery();
271: List ids = proxy.searchFor(site.getId(), cat_id, search, from,
272: count);
273: if (ids.size() == 0)
274: return new ArrayList();
275: Criteria crit = ssn.createCriteria(ReplyForm.class).add(
276: Expression.eq("site.id", new Integer(site.getId())));
277: crit = crit.add(Expression.in("id", ids));
278: if (StringUtils.isEmpty(orderField))
279: orderField = FIELD_WRITETIME;
280: crit = crit.addOrder(Order.desc(orderField));
281: /*
282: if(from>=0)
283: crit.setFirstResult(from);
284: if(count>=0)
285: crit.setMaxResults(count);
286: */
287: List replies = crit.list();
288: //过滤掉没有权限的日记分类
289: Iterator ls = replies.iterator();
290: while (ls.hasNext()) {
291: ReplyForm reply = (ReplyForm) ls.next();
292: if (loginUser == null || !loginUser.isAdmin()) {
293: if (reply.getLog().getCategory().getType() == CategoryForm.TYPE_OWNER)
294: ls.remove();
295: }
296: }
297: return replies;
298: }
299:
300: /**
301: * 列出某个分类下的评论
302: * @param ssn
303: * @param site
304: * @param cat_id
305: * @param orderField
306: * @return
307: * @throws HibernateException
308: */
309: public static List listReplies(Session ssn, SiteForm site,
310: UserForm loginUser, int cat_id, int userid, int from,
311: int count, String orderField, int year, int month, int date)
312: throws HibernateException {
313: String hql = "FROM " + ReplyForm.class.getName()
314: + " AS reply WHERE reply.site.id=? ";
315: if (cat_id != -1)
316: hql += " AND reply.log.category.id=?";
317: else
318: hql += " AND reply.log.category.id<>?";
319: if (userid > 0)
320: hql += " AND reply.author.id=" + userid;
321: if (loginUser == null || !loginUser.isAdmin())
322: hql += " AND reply.log.category.type<>"
323: + CategoryForm.TYPE_OWNER;
324:
325: hql += " AND reply.log.status=?";
326:
327: Calendar begin = null;
328: Calendar end = null;
329: boolean hasTime = false;
330: if (year != -1 && month != -1 && date != -1) {//查询某天
331: begin = buildCalendar(year, month, date, true);
332: end = (Calendar) begin.clone();
333: end.add(Calendar.DATE, 1);
334: hql += " AND reply.writeTime>=? AND reply.writeTime<?";
335: hasTime = true;
336: } else if (year != -1 && month != -1) {//查询某月
337: begin = buildCalendar(year, month, 1, true);
338: end = (Calendar) begin.clone();
339: end.add(Calendar.MONTH, 1);
340: hql += " AND reply.writeTime>=? AND reply.writeTime<?";
341: hasTime = true;
342: } else if (year != -1) {//查询某年
343: begin = buildCalendar(year, 1, 1, true);
344: end = (Calendar) begin.clone();
345: end.add(Calendar.YEAR, 1);
346: hql += " AND reply.writeTime>=? AND reply.writeTime<?";
347: hasTime = true;
348: }
349: if (StringUtils.isEmpty(orderField))
350: orderField = FIELD_WRITETIME;
351: hql += " ORDER BY reply." + orderField + " DESC";
352: Query query = ssn.createQuery(hql);
353: query.setInteger(0, site.getId());
354: query.setInteger(1, cat_id);
355: query.setInteger(2, LogForm.STATUS_NORMAL);
356: if (hasTime) {
357: query.setCalendar(3, begin);
358: query.setCalendar(4, end);
359: }
360: query.setFirstResult(from);
361: if (count > 0) {
362: query.setMaxResults(count);
363: }
364: return query.list();
365:
366: }
367:
368: /**
369: * 列出最新的评论信息
370: *
371: * @param ssn
372: * @param site
373: * @param count
374: * @return @throws
375: * SQLException
376: * @throws HibernateException
377: */
378: public static List listReplies(Session ssn, SiteForm site,
379: UserForm loginUser, int count) throws HibernateException {
380: String hql = "FROM " + ReplyForm.class.getName()
381: + " AS r WHERE r.site.id=? AND r.log.status=?";
382: if (loginUser == null || !loginUser.isAdmin())
383: hql += " AND r.log.category.type<>?";
384: hql += " ORDER BY r.writeTime DESC";
385: Query query = ssn.createQuery(hql);
386: query.setInteger(0, site.getId());
387: query.setInteger(1, LogForm.STATUS_NORMAL);
388: if (loginUser == null || !loginUser.isAdmin())
389: query.setInteger(2, CategoryForm.TYPE_OWNER);
390: if (count > 0)
391: query.setMaxResults(count);
392: return query.list();
393: }
394:
395: /**
396: * 获取日记总数
397: * @param ssn
398: * @param site
399: * @param cat_id 指定某个日记分类
400: * @return
401: */
402: public static int getRepliesCount(Session ssn, SiteForm site,
403: UserForm loginUser, int cat_id, int userid, int year,
404: int month, int date) throws HibernateException {
405: String hql = "SELECT COUNT(*) FROM "
406: + ReplyForm.class.getName()
407: + " AS reply WHERE reply.site.id=? ";
408: if (cat_id != -1)
409: hql += " AND reply.log.category.id=?";
410: else
411: hql += " AND reply.log.category.id<>?";
412: if (userid > 0)
413: hql += " AND reply.author.id=" + userid;
414: if (loginUser == null || !loginUser.isAdmin())
415: hql += " AND reply.log.category.type<>"
416: + CategoryForm.TYPE_OWNER;
417:
418: hql += " AND reply.log.status=?";
419:
420: Calendar begin = null;
421: Calendar end = null;
422: boolean hasTime = false;
423: if (year != -1 && month != -1 && date != -1) {//查询某天
424: begin = buildCalendar(year, month, date, true);
425: end = (Calendar) begin.clone();
426: end.add(Calendar.DATE, 1);
427: hql += " AND reply.writeTime>=? AND reply.writeTime<?";
428: hasTime = true;
429: } else if (year != -1 && month != -1) {//查询某月
430: begin = buildCalendar(year, month, 1, true);
431: end = (Calendar) begin.clone();
432: end.add(Calendar.MONTH, 1);
433: hql += " AND reply.writeTime>=? AND reply.writeTime<?";
434: hasTime = true;
435: } else if (year != -1) {//查询某年
436: begin = buildCalendar(year, 1, 1, true);
437: end = (Calendar) begin.clone();
438: end.add(Calendar.YEAR, 1);
439: hql += " AND reply.writeTime>=? AND reply.writeTime<?";
440: hasTime = true;
441: }
442: Query query = ssn.createQuery(hql);
443: query.setInteger(0, site.getId());
444: query.setInteger(1, cat_id);
445: query.setInteger(2, LogForm.STATUS_NORMAL);
446: if (hasTime) {
447: query.setCalendar(3, begin);
448: query.setCalendar(4, end);
449: }
450: List res = query.list();
451: int Replycount = (res.size() > 0) ? ((Integer) res.get(0))
452: .intValue() : 0;
453: return Replycount;
454: }
455: }
|