1 package com.quantum.model;
3 import java.beans.PropertyChangeListener;
4 import java.beans.PropertyChangeSupport;
5 import java.sql.Connection;
6 import java.sql.SQLException;
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.HashSet;
10 import java.util.Hashtable;
11 import java.util.List;
15 import com.quantum.IQuantumConstants;
16 import com.quantum.QuantumPlugin;
17 import com.quantum.adapters.AdapterFactory;
18 import com.quantum.adapters.DatabaseAdapter;
19 import com.quantum.sql.ConnectionEstablisher;
20 import com.quantum.sql.MultiSQLServer;
22 import org.eclipse.jface.preference.IPreferenceStore;
25 * Class Bookmark holds the "static" information of a bookmark, that is the data that
26 * is saved and loaded from the external file and describes a bookmark. This info will
27 * be filled up by the end user.
31 public class Bookmark {
33 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
34 private String name = ""; //$NON-NLS-1$
35 private String username = ""; //$NON-NLS-1$
36 private String password = ""; //$NON-NLS-1$
37 private String connect = ""; //$NON-NLS-1$
38 private String driver = ""; //$NON-NLS-1$
39 private String type = ""; //$NON-NLS-1$
40 private String driverFile = ""; //$NON-NLS-1$
43 * A quick list is a list of favourite tables that a person might want to view
44 * without having to look at the entire list of tables.
46 private Map quickList = new Hashtable();
47 private Set schemas = new HashSet();
48 private Connection connection = null;
49 private ConnectionEstablisher connectionEstablisher;
50 private boolean changed = true;
51 private List queries = Collections.synchronizedList(new ArrayList());
52 private boolean promptForPassword = false;
53 private boolean autoCommit = true;
54 private String autoCommitPreference = IQuantumConstants.autoCommitTrue;
57 this(MultiSQLServer.getInstance());
60 public Bookmark(ConnectionEstablisher connectionEstablisher) {
61 this.connectionEstablisher = connectionEstablisher;
64 public Bookmark(Bookmark data) {
66 setName(data.getName());
67 setUsername(data.getUsername());
68 setPassword(data.getPassword());
69 setConnect(data.getConnect());
70 setDriver(data.getDriver());
71 setType(data.getType());
72 setDriverFile(data.getDriverFile());
73 setPromptForPassword(data.getPromptForPassword());
74 setAutoCommit(data.isAutoCommit());
75 setAutoCommitPreference(data.getAutoCommitPreference());
77 this.schemas.addAll(data.schemas);
78 this.quickList = new Hashtable(data.quickList);
82 * Returns the JDBC URL.
85 public String getConnect() {
98 * Returns the driverFile.
101 String getDriverFile() {
106 * Returns the password.
109 public String getPassword() {
114 * Returns the username.
117 public String getUsername() {
123 * @param connect The connect to set
125 public void setConnect(String connect) {
126 if (connect == null) {
127 connect = ""; //$NON-NLS-1$
129 this.connect = connect;
134 * @param driver The driver to set
136 void setDriver(String driver) {
137 if (driver == null) {
138 driver = ""; //$NON-NLS-1$
140 this.driver = driver;
144 * Sets the driverFile.
145 * @param driverFile The driverFile to set
147 void setDriverFile(String driverFile) {
148 if (driverFile == null) {
149 driverFile = ""; //$NON-NLS-1$
151 this.driverFile = driverFile;
156 * @param password The password to set
158 public void setPassword(String password) {
159 if (password == null) {
160 password = ""; //$NON-NLS-1$
162 this.password = password;
167 * @param username The username to set
169 public void setUsername(String username) {
170 if (username == null) {
171 username = ""; //$NON-NLS-1$
173 this.username = username;
180 public String getName() {
186 * @param name The name to set
188 public void setName(String name) {
190 name = ""; //$NON-NLS-1$
192 if (!name.equals(this.name)) {
194 String oldName = this.name;
196 this.propertyChangeSupport.firePropertyChange("name", oldName, this.name);
201 public boolean isEmpty() {
202 if (name.equals("") && //$NON-NLS-1$
203 username.equals("") && //$NON-NLS-1$
204 password.equals("") && //$NON-NLS-1$
205 connect.equals("") && //$NON-NLS-1$
206 driver.equals("") && //$NON-NLS-1$
207 type.equals("") && //$NON-NLS-1$
208 driverFile.equals("")) { //$NON-NLS-1$
213 public String toString() {
214 StringBuffer buffer = new StringBuffer();
215 buffer.append("["); //$NON-NLS-1$
216 buffer.append("name="); //$NON-NLS-1$
218 buffer.append(", "); //$NON-NLS-1$
219 buffer.append("username="); //$NON-NLS-1$
220 buffer.append(username);
221 buffer.append(", "); //$NON-NLS-1$
222 buffer.append("password=****"); //$NON-NLS-1$
223 buffer.append(", "); //$NON-NLS-1$
224 buffer.append("connect="); //$NON-NLS-1$
225 buffer.append(connect);
226 buffer.append(", "); //$NON-NLS-1$
227 buffer.append("driver="); //$NON-NLS-1$
228 buffer.append(driver);
229 buffer.append(", "); //$NON-NLS-1$
230 buffer.append("type="); //$NON-NLS-1$
232 buffer.append(", "); //$NON-NLS-1$
233 buffer.append("driverFile="); //$NON-NLS-1$
234 buffer.append(driverFile);
235 buffer.append("]"); //$NON-NLS-1$
236 return buffer.toString();
239 public String getType() {
243 public void setType(String type) {
247 public Connection connect(PasswordFinder passwordFinder) throws ConnectionException {
248 boolean isConnected = isConnected();
249 if (this.connection == null) {
250 this.connection = this.connectionEstablisher.connect(this, passwordFinder);
253 if (isConnected() != isConnected) {
254 this.propertyChangeSupport.firePropertyChange(
255 "connected", isConnected, isConnected());
257 return this.connection;
261 * Returns the connection object.
262 * @return the Connection object associated with the current JDBC source.
265 public Connection getConnection() throws NotConnectedException {
266 if (this.connection == null) {
267 throw new NotConnectedException();
269 return this.connection;
273 * @return true if the BookmarkNode is connected to a JDBC source
275 public boolean isConnected() {
276 return (connection != null);
280 * Sets the connection member. From that moment on the BookmarkNode is "connected" (open)
281 * @param connection : a valid connection to a JDBC source
283 public void setConnection(Connection connection) {
284 this.connection = connection;
287 public void disconnect() throws ConnectionException {
288 boolean isConnected = isConnected();
290 if (this.connection != null) {
291 this.connectionEstablisher.disconnect(this.connection);
294 this.connection = null;
295 if (isConnected() != isConnected) {
296 this.propertyChangeSupport.firePropertyChange(
297 "connected", isConnected, isConnected());
301 public void addSchema(String schema) {
302 if (schema != null && schema.trim().length() > 0) {
303 addSchema(new Schema(schema));
307 public void addSchema(Schema qualifier) {
308 if (qualifier != null) {
309 this.schemas.add(qualifier);
311 this.propertyChangeSupport.firePropertyChange("schemas", null, null);
315 public void setSchemas(Schema[] schemas) {
316 this.schemas.clear();
317 for (int i = 0, length = (schemas == null) ? 0 : schemas.length;
320 this.schemas.add(schemas[i]);
324 this.propertyChangeSupport.firePropertyChange("schemas", null, null);
328 * @return a list of all the schemas that have been set up.
330 public Schema[] getSchemas() {
331 Set set = new HashSet();
332 set.addAll(this.schemas);
334 set.add(new Schema(getAdapter().getDefaultSchema(this.username),
335 this.username, true));
337 List list = new ArrayList(set);
338 Collections.sort(list);
339 return (Schema[]) list.toArray(new Schema[list.size()]);
343 * @see java.lang.Object#equals(java.lang.Object)
345 public boolean equals(Object obj) {
346 if (!(obj instanceof Bookmark)) return false;
347 String name = ((Bookmark)obj).getName();
348 if (name.equals(this.getName())) return true;
355 public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
356 this.propertyChangeSupport.addPropertyChangeListener(listener);
362 public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
363 this.propertyChangeSupport.removePropertyChangeListener(listener);
366 public void addQuickListEntry(String type, String schemaName, String name) {
367 Entity entity = EntityFactory.getInstance().create(this, schemaName, name, type);
368 this.quickList.put(entity.getCondQualifiedName(), entity);
369 this.propertyChangeSupport.firePropertyChange("quickList", null, null);
373 public void addQuickListEntry(Entity entity) {
374 addQuickListEntry(entity.getType(), entity.getSchema(), entity.getName());
377 public void removeQuickListEntry(Entity entity) {
378 if (entity != null && this.quickList.containsKey(entity.getCondQualifiedName())) {
379 this.quickList.remove(entity.getCondQualifiedName());
380 this.propertyChangeSupport.firePropertyChange("quickList", null, null);
385 public Entity[] getQuickListEntries() {
386 return (Entity[]) this.quickList.values().toArray(new Entity[this.quickList.size()]);
389 public boolean hasQuickList() {
390 return !this.quickList.isEmpty();
395 public boolean isChanged() {
402 public void setChanged(boolean b) {
406 public Database getDatabase() throws NotConnectedException {
407 if (!isConnected()) {
408 throw new NotConnectedException();
410 return new Database(this);
413 public DatabaseAdapter getAdapter() {
414 return AdapterFactory.getInstance().getAdapter(getType());
417 public Entity[] getEntitiesForSchema(Schema schema, String type) throws SQLException {
419 Entity[] entities = getDatabase().getEntities(this, schema, type);
421 } catch (NotConnectedException e) {
422 return new Entity[0];
426 public Entity getEntity(Schema schema, String name) throws SQLException {
427 Entity result = null;
428 if (schema != null && name != null) {
429 Entity[] entities = getEntitiesForSchema(schema, null);
430 for (int i = 0, length = (entities == null) ? 0 : entities.length;
431 result == null && i < length;
433 if (schema.equals(entities[i].getSchema()) &&
434 name.equals(entities[i].getName())) {
435 result = entities[i];
442 public boolean isInQuickList(Entity entity) {
443 return this.quickList.containsKey(entity.getCondQualifiedName());
450 public void addQuery(String queryString) {
451 if (this.queries.contains(queryString)) {
452 this.queries.remove(queryString);
454 this.queries.add(queryString);
456 int size = getQueryHistorySize();
458 while (this.queries.size() > size) {
459 this.queries.remove(0);
461 this.propertyChangeSupport.firePropertyChange("queries", null, null);
465 public String[] getQueries() {
466 return (String[]) this.queries.toArray(new String[this.queries.size()]);
469 private int getQueryHistorySize() {
470 IPreferenceStore store =
471 QuantumPlugin.getDefault().getPreferenceStore();
472 return store.getInt(getClass().getName() + ".queryHistorySize"); //$NON-NLS-1$
477 public boolean getPromptForPassword() {
478 return promptForPassword;
484 public void setPromptForPassword(boolean b) {
485 promptForPassword = b;
491 public boolean isAutoCommit() {
498 public String getAutoCommitPreference() {
499 return autoCommitPreference;
505 public void setAutoCommit(boolean b) {
512 public void setAutoCommitPreference(String string) {
513 autoCommitPreference = string;
516 // Returns true or false indicating whether this bookmark must be set to AutoCommit on connection or not
517 public boolean getDefaultAutoCommit(){
518 if (autoCommitPreference.equals(IQuantumConstants.autoCommitTrue)) return true;
519 else if (autoCommitPreference.equals(IQuantumConstants.autoCommitFalse)) return false;
520 else if (autoCommitPreference.equals(IQuantumConstants.autoCommitSaved)) return autoCommit;
525 public void setJDBCDriver(JDBCDriver jdbcDriver) {
526 jdbcDriver = BookmarkCollection.getInstance().findDriver(jdbcDriver);
527 setDriver(jdbcDriver.getClassName());
528 setDriverFile(jdbcDriver.getJarFileName());
531 public JDBCDriver getJDBCDriver() {
532 return BookmarkCollection.getInstance().findDriver(getDriver(), getDriverFile());