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
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
package com.vci.client.omd.attribpool.ui;
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.HashMap;
 
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
 
import com.vci.client.common.datatype.VTDouble;
import com.vci.client.common.datatype.VTInteger;
import com.vci.client.common.datatype.VTLong;
import com.vci.client.common.datatype.VTString;
import com.vci.client.omd.attribpool.toOutside.InterAP;
import com.vci.client.omd.attribpool.toOutside.InterAPManager;
import com.vci.client.omd.attribpool.toOutside.Tool;
import com.vci.client.omd.attribpool.toOutside.UsedNames;
import com.vci.corba.common.VCIError;
import com.vci.corba.omd.atm.AttribItem;
import com.vci.mw.InvocationUtility;
import com.vci.omd.dataType.VTDataType;
 
/**
 * 属性项界面
 * @author Administrator
 *
 */
public class AttribPanel extends JPanel{
 
    /**
     * 
     */
    private static final long serialVersionUID = -3191437674655429531L;
    private final String SYSUSEDNAMES = "sysUsedNames";
    private final String DATABASEUSEDNAMES = "dataBaseUsedNames";
    /**
     * 0:显示; 1: 创建; 2: 修改
     */
    private int flag;
    private JTextField tfName, tfLabel;
    private JTextArea taDescrip;
    private JComboBox combVTType;
    //vtTypePanel: 数据类型改变时,动态加载不同的Panel
    private JPanel centerPanel, vtTypePanel, btnPanel;
    private JButton btnOK, btnCancel;
    //各个数据类型独自的panel
    private VTDataTypePanel dataTypePanel;
    private AttribItem attribItem;
    private HashMap<String, String> detailInfoMap = null;
    /**
     * oracle 查询的最大标识符是30, 为解决多表查询时, Hibernate
     * 覆盖同名列的值, 故留一位后缀(2)用作别名
     */
    private final int NAME_MAX_LENGTH = 29;
    private static AttribPanel attribPanel = null;
    
    public static AttribPanel getInstance(){
        if(attribPanel == null){
            attribPanel = new AttribPanel();
        }
        return attribPanel;
    }
    
    public AttribPanel(){
        initUI();
        addListener();
    }
    
    private void initUI(){
        this.setBackground(Color.WHITE);
        this.setLayout(new BorderLayout(0, 0));
        this.setBorder(BorderFactory.createTitledBorder("属性项"));
        
        //northPanel
        JPanel northPanel = new JPanel();
        this.add(northPanel, BorderLayout.NORTH);
        centerPanel = new JPanel();
        centerPanel.setBackground(Color.white);
        centerPanel.setLayout(new BorderLayout());
        this.add(centerPanel, BorderLayout.CENTER);
        
        vtTypePanel = new JPanel();
        vtTypePanel.setLayout(new BorderLayout());
        centerPanel.add(vtTypePanel);
        btnPanel = new JPanel();
        btnOK = new JButton("确定");
        btnCancel = new JButton("取消");
        btnPanel.add(btnOK);
        btnPanel.add(btnCancel);
        centerPanel.add(btnPanel, BorderLayout.SOUTH);
        
        northPanel.setLayout(new GridBagLayout());
        GridBagConstraints g = new GridBagConstraints();
        g.anchor = GridBagConstraints.EAST;
        g.insets = new Insets(10, 5, 0, 5);
        g.gridx = 0;
        g.gridy = 0;
        northPanel.add(new JLabel("名称"), g);
        
        tfName = new JTextField(35);
        GridBagConstraints g1 = new GridBagConstraints();
        g1.insets = new Insets(10, 5, 0, 5);
        g1.gridx = 1;
        g1.gridy = 0;
        northPanel.add(tfName, g1);
        g.gridx = 2;
        northPanel.add(new JLabel("标签"), g);
        tfLabel = new JTextField(35);
        g1.gridx = 3;
        g1.weighty = 1.0;
        g1.fill = GridBagConstraints.HORIZONTAL;
        g1.gridwidth = GridBagConstraints.REMAINDER;
        northPanel.add(tfLabel, g1);
        
        g.gridx = 0;
        g.gridy = 1;
        northPanel.add(new JLabel("描述"), g);
        taDescrip = new JTextArea(3, 20);
        taDescrip.setLineWrap(true);
        g1.gridx = 1;
        g1.gridy = 1;
        northPanel.add(new JScrollPane(taDescrip), g1);
        
        g.gridx = 0;
        g.gridy = 2;
        northPanel.add(new JLabel("属性类型"), g);
        combVTType = new JComboBox();
        initCombVTType();
        g1.gridx = 1;
        g1.gridy = 2;
        northPanel.add(combVTType, g1);
        
    }
    
    /**
     * @param abItem : 用来显示和修改属性项
     * @param flag : 当前状态(创建,修改,删除)
     */
    public void updateDataAndUI(AttribItem abItem, int flag){
        this.flag = flag;
        if(abItem != null){
            tfName.setText(abItem.name);
            tfLabel.setText(abItem.label);
            taDescrip.setText(abItem.description);
            combVTType.setSelectedItem(abItem.vtDataType);
            attribItem = abItem;
        }else{
            tfName.setText("");
            tfLabel.setText("");
            taDescrip.setText("");
            initCombVTType();
        }
        
        if(flag == 0){
            //名称,标签,描述,属性类型  组件群
            {
                tfName.setEditable(false);
                tfLabel.setEditable(false);
                taDescrip.setEditable(false);
                combVTType.setEnabled(false);
            }
            dataTypePanel.updataUIStatus(abItem, flag);
            btnOK.setVisible(false);
            btnCancel.setVisible(false);
        }
        
        
        if(flag == 1){
            //名称,标签,描述,属性类型  组件群
            {
                tfName.setEditable(true);
                tfLabel.setEditable(true);
                taDescrip.setEditable(true);
                combVTType.setEnabled(true);
            }
            dataTypePanel.updataUIStatus(abItem, flag);
            btnOK.setVisible(true);
            btnCancel.setVisible(true);
        }
        
        if(flag == 2){
            //名称,标签,描述,属性类型  组件群
            {
                tfName.setEditable(false);
                tfLabel.setEditable(true);
                taDescrip.setEditable(true);
                combVTType.setEnabled(true);
            }
            dataTypePanel.updataUIStatus(abItem, flag);
            btnOK.setVisible(true);
            btnCancel.setVisible(true);
        }
    }
    
    public void addCancelActionListener(ActionListener listener) {
        btnCancel.addActionListener(listener);
    }
    
    /**
     * 为CombVTType添加数据类型
     */
    private void initCombVTType(){
        combVTType.removeAllItems();
        for(int i = 0; i < VTDataType.DateType.length; i++){
            combVTType.addItem(VTDataType.DateType[i]);
        }
    }
    
