KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > dbPort > ConvertDialog


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

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 /**
25  * Conversion Dialog
26  *
27  * @author Jorg Janke
28  * @version $Id: ConvertDialog.java,v 1.14 2003/09/27 01:22:19 jjanke Exp $
29  */

30 public class ConvertDialog extends JFrame implements ActionListener
31 {
32     /**
33      * Convert Dialog
34      */

35     public ConvertDialog()
36     {
37         try
38         {
39             jbInit();
40             //
41
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             // Set up environment
46
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(); // set UI
52
//
53
pack();
54             setVisible(true);
55         }
56         catch(Exception JavaDoc e)
57         {
58             System.err.println(e);
59         }
60     } // ConvertDialog
61

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     /**
82      * Static Layout
83      * @throws Exception
84      */

85     private void jbInit() throws Exception JavaDoc
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         //
94
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         //
105
infoPane.setBackground(Color.lightGray);
106         infoPane.setEditable(false);
107         scrollPane.setPreferredSize(new Dimension(200, 200));
108         lTarget.setText("Target");
109         fVerbose.setText("Verbose");
110         //
111
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     } // jbInit
143

144     /**
145      * Action Listener
146      * @param e event
147      */

148     public void actionPerformed(ActionEvent e)
149     {
150         setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
151         //
152
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         //
164
setCursor(Cursor.getDefaultCursor());
165     } // actionListener
166

167     /**
168      * Execute toggle
169      */

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     } // cmd_execute
177

178     /**
179      * Select File and add to selection
180      */

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 JavaDoc fileName = f.getAbsolutePath();
191         //
192
fSelectFile.addItem(fileName);
193         fSelectFile.setSelectedItem(fileName);
194     } // cmd_selectFile
195

196     /*************************************************************************/
197
198     /**
199      * Start Pressed
200      */

201     private void cmd_start()
202     {
203         // Open and read File
204
File file = new File ((String JavaDoc)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 JavaDoc sb = new StringBuffer JavaDoc (1000);
212         //
213
try
214         {
215             FileReader fr = new FileReader(file);
216             BufferedReader in = new BufferedReader(fr);
217
218             String JavaDoc 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         // Target system
241
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             //
247
convert.execute(sb.toString());
248             if (convert.hasError())
249             {
250                 StringBuffer JavaDoc sbb = new StringBuffer JavaDoc ("- 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 JavaDoc target = (String JavaDoc)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             //
274
String JavaDoc cc = convert.convertAll(sb.toString());
275
276             // Output file name
277
String JavaDoc 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             // Write to file
285
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                 //
294
out.write(cc);
295                 //
296
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     } // cmd_start
306

307
308     /*************************************************************************/
309
310     /**
311      * Start Dialog
312      * @param args ignored
313      */

314     public static void main(String JavaDoc[] args)
315     {
316         new ConvertDialog();
317     } // main
318
} // ConvertDialog
319
Popular Tags