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.Arrays;
9 import java.util.Collections;
10 import java.util.HashSet;
11 import java.util.Hashtable;
12 import java.util.List;
16 import com.quantum.IQuantumConstants;
17 import com.quantum.QuantumPlugin;
18 import com.quantum.adapters.AdapterFactory;
19 import com.quantum.adapters.DatabaseAdapter;
20 import com.quantum.sql.ConnectionEstablisher;
21 import com.quantum.sql.MultiSQLServer;
23 import org.eclipse.jface.preference.IPreferenceStore;
26 * Class Bookmark holds the "static" information of a bookmark, that is the data that
27 * is saved and loaded from the external file and describes a bookmark. This info will
28 * be filled up by the end user.
32 public class Bookmark implements Displayable {
34 public static final int SCHEMA_RULE_USE_ALL = 1;
35 public static final int SCHEMA_RULE_USE_DEFAULT = 2;
36 public static final int SCHEMA_RULE_USE_SELECTED = 3;
38 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
39 private String name = ""; //$NON-NLS-1$
40 private String username = ""; //$NON-NLS-1$
41 private String password = ""; //$NON-NLS-1$
42 private String connectionUrl = ""; //$NON-NLS-1$
43 private JDBCDriver driver;
45 private int schemaRule = SCHEMA_RULE_USE_ALL;
48 * A quick list is a list of favourite tables that a person might want to view
49 * without having to look at the entire list of tables.
51 private Map quickList = new Hashtable();
52 private Set schemas = new HashSet();
53 private Connection connection = null;
54 private ConnectionEstablisher connectionEstablisher;
55 private boolean changed = true;
56 private List queries = Collections.synchronizedList(new ArrayList());
57 private boolean promptForPassword = false;
58 private boolean autoCommit = true;
59 private String autoCommitPreference = IQuantumConstants.autoCommitTrue;
60 private Database database;
63 this(MultiSQLServer.getInstance());
66 public Bookmark(ConnectionEstablisher connectionEstablisher) {
67 this.connectionEstablisher = connectionEstablisher;
70 public Bookmark(Bookmark data) {
72 setName(data.getName());
73 setUsername(data.getUsername());
74 setPassword(data.getPassword());
75 setConnect(data.getConnect());
76 setJDBCDriver(data.getJDBCDriver());
77 setPromptForPassword(data.getPromptForPassword());
78 setAutoCommit(data.isAutoCommit());
79 setAutoCommitPreference(data.getAutoCommitPreference());
80 setSchemaRule(data.getSchemaRule());
82 this.schemas.addAll(data.schemas);
83 this.quickList = new Hashtable(data.quickList);
87 * Returns the JDBC URL.
90 public String getConnect() {
91 return this.connectionUrl;
95 * Returns the password.
98 public String getPassword() {
99 if (this.promptForPassword) {
102 return this.password;
107 * Returns the username.
110 public String getUsername() {
116 * @param connectionUrl The connect to set
118 public void setConnect(String connectionUrl) {
119 if (connectionUrl == null) {
120 connectionUrl = ""; //$NON-NLS-1$
122 this.connectionUrl = connectionUrl;
127 * @param password The password to set
129 public void setPassword(String password) {
130 if (password == null) {
131 password = ""; //$NON-NLS-1$
133 this.password = password;
138 * @param username The username to set
140 public void setUsername(String username) {
141 if (username == null) {
142 username = ""; //$NON-NLS-1$
144 this.username = username;
151 public String getName() {
157 * @param name The name to set
159 public void setName(String name) {
161 name = ""; //$NON-NLS-1$
163 if (!name.equals(this.name)) {
165 String oldName = this.name;
167 this.propertyChangeSupport.firePropertyChange("name", oldName, this.name);
172 public boolean isEmpty() {
173 if (name.equals("") && //$NON-NLS-1$
174 username.equals("") && //$NON-NLS-1$
175 password.equals("") && //$NON-NLS-1$
176 connectionUrl.equals("") && //$NON-NLS-1$
177 driver.equals("") && //$NON-NLS-1$
183 public String toString() {
184 StringBuffer buffer = new StringBuffer();
185 buffer.append("["); //$NON-NLS-1$
186 buffer.append("name="); //$NON-NLS-1$
188 buffer.append(", "); //$NON-NLS-1$
189 buffer.append("username="); //$NON-NLS-1$
190 buffer.append(username);
191 buffer.append(", "); //$NON-NLS-1$
192 buffer.append("password=****"); //$NON-NLS-1$
193 buffer.append(", "); //$NON-NLS-1$
194 buffer.append("connect="); //$NON-NLS-1$
195 buffer.append(connectionUrl);
196 buffer.append(", "); //$NON-NLS-1$
197 buffer.append("driver="); //$NON-NLS-1$
198 buffer.append(driver);
199 buffer.append("]"); //$NON-NLS-1$
200 return buffer.toString();
203 public Connection connect(PasswordFinder passwordFinder) throws ConnectionException {
204 boolean isConnected = isConnected();
205 if (this.connection == null) {
206 this.connection = this.connectionEstablisher.connect(this, passwordFinder);
209 if (isConnected() != isConnected) {
210 this.propertyChangeSupport.firePropertyChange(
211 "connected", isConnected, isConnected());
213 return this.connection;
217 * Returns the connection object.
218 * @return the Connection object associated with the current JDBC source.
221 public Connection getConnection() throws NotConnectedException {
222 if (this.connection == null) {
223 throw new NotConnectedException();
225 return this.connection;
229 * @return true if the BookmarkNode is connected to a JDBC source
231 public boolean isConnected() {
232 return (connection != null);
236 * Sets the connection member. From that moment on the BookmarkNode is "connected" (open)
237 * @param connection : a valid connection to a JDBC source
239 public void setConnection(Connection connection) {
240 this.connection = connection;
243 public void disconnect() throws SQLException {
244 boolean isConnected = isConnected();
246 if (this.connection != null) {
247 this.connectionEstablisher.disconnect(this.connection);
250 this.connection = null;
251 this.database = null;
252 if (isConnected() != isConnected) {
253 this.propertyChangeSupport.firePropertyChange(
254 "connected", isConnected, isConnected());
258 public void addSchema(String schema) {
259 if (schema != null && schema.trim().length() > 0) {
260 addSchema(new Schema(schema));
264 public void addSchema(Schema qualifier) {
265 if (qualifier != null) {
266 this.schemas.add(qualifier);
268 this.propertyChangeSupport.firePropertyChange("schemas", null, null);
272 public void setSchemaSelections(Schema[] schemas) {
273 this.schemas.clear();
274 for (int i = 0, length = (schemas == null) ? 0 : schemas.length;
277 this.schemas.add(schemas[i]);
281 this.propertyChangeSupport.firePropertyChange("schemas", null, null);
285 * @return a list of all the schemas that have been set up.
287 public Schema[] getSchemaSelections() {
288 List list = new ArrayList(this.schemas);
289 Collections.sort(list);
290 return (Schema[]) list.toArray(new Schema[list.size()]);
293 public Schema[] getSchemas() throws NotConnectedException, SQLException {
294 Schema[] schemas = null;
295 if (useUsernameAsSchema()) {
297 } else if (useAllSchemas()) {
298 schemas = getDatabase().getSchemas();
300 schemas = verifySchemas(getSchemaSelections());
302 return (schemas == null || schemas.length == 0)
303 ? new Schema[] { getDefaultSchema() }
308 * @param schemaSelections
310 * @throws SQLException
311 * @throws NotConnectedException
313 private Schema[] verifySchemas(Schema[] schemaSelections)
314 throws NotConnectedException, SQLException {
315 Schema[] schemasFromDatabase = getDatabase().getSchemas();
316 List list = Arrays.asList(schemasFromDatabase);
317 for (int i = 0, length = schemaSelections == null ? 0 : schemaSelections.length;
319 schemaSelections[i].setExists(list.contains(schemaSelections[i]));
321 return schemaSelections;
326 * @throws NotConnectedException
327 * @throws SQLException
329 private Schema getDefaultSchema() throws NotConnectedException, SQLException {
330 String username = getDatabase().getUsername();
331 String actual = getAdapter().getDefaultSchema(username);
332 return new Schema(actual, username, true);
336 * @see java.lang.Object#equals(java.lang.Object)
338 public boolean equals(Object obj) {
339 if (!(obj instanceof Bookmark)) return false;
340 String name = ((Bookmark)obj).getName();
341 if (name.equals(this.getName())) return true;
348 public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
349 this.propertyChangeSupport.addPropertyChangeListener(listener);
355 public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
356 this.propertyChangeSupport.removePropertyChangeListener(listener);
359 public void addQuickListEntry(String type, String schemaName, String name, boolean isSynonym) {
360 Entity entity = EntityFactory.getInstance().create(this, schemaName, name, type, isSynonym);
361 this.quickList.put(entity.getQualifiedName(), entity);
362 this.propertyChangeSupport.firePropertyChange("quickList", null, null);
366 public void addQuickListEntry(Entity entity) {
367 addQuickListEntry(entity.getType(), entity.getSchema(), entity.getName(), entity.isSynonym());
370 public void removeQuickListEntry(Entity entity) {
371 if (entity != null && this.quickList.containsKey(entity.getQualifiedName())) {
372 this.quickList.remove(entity.getQualifiedName());
373 this.propertyChangeSupport.firePropertyChange("quickList", null, null);
378 public Entity[] getQuickListEntries() {
379 return (Entity[]) this.quickList.values().toArray(new Entity[this.quickList.size()]);
382 public boolean hasQuickList() {
383 return !this.quickList.isEmpty();
388 public boolean isChanged() {
395 public void setChanged(boolean b) {
399 public Database getDatabase() throws NotConnectedException {
400 if (!isConnected()) {
401 throw new NotConnectedException();
403 if (this.database == null) {
404 this.database = new Database(this);
406 return this.database;
409 public DatabaseAdapter getAdapter() {
410 return this.driver == null
412 : AdapterFactory.getInstance().getAdapter(this.driver.getType());
415 public Entity[] getEntitiesForSchema(Schema schema, String type) throws SQLException {
417 Entity[] entities = getDatabase().getEntities(this, schema, type);
419 } catch (NotConnectedException e) {
420 return new Entity[0];
424 public Entity getEntity(Schema schema, String name) throws SQLException {
425 Entity result = null;
426 if (schema != null && name != null) {
427 Entity[] entities = getEntitiesForSchema(schema, null);
428 for (int i = 0, length = (entities == null) ? 0 : entities.length;
429 result == null && i < length;
431 if (schema.equals(entities[i].getSchema()) &&
432 name.equals(entities[i].getName())) {
433 result = entities[i];
440 public boolean isInQuickList(Entity entity) {
441 return this.quickList.containsKey(entity.getQualifiedName());
448 public void addQuery(String queryString) {
449 if (this.queries.contains(queryString)) {
450 this.queries.remove(queryString);
452 this.queries.add(queryString);
454 int size = getQueryHistorySize();
456 while (this.queries.size() > size) {
457 this.queries.remove(0);
459 this.propertyChangeSupport.firePropertyChange("queries", null, null);
463 public String[] getQueries() {
464 return (String[]) this.queries.toArray(new String[this.queries.size()]);
467 private int getQueryHistorySize() {
468 IPreferenceStore store =
469 QuantumPlugin.getDefault().getPreferenceStore();
470 return store.getInt(getClass().getName() + ".queryHistorySize"); //$NON-NLS-1$
475 public boolean getPromptForPassword() {
476 return promptForPassword;
482 public void setPromptForPassword(boolean b) {
483 promptForPassword = b;
489 public boolean isAutoCommit() {
496 public String getAutoCommitPreference() {
497 return autoCommitPreference;
503 public void setAutoCommit(boolean b) {
510 public void setAutoCommitPreference(String string) {
511 autoCommitPreference = string;
514 // Returns true or false indicating whether this bookmark must be set to AutoCommit on connection or not
515 public boolean getDefaultAutoCommit(){
516 if (autoCommitPreference.equals(IQuantumConstants.autoCommitTrue)) return true;
517 else if (autoCommitPreference.equals(IQuantumConstants.autoCommitFalse)) return false;
518 else if (autoCommitPreference.equals(IQuantumConstants.autoCommitSaved)) return autoCommit;
523 public void setJDBCDriver(JDBCDriver jdbcDriver) {
524 this.driver = BookmarkCollection.getInstance().findDriver(jdbcDriver);
528 public JDBCDriver getJDBCDriver() {
531 public boolean useAllSchemas() {
532 return this.schemaRule == SCHEMA_RULE_USE_ALL;
534 public boolean useUsernameAsSchema() {
535 return this.schemaRule == SCHEMA_RULE_USE_DEFAULT;
537 public boolean useSelectedSchemas() {
538 return this.schemaRule == SCHEMA_RULE_USE_SELECTED;
540 public int getSchemaRule() {
541 return this.schemaRule;
543 public void setSchemaRule(int schemaRule) {
544 if (this.schemaRule != schemaRule) {
545 this.schemaRule = schemaRule;
546 this.propertyChangeSupport.firePropertyChange("schemas", null, null);
550 public String getDisplayName() {
557 public void removeQuery(String query) {
558 if (this.queries.remove(query)) {
559 this.propertyChangeSupport.firePropertyChange("queries", null, null);