wangting
2025-01-16 18c43123b51a1688ab4ae01fe3d171c7d92e619b
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
package com.vci.client.portal.NewNewUI.actionmng;
 
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
 
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 
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.corba.portal.data.PLActionParam;
import com.vci.client.LogonApplication;
import com.vci.client.portal.utility.UITools;
import com.vci.common.utility.ObjectUtility;
import com.vci.corba.common.VCIError;
 
public class ImpAction {
 
    private Map<String, PLActionCls> clsMap;
 
    public ImpAction(){
        this.parseActionCls();
    }
    public boolean impExcel(){
        JFileChooser jf = new JFileChooser();
        jf.setDialogTitle("打开");
        jf.removeChoosableFileFilter(jf.getFileFilter());
        jf.setFileFilter(new FileFilter() {
            public String getDescription() {
                return "xls";
            }
            public boolean accept(File f) {
                return ((f.isDirectory()) || (f.getName().endsWith(".xls")));
            }
        });
        int showOpenDialog = jf.showOpenDialog(LogonApplication.frame);
        if(showOpenDialog == JFileChooser.APPROVE_OPTION){
            File selectedFile = jf.getSelectedFile();
            String fileName = selectedFile.getAbsolutePath();
            if(!fileName.endsWith(".xls")){
                fileName = fileName + ".xls";
            }
            FileInputStream fin = null;
            try {
                fin = new FileInputStream(fileName);
                HSSFWorkbook workbook = new HSSFWorkbook(fin);
                this.importActionCls(workbook);
                List<PLAction> actions = new ArrayList<PLAction>();
                this.importActions(workbook, actions);
                this.importActionParams(workbook, actions);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                if(fin != null){
                    try{
                        fin.close();
                    }catch(Exception e){
                    }
                }
            }
        }
        return false;
    }
 
    private void importActionParams(HSSFWorkbook workbook,    List<PLAction> actions) {
        HSSFSheet sheet = workbook.getSheet("param_sheet");
        HSSFRow titleRow = sheet.getRow(0);
        for(int i = 1; i<=sheet.getLastRowNum(); i++){
            HSSFRow row = sheet.getRow(i);
            this.implActionParam(row, titleRow, actions);
        }
    }
    private void implActionParam(HSSFRow row, HSSFRow titleRow, List<PLAction> actions) {
        PLActionParam actionParam = null;
        for(int i=0; i< titleRow.getLastCellNum(); i++){
            String title = titleRow.getCell(i).getStringCellValue();
            /*int cellType = titleRow.getCell(i).getCellType();
            String value = "";
            if(cellType == HSSFCell.CELL_TYPE_NUMERIC){
                value = String.valueOf(row.getCell(i).getNumericCellValue());
            }else if(cellType == HSSFCell.CELL_TYPE_STRING){
                value = row.getCell(i).getStringCellValue();
            }*/
            String value = row.getCell(i).toString();
            if(value == null || value.trim().length() == 0){
                continue;
            }
            if(actionParam == null){
                actionParam = new PLActionParam();
            }
            if(title.equals("参数名称")){
                actionParam.name = value.trim();
            }else if(title.equals("默认值")){
                actionParam.defaultValue = value.trim();
            }else if(title.equals("提示信息")){
                actionParam.description = value.trim();
            }else if(title.equals("所属action")){
                actionParam.action = value.trim();
            }
        }
        try {
            if(actionParam != null){
                String actionName = actionParam.action;
                String actionParamName = actionParam.name;
                for(PLAction action: actions){
                    if(actionName.equals(action.plCode)){
                        actionParam.action =  action.plOId;
                        PLActionParam[] params = UITools.getService().getPLActionParamArrayByActionId(action.plOId);
                        boolean isExist = false;
                        for(PLActionParam param: params){
                            if(actionParamName.equals(param.name)){
                                isExist = true;
                                break;
                            }
                        }
                        if(!isExist){
                            UITools.getService().createPLActionParam(actionParam);
                        }
                    }
                }
            }
        } catch (VCIError e) {
            e.printStackTrace();
        }
    }
    private void importActionCls(HSSFWorkbook workbook) {
        HSSFSheet sheet = workbook.getSheet("cls_sheet");
        HSSFRow titleRow = sheet.getRow(0);
        for(int i = 1; i<=sheet.getLastRowNum(); i++){
            HSSFRow row = sheet.getRow(i);
            this.implActionCls(row, titleRow);
        }
    }
 
    private void implActionCls(HSSFRow row, HSSFRow titleRow) {
        PLActionCls actionCls = null;
        for(int i=0; i< titleRow.getLastCellNum(); i++){
            String title = titleRow.getCell(i).getStringCellValue();
            int cellType = titleRow.getCell(i).getCellType();
            //String value = "";
            String value = row.getCell(i).toString();
            /*if(cellType == HSSFCell.CELL_TYPE_NUMERIC){
                value = String.valueOf(row.getCell(i).getNumericCellValue());
            }else if(cellType == HSSFCell.CELL_TYPE_STRING){
                value = row.getCell(i).getStringCellValue();
            }*/
            if(value == null || value.trim().length() == 0){
                continue;
            }
            if(actionCls == null){
                actionCls = new PLActionCls();
            }
            if(title.equals("类全路径")){
                String clsPath = value.trim();
                if(clsMap.get(clsPath) != null){
                    actionCls = null;
                    break;
                } 
                if(clsPath.contains("#")){
                    int lastIndex = clsPath.lastIndexOf("#");
                    PLActionCls parent = clsMap.get(clsPath.substring(0, lastIndex));
                    actionCls.pid = parent.id;
                    actionCls.name = clsPath.substring(lastIndex+1, clsPath.length());
                }else{
                    actionCls.pid = "";
                    actionCls.name = value.trim();
                }
            }else if(title.equals("描述")){
                actionCls.description = value.trim();
            }else if(title.equals("创建人")){
                actionCls.creator = value.trim();
            }else if(title.equals("创建时间")){
                actionCls.createTime = Long.valueOf(value.trim());
            }else if(title.equals("分类序号")){
                actionCls.serialno = Short.valueOf(value.trim());
            }
        }
        if(actionCls != null){
            UITools.getService().creaetePLActionCls(actionCls);
            this.parseActionCls();
        }
    }
 
