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;
62 this(MultiSQLServer.getInstance());
65 public Bookmark(ConnectionEstablisher connectionEstablisher) {
66 this.connectionEstablisher = connectionEstablisher;
69 public Bookmark(Bookmark data) {
71 setName(data.getName());
72 setUsername(data.getUsername());
73 setPassword(data.getPassword());
74 setConnect(data.getConnect());
75 setJDBCDriver(data.getJDBCDriver());
76 setPromptForPassword(data.getPromptForPassword());
77 setAutoCommit(data.isAutoCommit());
78 setAutoCommitPreference(data.getAutoCommitPreference());
79 setSchemaRule(data.getSchemaRule());
81 this.schemas.addAll(data.schemas);
82 this.quickList = new Hashtable(data.quickList);
86 * Returns the JDBC URL.
89 public String getConnect() {
90 return this.connectionUrl;
94 * Returns the password.
97 public String getPassword() {
98 if (this.promptForPassword) {
101 return this.password;
106 * Returns the username.
109 public String getUsername() {
115 * @param connectionUrl The connect to set
117 public void setConnect(String connectionUrl) {
118 if (connectionUrl == null) {
119 connectionUrl = ""; //$NON-NLS-1$
121 this.connectionUrl = connectionUrl;
126 * @param password The password to set
128 public void setPassword(String password) {
129 if (password == null) {
130 password = ""; //$NON-NLS-1$
132 this.password = password;
137 * @param username The username to set
139 public void setUsername(String username) {
140 if (username == null) {
141 username = ""; //$NON-NLS-1$
143 this.username = username;
150 public String getName() {
156 * @param name The name to set
158 public void setName(String name) {
160 name = ""; //$NON-NLS-1$
162 if (!name.equals(this.name)) {
164 String oldName = this.name;
166 this.propertyChangeSupport.firePropertyChange("name", oldName, this.name);
171 public boolean isEmpty() {
172 if (name.equals("") && //$NON-NLS-1$
173 username.equals("") && //$NON-NLS-1$
174 password.equals("") && //$NON-NLS-1$
175 connectionUrl.equals("") && //$NON-NLS-1$
176 driver.equals("") && //$NON-NLS-1$
182 public String toString() {
183 StringBuffer buffer = new StringBuffer();
184 buffer.append("["); //$NON-NLS-1$
185 buffer.append("name="); //$NON-NLS-1$
187 buffer.append(", "); //$NON-NLS-1$
188 buffer.append("username="); //$NON-NLS-1$
189 buffer.append(username);
190 buffer.append(", "); //$NON-NLS-1$
191 buffer.append("password=****"); //$NON-NLS-1$
192 buffer.append(", "); //$NON-NLS-1$
193 buffer.append("connect="); //$NON-NLS-1$
194 buffer.append(connectionUrl);
195 buffer.append(", "); //$NON-NLS-1$
196 buffer.append("driver="); //$NON-NLS-1$
197 buffer.append(driver);
198 buffer.append("]"); //$NON-NLS-1$
199 return buffer.toString();
202 public Connection connect(PasswordFinder passwordFinder) throws ConnectionException {
203 boolean isConnected = isConnected();
204 if (this.connection == null) {
205 this.connection = this.connectionEstablisher.connect(this, passwordFinder);
208 if (isConnected() != isConnected) {
209 this.propertyChangeSupport.firePropertyChange(
210 "connected", isConnected, isConnected());
212 return this.connection;
216 * Returns the connection object.
217 * @return the Connection object associated with the current JDBC source.
220 public Connection getConnection() throws NotConnectedException {
221 if (this.connection == null) {
222 throw new NotConnectedException();
224 return this.connection;
228 * @return true if the BookmarkNode is connected to a JDBC source
230 public boolean isConnected() {
231 return (connection != null);
235 * Sets the connection member. From that moment on the BookmarkNode is "connected" (open)
236 * @param connection : a valid connection to a JDBC source
238 public void setConnection(Connection connection) {
239 this.connection = connection;
242 public void disconnect() throws ConnectionException {
243 boolean isConnected = isConnected();
245 if (this.connection != null) {
246 this.connectionEstablisher.disconnect(this.connection);
249 this.connection = null;
250 if (isConnected() != isConnected) {
251 this.propertyChangeSupport.firePropertyChange(
252 "connected", isConnected, isConnected());
256 public void addSchema(String schema) {
257 if (schema != null && schema.trim().length() > 0) {
258 addSchema(new Schema(schema));
262 public void addSchema(Schema qualifier) {
263 if (qualifier != null) {
264 this.schemas.add(qualifier);
266 this.propertyChangeSupport.firePropertyChange("schemas", null, null);
270 public void setSchemaSelections(Schema[] schemas) {
271 this.schemas.clear();
272 for (int i = 0, length = (schemas == null) ? 0 : schemas.length;
275 this.schemas.add(schemas[i]);
279 this.propertyChangeSupport.firePropertyChange("schemas", null, null);
283 * @return a list of all the schemas that have been set up.
285 public Schema[] getSchemaSelections() {
286 List list = new ArrayList(this.schemas);
287 Collections.sort(list);
288 return (Schema[]) list.toArray(new Schema[list.size()]);
291 public Schema[] getSchemas() throws NotConnectedException, SQLException {
292 Schema[] schemas = null;
293 if (useUsernameAsSchema()) {
295 } else if (useAllSchemas()) {
296 schemas = getDatabase().getSchemas();
298 schemas = verifySchemas(getSchemaSelections());
300 return (schemas == null || schemas.length == 0)
301 ? new Schema[] { getDefaultSchema() }
306 * @param schemaSelections
308 * @throws SQLException
309 * @throws NotConnectedException
311 private Schema[] verifySchemas(Schema[] schemaSelections)
312 throws NotConnectedException, SQLException {
313 Schema[] schemasFromDatabase = getDatabase().getSchemas();
314 List list = Arrays.asList(schemasFromDatabase);
315 for (int i = 0, length = schemaSelections == null ? 0 : schemaSelections.length;
317 schemaSelections[i].setExists(list.contains(schemaSelections[i]));
319 return schemaSelections;
324 * @throws NotConnectedException
325 * @throws SQLException
327 private Schema getDefaultSchema() throws NotConnectedException, SQLException {
328 String username = getDatabase().getUsername();
329 String actual = getAdapter().getDefaultSchema(username);
330 return new Schema(actual, username, true);
334 * @see java.lang.Object#equals(java.lang.Object)
336 public boolean equals(Object obj) {
337 if (!(obj instanceof Bookmark)) return false;
338 String name = ((Bookmark)obj).getName();
339 if (name.equals(this.getName())) return true;
346 public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
347 this.propertyChangeSupport.addPropertyChangeListener(listener);
353 public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
354 this.propertyChangeSupport.removePropertyChangeListener(listener);
357 public void addQuickListEntry(String type, String schemaName, String name) {
358 Entity entity = EntityFactory.getInstance().create(this, schemaName, name, type);
359 this.quickList.put(entity.getQualifiedName(), entity);
360 this.propertyChangeSupport.firePropertyChange("quickList", null, null);
364 public void addQuickListEntry(Entity entity) {
365 addQuickListEntry(entity.getType(), entity.getSchema(), entity.getName());
368 public void removeQuickListEntry(Entity entity) {
369 if (entity != null && this.quickList.containsKey(entity.getQualifiedName())) {
370 this.quickList.remove(entity.getQualifiedName());
371 this.propertyChangeSupport.firePropertyChange("quickList", null, null);
376 public Entity[] getQuickListEntries() {
377 return (Entity[]) this.quickList.values().toArray(new Entity[this.quickList.size()]);
380 public boolean hasQuickList() {
381 return !this.quickList.isEmpty();
386 public boolean isChanged() {
393 public void setChanged(boolean b) {
397 public Database getDatabase() throws NotConnectedException {
398 if (!isConnected()) {
399 throw new NotConnectedException();
401 return new Database(this);
404 public DatabaseAdapter getAdapter() {
405 return this.driver == null
407 : AdapterFactory.getInstance().getAdapter(this.driver.getType());
410 public Entity[] getEntitiesForSchema(Schema schema, String type) throws SQLException {
412 Entity[] entities = getDatabase().getEntities(this, schema, type);
414 } catch (NotConnectedException e) {
415 return new Entity[0];
419 public Entity getEntity(Schema schema, String name) throws SQLException {
420 Entity result = null;
421 if (schema != null && name != null) {
422 Entity[] entities = getEntitiesForSchema(schema, null);
423 for (int i = 0, length = (entities == null) ? 0 : entities.length;
424 result == null && i < length;
426 if (schema.equals(entities[i].getSchema()) &&
427 name.equals(entities[i].getName())) {
428 result = entities[i];
435 public boolean isInQuickList(Entity entity) {
436 return this.quickList.containsKey(entity.getQualifiedName());
443 public void addQuery(String queryString) {
444 if (this.queries.contains(queryString)) {
445 this.queries.remove(queryString);
447 this.queries.add(queryString);
449 int size = getQueryHistorySize();
451 while (this.queries.size() > size) {
452 this.queries.remove(0);
454 this.propertyChangeSupport.firePropertyChange("queries", null, null);
458 public String[] getQueries() {
459 return (String[]) this.queries.toArray(new String[this.queries.size()]);
462 private int getQueryHistorySize() {
463 IPreferenceStore store =
464 QuantumPlugin.getDefault().getPreferenceStore();
465 return store.getInt(getClass().getName() + ".queryHistorySize"); //$NON-NLS-1$
470 public boolean getPromptForPassword() {
471 return promptForPassword;
477 public void setPromptForPassword(boolean b) {
478 promptForPassword = b;
484 public boolean isAutoCommit() {
491 public String getAutoCommitPreference() {
492 return autoCommitPreference;
498 public void setAutoCommit(boolean b) {
505 public void setAutoCommitPreference(String string) {
506 autoCommitPreference = string;
509 // Returns true or false indicating whether this bookmark must be set to AutoCommit on connection or not
510 public boolean getDefaultAutoCommit(){
511 if (autoCommitPreference.equals(IQuantumConstants.autoCommitTrue)) return true;
512 else if (autoCommitPreference.equals(IQuantumConstants.autoCommitFalse)) return false;
513 else if (autoCommitPreference.equals(IQuantumConstants.autoCommitSaved)) return autoCommit;
518 public void setJDBCDriver(JDBCDriver jdbcDriver) {
519 this.driver = BookmarkCollection.getInstance().findDriver(jdbcDriver);
523 public JDBCDriver getJDBCDriver() {
526 public boolean useAllSchemas() {
527 return this.schemaRule == SCHEMA_RULE_USE_ALL;
529 public boolean useUsernameAsSchema() {
530 return this.schemaRule == SCHEMA_RULE_USE_DEFAULT;
532 public boolean useSelectedSchemas() {
533 return this.schemaRule == SCHEMA_RULE_USE_SELECTED;
535 public int getSchemaRule() {
536 return this.schemaRule;
538 public void setSchemaRule(int schemaRule) {
539 if (this.schemaRule != schemaRule) {
540 this.schemaRule = schemaRule;
541 this.propertyChangeSupport.firePropertyChange("schemas", null, null);
545 public String getDisplayName() {