1 19 20 package org.netbeans.modules.j2ee.persistence.wizard.fromdb; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.BufferedOutputStream ; 25 import java.io.IOException ; 26 import java.io.OutputStream ; 27 import java.sql.Connection ; 28 import java.sql.SQLException ; 29 import java.util.ArrayList ; 30 import java.util.List ; 31 import javax.swing.SwingUtilities ; 32 import org.netbeans.api.db.explorer.ConnectionManager; 33 import org.netbeans.api.db.explorer.DatabaseConnection; 34 import org.netbeans.modules.dbschema.DBException; 35 import org.netbeans.modules.dbschema.DBIdentifier; 36 import org.netbeans.modules.dbschema.SchemaElement; 37 import org.netbeans.modules.dbschema.SchemaElementUtil; 38 import org.netbeans.modules.dbschema.jdbcimpl.ConnectionProvider; 39 import org.netbeans.modules.dbschema.jdbcimpl.SchemaElementImpl; 40 import org.netbeans.modules.dbschema.util.NameUtil; 41 import org.netbeans.modules.j2ee.persistence.util.EventRequestProcessor; 42 import org.openide.ErrorManager; 43 import org.openide.filesystems.FileLock; 44 import org.openide.filesystems.FileObject; 45 import org.openide.filesystems.FileUtil; 46 import org.openide.util.NbBundle; 47 import org.openide.util.Utilities; 48 49 53 public class DBSchemaManager { 54 55 public static final String DBSCHEMA_EXT = "dbschema"; 57 private final EventRequestProcessor erp = new EventRequestProcessor(); 58 59 private DatabaseConnection oldDBConn; 60 private boolean oldDBConnWasConnected; 61 private Connection conn; 62 private SchemaElement schemaElement; 63 64 private SQLException exception; 65 66 private FileObject schemaFileObject; 67 private SchemaElement fileSchemaElement; 68 69 public SchemaElement getSchemaElement(final DatabaseConnection dbconn) throws SQLException { 70 assert SwingUtilities.isEventDispatchThread(); 71 72 if (oldDBConn == dbconn && schemaElement != null) { 73 return schemaElement; 74 } 75 76 schemaElement = null; 77 78 List <EventRequestProcessor.Action> actions = new ArrayList <EventRequestProcessor.Action>(); 79 80 if (oldDBConn != null && oldDBConn != dbconn && !oldDBConnWasConnected) { 81 actions.add(new EventRequestProcessor.AsynchronousAction() { 83 public void run(EventRequestProcessor.Context actionContext) { 84 actionContext.getProgress().progress(NbBundle.getMessage(DBSchemaManager.class, "LBL_ClosingPreviousConnection")); 85 86 ConnectionManager.getDefault().disconnect(oldDBConn); 87 oldDBConn = null; 88 conn = null; 89 } 90 }); 91 } else { 92 oldDBConn = null; 95 conn = null; 96 } 97 98 actions.add(new EventRequestProcessor.SynchronousAction() { 99 public void run(EventRequestProcessor.Context actionContext) { 100 ConnectionManager.getDefault().showConnectionDialog(dbconn); 101 conn = dbconn.getJDBCConnection(); 102 } 103 104 public boolean isEnabled() { 105 conn = dbconn.getJDBCConnection(); 106 oldDBConnWasConnected = conn != null; 107 return !oldDBConnWasConnected; 108 } 109 }); 110 111 actions.add(new EventRequestProcessor.CancellableAction() { 112 113 private SchemaElementImpl schemaElementImpl; 114 private boolean cancelled; 115 116 public void run(final EventRequestProcessor.Context actionContext) { 117 actionContext.getProgress().progress(NbBundle.getMessage(DBSchemaManager.class, "LBL_RetrievingSchema")); 118 119 oldDBConn = dbconn; 120 121 ConnectionProvider connectionProvider = null; 122 try { 123 connectionProvider = new ConnectionProvider(conn, dbconn.getDriverClass()); 124 connectionProvider.setSchema(dbconn.getSchema()); 125 } catch (SQLException e) { 126 exception = e; 127 return; 128 } 129 130 synchronized (this) { 131 if (cancelled) { 132 return; 133 } 134 schemaElementImpl = new SchemaElementImpl(connectionProvider); 135 } 136 137 try { 138 schemaElementImpl.setName(DBIdentifier.create("dbschema")); } catch (DBException e) { 140 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 141 return; 142 } 143 schemaElement = new SchemaElement(schemaElementImpl); 144 145 schemaElementImpl.addPropertyChangeListener(new PropertyChangeListener () { 146 public void propertyChange(PropertyChangeEvent event) { 147 String propertyName = event.getPropertyName(); 148 String message = null; 149 150 if ("totalCount".equals(propertyName)) { int workunits = ((Integer )event.getNewValue()).intValue(); 152 actionContext.getProgress().switchToDeterminate(workunits); 153 } else if ("progress".equals(propertyName)) { int workunit = ((Integer )event.getNewValue()).intValue(); 155 actionContext.getProgress().progress(workunit); 156 } else if ("tableName".equals(propertyName)) { message = NbBundle.getMessage(DBSchemaManager.class, "LBL_RetrievingTable", event.getNewValue()); 158 } else if ("viewName".equals(propertyName)) { message = NbBundle.getMessage(DBSchemaManager.class, "LBL_RetrievingView", event.getNewValue()); 160 } else if ("FKt".equals(propertyName)) { message = NbBundle.getMessage(DBSchemaManager.class, "LBL_RetrievingTableKeys", event.getNewValue()); 162 } else if ("FKv".equals(propertyName)) { message = NbBundle.getMessage(DBSchemaManager.class, "LBL_RetrievingViewKeys", event.getNewValue()); 164 } 165 166 if (message != null) { 167 actionContext.getProgress().progress(message); 168 } 169 } 170 }); 171 172 schemaElementImpl.initTables(connectionProvider); 173 } 174 175 public boolean getRunInEventThread() { 176 return false; 177 } 178 179 public boolean isEnabled() { 180 return conn != null; 181 } 182 183 public boolean cancel() { 184 synchronized (this) { 185 cancelled = true; 186 if (schemaElementImpl != null) { 187 schemaElementImpl.setStop(true); 188 } 189 } 190 return true; 191 } 192 }); 193 194 exception = null; 195 boolean success = erp.invoke(actions, true); 196 if (exception != null) { 197 throw exception; 198 } 199 200 if (!success) { 201 schemaElement = null; 203 } 204 return schemaElement; 205 } 206 207 public SchemaElement getSchemaElement(final FileObject fo) { 208 assert SwingUtilities.isEventDispatchThread(); 209 210 if (fo == schemaFileObject) { 211 return fileSchemaElement; 212 } 213 214 schemaFileObject = null; 215 fileSchemaElement = null; 216 217 List <EventRequestProcessor.Action> actions = new ArrayList <EventRequestProcessor.Action>(); 218 219 actions.add(new EventRequestProcessor.AsynchronousAction() { 220 public void run(EventRequestProcessor.Context actionContext) { 221 actionContext.getProgress().progress(NbBundle.getMessage(DBSchemaManager.class, "LBL_ReadingSchemaFile")); 222 223 schemaFileObject = fo; 224 fileSchemaElement = SchemaElementUtil.forName(fo); 225 } 226 }); 227 228 erp.invoke(actions); 229 230 return fileSchemaElement; 231 } 232 233 240 public static FileObject updateDBSchemas(SchemaElement schemaElement, DBSchemaFileList dbschemaFileList, FileObject folder, String projectName) throws IOException { 241 FileObject result = updateDBSchemas(schemaElement, dbschemaFileList); 242 if (result == null) { 243 result = writeDBSchema(schemaElement, folder, projectName); 244 } 245 return result; 246 } 247 248 253 private static FileObject updateDBSchemas(SchemaElement schemaElement, DBSchemaFileList dbschemaFileList) throws IOException { 254 FileObject updatedDBSchemaFile = null; 255 DBIdentifier schemaFullName = schemaElement.getSchema(); 256 String schemaName = schemaFullName != null ? schemaFullName.getName() : null; 257 258 for (FileObject dbschemaFile : dbschemaFileList.getFileList()) { 259 SchemaElement existingSchemaElement = SchemaElementUtil.forName(dbschemaFile); 260 DBIdentifier existingSchemaFullName = existingSchemaElement.getSchema(); 261 String existingSchemaName = existingSchemaFullName != null ? existingSchemaFullName.getName() : null; 262 263 if (Utilities.compareObjects(existingSchemaElement.getUrl(), schemaElement.getUrl()) && 264 Utilities.compareObjects(existingSchemaName, schemaName)) { 265 DBIdentifier existingDBSchemaName = existingSchemaElement.getName(); 266 overwriteDBSchema(schemaElement, dbschemaFile, existingDBSchemaName); 267 updatedDBSchemaFile = dbschemaFile; 268 } 269 } 270 271 return updatedDBSchemaFile; 272 } 273 274 277 private static FileObject writeDBSchema(SchemaElement schemaElement, FileObject folder, String projectName) throws IOException { 278 String schemaName = schemaElement.getSchema().getName(); 279 String fileName = (schemaName != null && schemaName != "" ? schemaName + "_" : "") + projectName; fileName = fileName.replace(NameUtil.dbElementSeparator, '_'); 283 String freeFileName = FileUtil.findFreeFileName(folder, fileName, DBSCHEMA_EXT); 284 DBIdentifier dbschemaName = DBIdentifier.create(freeFileName); 285 286 try { 287 schemaElement.setName(dbschemaName); 288 } catch (DBException e) { 289 IOException ioe = new IOException (e.getMessage()); 290 ioe.initCause(e); 291 throw ioe; 292 } 293 294 FileObject dbschemaFile = folder.createData(freeFileName, DBSCHEMA_EXT); 295 writeSchemaElement(schemaElement, dbschemaFile); 296 297 return dbschemaFile; 298 } 299 300 304 private static void overwriteDBSchema(SchemaElement schemaElement, FileObject dbschemaFile, DBIdentifier dbschemaName) throws IOException { 305 try { 306 schemaElement.setName(dbschemaName); 307 } catch (DBException e) { 308 IOException ioe = new IOException (e.getMessage()); 309 ioe.initCause(e); 310 throw ioe; 311 } 312 313 FileObject parent = dbschemaFile.getParent(); 316 String fileName = dbschemaFile.getName(); 317 String fileExt = dbschemaFile.getExt(); 318 dbschemaFile.delete(); 319 FileObject newDBSchemaFile = parent.createData(fileName, fileExt); 320 321 writeSchemaElement(schemaElement, newDBSchemaFile); 322 } 323 324 328 private static void writeSchemaElement(SchemaElement schemaElement, FileObject dbschemaFile) throws IOException { 329 FileLock lock = dbschemaFile.lock(); 330 try { 331 OutputStream os = new BufferedOutputStream (dbschemaFile.getOutputStream(lock)); 332 try { 333 schemaElement.save(os); 334 } finally { 335 os.close(); 336 } 337 } finally { 338 lock.releaseLock(); 339 } 340 } 341 } 342 | Popular Tags |