ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
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
package com.vci.client.framework.systemConfig.stafforgmanage;
 
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.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JPanel;
 
import com.vci.client.LogonApplication;
import com.vci.client.common.objects.RoleObject;
import com.vci.client.common.objects.UserObject;
import com.vci.client.framework.delegate.RightManagementClientDelegate;
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.common.utility.ObjectUtility;
import com.vci.corba.common.VCIError;
 
public class RolesExcelImportDialogActionListener  extends JPanel implements ActionListener  {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    /**
     * 父窗体
     */
    private RolesImportDialog ownerDialog = null;
    /**
     * 所有Roles信息。
     */
    private ArrayList<RoleObject> fileDatas = new ArrayList<RoleObject>();
    private UserObject logonUserInfo;//
    private String userName = LogonApplication.getUserEntityObject().getUserName();
    
    public RolesExcelImportDialogActionListener(
            RolesImportDialog excelImprotDialog) {
        this.ownerDialog = excelImprotDialog;
    }
 
    public RolesExcelImportDialogActionListener() {
        // 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();
            save();
            
        } else if (actionCommand.equals(ownerDialog.getBtnSelect()
                .getActionCommand())) {
            getFile();
        }     
    }
 
    /**
     * 保存对象
     */
    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("正在导入表单人员信息......");
        boolean b = importExcelData();
        return b;
    }
 
    /**
     * 收集表单信息。
     * 
     * @throws VCIError
     * @throws IOException
     */
    // add by caill start 2016.1.20 收集导入文件中的数据
    private boolean collectionDatas() throws VCIError, IOException{
        LinkedList<File> selectedFiles = this.ownerDialog.getSelectedFiles();
        RoleObject[] preRoles = null;
        int type = logonUserInfo.getUserType();
        if(type < 2){
            type = type+1;
        }
        try {
            //add by caill 2016.1.20根据type获得数据库中已经存在的角色
            preRoles = new RightManagementClientDelegate(LogonApplication.getUserEntityObject()).fetchRoleInfoByRoleType(type);
        } catch (VCIException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        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();
                        for (int i = 1; i < dataSet.size(); i++) {
                            String[] oneData = dataSet.get(i);
                            String userId = ObjectUtility
                                    .getNewObjectID36();
                            RoleObject RolesObj = new RoleObject();
                            RolesObj.setId(userId);
                            boolean flag = false;
                            //add by caill 2016.1.20对于数据库中存在的同名角色,不覆盖不导入
                            for(int j = 0;j < preRoles.length;j++){
                                if(preRoles[j].getName().equals(oneData[0])){
                                    flag = true;
                                }
                            }
                            if(oneData[0] != null && !oneData[0].trim().equals("") && flag == false){
                                RolesObj.setName(oneData[0]); // 名称
                                RolesObj.setDescription(oneData[1]);// 描述
                                RolesObj.setType(logonUserInfo.getUserType());
                                RolesObj.setCreateTime(System.currentTimeMillis());
                                RolesObj.setCreateUser(userName);
                                RolesObj.setUpdateTime(System.currentTimeMillis());
                                RolesObj.setUpdateUser(userName);
                                RolesObj.setGrantor("");
                                
                                fileDatas.add(RolesObj);
                            }
                            
                        }
                    }
                }
            }
        }
        return true;
    }
    /**
     * 导入表单数据
     * @throws VCIException 
     */
    //add by caill start 2016.1.20将导入的数据插入到数据库中
    private boolean importExcelData() throws VCIError {
        boolean b = new RightManagementClientDelegate(LogonApplication.getUserEntityObject()).batchSaveRoles(fileDatas.toArray(new RoleObject[]{}));
        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 File getExcelFile() {
        File file = null;
        String filePath = VCISwingUtil.getExcelFileURL(ownerDialog, true, "");
        if (filePath != null) {
            file = new File(filePath);
        }
        return file;
    }
 
}