wangting
2024-09-27 a3e87f78ee262ca9bb7d9b0c997639d5f3295890
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
package com.vci.client.uif.engine.client.tableArea;
 
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextField;
 
import com.vci.client.portal.utility.DataModelFactory;
import com.vci.client.portal.utility.PLDefination;
import com.vci.client.portal.utility.PRM;
import com.vci.client.portal.utility.PRMItem;
import com.vci.client.portal.utility.UITools;
import com.vci.client.ui.swing.VCISwingUtil;
import com.vci.client.ui.swing.components.VCIJButton;
import com.vci.client.ui.swing.components.VCIJComboBox;
import com.vci.client.ui.swing.components.VCIJDialog;
import com.vci.client.ui.swing.components.VCIJLabel;
import com.vci.client.ui.swing.components.VCIJPanel;
import com.vci.client.ui.swing.components.VCIJScrollPane;
import com.vci.client.ui.swing.components.VCIJTextField;
import com.vci.client.uif.actions.client.UIFCache;
import com.vci.client.uif.actions.client.UIFUtils;
import com.vci.client.uif.engine.client.controls.ControlFactory;
import com.vci.client.uif.engine.client.controls.ICustomControl;
import com.vci.client.uif.engine.client.controls.VCIJComboBoxModelValueObject;
import com.vci.corba.common.VCIError;
import com.vci.corba.omd.atm.AttribItem;
import com.vci.corba.portal.data.PortalVI;
import com.vci.mw.ClientContextVariable;
import com.vci.omd.dataType.VTDataType;
 
public class TablePanalAdvanceSearchDialog extends VCIJDialog implements ActionListener{
 
    /**
     * 
     */
    private static final long serialVersionUID = 4547945867911768830L;
    
    private String btmType = "";
    private String context = "";
    private PLDefination defination = null;
    private DataModelFactory factory = null;
    private TablePanel tablePanel = null;
    
    private VCIJButton btnSearch = VCISwingUtil.createVCIJButton("search", "查询", "查询", "search.png", this);
    private VCIJButton btnCancel = VCISwingUtil.createVCIJButton("cancel", "取消", "取消", "cancel.png", this);
    
    private HashMap<String, VCIJLabel> ctlLblMap = new HashMap<String, VCIJLabel>();
    private HashMap<String, VCIJTextField> ctlComptMap = new HashMap<String, VCIJTextField>();
    
 
    private final String AND_NAME = "并且";
    private final String AND_VALUE = "\\AND";
    private final String OR_NAME = "或者";
    private final String OR_VALUE = "\\OR";
    
    private final String EQUAL_NAME = "等于";
    private final String EQUAL_NOT_NAME = "不等于";
    private final String EQUAL_NOT_VALUE = "!=";
    private final String CONTAIN_NAME = "包含";
    private final String CONTAIN_VALUE = "*";
    private final String FUZZY = "模糊";
    private final String GREAT_NAME = "大于";
    private final String GREAT_VALUE = ">";
    private final String GREAT_EQUAL_NAME = "大于等于";
    private final String GREAT_EQUAL_VALUE = ">=";
    private final String LESS_NAME = "小于";
    private final String LESS_VALUE = "<";
    private final String LESS_EQUAL_NAME = "小于等于";
    private final String LESS_EQUAL_VALUE = "<=";
    
    public TablePanalAdvanceSearchDialog(
            String type, String context,
            PLDefination defination, DataModelFactory factory,
            TablePanel tablePanel) {
        
        super(ClientContextVariable.getFrame(), true);
        
        this.btmType = type;
        this.context = context;
        this.defination = defination;
        
        this.factory = factory;
        this.tablePanel = tablePanel;
        
    }
    
    public void init(){
        
        setIconImage(VCISwingUtil.createImageIcon("search.png").getImage());
        
        setTitle("高级查询");
        
        setLayout(new BorderLayout());
 
        add(new VCIJScrollPane(getCenterAreaPanel()), BorderLayout.CENTER);
        
        add(getSouthButtonPanel(), BorderLayout.SOUTH);
    }
    
