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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
package com.vci.client.portal.NewNewUI.actionExport;
 
 
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
 
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
 
import javax.swing.JTable;
 
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
 
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
 
import com.vci.client.LogonApplication;
import com.vci.client.portal.NewNewUI.Export.BaseExportNode;
import com.vci.client.portal.NewNewUI.Export.ExportTreeCellRenderer;
import com.vci.client.portal.NewNewUI.Export.VCIImportTree;
import com.vci.client.portal.utility.UITools;
import com.vci.client.ui.swing.CheckBoxLabel;
import com.vci.client.ui.swing.VCIOptionPane;
import com.vci.client.ui.tree.CheckBoxTreeCellRenderer;
import com.vci.client.ui.tree.CheckBoxTreeManager;
import com.vci.common.utility.ObjectUtility;
import com.vci.corba.common.VCIError;
import com.vci.corba.portal.*;
import com.vci.corba.portal.data.PLAction;
import com.vci.corba.portal.data.PLActionCls;
import com.vci.corba.portal.data.PLActionParam;
 
public class VCIActionImportTree extends VCIImportTree {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private CheckBoxTreeManager treeManager = null;
    // 导入的对象
    private ExportBeans exportBeans = null;
    // 库中的所有Action分类对象
    private PLActionCls[] pLActionClses = null;
    // 库中的所有action对象
    private PLAction[] plActions = null;
    // log对象
    private ExportActionLogBean logBean = null;
    // 存储库中的action,plCode作为关键字
    private HashMap<String/* plCode */, PLAction> actionsInDB = null;
    // getPLActionListFromOptionListByPLActionClsId的临时变量,按actionCls属性存储action到list中
    private HashMap<String, ArrayList<PLAction>> temActionListMap = null;
    // 更换actionId后,可以从此导航中获得原来的id
    private HashMap<String /* newId */, String /* oldId */> actionIdNav = new HashMap<String /* newId */, String /* oldId */>();
    // 更换actionClsId后,可以从此导航中获得原来的id
    private HashMap<String /* newId */, String /* oldId */> actionClsIdNav = new HashMap<String /* newId */, String /* oldId */>();
    // 存储符合条件的PLActionCls
    private ArrayList<PLActionCls> optionPLActionClsList = null;
 
    // 存储符合条件的PLAction
    private ArrayList<PLAction> optionPLActionList = null;
    private int pLActionClsesLength = 0;
    // 接受传来的PrintWriter 向文件中写入日志
    private PrintWriter fileLog = null;
    // 接受传来的PrintWriter 向"下一步"写入日志
    private DefaultTableModel tableLog = null;
    // 保存“action分类”的name 保证每个分类只存入分类一次 降低资源开销
    private HashSet<String> saveActionClsLog = null;
    //保存所有父分类,用id主key
    private HashMap<String/*id*/,PLActionCls> actionClses = new HashMap<String/*id*/,PLActionCls>();
    public VCIActionImportTree(DefaultTreeModel treeModel) {
        super(treeModel);
        this.setCellRenderer(new ExportTreeCellRenderer());
    }
 
    public VCIActionImportTree() {
        super(new DefaultTreeModel(new DefaultMutableTreeNode("Action分类")));
        this.setCellRenderer(new ExportTreeCellRenderer());
    }
 