    private String getClsPath(PLActionCls cls) {
        try {
            PLActionCls[] clses = UITools.getService().getPLActionClsArray();
            StringBuilder sbuf = new StringBuilder();
            this.parseClsPath(cls, clses, sbuf);
            return sbuf.substring(0, sbuf.length()-1).toString();
        } catch (VCIError e) {
            e.printStackTrace();
        }
        return "";
    }
    private void parseClsPath(PLActionCls cls, PLActionCls[] clses, StringBuilder sbuf) {
        if(cls.pid != null && cls.pid.trim().length() > 0){
            for(PLActionCls actionCls: clses){
                if(cls.pid.equals(actionCls.id)){
                    this.parseClsPath(actionCls, clses, sbuf);
                }
            }
        }
        sbuf.append(cls.name).append("#");
    }
 
    private void importActions(HSSFWorkbook workbook, List<PLAction> selActions) {
        List<PLAction> actions = new ArrayList<PLAction>();
        HSSFSheet sheet = workbook.getSheet("action_sheet");
        HSSFRow titleRow = sheet.getRow(0);
        for(int i = 1; i<=sheet.getLastRowNum(); i++){
            HSSFRow row = sheet.getRow(i);
            PLAction action = this.parse2Action(row, titleRow);
            if(action != null){
                actions.add(action);
            }
        }
        System.out.println(actions.size());
        try {
            for(PLAction action: actions){
                Constraint[] consArray = new Constraint[6];
                consArray[0] = new Constraint("plcode", action.plCode);
                consArray[1] = new Constraint("plname", action.plName);
                consArray[2] = new Constraint("plbsurl", action.plBSUrl);
                consArray[3] = new Constraint("plcsclass", action.plCSClass);
                consArray[4] = new Constraint("pltypetype", action.plTypeType);
                consArray[5] = new Constraint("plactioncls", action.plActionCls);
                PLAction[] actionArr = UITools.getService().getPLActionsByConsArray(consArray);
                if(actionArr != null && actionArr.length > 0){
                    selActions.add(actionArr[0]);
                    continue;
                }
                action.plOId = ObjectUtility.getNewObjectID36();
                UITools.getService().savePLAction(action);
                selActions.add(action);
            }
        } catch (VCIError e) {
            e.printStackTrace();
        }
    }
    
    private PLAction parse2Action(HSSFRow row, HSSFRow titleRow) {
        PLAction action = null;
        for(int i=0; i< titleRow.getLastCellNum(); i++){
            String title = titleRow.getCell(i).getStringCellValue();
            /*int cellType = titleRow.getCell(i).getCellType();
            String value = "";
            if(cellType == HSSFCell.CELL_TYPE_NUMERIC){
                value = String.valueOf(row.getCell(i).getNumericCellValue());
            }else if(cellType == HSSFCell.CELL_TYPE_STRING){
                value = row.getCell(i).getStringCellValue();
            }*/
            String value = row.getCell(i).toString();
            if(value == null || value.trim().length() == 0){
                continue;
            }
            if(action == null){
                action = new PLAction();
            }
            if(title.equals("编号")){
                action.plCode = value.trim();
            }else if(title.equals("名称")){
                action.plName = value.trim();
            }else if(title.equals("类路径")){
                action.plCSClass = value.trim();
            }else if(title.equals("链接地址")){
                action.plBSUrl = value.trim();
            }else if(title.equals("类型")){
                action.plTypeType = value.trim();
            }else if(title.equals("描述")){
                action.plDesc = value.trim();
            }else if(title.equals("创建人")){
                action.plCreateUser = value.trim();
            }else if(title.equals("创建时间")){
                action.plCreateTime = Long.valueOf(value.trim());
            }else if(title.equals("修改人")){
                action.plModifyUser = value.trim();
            }else if(title.equals("修改时间")){
                action.plModifyTime = Long.valueOf(value.trim());
            }else if(title.equals("所属分类")){
                PLActionCls plActionCls = clsMap.get(value.trim());
                if(plActionCls != null){
                    action.plActionCls = plActionCls.id;
                }else{
                    action.plActionCls = "";
                }
            }else if(title.equals("plLicensOrs")){
                action.plLicensOrs = value.trim();
            }
        }
        return action;
    }
 
    private void parseActionCls() {
        try {
            clsMap = new HashMap<String, PLActionCls>();
            PLActionCls[] allClses = UITools.getService().getPLActionClsArray();
            for(PLActionCls cls: allClses){
                String clsPath = this.getClsPath(cls);
                clsMap.put(clsPath, cls);
            }
        } catch (VCIError e) {
            e.printStackTrace();
        }
    }
}