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
package com.vci.client.framework.specialrole;
 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
 
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
 
import com.vci.client.LogonApplication;
import com.vci.client.common.objects.RoleObject;
import com.vci.client.common.objects.UserEntityObject;
import com.vci.client.common.objects.UserObject;
import com.vci.client.framework.delegate.RightManagementClientDelegate;
import com.vci.client.framework.systemConfig.stafforgmanage.ListLabelCellRenderForRightRole;
import com.vci.client.logon.base.BaseJDialog;
import com.vci.client.ui.exception.VCIException;
import com.vci.client.ui.image.BundleImage;
import com.vci.client.ui.locale.LocaleDisplay;
import com.vci.client.ui.swing.VCIOptionPane;
import com.vci.corba.framework.data.RoleInfo;
 
/**
 * 角色增加成员界面管理
 * @author wangxl
 *
 */
public class SelectUserDialog extends BaseJDialog{
 
    private static final long serialVersionUID = 1L;
    private JButton conformBut = new JButton(LocaleDisplay.getI18nString("rmip.stafforg.operate.conform", "RMIPFramework", getLocale()));
    private JButton cancelBut = new JButton(LocaleDisplay.getI18nString("rmip.stafforg.operate.cancel", "RMIPFramework", getLocale()));
    private JList  allUserList = new JList();  //所有成员
    private JList choosedUserList = new JList(); //拥有成员
    private UserObject logonUserInfo = LogonApplication.getUserObject();
    private RoleObject rightRoleObj;
    
    private RightManagementClientDelegate _RMCDelegate = new RightManagementClientDelegate(LogonApplication.getUserEntityObject());
    
    private JTextField searceText = new JTextField();
    public SelectUserDialog(RoleObject rightRoleObj){
        super(LogonApplication.frame);
        this.rightRoleObj = rightRoleObj;
        
        this.setModal(true);
        init();
    }
     
    
    /**
     * 初始化界面
     */
    private void init() {
         JLabel titleLabel = new JLabel();
         titleLabel.setText("角色添加成员");
         titleLabel.setIcon(new BundleImage().createImageIcon ("role.png"));
         JPanel bottomPanel = new JPanel();
         conformBut.setPreferredSize(new Dimension(50,25));
         bottomPanel.add(conformBut);
         cancelBut.setPreferredSize(new Dimension(50,25));
         bottomPanel.add(cancelBut);
         
         JPanel contentPanel = initCenterContentPanel();
         initUser(); //初始化成员信息
         JPanel midPanel = new JPanel();
         midPanel.setLayout(new BorderLayout());
         JTextField jTextField1 = new JTextField();//instead of up line
         JTextField jTextField2 = new JTextField();//instead of down line
         jTextField1.setPreferredSize(new Dimension(63,2));
         jTextField2.setPreferredSize(new Dimension(63,2));
         midPanel.add(jTextField1, BorderLayout.NORTH);
         midPanel.add(jTextField2, BorderLayout.SOUTH);
         midPanel.add(contentPanel, BorderLayout.CENTER);
         
         this.setLayout(new BorderLayout());
         this.add(titleLabel, BorderLayout.NORTH);
         this.add(midPanel, BorderLayout.CENTER);
         this.add(bottomPanel, BorderLayout.SOUTH); 
//         int x = (int)(this.getParent().getLocationOnScreen().getX()) + 200;
//         int y = (int)(this.getParent().getLocationOnScreen().getY()) + 80;
//         this.setLocation(x , y);
//         this.setSize(600, 450);
         initDialogSize(600, 450);
 
    }
    
