📄 logmanager.java
字号:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package dlog4j;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import net.sf.hibernate.Criteria;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.expression.Expression;
import net.sf.hibernate.expression.Order;
import org.apache.commons.lang.StringUtils;
import org.apache.lucene.queryParser.ParseException;
import dlog4j.formbean.CategoryForm;
import dlog4j.formbean.DraftForm;
import dlog4j.formbean.LogForm;
import dlog4j.formbean.SiteForm;
import dlog4j.formbean.UserForm;
import dlog4j.search.SearchProxy;
import dlog4j.security.DlogRole;
/**
* @author Liudong
* 日记管理器
*/
public class LogManager extends ManagerBase{
/**
* 日记的搜索
* @deprecated 该方法由searchAllLogs来代替
* @param ssn
* @param site
* @param loginUser
* @param cat_id
* @param search
* @param from
* @param count
* @return
* @throws HibernateException
*/
public static List searchLogs(Session ssn, SiteForm site, UserForm loginUser, int cat_id, String search, String orderField, int from, int count) throws HibernateException, IOException, ParseException
{
SearchProxy proxy = SearchProxy.getLogQuery();
List ids = proxy.searchFor(site.getId(),cat_id,search,from,count);
if(ids.size()==0)
return new ArrayList();
//System.out.println("search.count="+ids.size());
Criteria crit = ssn.createCriteria(LogForm.class).add(Expression.eq("site.id", new Integer(site.getId())));
crit = crit.add(Expression.in("id",ids));
if(StringUtils.isEmpty(orderField))
orderField = "logTime";
crit = crit.addOrder(Order.desc(orderField));
List logs = crit.list();
//过滤掉没有权限的日记分类
Iterator ls = logs.iterator();
while(ls.hasNext()) {
LogForm log = (LogForm)ls.next();
if(loginUser==null||!loginUser.isAdmin()) {
if(log.getCategory().getType()==CategoryForm.TYPE_OWNER)
ls.remove();
}
}
return logs;
}
/**
* 搜索符合条件的所有日记
* @param ssn
* @param site
* @param loginUser
* @param cat_id
* @param search
* @return
* @throws HibernateException
* @throws IOException
* @throws ParseException
*/
public static List searchAllLogs(Session ssn, SiteForm site, UserForm loginUser, int cat_id, String search, String orderField) throws HibernateException, IOException, ParseException
{
List logs = new ArrayList();
SearchProxy proxy = SearchProxy.getLogQuery();
List ids = proxy.searchFor(site.getId(),cat_id,search,0,-1);
if(ids.size()>0) {
Criteria crit = ssn.createCriteria(LogForm.class).add(Expression.eq("site.id", new Integer(site.getId())));
crit = crit.add(Expression.in("id",ids));
if(StringUtils.isEmpty(orderField))
orderField = "logTime";
crit = crit.addOrder(Order.desc(orderField));
logs = crit.list();
//过滤掉没有权限的日记分类
Iterator ls = logs.iterator();
while(ls.hasNext()) {
LogForm log = (LogForm)ls.next();
if(loginUser==null||!loginUser.isAdmin()) {
if(log.getCategory().getType()==CategoryForm.TYPE_OWNER) {
ls.remove();
}
}
}
}
if(ids!=null)
ids.clear();
return logs;
}
/**
* 得到查询出的日记总数
* @deprecated 该方法已经由searchAllLogs来代替
* @param ssn
* @param site
* @param loginUser
* @param cat_id
* @param search
* @return
* @throws HibernateException
* @throws IOException
* @throws ParseException
*/
public static int getSearchLogCount(Session ssn, SiteForm site, UserForm loginUser, int cat_id, String search) throws HibernateException, IOException, ParseException
{
SearchProxy proxy = SearchProxy.getLogQuery();
List ids = proxy.searchFor(site.getId(),cat_id,search,0,-1);
if(ids.size()==0)
return 0;
Criteria crit = ssn.createCriteria(LogForm.class).add(Expression.eq("site.id", new Integer(site.getId())));
crit = crit.add(Expression.in("id",ids));
List logs = crit.list();
//过滤掉没有权限的日记分类
Iterator ls = logs.iterator();
while(ls.hasNext()) {
LogForm log = (LogForm)ls.next();
if(loginUser==null||!loginUser.isAdmin()) {
if(log.getCategory().getType()==CategoryForm.TYPE_OWNER)
ls.remove();
}
}
int lc = logs.size();
logs.clear();
return lc;
}
/**
* 读取指定的日记信息
* @param ssn
* @param site
* @param loginUser
* @param log_id
* @return
*/
public static LogForm getLogForm(Session ssn, SiteForm site, UserForm loginUser, int log_id)
{
LogForm log = null;
try{
log = (LogForm)ssn.load(LogForm.class, new Integer(log_id));
if(log.getStatus()==LogForm.STATUS_DELETED)
log = null;
else
if(log!=null && log.getSite().getId()!=site.getId())
log = null;
else
if(log!=null&&(loginUser==null||!loginUser.isAdmin())&&log.getCategory().isOwnerOnly())
log = null;
}catch(HibernateException e){}
return log;
}
/**
* 统计出指定月份每天的日记篇数
* 使用SQL查询方式避免加载日记内容以提供查询速度
* @param ssn
* @param site
* @param year
* @param month (1-12)
* @return
* @throws HibernateException
*/
public static int[] statLogs(Session ssn, SiteForm site, UserForm loginUser, int year, int month) throws HibernateException
{
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month-1);
return statLogs(ssn,site,loginUser,cal);
}
/**
* 统计指定月份每天的日记数(使用Hibernate查询方式)
* 使用SQL查询方式避免加载日记内容以提供查询速度
* @param ssn
* @param site
* @param loginUser
* @param month
* @return
* @throws HibernateException
*/
public static int[] statLogs(Session ssn, SiteForm site, UserForm loginUser, Calendar month) throws HibernateException{
Calendar firstDate = (Calendar)month.clone();
firstDate.set(Calendar.DATE,1);
resetCalendar(firstDate);
Calendar nextMonthFirstDate = (Calendar)firstDate.clone();
nextMonthFirstDate.add(Calendar.MONTH,1);
//计算指定月份有多少天
Calendar tempCal = (Calendar)nextMonthFirstDate.clone();
tempCal.add(Calendar.DATE,-1);
int dateCount = tempCal.get(Calendar.DATE);
int[] logCounts = new int[dateCount];
//查询出当月的所有日记进行统计
StringBuffer hql = new StringBuffer("SELECT log.logTime FROM ");
hql.append(LogForm.class.getName());
hql.append(" AS log WHERE log.logTime>=? AND log.logTime<? AND log.status=?");
Query q = ssn.createQuery(hql.toString());
q.setTimestamp(0, firstDate.getTime());
q.setTimestamp(1, nextMonthFirstDate.getTime());
q.setInteger(2, LogForm.STATUS_NORMAL);
Iterator logs = q.list().iterator();
while(logs.hasNext()){
tempCal.setTime((Timestamp)logs.next());
int date = tempCal.get(Calendar.DATE) - 1;
logCounts[date]++;
}
return logCounts;
}
/**
* 统计指定月份每天的日记数(使用SQL查询方式)
* @deprecated 使用Hibernate的HQL来查询
* @param ssn
* @param site
* @param loginUser
* @param month
* @return
* @throws HibernateException
*/
public static int[] statLogs(Connection conn, SiteForm site, UserForm loginUser, Calendar month) throws SQLException{
Calendar firstDate = (Calendar)month.clone();
firstDate.set(Calendar.DATE,1);
resetCalendar(firstDate);
Calendar nextMonthFirstDate = (Calendar)firstDate.clone();
nextMonthFirstDate.add(Calendar.MONTH,1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -