田源
2024-03-07 4b4083fd73dc27ece42f4835483565eef0e4f608
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
package com.vci.server.framework.systemConfig.log;
 
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;
 
import org.hibernate.HibernateException;
import org.hibernate.criterion.Order;
import org.hibernate.type.Type;
 
import com.vci.common.resource.CommonProperties;
import com.vci.corba.common.data.VCIInvocationInfo;
import com.vci.server.base.persistence.dao.BaseService;
import com.vci.server.base.persistence.dao.HibernateCallback;
import com.vci.server.base.persistence.dao.HibernateCallbackExt;
import com.vci.server.base.persistence.dao.HibernateSessionFactory;
import com.vci.server.base.persistence.dao.HibernateTemplate;
import com.vci.server.framework.systemConfig.SystemCfg;
import com.vci.server.framework.systemConfig.SystemCfgDAOImpl;
 
/**
 * 日志模块提供服务的Service
 * @author xiong fei 
 * @time 2011-06-16
 */
public class LogService extends BaseService {
    
    private final String LOG_DELETE_AUTO = "log.delete.auto";//配置日志是否进行自动删除
    private final String LOG_SAVE_PERIODS = "log.save.periods";//配置的日志保存期限下拉框的值
    private final String LOG_QUERY_PAGESIZE = "log.query.pagesize";//配置的日志页面显示条数
    
    public LogService() {
        AlterTable();
    }
    