    private VCIJPanel getCenterAreaPanel(){
        VCIJPanel pal = new VCIJPanel(new GridBagLayout());
 
        PortalVI tableDef = null;
        try {
            if(!"".equals(getDefination().getLinkType())){
                tableDef = factory.getTableDefinationByTypeAndTableName(getDefination().getLinkType(), getDefination().getTemplateId());
            } else {
                tableDef = factory.getTableDefinationByTypeAndTableName(getDefination().getShowType(), getDefination().getTemplateId());
            }
            PRM tablePrm = UITools.getPRM(tableDef.prm);
            if(tablePrm == null) return pal;
            String[] queryCols = tablePrm.getPrmItemList().get(0).getItemKeyFields().split(",");
            if(queryCols == null) return pal;
            if (queryCols.length == 0) {
                return pal;
            }
            // 第个属性出现多少次
            Map<String, Integer> colAndUseCountMap = tablePrm.getPrmItemList().get(0).getColAndUseCountMap();
            if(colAndUseCountMap == null || colAndUseCountMap.size() == 0){
                return pal;
            }
            // 属性对应的名称
            PortalVI formView = factory.getFormViewById(tablePrm.getPrmItemList().get(0).getItemInObj());
            PRM formPrm = UITools.getPRM(formView.prm);
            //Map<String, String> colAndNameMap = formPrm.getColAndNameMap();
            //得到每一个控件配置信息
            Map<String, PRMItem> prmItemMap = UIFUtils.getColAndPRMItemMap(formPrm);
            
            pal = loadAttrToPanel(colAndUseCountMap, prmItemMap);
            
        } catch (VCIError e) {
            e.printStackTrace();
        }
        return pal;
    }
    
    private VCIJPanel loadAttrToPanel(Map<String, Integer> mapNums, Map<String, PRMItem> prmItemMap){
 
        VCIJPanel pal = new VCIJPanel(new GridBagLayout());
        
        Iterator<String> keyNums = mapNums.keySet().iterator();
 
        int gridx = -1;
        int gridy = -1;
        
        while(keyNums.hasNext()){
 
            gridx = 0;
            gridy++;
            
            String field = keyNums.next();
            Integer count = mapNums.get(field);
            PRMItem item = prmItemMap.get(field);
            if(item == null) {
                continue;
            }
            //去掉item必填和只读的属性
            item.setItemIsEditable("0");
            item.setItemIsRequired("0");
            
            //获得属性的类型
            String type = field;
            if(type.indexOf(".") != -1) {
                type = type.substring(type.lastIndexOf(".") + 1);
            }
            //判断是否是系统默认的属性
            if(UIFUtils.getDefaultAttributeDataType(type) != null) {
                type = UIFUtils.getDefaultAttributeDataType(type);
            } else {
                AttribItem ai = UIFCache.getInstance().getAllAttributeMap().get(type);
                if(ai != null) {
                    type = ai.vtDataType;
                }
            }
            
            VCIJLabel lbl = new VCIJLabel(item.getItemName());
            VCIJComboBox cbx = new VCIJComboBox(getNewComboBoxModel(type));
            Component txt = ControlFactory.createControl(item);
            
            pal.add(lbl, getGBC(gridx, gridy, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE));
            
            gridx++;
            pal.add(cbx, getGBC(gridx, gridy, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE));
            
            gridx++;
            GridBagConstraints gbc = getGBC(gridx, gridy, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH);
            if(count == 1){
                Insets insets = gbc.insets;
                gbc.insets = new Insets(insets.top, insets.left, 10, insets.right);
            }
            pal.add(txt, gbc);
            
            LinkedList<InnerStruct> list = new LinkedList<InnerStruct>();
            InnerStruct is = new InnerStruct(field, null, cbx, txt);
            list.add(is);
            
            singleFilterMap.put(field, list);
            
            if(count <= 1) {
                continue;
            }
            
            gridy = add(count, gridy, pal, field, item);
        }
        
        gridy++;
        VCIJLabel lblFill = new VCIJLabel("");
        pal.add(lblFill, getGBC(0, gridy, 3, 1, 1.0, 1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH));
        
        return pal;
    }
    
