package com.vci.client.log.ui;
|
|
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionListener;
|
import java.io.File;
|
import java.util.LinkedHashMap;
|
|
import javax.swing.JFileChooser;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import com.vci.client.LogonApplication;
|
import com.vci.client.common.objects.LogObject;
|
import com.vci.client.log.delegate.LogManagementClientDelegate;
|
import com.vci.client.ui.exception.VCIException;
|
import com.vci.client.ui.swing.VCIOptionPane;
|
import com.vci.client.ui.swing.VCISwingUtil;
|
import com.vci.client.utils.excel.ExcelFileOperation;
|
|
public class LogManagementPanelActionListener implements ActionListener {
|
|
private LinkedHashMap<String, Runnable> actionMaps = new LinkedHashMap<String, Runnable>();
|
private LogManagementPanel owner = null;
|
|
public LogManagementPanelActionListener(LogManagementPanel owner) {
|
this.owner = owner;
|
initActionMaps();
|
}
|
|
private void initActionMaps() {
|
actionMaps.put("query", new Runnable() { public void run() {
|
queryAction_Performed();
|
}});
|
actionMaps.put("export", new Runnable() { public void run() {
|
exportAction_Performed();
|
}});
|
}
|
|
@Override
|
public void actionPerformed(ActionEvent e) {
|
String key = e.getActionCommand();
|
if(actionMaps.containsKey(key)){
|
actionMaps.get(key).run();
|
}
|
}
|
|
private void queryAction_Performed() {
|
this.owner.tablePanel.setPageIndex(1);
|
this.owner.tablePanel.refreshTableData();
|
}
|
|
private void exportAction_Performed() {
|
try{
|
LogManagementClientDelegate logDel = new LogManagementClientDelegate(LogonApplication.getUserEntityObject());
|
LogObject[] objs = logDel.fetchLogInfo(1,100000,this.owner.getSQL());//取出条件查询的结果集
|
String[][] logString = new String[objs.length + 1][8];
|
for(int i = 0;i<this.owner.tablePanel.getTableModel().getColumnCount() - 2;i++){//初始化第一行
|
logString[0][i] = this.owner.tablePanel.getTableModel().getColumnName(i + 2);
|
}
|
//获取表格中勾选的数据序号数组
|
int [] orderNum = this.owner.tablePanel.getSelectedRowIndexs();
|
int m = 0;
|
if(orderNum.length==0){
|
for(int j = 0;j<objs.length;j++){
|
LogObject obj = objs[j];
|
/* StringBuffer desc = new StringBuffer();
|
desc.append(obj.getType()).append(" ").append(obj.getResult());*/
|
logString[j+1][0]=obj.getUsername();//用户名
|
logString[j+1][1]=obj.getTruename();//姓名
|
logString[j+1][2]=obj.getUserIp();//用户IP
|
logString[j+1][3]=obj.getModule();//模块
|
logString[j+1][4]=obj.getType();//操作
|
logString[j+1][5]=obj.getDate();//时间
|
String result = "操作成功";
|
String memo = obj.getResult();
|
if(StringUtils.isNotBlank(memo)){
|
boolean hasResult =false;
|
if(memo.startsWith("操作") && memo.length()>4){
|
result = memo.substring(0,4);
|
hasResult = true;
|
}
|
if((memo.startsWith("登入") || memo.startsWith("登出"))&& memo.length()>4){
|
result = memo.substring(0,4);
|
hasResult = true;
|
}
|
if(memo.length()>5 && hasResult){
|
memo = memo.substring(5);
|
}else{
|
//memo = "";
|
}
|
}
|
logString[j+1][6]=result;//结果
|
logString[j+1][7]=memo;//描述
|
}
|
}else{
|
for(int j = 0;j<objs.length;j++){
|
LogObject obj = objs[j];
|
if(m==orderNum.length){
|
break;
|
}
|
for(int k = m;k<orderNum.length;k++){
|
if(orderNum[k]>j){
|
break;
|
}
|
if(orderNum[k]==j){
|
logString[m+1][0]=obj.getUsername();//用户名
|
logString[m+1][1]=obj.getTruename();//姓名
|
logString[m+1][2]=obj.getUserIp();//用户IP
|
logString[m+1][3]=obj.getModule();//模块
|
logString[m+1][4]=obj.getType();//操作
|
logString[m+1][5]=obj.getDate();//时间
|
String result = "操作成功";
|
String memo = obj.getResult();
|
if(StringUtils.isNotBlank(memo)){
|
boolean hasResult =false;
|
if(memo.startsWith("操作") && memo.length()>4){
|
result = memo.substring(0,4);
|
hasResult = true;
|
}
|
if((memo.startsWith("登入") || memo.startsWith("登出"))&& memo.length()>4){
|
result = memo.substring(0,4);
|
hasResult = true;
|
}
|
if(memo.length()>5 && hasResult){
|
memo = memo.substring(5);
|
}else{
|
//memo = "";
|
}
|
}
|
logString[m+1+1][6]=result;//结果
|
logString[m+1+1][7]=memo;//描述
|
m++;
|
break;
|
}
|
}
|
}
|
}
|
boolean exportSuccess = exportExcel(logString);
|
if(exportSuccess){
|
VCIOptionPane.showMessage(LogonApplication.frame,"导出日志文件成功!");
|
}
|
} catch (VCIException e1) {
|
VCIOptionPane.showError(LogonApplication.frame, "RMIPFramework", e1);
|
}
|
}
|
|
/**
|
* 导出日志
|
* @param logString
|
* @return
|
*/
|
private boolean exportExcel(String[][] logString){
|
String EXPORTFILE = "日志信息导出";
|
File f = this.getFile();
|
if (f == null) return false;
|
/**判断导出的文件是否被使用的校验,如果文件已经打开,不允许导入**/
|
if(!f.renameTo(f) && f.exists()) {
|
VCIOptionPane.showMessageDialog(this.owner, "文件已经被使用,请重新选择!");
|
if(exportExcel(logString)){
|
return true;
|
}
|
}
|
new ExcelFileOperation().writeExcelFileInfo(f.getPath(), EXPORTFILE,logString);
|
return true;
|
}
|
|
/**
|
* 获取选择导入的文件
|
*
|
* @return
|
*/
|
private File getFile() {
|
File f = null;
|
JFileChooser fileDialog = new JFileChooser("选择文件");
|
@SuppressWarnings("static-access")
|
String fileURL = new VCISwingUtil().getExcelFileURL(fileDialog, true, "");
|
if (fileURL == null) return null;
|
System.out.println(fileURL);
|
f = new File(fileURL);
|
return f;
|
}
|
|
}
|