yuxc
2025-01-15 2bea732496b4f5051233ed94e206160992351596
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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;
    }
}