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
package com.vci.client.portal.NewNewUI.actionExport;
 
import java.io.File;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
 
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
 
import com.vci.client.portal.NewNewUI.Export.ExportTreeCellRenderer;
import com.vci.client.portal.NewNewUI.Export.VCIExportTree;
import com.vci.client.portal.utility.UITools;
import com.vci.client.ui.tree.CheckBoxTreeManager;
import com.vci.corba.common.VCIError;
import com.vci.corba.portal.*;
import com.vci.corba.portal.data.Constraint;
import com.vci.corba.portal.data.PLAction;
import com.vci.corba.portal.data.PLActionCls;
import com.vci.mw.ClientContextVariable;
 
public class VCIActionExportTree extends VCIExportTree{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private CheckBoxTreeManager  treeManager = null;
    private ExportBeans exportBeans = null;
    private PrintWriter fileLog = null ; 
    private DefaultTableModel tableLog = null;
    private SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
    private HashMap<String,PLActionCls> actionCls = new HashMap<String,PLActionCls>();
    public VCIActionExportTree(DefaultTreeModel treeModel) {
        super(treeModel);    
        this.setCellRenderer(new ExportTreeCellRenderer());
    }
    public VCIActionExportTree() {
        super(new DefaultTreeModel(new DefaultMutableTreeNode("Action分类")));        
        this.setCellRenderer(new ExportTreeCellRenderer());
    }
 