    private int  add(int count, int gridyCurrent, VCIJPanel pal, String field, PRMItem item){
        int gridx = 0;
        int gridy = gridyCurrent;
        
        LinkedList<InnerStruct> dupFilterList = new LinkedList<InnerStruct>();
        
        for(int i = 2; i <= count; i++){
            //获得属性的类型
            String type = field;
            if(type.indexOf(".") != -1) {
                type = type.substring(type.lastIndexOf(".") + 1);
            }
            //判断是否是系统默认的属性
            if(UIFUtils.getDefaultAttributeDataType(type) != null) {
                type = UIFUtils.getDefaultAttributeDataType(type);
            } else {
                AttribItem ai = UIFCache.getInstance().getAllAttributeMap().get(type);
                if(ai != null) {
                    type = ai.vtDataType;
                }
            }
            
            VCIJComboBox cbxAndOr = new VCIJComboBox(getNewAndOrComboBoxModel());
            VCIJComboBox cbxFilter = new VCIJComboBox(getNewComboBoxModel(type));
            Component txtFilter = ControlFactory.createControl(item);
            
            gridx = 0;
            gridy++;
            pal.add(cbxAndOr, getGBC(gridx, gridy, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE));
            
            gridx++;
            pal.add(cbxFilter, getGBC(gridx, gridy, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE));
 
            gridx++;
            GridBagConstraints gbc = getGBC(gridx, gridy, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH);
            if(i == count){
                Insets insets = gbc.insets;
                gbc.insets = new Insets(insets.top, insets.left, 10, insets.right);
            }
            pal.add(txtFilter, gbc);
            
            InnerStruct is = new InnerStruct(field, cbxAndOr, cbxFilter, txtFilter);
            dupFilterList.add(is);
        }
        
        // 存储每个属性重复出现的查询条件数据结构
        dupFilterMap.put(field, dupFilterList);
        
        return gridy;
    }
    
    /**
     * 
     */
    private HashMap<String, LinkedList<InnerStruct>> singleFilterMap = new HashMap<String, LinkedList<InnerStruct>>();
    
    /**
     * 同一个属性,重复定义出的那些组查询条件
     */
    private HashMap<String, LinkedList<InnerStruct>> dupFilterMap = new HashMap<String, LinkedList<InnerStruct>>();
    
    class InnerStruct{
        private String field = "";
        private VCIJComboBox cbxAndOr = null;
        private VCIJComboBox cbxFilter = null;
        private Component vlaueCompt = null;
        public InnerStruct(
                String field, 
                VCIJComboBox cbxAndOr, VCIJComboBox cbxFilter,
                Component txtValue) {
            super();
            this.cbxAndOr = cbxAndOr;
            this.cbxFilter = cbxFilter;
            this.vlaueCompt = txtValue;
        }
        public String getField() {
            return field;
        }
        public void setField(String field) {
            this.field = field;
        }
        public VCIJComboBox getCbxAndOr() {
            return cbxAndOr;
        }
        public void setCbxAndOr(VCIJComboBox cbxAndOr) {
            this.cbxAndOr = cbxAndOr;
        }
        public VCIJComboBox getCbxFilter() {
            return cbxFilter;
        }
        public void setCbxFilter(VCIJComboBox cbxFilter) {
            this.cbxFilter = cbxFilter;
        }
        public Component getVlaueCompt() {
            return vlaueCompt;
        }
        public void setVlaueCompt(Component txtValue) {
            this.vlaueCompt = txtValue;
        }
        
    }
    
    private DefaultComboBoxModel getNewAndOrComboBoxModel(){
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        model.addElement(AND_NAME);
        model.addElement(OR_NAME);
        return model;
    }
    
