package com.vci.client.framework.systemConfig.stafforgmanage; 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.Date; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import javax.swing.JPanel; import com.vci.client.LogonApplication; import com.vci.client.common.objects.RoleObject; import com.vci.client.common.objects.UserObject; import com.vci.client.framework.delegate.RightManagementClientDelegate; import com.vci.client.utils.excel.ExcelDocumentUtils; import com.vci.client.utils.excel.SheetDataSet; import com.vci.client.ui.exception.VCIException; 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.common.utility.ObjectUtility; import com.vci.corba.common.VCIError; public class RolesExcelImportDialogActionListener extends JPanel implements ActionListener { /** * */ private static final long serialVersionUID = 1L; /** * 父窗体 */ private RolesImportDialog ownerDialog = null; /** * 所有Roles信息。 */ private ArrayList fileDatas = new ArrayList(); private UserObject logonUserInfo;// private String userName = LogonApplication.getUserEntityObject().getUserName(); public RolesExcelImportDialogActionListener( RolesImportDialog excelImprotDialog) { this.ownerDialog = excelImprotDialog; } public RolesExcelImportDialogActionListener() { // TODO Auto-generated constructor stub } @Override public void actionPerformed(ActionEvent e) { try{ logonUserInfo = new RightManagementClientDelegate(LogonApplication.getUserEntityObject()).fetchUserInfoByName(userName); }catch(VCIException ex) { VCIOptionPane.showError(LogonApplication.frame,"RMIPFramework", ex); return; } 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("正在获取系统角色数据......"); HashMap allDept = ownerDialog.getAllDept(); if (allDept.size() == 0) { //fetchAllDeptMessage(); } 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{ LinkedList selectedFiles = this.ownerDialog.getSelectedFiles(); RoleObject[] preRoles = null; int type = logonUserInfo.getUserType(); if(type < 2){ type = type+1; } try { //add by caill 2016.1.20根据type获得数据库中已经存在的角色 preRoles = new RightManagementClientDelegate(LogonApplication.getUserEntityObject()).fetchRoleInfoByRoleType(type); } catch (VCIException e) { // TODO Auto-generated catch block e.printStackTrace(); } for (File f : selectedFiles) { List sheetDataSets = this.getFileList(f); if (sheetDataSets != null && !sheetDataSets.isEmpty()) { 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[] oneData = dataSet.get(i); String userId = ObjectUtility .getNewObjectID36(); RoleObject RolesObj = new RoleObject(); RolesObj.setId(userId); boolean flag = false; //add by caill 2016.1.20对于数据库中存在的同名角色,不覆盖不导入 for(int j = 0;j < preRoles.length;j++){ if(preRoles[j].getName().equals(oneData[0])){ flag = true; } } if(oneData[0] != null && !oneData[0].trim().equals("") && flag == false){ RolesObj.setName(oneData[0]); // 名称 RolesObj.setDescription(oneData[1]);// 描述 RolesObj.setType(logonUserInfo.getUserType()); RolesObj.setCreateTime(System.currentTimeMillis()); RolesObj.setCreateUser(userName); RolesObj.setUpdateTime(System.currentTimeMillis()); RolesObj.setUpdateUser(userName); RolesObj.setGrantor(""); fileDatas.add(RolesObj); } } } } } } return true; } /** * 导入表单数据 * @throws VCIException */ //add by caill start 2016.1.20将导入的数据插入到数据库中 private boolean importExcelData() throws VCIError { boolean b = new RightManagementClientDelegate(LogonApplication.getUserEntityObject()).batchSaveRoles(fileDatas.toArray(new RoleObject[]{})); return b; } /** * 获取选中的文件 * * @return * @autor caicong * @data 2014-3-12 */ @SuppressWarnings({ "deprecation", "unused" }) private LinkedList getSelectedFiles() { LinkedList selectedFiles = ownerDialog.tablePanel .getSelectedRowObjects(); 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.tablePanel.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; } }