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.sql.SQLException;
019: import java.util.ArrayList;
020: import java.util.Calendar;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import javax.servlet.ServletRequest;
025: import javax.servlet.http.HttpServletRequest;
026:
027: import net.sf.hibernate.Criteria;
028: import net.sf.hibernate.HibernateException;
029: import net.sf.hibernate.Query;
030: import net.sf.hibernate.Session;
031: import net.sf.hibernate.expression.Expression;
032: import net.sf.hibernate.expression.Order;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036:
037: import dlog4j.formbean.LogForm;
038: import dlog4j.formbean.ReplyForm;
039: import dlog4j.formbean.SiteForm;
040:
041: /**
042: * @author Liudong
043: * 用于管理子站点的类,例如通过它来获取当前正在访问的站点
044: */
045: public class SiteManager extends ManagerBase {
046:
047: final static Log log = LogFactory.getLog(SiteManager.class);
048:
049: /**
050: * 列出所有的个人Dlog,注意:不包括id=0的dlogcn这条记录
051: * @param ssn
052: * @param status 只列出status值为该参数值的个人Dlog,如果该值为-1则列出所有的个人Dlog
053: * @return
054: * @throws HibernateException
055: */
056: public static List listSites(Session ssn, int status, int count)
057: throws HibernateException {
058: List sites = new ArrayList();
059: String hql = "FROM " + SiteForm.class.getName()
060: + " AS s WHERE s.id>0";
061: if (status != -1)
062: hql += " AND s.status=" + status;
063: Query q = ssn.createQuery(hql);
064: if (count > 0)
065: q.setMaxResults(count);
066: return q.list();
067: }
068:
069: /**
070: * 读取一个网站的详细信息
071: * @param ssn
072: * @param sitename
073: * @return
074: * @throws HibernateException
075: */
076: public static SiteForm loadSite(String sitename)
077: throws HibernateException, SQLException {
078: SiteForm site = null;
079: Session ssn = null;
080: try {
081: ssn = ManagerBase.getSession();
082: return loadSite(ssn, sitename);
083: } finally {
084: close(ssn);
085: }
086: }
087:
088: /**
089: * 读取一个网站的详细信息
090: * @param ssn
091: * @param sitename
092: * @return
093: * @throws HibernateException
094: */
095: public static SiteForm loadSite(Session ssn, String sitename)
096: throws HibernateException {
097: Criteria crit = ssn.createCriteria(SiteForm.class);
098: crit = crit.add(Expression.eq("name", sitename));
099: Iterator sites = crit.list().iterator();
100: SiteForm site = null;
101: if (sites.hasNext())
102: site = (SiteForm) sites.next();
103: return site;
104: }
105:
106: /**
107: * 读取一个网站的详细信息
108: * @param ssn
109: * @param id
110: * @return
111: * @throws HibernateException
112: */
113: public static SiteForm loadSite(int id) throws HibernateException,
114: SQLException {
115: SiteForm site = null;
116: Session ssn = null;
117: try {
118: ssn = ManagerBase.getSession();
119: return loadSite(ssn, id);
120: } finally {
121: close(ssn);
122: }
123: }
124:
125: /**
126: * 读取一个网站的详细信息
127: * @param ssn
128: * @param id
129: * @return
130: * @throws HibernateException
131: */
132: public static SiteForm loadSite(Session ssn, int id)
133: throws HibernateException {
134: Criteria crit = ssn.createCriteria(SiteForm.class);
135: crit = crit.add(Expression.eq("id", new Integer(id)));
136: Iterator sites = crit.list().iterator();
137: SiteForm site = null;
138: if (sites.hasNext())
139: site = (SiteForm) sites.next();
140: return site;
141: }
142:
143: /**
144: * 获取当前正在访问的站点信息
145: * @param request
146: * @return
147: * @throws Exception
148: */
149: public static SiteForm getCurrentSite(ServletRequest request) {
150: HttpServletRequest req = (HttpServletRequest) request;
151: SiteForm site = (SiteForm) req.getSession().getAttribute(
152: SiteForm.class.getName());
153: if (site == null) {
154: //如果指定了log_id参数则读取该log对应的site
155: Session ssn = null;
156: try {
157: site = SiteManager.loadSite(1);
158: if (site != null)
159: req.getSession().setAttribute(
160: SiteForm.class.getName(), site);
161: } catch (SQLException e) {
162: log.error("获取当前访问的SiteForm实例失败", e);
163: } catch (HibernateException e) {
164: log.error("获取当前访问的SiteForm实例失败", e);
165: } finally {
166: if (ssn != null)
167: close(ssn);
168: }
169: }
170: return site;
171: }
172:
173: public static int[] statSiteLogs(ServletRequest req, int year,
174: int month) throws HibernateException, SQLException {
175: Session ssn = null;
176: try {
177: ssn = getSession();
178: return statSiteLogs(ssn, getCurrentSite(req), year, month);
179: } finally {
180: close(ssn);
181: }
182: }
183:
184: protected static Calendar getFirstDate(Calendar cal) {
185: Calendar month = null;
186: if (cal == null)
187: month = Calendar.getInstance();
188: else
189: month = (Calendar) cal.clone();
190: month.set(Calendar.HOUR_OF_DAY, 0);
191: month.set(Calendar.MINUTE, 0);
192: month.set(Calendar.SECOND, 0);
193: month.set(Calendar.MILLISECOND, 0);
194: month.set(Calendar.DATE, 1);
195: return month;
196: }
197:
198: protected static Calendar getLastDate(Calendar cal) {
199: Calendar month = null;
200: if (cal == null)
201: month = Calendar.getInstance();
202: else
203: month = (Calendar) cal.clone();
204: month.add(Calendar.MONTH, 1);
205: month.set(Calendar.DATE, 1);
206: month.set(Calendar.HOUR_OF_DAY, 0);
207: month.set(Calendar.MINUTE, 0);
208: month.set(Calendar.SECOND, 0);
209: month.set(Calendar.MILLISECOND, 0);
210: return month;
211: }
212:
213: /**
214: * 某年某月的网站日记数统计
215: * @param ssn
216: * @param site
217: * @param year
218: * @param month
219: * @return
220: * @throws HibernateException
221: */
222: public static int[] statSiteLogs(Session ssn, SiteForm site,
223: int year, int month) throws HibernateException {
224: Calendar cal = Calendar.getInstance();
225: cal.set(Calendar.YEAR, year);
226: cal.set(Calendar.MONTH, month - 1);
227: Calendar from = getFirstDate(cal);
228: Calendar to = getLastDate(cal);
229: Criteria crit = ssn.createCriteria(LogForm.class);
230: if (site != null)
231: crit = crit.add(Expression.eq("site.id", new Integer(site
232: .getId())));
233: crit = crit.add(Expression.between("logTime", from.getTime(),
234: to.getTime()));
235: crit = crit.add(Expression.lt("logTime", to.getTime()));
236: crit = crit.addOrder(Order.asc("logTime"));
237: List datas = crit.list();
238: int dc = from.getActualMaximum(Calendar.DAY_OF_MONTH);
239: int[] logs = new int[dc];
240: for (int i = 0; i < datas.size(); i++) {
241: LogForm sb = (LogForm) datas.get(i);
242: cal.setTime(sb.getLogTime());
243: logs[cal.get(Calendar.DATE) - 1]++;
244: }
245: return logs;
246: }
247:
248: public static int[] statSiteReplies(ServletRequest req, int year,
249: int month) throws HibernateException, SQLException {
250: Session ssn = null;
251: try {
252: ssn = getSession();
253: return statSiteReplies(ssn, getCurrentSite(req), year,
254: month);
255: } finally {
256: close(ssn);
257: }
258: }
259:
260: /**
261: * 某年某月的网站评论数统计
262: * @param ssn
263: * @param site
264: * @param year
265: * @param month
266: * @return
267: * @throws HibernateException
268: */
269: public static int[] statSiteReplies(Session ssn, SiteForm site,
270: int year, int month) throws HibernateException {
271: Calendar cal = Calendar.getInstance();
272: cal.set(Calendar.YEAR, year);
273: cal.set(Calendar.MONTH, month - 1);
274: Calendar from = getFirstDate(cal);
275: Calendar to = getLastDate(cal);
276: Criteria crit = ssn.createCriteria(ReplyForm.class);
277: if (site != null)
278: crit = crit.add(Expression.eq("site.id", new Integer(site
279: .getId())));
280: crit = crit.add(Expression.between("writeTime", from.getTime(),
281: to.getTime()));
282: crit = crit.add(Expression.lt("writeTime", to.getTime()));
283: crit = crit.addOrder(Order.asc("writeTime"));
284: List datas = crit.list();
285: int dc = from.getActualMaximum(Calendar.DAY_OF_MONTH);
286: int[] replies = new int[dc];
287: for (int i = 0; i < datas.size(); i++) {
288: ReplyForm sb = (ReplyForm) datas.get(i);
289: cal.setTime(sb.getWriteTime());
290: replies[cal.get(Calendar.DATE) - 1]++;
291: }
292: return replies;
293: }
294:
295: public static void main(String[] args) throws Exception {
296: Calendar cal = Calendar.getInstance();
297: System.out.println(getFirstDate(cal).getTime());
298: System.out.println(getLastDate(cal).getTime());
299: }
300:
301: }
|