    /**
     * 监听CombVTType
     */
    private void typeItemChanged(ItemEvent e){
        
        String typeItem = (String) combVTType.getSelectedItem();
        if(typeItem.equals(VTDataType.VTSTRING)){
            dataTypePanel = new VTStringPanel();
        }else if(typeItem.equals(VTDataType.VTINTEGER)){
            dataTypePanel = new VTIntegerPanel();
        }else if(typeItem.equals(VTDataType.VTLONG)){
            dataTypePanel = new VTLongPanel();
        }else if(typeItem.equals(VTDataType.VTDOUBLE)){
            dataTypePanel = new VTDoublePanel();
        }else if(typeItem.equals(VTDataType.VTBOOLEAN)){
            dataTypePanel = new VTBooleanPanel();
//        }else if(typeItem.equals(VTDataType.VTIMAGE)){
//            dataTypePanel = new VTImagePanel();
        }else if(typeItem.equals(VTDataType.VTDATE)){
            dataTypePanel = new VTDatePanel();
        }else if(typeItem.equals(VTDataType.VTTIME)){
            dataTypePanel = new VTTimePanel();
        }else if(typeItem.equals(VTDataType.VTDATETIME)){
            dataTypePanel = new VTDateTimePanel();
        }else if(typeItem.equals(VTDataType.VTNOTE)){
            dataTypePanel = new VTNotePanel();
        }else if(typeItem.equals(VTDataType.VTFILEPATH)){
            dataTypePanel = new VTFilePathPanel();
        }else if(typeItem.equals(VTDataType.VTCLOB)){
            dataTypePanel = new VTClobPanel();
        }
            
        if(dataTypePanel == null){
            dataTypePanel = new VTStringPanel();
        }
        vtTypePanel.removeAll();
        vtTypePanel.updateUI();
        vtTypePanel.add(dataTypePanel, BorderLayout.CENTER);
    }
    
    
    private void addListener(){
        btnOK.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e){
                //创建属性项
                if(flag == 1){
                    attribItem = new AttribItem();
                    if(!checkName()){
                        return;
                    }
                    detailInfoMap = dataTypePanel.getDetailInfo();
                    if(!checkDefValue()){
                        return;
                    }
                    attribItem.name = tfName.getText().toLowerCase().replaceAll(" ", "");
                    attribItem.label = tfLabel.getText();
                    attribItem.description = taDescrip.getText();
                    attribItem.vtDataType = (String)combVTType.getSelectedItem();
                    attribItem.defValue = detailInfoMap.get(VTDataTypePanel.DEFVALUE) == null ? "" : detailInfoMap.get(VTDataTypePanel.DEFVALUE);
                    attribItem.rage = detailInfoMap.get(VTDataTypePanel.RAGE) == null ? "" : detailInfoMap.get(VTDataTypePanel.RAGE);
                    attribItem.other = detailInfoMap.get(VTDataTypePanel.OTHER) == null ? "" : detailInfoMap.get(VTDataTypePanel.OTHER);
                    String userName = InvocationUtility.getInvocation().userName;
                    attribItem.creator = userName;
                    attribItem.modifier = userName;
                    boolean addSuccess = false;
                    try {
                        addSuccess = APClient.getService().addAttribItem(attribItem);
                    } catch (VCIError e1) {
                        e1.printStackTrace();
                    }
                    if(!addSuccess){
                        JOptionPane.showMessageDialog(null, "添加属性失败", "添加属性失败", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    JOptionPane.showMessageDialog(null, "添加属性成功", "添加属性成功", JOptionPane.INFORMATION_MESSAGE);
                    AttribPoolPanel.getInstance().initTableData();
                    AttribItem selectedAb = AttribPoolPanel.getInstance().getSelectedAb();
                    updateDataAndUI(selectedAb, 0);
                    AttribPoolPanel.getInstance().setFlag(0);
                }else if(flag == 2){
                    if(attribItem == null){
                        JOptionPane.showMessageDialog(null, "请选择要修改的属性项", "选择属性项", JOptionPane.WARNING_MESSAGE);
                    }
                    
                    detailInfoMap = dataTypePanel.getDetailInfo();
                    if(!checkDefValue()){
                        return;
                    }
                    boolean compatible = isCompatible(attribItem);
                    boolean hasInstance = hasInstance(attribItem.name);
                    //产生数据, 并且不兼容
                    if(hasInstance && !compatible){
                        JOptionPane.showMessageDialog(null, "无效变更, 不兼容已产生的数据", "无效变更", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    attribItem.label = tfLabel.getText();
                    attribItem.description = taDescrip.getText();
                    attribItem.vtDataType = (String)combVTType.getSelectedItem();
                    attribItem.defValue = detailInfoMap.get(VTDataTypePanel.DEFVALUE) == null ? "" : detailInfoMap.get(VTDataTypePanel.DEFVALUE);
                    attribItem.rage = detailInfoMap.get(VTDataTypePanel.RAGE) == null ? "" : detailInfoMap.get(VTDataTypePanel.RAGE);
                    attribItem.other = detailInfoMap.get(VTDataTypePanel.OTHER) == null ? "" : detailInfoMap.get(VTDataTypePanel.OTHER);
                    attribItem.modifier = InvocationUtility.getInvocation().userName;
                    try {
                        boolean mdSuccess = APClient.getService().modifyAbItem(attribItem);
                        if(!mdSuccess){
                            JOptionPane.showMessageDialog(null, "修改属性失败", "修改属性失败", JOptionPane.ERROR_MESSAGE);
                            return;
                        }
                        JOptionPane.showMessageDialog(null, "修改属性成功", "修改属性成功", JOptionPane.INFORMATION_MESSAGE);
                        AttribPoolPanel.getInstance().initTableData();
                        
                        //修改业务类型, 链接类型中该属性字段
                        ArrayList<InterAP> interAPList = InterAPManager.getInstance().getInterAPList();
                        for(int i = 0; i < interAPList.size(); i++){
                            InterAP interAP = interAPList.get(i);
                            interAP.alterAp(attribItem.name);
                        }
                    } catch (VCIError e1) {
                        e1.printStackTrace();
                    }
                    
                }
                
            }
        });
        
        btnCancel.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                if(flag == 1){
                    updateDataAndUI(null, 0);
                }else if(flag == 2){
                    updateDataAndUI(attribItem, 0);
                }
            }
        });
        
        combVTType.addItemListener(new ItemListener() {
            
            @Override
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange() == ItemEvent.SELECTED){
                    typeItemChanged(e);
                }
            }
        });
    }
    
    private boolean checkName(){
        if(tfName.getText().equals("")){
            JOptionPane.showMessageDialog(this, "请输入属性名", "注意", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        
        String name = tfName.getText();
        if(!name.matches("[a-z A-Z]*")){
            JOptionPane.showMessageDialog(this, "属性名只能为英文字母", "注意", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        int length = name.length();
        if(length > NAME_MAX_LENGTH){
            JOptionPane.showMessageDialog(this, "属性名长度不能超过:" + NAME_MAX_LENGTH, "属性名过长", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        String abName = name.toLowerCase();
        if(usedBySystem(abName)){
            JOptionPane.showMessageDialog(this, "属性名已被系统属性使用", "属性名无效", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        if(usedByDataBase(abName)){
            JOptionPane.showMessageDialog(this, "属性名是数据库关键字", "属性名无效", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        try {
            if(APClient.getService().checkRowIsExists(abName)){
                JOptionPane.showMessageDialog(this, "属性名已经存在", "注意", JOptionPane.WARNING_MESSAGE);
                return false;
            }
        } catch (VCIError e) {
            e.printStackTrace();
        } catch (HeadlessException e) {
            e.printStackTrace();
        }
        return true;
    }
    
    /**
     * 检查默认值与属性类型是否匹配
     * @return
     */
    private boolean checkDefValue(){
        
        String defValue = detailInfoMap.get(VTDataTypePanel.DEFVALUE);
        String vtType = (String)combVTType.getSelectedItem();
        String rageTemp = detailInfoMap.get(VTDataTypePanel.RAGE);
        String rages = null;
        if(rageTemp != null){
            rages = detailInfoMap.get(VTDataTypePanel.RAGE);
        }
        
        if(defValue != null && !defValue.equals("")){
            
            if(vtType.equals(VTDataType.VTSTRING)){
                try{
                    String.valueOf(defValue);
                }catch(Exception e){
                    JOptionPane.showMessageDialog(null, "请输入String类型的默认值", "请输入正确的默认值", JOptionPane.ERROR_MESSAGE);
                    return false;
                }
                if(rages == null || rages.equals("")){
                    return true;
                }
                
                VTString obj = new VTString(String.valueOf(defValue));
                boolean flag = obj.checkRageValueByRage(rages);
                if(!flag){
                    JOptionPane.showMessageDialog(null, "默认值与值域冲突", "默认值与值域冲突", JOptionPane.ERROR_MESSAGE);
                    return false;
                }
            }else if(vtType.equals(VTDataType.VTINTEGER)){
                try{
                    Integer.valueOf(defValue);
                }catch(Exception e){
                    JOptionPane.showMessageDialog(null, "请输入Integer类型的默认值", "请输入正确的默认值", JOptionPane.ERROR_MESSAGE);
                    return false;
                }
                if(rages == null || rages.equals("")){
                    return true;
                }
                
                VTInteger obj = new VTInteger(Integer.valueOf(defValue));
                boolean flag = obj.checkRageValueByRage(rages);
                if(!flag){
                    JOptionPane.showMessageDialog(null, "默认值与值域冲突", "默认值与值域冲突", JOptionPane.ERROR_MESSAGE);
                    return false;
                }
            }else if(vtType.equals(VTDataType.VTLONG)){
                try{
                    Long.valueOf(defValue);
                }catch(Exception e){
                    JOptionPane.showMessageDialog(null, "请输入Long类型的默认值", "请输入正确的默认值", JOptionPane.ERROR_MESSAGE);
                    return false;
                }
                if(rages == null || rages.equals("")){
                    return true;
                }
                
                VTLong obj = new VTLong(Long.valueOf(defValue));
                boolean flag = obj.checkRageValueByRage(rages);
                if(!flag){
                    JOptionPane.showMessageDialog(null, "默认值与值域冲突", "默认值与值域冲突", JOptionPane.ERROR_MESSAGE);
                    return false;
                }
            }else if(vtType.equals(VTDataType.VTDOUBLE)){
                try{
                    Double.valueOf(defValue);
                }catch(Exception e){
                    JOptionPane.showMessageDialog(null, "请输入Double类型的默认值", "请输入正确的默认值", JOptionPane.ERROR_MESSAGE);
                    return false;
                }
                if(rages == null || rages.equals("")){
                    return true;
                }
                VTDouble obj = new VTDouble(Double.valueOf(defValue));
                boolean flag = obj.checkRageValueByRage(rages);
                if(!flag){
                    JOptionPane.showMessageDialog(null, "默认值与值域冲突", "默认值与值域冲突", JOptionPane.ERROR_MESSAGE);
                    return false;
                }
                
            }
        }
        
        return true;
    }
    
    /**
     * 获取other中指定项目的值
     * @return
     */
    public String getOtherValueByType(String other, String type){
        String[] otherArray = other.split(";");
        for(int i = 0; i < otherArray.length; i++){
            String otherValue = otherArray[i];
            if(otherValue.contains(type)){
                return otherValue.substring(otherValue.indexOf("=") + 2, otherValue.length());
            }
        }
        return null;
        
    }
    
    /**
     * 修改属性时, 判断当前输入的属性是否能兼容之前的属性
     * @param abItem
     * @return
     */
    private boolean isCompatible(AttribItem abItem){
        //TODO
        String dataType = abItem.vtDataType;
        String other = abItem.other;
        String newOther = detailInfoMap.get(VTDataTypePanel.OTHER) == null ? "" : detailInfoMap.get(VTDataTypePanel.OTHER);
        String newType = (String)combVTType.getSelectedItem();
        if(newType.equals(VTDataType.VTSTRING)){
            if(dataType.equals(VTDataType.VTINTEGER) || dataType.equals(VTDataType.VTLONG)){
                return true;
            }
            if(dataType.equals(VTDataType.VTSTRING)){
                int length = Integer.valueOf(getOtherValueByType(other, "length"));
                int newLen = Integer.valueOf(getOtherValueByType(newOther, "length"));
                if(length <= newLen){
                    return true;
                }else{
                    return false;
                }
            }
        }
        
        if(newType.equals(dataType)){
            return true;
        }
        
        return false;
    }
    
    /**
     * 判断该属性是否已经在业务类型中, 或者链接类型中产生了数据
     * @param abName
     * @return
     */
    private boolean hasInstance(String abName){
        ArrayList<InterAP> interAPList = InterAPManager.getInstance().getInterAPList();
        for(int i = 0; i < interAPList.size(); i++){
            InterAP interAP = interAPList.get(i);
            boolean flag = interAP.hasInstance(abName);
            if(flag){
                return flag;
            }
        }
        return false;
    }
    
    /**
     * 检查该属性名是否被系统属性使用
     * @param abName
     * @return
     */
    private boolean usedBySystem(String abName){
        boolean flag = false;
        String[] names = UsedNames.getProperty(SYSUSEDNAMES).toUpperCase().split(",");
        ArrayList<String> nameList = Tool.convertArrayToList(names);
        if(nameList.contains(abName.toUpperCase())){
            flag = true;
        }
        return flag;
    }
    
    /**
     * 检查该属性名是否属于数据库关键字
     * @param abName
     * @return
     */
    private boolean usedByDataBase(String abName){
        boolean flag = false;
        String[] names = UsedNames.getProperty(DATABASEUSEDNAMES).toUpperCase().split(",");
        ArrayList<String> nameList = Tool.convertArrayToList(names);
        if(nameList.contains(abName.toUpperCase())){
            flag = true;
        }
        return flag;
    }
}