    @SuppressWarnings("rawtypes")
    public void initTree(String filePath) {
        try {
            this.getSelectionModel().setSelectionMode(
                    TreeSelectionModel.SINGLE_TREE_SELECTION);
            actionIdNav.clear();
            actionClsIdNav.clear();
            actionsInDB = null;
            temActionListMap = null;
            this.optionPLActionClsList = new ArrayList<PLActionCls>();
            this.optionPLActionList = new ArrayList<PLAction>();
            this.pLActionClses = UITools.getService().getPLActionClsArray();
            this.plActions = UITools.getService().getAllPLAction();
            this.saveActionClsLog = new HashSet<String> ();
            this.pLActionClsesLength = pLActionClses.length;
        } catch (Exception e) {
            VCIOptionPane.showMessage(LogonApplication.frame, "导入数据时出错");
            e.printStackTrace();
        }
        optionPLActionClsList.addAll(Arrays.asList(pLActionClses));
        // optionPLActionList.addAll(Arrays.asList(plActions));
        // 获得导入对象
        HashMap map = null;
        try {
            ObjectInputStream obj = new ObjectInputStream(new FileInputStream(
                    filePath));
            map = (HashMap) obj.readObject();
        } catch (Exception e) {
 
            VCIOptionPane.showMessage(LogonApplication.frame,
                    "导入对象未获取成功!!导入对象版本号不同!");
 
            // e.printStackTrace();
        }
        if(map != null){
            exportBeans = (ExportBeans) map.get("exportBeans");
            // 添加导入的数据
            addImportData(this.optionPLActionClsList, this.optionPLActionList);
        }else{
            PLActionCls plac = new PLActionCls("", "", "", "", "", 0, (short)0);
            ExportPLActionClsBean exportPLActionBean = new ExportPLActionClsBean(plac);
            exportPLActionBean.setMessage("导入数据失败");
            exportPLActionBean.setLeaf(BaseExportNode.IS_A_Message);
            DefaultMutableTreeNode root = new DefaultMutableTreeNode(exportPLActionBean, true);
            this.setModel(new DefaultTreeModel(root));
            return;
 
        }
        
       //设置树默认属性 (全部展开、全部勾选、不可编辑)
        this.setModel(new DefaultTreeModel(new DefaultMutableTreeNode(
                "Action分类")));
 
        if (treeManager == null) {
            treeManager = new CheckBoxTreeManager(this) {
                public void mouseClicked(MouseEvent me) {
                    // 覆盖点击选择事件
                }
            };
        } else {
            TreePath[] paths = treeManager.getSelectionModel()
                    .getSelectionPaths();
            if (paths != null)
                treeManager.getSelectionModel().removeSelectionPaths(paths);
        }
        treeManager.getSelectionModel().setSelectionPath(
                new TreePath(this.getModel().getRoot()));
 
        CheckBoxTreeCellRenderer render = (CheckBoxTreeCellRenderer) this
                .getCellRenderer();
        this.updateUI();
        for (int i = 0; i < render.getComponentCount(); i++) {
            if (render.getComponent(i).getClass().getName()
                    .equals("com.vci.base.ui.swing.CheckBoxLabel")) {
                CheckBoxLabel label = (CheckBoxLabel) render.getComponent(i);
                label.setEnabled(false);
            }
        }
        // 刷新分类树
        refreshImportTree(this.getPathForRow(0));
    }
 
    private void refreshImportTree(TreePath pathForRow) {
 
        DefaultTreeModel dtml = (DefaultTreeModel) this.getModel();
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) pathForRow
                .getLastPathComponent();
        // 将PLActionCls 、PLAction 对应的set集合转换成数组(匹配原addExportTreeNode方法)
        PLActionCls[] newPLActionClsArray = new PLActionCls[optionPLActionClsList.size()];
        PLAction[] newPLActionArray = new PLAction[optionPLActionList.size()];
        optionPLActionClsList.toArray(newPLActionClsArray);
        optionPLActionList.toArray(newPLActionArray);
        //清楚分类id
        actionClses.clear();
        //保存所有分类id
        for (int i = 0; i < newPLActionClsArray.length; i++) {
            actionClses.put(newPLActionClsArray[i].id,newPLActionClsArray[i]);
        }
        //保存所有存在action的分类(子分类存在action也保存)
        HashSet<PLActionCls> pLActionClsList =new HashSet<PLActionCls> ();
        for (int i = 0; i < newPLActionArray.length; i++) {
                //缓存该分类的所有父分类
                saveParentCls(newPLActionArray[i].plActionCls,pLActionClsList);
        }
        PLActionCls[] actionClses = new PLActionCls[pLActionClsList.size()];
        pLActionClsList.toArray(actionClses);
        
