KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > dbschema > jdbcimpl > wizard > CaptureSchema


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.dbschema.jdbcimpl.wizard;
21
22 import java.beans.*;
23 import java.text.MessageFormat JavaDoc;
24 import java.util.*;
25 import org.netbeans.api.db.explorer.ConnectionManager;
26 import org.netbeans.api.db.explorer.DatabaseConnection;
27
28 import org.openide.ErrorManager;
29 import org.openide.awt.StatusDisplayer;
30 import org.openide.filesystems.*;
31 import org.openide.loaders.DataFolder;
32 import org.openide.util.NbBundle;
33 import org.openide.util.RequestProcessor;
34
35 import org.netbeans.modules.dbschema.*;
36 import org.netbeans.modules.dbschema.jdbcimpl.*;
37
38 public class CaptureSchema {
39
40     ResourceBundle bundle = NbBundle.getBundle("org.netbeans.modules.dbschema.jdbcimpl.resources.Bundle"); //NOI18N
41

42     private final String JavaDoc defaultName = bundle.getString("DefaultSchemaName"); //NOI18N
43
private DBSchemaWizardData data;
44     
45     /** Creates new CaptureSchema */
46     public CaptureSchema(DBSchemaWizardData data) {
47         this.data = data;
48     }
49
50     protected void start() {
51         //getting values from package panel
52
String JavaDoc target = data.getName();
53         DataFolder folder = data.getDestinationPackage();
54         
55         //getting values from table selection panel
56
final LinkedList tables = data.getTables();
57         final LinkedList views = data.getViews();
58         final boolean allTables = data.isAllTables();
59         
60         ConnectionProvider cp = data.getConnectionProvider();
61         
62         try {
63             final ConnectionProvider c = cp;
64             
65             final FileObject fo = folder.getPrimaryFile();
66             if (target == null || target.equals("")) //NOI18N
67
target = FileUtil.findFreeFileName(fo, defaultName, "dbschema"); //NOI18N
68

69             final boolean conned = data.isConnected();
70             final boolean ec = data.isExistingConn();
71             final DatabaseConnection dbconn = data.getDatabaseConnection();
72             final String JavaDoc target1 = target;
73             
74             //delete values from panels
75
data = null;
76             
77             RequestProcessor.getDefault().post(new Runnable JavaDoc() {
78                 public void run () {
79                     try {
80                         StatusDisplayer.getDefault().setStatusText(bundle.getString("CreatingDatabaseSchema")); //NOI18N
81

82                         final ProgressFrame pf = new ProgressFrame();
83                         final SchemaElementImpl sei = new SchemaElementImpl(c);
84                         
85                         PropertyChangeListener listener = new PropertyChangeListener() {
86                             public void propertyChange(PropertyChangeEvent event) {
87                                 String JavaDoc message;
88                                 
89                                 if (event.getPropertyName().equals("totalCount")) { //NOI18N
90
pf.setMaximum(((Integer JavaDoc)event.getNewValue()).intValue());
91                                     return;
92                                 }
93
94                                 if (event.getPropertyName().equals("progress")) { //NOI18N
95
pf.setValue(((Integer JavaDoc)event.getNewValue()).intValue());
96                                     return;
97                                 }
98                                 
99                                 if (event.getPropertyName().equals("tableName")) { //NOI18N
100
message = MessageFormat.format(bundle.getString("CapturingTable"), new String JavaDoc[] {((String JavaDoc) event.getNewValue()).toUpperCase()}); //NOI18N
101
pf.setMessage(message);
102                                     return;
103                                 }
104                                 
105                                 if (event.getPropertyName().equals("FKt")) { //NOI18N
106
message = MessageFormat.format(bundle.getString("CaptureFK"), new String JavaDoc[] {((String JavaDoc) event.getNewValue()).toUpperCase(), bundle.getString("CaptureFKtable")}); //NOI18N
107
pf.setMessage(message);
108                                     return;
109                                 }
110                                 
111                                 if (event.getPropertyName().equals("FKv")) { //NOI18N
112
message = MessageFormat.format(bundle.getString("CaptureFK"), new String JavaDoc[] {((String JavaDoc) event.getNewValue()).toUpperCase(), bundle.getString("CaptureFKview")}); //NOI18N
113
pf.setMessage(message);
114                                     return;
115                                 }
116                                 
117                                 if (event.getPropertyName().equals("viewName")) { //NOI18N
118
message = MessageFormat.format(bundle.getString("CapturingView"), new String JavaDoc[] {((String JavaDoc) event.getNewValue()).toUpperCase()}); //NOI18N
119
pf.setMessage(message);
120                                     return;
121                                 }
122                                 
123                                 if (event.getPropertyName().equals("cancel")) { //NOI18N
124
sei.setStop(true);
125                                     StatusDisplayer.getDefault().setStatusText(""); //NOI18N
126
return;
127                                 }
128                             }
129                         };
130                         
131                         pf.propertySupport.addPropertyChangeListener(listener);
132                         pf.setVisible(true);
133                         
134                         sei.propertySupport.addPropertyChangeListener(listener);
135                         final SchemaElement se = new SchemaElement(sei);
136                         se.setName(DBIdentifier.create(target1));
137                         if (allTables)
138                             sei.initTables(c, tables, views, true);
139                         else
140                             sei.initTables(c, tables, views, false);
141                         pf.finishProgress();
142                         
143                         if (! sei.isStop()) {
144                             fo.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
145                                 public void run() throws java.io.IOException JavaDoc {
146                                     FileObject fo1 = fo.createData(target1, "dbschema"); //NOI18N
147
FileLock fl = fo1.lock();
148                                     java.io.OutputStream JavaDoc out = fo1.getOutputStream(fl);
149                                     if (out == null)
150                                         throw new java.io.IOException JavaDoc("Unable to open output stream");
151
152                                     pf.setMessage(bundle.getString("SavingDatabaseSchema")); //NOI18N
153
StatusDisplayer.getDefault().setStatusText(bundle.getString("SavingDatabaseSchema")); //NOI18N
154

155                                     se.save(out);
156                                     fl.releaseLock();
157                                 }
158                             });
159                             
160                             pf.setMessage(bundle.getString("SchemaSaved")); //NOI18N
161
StatusDisplayer.getDefault().setStatusText(bundle.getString("SchemaSaved")); //NOI18N
162

163                             pf.setVisible(false);
164                             pf.dispose();
165                         }
166                         
167                         if (conned)
168                             if (ec)
169                                 ConnectionManager.getDefault().disconnect(dbconn);
170                             else
171                                 c.closeConnection();
172                     } catch (Exception JavaDoc exc) {
173                         ErrorManager.getDefault().notify(exc);
174                     }
175                 }
176             }, 0);
177         } catch (Exception JavaDoc exc) {
178             String JavaDoc message = MessageFormat.format(bundle.getString("UnableToCreateSchema"), new String JavaDoc[] {exc.getMessage()}); //NOI18N
179
StatusDisplayer.getDefault().setStatusText(message);
180             
181             try {
182                 if (cp != null)
183                     if (data.isConnected())
184                         if (data.isExistingConn())
185                             ConnectionManager.getDefault().disconnect(data.getDatabaseConnection());
186                         else
187                             cp.closeConnection();
188             } catch (Exception JavaDoc exc1) {
189                 //unable to disconnect
190
}
191         }
192     }
193 }
194
Popular Tags