KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > SiteManager


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.sql.SQLException 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 javax.servlet.ServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26
27 import net.sf.hibernate.Criteria;
28 import net.sf.hibernate.HibernateException;
29 import net.sf.hibernate.Query;
30 import net.sf.hibernate.Session;
31 import net.sf.hibernate.expression.Expression;
32 import net.sf.hibernate.expression.Order;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37 import dlog4j.formbean.LogForm;
38 import dlog4j.formbean.ReplyForm;
39 import dlog4j.formbean.SiteForm;
40
41 /**
42  * @author Liudong
43  * 用于管理子站点的类,例如通过它来获取当前正在访问的站点
44  */

45 public class SiteManager extends ManagerBase{
46
47     final static Log log = LogFactory.getLog(SiteManager.class);
48     /**
49      * 列出所有的个人Dlog,注意:不包括id=0的dlogcn这条记录
50      * @param ssn
51      * @param status 只列出status值为该参数值的个人Dlog,如果该值为-1则列出所有的个人Dlog
52      * @return
53      * @throws HibernateException
54      */

55     public static List JavaDoc listSites(Session ssn, int status, int count) throws HibernateException{
56         List JavaDoc sites = new ArrayList JavaDoc();
57         String JavaDoc hql = "FROM "+SiteForm.class.getName()+" AS s WHERE s.id>0";
58         if(status!=-1)
59             hql += " AND s.status=" + status;
60         Query q = ssn.createQuery(hql);
61         if(count>0)
62             q.setMaxResults(count);
63         return q.list();
64     }
65     /**
66      * 读取一个网站的详细信息
67      * @param ssn
68      * @param sitename
69      * @return
70      * @throws HibernateException
71      */

72     public static SiteForm loadSite(String JavaDoc sitename) throws HibernateException, SQLException JavaDoc{
73         SiteForm site = null;
74         Session ssn = null;
75         try {
76             ssn = ManagerBase.getSession();
77             return loadSite(ssn,sitename);
78         }finally {
79             close(ssn);
80         }
81     }
82     /**
83      * 读取一个网站的详细信息
84      * @param ssn
85      * @param sitename
86      * @return
87      * @throws HibernateException
88      */

89     public static SiteForm loadSite(Session ssn, String JavaDoc sitename) throws HibernateException{
90         Criteria crit = ssn.createCriteria(SiteForm.class);
91         crit = crit.add(Expression.eq("name",sitename));
92         Iterator JavaDoc sites = crit.list().iterator();
93         SiteForm site = null;
94         if(sites.hasNext())
95             site = (SiteForm)sites.next();
96         return site;
97     }
98     /**
99      * 读取一个网站的详细信息
100      * @param ssn
101      * @param id
102      * @return
103      * @throws HibernateException
104      */

105     public static SiteForm loadSite(int id) throws HibernateException, SQLException JavaDoc{
106         SiteForm site = null;
107         Session ssn = null;
108         try {
109             ssn = ManagerBase.getSession();
110             return loadSite(ssn,id);
111         }finally {
112             close(ssn);
113         }
114     }
115     /**
116      * 读取一个网站的详细信息
117      * @param ssn
118      * @param id
119      * @return
120      * @throws HibernateException
121      */

122     public static SiteForm loadSite(Session ssn, int id) throws HibernateException{
123         Criteria crit = ssn.createCriteria(SiteForm.class);
124         crit = crit.add(Expression.eq("id",new Integer JavaDoc(id)));
125         Iterator JavaDoc sites = crit.list().iterator();
126         SiteForm site = null;
127         if(sites.hasNext())
128             site = (SiteForm)sites.next();
129         return site;
130     }
131     /**
132      * 获取当前正在访问的站点信息
133      * @param request
134      * @return
135      * @throws Exception
136      */

137     public static SiteForm getCurrentSite(ServletRequest JavaDoc request){
138         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc)request;
139         SiteForm site = (SiteForm)req.getSession().getAttribute(SiteForm.class.getName());
140         if(site==null){
141             //如果指定了log_id参数则读取该log对应的site
142
Session ssn = null;
143             try {
144                 site = SiteManager.loadSite(1);
145                 if(site!=null)
146                     req.getSession().setAttribute(SiteForm.class.getName(), site);
147             }catch(SQLException JavaDoc e) {
148                 log.error("获取当前访问的SiteForm实例失败",e);
149             }catch(HibernateException e) {
150                 log.error("获取当前访问的SiteForm实例失败",e);
151             }finally {
152                 if(ssn!=null)
153                     close(ssn);
154             }
155         }
156         return site;
157     }
158     public static int[] statSiteLogs(ServletRequest JavaDoc req, int year, int month) throws HibernateException, SQLException JavaDoc{
159         Session ssn = null;
160         try {
161             ssn = getSession();
162             return statSiteLogs(ssn,getCurrentSite(req),year,month);
163         }finally {
164             close(ssn);
165         }
166     }
167     protected static Calendar JavaDoc getFirstDate(Calendar JavaDoc cal) {
168         Calendar JavaDoc month = null;
169         if(cal==null)
170             month = Calendar.getInstance();
171         else
172             month = (Calendar JavaDoc)cal.clone();
173         month.set(Calendar.HOUR_OF_DAY,0);
174         month.set(Calendar.MINUTE,0);
175         month.set(Calendar.SECOND,0);
176         month.set(Calendar.MILLISECOND,0);
177         month.set(Calendar.DATE,1);
178         return month;
179     }
180     protected static Calendar JavaDoc getLastDate(Calendar JavaDoc cal) {
181         Calendar JavaDoc month = null;
182         if(cal==null)
183             month = Calendar.getInstance();
184         else
185             month = (Calendar JavaDoc)cal.clone();
186         month.add(Calendar.MONTH,1);
187         month.set(Calendar.DATE,1);
188         month.set(Calendar.HOUR_OF_DAY,0);
189         month.set(Calendar.MINUTE,0);
190         month.set(Calendar.SECOND,0);
191         month.set(Calendar.MILLISECOND,0);
192         return month;
193     }
194     /**
195      * 某年某月的网站日记数统计
196      * @param ssn
197      * @param site
198      * @param year
199      * @param month
200      * @return
201      * @throws HibernateException
202      */

