田源
2025-01-13 03e602bbaee807c42a22df05f1f00c558ffe9fa0
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package com.vci.server.framework.delegate;
 
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import com.vci.common.utility.ObjectUtility;
import com.vci.corba.common.VCIError;
import com.vci.corba.framework.data.LogInfo;
import com.vci.corba.framework.data.LogPeriodInfo;
import com.vci.corba.framework.data.SystemCfgInfo;
import com.vci.corba.common.data.UserEntityInfo;
import com.vci.corba.framework.data.UserInfo;
import com.vci.server.base.delegate.UserEntityDelegate;
import com.vci.server.framework.systemConfig.SystemCfg;
import com.vci.server.framework.systemConfig.log.Log;
import com.vci.server.framework.systemConfig.log.LogPeriod;
import com.vci.server.framework.systemConfig.log.LogService;
 
public class LogManagementDelegate {
 
    /**
     * 在初始化日志模块时,检查是否配置了自动删除
     * @return true表示设置了自动删除
     * @throws VCIError
     */
    public boolean getIsAutoDelete() throws VCIError {
        boolean res = false;
        try {
            LogService logSrv = new LogService();
            res = logSrv.getIsAutoDelete();
        } catch (Exception e) {
            e.printStackTrace();
            throw new VCIError("140101", new String[]{});//获取日志删除配置出错,请重试!
        }
        return res ;
    }
    
