1 14 package org.compiere.dbPort; 15 16 import java.io.*; 17 import java.sql.*; 18 import java.awt.*; 19 import java.awt.event.*; 20 import javax.swing.*; 21 22 import org.compiere.db.*; 23 24 30 public class ConvertDialog extends JFrame implements ActionListener 31 { 32 35 public ConvertDialog() 36 { 37 try 38 { 39 jbInit(); 40 fSelectFile.addItem("D:\\compiere\\db\\database\\create\\views.sql"); 42 fSelectFile.addItem("D:\\compiere\\db\\database\\create\\temporary.sql"); 43 fSelectFile.addItem("D:\\compiere\\db\\database\\create\\sequences.sql"); 44 fSelectFile.addItem("D:\\compiere\\db\\database\\create\\compiere.sql"); 45 fConnect.setValue(CConnection.get(Database.DB_POSTGRESQL, 47 "linux", DB_PostgreSQL.DEFAULT_PORT, "compiere")); 48 fTarget.setSelectedItem(Database.DB_POSTGRESQL); 49 fExecute.setSelected(true); 50 51 cmd_execute(); pack(); 54 setVisible(true); 55 } 56 catch(Exception e) 57 { 58 System.err.println(e); 59 } 60 } 62 private JPanel parameterPanel = new JPanel(); 63 private GridBagLayout gridBagLayout1 = new GridBagLayout(); 64 private JLabel lSelectFile = new JLabel(); 65 private JComboBox fSelectFile = new JComboBox(); 66 private JButton bSelectFile = new JButton(); 67 private JCheckBox fExecute = new JCheckBox(); 68 private JLabel lConnect = new JLabel(); 69 private CConnectionEditor fConnect = new CConnectionEditor(); 70 private JButton bStart = new JButton(); 71 private JScrollPane scrollPane = new JScrollPane(); 72 private JTextArea infoPane = new JTextArea(); 73 private Component component1; 74 private Component component2; 75 private Component component3; 76 private Component component4; 77 private JLabel lTarget = new JLabel(); 78 private JComboBox fTarget = new JComboBox(Database.DB_NAMES); 79 private JCheckBox fVerbose = new JCheckBox(); 80 81 85 private void jbInit() throws Exception  86 { 87 component1 = Box.createHorizontalStrut(8); 88 component2 = Box.createHorizontalStrut(8); 89 component3 = Box.createVerticalStrut(8); 90 component4 = Box.createVerticalStrut(8); 91 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 92 this.setTitle("DB Convert Dialog"); 93 parameterPanel.setLayout(gridBagLayout1); 95 lSelectFile.setText("Select File"); 96 fSelectFile.setEditable(true); 97 bSelectFile.setText("add file"); 98 bSelectFile.addActionListener(this); 99 fExecute.setText("Execute Directly"); 100 fExecute.addActionListener(this); 101 lConnect.setText("Connection"); 102 bStart.setText("Start"); 103 bStart.addActionListener(this); 104 infoPane.setBackground(Color.lightGray); 106 infoPane.setEditable(false); 107 scrollPane.setPreferredSize(new Dimension(200, 200)); 108 lTarget.setText("Target"); 109 fVerbose.setText("Verbose"); 110 this.getContentPane().add(parameterPanel, BorderLayout.NORTH); 112 parameterPanel.add(lSelectFile, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0 113 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0)); 114 parameterPanel.add(fSelectFile, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0 115 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); 116 parameterPanel.add(bSelectFile, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0 117 ,GridBagConstraints.SOUTHWEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); 118 parameterPanel.add(fExecute, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0 119 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); 120 parameterPanel.add(lConnect, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0 121 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0)); 122 parameterPanel.add(fConnect, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0 123 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0)); 124 parameterPanel.add(bStart, new GridBagConstraints(3, 4, 1, 1, 0.0, 0.0 125 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); 126 parameterPanel.add(component1, new GridBagConstraints(5, 0, 1, 2, 0.0, 0.0 127 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 128 parameterPanel.add(component2, new GridBagConstraints(0, 0, 1, 2, 0.0, 0.0 129 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 130 parameterPanel.add(component3, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0 131 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 132 parameterPanel.add(component4, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0 133 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 134 parameterPanel.add(lTarget, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0 135 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); 136 this.getContentPane().add(scrollPane, BorderLayout.CENTER); 137 scrollPane.getViewport().add(infoPane, null); 138 parameterPanel.add(fTarget, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0 139 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); 140 parameterPanel.add(fVerbose, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0 141 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); 142 } 144 148 public void actionPerformed(ActionEvent e) 149 { 150 setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 151 if (e.getSource() == bStart) 153 { 154 bStart.setEnabled(false); 155 cmd_start(); 156 bStart.setEnabled(true); 157 } 158 else if (e.getSource() == bSelectFile) 159 cmd_selectFile(); 160 161 else if (e.getSource() == fExecute) 162 cmd_execute(); 163 setCursor(Cursor.getDefaultCursor()); 165 } 167 170 private void cmd_execute() 171 { 172 lConnect.setEnabled(fExecute.isSelected()); 173 fConnect.setReadWrite(fExecute.isSelected()); 174 lTarget.setEnabled(!fExecute.isSelected()); 175 fTarget.setEnabled(!fExecute.isSelected()); 176 } 178 181 private void cmd_selectFile() 182 { 183 JFileChooser fc = new JFileChooser(); 184 fc.setMultiSelectionEnabled(false); 185 if (fc.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) 186 return; 187 File f = fc.getSelectedFile(); 188 if (f == null || !f.isFile()) 189 return; 190 String fileName = f.getAbsolutePath(); 191 fSelectFile.addItem(fileName); 193 fSelectFile.setSelectedItem(fileName); 194 } 196 197 198 201 private void cmd_start() 202 { 203 File file = new File ((String )fSelectFile.getSelectedItem()); 205 if (!file.exists() || file.isDirectory()) 206 { 207 infoPane.append("File does not exist or a directory: " + file + "\n"); 208 return; 209 } 210 infoPane.append("Opening file: " + file + "\n"); 211 StringBuffer sb = new StringBuffer (1000); 212 try 214 { 215 FileReader fr = new FileReader(file); 216 BufferedReader in = new BufferedReader(fr); 217 218 String line = null; 219 int lines = 0; 220 while((line = in.readLine()) != null) 221 { 222 lines++; 223 sb.append(line).append('\n'); 224 } 225 in.close(); 226 fr.close(); 227 infoPane.append("- Read lines: " + lines + ", size: " + sb.length() + "\n"); 228 } 229 catch (FileNotFoundException fnf) 230 { 231 infoPane.append("Error: " + fnf + "\n"); 232 return; 233 } 234 catch (IOException ioe) 235 { 236 infoPane.append("Error: " + ioe + "\n"); 237 return; 238 } 239 240 if (fExecute.isSelected()) 242 { 243 CConnection cc = (CConnection)fConnect.getValue(); 244 Convert convert = new Convert (cc.getType(), cc.getConnection (true, Connection.TRANSACTION_SERIALIZABLE)); 245 convert.setVerbose(fVerbose.isSelected()); 246 convert.execute(sb.toString()); 248 if (convert.hasError()) 249 { 250 StringBuffer sbb = new StringBuffer ("- Error: "); 251 if (convert.getConversionError() != null) 252 sbb.append(convert.getConversionError()).append(' '); 253 if (convert.getException() != null) 254 { 255 sbb.append(convert.getException()); 256 convert.getException().printStackTrace(); 257 } 258 sbb.append("\n"); 259 infoPane.append(sbb.toString()); 260 } 261 else 262 infoPane.append("- OK\n"); 263 } 264 else 265 { 266 String target = (String )fTarget.getSelectedItem(); 267 if (Database.DB_ORACLE.equals(target)) 268 { 269 infoPane.append("No conversion needed.\n"); 270 return; 271 } 272 Convert convert = new Convert (target , null); 273 String cc = convert.convertAll(sb.toString()); 275 276 String fileName = file.getAbsolutePath(); 278 int pos = fileName.lastIndexOf("."); 279 if (pos == -1) 280 fileName += target; 281 else 282 fileName = fileName.substring(0, pos) + target + fileName.substring(pos); 283 infoPane.append("Writing to: " + fileName + "\n"); 284 try 286 { 287 FileWriter fw = new FileWriter(fileName, false); 288 BufferedWriter out = new BufferedWriter (fw); 289 out.write("-- Compiere dbPort - Convert Oracle to " + target); 290 out.newLine(); 291 out.write("-- Version " + Convert.VERSION.replace('$', ' ')); 292 out.newLine(); 293 out.write(cc); 295 out.close(); 297 fw.close(); 298 } 299 catch (IOException ioe) 300 { 301 infoPane.append("Error: " + ioe + "\n"); 302 } 303 infoPane.append("- Written: " + cc.length() + "\n"); 304 } 305 } 307 308 309 310 314 public static void main(String [] args) 315 { 316 new ConvertDialog(); 317 } } | Popular Tags |