001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.dbschema.jdbcimpl.wizard;
043:
044: import java.beans.*;
045: import java.text.MessageFormat;
046: import java.util.*;
047: import org.netbeans.api.db.explorer.ConnectionManager;
048: import org.netbeans.api.db.explorer.DatabaseConnection;
049:
050: import org.openide.awt.StatusDisplayer;
051: import org.openide.filesystems.*;
052: import org.openide.loaders.DataFolder;
053: import org.openide.util.NbBundle;
054: import org.openide.util.RequestProcessor;
055:
056: import org.netbeans.modules.dbschema.*;
057: import org.netbeans.modules.dbschema.jdbcimpl.*;
058: import org.openide.util.Exceptions;
059:
060: public class CaptureSchema {
061:
062: ResourceBundle bundle = NbBundle
063: .getBundle("org.netbeans.modules.dbschema.jdbcimpl.resources.Bundle"); //NOI18N
064:
065: private final String defaultName = bundle
066: .getString("DefaultSchemaName"); //NOI18N
067: private DBSchemaWizardData data;
068:
069: /** Creates new CaptureSchema */
070: public CaptureSchema(DBSchemaWizardData data) {
071: this .data = data;
072: }
073:
074: protected void start() {
075: //getting values from package panel
076: String target = data.getName();
077: DataFolder folder = data.getDestinationPackage();
078:
079: //getting values from table selection panel
080: final LinkedList tables = data.getTables();
081: final LinkedList views = data.getViews();
082: final boolean allTables = data.isAllTables();
083:
084: ConnectionProvider cp = data.getConnectionProvider();
085:
086: try {
087: final ConnectionProvider c = cp;
088:
089: final FileObject fo = folder.getPrimaryFile();
090: if (target == null || target.equals("")) //NOI18N
091: target = FileUtil.findFreeFileName(fo, defaultName,
092: "dbschema"); //NOI18N
093:
094: final boolean conned = data.isConnected();
095: final boolean ec = data.isExistingConn();
096: final DatabaseConnection dbconn = data
097: .getDatabaseConnection();
098: final String target1 = target;
099:
100: //delete values from panels
101: data = null;
102:
103: RequestProcessor.getDefault().post(new Runnable() {
104: public void run() {
105: try {
106: StatusDisplayer
107: .getDefault()
108: .setStatusText(
109: bundle
110: .getString("CreatingDatabaseSchema")); //NOI18N
111:
112: final ProgressFrame pf = new ProgressFrame();
113: final SchemaElementImpl sei = new SchemaElementImpl(
114: c);
115:
116: PropertyChangeListener listener = new PropertyChangeListener() {
117: public void propertyChange(
118: PropertyChangeEvent event) {
119: String message;
120:
121: if (event.getPropertyName().equals(
122: "totalCount")) { //NOI18N
123: pf.setMaximum(((Integer) event
124: .getNewValue()).intValue());
125: return;
126: }
127:
128: if (event.getPropertyName().equals(
129: "progress")) { //NOI18N
130: pf.setValue(((Integer) event
131: .getNewValue()).intValue());
132: return;
133: }
134:
135: if (event.getPropertyName().equals(
136: "tableName")) { //NOI18N
137: message = MessageFormat
138: .format(
139: bundle
140: .getString("CapturingTable"),
141: new String[] { ((String) event
142: .getNewValue())
143: .toUpperCase() }); //NOI18N
144: pf.setMessage(message);
145: return;
146: }
147:
148: if (event.getPropertyName().equals(
149: "FKt")) { //NOI18N
150: message = MessageFormat
151: .format(
152: bundle
153: .getString("CaptureFK"),
154: new String[] {
155: ((String) event
156: .getNewValue())
157: .toUpperCase(),
158: bundle
159: .getString("CaptureFKtable") }); //NOI18N
160: pf.setMessage(message);
161: return;
162: }
163:
164: if (event.getPropertyName().equals(
165: "FKv")) { //NOI18N
166: message = MessageFormat
167: .format(
168: bundle
169: .getString("CaptureFK"),
170: new String[] {
171: ((String) event
172: .getNewValue())
173: .toUpperCase(),
174: bundle
175: .getString("CaptureFKview") }); //NOI18N
176: pf.setMessage(message);
177: return;
178: }
179:
180: if (event.getPropertyName().equals(
181: "viewName")) { //NOI18N
182: message = MessageFormat
183: .format(
184: bundle
185: .getString("CapturingView"),
186: new String[] { ((String) event
187: .getNewValue())
188: .toUpperCase() }); //NOI18N
189: pf.setMessage(message);
190: return;
191: }
192:
193: if (event.getPropertyName().equals(
194: "cancel")) { //NOI18N
195: sei.setStop(true);
196: StatusDisplayer.getDefault()
197: .setStatusText(""); //NOI18N
198: return;
199: }
200: }
201: };
202:
203: pf.propertySupport
204: .addPropertyChangeListener(listener);
205: pf.setVisible(true);
206:
207: sei.propertySupport
208: .addPropertyChangeListener(listener);
209: final SchemaElement se = new SchemaElement(sei);
210: se.setName(DBIdentifier.create(target1));
211: if (allTables)
212: sei.initTables(c, tables, views, true);
213: else
214: sei.initTables(c, tables, views, false);
215: pf.finishProgress();
216:
217: if (!sei.isStop()) {
218: fo.getFileSystem().runAtomicAction(
219: new FileSystem.AtomicAction() {
220: public void run()
221: throws java.io.IOException {
222: FileObject fo1 = fo
223: .createData(
224: target1,
225: "dbschema"); //NOI18N
226: FileLock fl = fo1.lock();
227: java.io.OutputStream out = fo1
228: .getOutputStream(fl);
229: if (out == null)
230: throw new java.io.IOException(
231: "Unable to open output stream");
232:
233: pf
234: .setMessage(bundle
235: .getString("SavingDatabaseSchema")); //NOI18N
236: StatusDisplayer
237: .getDefault()
238: .setStatusText(
239: bundle
240: .getString("SavingDatabaseSchema")); //NOI18N
241:
242: se.save(out);
243: fl.releaseLock();
244: }
245: });
246:
247: pf.setMessage(bundle
248: .getString("SchemaSaved")); //NOI18N
249: StatusDisplayer.getDefault().setStatusText(
250: bundle.getString("SchemaSaved")); //NOI18N
251:
252: pf.setVisible(false);
253: pf.dispose();
254: }
255:
256: if (conned)
257: if (ec)
258: ConnectionManager.getDefault()
259: .disconnect(dbconn);
260: else
261: c.closeConnection();
262: } catch (Exception exc) {
263: Exceptions.printStackTrace(exc);
264: }
265: }
266: }, 0);
267: } catch (Exception exc) {
268: String message = MessageFormat.format(bundle
269: .getString("UnableToCreateSchema"),
270: new String[] { exc.getMessage() }); //NOI18N
271: StatusDisplayer.getDefault().setStatusText(message);
272:
273: try {
274: if (cp != null)
275: if (data.isConnected())
276: if (data.isExistingConn())
277: ConnectionManager.getDefault().disconnect(
278: data.getDatabaseConnection());
279: else
280: cp.closeConnection();
281: } catch (Exception exc1) {
282: //unable to disconnect
283: }
284: }
285: }
286: }
|