田源
2024-03-07 4b4083fd73dc27ece42f4835483565eef0e4f608
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
package com.vci.client.omd.linktype;
 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.HighlighterFactory;
 
import com.vci.corba.common.VCIError;
import com.vci.corba.omd.ltm.LinkType;
 
public class ConsistencyCheckDialog extends JDialog{
 
    /**
     * 
     */
    private static final long serialVersionUID = 6997597482667501498L;
    Map<String, String> dbCheckMap;
    Map<String, List<String>> btmCheckMap;
    private JPanel centerPanel;
    private JPanel southPanel;
    private JButton btnRepair;
    private JButton btnCancel;
    private JXTable table;
    private DefaultTableModel model;
    private final int TABLE_HEADER_HEIGHT = 25;
    private final int ROW_HEIGHT = 30;
    
    public ConsistencyCheckDialog(Map<String, String> dbCheckMap, Map<String, List<String>> btmCheckMap){
        this.dbCheckMap = dbCheckMap;
        this.btmCheckMap = btmCheckMap;
        initUI();
        addListener();
        initData();
    }
 
    private void initUI(){
        this.setTitle("修复对话框");
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setSize(screenSize.width/2, screenSize.height/2);
        this.setModal(true);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setLayout(new BorderLayout());
        
        centerPanel = new JPanel();
        centerPanel.setLayout(new BorderLayout());
        southPanel = new JPanel();
        southPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        this.add(centerPanel, BorderLayout.CENTER);
        this.add(southPanel, BorderLayout.SOUTH);
        
        table = new JXTable();
        model = new DefaultTableModel();
        model.setColumnCount(3);
        model.setColumnIdentifiers(new String[]{"类型名 ", "操作", "状态"});
        table.setModel(model);
        table.setHorizontalScrollEnabled(true);
        table.setAutoscrolls(true);
        table.setSortable(false);
        table.setHighlighters(HighlighterFactory.createAlternateStriping());
        table.setRowHeight(ROW_HEIGHT);
        //设置表头高度
        JTableHeader tableHeader = table.getTableHeader();
        Dimension size = tableHeader.getPreferredSize();
        size.height = TABLE_HEADER_HEIGHT;
        tableHeader.setPreferredSize(size);
        
        centerPanel.add(new JScrollPane(table), BorderLayout.CENTER);
        
        btnRepair = new JButton("修复");
        btnCancel = new JButton("关闭");
        southPanel.add(btnRepair);
        southPanel.add(btnCancel);
    }
    
    private void initData(){
        table.setEditable(true);
        model.setRowCount(dbCheckMap.size() + btmCheckMap.size());
        int i = 0;
        for(Iterator<String> ite = dbCheckMap.keySet().iterator(); ite.hasNext();){
            String typeName = ite.next();
            String dml = dbCheckMap.get(typeName);
            table.setValueAt(typeName, i, 0);
            String dml_ = dml.replace("_CREATE", "创建表");
            dml_ = dml_.replace("_ADD", "增加列");
            dml_ = dml_.replace("_DROP", "移除列");
            table.setValueAt(dml_, i, 1);
            table.setValueAt("未修复", i, 2);
            i++;
        }
        
        for(Iterator<String> ite = btmCheckMap.keySet().iterator(); ite.hasNext();){
            String typeName = ite.next();
            List<String> btms = btmCheckMap.get(typeName);
            StringBuilder stb = new StringBuilder("移除form端业务类型:");
            StringBuilder stb_ = new StringBuilder("移除to端业务类型:");
            for(int k = 0; k < btms.size(); k++){
                String btm = btms.get(k);
                String btm_ = btm.substring(2);
                if(btm.contains("F_")){
                    stb.append(btm_);
                    stb.append(",");
                }else{
                    stb_.append(btm_);
                    stb_.append(",");
                }
            }
            StringBuilder result = new StringBuilder("");
            String stbString = stb.toString();
            if(stbString.contains(",")){
                stbString = stb.substring(0, stbString.lastIndexOf(","));
                result.append(stbString);
            }
            
            String stbString_ = stb_.toString();
            if(stbString_.contains(",")){
                stbString_ = stb_.substring(0, stbString_.lastIndexOf(","));
                if(!result.equals("")){
                    result.append(";");
                }
                result.append(stbString_);
            }
            table.setValueAt(typeName, i, 0);
            table.setValueAt(result.toString(), i, 1);
            table.setValueAt("未修复", i, 2);
            i++;
        }
        table.setEditable(false);
    }
    
