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 {
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());
80 this.schemas.addAll(data.schemas);
81 this.quickList = new Hashtable(data.quickList);
85 * Returns the JDBC URL.
88 public String getConnect() {
89 return this.connectionUrl;
93 * Returns the password.
96 public String getPassword() {
97 if (this.promptForPassword) {
100 return this.password;
105 * Returns the username.
108 public String getUsername() {
114 * @param connectionUrl The connect to set
116 public void setConnect(String connectionUrl) {
117 if (connectionUrl == null) {
118 connectionUrl = ""; //$NON-NLS-1$
120 this.connectionUrl = connectionUrl;
125 * @param password The password to set
127 public void setPassword(String password) {
128 if (password == null) {
129 password = ""; //$NON-NLS-1$
131 this.password = password;
136 * @param username The username to set
138 public void setUsername(String username) {
139 if (username == null) {
140 username = ""; //$NON-NLS-1$
142 this.username = username;
149 public String getName() {
155 * @param name The name to set
157 public void setName(String name) {
159 name = ""; //$NON-NLS-1$
161 if (!name.equals(this.name)) {
163 String oldName = this.name;
165 this.propertyChangeSupport.firePropertyChange("name", oldName, this.name);
170 public boolean isEmpty() {
171 if (name.equals("") && //$NON-NLS-1$
172 username.equals("") && //$NON-NLS-1$
173 password.equals("") && //$NON-NLS-1$
174 connectionUrl.equals("") && //$NON-NLS-1$
175 driver.equals("") && //$NON-NLS-1$
181 public String toString() {
182 StringBuffer buffer = new StringBuffer();
183 buffer.append("["); //$NON-NLS-1$
184 buffer.append("name="); //$NON-NLS-1$
186 buffer.append(", "); //$NON-NLS-1$
187 buffer.append("username="); //$NON-NLS-1$
188 buffer.append(username);
189 buffer.append(", "); //$NON-NLS-1$
190 buffer.append("password=****"); //$NON-NLS-1$
191 buffer.append(", "); //$NON-NLS-1$
192 buffer.append("connect="); //$NON-NLS-1$
193 buffer.append(connectionUrl);
194 buffer.append(", "); //$NON-NLS-1$
195 buffer.append("driver="); //$NON-NLS-1$
196 buffer.append(driver);
197 buffer.append("]"); //$NON-NLS-1$
198 return buffer.toString();
201 public Connection connect(PasswordFinder passwordFinder) throws ConnectionException {
202 boolean isConnected = isConnected();
203 if (this.connection == null) {
204 this.connection = this.connectionEstablisher.connect(this, passwordFinder);
207 if (isConnected() != isConnected) {
208 this.propertyChangeSupport.firePropertyChange(
209 "connected", isConnected, isConnected());
211 return this.connection;
215 * Returns the connection object.
216 * @return the Connection object associated with the current JDBC source.
219 public Connection getConnection() throws NotConnectedException {
220 if (this.connection == null) {
221 throw new NotConnectedException();
223 return this.connection;
227 * @return true if the BookmarkNode is connected to a JDBC source
229 public boolean isConnected() {
230 return (connection != null);
234 * Sets the connection member. From that moment on the BookmarkNode is "connected" (open)
235 * @param connection : a valid connection to a JDBC source
237 public void setConnection(Connection connection) {
238 this.connection = connection;
241 public void disconnect() throws ConnectionException {
242 boolean isConnected = isConnected();
244 if (this.connection != null) {
245 this.connectionEstablisher.disconnect(this.connection);
248 this.connection = null;
249 if (isConnected() != isConnected) {
250 this.propertyChangeSupport.firePropertyChange(
251 "connected", isConnected, isConnected());
255 public void addSchema(String schema) {
256 if (schema != null && schema.trim().length() > 0) {
257 addSchema(new Schema(schema));
261 public void addSchema(Schema qualifier) {
262 if (qualifier != null) {
263 this.schemas.add(qualifier);
265 this.propertyChangeSupport.firePropertyChange("schemas", null, null);
269 public void setSchemaSelections(Schema[] schemas) {
270 this.schemas.clear();
271 for (int i = 0, length = (schemas == null) ? 0 : schemas.length;
274 this.schemas.add(schemas[i]);
278 this.propertyChangeSupport.firePropertyChange("schemas", null, null);
282 * @return a list of all the schemas that have been set up.
284 public Schema[] getSchemaSelections() {
285 List list = new ArrayList(this.schemas);
286 Collections.sort(list);
287 return (Schema[]) list.toArray(new Schema[list.size()]);
290 public Schema[] getSchemas() throws NotConnectedException, SQLException {
291 Schema[] schemas = null;
292 if (useUsernameAsSchema()) {
294 } else if (useAllSchemas()) {
295 schemas = getDatabase().getSchemas();
297 schemas = verifySchemas(getSchemaSelections());
299 return (schemas == null || schemas.length == 0)
300 ? new Schema[] { getDefaultSchema() }
305 * @param schemaSelections
307 * @throws SQLException
308 * @throws NotConnectedException
310 private Schema[] verifySchemas(Schema[] schemaSelections)
311 throws NotConnectedException, SQLException {
312 Schema[] schemasFromDatabase = getDatabase().getSchemas();
313 List list = Arrays.asList(schemasFromDatabase);
314 for (int i = 0, length = schemaSelections == null ? 0 : schemaSelections.length;
316 schemaSelections[i].setExists(list.contains(schemaSelections[i]));
318 return schemaSelections;
323 * @throws NotConnectedException
324 * @throws SQLException
326 private Schema getDefaultSchema() throws NotConnectedException, SQLException {
327 String username = getDatabase().getUsername();
328 String actual = getAdapter().getDefaultSchema(username);
329 return new Schema(actual, username, true);
333 * @see java.lang.Object#equals(java.lang.Object)
335 public boolean equals(Object obj) {
336 if (!(obj instanceof Bookmark)) return false;
337 String name = ((Bookmark)obj).getName();
338 if (name.equals(this.getName())) return true;
345 public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
346 this.propertyChangeSupport.addPropertyChangeListener(listener);
352 public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
353 this.propertyChangeSupport.removePropertyChangeListener(listener);
356 public void addQuickListEntry(String type, String schemaName, String name) {
357 Entity entity = EntityFactory.getInstance().create(this, schemaName, name, type);
358 this.quickList.put(entity.getQualifiedName(), entity);
359 this.propertyChangeSupport.firePropertyChange("quickList", null, null);
363 public void addQuickListEntry(Entity entity) {
364 addQuickListEntry(entity.getType(), entity.getSchema(), entity.getName());
367 public void removeQuickListEntry(Entity entity) {
368 if (entity != null && this.quickList.containsKey(entity.getQualifiedName())) {
369 this.quickList.remove(entity.getQualifiedName());
370 this.propertyChangeSupport.firePropertyChange("quickList", null, null);
375 public Entity[] getQuickListEntries() {
376 return (Entity[]) this.quickList.values().toArray(new Entity[this.quickList.size()]);
379 public boolean hasQuickList() {
380 return !this.quickList.isEmpty();
385 public boolean isChanged() {
392 public void setChanged(boolean b) {
396 public Database getDatabase() throws NotConnectedException {
397 if (!isConnected()) {
398 throw new NotConnectedException();
400 return new Database(this);
403 public DatabaseAdapter getAdapter() {
404 return this.driver == null
406 : AdapterFactory.getInstance().getAdapter(this.driver.getType());
409 public Entity[] getEntitiesForSchema(Schema schema, String type) throws SQLException {
411 Entity[] entities = getDatabase().getEntities(this, schema, type);
413 } catch (NotConnectedException e) {
414 return new Entity[0];
418 public Entity getEntity(Schema schema, String name) throws SQLException {
419 Entity result = null;
420 if (schema != null && name != null) {
421 Entity[] entities = getEntitiesForSchema(schema, null);
422 for (int i = 0, length = (entities == null) ? 0 : entities.length;
423 result == null && i < length;
425 if (schema.equals(entities[i].getSchema()) &&
426 name.equals(entities[i].getName())) {
427 result = entities[i];
434 public boolean isInQuickList(Entity entity) {
435 return this.quickList.containsKey(entity.getQualifiedName());
442 public void addQuery(String queryString) {
443 if (this.queries.contains(queryString)) {
444 this.queries.remove(queryString);
446 this.queries.add(queryString);
448 int size = getQueryHistorySize();
450 while (this.queries.size() > size) {
451 this.queries.remove(0);
453 this.propertyChangeSupport.firePropertyChange("queries", null, null);
457 public String[] getQueries() {
458 return (String[]) this.queries.toArray(new String[this.queries.size()]);
461 private int getQueryHistorySize() {
462 IPreferenceStore store =
463 QuantumPlugin.getDefault().getPreferenceStore();
464 return store.getInt(getClass().getName() + ".queryHistorySize"); //$NON-NLS-1$
469 public boolean getPromptForPassword() {
470 return promptForPassword;
476 public void setPromptForPassword(boolean b) {
477 promptForPassword = b;
483 public boolean isAutoCommit() {
490 public String getAutoCommitPreference() {
491 return autoCommitPreference;
497 public void setAutoCommit(boolean b) {
504 public void setAutoCommitPreference(String string) {
505 autoCommitPreference = string;
508 // Returns true or false indicating whether this bookmark must be set to AutoCommit on connection or not
509 public boolean getDefaultAutoCommit(){
510 if (autoCommitPreference.equals(IQuantumConstants.autoCommitTrue)) return true;
511 else if (autoCommitPreference.equals(IQuantumConstants.autoCommitFalse)) return false;
512 else if (autoCommitPreference.equals(IQuantumConstants.autoCommitSaved)) return autoCommit;
517 public void setJDBCDriver(JDBCDriver jdbcDriver) {
518 this.driver = BookmarkCollection.getInstance().findDriver(jdbcDriver);
522 public JDBCDriver getJDBCDriver() {
525 public boolean useAllSchemas() {
526 return this.schemaRule == SCHEMA_RULE_USE_ALL;
528 public boolean useUsernameAsSchema() {
529 return this.schemaRule == SCHEMA_RULE_USE_DEFAULT;
531 public boolean useSelectedSchemas() {
532 return this.schemaRule == SCHEMA_RULE_USE_SELECTED;
534 public int getSchemaRule() {
535 return this.schemaRule;
537 public void setSchemaRule(int schemaRule) {
538 if (this.schemaRule != schemaRule) {
539 this.schemaRule = schemaRule;
540 this.propertyChangeSupport.firePropertyChange("schemas", null, null);