    private JPanel initCenterContentPanel() {
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(null);
        
        JScrollPane jspAllRole = new JScrollPane();
        jspAllRole.getViewport().add(allUserList);
        
        JLabel searchName = new JLabel("角色人员");
        JButton queryBtn = new JButton("查询");
        searchName.setBounds(new Rectangle(100,10,90,26));
        searceText.setBounds(new Rectangle(160,10,180,26));
        queryBtn.setBounds(new Rectangle(370,10,60,26));
        queryBtn.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent arg0) {
                DefaultListModel allListModel = (DefaultListModel) allUserList.getModel();
                String queryStr = searceText.getText().trim();
                allListModel.removeAllElements();
                initUser();
                if(!"".equals(queryStr)){
                    int size = allUserList.getModel().getSize();
                    allListModel = new DefaultListModel();
                    for(int i=0;i<size;i++){
                        UserObject userObject = (UserObject)allUserList.getModel().getElementAt(i);
                        if(userObject.getTrueName().contains(queryStr)||userObject.getUserName().contains(queryStr)){
                            allListModel.addElement(userObject);
                        }
                    }
                    allUserList.setModel(allListModel);
                }
            }
        });
        contentPanel.add(searchName);
        contentPanel.add(searceText);
        contentPanel.add(queryBtn);
        
        jspAllRole.setPreferredSize(new Dimension(250,300));
        jspAllRole.setBounds(new Rectangle(10,50,250,300));
        jspAllRole.setBorder(new TitledBorder("角色外成员"));
        allUserList.setCellRenderer(new ListLabelCellRenderForRightRole());
        
        JScrollPane jspChooseRole = new JScrollPane();
        jspChooseRole.getViewport().add(choosedUserList);
        jspChooseRole.setPreferredSize(new Dimension(250,300));
        jspChooseRole.setBounds(new Rectangle(330,50,250,300));
        jspChooseRole.setBorder(new TitledBorder("角色内成员"));
        choosedUserList.setCellRenderer(new ListLabelCellRenderForRightRole());
        
        DefaultListModel selectListModel = new DefaultListModel();
        String roleId = rightRoleObj.getId();
        int userType = logonUserInfo.getUserType();
        try {
            UserObject[] selectUserObjs = _RMCDelegate.fetchUserInfoByRoleId(roleId ,userType );
            if (selectUserObjs.length != 0){
                for (int i = 0 ; i < selectUserObjs.length ; i ++){
//                    // 三员管理中,不能移除自己的三员角色 Jason 2019.7.2
//                    if (selectUserObjs[i].getId().equals(logonUserInfo.getId()))
//                        continue;
                    
                    selectListModel.addElement(selectUserObjs[i]);
                }
            }
        } catch (VCIException e1) {
            VCIOptionPane.showError(LogonApplication.frame,"RMIPFramework", e1);
        }
       
        choosedUserList.setModel(selectListModel);
        JButton addButton = new JButton(">>");
        addButton.setBounds(270, 100, 50, 25);
        
        JButton removeButton = new JButton("<<");
        removeButton.setBounds(270, 250, 50, 25);
        
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addButton_actionPerformed();
            }
          });
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                removeButton_actionPerformed();
            }
          });
        
        conformBut.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                conformCreate();
            }
        });
        cancelBut.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                cancelCreate();
            }
        });
        contentPanel.add(jspChooseRole);
        contentPanel.add(jspAllRole);
        contentPanel.add(addButton);
        contentPanel.add(removeButton);
        return contentPanel;
    }
    
    /**
     * 初始化成员信息
     */
    private void initUser(){
        try{
            // 1、可选的用户是全部的用户
//            UserObject[] userInfo = new RightManagementClientDelegate(LogonApplication.getUserEntityObject())
//                                           .fetchUserInfo();
            UserObject[] userInfo = _RMCDelegate.fetchUserInfo();
            
            List<String> lstThreeRoleUserID = new ArrayList<String>();
            Boolean isThreeRole = isThreeRole(rightRoleObj.getId());
            if (isThreeRole) {
                getRoleUserIDs("系统管理员", lstThreeRoleUserID);
                getRoleUserIDs("安全管理员", lstThreeRoleUserID);
                getRoleUserIDs("审计管理员", lstThreeRoleUserID);
//                RoleObject role = _RMCDelegate.fetchRoleByName("系统管理员");
//                UserObject[] users = _RMCDelegate.fetchUsersByRoleId(role.getId());
//                for (UserObject user : users) {
//                    lstThreeRoleUserID.add(user.getId());
//                }
            }
            
            int length = userInfo.length;
            DefaultListModel alllistModel = new DefaultListModel();
            DefaultListModel selectlistModel = (DefaultListModel) choosedUserList.getModel();
            int selectLenth = selectlistModel.getSize();
            for (int i = 0; i < length; i++) {
                // 2、二次过滤 
                // 为三员角色分配成员时,可行的用户是普通用户
                if (userInfo[i].getUserType() < 2 || userInfo[i].getStatus() == 1){
                    continue;
                }
                // 对三员角色分配用户时,不能给自己添加三员角色,Jason 2019.7.2
                if (userInfo[i].getId().equalsIgnoreCase(logonUserInfo.getId())){
                    continue;
                }
                
                // 用户只能具有一个三员角色
                if (isThreeRole && lstThreeRoleUserID.contains(userInfo[i].getId()))
                    continue;
                
                boolean hasSelect = false;
                for (int j = 0; j < selectLenth; j++) {
                    UserObject selectUserInfo = (UserObject)selectlistModel.get(j);
                    if (selectUserInfo.getId().equals(userInfo[i].getId())) {
                        hasSelect = true;
                        break;
                    }
                }
                if (!hasSelect) {
                    alllistModel.addElement(userInfo[i]);
                }
            }
            allUserList.setModel(alllistModel);
        }  catch (VCIException e) {
            VCIOptionPane.showError(LogonApplication.frame,"RMIPFramework", e);
            return ;
        }
 
    }
    
    private void getRoleUserIDs(String roleName, List<String> lstId) {
        try {
            RoleObject role = _RMCDelegate.fetchRoleByName(roleName);
            UserObject[] users = _RMCDelegate.fetchUsersByRoleId(role.getId());
            for (UserObject user : users) {
                lstId.add(user.getId());
            }
        } catch (VCIException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    private Boolean isThreeRole(String roleId){
        try{
            RoleObject role = _RMCDelegate.fetchRoleInfoById(roleId);
            String sRole = role.getName();
            if (sRole.equalsIgnoreCase("系统管理员") || sRole.equalsIgnoreCase("安全管理员") || sRole.equalsIgnoreCase("审计管理员"))
                return true;
            
            return false;
        }catch (VCIException e) {
            VCIOptionPane.showError(LogonApplication.frame, "RMIPFramework", e);
            return false;
        }
    }
    
    
    private Boolean hasThreeRole(UserObject user){
        try{
            RoleObject[] roles = _RMCDelegate.fetchRoleInfoByUserId(user.getId());
            for (RoleObject role : roles){
                String sRole = role.getName();
                if (sRole.equalsIgnoreCase("系统管理员") || sRole.equalsIgnoreCase("安全管理员") || sRole.equalsIgnoreCase("审计管理员"))
                    return true;
            }
        }catch (VCIException e) {
            VCIOptionPane.showError(LogonApplication.frame, "RMIPFramework", e);
            return false;
        }
        
        return false;
    }
    
    
    /**
     * 左移
     */
     private void addButton_actionPerformed() {
            
            Object[] objs = allUserList.getSelectedValues();
            if ((objs == null) || (objs.length == 0)) {
                VCIOptionPane.showMessageDialog(this,
                        "请选择要增加的成员!");
                return;
            }
            int len = objs.length;
            UserObject[] addUserInfo = new UserObject[len];
            for (int i = 0; i < len; i++) {
                addUserInfo[i] = (UserObject)objs[i];
            }
            DefaultListModel selectListModel = (DefaultListModel) choosedUserList.getModel();
            DefaultListModel allListModel = (DefaultListModel) allUserList.getModel();
            
            int size = selectListModel.getSize();
            for (int i = 0; i < size; i++) {
                UserObject entity = (UserObject) selectListModel.getElementAt(i);
                for (int j = 0; j < len; j++) {
                    if (entity.getId().equals(addUserInfo[j].getId())) {
                        VCIOptionPane.showMessageDialog(this, 
                                "要增加的成员已经存在,请重新选择!");
                        return;
                    }
                }
            }
            for (int i = 0; i < len; i++) {
                selectListModel.addElement(addUserInfo[i]);
                allListModel.removeElement(addUserInfo[i]);
            }
            
        }
        
     /**
      * 右移
      */
        private void removeButton_actionPerformed() {
           
            Object[] objs = choosedUserList.getSelectedValues();
            if ((objs == null) || (objs.length == 0)) {
                VCIOptionPane.showMessageDialog(this, "请选择要移除的成员!");
                return;
            }
            
            int len = objs.length;
            List<UserObject> deleteUser = new ArrayList<UserObject>();
            for (int i = 0; i < len; i++) {
                UserObject user = (UserObject)objs[i];
                // 三员管理中,不能移除自己的三员角色 Jason 2019.7.2
                if (user.getId().equals(logonUserInfo.getId()))
                    continue;
 
                deleteUser.add(user);
            }
            
            len = deleteUser.size();
            
            DefaultListModel listModel = (DefaultListModel) choosedUserList.getModel();
            DefaultListModel allListModel = (DefaultListModel) allUserList.getModel();
            for (int i = 0; i < listModel.size(); i++) {
                UserObject entity = (UserObject) listModel.getElementAt(i);
                for (int j = 0; j < len; j++) {
                    if (entity.getId().equals(deleteUser.get(j).getId())) {
                        listModel.removeElement(entity);
                        allListModel.addElement(entity);
                        i--;
                        break;
                    }
                }
            }
        }
        
        /**
         * 得到选中的角色
         * @return
         */
        public UserObject[] getSelectedUserObj(){
            DefaultListModel listModel = (DefaultListModel) choosedUserList.getModel();
            int size = listModel.size();
            UserObject[] userInfo = new UserObject[size];
            for (int i = 0; i < size; i++) {
                userInfo[i] = (UserObject)listModel.get(i);
            }
            return userInfo;
        }
        
        /**
         * 确定事件
         */
        public void conformCreate(){
            String roleId = this.rightRoleObj.getId();
            String[] userIds;
            UserObject[] selectedUserObj = this.getSelectedUserObj();
            
            userIds = new String[selectedUserObj.length];
            for (int i = 0 ; i < selectedUserObj.length ; i ++){
                userIds[i] = selectedUserObj[i].getId();
            }
            try{
                new RightManagementClientDelegate(LogonApplication.getUserEntityObject()).saveRight(roleId , userIds);
            }catch(VCIException e){
                VCIOptionPane.showError(LogonApplication.frame,"RMIPFramework", e);
                return;
            }
            this.dispose();
       }
        public void cancelCreate(){
              dispose();
        }
 
}