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 dbCheckMap; Map> 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 dbCheckMap, Map> 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 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 ite = btmCheckMap.keySet().iterator(); ite.hasNext();){ String typeName = ite.next(); List 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 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 result = repairXml(); updateStatus_(result); } } }); btnCancel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dipose_(); } }); } private void dipose_(){ this.dispose(); } /** * 获取需要修复的伪sql * @return */ private List getRepairDML() { List list = new ArrayList(); for(Iterator 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 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 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 repairXml(){ List result = new ArrayList(); for(Iterator ite = btmCheckMap.keySet().iterator(); ite.hasNext();){ String linkName = ite.next(); List 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 btms = new ArrayList(); 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(); 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; } }