    /*
     * 日志表增加日志内容字段
     */
    private void AlterTable() {
        String sql = "SELECT COUNT(1) as COLNUM FROM USER_TAB_COLUMNS WHERE TABLE_NAME='PLLOG' AND COLUMN_NAME='PLCONTENT'";
        Connection connection = HibernateSessionFactory.getSessionConnection();
        PreparedStatement pst = null;
        ResultSet rs = null;
        
        try {
            pst = connection.prepareStatement(sql);
            rs = pst.executeQuery();
 
            int count = 0;
            while(rs.next()){
                count = rs.getInt("COLNUM");
            }
            rs.close();
            rs = null;
            pst.close();
            pst = null;
            
            if (count == 0) {
                sql = "ALTER TABLE PLLOG ADD PLCONTENT VARCHAR2(1000)";
                pst = connection.prepareStatement(sql);
                pst.execute();
            }
            
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                
                if(pst != null) {
                    pst.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }
    
    /**
     * 获取日志删除配置
     * @return true表示自动删除,false表示手动删除
     */
    public boolean getIsAutoDelete() {
        boolean res = false;
        String isAuto = CommonProperties.getStringProperty(LOG_DELETE_AUTO);
        if(isAuto.equals("Y")){
            res = true;
        }
        return res;
    }
    
    /**
     * 获取配置好的保存和备份期限的下拉框的值
     * @return
     */
    public LogPeriod[] getPeriods() {
        String periods = CommonProperties.getStringProperty(LOG_SAVE_PERIODS);
        String[] period = periods.split("::");
        LogPeriod[] res = new LogPeriod[period.length];
        for(int i = 0;i<period.length;i++){
            String[] temp = period[i].split("#");
            LogPeriod obj = new LogPeriod();
            obj.setCode(temp[0]);
            obj.setValue(temp[1]);
            res[i] = obj;
        }
        return res;
    }
    
    /**
     * 获取配置好的日志页面显示条数
     * @return
     */
    public int getPageSize() {
        String pageSize = CommonProperties.getStringProperty(LOG_QUERY_PAGESIZE);
        if(pageSize == null){//如果获取失败,返回0
            return 0;
        } else {
            return Integer.parseInt(pageSize);
        }
    }
    
    /**
     * 保存日志
     * <p>Description:保存日志 </p>
     * 
     * @author xf
     * @time 2012-6-1
     */
    public void saveLog(final Log log){
        new HibernateTemplate().run(new HibernateCallback() {
            @Override
            public Object execute() throws HibernateException {
                LogDAOImpl impl = new LogDAOImpl();
                impl.save(log);
                return true;
            }
        });
    }
    
    /**
     * 获取本次查询日志总数
     * @param sql 前台条件查询的SQL
     * @return 日志总数
     */
    public long getSumLogRows(final String sql) {
        return (Long)new HibernateTemplate().run(new HibernateCallback() {
            
            public Object execute() throws HibernateException {
                LogDAOImpl impl = new LogDAOImpl();
                return impl.getCountForLog(sql);
            }
        });
    }
    
    /**
     * 使用SQL获取日志的列表
     * @param sql sql语句
     * @return 日志的对象
     */
    public List<Log> getLogListBySql(final String sql){
        return (List)new HibernateTemplate().runExt(new HibernateCallbackExt() {
                    
                    public Object execute(Connection connection) throws HibernateException,
                            SQLException {
                        LogDAOImpl impl = new LogDAOImpl();
                        return impl.findEntites("from Log where " + sql + " order by date desc ",new String[0]);
                    }
                });
    }
    /**
     * 分页查询,获取本次查询的日志集合
     * @param pageNO 当前页数
     * @param PageSize 页面大小
     * @return
     */
    public List getLogList(final int pageNo,final int pageSize,final String sql){
        return (List)new HibernateTemplate().runExt(new HibernateCallbackExt() {
            
            public Object execute(Connection connection) throws HibernateException,
                    SQLException {
                LogDAOImpl impl = new LogDAOImpl();
                Order order = Order.desc("date");//按时间排序
                return impl.findList(pageNo, pageSize,sql,order);
            }
        });
    }
    
    public List getLogListByContion(final int pageNo,final int pageSize,final String sql){
        return (List)new HibernateTemplate().run(new HibernateCallback() {
            @Override
            public Object execute() throws HibernateException {
                int V_PAGE_ROWNUM_START = (pageNo - 1) * pageSize;
                int V_PAGE_ROWNUM_END = (pageNo ) * pageSize;
                
                LogDAOImpl impl = new LogDAOImpl();
                StringBuffer sb = new StringBuffer();
                sb.append(" SELECT * FROM ( ");
                sb.append("   SELECT ROW_.*,ROWNUM RN FROM ( ");
                sb.append(" select * from pllog P WHERE ");
                sb.append(sql);
                sb.append(" ORDER BY PLDATE DESC ");
                sb.append("  ) ROW_ ");
                sb.append(" )WHERE RN <= ");
                sb.append(V_PAGE_ROWNUM_END);
                sb.append(" AND RN > ");
                sb.append(V_PAGE_ROWNUM_START);
                return impl.findEntites(sb.toString(), new String[]{}, "P", Log.class);
            }
        });
    }
    
    
    /**
     * 通过SQL语句执行日志删除,效率更高
     * @param sql
     * @return
     */
    public boolean deleteLogBySql(final String sql, final long curPeriod){
        return (Boolean)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                LogDAOImpl impl = new LogDAOImpl();
                impl.createSQLQuery(sql, new String[0]);
                
                String result = "删除 " + curPeriod + " 个月之前的日志 ";
                writeLogDeleteLog("logAutoDeleter", getServerIP(), result, impl);
                
                return true;
            }
        });
    }
    
    private String getServerIP(){
        String ip = "127.0.0.1";
        try {
            ip = java.net.InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return ip;
    }
    
    /**
     * 保存日志保存/备份期限
     * @param sysCfg 系统配置表的对象
     * @return
     */
    public boolean savePeriod(final SystemCfg sysCfg) {
        return (Boolean)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                SystemCfgDAOImpl impl = new SystemCfgDAOImpl();
                String sql = "from SystemCfg s where s.name = '"+sysCfg.getName()+"'";
                SystemCfg temp = impl.findEntity(sql);
                if(temp == null) {//如果不存在配置,插入
                    sysCfg.setUserEntity(userEntity);
                    impl.save(sysCfg);
                } else {//如果存在配置,更新
                    temp.setName(sysCfg.getName());
                    temp.setValue(sysCfg.getValue());
                    temp.setUserEntity(userEntity);
                    impl.update(temp);
                }
                return true;
            }
        });
    }
    
    /**
     * 获取保存/备份期限值
     * @param type
     * @return
     */
    public int getCurPeriod(final String type) {
        return (Integer)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                SystemCfgDAOImpl impl = new SystemCfgDAOImpl();
                SystemCfg sysCfg = new SystemCfg();
                int curPeriod = 0;
                String sql = " from SystemCfg s where s.name ='"+type+"'";
                sysCfg = impl.findEntity(sql);
                if(sysCfg != null){
                    curPeriod = Integer.parseInt(sysCfg.getValue());
                } 
                return curPeriod;
            }
        });
    }
    
    /**
     * 手动删除日志
     * @param deleteDate 所选日期,将删除该日期之前的日志
     * @return
     */
    public boolean deleteLog(final String deleteDate) {
        return (Boolean)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                LogDAOImpl impl = new LogDAOImpl();
                String delDate = deleteDate;
                String sql = "delete from pllog where pltype<>'删除' and plmodule<>'日志管理' and pllogtype<>'一般操作' and pldate <= to_date('"+delDate+"','yyyy-MM-dd') ";
                
                if(deleteDate.contains("##")){
                    String[] vvs = deleteDate.split("##");
                    delDate = vvs[0];
                    sql = "delete from pllog where pltype<>'删除' and plmodule<>'日志管理' and pllogtype<>'一般操作'  and pldate <= to_date('"+delDate+"','yyyy-MM-dd') and " + vvs[1];
                }
                
                impl.createSQLQuery(sql);
 
                VCIInvocationInfo vcii = HibernateSessionFactory.getVciSessionInfo();
                String result = "删除 " + delDate + " 之前的日志 ";
                writeLogDeleteLog(vcii.userName, vcii.clientIPInfo, result, impl);
                return true;
            }
        });
    }
    
    
    /**
     * 记录日志删除的日志
     */
    public void writeLogDeleteLog(String userName, String ip, String result, LogDAOImpl impl){
        if(impl == null){
            impl = new LogDAOImpl();
        }
        String sql = "insert into pllog(ploid, pluser, plip, plresult, pldate, pltype, plmodule, pllogtype) " +
                "values(" +
                "get_uuid()," +
                "'" + userName + "'," +
                "'" + ip + "'," + 
                "'" + result + "'," + 
                "sysdate," + 
                "'" + "删除" + "'," + 
                "'" + "日志管理" + "'," + 
                "'" + "一般操作" + "'" + 
                ")";
        impl.createSQLQuery(sql);
    }
