wangting
2024-12-26 fa261e8c1220b31af54e8167e4de9c3320b1af27
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
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<Integer> ColumnNameisRed = new ArrayList<Integer>();
    /**
     * 所有user信息。
     */
    private ArrayList<FunctionObject> fileDatas = new ArrayList<FunctionObject>();
    //private ArrayList<FunctionObject> fileDatas=null;
    private ArrayList<FunctionObject> fileDatas2 = new ArrayList<FunctionObject>();
    /**
     * 用户
     */
    private ArrayList<String[]> deptAndUserRelation = new ArrayList<String[]>();
    //模块对象
    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<File> 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<String, String> 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<File> selectedFiles = this.ownerDialog.getSelectedFiles();
        boolean b=false;
        for (File f : selectedFiles) {
            List<SheetDataSet> 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<String[]> 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<FunctionObject>();
                            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<fileDatas.size();c++){
//                                                        if(String.valueOf(fileDatas.get(c).getModuleNo()).equals(dataSet.get(j)[5])){
//                                                            funObj.setParentId(fileDatas.get(c).getId());
//                                                        }
//                                                    }
//                                                    break;
//                                                }
//                                            }
//                                            fuzhi(funObj,oneData);
//                                            fileDatas.add(funObj);
                                        }
                                        
                                    } 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<fileDatas.size();c++){
//                                                    if(String.valueOf(fileDatas.get(c).getModuleNo()).equals(dataSet.get(j)[5])){
//                                                        funObj.setParentId(fileDatas.get(c).getId());
//                                                    }
//                                                }
//                                                
//                                                break;
//                                            }
//                                        }
//                                        fuzhi(funObj,oneData);
//                                        fileDatas.add(funObj);
                                        boo = false;
                                    }
                                    b=false;
                                }
                            }
                            
                            if(oneData[14].equals("-1")) {    
                                importExcelData(count);
                                FuncOperationObject foObj=new FuncOperationObject();
                                int len=fileDatas.size();
                                //**************同一节点下不能有相同的操作类型********************
                                String dataOperName=oneData[18];
                                String plFuncOid=fileDatas.get(len-1).getId();
                                try {
                                    same = funoperDelegate.selSameOper(dataOperName,plFuncOid);
                                } catch (VCIException e1) {
                                    // TODO Auto-generated catch block
                                    e1.printStackTrace();
                                }
                                //******************************************************
                                if(same == false) {
                                    foObj.setId(userId);
                                    foObj.setFuncId(fileDatas.get(len-1).getId());
                                    try {
                                        OperateInfo operObj = modelDelegate.fetchOperateTypeByName(oneData[18]);
                                        foObj.setOperId(operObj.id);
                                    } catch (VCIException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                    foObj.setNumber(Integer.parseInt(oneData[16]));
                                    foObj.setOperAlias(oneData[15]);
                                    foObj.setIsValid(Integer.parseInt(oneData[17]) != 0);
                                    try {
                                        funoperDelegate.saveFuncOperation2(foObj);
                                    } catch (VCIException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                    
                                } else {
                                    foObj.setNumber(Integer.parseInt(oneData[16]));
                                    foObj.setOperAlias(oneData[15]);
                                    foObj.setIsValid(Integer.parseInt(oneData[17]) != 0);
                                    try {
                                        funoperDelegate.updateOperation(foObj,dataOperName,plFuncOid);
                                    } catch (VCIException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }
                                count=fileDatas.size();
                                b=true;
                            }                            
                        }
                    }
                }
            }
        }
        return b;
    }
 
    public void fuzhi(FunctionObject funObj,String[] oneData){
        funObj.setName(oneData[0]);
        funObj.setResourceC(oneData[1]);
        funObj.setSuffixC(oneData[2]);
        funObj.setDesc(oneData[6]);
        funObj.setResourceB(oneData[3]);
        funObj.setSuffixB(oneData[4]);
        funObj.setSequence(Integer.parseInt(oneData[9]));
        //funObj.setModuleNo(Integer.parseInt(oneData[5]));
        funObj.setImage(oneData[8]);
        funObj.setIsValid(Integer.parseInt(oneData[7]) != 0);
        funObj.setAliasName(oneData[10]);
        funObj.setResourceDotNet(oneData[12]);
        funObj.setResourceMobile(oneData[13]);    
    }
    /**
     * 导入表单数据
     * @throws VCIException 
     */
    private boolean importExcelData(int count) throws VCIError {
        //boolean b = new RightManagementClientDelegate(LogonApplication.getUserEntityObject()).batchSaveUsers(fileDatas.toArray(new UserObject[]{}),deptAndUserRelation.toArray(new String[][]{}));
        boolean b=false;
        
        try {
            b=    modelDelegate.importModules(fileDatas.toArray(new FunctionObject[]{}),count);
        } catch (VCIException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //boolean b=true;
        System.out.println("uuuuuuuuu="+b);
        return b;
    }
    /**
     * 获取选中的文件
     * 
     * @return
     * @autor caicong
     * @data 2014-3-12
     */
    @SuppressWarnings({ "deprecation", "unused" })
    private LinkedList<File> getSelectedFiles() {
        LinkedList<File> selectedFiles = ownerDialog.tablePanel
                .getSelectedRowObjects();
        return selectedFiles;
    }
 
    /**
     * 获取表单数据
     * 
     * @param f
     * @return
     * @throws IOException
     * @throws VCIError
     * @autor caicong
     * @data 2014-3-11
     */
    private List<SheetDataSet> getFileList(File f) throws VCIError, IOException {
        // 获取流
        BufferedInputStream fileInputStream = new BufferedInputStream(
                new FileInputStream(f));
        String name = f.getName();
        // 获取表list
        List<SheetDataSet> sheetDataSets = ExcelDocumentUtils
                .readExcelDocument(name, fileInputStream);
        return sheetDataSets;
    }
 
    private void getFile() {
        File f = getRequiredFile(true);
        if (f == null)
            return;
        ArrayList<File> 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<String, CellStyle> getStyleMap() {
                                LinkedHashMap<String, CellStyle> styleMap = new LinkedHashMap<String, CellStyle>();
                                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;
    }
 
}