package com.vci.client.framework.systemConfig.stafforgmanage.listeners; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import javax.swing.JPanel; import com.vci.client.ClientSession; import com.vci.client.LogonApplication; import com.vci.client.common.objects.UserEntityObject; import com.vci.client.framework.delegate.UserEntityClientDelegate; import com.vci.client.framework.systemConfig.stafforgmanage.MachSecurityImportDialog; import com.vci.client.ui.process.QANProcessBar; import com.vci.client.ui.process.QANProcessBarFrame; import com.vci.client.ui.swing.VCIOptionPane; import com.vci.client.ui.swing.VCISwingUtil; import com.vci.client.ui.swing.components.VCIJOptionPane; import com.vci.client.utils.excel.ExcelDocumentUtils; import com.vci.client.utils.excel.SheetDataSet; import com.vci.common.exception.VciException; import com.vci.common.utility.ObjectUtility; import com.vci.corba.common.VCIError; import com.vci.corba.framework.data.MachSecurityInfo; import com.vci.corba.common.data.UserEntityInfo; public class MachSecurityImportActionListener extends JPanel implements ActionListener { /** * */ private static final long serialVersionUID = 1L; /** * 父窗体 */ private MachSecurityImportDialog ownerDialog = null; /** * 所有Roles信息。 */ private ArrayList fileDatas = new ArrayList(); private String userName = LogonApplication.getUserEntityObject().getUserName(); public MachSecurityImportActionListener(MachSecurityImportDialog ownerDialog) { this.ownerDialog = ownerDialog; } public MachSecurityImportActionListener() { // TODO Auto-generated constructor stub } @Override public void actionPerformed(ActionEvent e) { String actionCommand = e.getActionCommand(); if (actionCommand.equals(ownerDialog.getBtnSave().getActionCommand())) { LinkedList selectedFiles = this.ownerDialog.getSelectedFiles(); if (selectedFiles == null || selectedFiles.isEmpty()) { VCIOptionPane.showMessageDialog(LogonApplication.frame, "未选择文件!"); return; } fileDatas.clear(); save(); } else if (actionCommand.equals(ownerDialog.getBtnSelect().getActionCommand())) { getFile(); } } /** * 保存对象 */ private void save() { final QANProcessBarFrame frame = new QANProcessBarFrame(); Thread t = new Thread() { public void run() { boolean b = false; frame.setContent("正在进行处理数据,请稍等......"); try { b = importData(frame); } catch (VCIError e) { frame.setProcessBarCancel(true); VCIOptionPane.showMessageDialog(LogonApplication.frame, e.code); } catch (VciException e) { } catch (IOException e) { e.printStackTrace(); } finally { frame.setProcessBarCancel(true); if (b) { VCIOptionPane.showMessageDialog(LogonApplication.frame, "导入成功!"); ownerDialog.dispose(); } else { VCIOptionPane.showMessageDialog(LogonApplication.frame, "导入失败!"); } } } }; QANProcessBar bar = new QANProcessBar(t, frame, frame, "导入机器密级数据......", false); bar.setVisible(true); } /** * 导入 * * @param sheetDataSet * @return * @throws IOException * @throws VCIError * @throws VciException * @autor caicong * @data 2014-3-12 */ private boolean importData(QANProcessBarFrame frame) throws VCIError, VciException, IOException { frame.setContent("正在获取机器密级数据......"); frame.setContent("正在收集表单数据......"); boolean isSuccess = collectionDatas(); if (!isSuccess) return false; frame.setContent("正在导入表单人员信息......"); boolean b = importExcelData(); return b; } /** * 收集表单信息。 * * @throws VCIError * @throws IOException */ // add by caill start 2016.1.20 收集导入文件中的数据 private boolean collectionDatas() throws VCIError, IOException { List selectedFiles = this.ownerDialog.getSelectedFiles(); MachSecurityInfo[] msInfos = null; MachSecurityInfo[] oldInfos = ClientSession.getFrameworkService().getAllMachSecurity(); List lstIP = new ArrayList(); for (MachSecurityInfo info : msInfos) { lstIP.add(info.ipAddress); } UserEntityObject user = LogonApplication.getUserEntityObject(); for (File f : selectedFiles) { List sheetDataSets = this.getFileList(f); if (sheetDataSets == null || sheetDataSets.isEmpty()) { continue; } for (SheetDataSet sheet : sheetDataSets) { // sheet不能为空并且必须有除表头外的一条数据 if (sheet != null && sheet.getDataSet() != null && sheet.getDataSet().size() > 1) { List dataSet = sheet.getDataSet(); for (int i = 1; i < dataSet.size(); i++) { String[] data = dataSet.get(i); if (data[0] != null && !data[0].trim().equals("")) continue; // 对于数据库中存在的IP地址,不覆盖不导入 if (lstIP.contains(data[0])) continue; String id = ObjectUtility.getNewObjectID36(); MachSecurityInfo msInfo = new MachSecurityInfo(); msInfo.id = id; msInfo.ipAddress = data[0]; msInfo.name = data[1]; msInfo.secretGrade = Short.parseShort(data[2]); msInfo.desc = data[3]; msInfo.macAddress = ""; msInfo.creator = user.getUserName(); msInfo.createTime = System.currentTimeMillis(); msInfo.modifier = user.getUserName(); msInfo.modifyTime = System.currentTimeMillis(); fileDatas.add(msInfo); } } } } return true; } /** * 导入表单数据 * * @throws VciException */ private boolean importExcelData() throws VCIError { UserEntityObject user = LogonApplication.getUserEntityObject(); UserEntityInfo userInfo = UserEntityClientDelegate.changeUserEntityToInfo(user); return ClientSession.getFrameworkService().batchSaveMachSecurity( fileDatas.toArray(new MachSecurityInfo[0]), userInfo); } /** * 获取选中的文件 * * @return * @autor caicong * @data 2014-3-12 */ @SuppressWarnings({ "deprecation", "unused" }) private LinkedList getSelectedFiles() { Object[] objs = ownerDialog.getSelectedDatas(); LinkedList selectedFiles = new LinkedList(); for (Object obj : objs) { selectedFiles.add((File)obj); } return selectedFiles; } /** * 获取表单数据 * * @param f * @return * @throws IOException * @throws VCIError * @autor caicong * @data 2014-3-11 */ private List getFileList(File f) throws VCIError, IOException { // 获取流 BufferedInputStream fileInputStream = new BufferedInputStream(new FileInputStream(f)); String name = f.getName(); // 获取表list List sheetDataSets = ExcelDocumentUtils.readExcelDocument(name, fileInputStream); return sheetDataSets; } private void getFile() { File f = getRequiredFile(true); if (f == null) return; ArrayList lists = ownerDialog.getLists(); /** 判断导入的文件是否被使用的校验,如果文件已经打开,不允许导入 **/ if (lists.contains(f)) { VCIOptionPane.showMessageDialog(LogonApplication.frame, "文件重复,请重新选择!"); return; } // 将文件保存到table中 lists.add(f); ownerDialog.refreshTableData(); } private File getRequiredFile(boolean isRead) { File files = getExcelFile(); // add by xchao 2014.04.15 begin // 有错误数据要输出时,必须选择有效的excel文件 while (files == null) { return null; } String fileName = files.getAbsolutePath(); // 需要写入时,必须选择一个有效的、可以写入的文件 if (!isRead) { // 如果文件被打开着,则必须选择其它的未打开着的文件 while (files.exists() && !files.renameTo(new File(fileName))) { VCIJOptionPane.showMessageDialog(null, "另一个程序正在使用此文件,进程无法访问,请重新选择!"); // 重新选择文件时,也必须选择有效的excel文件 files = null; while (files == null) { files = getExcelFile(); while (files == null) { return null; } } fileName = files.getAbsolutePath(); } } return files; } public String[] getExcel() { return null; } private File getExcelFile() { File file = null; String filePath = VCISwingUtil.getExcelFileURL(ownerDialog, true, ""); if (filePath != null) { file = new File(filePath); } return file; } }