        addExportTreeNode(dtml, node, actionClses, newPLActionArray);
 
        // 展开所有树节点
        TreeNode rootNode = (TreeNode) this.getModel().getRoot();
        this.expandAllTreeNode(new TreePath(rootNode), true);
 
    }
 
    private void addExportTreeNode(DefaultTreeModel dtml,
            DefaultMutableTreeNode node, PLActionCls[] pLActionClses,
            PLAction[] plActions) {
        // node.removeAllChildren();
        String pid = "";
        Object userObject = node.getUserObject();
        if (!(userObject instanceof String)) {
 
            if (userObject instanceof ExportPLActionClsBean) {
                PLActionCls pac = ((ExportPLActionClsBean) userObject)
                        .getBean();
                pid = pac.id;
            }
        }
 
        // 添加子节点(源table节点)
        for (int i = 0; i < plActions.length; i++) {
            if ((!pid.equals("")) && plActions[i].plActionCls.equals(pid)) {
 
                ExportPLActionBean exportPLActionBean = new ExportPLActionBean(
                        plActions[i]);
                if (plActions[i].plName.endsWith("@#修改$%")) {
                    plActions[i].plName = plActions[i].plName.replace(
                            "@#修改$%", "");
                    exportPLActionBean.setMessage("【修改】");
                }else 
                if (plActions[i].plName.endsWith("@#存在在未分类中$%")) {
                    plActions[i].plName = plActions[i].plName.replace(
                            "@#存在在未分类中$%", "");
                    exportPLActionBean.setMessage("【action存在在未分类中,修改Action并按导入文件创建action分类】");
                } 
                else {
                    exportPLActionBean.setMessage("【新增】");
                    exportPLActionBean.setFontColor(Color.BLACK);
                }
                DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(
                        exportPLActionBean);
//                TreePath newChildTreePath = new TreePath(newChild);
 
                dtml.insertNodeInto(newChild, node, node.getChildCount());
 
            }
        }
        // 添加子节点(源树节点)
        for (int i = 0; i < pLActionClses.length; i++) {
            if (pLActionClses[i].pid.equals(pid)) {
                
                if(pid.equals("") && pLActionClses[i].name.equals("未分类")){
                  continue;
                }
                ExportPLActionClsBean exportPLActionClsBean = new ExportPLActionClsBean(
                        pLActionClses[i]);
                if (i >= this.pLActionClsesLength) {
                    exportPLActionClsBean.setMessage("【新增】");
                    exportPLActionClsBean.setFontColor(Color.BLACK);
                }
                DefaultMutableTreeNode child = new DefaultMutableTreeNode(
                        exportPLActionClsBean);
                dtml.insertNodeInto(child, node, node.getChildCount());
                addExportTreeNode(dtml, child, pLActionClses, plActions);
            }
 
        }
 
        if (pid.equals("")) {
            PLActionCls plac = new PLActionCls("", "未分类", "", "", "", 0, (short)0);
            DefaultMutableTreeNode child = new DefaultMutableTreeNode(
                    new ExportPLActionClsBean(plac), true);
            dtml.insertNodeInto(child, node, node.getChildCount());
            for (int i = 0; i < plActions.length; i++) {
                if (plActions[i].plActionCls.equals("")) {
                    ExportPLActionBean exportPLActionBean = new ExportPLActionBean(
                            plActions[i]);
 
                    if (plActions[i].plName.endsWith("@#修改$%")) {
                        plActions[i].plName = plActions[i].plName.replace(
                                "@#修改$%", "");
                        exportPLActionBean.setMessage("【修改】");
                    }else 
                    if (plActions[i].plName.endsWith("@#存在在未分类中$%")) {
                        plActions[i].plName = plActions[i].plName.replace(
                                "@#存在在未分类中$%", "");
                        exportPLActionBean.setMessage("【修改】");
                    } 
                    else {
                        exportPLActionBean.setMessage("【新增】");
                        exportPLActionBean.setFontColor(Color.BLACK);
                    }
 
                    DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(
                            exportPLActionBean, false);
                    dtml.insertNodeInto(newChild, child, child.getChildCount());
                }
            }
 
        }
        dtml.reload(node);
 
    }
    /**
     * 保存所有父分类
     * @param plActionCls
     * @param pLActionClsList
     */
        private void saveParentCls(String plActionCls,
                HashSet<PLActionCls> pLActionClsList) {
            if(actionClses.containsKey(plActionCls)){
                PLActionCls pCls = actionClses.get(plActionCls);
                pLActionClsList.add(pCls);
                saveParentCls(pCls.pid,pLActionClsList);
            }
            
        }
    /**
     * @Use get all information from 'exportBeans' and put it into reference
     *      list
     * @param pLActionClsList
     * @param plActionList
     */
    private void addImportData(ArrayList<PLActionCls> pLActionClsList,
            ArrayList<PLAction> plActionList) {
 
        HashMap<String/* actionId */, PLAction> pLActionBeans = this.exportBeans
                .getPLActions();
        if (pLActionBeans == null) {
            VCIOptionPane.showMessage(LogonApplication.frame, "导入对象未获取成功!!");
            return;
        }
        Set<Entry<String, PLAction>> pLActions = pLActionBeans.entrySet();
        for (Entry<String, PLAction> entry : pLActions) {
            PLAction plAction = entry.getValue();
            PLAction plActionInDB = getPLActionInDB(plAction);
 
            if (plActionInDB != null) {
                // String newId = ObjectUtility.getNewObjectID36();
                // actionIdNav.put(plAction.plOId, plAction.plOId);
                if( plActionInDB.plActionCls != ""){
                    plAction.plActionCls = plActionInDB.plActionCls;
                    // plAction.plOId = newId;
                    plAction.plName += "@#修改$%";
                    plActionList.add(plAction);
                    continue;
                }else{
                    plAction.plName += "@#存在在未分类中$%";
                }
 
            }
            // 将该实例acion存入到plActionList中
            plActionList.add(plAction);
 
            // 定义list存储当前action到数据库action的路径上的所有PLActionCls对象
            List<PLActionCls> plActionClsList = new ArrayList<PLActionCls>();
            // 获得库中的最近的PLActionCls对象
            String pId = ObjectUtility.getNewObjectID36();
            String oId = ObjectUtility.getNewObjectID36();
 
            PLActionCls pLActionCls = getParentPLActionClsInDBById(plAction,
                    pId, plActionClsList);
 
            changePLActionOID(plAction, oId, pId);
 
//            System.out.println(plActionClsList.size());
 
            if (plActionClsList.size() == 1) {
                // 删除"未分类"分类对象
                PLActionCls plActionCls = plActionClsList.get(plActionClsList
                        .size() - 1);
 
                if(pLActionCls != null){
                    plAction.plActionCls = pLActionCls.id;
                    if(plActionCls.name.equals("未分类")){
                        plAction.plActionCls = "";
                    }
                    plActionClsList.remove(plActionClsList.size() - 1);
                }else{
                    if(plActionCls.name.equals("未分类")){                    
                        plActionClsList.remove(plActionClsList.size() - 1);
                    }else{
                        plActionCls.pid = "";
                    }
                    
                }    
                
            }
            if (plActionClsList.size() >= 2) {
 
                PLActionCls plActionCls2 = plActionClsList.get(plActionClsList
                        .size() - 2);
                PLActionCls plActionCls1 = plActionClsList.get(plActionClsList
                        .size() - 1);
                if (pLActionCls != null) {
                    // 将导入对象与“库中的最近的PLActionCls对 象”name相同的对象
                    // 的下一个PLActionCls对象父id改为库中“最近”PLActionCls对象id
                    plActionClsList.remove(plActionClsList.size() - 1);
                    plActionCls2.pid = pLActionCls.id;
                }else{
                    if(plActionCls1.name.equals("未分类")){                    
                        plActionClsList.remove(plActionClsList.size() - 1);
                        plActionCls2.pid = "";
                    }else{
                        plActionCls1.pid = "";
                    }
                
                }
 
            }
            // 增加到整体list中用来最终持久化
            pLActionClsList.addAll(plActionClsList);
        }
    }
 
    private void changePLActionOID(PLAction plAction, String oId, String pId) {
        actionIdNav.put(oId, plAction.plOId);
        plAction.plOId = oId;
        plAction.plActionCls = pId;
    }
 
    private PLAction getPLActionInDB(PLAction plAction) {
        if (actionsInDB == null) {
            actionsInDB = new HashMap<String/* plCode */, PLAction>();
            for (PLAction action : this.plActions) {
                actionsInDB.put(action.plCode, action);
            }
        }
        return actionsInDB.get(plAction.plCode);
    }
 
    /***
     * 获得库中的PLActionCls对象,用来嫁接导入来的树
     * 
     * @param plActionClsId
     * @param plActionClsList
     * @return
     */
    private PLActionCls getParentPLActionClsInDBById(Object plActionClsIdObj,
            String newPLActionClsId, List<PLActionCls> plActionClsList) {
        String pId = null;
        if (plActionClsIdObj instanceof PLAction) {
            pId = ((PLAction) plActionClsIdObj).plActionCls;
            if(pId.equals("")){
                PLActionCls noneCls =     new PLActionCls("", "未分类", "", "", "", 0, (short)0);
                plActionClsList.add(noneCls);
                return noneCls;
            }
        }
        if (plActionClsIdObj instanceof PLActionCls) {
            pId = ((PLActionCls) plActionClsIdObj).pid;
            pId = actionClsIdNav.get(pId);
            if(pId.equals("")){
                pId = "null";
            }
                
        }
 
        PLActionCls plActionCls = this.exportBeans.getPLActionClsBeanById(pId);// 从导入对象中获得父分类
 
        
        PLActionCls tempPLActionCls = null;
        if (plActionCls != null) {// plActionCls为空 证明父节点为根节点
 
            plActionClsList.add(plActionCls);
            if(this.optionPLActionClsList.contains(plActionCls)){
                return plActionCls ;
            }
            for (PLActionCls Cls : pLActionClses) {
                if ((plActionCls.id.equals("") && plActionCls.name.trim()
                        .equals("未分类"))
                        || plActionCls.name.trim().equals(Cls.name.trim())) {
                    tempPLActionCls = Cls;
                }
            }
            plActionCls.id = newPLActionClsId; // 修改父分类id
        } else {
            return null;
        }
 
        if (tempPLActionCls == null) {
            String newClsId = ObjectUtility.getNewObjectID36();
            actionClsIdNav.put(newClsId, plActionCls.pid);
            plActionCls.pid = newClsId;
            return getParentPLActionClsInDBById(plActionCls, newClsId,
                    plActionClsList);
        }
        return tempPLActionCls;
    }
 
    @Override
    public boolean hasSelectImportContent(){
        TreePath path = new TreePath(this.getModel().getRoot());
        if(path.getLastPathComponent().toString().equals("导入数据失败")){
            return false;
        };
        TreePath[] treePath = treeManager.getSelectionModel()
                .getSelectionPaths();
        if (treePath == null) {
            return false;
        }
        return true;
    }
 
    @Override
    public void exeImport(PrintWriter log, JTable tblExportLog) {
        // 只保留导入的符合条件的PLActionCls
        optionPLActionClsList.removeAll(Arrays.asList(pLActionClses));
//        System.out.println(this.optionPLActionClsList.size());
        // 符合条件的PLAction
//        System.out.println(this.optionPLActionList.size());
 
        this.fileLog = log;
        this.addImportLogHeader(tblExportLog);
        this.tableLog = (DefaultTableModel) tblExportLog.getModel();
        while (this.tableLog.getRowCount() > 0) {// 清除tableLog,准备重新绘制
            this.tableLog.removeRow(0);
        }
        new ExportActionLogBean();
        TreePath[] treePath = treeManager.getSelectionModel()
                .getSelectionPaths();
 
        if (treePath != null) {
            try {
 
                for (int i = 0; i < treePath.length; i++) {
                    DefaultMutableTreeNode node = (DefaultMutableTreeNode) treePath[i]
                            .getLastPathComponent();
                    Object obj = node.getUserObject();
                    if (obj instanceof String) {// 如果是root节点,则保存所有模块 返回
                        String nodeName = (String) obj;
                        if ("Action分类".equals(nodeName)) {
                            for (PLAction action : this.optionPLActionList) {
                                // 持久化action及其类别
                                saveAction(action);
                                // this.exportBeans.addPLActionBean(action);
                                // ExportActionLogBean bean = exportBeans
                                // .getLogBean();
                                // this.fileLog.println(bean.getLogRowContent());
                                // this.tableLog.addRow(bean.getLogRowObjcets());
                            }
                            return;
                        }
                    }
 
                    if (obj instanceof ExportPLActionBean) {
                        PLAction action = ((ExportPLActionBean) obj).getBean();
                        // 持久化action及其类别
                        saveAction(action);
                    } else if (obj instanceof ExportPLActionClsBean) {
                        PLActionCls actionCls = ((ExportPLActionClsBean) obj)
                                .getBean();
                        ArrayList<PLActionCls> actionClses = getChildPLActionClsListFromOptionList(actionCls);
                        actionClses.add(actionCls);
                        for (PLActionCls plActionCls : actionClses) {
                            ArrayList<PLAction> actions = getPLActionListFromOptionListByPLActionClsId(plActionCls.id);
                            if (actions != null) {
                                for (PLAction plAction : actions) {
                                    saveAction(plAction);
                                }
                            }
                        }
                    }
 
                }
 
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
 
    }
 
    private ArrayList<PLAction> getPLActionListFromOptionListByPLActionClsId(
            String id) {
        if (temActionListMap == null) {
            temActionListMap = new HashMap<String, ArrayList<PLAction>>();
            for (PLAction plAction : this.optionPLActionList) {
                if (temActionListMap.get(plAction.plActionCls) == null) {
                    ArrayList<PLAction> temArray = new ArrayList<PLAction>();
                    temActionListMap.put(plAction.plActionCls, temArray);
                }
                temActionListMap.get(plAction.plActionCls).add(plAction);
            }
        }
 
        return temActionListMap.get(id);
    }
 
    private ArrayList<PLActionCls> getChildPLActionClsListFromOptionList(
            PLActionCls plActionCls) {
        ArrayList<PLActionCls> temActionClsList = new ArrayList<PLActionCls>();
 
        putPLActionCls(plActionCls, temActionClsList);
 
        return temActionClsList;
    }
 
    private void putPLActionCls(PLActionCls plActionCls,
            ArrayList<PLActionCls> temActionClsList) {
        ArrayList<PLActionCls> allPLActionClsList = new ArrayList<PLActionCls>();
        allPLActionClsList.addAll(this.optionPLActionClsList);
        allPLActionClsList.addAll(Arrays.asList(pLActionClses));
        for (PLActionCls actionCls : allPLActionClsList) {
 
            if (actionCls.pid.equals(plActionCls.id)) {
                temActionClsList.add(actionCls);
                putPLActionCls(actionCls, temActionClsList);
            }
 
        }
 
    }
 
    private void saveAction(PLAction action) {
 
        // 持久化action及其参数(ActionCls had added when initTree)
        PLAction actionInDB = this.getPLActionInDB(action);
        if (actionInDB != null) {// in db , update it
            this.actionIdNav.put(actionInDB.plOId, action.plOId);
            action.plOId = actionInDB.plOId;
            try {
                this.updateActionWithParams(action);
                logBean = new ExportActionLogBean("更新成功", action.plCode,
                        action.plName, action.plCSClass, action.plBSUrl,
                        action.plTypeType, action.plDesc, "");
            } catch (Exception e) {
                logBean = new ExportActionLogBean("更新失败", action.plCode,
                        action.plName, action.plCSClass, action.plBSUrl,
                        action.plTypeType, action.plDesc, "");
            } finally {
                this.writeLog(logBean);
            }
        } else {// not in db ,save it
            try {
                this.saveActionWithParams(action);
                logBean = new ExportActionLogBean("保存成功", action.plCode,
                        action.plName, action.plCSClass, action.plBSUrl,
                        action.plTypeType, action.plDesc, "未知");
            } catch (Exception e) {
                logBean = new ExportActionLogBean("保存失败", action.plCode,
                        action.plName, action.plCSClass, action.plBSUrl,
                        action.plTypeType, action.plDesc, "");
            } finally {
                this.writeLog(logBean);
            }
        }
 
        PLActionCls actionCls = this
                .getPLActionClsFromOptionListById(action.plActionCls);
 
        if (actionCls != null) {
            savePLActionCls(actionCls);
 
        }
    }
 
    /**
     * 写出log记录
     * 
     * @param logBean
     */
    private void writeLog(ExportActionLogBean logBean) {
        this.fileLog.println(logBean.getLogRowContent());
        this.tableLog.addRow(logBean.getInportLogRowObjcets());
    }
 
 
 
    private void savePLActionCls(PLActionCls actionCls) {
        if (actionCls != null) {
            if (!saveActionClsLog.contains(actionCls.name)) {
                saveActionClsLog.add(actionCls.name);
                UITools.getService().creaetePLActionCls(actionCls);
            }
            PLActionCls aCls = getPLActionClsFromOptionListById(actionCls.pid);
            savePLActionCls(aCls);
        }
 
    }
 
    private void saveActionWithParams(PLAction action) throws VCIError {
 
        String oldId = this.actionIdNav.get(action.plOId);
        PLActionParam[] params = exportBeans
                .getPLActionParamArrayByActionId(oldId);
 
        if (params != null) {
            for (PLActionParam plActionParam : params) {
                // 数据库不存该参数在则创建
                plActionParam.action = action.plOId;
                UITools.getService().createPLActionParam(plActionParam);
            }
        }
 
        UITools.getService().savePLAction(action);
 
    }
 
    private void updateActionWithParams(PLAction action) throws VCIError {
        String oldId = this.actionIdNav.get(action.plOId);
        PLActionParam[] params = exportBeans
                .getPLActionParamArrayByActionId(oldId);
 
        PLActionParam[] paramsInDB = UITools.getService()
                .getPLActionParamArrayByActionId(action.plOId);
 
        if (params != null) {
 
            HashMap<String, PLActionParam> paramsInDBMap = new HashMap<String, PLActionParam>();// 临时存储库中的参数
 
            if (paramsInDB != null) {// 数据库中参数存在,则存储到临时map中
                for (PLActionParam plActionParam : paramsInDB) {
                    paramsInDBMap.put(plActionParam.name, plActionParam);
                }
            }
 
            for (PLActionParam plActionParam : params) {
 
                PLActionParam actionParamInDB = paramsInDBMap
                        .get(plActionParam.name);
                if (actionParamInDB == null) {// 数据库不存该参数在则创建
                    UITools.getService().createPLActionParam(plActionParam);
                } else {// 数据库中存在该参数则更新
                    actionParamInDB.name = plActionParam.name;
                    actionParamInDB.defaultValue = plActionParam.defaultValue;
                    actionParamInDB.description = plActionParam.description;
                    UITools.getService().editPLActionParam(actionParamInDB);
                }
 
            }
        }
        UITools.getService().updatePLAction(action);
    }
 
    private PLActionCls getPLActionClsFromOptionListById(String plActionCls) {
        PLActionCls temActionCls = null;
        if (optionPLActionClsList != null) {
            for (PLActionCls actionCls : optionPLActionClsList) {
                if (actionCls.id.equals(plActionCls)) {
                    temActionCls = actionCls;
                }
            }
        }
        return temActionCls;
    }
 
//    private PLActionCls getPLActionClsFromDB(PLActionCls plActionCls) {
//        PLActionCls temActionCls = null;
//        PLActionCls plActionClsesInDB[] = null;
//        try {
//            plActionClsesInDB = Tool.getService().getPLActionClsArray();
//            for (PLActionCls actionCls : plActionClsesInDB) {
//                if (actionCls.name.equals(plActionCls.name)) {
//                    temActionCls = actionCls;
//                }
//            }
//
//        } catch (VCIError e) {
//            e.printStackTrace();
//        }
//        ;
//        return temActionCls;
//    }
 
    private void addImportLogHeader(JTable tblExportLog) {
        tblExportLog.setModel(new DefaultTableModel(new Object[][] { { null,
                null, null, null, null, null, null, null }, }, new String[] {
                "\u5E8F\u53F7", "导入状态", "\u7F16\u53F7", "\u540D\u79F0",
                "CS\u6CE8\u518C", "BS\u6CE8\u518C", "\u7C7B\u578B",
                "\u63CF\u8FF0" }) {
            /**
             * serialVersionUID
             */
            private static final long serialVersionUID = 1L;
            boolean[] columnEditables = new boolean[] { false, false, false,
                    false, false, false, false, false };
 
            public boolean isCellEditable(int row, int column) {
                return columnEditables[column];
            }
        });
        tblExportLog.getColumnModel().getColumn(0).setResizable(false);
        tblExportLog.getColumnModel().getColumn(0).setPreferredWidth(35);
        tblExportLog.getColumnModel().getColumn(0).setMinWidth(35);
        tblExportLog.getColumnModel().getColumn(0).setMaxWidth(35);
        tblExportLog.getColumnModel().getColumn(1).setMinWidth(60);
        tblExportLog.getColumnModel().getColumn(1).setMaxWidth(150);
        tblExportLog.getColumnModel().getColumn(2).setMinWidth(60);
        tblExportLog.getColumnModel().getColumn(2).setMaxWidth(150);
        tblExportLog.getColumnModel().getColumn(3).setMinWidth(60);
        tblExportLog.getColumnModel().getColumn(3).setMaxWidth(150);
        tblExportLog.getColumnModel().getColumn(4).setPreferredWidth(120);
        tblExportLog.getColumnModel().getColumn(4).setMinWidth(60);
        tblExportLog.getColumnModel().getColumn(4).setMaxWidth(150);
        tblExportLog.getColumnModel().getColumn(5).setPreferredWidth(120);
        tblExportLog.getColumnModel().getColumn(5).setMinWidth(60);
        tblExportLog.getColumnModel().getColumn(5).setMaxWidth(150);
        tblExportLog.getColumnModel().getColumn(6).setMinWidth(50);
        tblExportLog.getColumnModel().getColumn(6).setMaxWidth(100);
        tblExportLog.getColumnModel().getColumn(7).setMinWidth(60);
        tblExportLog.getColumnModel().getColumn(7).setMaxWidth(150);
 
    }
 
}