package com.vci.client.framework.rightConfig.functiontree; 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.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import javax.swing.JPanel; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Workbook; import com.vci.client.LogonApplication; import com.vci.client.common.TransmitTreeObject; import com.vci.client.common.objects.UserObject; import com.vci.client.framework.delegate.FuncOperationClientDelegate; import com.vci.client.framework.delegate.FunctionClientDelegate; import com.vci.client.framework.delegate.RightManagementClientDelegate; import com.vci.client.framework.rightConfig.object.FuncOperationObject; import com.vci.client.framework.rightConfig.object.FunctionObject; 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.client.utils.excel.ExcelCellStyleSettingCallback; import com.vci.client.utils.excel.ExcelFileOperation; import com.vci.client.utils.excel.WorkboolStyleSetting; import com.vci.common.utility.ObjectUtility; import com.vci.corba.common.VCIError; import com.vci.corba.framework.data.OperateInfo; public class FunOperateExcelImportDialogActionListener extends JPanel implements ActionListener { /** * */ private static final long serialVersionUID = 1L; /** * 父窗体 */ private static final String ENUM_USERSECURITYENUM = "usersecurityenum"; private FunOperateImportDialog ownerDialog = null; /** * 必填列 */ private ArrayList ColumnNameisRed = new ArrayList(); /** * 所有user信息。 */ private ArrayList fileDatas = new ArrayList(); //private ArrayList fileDatas=null; private ArrayList fileDatas2 = new ArrayList(); /** * 用户 */ private ArrayList deptAndUserRelation = new ArrayList(); //模块对象 private final FunctionClientDelegate modelDelegate = new FunctionClientDelegate(LogonApplication.getUserEntityObject()); private final FuncOperationClientDelegate funoperDelegate = new FuncOperationClientDelegate(LogonApplication.getUserEntityObject()); private TransmitTreeObject transmitTreeObject = new TransmitTreeObject(); private UserObject logonUserInfo;// private String userName = LogonApplication.getUserEntityObject().getUserName(); //Object obj=null; public FunOperateExcelImportDialogActionListener( FunOperateImportDialog excelImprotDialog) { this.ownerDialog = excelImprotDialog; } public FunOperateExcelImportDialogActionListener() { // 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(); deptAndUserRelation.clear(); save(); this.ownerDialog.getActionExecute().reloadFunctionTreeNode(); } else if (actionCommand.equals(ownerDialog.getBtnSelect() .getActionCommand())) { getFile(); } else if (actionCommand.equals(ownerDialog.getBtnExport() .getActionCommand())) { //downloadImportStuffTemplate(); } } /** * 保存对象 */ 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("正在导入表单人员信息......"); System.out.println("count==="+count); if(isSuccess==false){ importExcelData(count); } //boolean b = importExcelData(); return true; } /** * 收集表单信息。 * * @throws VCIError * @throws IOException */ // add by caill start int count=0; private boolean collectionDatas() throws VCIError, IOException{ LinkedList selectedFiles = this.ownerDialog.getSelectedFiles(); boolean b=false; 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(); String fParentId=""; //第一级的id(第二级的parentid) boolean boo=true; boolean first=false; String[] pd=new String[100]; int jibie=2; for (int i = 1; i < dataSet.size(); i++) { //fileDatas = new ArrayList(); String[] oneData = dataSet.get(i); String userId = ObjectUtility.getNewObjectID36(); FunctionObject funObj=new FunctionObject(); boolean onebl=false; boolean twobl=false; boolean same=false; String plName=oneData[0]; if(oneData[14].equals("1")) { try { onebl=modelDelegate.firstLevel(plName); } catch (VCIException e) { // TODO Auto-generated catch block e.printStackTrace(); } //如果第一级重名 if(onebl == true) { //第一级重名后覆盖第一级 fuzhi(funObj,oneData); try { fParentId=modelDelegate.changeFirstLevel(funObj,plName); pd[2]=fParentId; } catch (VCIException e) { // TODO Auto-generated catch block e.printStackTrace(); } funObj.setId(fParentId); //测试 fileDatas.add(funObj); //标记 first=true; } else { funObj.setId(userId); funObj.setParentId("business"); fuzhi(funObj,oneData); fileDatas.add(funObj); first=false; } b=false; } //######################### 合并 ######################### for(jibie=2;jibie<100;jibie++){ if(oneData[14].equals(String.valueOf(jibie))){ //2 if(first == true && boo == true){ try { if(pd[jibie]==null){ pd[jibie]=""; } twobl=modelDelegate.secondLevel(plName,pd[jibie]); } catch (VCIException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(twobl==true) { //重名后覆盖 fuzhi(funObj,oneData); try { fParentId=modelDelegate.changeSecondLevel(funObj,plName,pd[jibie]); pd[jibie+1]=fParentId; } catch (VCIException e) { // TODO Auto-generated catch block e.printStackTrace(); } //funObj.setId(fParentId); //测试 funObj.setId(pd[jibie+1]); fileDatas.add(funObj); //标记 boo = true; } else { //前几级重名,最后一级不重名的情况下 // funObj.setId(userId); // for(int j=i-1;j>=1;j--){ // if(dataSet.get(j)[14].equals(String.valueOf(jibie-1))){ // for(int c=0;c= 1;j--){ // if(dataSet.get(j)[14].equals(String.valueOf(jibie-1))){ // for(int c=0;c 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 boolean exportExcel(String[][] datas, String sheetName) { File file = getRequiredFile(false); if (file == null) { return false; } String fileName = file.getAbsolutePath(); // add by xchao 2014.05.15 end new ExcelFileOperation().writeExcelFileInfo(fileName, sheetName, datas, new ExcelCellStyleSettingCallback() { @Override public WorkboolStyleSetting doSetWorkbookStyle( final Workbook workbook) { WorkboolStyleSetting setting = new WorkboolStyleSetting() { @Override public LinkedHashMap getStyleMap() { LinkedHashMap styleMap = new LinkedHashMap(); org.apache.poi.ss.usermodel.CellStyle style = workbook .createCellStyle(); org.apache.poi.ss.usermodel.Font font = (org.apache.poi.ss.usermodel.Font) workbook .createFont(); font.setColor(HSSFFont.COLOR_RED); style.setFont(font); for (int column : ColumnNameisRed) { styleMap.put("0*" + (column) + "", style); } return styleMap; } }; return setting; } }); return true; } private File getExcelFile() { File file = null; String filePath = VCISwingUtil.getExcelFileURL(ownerDialog, true, ""); if (filePath != null) { file = new File(filePath); } return file; } }