1 19 20 package org.netbeans.modules.dbschema.jdbcimpl.wizard; 21 22 import java.beans.*; 23 import java.text.MessageFormat ; 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"); 42 private final String defaultName = bundle.getString("DefaultSchemaName"); private DBSchemaWizardData data; 44 45 46 public CaptureSchema(DBSchemaWizardData data) { 47 this.data = data; 48 } 49 50 protected void start() { 51 String target = data.getName(); 53 DataFolder folder = data.getDestinationPackage(); 54 55 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("")) target = FileUtil.findFreeFileName(fo, defaultName, "dbschema"); 69 final boolean conned = data.isConnected(); 70 final boolean ec = data.isExistingConn(); 71 final DatabaseConnection dbconn = data.getDatabaseConnection(); 72 final String target1 = target; 73 74 data = null; 76 77 RequestProcessor.getDefault().post(new Runnable () { 78 public void run () { 79 try { 80 StatusDisplayer.getDefault().setStatusText(bundle.getString("CreatingDatabaseSchema")); 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 message; 88 89 if (event.getPropertyName().equals("totalCount")) { pf.setMaximum(((Integer )event.getNewValue()).intValue()); 91 return; 92 } 93 94 if (event.getPropertyName().equals("progress")) { pf.setValue(((Integer )event.getNewValue()).intValue()); 96 return; 97 } 98 99 if (event.getPropertyName().equals("tableName")) { message = MessageFormat.format(bundle.getString("CapturingTable"), new String [] {((String ) event.getNewValue()).toUpperCase()}); pf.setMessage(message); 102 return; 103 } 104 105 if (event.getPropertyName().equals("FKt")) { message = MessageFormat.format(bundle.getString("CaptureFK"), new String [] {((String ) event.getNewValue()).toUpperCase(), bundle.getString("CaptureFKtable")}); pf.setMessage(message); 108 return; 109 } 110 111 if (event.getPropertyName().equals("FKv")) { message = MessageFormat.format(bundle.getString("CaptureFK"), new String [] {((String ) event.getNewValue()).toUpperCase(), bundle.getString("CaptureFKview")}); pf.setMessage(message); 114 return; 115 } 116 117 if (event.getPropertyName().equals("viewName")) { message = MessageFormat.format(bundle.getString("CapturingView"), new String [] {((String ) event.getNewValue()).toUpperCase()}); pf.setMessage(message); 120 return; 121 } 122 123 if (event.getPropertyName().equals("cancel")) { sei.setStop(true); 125 StatusDisplayer.getDefault().setStatusText(""); 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 { 146 FileObject fo1 = fo.createData(target1, "dbschema"); FileLock fl = fo1.lock(); 148 java.io.OutputStream out = fo1.getOutputStream(fl); 149 if (out == null) 150 throw new java.io.IOException ("Unable to open output stream"); 151 152 pf.setMessage(bundle.getString("SavingDatabaseSchema")); StatusDisplayer.getDefault().setStatusText(bundle.getString("SavingDatabaseSchema")); 155 se.save(out); 156 fl.releaseLock(); 157 } 158 }); 159 160 pf.setMessage(bundle.getString("SchemaSaved")); StatusDisplayer.getDefault().setStatusText(bundle.getString("SchemaSaved")); 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 exc) { 173 ErrorManager.getDefault().notify(exc); 174 } 175 } 176 }, 0); 177 } catch (Exception exc) { 178 String message = MessageFormat.format(bundle.getString("UnableToCreateSchema"), new String [] {exc.getMessage()}); 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 exc1) { 189 } 191 } 192 } 193 } 194 | Popular Tags |