package com.vci.server.framework.systemConfig.log; import java.sql.SQLException; import java.sql.Statement; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.lang3.StringUtils; import org.hibernate.HibernateException; import org.hibernate.Session; import com.vci.common.log.LogType; import com.vci.common.objects.UserEntity; import com.vci.common.utility.ObjectUtility; import com.vci.corba.common.VCIError; import com.vci.corba.common.data.VCIInvocationInfo; import com.vci.corba.common.data.UserEntityInfo; import com.vci.server.base.delegate.UserEntityDelegate; import com.vci.server.base.persistence.dao.HibernateSessionFactory; public class LogRecordUtil { private static LogService logSrv = new LogService(); /** * 日志记录 * @param userEntity 用户对象 * @param optType 操作类别 * @param resContent 操作结构 * @param logType 日志类型 * @throws VCIError */ public static void writeLog(UserEntity userEntity, String optType, String result, String content, LogType logType,String dataObjId) throws VCIError{ try { Log log = new Log(); log.setId(ObjectUtility.getNewObjectID36()); log.setDate(new Date()); log.setUserIp(userEntity.getIp()); VCIInvocationInfo invInfo = HibernateSessionFactory.getVciSessionInfo(); String user = userEntity.getUserName(); if (user == null) { user = invInfo.userName; } if (StringUtils.isEmpty(user)) user = "loguser"; log.setUsername(user); log.setResult(result); log.setContent(content); log.setType(optType); String module = userEntity.getModule(); if (StringUtils.isEmpty(module)) module = "log"; String moduleAlias = logSrv.getModuleAlias(module); if(moduleAlias.equals("")){ moduleAlias = module; } log.setModule(moduleAlias); log.setLogType(getLogTypeString(logType)); log.setObjId(dataObjId); String roleName = "";//logSrv.getRoleName(userEntity.getModule(),userEntity.getUserName()); log.setRoleName(roleName); logSrv.saveLog(log); } catch (Exception e) { e.printStackTrace(); throw new VCIError("140109", new String[0]); } } public static String getRoleName(UserEntity userEntity) { String roleName = logSrv.getRoleName(userEntity.getModule(),userEntity.getUserName()); return roleName; } public static String getModuleAlias(UserEntity userEntity) { String moduleAlias = logSrv.getModuleAlias(userEntity.getModule()); return moduleAlias; } public static String getLogSql(UserEntity userEntity, String optType, String result, String content, LogType logType,String dataObjId, String roleName, String moduleAlias) { Date currentDAte = new Date(); String fmtstr = "yyyy-mm-dd hh24:mi:ss"; SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = formatter.format(currentDAte); StringBuffer sb = new StringBuffer(); sb.append("insert into pllog (PLOID,PLUSER,PLIP,PLRESULT,PLCONTENT,PLDATE,PLTYPE,PLMODULE,PLLOGTYPE,PLOBJID,PLROLENAME) values(") .append("'").append(ObjectUtility.getNewObjectID36()).append("'") .append(",'").append(userEntity.getUserName()).append("'") .append(",'").append(userEntity.getIp()).append("'") .append(",'").append(result).append("'") .append(",'").append(content).append("'") .append(",to_date('").append(dateString).append("','").append(fmtstr).append("')") .append(",'").append(optType).append("'") .append(",'").append(moduleAlias).append("'") .append(",'").append(getLogTypeString(logType)).append("'") .append(",'").append(dataObjId).append("'") .append(",'").append(roleName).append("'") .append(")"); return sb.toString(); } public static void batchSaveLog(String[] sqls) throws HibernateException, SQLException { Session session = HibernateSessionFactory.getSession(); Statement st = session.connection().createStatement(); for (int i = 0; i < sqls.length; i++) { st.addBatch(sqls[i]); if ((i + 1) % 200 == 0) { st.executeBatch(); } } if (sqls.length % 200 != 0) { st.executeBatch(); } if (st != null) { st.close(); st = null; } } /*public static void writeLog(UserEntity userEntity, String optType, String resContent, LogType logType){ try{ Log log = new Log(); log.setId(ObjectUtility.getNewObjectID36()); log.setDate(new Date()); log.setUserIp(userEntity.getIp()); log.setUsername(userEntity.getUserName()); log.setResult(resContent); log.setType(optType); String moduleAlias = logSrv.getModuleAlias(userEntity.getModule()); if(moduleAlias.equals("")){ moduleAlias = userEntity.getModule(); } log.setModule(moduleAlias); log.setLogType(getLogTypeString(logType)); String roleName = logSrv.getRoleName(userEntity.getModule(),userEntity.getUserName()); log.setRoleName(roleName); logSrv.saveLog(log); }catch (Exception e) { e.printStackTrace(); } }*/ /** * 日志记录 * @param userEntity 用户对象 * @param optType 操作类别 * @param resContent 操作结构 * @param logType 日志类型 * @throws VCIError */ public static void writeLog(UserEntityInfo userEntityInfo, String optType, String result, String content, LogType logType,String objId) throws VCIError{ writeLog(UserEntityDelegate.changeUserEntityInfoToEntity(userEntityInfo), optType, result, content, logType,objId); } /** * 操作类型转换:将系统传递类型转换为普通文字描述 * * @param logType * @return */ private static String getLogTypeString(LogType logType){ String res = ""; if(logType == LogType.Login) { res = "登录"; } else if(logType == LogType.Logout) { res = "登出"; } else if(logType == LogType.GrantPrivileges) { res = "授权"; } else if(logType == LogType.GeneralOperation) { res = "一般操作"; } else if(logType == LogType.Integration) { res = "集成应用"; } else { res = logType.getLabel(); } return res; } }