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
package com.vci.client.omd.btm.ui;
 
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;
 
 
public class ConsistencyCheckDialog extends JDialog{
 
    /**
     * 
     */
    private static final long serialVersionUID = 6997597482667501498L;
    Map<String, String> map;
    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> map){
        this.map = map;
        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(map.size());
        int i = 0;
        for(Iterator<String> ite = map.keySet().iterator(); ite.hasNext();){
            String typeName = ite.next();
            String dml = map.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++;
        }
        table.setEditable(false);
    }
    
    private void addListener() {
        btnRepair.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                if(map.size() < 1){
                    JOptionPane.showMessageDialog(getDialog(), "已经全部成功修复", "已经全部成功修复", JOptionPane.INFORMATION_MESSAGE);
                    return;
                }
                if(JOptionPane.YES_OPTION != JOptionPane.showConfirmDialog(
                        getDialog(), "修复将改变数据库的结构", "修复确认框", JOptionPane.YES_NO_OPTION )){
                    return;
                }
                List<String> list = getRepairDML();
                if(list.size() < 1){
                    return;
                }
                try {
                    String[] result = BtmClient.getService().executeRepair(list.toArray(new String[0]));
                    updateStatus(result);
                } catch (VCIError e1) {
                    e1.printStackTrace();
                }
            }
 
        });
        
        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 = map.keySet().iterator(); ite.hasNext();){
            String type = ite.next();
            String dml = map.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);
        for(int i = 0; i < table.getRowCount(); i++){
            String typeName = (String) table.getValueAt(i, 0);
            if(list.contains(typeName)){
                table.setValueAt("已修复", i, 2);
                map.remove(typeName);
            }else if(list.contains(typeName + "_ADD")){
                table.setValueAt("增加已修复", i, 2);
                String sql = map.get(typeName);
                sql = sql.substring(sql.indexOf(";") + 1, sql.length());
                map.put(typeName, sql);
            }else if(list.contains(typeName + "_DROP")){
                table.setValueAt("移除已修复", i, 2);
                String sql = map.get(typeName);
                sql = sql.substring(0, sql.indexOf(";"));
                map.put(typeName, sql);
            }else{
                if(!((String) table.getValueAt(i, 2)).equals("已修复")){
                    table.setValueAt("修复失败", i, 2);
                }
            }
        }
        table.updateUI();
        table.setEditable(false);
    }
    
    private JDialog getDialog(){
        return this;
    }
}