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.sql.Connection;
020: import java.sql.PreparedStatement;
021: import java.sql.ResultSet;
022: import java.sql.SQLException;
023: import java.sql.Timestamp;
024: import java.util.ArrayList;
025: import java.util.Calendar;
026: import java.util.Iterator;
027: import java.util.List;
028:
029: import net.sf.hibernate.Criteria;
030: import net.sf.hibernate.HibernateException;
031: import net.sf.hibernate.Query;
032: import net.sf.hibernate.Session;
033: import net.sf.hibernate.expression.Expression;
034: import net.sf.hibernate.expression.Order;
035:
036: import org.apache.commons.lang.StringUtils;
037: import org.apache.lucene.queryParser.ParseException;
038:
039: import dlog4j.formbean.CategoryForm;
040: import dlog4j.formbean.DraftForm;
041: import dlog4j.formbean.LogForm;
042: import dlog4j.formbean.SiteForm;
043: import dlog4j.formbean.UserForm;
044: import dlog4j.search.SearchProxy;
045: import dlog4j.security.DlogRole;
046:
047: /**
048: * @author Liudong
049: * 日记管理器
050: */
051: public class LogManager extends ManagerBase {
052:
053: /**
054: * 日记的搜索
055: * @deprecated 该方法由searchAllLogs来代替
056: * @param ssn
057: * @param site
058: * @param loginUser
059: * @param cat_id
060: * @param search
061: * @param from
062: * @param count
063: * @return
064: * @throws HibernateException
065: */
066: public static List searchLogs(Session ssn, SiteForm site,
067: UserForm loginUser, int cat_id, String search,
068: String orderField, int from, int count)
069: throws HibernateException, IOException, ParseException {
070: SearchProxy proxy = SearchProxy.getLogQuery();
071: List ids = proxy.searchFor(site.getId(), cat_id, search, from,
072: count);
073: if (ids.size() == 0)
074: return new ArrayList();
075: //System.out.println("search.count="+ids.size());
076: Criteria crit = ssn.createCriteria(LogForm.class).add(
077: Expression.eq("site.id", new Integer(site.getId())));
078: crit = crit.add(Expression.in("id", ids));
079:
080: if (StringUtils.isEmpty(orderField))
081: orderField = "logTime";
082: crit = crit.addOrder(Order.desc(orderField));
083: List logs = crit.list();
084: //过滤掉没有权限的日记分类
085: Iterator ls = logs.iterator();
086: while (ls.hasNext()) {
087: LogForm log = (LogForm) ls.next();
088: if (loginUser == null || !loginUser.isAdmin()) {
089: if (log.getCategory().getType() == CategoryForm.TYPE_OWNER)
090: ls.remove();
091: }
092: }
093: return logs;
094: }
095:
096: /**
097: * 搜索符合条件的所有日记
098: * @param ssn
099: * @param site
100: * @param loginUser
101: * @param cat_id
102: * @param search
103: * @return
104: * @throws HibernateException
105: * @throws IOException
106: * @throws ParseException
107: */
108: public static List searchAllLogs(Session ssn, SiteForm site,
109: UserForm loginUser, int cat_id, String search,
110: String orderField) throws HibernateException, IOException,
111: ParseException {
112: List logs = new ArrayList();
113: SearchProxy proxy = SearchProxy.getLogQuery();
114: List ids = proxy.searchFor(site.getId(), cat_id, search, 0, -1);
115: if (ids.size() > 0) {
116: Criteria crit = ssn.createCriteria(LogForm.class)
117: .add(
118: Expression.eq("site.id", new Integer(site
119: .getId())));
120: crit = crit.add(Expression.in("id", ids));
121: if (StringUtils.isEmpty(orderField))
122: orderField = "logTime";
123: crit = crit.addOrder(Order.desc(orderField));
124: logs = crit.list();
125: //过滤掉没有权限的日记分类
126: Iterator ls = logs.iterator();
127: while (ls.hasNext()) {
128: LogForm log = (LogForm) ls.next();
129: if (loginUser == null || !loginUser.isAdmin()) {
130: if (log.getCategory().getType() == CategoryForm.TYPE_OWNER) {
131: ls.remove();
132: }
133: }
134: }
135: }
136: if (ids != null)
137: ids.clear();
138: return logs;
139: }
140:
141: /**
142: * 得到查询出的日记总数
143: * @deprecated 该方法已经由searchAllLogs来代替
144: * @param ssn
145: * @param site
146: * @param loginUser
147: * @param cat_id
148: * @param search
149: * @return
150: * @throws HibernateException
151: * @throws IOException
152: * @throws ParseException
153: */
154: public static int getSearchLogCount(Session ssn, SiteForm site,
155: UserForm loginUser, int cat_id, String search)
156: throws HibernateException, IOException, ParseException {
157: SearchProxy proxy = SearchProxy.getLogQuery();
158: List ids = proxy.searchFor(site.getId(), cat_id, search, 0, -1);
159: if (ids.size() == 0)
160: return 0;
161: Criteria crit = ssn.createCriteria(LogForm.class).add(
162: Expression.eq("site.id", new Integer(site.getId())));
163: crit = crit.add(Expression.in("id", ids));
164: List logs = crit.list();
165: //过滤掉没有权限的日记分类
166: Iterator ls = logs.iterator();
167: while (ls.hasNext()) {
168: LogForm log = (LogForm) ls.next();
169: if (loginUser == null || !loginUser.isAdmin()) {
170: if (log.getCategory().getType() == CategoryForm.TYPE_OWNER)
171: ls.remove();
172: }
173: }
174: int lc = logs.size();
175: logs.clear();
176: return lc;
177: }
178:
179: /**
180: * 读取指定的日记信息
181: * @param ssn
182: * @param site
183: * @param loginUser
184: * @param log_id
185: * @return
186: */
187: public static LogForm getLogForm(Session ssn, SiteForm site,
188: UserForm loginUser, int log_id) {
189: LogForm log = null;
190: try {
191: log = (LogForm) ssn
192: .load(LogForm.class, new Integer(log_id));
193: if (log.getStatus() == LogForm.STATUS_DELETED)
194: log = null;
195: else if (log != null
196: && log.getSite().getId() != site.getId())
197: log = null;
198: else if (log != null
199: && (loginUser == null || !loginUser.isAdmin())
200: && log.getCategory().isOwnerOnly())
201: log = null;
202: } catch (HibernateException e) {
203: }
204: return log;
205: }
206:
207: /**
208: * 统计出指定月份每天的日记篇数
209: * 使用SQL查询方式避免加载日记内容以提供查询速度
210: * @param ssn
211: * @param site
212: * @param year
213: * @param month (1-12)
214: * @return
215: * @throws HibernateException
216: */
217: public static int[] statLogs(Session ssn, SiteForm site,
218: UserForm loginUser, int year, int month)
219: throws HibernateException {
220: Calendar cal = Calendar.getInstance();
221: cal.set(Calendar.YEAR, year);
222: cal.set(Calendar.MONTH, month - 1);
223: return statLogs(ssn, site, loginUser, cal);
224: }
225:
226: /**
227: * 统计指定月份每天的日记数(使用Hibernate查询方式)
228: * 使用SQL查询方式避免加载日记内容以提供查询速度
229: * @param ssn
230: * @param site
231: * @param loginUser
232: * @param month
233: * @return
234: * @throws HibernateException
235: */
236: public static int[] statLogs(Session ssn, SiteForm site,
237: UserForm loginUser, Calendar month)
238: throws HibernateException {
239:
240: Calendar firstDate = (Calendar) month.clone();
241: firstDate.set(Calendar.DATE, 1);
242: resetCalendar(firstDate);
243: Calendar nextMonthFirstDate = (Calendar) firstDate.clone();
244: nextMonthFirstDate.add(Calendar.MONTH, 1);
245:
246: //计算指定月份有多少天
247: Calendar tempCal = (Calendar) nextMonthFirstDate.clone();
248: tempCal.add(Calendar.DATE, -1);
249: int dateCount = tempCal.get(Calendar.DATE);
250:
251: int[] logCounts = new int[dateCount];
252:
253: //查询出当月的所有日记进行统计
254:
255: StringBuffer hql = new StringBuffer("SELECT log.logTime FROM ");
256: hql.append(LogForm.class.getName());
257: hql
258: .append(" AS log WHERE log.logTime>=? AND log.logTime<? AND log.status=?");
259: Query q = ssn.createQuery(hql.toString());
260: q.setTimestamp(0, firstDate.getTime());
261: q.setTimestamp(1, nextMonthFirstDate.getTime());
262: q.setInteger(2, LogForm.STATUS_NORMAL);
263:
264: Iterator logs = q.list().iterator();
265: while (logs.hasNext()) {
266: tempCal.setTime((Timestamp) logs.next());
267: int date = tempCal.get(Calendar.DATE) - 1;
268: logCounts[date]++;
269: }
270:
271: return logCounts;
272: }
273:
274: /**
275: * 统计指定月份每天的日记数(使用SQL查询方式)
276: * @deprecated 使用Hibernate的HQL来查询
277: * @param ssn
278: * @param site
279: * @param loginUser
280: * @param month
281: * @return
282: * @throws HibernateException
283: */
284: public static int[] statLogs(Connection conn, SiteForm site,
285: UserForm loginUser, Calendar month) throws SQLException {
286:
287: Calendar firstDate = (Calendar) month.clone();
288: firstDate.set(Calendar.DATE, 1);
289: resetCalendar(firstDate);
290: Calendar nextMonthFirstDate = (Calendar) firstDate.clone();
291: nextMonthFirstDate.add(Calendar.MONTH, 1);
292:
293: //计算指定月份有多少天
294: Calendar tempCal = (Calendar) nextMonthFirstDate.clone();
295: tempCal.add(Calendar.DATE, -1);
296: int dateCount = tempCal.get(Calendar.DATE);
297:
298: int[] logCounts = new int[dateCount];
299:
300: //查询出当月的所有日记进行统计
301:
302: String sql = "SELECT logTime FROM dlog_journal WHERE logTime>=? AND logTime<? AND status=?";
303:
304: PreparedStatement ps = null;
305: ResultSet rs = null;
306:
307: try {
308: ps = conn.prepareStatement(sql);
309: ps.setTimestamp(1, new Timestamp(firstDate.getTime()
310: .getTime()));
311: ps.setTimestamp(2, new Timestamp(nextMonthFirstDate
312: .getTime().getTime()));
313: ps.setInt(3, LogForm.STATUS_NORMAL);
314: if (ps.execute()) {
315: rs = ps.getResultSet();
316: while (rs.next()) {
317: tempCal.setTime(rs.getDate(1));
318: int date = tempCal.get(Calendar.DATE) - 1;
319: logCounts[date]++;
320: }
321: }
322: } finally {
323: close(rs, ps, null);
324: }
325:
326: return logCounts;
327: }
328:
329: /**
330: * 清除日历的时间字段
331: * @param cal
332: */
333: protected static void resetCalendar(Calendar cal) {
334: cal.set(Calendar.HOUR_OF_DAY, 0);
335: cal.set(Calendar.MINUTE, 0);
336: cal.set(Calendar.SECOND, 0);
337: cal.set(Calendar.MILLISECOND, 0);
338: }
339:
340: /**
341: * 根据参数构建一个日历对象实例
342: * @param year
343: * @param month 1-12
344: * @param date
345: * @param clearTime 是否清除时间字段
346: * @return
347: */
348: protected static Calendar buildCalendar(int year, int month,
349: int date, boolean clearTime) {
350: Calendar cal = Calendar.getInstance();
351: if (clearTime)
352: resetCalendar(cal);
353: if (year != -1)
354: cal.set(Calendar.YEAR, year);
355: if (month != -1)
356: cal.set(Calendar.MONTH, month - 1);
357: if (date != -1)
358: cal.set(Calendar.DATE, date);
359: return cal;
360: }
361:
362: /**
363: * 读取草稿的详细信息
364: * @param ssn
365: * @param site
366: * @param draft_id
367: * @return
368: * @throws HibernateException
369: */
370: public static DraftForm getDraft(Session ssn, SiteForm site,
371: int draft_id) throws HibernateException {
372: Criteria crit = ssn.createCriteria(DraftForm.class);
373: crit = crit.add(Expression.eq("site.id", new Integer(site
374: .getId())));
375: crit = crit.add(Expression.eq("id", new Integer(draft_id)));
376: DraftForm draft = null;
377: List drafts = crit.list();
378: if (drafts.size() > 0)
379: draft = (DraftForm) drafts.get(0);
380: return draft;
381:
382: }
383:
384: /**
385: * 列出某个网站的所有草稿信息
386: * @param ssn
387: * @param site
388: * @return
389: * @throws HibernateException
390: */
391: public static List listDrafts(Session ssn, SiteForm site,
392: UserForm loginUser) throws HibernateException {
393: if (loginUser == null
394: || (!loginUser.isAdmin() && !loginUser.isFriend()))
395: return new ArrayList();
396: Criteria crit = ssn.createCriteria(DraftForm.class);
397: crit = crit.add(Expression.eq("site.id", new Integer(site
398: .getId())));
399: //if(loginUser.isFriend())
400: crit = crit.add(Expression.eq("owner.id", new Integer(loginUser
401: .getId())));
402: crit = crit.addOrder(Order.desc("logTime"));
403: return crit.list();
404: }
405:
406: /**
407: * 列出某个分类下的日志
408: * @param ssn
409: * @param site
410: * @param cat_id
411: * @param orderField
412: * @return
413: * @throws HibernateException
414: */
415: public static List listLogs(Session ssn, SiteForm site,
416: UserForm loginUser, int cat_id, int userid, int from,
417: int count, String orderField, int year, int month, int date)
418: throws HibernateException {
419: String hql = "FROM " + LogForm.class.getName()
420: + " AS log WHERE log.site.id=? AND log.status=?";
421: if (cat_id != -1)
422: hql += " AND log.category.id=?";
423: else
424: hql += " AND log.category.id<>?";
425: if (userid > 0)
426: hql += " AND log.owner.id=" + userid;
427:
428: if (loginUser != null
429: && loginUser.getUserRole() == DlogRole.ROLE_BUDDY) {
430: int[] cats = loginUser.getOwnerCatids();
431: if (cats.length == 0)
432: hql += " AND log.category.type<>"
433: + CategoryForm.TYPE_OWNER;
434: else {
435: hql += " AND (log.category.type<>"
436: + CategoryForm.TYPE_OWNER;
437: for (int i = 0; i < cats.length; i++) {
438: hql += " OR log.category.id=" + cats[i];
439: }
440: hql += ")";
441: }
442: } else if (loginUser == null || !loginUser.isAdmin())
443: hql += " AND log.category.type<>" + CategoryForm.TYPE_OWNER;
444:
445: Calendar begin = null;
446: Calendar end = null;
447: boolean hasTime = false;
448: if (year != -1 && month != -1 && date != -1) {//查询某天
449: begin = buildCalendar(year, month, date, true);
450: end = (Calendar) begin.clone();
451: end.add(Calendar.DATE, 1);
452: hql += " AND log.logTime>=? AND log.logTime<?";
453: hasTime = true;
454: } else if (year != -1 && month != -1) {//查询某月
455: begin = buildCalendar(year, month, 1, true);
456: end = (Calendar) begin.clone();
457: end.add(Calendar.MONTH, 1);
458: hql += " AND log.logTime>=? AND log.logTime<?";
459: hasTime = true;
460: } else if (year != -1) {//查询某年
461: begin = buildCalendar(year, 1, 1, true);
462: end = (Calendar) begin.clone();
463: end.add(Calendar.YEAR, 1);
464: hql += " AND log.logTime>=? AND log.logTime<?";
465: hasTime = true;
466: }
467: if (StringUtils.isEmpty(orderField))
468: orderField = "logTime";
469: hql += " ORDER BY log." + orderField + " DESC";
470: Query query = ssn.createQuery(hql);
471: query.setInteger(0, site.getId());
472: query.setInteger(1, LogForm.STATUS_NORMAL);
473: query.setInteger(2, cat_id);
474: if (hasTime) {
475: query.setCalendar(3, begin);
476: query.setCalendar(4, end);
477: }
478: query.setFirstResult(from);
479: query.setMaxResults(count);
480: return query.list();
481:
482: }
483:
484: /**
485: * 获取某个日记分类下的日记数
486: * @param ssn
487: * @param cat_id
488: * @return
489: * @throws HibernateException
490: */
491: public static int getLogCount(Session ssn, int cat_id)
492: throws HibernateException {
493: Query q2 = ssn.createQuery("SELECT COUNT(*) FROM "
494: + LogForm.class.getName()
495: + " AS log WHERE log.category.id=?");
496: q2.setInteger(0, cat_id);
497: List res = q2.list();
498: return (res.size() > 0) ? ((Integer) res.get(0)).intValue() : 0;
499: }
500:
501: /**
502: * 获取日记总数
503: * @param ssn
504: * @param site
505: * @param cat_id 指定某个日记分类
506: * @return
507: */
508: public static int getLogCount(Session ssn, SiteForm site,
509: UserForm loginUser, int cat_id, int userid, int year,
510: int month, int date) throws HibernateException {
511: String hql = "SELECT COUNT(*) FROM " + LogForm.class.getName()
512: + " AS log WHERE log.site.id=? AND log.status=?";
513: if (cat_id != -1)
514: hql += " AND log.category.id=?";
515: else
516: hql += " AND log.category.id<>?";
517: if (userid > 0)
518: hql += " AND log.owner.id=" + userid;
519:
520: if (loginUser != null
521: && loginUser.getUserRole() == DlogRole.ROLE_BUDDY) {
522: int[] cats = loginUser.getOwnerCatids();
523: if (cats.length == 0)
524: hql += " AND log.category.type<>"
525: + CategoryForm.TYPE_OWNER;
526: else {
527: hql += " AND (log.category.type<>"
528: + CategoryForm.TYPE_OWNER;
529: for (int i = 0; i < cats.length; i++) {
530: hql += " OR log.category.id=" + cats[i];
531: }
532: hql += ")";
533: }
534: } else if (loginUser == null || !loginUser.isAdmin())
535: hql += " AND log.category.type<>" + CategoryForm.TYPE_OWNER;
536:
537: Calendar begin = null;
538: Calendar end = null;
539: boolean hasTime = false;
540: if (year != -1 && month != -1 && date != -1) {//查询某天
541: begin = buildCalendar(year, month, date, true);
542: end = (Calendar) begin.clone();
543: end.add(Calendar.DATE, 1);
544: hql += " AND log.logTime>=? AND log.logTime<?";
545: hasTime = true;
546: } else if (year != -1 && month != -1) {//查询某月
547: begin = buildCalendar(year, month, 1, true);
548: end = (Calendar) begin.clone();
549: end.add(Calendar.MONTH, 1);
550: hql += " AND log.logTime>=? AND log.logTime<?";
551: hasTime = true;
552: } else if (year != -1) {//查询某年
553: begin = buildCalendar(year, 1, 1, true);
554: end = (Calendar) begin.clone();
555: end.add(Calendar.YEAR, 1);
556: hql += " AND log.logTime>=? AND log.logTime<?";
557: hasTime = true;
558: }
559: Query query = ssn.createQuery(hql);
560: query.setInteger(0, site.getId());
561: query.setInteger(1, LogForm.STATUS_NORMAL);
562: query.setInteger(2, cat_id);
563: if (hasTime) {
564: query.setCalendar(3, begin);
565: query.setCalendar(4, end);
566: }
567: List res = query.list();
568: int logcount = (res.size() > 0) ? ((Integer) res.get(0))
569: .intValue() : 0;
570: return logcount;
571: }
572: }
|