    private void addListener() {
        btnRepair.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                if(dbCheckMap.size() < 1 && btmCheckMap.size() < 1){
                    JOptionPane.showMessageDialog(getDialog(), "已经全部成功修复", "已经全部成功修复", JOptionPane.INFORMATION_MESSAGE);
                    return;
                }
                if(JOptionPane.YES_OPTION != JOptionPane.showConfirmDialog(
                        getDialog(), "修复将改变数据库或类型数据文件的结构", "修复确认框", JOptionPane.YES_NO_OPTION )){
                    return;
                }
                if(dbCheckMap.size() > 0){
                    List<String> list = getRepairDML();
                    if(list.size() < 1){
                        return;
                    }
                    try {
                        String[] result = LinkTypeStart.getService().executeRepair(list.toArray(new String[0]));
                        updateStatus(result);
                    } catch (VCIError e1) {
                        e1.printStackTrace();
                    }
                }
                
                if(btmCheckMap.size() > 0){
                    List<String> result = repairXml();
                    updateStatus_(result);
                }
            }
 
        });
        
        btnCancel.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                dipose_();
            }
        });
    }
    
    private void dipose_(){
        this.dispose();
    }
    
    /**
     * 获取需要修复的伪sql
     * @return
     */
    private List<String> getRepairDML() {
        List<String> list = new ArrayList<String>();
        for(Iterator<String> ite = dbCheckMap.keySet().iterator(); ite.hasNext();){
            String type = ite.next();
            String dml = dbCheckMap.get(type);
            list.add(type + "/DML" + dml);
        }
        return list;
    }
    
    /**
     * 根据修复的结果, 更新table中的状态
     * 并且将已成功的type, 从 map中移除
     * 部分成功的, 移除map中value的成功部分
     * @param result
     */
    private void updateStatus(String[] result) {
        table.setEditable(true);
        List<String> list = Arrays.asList(result);// LinkTypeProvider.getInstance().parseArrayToList(result);
        for(int i = 0; i < table.getRowCount(); i++){
            String typeName = (String) table.getValueAt(i, 0);
            String operation = (String) table.getValueAt(i, 1);
            //第二列包含"移除业务类型"的需要修改的是链接类型的xml文件
            if(operation.contains("业务类型")){
                continue;
            }
            if(list.contains(typeName)){
                table.setValueAt("已修复", i, 2);
                dbCheckMap.remove(typeName);
            }else if(list.contains(typeName + "_ADD")){
                table.setValueAt("增加已修复", i, 2);
                String sql = dbCheckMap.get(typeName);
                sql = sql.substring(sql.indexOf(";") + 1, sql.length());
                dbCheckMap.put(typeName, sql);
            }else if(list.contains(typeName + "_DROP")){
                table.setValueAt("移除已修复", i, 2);
                String sql = dbCheckMap.get(typeName);
                sql = sql.substring(0, sql.indexOf(";"));
                dbCheckMap.put(typeName, sql);
            }else{
                if(!((String) table.getValueAt(i, 2)).equals("已修复")){
                    table.setValueAt("修复失败", i, 2);
                }
            }
        }
        table.updateUI();
        table.setEditable(false);
    }
    
    /**
     * 根据修复的结果, 更新table中的状态
     * 并且将已成功的type, 从 map中移除
     * 部分成功的, 移除map中value的成功部分
     * @param result
     */
    private void updateStatus_(List<String> result) {
        table.setEditable(true);
        for(int i = 0; i < table.getRowCount(); i++){
            String typeName = (String) table.getValueAt(i, 0);
            String operation = (String) table.getValueAt(i, 1);
            //第二列包含"移除业务类型"的需要修改的是链接类型的xml文件
            if(operation.contains("业务类型")){
                if(result.contains(typeName)){
                    table.setValueAt("已修复", i, 2);
                    btmCheckMap.remove(typeName);
                }
            }
        }
        table.updateUI();
        table.setEditable(false);
    }
    
    private JDialog getDialog(){
        return this;
    }
    
    /**
     * 修复链接类型的xml文件
     * @return
     */
    private List<String> repairXml(){
        List<String> result = new ArrayList<String>();
        for(Iterator<String> ite = btmCheckMap.keySet().iterator(); ite.hasNext();){
            String linkName = ite.next();
            List<String> list = btmCheckMap.get(linkName);
            LinkType link = null;
            try {
                link = LinkTypeStart.getService().getLinkType(linkName);
            } catch (VCIError e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
                continue;
            }
            //将list中包含的F_btm移除, 重新设置btmItemsFrom
            String[] btms_ = link.btmItemsFrom;
            List<String> btms = new ArrayList<String>();
            for(int i = 0; i < btms_.length; i++){
                if(!list.contains("F_" + btms_[i])){
                    btms.add(btms_[i]);
                }else{
                    if(link.primitivesFrom.equals(btms_[i])){
                        link.primitivesFrom = "";
                    }
                }
            }
            link.btmItemsFrom = btms.toArray(new String[0]);
            
            //将list中包含的T_btm移除, 重新设置btmItemsTo
            btms_ = link.btmItemsTo;
            btms = new ArrayList<String>();
            for(int i = 0; i < btms_.length; i++){
                if(!list.contains("T_" + btms_[i])){
                    btms.add(btms_[i]);
                }else{
                    if(link.primitivesTo.equals(btms_[i])){
                        link.primitivesTo = "";
                    }
                }
            }
            link.btmItemsTo = btms.toArray(new String[0]);
            link.id = link.name;
            try {
                if(LinkTypeStart.getService().modifyLinkType(link)){
                    result.add(linkName);
                }
            } catch (VCIError e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}