package com.quantum.model; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; import java.sql.Connection; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.Hashtable; import java.util.List; import java.util.Map; import java.util.Set; import com.quantum.IQuantumConstants; import com.quantum.QuantumPlugin; import com.quantum.adapters.AdapterFactory; import com.quantum.adapters.DatabaseAdapter; import com.quantum.sql.ConnectionEstablisher; import com.quantum.sql.MultiSQLServer; import org.eclipse.jface.preference.IPreferenceStore; /** * Class Bookmark holds the "static" information of a bookmark, that is the data that * is saved and loaded from the external file and describes a bookmark. This info will * be filled up by the end user. * * @author root */ public class Bookmark { private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this); private String name = ""; //$NON-NLS-1$ private String username = ""; //$NON-NLS-1$ private String password = ""; //$NON-NLS-1$ private String connect = ""; //$NON-NLS-1$ private String driver = ""; //$NON-NLS-1$ private String type = ""; //$NON-NLS-1$ private String driverFile = ""; //$NON-NLS-1$ /** * A quick list is a list of favourite tables that a person might want to view * without having to look at the entire list of tables. */ private Map quickList = new Hashtable(); private Set schemas = new HashSet(); private Connection connection = null; private ConnectionEstablisher connectionEstablisher; private boolean changed = true; private List queries = Collections.synchronizedList(new ArrayList()); private boolean promptForPassword = false; private boolean autoCommit = true; private String autoCommitPreference = IQuantumConstants.autoCommitTrue; public Bookmark() { this(MultiSQLServer.getInstance()); } public Bookmark(ConnectionEstablisher connectionEstablisher) { this.connectionEstablisher = connectionEstablisher; } public Bookmark(Bookmark data) { this(); setName(data.getName()); setUsername(data.getUsername()); setPassword(data.getPassword()); setConnect(data.getConnect()); setDriver(data.getDriver()); setType(data.getType()); setDriverFile(data.getDriverFile()); setPromptForPassword(data.getPromptForPassword()); setAutoCommit(data.isAutoCommit()); setAutoCommitPreference(data.getAutoCommitPreference()); this.schemas.addAll(data.schemas); this.quickList = new Hashtable(data.quickList); } /** * Returns the JDBC URL. * @return String */ public String getConnect() { return connect; } /** * Returns the driver. * @return String */ public String getDriver() { return driver; } /** * Returns the driverFile. * @return String */ public String getDriverFile() { return driverFile; } /** * Returns the password. * @return String */ public String getPassword() { return password; } /** * Returns the username. * @return String */ public String getUsername() { return username; } /** * Sets the connect. * @param connect The connect to set */ public void setConnect(String connect) { if (connect == null) { connect = ""; //$NON-NLS-1$ } this.connect = connect; } /** * Sets the driver. * @param driver The driver to set */ public void setDriver(String driver) { if (driver == null) { driver = ""; //$NON-NLS-1$ } this.driver = driver; } /** * Sets the driverFile. * @param driverFile The driverFile to set */ public void setDriverFile(String driverFile) { if (driverFile == null) { driverFile = ""; //$NON-NLS-1$ } this.driverFile = driverFile; } /** * Sets the password. * @param password The password to set */ public void setPassword(String password) { if (password == null) { password = ""; //$NON-NLS-1$ } this.password = password; } /** * Sets the username. * @param username The username to set */ public void setUsername(String username) { if (username == null) { username = ""; //$NON-NLS-1$ } this.username = username; } /** * Returns the name. * @return String */ public String getName() { return name; } /** * Sets the name. * @param name The name to set */ public void setName(String name) { if (name == null) { name = ""; //$NON-NLS-1$ } if (!name.equals(this.name)) { String oldName = this.name; this.name = name; this.propertyChangeSupport.firePropertyChange("name", oldName, this.name); this.changed = true; } } public boolean isEmpty() { if (name.equals("") && //$NON-NLS-1$ username.equals("") && //$NON-NLS-1$ password.equals("") && //$NON-NLS-1$ connect.equals("") && //$NON-NLS-1$ driver.equals("") && //$NON-NLS-1$ type.equals("") && //$NON-NLS-1$ driverFile.equals("")) { //$NON-NLS-1$ return true; } return false; } public String toString() { StringBuffer buffer = new StringBuffer(); buffer.append("["); //$NON-NLS-1$ buffer.append("name="); //$NON-NLS-1$ buffer.append(name); buffer.append(", "); //$NON-NLS-1$ buffer.append("username="); //$NON-NLS-1$ buffer.append(username); buffer.append(", "); //$NON-NLS-1$ buffer.append("password=****"); //$NON-NLS-1$ buffer.append(", "); //$NON-NLS-1$ buffer.append("connect="); //$NON-NLS-1$ buffer.append(connect); buffer.append(", "); //$NON-NLS-1$ buffer.append("driver="); //$NON-NLS-1$ buffer.append(driver); buffer.append(", "); //$NON-NLS-1$ buffer.append("type="); //$NON-NLS-1$ buffer.append(type); buffer.append(", "); //$NON-NLS-1$ buffer.append("driverFile="); //$NON-NLS-1$ buffer.append(driverFile); buffer.append("]"); //$NON-NLS-1$ return buffer.toString(); } public String getType() { return type; } public void setType(String type) { this.type = type; } public Connection connect(PasswordFinder passwordFinder) throws ConnectionException { boolean isConnected = isConnected(); if (this.connection == null) { this.connection = this.connectionEstablisher.connect(this, passwordFinder); } if (isConnected() != isConnected) { this.propertyChangeSupport.firePropertyChange( "connected", isConnected, isConnected()); } return this.connection; } /** * Returns the connection object. * @return the Connection object associated with the current JDBC source. * */ public Connection getConnection() throws NotConnectedException { if (this.connection == null) { throw new NotConnectedException(); } return this.connection; } /** * @return true if the BookmarkNode is connected to a JDBC source */ public boolean isConnected() { return (connection != null); } /** * Sets the connection member. From that moment on the BookmarkNode is "connected" (open) * @param connection : a valid connection to a JDBC source */ public void setConnection(Connection connection) { this.connection = connection; } public void disconnect() throws ConnectionException { boolean isConnected = isConnected(); try { if (this.connection != null) { this.connectionEstablisher.disconnect(this.connection); } } finally { this.connection = null; if (isConnected() != isConnected) { this.propertyChangeSupport.firePropertyChange( "connected", isConnected, isConnected()); } } } public void addSchema(String schema) { if (schema != null && schema.trim().length() > 0) { addSchema(new Schema(schema)); } } public void addSchema(Schema qualifier) { if (qualifier != null) { this.schemas.add(qualifier); this.changed = true; this.propertyChangeSupport.firePropertyChange("schemas", null, null); } } public void setSchemas(Schema[] schemas) { this.schemas.clear(); for (int i = 0, length = (schemas == null) ? 0 : schemas.length; i < length; i++) { this.schemas.add(schemas[i]); } this.changed = true; this.propertyChangeSupport.firePropertyChange("schemas", null, null); } /** * @return a list of all the schemas that have been set up. */ public Schema[] getSchemas() { Set set = new HashSet(); set.addAll(this.schemas); if (set.isEmpty()) { set.add(new Schema(getAdapter().getDefaultSchema(this.username), this.username, true)); } List list = new ArrayList(set); Collections.sort(list); return (Schema[]) list.toArray(new Schema[list.size()]); } /** * @see java.lang.Object#equals(java.lang.Object) */ public boolean equals(Object obj) { if (!(obj instanceof Bookmark)) return false; String name = ((Bookmark)obj).getName(); if (name.equals(this.getName())) return true; return false; } /** * @param listener */ public synchronized void addPropertyChangeListener(PropertyChangeListener listener) { this.propertyChangeSupport.addPropertyChangeListener(listener); } /** * @param listener */ public synchronized void removePropertyChangeListener(PropertyChangeListener listener) { this.propertyChangeSupport.removePropertyChangeListener(listener); } public void addQuickListEntry(String type, String schemaName, String name) { Entity entity = EntityFactory.getInstance().create(this, schemaName, name, type); this.quickList.put(entity.getCondQualifiedName(), entity); this.propertyChangeSupport.firePropertyChange("quickList", null, null); this.changed = true; } public void addQuickListEntry(Entity entity) { addQuickListEntry(entity.getType(), entity.getSchema(), entity.getName()); } public void removeQuickListEntry(Entity entity) { if (entity != null && this.quickList.containsKey(entity.getCondQualifiedName())) { this.quickList.remove(entity.getCondQualifiedName()); this.propertyChangeSupport.firePropertyChange("quickList", null, null); this.changed = true; } } public Entity[] getQuickListEntries() { return (Entity[]) this.quickList.values().toArray(new Entity[this.quickList.size()]); } public boolean hasQuickList() { return !this.quickList.isEmpty(); } /** * @return */ public boolean isChanged() { return changed; } /** * @param b */ public void setChanged(boolean b) { changed = b; } public Database getDatabase() throws NotConnectedException { if (!isConnected()) { throw new NotConnectedException(); } return new Database(this); } public DatabaseAdapter getAdapter() { return AdapterFactory.getInstance().getAdapter(getType()); } public Entity[] getEntitiesForSchema(Schema schema, String type) throws SQLException { try { Entity[] entities = getDatabase().getEntities(this, schema, type); return entities; } catch (NotConnectedException e) { return new Entity[0]; } } public Entity getEntity(Schema schema, String name) throws SQLException { Entity result = null; if (schema != null && name != null) { Entity[] entities = getEntitiesForSchema(schema, null); for (int i = 0, length = (entities == null) ? 0 : entities.length; result == null && i < length; i++) { if (schema.equals(entities[i].getSchema()) && name.equals(entities[i].getName())) { result = entities[i]; } } } return result; } public boolean isInQuickList(Entity entity) { return this.quickList.containsKey(entity.getCondQualifiedName()); } /** * * @param queryString */ public void addQuery(String queryString) { if (this.queries.contains(queryString)) { this.queries.remove(queryString); } this.queries.add(queryString); int size = getQueryHistorySize(); while (this.queries.size() > size) { this.queries.remove(0); } this.propertyChangeSupport.firePropertyChange("queries", null, null); this.changed = true; } public String[] getQueries() { return (String[]) this.queries.toArray(new String[this.queries.size()]); } private int getQueryHistorySize() { IPreferenceStore store = QuantumPlugin.getDefault().getPreferenceStore(); return store.getInt(getClass().getName() + ".queryHistorySize"); //$NON-NLS-1$ } /** * @return */ public boolean getPromptForPassword() { return promptForPassword; } /** * @param b */ public void setPromptForPassword(boolean b) { promptForPassword = b; } /** * @return */ public boolean isAutoCommit() { return autoCommit; } /** * @return */ public String getAutoCommitPreference() { return autoCommitPreference; } /** * @param b */ public void setAutoCommit(boolean b) { autoCommit = b; } /** * @param string */ public void setAutoCommitPreference(String string) { autoCommitPreference = string; } // Returns true or false indicating whether this bookmark must be set to AutoCommit on connection or not public boolean getDefaultAutoCommit(){ if (autoCommitPreference.equals(IQuantumConstants.autoCommitTrue)) return true; else if (autoCommitPreference.equals(IQuantumConstants.autoCommitFalse)) return false; else if (autoCommitPreference.equals(IQuantumConstants.autoCommitSaved)) return autoCommit; return true; } }