//    /**
//     * 删除日志
//     * @param list
//     * @return
//     */
//    public boolean deleteLog(final List<Log> list) {
//        return (Boolean)new HibernateTemplate().run(new HibernateCallback() {
//            public Object execute() throws HibernateException {        
//                LogDAOImpl impl = new LogDAOImpl();
//                for(Log entity:list){
//                    impl.delete(entity);
//                }
//                return true;
//            }
//        });
//    }
//    
    /**
     * 根据模块的源路径返回模块的别名
     * @param modulePath
     * @return
     */
    public String getModuleAlias(final String modulePath){
        return (String)new HibernateTemplate().run(new HibernateCallback() {
            @Override
            public Object execute() throws HibernateException {
                String res = "";
                LogDAOImpl impl = new LogDAOImpl();
                String sql = "SELECT T.PLALIASNAME FROM PLFUNCTION T WHERE T.PLRESOURCEC = ?";
                Object[] values = { modulePath };
                LinkedHashMap<String, org.hibernate.type.Type> map = new LinkedHashMap<String, Type>();
                map.put("PLALIASNAME", org.hibernate.type.StringType.INSTANCE);
                List<String> list = impl.findEntitesBySQL(sql, values, map);
                if(list.size() != 0){
//                    Object[] data = list.get(0);
//                    if(data != null && data.length > 0){
//                        res = (String) data[0];
//                    }
                    res = list.get(0);
                    if(res == null || "null".equals(res)) {
                        res = "";
                    }
                }
                return res;
            }
        });
    }
    
    /**
     * 根据模块的源路径返回角色名称
     * 
     */
    /*public String getRoleName(final String modulePath,final String userName){
        return (String)new HibernateTemplate().run(new HibernateCallback() {
            @Override
            public Object execute() throws HibernateException {
                String res = "";
                LogDAOImpl impl = new LogDAOImpl();
                StringBuilder logSql = new StringBuilder();
                logSql.append(" select pr.plname from plrole pr where pr.pluid in ( ");
                logSql.append("  select r.plroleoid  from Plroleright r where r.plfuncoid in ( ");
                logSql.append("   select pf.ploid  from plfunction pf  where pf.plresourcec = ");
                logSql.append(" ? ");
                logSql.append(" )");
                logSql.append("  )");
                logSql.append(" intersect ");
                logSql.append(" select pr.plname from plrole pr where pr.pluid in ( ");
                logSql.append("    select ur.plroleuid from pluserrole ur where ur.pluseruid in ( ");
                logSql.append("           select pu.pluid from pluser pu where pu.plusername= ");
                logSql.append(" ? ");
                logSql.append(" ) ");
                logSql.append(" ) ");
                Object[] values = { modulePath, userName};
                LinkedHashMap<String, org.hibernate.type.Type> map = new LinkedHashMap<String, Type>();
                List<String> list = impl.findEntitesBySQL(logSql.toString(), values, map);
                if(list.size() != 0){
                    res = list.get(0);
                    if(res == null || "null".equals(res)) {
                        res = "";
                    }
                }
                return res;
            }
        });
    }*/
 
    public String getRoleName(final String modulePath,final String userName){
        return (String)new HibernateTemplate().runExt(new HibernateCallbackExt() {
            @Override
            public Object execute(Connection paramConnection)
                    throws HibernateException, SQLException {
                String res = "";
                LogDAOImpl impl = new LogDAOImpl();
                StringBuilder logSql = new StringBuilder();
                logSql.append(" select pr.plname from plrole pr where pr.pluid in ( ");
                logSql.append("  select r.plroleoid  from Plroleright r where r.plfuncoid in ( ");
                logSql.append("   select pf.ploid  from plfunction pf  where pf.plresourcec = ");
                logSql.append(" ? ");
                logSql.append(" )");
                logSql.append("  )");
                logSql.append(" intersect ");
                logSql.append(" select pr.plname from plrole pr where pr.pluid in ( ");
                logSql.append("    select ur.plroleuid from pluserrole ur where ur.pluseruid in ( ");
                logSql.append("           select pu.pluid from pluser pu where pu.plusername= ");
                logSql.append(" ? ");
                logSql.append(" ) ");
                logSql.append(" ) ");
                Object[] values = { modulePath, userName};
                LinkedHashMap<String, org.hibernate.type.Type> map = new LinkedHashMap<String, Type>();
                List<String> list = impl.findEntitesBySQL(logSql.toString(), values, map);
                if(list.size() != 0){
                    res = list.get(0);
                }else{
                    res =getMessage(paramConnection,userName);
                }
                return res;
            }
        });
    }
    
    public String getMessage(Connection paramConnection,String userName){
        String res = "";
        StringBuilder logSql = new StringBuilder();
        logSql.append(" select pr.plname ,pr.pltype from plrole pr where pr.pluid in ( ");
        logSql.append("    select ur.plroleuid from pluserrole ur where ur.pluseruid in ( ");
        logSql.append("           select pu.pluid from pluser pu where pu.plusername= ");
        logSql.append(" '"+userName+"' ");
        logSql.append(" ) ");
        logSql.append(" ) ");
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = paramConnection.prepareStatement(logSql.toString());
            rs = pstmt.executeQuery();
            LinkedHashMap<String, String> value = new LinkedHashMap<String, String>();
            while(rs.next()){
                value.put(rs.getString(2),rs.getString(1));
            }
            if(value.get("1")!=null){
                res = value.get("1");
            }else{
                res = value.get("2");
            }
            
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                
                if(pstmt != null) {
                    pstmt.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return res;
    }
    
}