KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > ReplyManager


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.util.ArrayList JavaDoc;
20 import java.util.Calendar JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import net.sf.hibernate.Criteria;
25 import net.sf.hibernate.HibernateException;
26 import net.sf.hibernate.Query;
27 import net.sf.hibernate.Session;
28 import net.sf.hibernate.expression.Expression;
29 import net.sf.hibernate.expression.Order;
30
31 import org.apache.commons.lang.StringUtils;
32 import org.apache.lucene.queryParser.ParseException;
33
34 import dlog4j.formbean.CategoryForm;
35 import dlog4j.formbean.LogForm;
36 import dlog4j.formbean.ReplyForm;
37 import dlog4j.formbean.SiteForm;
38 import dlog4j.formbean.UserForm;
39 import dlog4j.search.SearchProxy;
40 /**
41  * @author Liudong 评论管理
42  */

43 public class ReplyManager {
44
45     public final static String JavaDoc FIELD_WRITETIME = "writeTime";
46     /**
47      * 读取指定的评论信息
48      * @param ssn
49      * @param site
50      * @param loginUser
51      * @param log_id
52      * @return
53      */

54     public static ReplyForm getLogForm(Session ssn, SiteForm site, UserForm loginUser, int log_id)
55     {
56         ReplyForm reply = null;
57         try{
58             reply = (ReplyForm)ssn.load(ReplyForm.class, new Integer JavaDoc(log_id));
59             if(reply!=null && reply.getSite().getId()!=site.getId())
60                 reply = null;
61             if(reply!=null)
62                 reply = null;
63         }catch(HibernateException e){}
64         return reply;
65     }
66     
67     /**
68      * 统计出指定月份每天的评论篇数
69      * @param ssn
70      * @param site
71      * @param year
72      * @param month (1-12)
73      * @return
74      * @throws HibernateException
75      */

76     public static int[] statReplies(Session ssn, SiteForm site, UserForm loginUser, int year, int month) throws HibernateException
77     {
78         Calendar JavaDoc cal = Calendar.getInstance();
79         cal.set(Calendar.YEAR, year);
80         cal.set(Calendar.MONTH, month-1);
81         return statReplies(ssn,site,loginUser,cal);
82     }
83     
84     public static int[] statReplies(Session ssn, SiteForm site, UserForm loginUser, Calendar JavaDoc month) throws HibernateException{
85         
86         Calendar JavaDoc firstDate = (Calendar JavaDoc)month.clone();
87         firstDate.set(Calendar.DATE,1);
88         resetCalendar(firstDate);
89         Calendar JavaDoc nextMonthFirstDate = (Calendar JavaDoc)firstDate.clone();
90         nextMonthFirstDate.add(Calendar.MONTH,1);
91         
92         //计算指定月份有多少天
93
Calendar JavaDoc tempCal = (Calendar JavaDoc)nextMonthFirstDate.clone();
94         tempCal.add(Calendar.DATE,-1);
95         int dateCount = tempCal.get(Calendar.DATE);
96         
97         int[] replyCounts = new int[dateCount];
98         
99         //查询出当月的所有日记进行统计
100

101         Criteria crit = ssn.createCriteria(ReplyForm.class).add(Expression.eq("site.id", new Integer JavaDoc(site.getId())));
102         crit = crit.addOrder(Order.asc(FIELD_WRITETIME));
103         crit = crit.add(Expression.ge(FIELD_WRITETIME,firstDate.getTime()));
104         crit = crit.add(Expression.lt(FIELD_WRITETIME,nextMonthFirstDate.getTime()));
105         
106         Iterator JavaDoc replyies = crit.list().iterator();
107         while(replyies.hasNext()){
108             ReplyForm reply = (ReplyForm)replyies.next();
109             if(loginUser==null)
110                 continue;
111             tempCal.setTime(reply.getWriteTime());
112             int date = tempCal.get(Calendar.DATE) - 1;
113             replyCounts[date]++;
114         }
115         
116         return replyCounts;
117     }
118     /**
119      * 清除日历的时间字段
120      * @param cal
121      */

122     protected static void resetCalendar(Calendar JavaDoc cal){
123         cal.set(Calendar.HOUR_OF_DAY,0);
124         cal.set(Calendar.MINUTE,0);
125         cal.set(Calendar.SECOND,0);
126         cal.set(Calendar.MILLISECOND,0);
127     }
128     /**
129      * 根据参数构建一个日历对象实例
130      * @param year
131      * @param month 1-12
132      * @param date
133      * @param clearTime 是否清除时间字段
134      * @return
135      */

136     protected static Calendar JavaDoc buildCalendar(int year,int month,int date,boolean clearTime){
137         Calendar JavaDoc cal = Calendar.getInstance();
138         if(clearTime)
139             resetCalendar(cal);
140         if(year!=-1)
141             cal.set(Calendar.YEAR,year);
142         if(month!=-1)
143             cal.set(Calendar.MONTH,month-1);
144         if(date!=-1)
145             cal.set(Calendar.DATE,date);
146         return cal;
147     }
148     /**
149      * 搜索符合条件的所有评论
150      * @param ssn
151      * @param site
152      * @param loginUser
153      * @param cat_id
154      * @param search
155      * @return
156      * @throws HibernateException
157      * @throws IOException
158      * @throws ParseException
159      */

160     public static List JavaDoc searchAllReplies(Session ssn, SiteForm site, UserForm loginUser, int cat_id, String JavaDoc search, String JavaDoc orderField) throws HibernateException, IOException JavaDoc, ParseException
161     {
162         List JavaDoc logs = new ArrayList JavaDoc();
163         SearchProxy proxy = SearchProxy.getReplyQuery();
164         List JavaDoc ids = proxy.searchFor(site.getId(),cat_id,search,0,-1);
165         if(ids.size()>0) {
166             Criteria crit = ssn.createCriteria(ReplyForm.class).add(Expression.eq("site.id", new Integer JavaDoc(site.getId())));
167             crit = crit.add(Expression.in("id",ids));
168             if(StringUtils.isEmpty(orderField))
169                 orderField = "writeTime";
170             crit = crit.addOrder(Order.desc(orderField));
171             logs = crit.list();
172             //过滤掉没有权限的日记分类
173
Iterator JavaDoc ls = logs.iterator();
174             while(ls.hasNext()) {
175                 ReplyForm reply = (ReplyForm)ls.next();
176                 if(reply.getLog().getStatus()!=LogForm.STATUS_NORMAL)
177                     ls.remove();
178                 if(loginUser==null||!loginUser.isAdmin()) {
179                     if(reply.getLog().getCategory().getType()==CategoryForm.TYPE_OWNER) {
180                         ls.remove();
181                     }
182                 }
183             }
184         }
185         if(ids!=null)
186             ids.clear();
187         return logs;
188     }
189     /**
190      * 获取查询到的评论信息的总数,用于分页
191      * @deprecated 由searchAllReplies替换
192      * @param ssn
193      * @param site
194      * @param loginUser
195      * @param cat_id
196      * @param userid
197      * @param search
198      * @return
199      * @throws IOException
200      * @throws ParseException
201      * @throws HibernateException
202      */

203     public static int getSearchReplyCount(Session ssn, SiteForm site, UserForm loginUser, int cat_id, int userid, String JavaDoc search) throws IOException JavaDoc, ParseException, HibernateException {
204         SearchProxy proxy = SearchProxy.getReplyQuery();
205         List JavaDoc ids = proxy.searchFor(site.getId(),cat_id,search,0,-1);
206         if(ids.size()==0)
207             return 0;
208         Criteria crit = ssn.createCriteria(ReplyForm.class).add(Expression.eq("site.id", new Integer JavaDoc(site.getId())));
209         crit = crit.add(Expression.in("id",ids));
210         List JavaDoc replies = crit.list();
211         //过滤掉没有权限的日记分类
212
Iterator JavaDoc ls = replies.iterator();
213         while(ls.hasNext()) {
214             ReplyForm reply = (ReplyForm)ls.next();
215             if(loginUser==null||!loginUser.isAdmin()) {
216                 if(reply.getLog().getCategory().getType()==CategoryForm.TYPE_OWNER)
217                     ls.remove();
218             }
219         }
220         int rc = replies.size();
221         replies.clear();
222         return rc;
223     }
224     /**
225      * 搜索评论信息
226      * @deprecated 由searchAllReplies替换
227      * @param ssn
228      * @param site
229      * @param loginUser
230      * @param cat_id
231      * @param userid
232      * @param search
233      * @param orderField
234      * @param from
235      * @param count
236      * @return
237      * @throws IOException
238      * @throws ParseException
239      * @throws HibernateException
240      */

241     public static List JavaDoc searchReplies(Session ssn, SiteForm site, UserForm loginUser, int cat_id, int userid, String JavaDoc search, String JavaDoc orderField,int from,int count) throws IOException JavaDoc, ParseException, HibernateException {
242         SearchProxy proxy = SearchProxy.getReplyQuery();
243         List JavaDoc ids = proxy.searchFor(site.getId(),cat_id,search,from,count);
244         if(ids.size()==0)
245             return new ArrayList JavaDoc();
246         Criteria crit = ssn.createCriteria(ReplyForm.class).add(Expression.eq("site.id", new Integer JavaDoc(site.getId())));
247         crit = crit.add(Expression.in("id",ids));
248         if(StringUtils.isEmpty(orderField))
249             orderField = FIELD_WRITETIME;
250         crit = crit.addOrder(Order.desc(orderField));
251         /*
252         if(from>=0)
253             crit.setFirstResult(from);
254         if(count>=0)
255             crit.setMaxResults(count);
256         */

257         List JavaDoc replies = crit.list();
258         //过滤掉没有权限的日记分类
259
Iterator JavaDoc ls = replies.iterator();
260         while(ls.hasNext()) {
261             ReplyForm reply = (ReplyForm)ls.next();
262             if(loginUser==null||!loginUser.isAdmin()) {
263                 if(reply.getLog().getCategory().getType()==CategoryForm.TYPE_OWNER)
264                     ls.remove();
265             }
266         }
267         return replies;
268     }
269     /**
270      * 列出某个分类下的评论
271      * @param ssn
272      * @param site
273      * @param cat_id
274      * @param orderField
275      * @return
276      * @throws HibernateException
277      */

278     public static List JavaDoc listReplies(Session ssn, SiteForm site, UserForm loginUser, int cat_id, int userid, int from, int count, String JavaDoc orderField, int year, int month, int date)
279         throws HibernateException {
280         String JavaDoc hql = "FROM " + ReplyForm.class.getName() + " AS reply WHERE reply.site.id=? ";
281         if(cat_id!=-1)
282             hql += " AND reply.log.category.id=?";
283         else
284             hql += " AND reply.log.category.id<>?";
285         if(userid>0)
286             hql += " AND reply.author.id="+userid;
287         if(loginUser==null||!loginUser.isAdmin())
288             hql += " AND reply.log.category.type<>" + CategoryForm.TYPE_OWNER;
289
290         hql += " AND reply.log.status=?";
291         
292         Calendar JavaDoc begin = null;
293         Calendar JavaDoc end = null;
294         boolean hasTime = false;
295         if(year!=-1&&month!=-1&&date!=-1){//查询某天
296
begin = buildCalendar(year,month,date,true);
297             end = (Calendar JavaDoc)begin.clone();
298             end.add(Calendar.DATE,1);
299             hql += " AND reply.writeTime>=? AND reply.writeTime<?";
300             hasTime = true;
301         }
302         else
303         if(year!=-1&&month!=-1){//查询某月
304
begin = buildCalendar(year,month,1,true);
305             end = (Calendar JavaDoc)begin.clone();
306             end.add(Calendar.MONTH,1);
307             hql += " AND reply.writeTime>=? AND reply.writeTime<?";
308             hasTime = true;
309         }
310         else
311         if(year!=-1){//查询某年
312
begin = buildCalendar(year,1,1,true);
313             end = (Calendar JavaDoc)begin.clone();
314             end.add(Calendar.YEAR,1);
315             hql += " AND reply.writeTime>=? AND reply.writeTime<?";
316             hasTime = true;
317         }
318         if(StringUtils.isEmpty(orderField))
319             orderField = FIELD_WRITETIME;
320         hql += " ORDER BY reply." + orderField + " DESC";
321         Query query = ssn.createQuery(hql);
322         query.setInteger(0,site.getId());
323         query.setInteger(1,cat_id);
324         query.setInteger(2,LogForm.STATUS_NORMAL);
325         if(hasTime){
326             query.setCalendar(3,begin);
327             query.setCalendar(4,end);
328         }
329         query.setFirstResult(from);
330         if ( count > 0 ) {
331             query.setMaxResults(count);
332         }
333         return query.list();
334         
335     }
336     
337     
338     /**
339      * 列出最新的评论信息
340      *
341      * @param ssn
342      * @param site
343      * @param count
344      * @return @throws
345      * SQLException
346      * @throws HibernateException
347      */

348     public static List JavaDoc listReplies(Session ssn, SiteForm site,
349             UserForm loginUser, int count) throws HibernateException {
350         String JavaDoc hql = "FROM " + ReplyForm.class.getName()
351                 + " AS r WHERE r.site.id=? AND r.log.status=?";
352         if (loginUser == null || !loginUser.isAdmin())
353                 hql += " AND r.log.category.type<>?";
354         hql += " ORDER BY r.writeTime DESC";
355         Query query = ssn.createQuery(hql);
356         query.setInteger(0, site.getId());
357         query.setInteger(1, LogForm.STATUS_NORMAL);
358         if (loginUser == null || !loginUser.isAdmin())
359                 query.setInteger(2, CategoryForm.TYPE_OWNER);
360         if (count > 0) query.setMaxResults(count);
361         return query.list();
362     }
363     
364     /**
365      * 获取日记总数
366      * @param ssn
367      * @param site
368      * @param cat_id 指定某个日记分类
369      * @return
370      */

371     public static int getRepliesCount(Session ssn, SiteForm site, UserForm loginUser, int cat_id, int userid , int year, int month, int date)
372         throws HibernateException {
373         String JavaDoc hql = "SELECT COUNT(*) FROM " + ReplyForm.class.getName() + " AS reply WHERE reply.site.id=? ";
374         if(cat_id!=-1)
375             hql += " AND reply.log.category.id=?";
376         else
377             hql += " AND reply.log.category.id<>?";
378         if(userid>0)
379             hql += " AND reply.author.id="+userid;
380         if(loginUser==null||!loginUser.isAdmin())
381             hql += " AND reply.log.category.type<>" + CategoryForm.TYPE_OWNER;
382
383         hql += " AND reply.log.status=?";
384         
385         Calendar JavaDoc begin = null;
386         Calendar JavaDoc end = null;
387         boolean hasTime = false;
388         if(year!=-1&&month!=-1&&date!=-1){//查询某天
389
begin = buildCalendar(year,month,date,true);
390             end = (Calendar JavaDoc)begin.clone();
391             end.add(Calendar.DATE,1);
392             hql += " AND reply.writeTime>=? AND reply.writeTime<?";
393             hasTime = true;
394         }
395         else
396         if(year!=-1&&month!=-1){//查询某月
397
begin = buildCalendar(year,month,1,true);
398             end = (Calendar JavaDoc)begin.clone();
399             end.add(Calendar.MONTH,1);
400             hql += " AND reply.writeTime>=? AND reply.writeTime<?";
401             hasTime = true;
402         }
403         else
404         if(year!=-1){//查询某年
405
begin = buildCalendar(year,1,1,true);
406             end = (Calendar JavaDoc)begin.clone();
407             end.add(Calendar.YEAR,1);
408             hql += " AND reply.writeTime>=? AND reply.writeTime<?";
409             hasTime = true;
410         }
411         Query query = ssn.createQuery(hql);
412         query.setInteger(0,site.getId());
413         query.setInteger(1,cat_id);
414         query.setInteger(2,LogForm.STATUS_NORMAL);
415         if(hasTime){
416             query.setCalendar(3,begin);
417             query.setCalendar(4,end);
418         }
419         List JavaDoc res = query.list();
420         int Replycount = (res.size() > 0) ? ((Integer JavaDoc) res.get(0)).intValue() : 0;
421         return Replycount;
422     }
423 }
424
Popular Tags