    /**
     * 获得属性类型支持的查询条件
     * TODO 未完成
     * @param type 属性的类型
     * @return
     */
    private DefaultComboBoxModel getNewComboBoxModel(String type){
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        if(type.equals(VTDataType.VTSTRING)) {
            model.addElement(EQUAL_NAME); //等于
            model.addElement(EQUAL_NOT_NAME); //不等于
            model.addElement(CONTAIN_NAME); //包含
            model.addElement(FUZZY); //模糊
        } else if (type.equals(VTDataType.VTINTEGER)
                || type.equals(VTDataType.VTLONG) 
                || type.equals(VTDataType.VTDOUBLE)) {
            model.addElement(EQUAL_NAME); //等于
            model.addElement(EQUAL_NOT_NAME); //不等于
            model.addElement(GREAT_NAME); //大于
            model.addElement(GREAT_EQUAL_NAME); //大于等于
            model.addElement(LESS_NAME); //小于
            model.addElement(LESS_EQUAL_NAME); //小于等于
        } else if (type.equals(VTDataType.VTBOOLEAN)) {
            model.addElement(EQUAL_NAME); //等于
            model.addElement(EQUAL_NOT_NAME); //不等于
        } else if (type.equals(VTDataType.VTDATETIME)
                || type.equals(VTDataType.VTDATE)
                || type.equals(VTDataType.VTTIME)) {
            model.addElement(EQUAL_NAME); //等于
            model.addElement(EQUAL_NOT_NAME); //不等于
            model.addElement(GREAT_NAME); //大于
            model.addElement(GREAT_EQUAL_NAME); //大于等于
            model.addElement(LESS_NAME); //小于
            model.addElement(LESS_EQUAL_NAME); //小于等于
        } else {
            model.addElement(EQUAL_NAME);
        }
//        model.addElement(EQUAL_NAME);
//        model.addElement(EQUAL_NOT_NAME);
//        model.addElement(CONTAIN_NAME);
//        model.addElement(GREAT_NAME);
//        model.addElement(GREAT_EQUAL_NAME);
//        model.addElement(LESS_NAME);
//        model.addElement(LESS_EQUAL_NAME);
        return model;
    }
 