203     public static int[] statSiteLogs(Session ssn, SiteForm site, int year, int month) throws HibernateException{
204         Calendar JavaDoc cal = Calendar.getInstance();
205         cal.set(Calendar.YEAR,year);
206         cal.set(Calendar.MONTH,month-1);
207         Calendar JavaDoc from = getFirstDate(cal);
208         Calendar JavaDoc to = getLastDate(cal);
209         Criteria crit = ssn.createCriteria(LogForm.class);
210         if(site!=null)
211             crit = crit.add(Expression.eq("site.id",new Integer JavaDoc(site.getId())));
212         crit = crit.add(Expression.between("logTime",from.getTime(),to.getTime()));
213         crit = crit.add(Expression.lt("logTime",to.getTime()));
214         crit = crit.addOrder(Order.asc("logTime"));
215         List JavaDoc datas = crit.list();
216         int dc = from.getActualMaximum(Calendar.DAY_OF_MONTH);
217         int[] logs = new int[dc];
218         for(int i=0;i<datas.size();i++) {
219             LogForm sb = (LogForm)datas.get(i);
220             cal.setTime(sb.getLogTime());
221             logs[cal.get(Calendar.DATE)-1] ++;
222         }
223         return logs;
224     }
225     public static int[] statSiteReplies(ServletRequest JavaDoc req, int year, int month) throws HibernateException, SQLException JavaDoc{
226         Session ssn = null;
227         try {
228             ssn = getSession();
229             return statSiteReplies(ssn,getCurrentSite(req),year,month);
230         }finally {
231             close(ssn);
232         }
233     }
234     /**
235      * 某年某月的网站评论数统计
236      * @param ssn
237      * @param site
238      * @param year
239      * @param month
240      * @return
241      * @throws HibernateException
242      */

243     public static int[] statSiteReplies(Session ssn, SiteForm site, int year, int month) throws HibernateException{
244         Calendar JavaDoc cal = Calendar.getInstance();
245         cal.set(Calendar.YEAR,year);
246         cal.set(Calendar.MONTH,month-1);
247         Calendar JavaDoc from = getFirstDate(cal);
248         Calendar JavaDoc to = getLastDate(cal);
249         Criteria crit = ssn.createCriteria(ReplyForm.class);
250         if(site!=null)
251             crit = crit.add(Expression.eq("site.id",new Integer JavaDoc(site.getId())));
252         crit = crit.add(Expression.between("writeTime",from.getTime(),to.getTime()));
253         crit = crit.add(Expression.lt("writeTime",to.getTime()));
254         crit = crit.addOrder(Order.asc("writeTime"));
255         List JavaDoc datas = crit.list();
256         int dc = from.getActualMaximum(Calendar.DAY_OF_MONTH);
257         int[] replies = new int[dc];
258         for(int i=0;i<datas.size();i++) {
259             ReplyForm sb = (ReplyForm)datas.get(i);
260             cal.setTime(sb.getWriteTime());
261             replies[cal.get(Calendar.DATE)-1] ++;
262         }
263         return replies;
264     }
265     public static void main(String JavaDoc[] args) throws Exception JavaDoc{
266         Calendar JavaDoc cal = Calendar.getInstance();
267         System.out.println(getFirstDate(cal).getTime());
268         System.out.println(getLastDate(cal).getTime());
269     }
270
271 }
272
Popular Tags