    @Override
    public void init() {
        this.getSelectionModel().setSelectionMode(
                TreeSelectionModel.SINGLE_TREE_SELECTION);
        DefaultTreeModel dtml = new DefaultTreeModel(new DefaultMutableTreeNode("Action分类"));
        this.setModel(dtml);
        // 刷新分类树
        refreshExportTree(this.getPathForRow(0));
        if (treeManager == null) {
            treeManager = new CheckBoxTreeManager(this);
        }else{
            TreePath[] paths = treeManager.getSelectionModel().getSelectionPaths();
            if(paths != null)
            treeManager.getSelectionModel().removeSelectionPaths(paths);
        }
        
    }
    private void refreshExportTree(TreePath pathForRow) {
        try {
            // 分类较少,直接查询所有分类
            PLActionCls[] pLActionClses = UITools.getService().getPLActionClsArray();
            PLAction[]  plActions = UITools.getService().getAllPLAction();    
            //清楚分类id
            actionCls.clear();
            //保存所有分类id
            for (int i = 0; i < pLActionClses.length; i++) {
                actionCls.put(pLActionClses[i].id,pLActionClses[i]);
            }
            //保存所有存在action的分类(子分类存在action也保存)
            HashSet<PLActionCls> pLActionClsList =new HashSet<PLActionCls> ();
            for (int i = 0; i < plActions.length; i++) {
                    //缓存该分类的所有父分类
                    saveParentCls(plActions[i].plActionCls,pLActionClsList);
            }
            PLActionCls[] actionClses = new PLActionCls[pLActionClsList.size()];
            pLActionClsList.toArray(actionClses);
            
            DefaultTreeModel dtml = (DefaultTreeModel) this.getModel();
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) pathForRow
                    .getLastPathComponent();
            addExportTreeNode(dtml, node,actionClses,plActions);
              //展开所有树节点
//                TreeNode rootNode    = (TreeNode)this.getModel().getRoot();
//                this.expandAllTreeNode(new TreePath(rootNode), true);
        } catch (VCIError e1) {
            e1.printStackTrace();
            JOptionPane.showMessageDialog(ClientContextVariable.getFrame(),
                    "刷新分类树异常:" + e1.getMessage(), "系统异常",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
 
/**
 * 保存所有父分类
 * @param plActionCls
 * @param pLActionClsList
 */
    private void saveParentCls(String plActionCls,
            HashSet<PLActionCls> pLActionClsList) {
        if(actionCls.containsKey(plActionCls)){
            PLActionCls pCls = actionCls.get(plActionCls);
            pLActionClsList.add(pCls);
            saveParentCls(pCls.pid,pLActionClsList);
        }
        
    }
    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 (PLAction plAction : plActions) {
 
            if ((!pid.equals("")) && plAction.plActionCls.equals(pid)) {
                DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(
                        new ExportPLActionBean(plAction));
//                TreePath newChildTreePath = new TreePath(newChild);    
//                
                dtml.insertNodeInto(newChild, node,
                        node.getChildCount());
                
            }
        }
        // 添加子节点(源树节点)
        for (PLActionCls plac : pLActionClses) {
    
                if(plac.pid.equals(pid)){
                    DefaultMutableTreeNode child = new DefaultMutableTreeNode(
                            new ExportPLActionClsBean(plac));
                    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 (PLAction plAction : plActions) {
                if (plAction.plActionCls.equals("")) {
                    DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(
                            new ExportPLActionBean(plAction), false);
                    dtml.insertNodeInto(newChild, child, child.getChildCount());
                }
            }
 
        }
        dtml.reload(node);    
        
    }
    @Override
    public String getExportDataFileName(String filePath,Long currentTimes) {
        
        return filePath + File.separator + "VCIACTIONMODELFILE"
                + format.format(new Date(currentTimes)) + ".vciamf";
        
    }
    @Override
    public String getExportLogFileName(String filePath ,Long currentTimes) {
        return filePath + File.separator + "VCIACTIONMODELFILE"
                + format.format(new Date(currentTimes)) + ".log";
    }
 
    public boolean hasSelectExportContent() {
        TreePath[] treePath = treeManager.getSelectionModel()
                .getSelectionPaths();
        if (treePath == null) {
            return false;
        }
        return true;
    }    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public HashMap getExportBeans(HashMap exportBeansMap,PrintWriter log,
            JTable tblExportLog) {
            
          
            this.fileLog = log;
            this.addTblExportLogHeader(tblExportLog);
            this.tableLog= (DefaultTableModel)tblExportLog.getModel();
            this.exportBeans = new ExportBeans();
            while(this.tableLog.getRowCount() > 0 ){
                this.tableLog.removeRow(0);
            }
 
            TreePath[] treePath = treeManager.getSelectionModel()
                    .getSelectionPaths();
            
            if (treePath != null) {
               try {
//                   List<PLActionCls> selActionCls = new ArrayList<PLActionCls>();
//                   List<PLAction> selAction = new ArrayList<PLAction>();
            
                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)) {
                            addAllData();
                            exportBeansMap.put("exportBeans", exportBeans);
                            return exportBeansMap ;
                        }
                    }
                    
                    if (obj instanceof ExportPLActionBean) {
                        //递归所有父节点添加到exportBeans中
                        allPLActionClsParent((DefaultMutableTreeNode) node.getParent());                        
                        this.exportBeans.addPLActionBean(((ExportPLActionBean) obj).getBean());
                        ExportActionLogBean bean = exportBeans.getLogBean();
                        this.fileLog.println(bean.getLogRowContent());
                        this.tableLog.addRow(bean.getLogRowObjcets());
                    }else if (obj instanceof ExportPLActionClsBean) {
                        //递归所有父节点添加到exportBeans中
                            allPLActionClsParent(node);
                        //递归所有子节点添加到exportBeans中
                            allPLActionClsChildren(node);                    
                        
                    }
                    
                    }
                    
               } catch (Exception e) {
                    e.printStackTrace();
                }
               }
            exportBeansMap.put("exportBeans", exportBeans);
            return exportBeansMap ;
 
    }
    
    /**
     * 将数据库中所有action数据导出
     */
    private void addAllData() {
        try {
            PLActionCls[] plActionCls = UITools.getService().getPLActionClsArray();
            
 
             
            for (PLActionCls plActionCls2 : plActionCls) {
                exportBeans.addPLActionClsBean(plActionCls2);
                PLAction[] plActions = getTablePanelData(plActionCls2);
                for (int i = 0; i < plActions.length; i++) {
                        exportBeans.addPLActionBean(plActions[i],plActionCls2.name);
                         ExportActionLogBean bean = exportBeans.getLogBean();
                        this.fileLog.println(bean.getLogRowContent());
                        this.tableLog.addRow(bean.getLogRowObjcets());
//                        tableLog.insertRow(0, bean.getLogRowObjcets());
                    
                }
                
            }        
            
            PLActionCls plac = new PLActionCls("", "未分类", "", "", "", 0, (short)0); 
            exportBeans.addPLActionClsBean(plac);
            PLAction[] plas = getTablePanelData(plac);
            for (int i = 0; i < plas.length; i++) {
                    exportBeans.addPLActionBean(plas[i],plac.name);
                     ExportActionLogBean bean = exportBeans.getLogBean();
                    this.fileLog.println(bean.getLogRowContent());
                    this.tableLog.addRow(bean.getLogRowObjcets());
//                    tableLog.insertRow(0, bean.getLogRowObjcets());
                
            }
            
        } catch (VCIError e) {
            e.printStackTrace();
        }    
        
    }
 
    /**
     * 查询Action数据
     * @param currentCls 
     * @return
     */
    private PLAction[] getTablePanelData(PLActionCls currentCls){
        Constraint[] consArray = new Constraint[1];
        if(currentCls.name.equals("未分类")) {
            consArray[0] = new Constraint("plactioncls", "");
        } else {
            consArray[0] = new Constraint("plactioncls", currentCls.id);
        }
        PLAction[] res = null;
        try {
            res = UITools.getService().getPLActionsByConsArray(consArray);
        } catch (VCIError e) {
            e.printStackTrace();
        }
        return res;
    }
    
    
    private void allPLActionClsParent(DefaultMutableTreeNode node) {
        DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
        PLActionCls pac = ((ExportPLActionClsBean)node.getUserObject()).getBean();
        if(pac.pid != ""){
            allPLActionClsParent(parent);
        }
        exportBeans.addPLActionClsBean(pac);
//        this.addPLActionByPLActionCls(pac);
        
    }
    /**
     * 通过action类别 添加类别下所有action
     * @param pac
     */
    private void addPLActionByPLActionCls(PLActionCls pac) {
         PLAction[]  action = this.getTablePanelData(pac);
         for (PLAction plAction : action) {
                exportBeans.addPLActionBean(plAction,pac.name);
                    ExportActionLogBean bean = exportBeans.getLogBean();
                this.fileLog.println(bean.getLogRowContent());
                this.tableLog.addRow(bean.getLogRowObjcets()); 
        }        
    }
    private void allPLActionClsChildren(DefaultMutableTreeNode node) {
        if(node.getUserObject() instanceof ExportPLActionClsBean){    
        PLActionCls pac = ((ExportPLActionClsBean)node.getUserObject()).getBean();
        exportBeans.addPLActionClsBean(pac);
        this.addPLActionByPLActionCls(pac);
        for(int i=0; i<node.getChildCount(); i++){
            DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(i);
            allPLActionClsChildren(child);
        }
    }    
    }
    
    /**
     * 为log记录Table添加数据
     * @param tblExportLog
     */
    private void addTblExportLogHeader(JTable tblExportLog) {
        tblExportLog.setModel(new DefaultTableModel(new Object[][] { { null,
            null, null, null, null, null, null, null, null }, },
            new String[] { "\u5E8F\u53F7", "\u5BFC\u51FA\u72B6\u6001",
                    "\u7F16\u53F7", "\u540D\u79F0", "CS\u6CE8\u518C",
                    "BS\u6CE8\u518C", "\u7C7B\u578B", "\u63CF\u8FF0",
                    "\u6240\u5C5E\u5206\u7C7B" }) {
        /**
         * serialVersionUID
         */
        private static final long serialVersionUID = 1L;
        boolean[] columnEditables = new boolean[] { false, 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).setMaxWidth(150);
    tblExportLog.getColumnModel().getColumn(8).setMinWidth(60);
    tblExportLog.getColumnModel().getColumn(8).setMaxWidth(150);
    
    }
   
}