    private GridBagConstraints getGBC(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill) {
        int padxy = 2;
        return new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, new Insets(padxy, padxy, padxy, padxy), padxy, padxy);
    }
    
    private VCIJPanel getSouthButtonPanel(){
        VCIJPanel pal = new VCIJPanel();
        pal.add(btnSearch);
        pal.add(btnCancel);
        return pal;
    }
    
    
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        String actionCommand = e.getActionCommand();
        if(actionCommand.equals(btnSearch.getActionCommand())) search();
        else if(actionCommand.equals(btnCancel.getActionCommand())) cancel();
    }
    
    
    private void search(){
        HashMap<String, String> conditionMap = getSearchConditionMap();
        this.tablePanel.getDataTablePanel().setPageIndex(1);
        this.tablePanel.getDataProvider().setLoadByCondition(true);
        this.tablePanel.getDataProvider().setNormalSearchFilterMap(conditionMap);
        this.tablePanel.getDataTablePanel().refreshTableData();
        
        cancel();
    }
    
    private HashMap<String, String> getSearchConditionMap(){
        HashMap<String, String> conditionMap = new HashMap<String, String>();
        
        Iterator<String> keys = singleFilterMap.keySet().iterator();
        while(keys.hasNext()){
            String field = keys.next();
            InnerStruct singleStruct = singleFilterMap.get(field).get(0);
            
            VCIJComboBox cbxFilterSignle = singleStruct.getCbxFilter();
            Component txtValueSingle = singleStruct.getVlaueCompt();
            
            String filter = cbxFilterSignle.getSelectedItem().toString();
            String value = getComponentValue(txtValueSingle);
 
            String valueToAdd = "";
            if(EQUAL_NAME.equals(filter)){
                valueToAdd = value;
            } else if(EQUAL_NOT_NAME.equals(filter)) {
                valueToAdd = EQUAL_NOT_VALUE + value;
            } else if(CONTAIN_NAME.equals(filter)){
                valueToAdd = CONTAIN_VALUE + value + CONTAIN_VALUE;
            } else if(FUZZY.equals(filter)){
                if(value.contains("%")){
                    value = value.replace("%", "*");
                }
                valueToAdd = value;
            }else if(GREAT_NAME.equals(filter)) {
                valueToAdd = GREAT_VALUE + value;
            } else if(GREAT_EQUAL_NAME.equals(filter)) {
                valueToAdd = GREAT_EQUAL_VALUE + value;
            } else if(LESS_NAME.equals(filter)) {
                valueToAdd = LESS_VALUE + value;
            } else if(LESS_EQUAL_NAME.equals(filter)) {
                valueToAdd = LESS_EQUAL_VALUE + value;
            }
            
            String dupFilterValue = getDupFilterConditonString(field);
            String mergeFilterValue = valueToAdd + dupFilterValue;
            if(!"".equals(mergeFilterValue)){ 
                conditionMap.put(field, mergeFilterValue);
            }
        }
        
        return conditionMap;
    }
    private String getDupFilterConditonString(String field){
        String res = "";
        LinkedList<InnerStruct> dupFliterList = dupFilterMap.get(field);
        if(dupFliterList == null) return res;
        else if(dupFliterList.size() == 0) return res;
        
        for(InnerStruct is : dupFliterList){
            VCIJComboBox cbxAndOr = is.getCbxAndOr();
            VCIJComboBox cbxFilter = is.getCbxFilter();
            Component cbxValue = is.getVlaueCompt();
            
            String value = getComponentValue(cbxValue);
            if(value == null || "".equals(value)) continue;
            
            String filter = cbxFilter.getSelectedItem().toString();
            
            String valueToAdd = "";
            if(EQUAL_NAME.equals(filter)){
                valueToAdd = value;
            } else if(EQUAL_NOT_NAME.equals(filter)) {
                valueToAdd = EQUAL_NOT_VALUE + value;
            } else if(CONTAIN_NAME.equals(filter)){
                valueToAdd = CONTAIN_VALUE + value + CONTAIN_VALUE;
            } else if(GREAT_NAME.equals(filter)) {
                valueToAdd = GREAT_VALUE + value;
            } else if(GREAT_EQUAL_NAME.equals(filter)) {
                valueToAdd = GREAT_EQUAL_VALUE + value;
            } else if(LESS_NAME.equals(filter)) {
                valueToAdd = LESS_VALUE + value;
            } else if(LESS_EQUAL_NAME.equals(filter)) {
                valueToAdd = LESS_EQUAL_VALUE + value;
            }
            
            String andOr = cbxAndOr.getSelectedItem().toString();
            if(AND_NAME.equals(andOr)){
                valueToAdd = AND_VALUE + valueToAdd;
            } else if(OR_NAME.equals(andOr)){
                valueToAdd = OR_VALUE + valueToAdd;
            }
            
            res += valueToAdd;
            
        }
        return res;
    }
    
    private void cancel(){
        setDialogResult(DialogResult.CANCEL);
        this.setVisible(false);
        this.dispose();
    }
    
    /**
     * 获得控件的值
     * @param compt
     * @return
     */
    private String getComponentValue(Component compt){
        String value = "";
        if(JTextField.class.isAssignableFrom(compt.getClass())){
            JTextField txtCompt = (JTextField)compt;
            String text = txtCompt.getText();
            value = text;
        } else if(compt instanceof VCIJComboBox){
            VCIJComboBox cbx = (VCIJComboBox)compt;
            ComboBoxModel model = cbx.getModel();
            Object selectObj = model.getSelectedItem();
            if(selectObj instanceof VCIJComboBoxModelValueObject){
                VCIJComboBoxModelValueObject vo  = (VCIJComboBoxModelValueObject) selectObj;
                value = vo.getValue();
            }
        } else if(compt instanceof ICustomControl){
            ICustomControl ctrl = (ICustomControl)compt;
            value = ctrl.getValue();
        }
        return value;
    }
 
    public String getBtmType() {
        return btmType;
    }
 
    public void setBtmType(String type) {
        this.btmType = type;
    }
 
    public String getContext() {
        return context;
    }
 
    public void setContext(String context) {
        this.context = context;
    }
 
    public PLDefination getDefination() {
        return defination;
    }
 
    public void setDefination(PLDefination defination) {
        this.defination = defination;
    }
 
    public DataModelFactory getFactory() {
        return factory;
    }
 
    public void setFactory(DataModelFactory factory) {
        this.factory = factory;
    }
}