package com.util;
import java.util.Map;
import javax.servlet.http.Http
ServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.app.common.manager.BaseManager;
import com.app.jtb.model.EfileArchive;
import com.app.jtb.model.EfileInfo;
import com.app.jtb.model.EfileList;
import com.app.jtb.model.SingleUser;
import com.app.permission.model.ActionInfo;
import com.app.permission.model.LogInfo;
import com.app.permission.model.User;
import com.app.permission.web.filter.LogData;
public
class LogUtils {
/**
* 保存日志
* @param request
* @param response
* @param o
* @param archiveSort
* @param opName
* @param logClass
* @return
*/
public static boolean saveLogInfo(BaseManager baseManager, HttpServletRequest request,
HttpServletResponse response, Object o, String archiveSort,
String opName, String logClass) {
String url = ((HttpServletRequest) request).getRequestURI();
// 截取URL
int index = url.indexOf("/");
String actionString = url.substring(index);
int indexEnd = url.lastIndexOf("?");
if (indexEnd != -1) {
actionString.substring(0, indexEnd);
}
HttpSession session = ((HttpServletRequest) request).getSession();
// 从session中取得user信息
User user = (User) session.getAttribute("user");
String upperUrl = url.toUpperCase();
if (user == null && upperUrl.indexOf("LOGIN.DO") == -1) {
return true;
}
/* 将用户的URL地址,转为中文 */
String urlName = "";
Object oa = baseManager.findUniqueBy(ActionInfo.class, "actionUrl", url);
if(oa != null){
ActionInfo ai = (ActionInfo)oa;
urlName = ai.getActionName();
}
Integer userId = user.getId(); // 用户编号
if (userId == null) {
userId = new Integer(0);
}
String realName = user.getRealName();// 真实姓名
String username = user.getUsername(); // 用户名
String ip = request.getRemoteAddr(); // iP地址
String deptName = user.getDepartment().getDepartmentName();
// 进行日志的封装
LogInfo logInfo = new LogInfo();
logInfo.setUserId(userId);
logInfo.setUsername(realName);
logInfo.setEnglishName(username);
logInfo.setIp(ip);
logInfo.setDeptName(deptName);
logInfo.setTime(new java.util.Date());
logInfo.setMessage(urlName);
if (actionString.length() > 254) {
actionString = actionString.substring(0, 254);
}
logInfo.setOpName(opName);
logInfo.setLogClass(logClass);
setObjectInfo(o, archiveSort, logInfo);
setSingleUser(logInfo, request);
logInfo.setUrl(actionString);
// 写入数据库
baseManager.saveObject(logInfo);
return false;
}
private static void setObjectInfo(Object o, String archiveSort, LogInfo logInfo) {
if (o != null) {
if (o
instanceof EfileArchive) {
EfileArchive e = (EfileArchive) o;
logInfo.setArchiveId(e.getArchiveId());
logInfo.setArchiveLabel(e.getArchiveLabel());
logInfo.setArchiveSort(archiveSort);
} else if (o instanceof EfileList) {
EfileList f = (EfileList) o;
EfileArchive a = f.getEfileArchive();
if(a != null){
logInfo.setArchiveId(a.getArchiveId());
logInfo.setArchiveLabel(a.getArchiveLabel());
}
logInfo.setFileId(f.getId());
logInfo.setFileOrderId(f.getOrderNo());
logInfo.setArchiveSort(archiveSort);
} else if(o instanceof EfileInfo){
EfileInfo l = (EfileInfo) o;
logInfo.setFid(l.getId());
logInfo.setFiTitle(l.getFileTitle());
logInfo.setArchiveSort(archiveSort);
}
}
}
public static void setSingleUser(LogInfo logInfo, HttpServletRequest request){
if(request.getSession().getAttribute("singleUser") != null){
SingleUser singleUser = (SingleUser) request.getSession().getAttribute("singleUser");
logInfo.setSearchUser(StringUtils.getNotNullString(singleUser.getUsername()));
logInfo.setSearchDepartment(StringUtils.getNotNullString(singleUser.getDepartment()));
logInfo.setSearchTel(StringUtils.getNotNullString(singleUser.getTel()));
}
//return logInfo;
}
}
文章转载自 http://www.javalearns.com/Html/?1561.html
更多Java学习文章请访问 Java免费学习网 http://www.javalearns.com