1 16 package dlog4j; 17 18 import java.sql.SQLException ; 19 import java.util.ArrayList ; 20 import java.util.Calendar ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import javax.servlet.ServletRequest ; 25 import javax.servlet.http.HttpServletRequest ; 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 45 public class SiteManager extends ManagerBase{ 46 47 final static Log log = LogFactory.getLog(SiteManager.class); 48 55 public static List listSites(Session ssn, int status, int count) throws HibernateException{ 56 List sites = new ArrayList (); 57 String 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 72 public static SiteForm loadSite(String sitename) throws HibernateException, SQLException { 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 89 public static SiteForm loadSite(Session ssn, String sitename) throws HibernateException{ 90 Criteria crit = ssn.createCriteria(SiteForm.class); 91 crit = crit.add(Expression.eq("name",sitename)); 92 Iterator sites = crit.list().iterator(); 93 SiteForm site = null; 94 if(sites.hasNext()) 95 site = (SiteForm)sites.next(); 96 return site; 97 } 98 105 public static SiteForm loadSite(int id) throws HibernateException, SQLException { 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 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 (id))); 125 Iterator sites = crit.list().iterator(); 126 SiteForm site = null; 127 if(sites.hasNext()) 128 site = (SiteForm)sites.next(); 129 return site; 130 } 131 137 public static SiteForm getCurrentSite(ServletRequest request){ 138 HttpServletRequest req = (HttpServletRequest )request; 139 SiteForm site = (SiteForm)req.getSession().getAttribute(SiteForm.class.getName()); 140 if(site==null){ 141 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 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 req, int year, int month) throws HibernateException, SQLException { 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 getFirstDate(Calendar cal) { 168 Calendar month = null; 169 if(cal==null) 170 month = Calendar.getInstance(); 171 else 172 month = (Calendar )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 getLastDate(Calendar cal) { 181 Calendar month = null; 182 if(cal==null) 183 month = Calendar.getInstance(); 184 else 185 month = (Calendar )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 203 public static int[] statSiteLogs(Session ssn, SiteForm site, int year, int month) throws HibernateException{ 204 Calendar cal = Calendar.getInstance(); 205 cal.set(Calendar.YEAR,year); 206 cal.set(Calendar.MONTH,month-1); 207 Calendar from = getFirstDate(cal); 208 Calendar to = getLastDate(cal); 209 Criteria crit = ssn.createCriteria(LogForm.class); 210 if(site!=null) 211 crit = crit.add(Expression.eq("site.id",new Integer (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 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 req, int year, int month) throws HibernateException, SQLException { 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 243 public static int[] statSiteReplies(Session ssn, SiteForm site, int year, int month) throws HibernateException{ 244 Calendar cal = Calendar.getInstance(); 245 cal.set(Calendar.YEAR,year); 246 cal.set(Calendar.MONTH,month-1); 247 Calendar from = getFirstDate(cal); 248 Calendar to = getLastDate(cal); 249 Criteria crit = ssn.createCriteria(ReplyForm.class); 250 if(site!=null) 251 crit = crit.add(Expression.eq("site.id",new Integer (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 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 [] args) throws Exception { 266 Calendar cal = Calendar.getInstance(); 267 System.out.println(getFirstDate(cal).getTime()); 268 System.out.println(getLastDate(cal).getTime()); 269 } 270 271 } 272 | Popular Tags |