    /**
     * 初始化日志模块时,获取保存期限和备份期限下拉框列表
     * @return 配置好的期限值的集合
     * @throws VCIError
     */
    public LogPeriodInfo[] getPeriods() throws VCIError {
        LogPeriod[] periods = null;
        LogPeriodInfo[] infos = null;
        try {
            LogService logSrv = new LogService();
            periods = logSrv.getPeriods();
            if(periods!=null) {
                infos = new LogPeriodInfo[periods.length];
                for(int i=0;i<periods.length;i++) {
                    infos[i] = changePeriodObjToInfo(periods[i]);
                }
            } else {
                infos = new LogPeriodInfo[0];
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new VCIError("140102", new String[]{});//获取日志配置期限出错,请重试!
        }
        return  infos;
    }
    
    /**
     * 获取日志页面显示条数
     * @return
     * @throws VCIError
     */
    public int getPageSize() throws VCIError {
        int pageSize = 0;
        try {
            LogService logSrv = new LogService();
            pageSize = logSrv.getPageSize();
        } catch (Exception e) {
            e.printStackTrace();
            throw new VCIError("140103", new String[]{});//获取日志页面显示条数出错,请重试!
        }
        return pageSize;
    }
    
    /**
     * 获取当前查询日志总数
     * @param sql 带条件的查询语句
     * @return
     * @throws VCIError
     */
    public long getSumLogRows(String sql) throws VCIError {
        long sumRows = 0;
        try {
            LogService logSrv = new LogService();
            sumRows = logSrv.getSumLogRows(sql);
        }catch (Exception e) {
            e.printStackTrace();
            throw new VCIError("140104", new String[]{});//获取本次查询日志总数出错,请重试!
        }
        return sumRows ;
    }
    
    /**
     * 获取全部日志信息
     * @return
     */
    public LogInfo[] fetchLogInfo(int pageNo,int pageSize,String sql) throws VCIError{
        LogInfo[] logInfos = null;
        try {
            LogService logSrv = new LogService();
            List<Log> list = logSrv.getLogList(pageNo, pageSize,sql);
            logInfos = new LogInfo[list.size()];
            Set<String> userIds = new HashSet<String>();
            for(int i = 0;i < list.size();i++){
                logInfos[i] = this.changeLogToLogInfo(list.get(i));
                
                if (!userIds.contains(logInfos[i].username))
                    userIds.add(logInfos[i].username);
            }
            
            if(userIds.size()>0){
                UserInfo[] userInfos = new RightManagementDelegate().fetchUserInfoByNames(userIds.toArray(new String[0]));
                Map<String,String> userInfoMap = new HashMap<String,String>();
                if(userInfos.length > 0 ){
                    for(int i = 0 ; i < userInfos.length; i ++){
                        UserInfo user = userInfos[i];
                        userInfoMap.put(user.userName, user.trueName);
                    }
                }
                for(int i = 0 ; i < logInfos.length ; i ++){
                    String userId  = logInfos[i].username;
                    if(userInfoMap.containsKey(userId)){
                        logInfos[i].truename = userInfoMap.get(userId);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new VCIError("140105", new String[]{});//查询日志出错,请重试!
        }
        return logInfos;
    }
    
    /**
     * 根据查询条件获取日志信息
     * <p>Description: </p>
     * 
     * @author Administrator
     * @time 2013-1-2
     * @param pageNo
     * @param pageSize
     * @param sql
     * @return
     * @throws VCIError
     */
    public LogInfo[] getLogListByContion(int pageNo,int pageSize,String sql) throws VCIError{
        LogInfo[] logInfos = null;
        try {
            LogService logSrv = new LogService();
            List<Log> list = logSrv.getLogListByContion(pageNo, pageSize,sql);
            logInfos = new LogInfo[list.size()];
            Set<String> userIds = new HashSet<String>();
            for(int i = 0;i < list.size();i++){
                logInfos[i] = this.changeLogToLogInfo(list.get(i));
                userIds.add(logInfos[i].username);
            }
            if(userIds.size()>0){
                UserInfo[] userInfos = new RightManagementDelegate().fetchUserInfoByNames(userIds.toArray(new String[0]));
                Map<String,String> userInfoMap = new HashMap<String,String>();
                if(userInfos.length > 0 ){
                    for(int i = 0 ; i < userInfos.length; i ++){
                        UserInfo user = userInfos[i];
                        userInfoMap.put(user.userName, user.trueName);
                    }
                }
                for(int i = 0 ; i < logInfos.length ; i ++){
                    String userId  = logInfos[i].username;
                    if(userInfoMap.containsKey(userId)){
                        logInfos[i].truename = userInfoMap.get(userId);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new VCIError("140105", new String[]{});//查询日志出错,请重试!
        }
        return logInfos;
    }
    
    /**
     * 保存日志保存/备份期限
     * @param info 系统配置表的对象
     * @return
     */
    public boolean savePeriod(SystemCfgInfo info,UserEntityInfo userEntityInfo) throws VCIError{
        boolean res = false;
        String id = ObjectUtility.getNewObjectID36();
        try {
            LogService logSrv = new LogService();
            info.id = id;
            UserEntityDelegate.setUserEntityToService(logSrv, userEntityInfo);
            res = logSrv.savePeriod(changeSysCfgInfoToObj(info));
        } catch (Exception e) {
            e.printStackTrace();
            throw new VCIError("140106", new String[]{});//保存期限出错,请重试!
        }
        return res ;
    }
    
    /**
     * 获取保存/备份期限值
     * @param type
     * @return
     * @throws VCIError
     */
    public int getCurPeriod(String type) throws VCIError{
        int period = 0;
        try {
            LogService logSrv = new LogService();
            period = logSrv.getCurPeriod(type);
        } catch (Exception e) {
            e.printStackTrace();
            throw new VCIError("140107", new String[]{});//获取期限数值出错,请重试!
        }
        return period;
    }
    
    public boolean deleteLog(String deleteDate) throws VCIError {
        boolean res = false;
        try {
            LogService logSrv = new LogService();
            res = logSrv.deleteLog(deleteDate);
        } catch (Exception e) {
            e.printStackTrace();
            throw new VCIError("140108", new String[]{});//删除日志出错,请重试!
        }
        return res;
    }
    
    
 
    
    /**
     * period
     * 服务器端对象转成CORBA对象
     * @param obj
     * @return
     */
    private LogPeriodInfo changePeriodObjToInfo(LogPeriod obj) {
        LogPeriodInfo info = new LogPeriodInfo();
        info.code = obj.getCode()==null?"":obj.getCode();
        info.value = obj.getValue()==null?"":obj.getValue();
        return info;
    }
    
    /**
     * 系统配置
     * CORBA对象转成服务器端对象
     * @param info
     * @return
     */
    private SystemCfg changeSysCfgInfoToObj(SystemCfgInfo info) {
        SystemCfg sys = new SystemCfg();
        sys.setId(info.id);
        sys.setName(info.name);
        sys.setValue(info.value);
        return sys;
    }
    
    /**
     * Log
     * 服务器端对象转成CORBA对象
     * @param obj
     * @return
     */
    public LogInfo changeLogToLogInfo(Log obj){
        LogInfo info = new LogInfo();
        info.puid = obj.getId() == null?"":obj.getId();
        info.type = obj.getType() == null?"":obj.getType();
        info.date = obj.getDate() == null?"":new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(obj.getDate());
        info.username = obj.getUsername() == null?"":obj.getUsername();
        info.userIp = obj.getUserIp() == null?"":obj.getUserIp();
        info.entityDesc = obj.getEntityDesc() == null?"":obj.getEntityDesc();
        info.property = obj.getProperty() == null?"":obj.getProperty();
        info.previousVal = obj.getPreviousVal() == null?"":obj.getPreviousVal();
        info.newVal = obj.getNewVal() == null?"":obj.getNewVal();
        info.result = obj.getResult() == null?"":obj.getResult();
        info.moduleName = obj.getModule() == null?"":obj.getModule();
        info.logType = obj.getLogType() == null ? "" : obj.getLogType();
        return info;
    }
}