KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > LogManager


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.io.IOException JavaDoc;
19 import java.sql.Connection JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.sql.Timestamp JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Calendar JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import net.sf.hibernate.Criteria;
30 import net.sf.hibernate.HibernateException;
31 import net.sf.hibernate.Query;
32 import net.sf.hibernate.Session;
33 import net.sf.hibernate.expression.Expression;
34 import net.sf.hibernate.expression.Order;
35
36 import org.apache.commons.lang.StringUtils;
37 import org.apache.lucene.queryParser.ParseException;
38
39 import dlog4j.formbean.CategoryForm;
40 import dlog4j.formbean.DraftForm;
41 import dlog4j.formbean.LogForm;
42 import dlog4j.formbean.SiteForm;
43 import dlog4j.formbean.UserForm;
44 import dlog4j.search.SearchProxy;
45 import dlog4j.security.DlogRole;
46
47 /**
48  * @author Liudong
49  * 日记管理器
50  */

51 public class LogManager extends ManagerBase{
52
53     /**
54      * 日记的搜索
55      * @deprecated 该方法由searchAllLogs来代替
56      * @param ssn
57      * @param site
58      * @param loginUser
59      * @param cat_id
60      * @param search
61      * @param from
62      * @param count
63      * @return
64      * @throws HibernateException
65      */

66     public static List JavaDoc searchLogs(Session ssn, SiteForm site, UserForm loginUser, int cat_id, String JavaDoc search, String JavaDoc orderField, int from, int count) throws HibernateException, IOException JavaDoc, ParseException
67     {
68         SearchProxy proxy = SearchProxy.getLogQuery();
69         List JavaDoc ids = proxy.searchFor(site.getId(),cat_id,search,from,count);
70         if(ids.size()==0)
71             return new ArrayList JavaDoc();
72         //System.out.println("search.count="+ids.size());
73
Criteria crit = ssn.createCriteria(LogForm.class).add(Expression.eq("site.id", new Integer JavaDoc(site.getId())));
74         crit = crit.add(Expression.in("id",ids));
75         
76         if(StringUtils.isEmpty(orderField))
77             orderField = "logTime";
78         crit = crit.addOrder(Order.desc(orderField));
79         List JavaDoc logs = crit.list();
80         //过滤掉没有权限的日记分类
81
Iterator JavaDoc ls = logs.iterator();
82         while(ls.hasNext()) {
83             LogForm log = (LogForm)ls.next();
84             if(loginUser==null||!loginUser.isAdmin()) {
85                 if(log.getCategory().getType()==CategoryForm.TYPE_OWNER)
86                     ls.remove();
87             }
88         }
89         return logs;
90     }
91     /**
92      * 搜索符合条件的所有日记
93      * @param ssn
94      * @param site
95      * @param loginUser
96      * @param cat_id
97      * @param search
98      * @return
99      * @throws HibernateException
100      * @throws IOException
101      * @throws ParseException
102      */

103     public static List JavaDoc searchAllLogs(Session ssn, SiteForm site, UserForm loginUser, int cat_id, String JavaDoc search, String JavaDoc orderField) throws HibernateException, IOException JavaDoc, ParseException
104     {
105         List JavaDoc logs = new ArrayList JavaDoc();
106         SearchProxy proxy = SearchProxy.getLogQuery();
107         List JavaDoc ids = proxy.searchFor(site.getId(),cat_id,search,0,-1);
108         if(ids.size()>0) {
109             Criteria crit = ssn.createCriteria(LogForm.class).add(Expression.eq("site.id", new Integer JavaDoc(site.getId())));
110             crit = crit.add(Expression.in("id",ids));
111             if(StringUtils.isEmpty(orderField))
112                 orderField = "logTime";
113             crit = crit.addOrder(Order.desc(orderField));
114             logs = crit.list();
115             //过滤掉没有权限的日记分类
116
Iterator JavaDoc ls = logs.iterator();
117             while(ls.hasNext()) {
118                 LogForm log = (LogForm)ls.next();
119                 if(loginUser==null||!loginUser.isAdmin()) {
120                     if(log.getCategory().getType()==CategoryForm.TYPE_OWNER) {
121                         ls.remove();
122                     }
123                 }
124             }
125         }
126         if(ids!=null)
127             ids.clear();
128         return logs;
129     }
130     /**
131      * 得到查询出的日记总数
132      * @deprecated 该方法已经由searchAllLogs来代替
133      * @param ssn
134      * @param site
135      * @param loginUser
136      * @param cat_id
137      * @param search
138      * @return
139      * @throws HibernateException
140      * @throws IOException
141      * @throws ParseException
142      */

143     public static int getSearchLogCount(Session ssn, SiteForm site, UserForm loginUser, int cat_id, String JavaDoc search) throws HibernateException, IOException JavaDoc, ParseException
144     {
145         SearchProxy proxy = SearchProxy.getLogQuery();
146         List JavaDoc ids = proxy.searchFor(site.getId(),cat_id,search,0,-1);
147         if(ids.size()==0)
148             return 0;
149         Criteria crit = ssn.createCriteria(LogForm.class).add(Expression.eq("site.id", new Integer JavaDoc(site.getId())));
150         crit = crit.add(Expression.in("id",ids));
151         List JavaDoc logs = crit.list();
152         //过滤掉没有权限的日记分类
153
Iterator JavaDoc ls = logs.iterator();
154         while(ls.hasNext()) {
155             LogForm log = (LogForm)ls.next();
156             if(loginUser==null||!loginUser.isAdmin()) {
157                 if(log.getCategory().getType()==CategoryForm.TYPE_OWNER)
158                     ls.remove();
159             }
160         }
161         int lc = logs.size();
162         logs.clear();
163         return lc;
164     }
165     /**
166      * 读取指定的日记信息
167      * @param ssn
168      * @param site
169      * @param loginUser
170      * @param log_id
171      * @return
172      */

173     public static LogForm getLogForm(Session ssn, SiteForm site, UserForm loginUser, int log_id)
174     {
175         LogForm log = null;
176         try{
177             log = (LogForm)ssn.load(LogForm.class, new Integer JavaDoc(log_id));
178             if(log.getStatus()==LogForm.STATUS_DELETED)
179                 log = null;
180             else
181             if(log!=null && log.getSite().getId()!=site.getId())
182                 log = null;
183             else
184             if(log!=null&&(loginUser==null||!loginUser.isAdmin())&&log.getCategory().isOwnerOnly())
185                 log = null;
186         }catch(HibernateException e){}
187         return log;
188     }
189     
190     /**
191      * 统计出指定月份每天的日记篇数
192      * 使用SQL查询方式避免加载日记内容以提供查询速度
193      * @param ssn
194      * @param site
195      * @param year
196      * @param month (1-12)
197      * @return
198      * @throws HibernateException
199      */

200     public static int[] statLogs(Session ssn, SiteForm site, UserForm loginUser, int year, int month) throws HibernateException
201     {
202         Calendar JavaDoc cal = Calendar.getInstance();
203         cal.set(Calendar.YEAR, year);
204         cal.set(Calendar.MONTH, month-1);
205         return statLogs(ssn,site,loginUser,cal);
206     }
207     
208     /**
209      * 统计指定月份每天的日记数(使用Hibernate查询方式)
210      * 使用SQL查询方式避免加载日记内容以提供查询速度
211      * @param ssn
212      * @param site
213      * @param loginUser
214      * @param month
215      * @return
216      * @throws HibernateException
217      */

218     public static int[] statLogs(Session ssn, SiteForm site, UserForm loginUser, Calendar JavaDoc month) throws HibernateException{
219         
220         Calendar JavaDoc firstDate = (Calendar JavaDoc)month.clone();
221         firstDate.set(Calendar.DATE,1);
222         resetCalendar(firstDate);
223         Calendar JavaDoc nextMonthFirstDate = (Calendar JavaDoc)firstDate.clone();
224         nextMonthFirstDate.add(Calendar.MONTH,1);
225         
226         //计算指定月份有多少天
227
Calendar JavaDoc tempCal = (Calendar JavaDoc)nextMonthFirstDate.clone();
228         tempCal.add(Calendar.DATE,-1);
229         int dateCount = tempCal.get(Calendar.DATE);
230         
231         int[] logCounts = new int[dateCount];
232         
233         //查询出当月的所有日记进行统计
234

235         StringBuffer JavaDoc hql = new StringBuffer JavaDoc("SELECT log.logTime FROM ");
236         hql.append(LogForm.class.getName());
237         hql.append(" AS log WHERE log.logTime>=? AND log.logTime<? AND log.status=?");
238         Query q = ssn.createQuery(hql.toString());
239         q.setTimestamp(0, firstDate.getTime());
240         q.setTimestamp(1, nextMonthFirstDate.getTime());
241         q.setInteger(2, LogForm.STATUS_NORMAL);
242         
243         Iterator JavaDoc logs = q.list().iterator();
244         while(logs.hasNext()){
245             tempCal.setTime((Timestamp JavaDoc)logs.next());
246             int date = tempCal.get(Calendar.DATE) - 1;
247             logCounts[date]++;
248         }
249         
250         return logCounts;
251     }
252     /**
253      * 统计指定月份每天的日记数(使用SQL查询方式)
254      * @deprecated 使用Hibernate的HQL来查询
255      * @param ssn
256      * @param site
257      * @param loginUser
258      * @param month
259      * @return
260      * @throws HibernateException
261      */

262     public static int[] statLogs(Connection JavaDoc conn, SiteForm site, UserForm loginUser, Calendar JavaDoc month) throws SQLException JavaDoc{
263         
264         Calendar JavaDoc firstDate = (Calendar JavaDoc)month.clone();
265         firstDate.set(Calendar.DATE,1);
266         resetCalendar(firstDate);
267         Calendar JavaDoc nextMonthFirstDate = (Calendar JavaDoc)firstDate.clone();
268         nextMonthFirstDate.add(Calendar.MONTH,1);
269         
270         //计算指定月份有多少天
271
Calendar JavaDoc tempCal = (Calendar JavaDoc)nextMonthFirstDate.clone();
272         tempCal.add(Calendar.DATE,-1);
273         int dateCount = tempCal.get(Calendar.DATE);
274         
275         int[] logCounts = new int[dateCount];
276         
277         //查询出当月的所有日记进行统计
278

279         String JavaDoc sql = "SELECT logTime FROM dlog_journal WHERE logTime>=? AND logTime<? AND status=?";
280         
281         PreparedStatement JavaDoc ps = null;
282         ResultSet JavaDoc rs = null;
283         
284         try{
285             ps = conn.prepareStatement(sql);
286             ps.setTimestamp(1, new Timestamp JavaDoc(firstDate.getTime().getTime()));
287             ps.setTimestamp(2, new Timestamp JavaDoc(nextMonthFirstDate.getTime().getTime()));
288             ps.setInt(3, LogForm.STATUS_NORMAL);
289             if(ps.execute()){
290                 rs = ps.getResultSet();
291                 while(rs.next()){
292                     tempCal.setTime(rs.getDate(1));
293                     int date = tempCal.get(Calendar.DATE) - 1;
294                     logCounts[date]++;
295                 }
296             }
297         }finally{
298             close(rs,ps,null);
299         }
300         
301         return logCounts;
302     }
303     /**
304      * 清除日历的时间字段
305      * @param cal
306      */

307     protected static void resetCalendar(Calendar JavaDoc cal){
308         cal.set(Calendar.HOUR_OF_DAY,0);
309         cal.set(Calendar.MINUTE,0);
310         cal.set(Calendar.SECOND,0);
311         cal.set(Calendar.MILLISECOND,0);
312     }
313     /**
314      * 根据参数构建一个日历对象实例
315      * @param year
316      * @param month 1-12
317      * @param date
318      * @param clearTime 是否清除时间字段
319      * @return
320      */

321     protected static Calendar JavaDoc buildCalendar(int year,int month,int date,boolean clearTime){
322         Calendar JavaDoc cal = Calendar.getInstance();
323         if(clearTime)
324             resetCalendar(cal);
325         if(year!=-1)
326             cal.set(Calendar.YEAR,year);
327         if(month!=-1)
328             cal.set(Calendar.MONTH,month-1);
329         if(date!=-1)
330             cal.set(Calendar.DATE,date);
331         return cal;
332     }
333     /**
334      * 读取草稿的详细信息
335      * @param ssn
336      * @param site
337      * @param draft_id
338      * @return
339      * @throws HibernateException
340      */

341     public static DraftForm getDraft(Session ssn, SiteForm site, int draft_id) throws HibernateException
342     {
343         Criteria crit = ssn.createCriteria(DraftForm.class);
344         crit = crit.add(Expression.eq("site.id", new Integer JavaDoc(site.getId())));
345         crit = crit.add(Expression.eq("id", new Integer JavaDoc(draft_id)));
346         DraftForm draft = null;
347         List JavaDoc drafts = crit.list();
348         if(drafts.size()>0)
349             draft = (DraftForm)drafts.get(0);
350         return draft;
351         
352     }
353     /**
354      * 列出某个网站的所有草稿信息
355      * @param ssn
356      * @param site
357      * @return
358      * @throws HibernateException
359      */

360     public static List JavaDoc listDrafts(Session ssn, SiteForm site, UserForm loginUser) throws HibernateException{
361         if(loginUser==null || (!loginUser.isAdmin()&&!loginUser.isFriend()))
362             return new ArrayList JavaDoc();
363         Criteria crit = ssn.createCriteria(DraftForm.class);
364         crit = crit.add(Expression.eq("site.id", new Integer JavaDoc(site.getId())));
365         //if(loginUser.isFriend())
366
crit = crit.add(Expression.eq("owner.id",new Integer JavaDoc(loginUser.getId())));
367         crit = crit.addOrder(Order.desc("logTime"));
368         return crit.list();
369     }
370     
371     /**
372      * 列出某个分类下的日志
373      * @param ssn
374      * @param site
375      * @param cat_id
376      * @param orderField
377      * @return
378      * @throws HibernateException
379      */

380     public static List JavaDoc listLogs(Session ssn, SiteForm site, UserForm loginUser, int cat_id, int userid,int from, int count, String JavaDoc orderField, int year, int month, int date)
381         throws HibernateException {
382         String JavaDoc hql = "FROM " + LogForm.class.getName() + " AS log WHERE log.site.id=? AND log.status=?";
383         if(cat_id!=-1)
384             hql += " AND log.category.id=?";
385         else
386             hql += " AND log.category.id<>?";
387         if(userid>0)
388             hql += " AND log.owner.id="+userid;
389         
390         if(loginUser!=null && loginUser.getUserRole()==DlogRole.ROLE_BUDDY){
391             int[] cats = loginUser.getOwnerCatids();
392             if(cats.length==0)
393                 hql += " AND log.category.type<>" + CategoryForm.TYPE_OWNER;
394             else{
395                 hql += " AND (log.category.type<>" + CategoryForm.TYPE_OWNER;
396                 for(int i=0;i<cats.length;i++){
397                     hql += " OR log.category.id=" + cats[i];
398                 }
399                 hql += ")";
400             }
401         }
402         else
403         if(loginUser==null||!loginUser.isAdmin())
404             hql += " AND log.category.type<>" + CategoryForm.TYPE_OWNER;
405         
406         Calendar JavaDoc begin = null;
407         Calendar JavaDoc end = null;
408         boolean hasTime = false;
409         if(year!=-1&&month!=-1&&date!=-1){//查询某天
410
begin = buildCalendar(year,month,date,true);
411             end = (Calendar JavaDoc)begin.clone();
412             end.add(Calendar.DATE,1);
413             hql += " AND log.logTime>=? AND log.logTime<?";
414             hasTime = true;
415         }
416         else
417         if(year!=-1&&month!=-1){//查询某月
418
begin = buildCalendar(year,month,1,true);
419             end = (Calendar JavaDoc)begin.clone();
420             end.add(Calendar.MONTH,1);
421             hql += " AND log.logTime>=? AND log.logTime<?";
422             hasTime = true;
423         }
424         else
425         if(year!=-1){//查询某年
426
begin = buildCalendar(year,1,1,true);
427             end = (Calendar JavaDoc)begin.clone();
428             end.add(Calendar.YEAR,1);
429             hql += " AND log.logTime>=? AND log.logTime<?";
430             hasTime = true;
431         }
432         if(StringUtils.isEmpty(orderField))
433             orderField = "logTime";
434         hql += " ORDER BY log." + orderField + " DESC";
435         Query query = ssn.createQuery(hql);
436         query.setInteger(0,site.getId());
437         query.setInteger(1,LogForm.STATUS_NORMAL);
438         query.setInteger(2,cat_id);
439         if(hasTime){
440             query.setCalendar(3,begin);
441             query.setCalendar(4,end);
442         }
443         query.setFirstResult(from);
444         query.setMaxResults(count);
445         return query.list();
446         
447     }
448     
449     /**
450      * 获取某个日记分类下的日记数
451      * @param ssn
452      * @param cat_id
453      * @return
454      * @throws HibernateException
455      */

456     public static int getLogCount(Session ssn, int cat_id) throws HibernateException{
457         Query q2 = ssn.createQuery("SELECT COUNT(*) FROM "+LogForm.class.getName()+" AS log WHERE log.category.id=?");
458         q2.setInteger(0, cat_id);
459         List JavaDoc res = q2.list();
460         return (res.size()>0)?((Integer JavaDoc)res.get(0)).intValue():0;
461     }
462     
463     /**
464      * 获取日记总数
465      * @param ssn
466      * @param site
467      * @param cat_id 指定某个日记分类
468      * @return
469      */

470     public static int getLogCount(Session ssn, SiteForm site, UserForm loginUser, int cat_id, int userid, int year, int month, int date)
471         throws HibernateException {
472         String JavaDoc hql = "SELECT COUNT(*) FROM " + LogForm.class.getName() + " AS log WHERE log.site.id=? AND log.status=?";
473         if(cat_id!=-1)
474             hql += " AND log.category.id=?";
475         else
476             hql += " AND log.category.id<>?";
477         if(userid>0)
478             hql += " AND log.owner.id="+userid;
479         
480         if(loginUser!=null && loginUser.getUserRole()==DlogRole.ROLE_BUDDY){
481             int[] cats = loginUser.getOwnerCatids();
482             if(cats.length==0)
483                 hql += " AND log.category.type<>" + CategoryForm.TYPE_OWNER;
484             else{
485                 hql += " AND (log.category.type<>" + CategoryForm.TYPE_OWNER;
486                 for(int i=0;i<cats.length;i++){
487                     hql += " OR log.category.id=" + cats[i];
488                 }
489                 hql += ")";
490             }
491         }
492         else
493         if(loginUser==null||!loginUser.isAdmin())
494             hql += " AND log.category.type<>" + CategoryForm.TYPE_OWNER;
495         
496         
497         Calendar JavaDoc begin = null;
498         Calendar JavaDoc end = null;
499         boolean hasTime = false;
500         if(year!=-1&&month!=-1&&date!=-1){//查询某天
501
begin = buildCalendar(year,month,date,true);
502             end = (Calendar JavaDoc)begin.clone();
503             end.add(Calendar.DATE,1);
504             hql += " AND log.logTime>=? AND log.logTime<?";
505             hasTime = true;
506         }
507         else
508         if(year!=-1&&month!=-1){//查询某月
509
begin = buildCalendar(year,month,1,true);
510             end = (Calendar JavaDoc)begin.clone();
511             end.add(Calendar.MONTH,1);
512             hql += " AND log.logTime>=? AND log.logTime<?";
513             hasTime = true;
514         }
515         else
516         if(year!=-1){//查询某年
517
begin = buildCalendar(year,1,1,true);
518             end = (Calendar JavaDoc)begin.clone();
519             end.add(Calendar.YEAR,1);
520             hql += " AND log.logTime>=? AND log.logTime<?";
521             hasTime = true;
522         }
523         Query query = ssn.createQuery(hql);
524         query.setInteger(0,site.getId());
525         query.setInteger(1,LogForm.STATUS_NORMAL);
526         query.setInteger(2,cat_id);
527         if(hasTime){
528             query.setCalendar(3,begin);
529             query.setCalendar(4,end);
530         }
531         List JavaDoc res = query.list();
532         int logcount = (res.size() > 0) ? ((Integer JavaDoc) res.get(0)).intValue() : 0;
533         return logcount;
534     }
535 }
536
Popular Tags