latest quantum sources 2.3.2
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / model / Bookmark.java
index 8082a39..3ee61be 100644 (file)
@@ -5,6 +5,7 @@ import java.beans.PropertyChangeSupport;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Hashtable;
@@ -29,15 +30,19 @@ import org.eclipse.jface.preference.IPreferenceStore;
  * @author root
  */
 public class Bookmark {
+       
+       public static final int SCHEMA_RULE_USE_ALL = 1;
+       public static final int SCHEMA_RULE_USE_DEFAULT = 2;
+       public static final int SCHEMA_RULE_USE_SELECTED = 3;
     
     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$
+    private String connectionUrl = ""; //$NON-NLS-1$
+    private JDBCDriver driver;
+    
+    private int schemaRule = SCHEMA_RULE_USE_ALL;
     
     /**
      * A quick list is a list of favourite tables that a person might want to view
@@ -67,9 +72,7 @@ public class Bookmark {
                setUsername(data.getUsername());
                setPassword(data.getPassword());
                setConnect(data.getConnect());
-               setDriver(data.getDriver());
-               setType(data.getType());
-               setDriverFile(data.getDriverFile());
+               setJDBCDriver(data.getJDBCDriver());
         setPromptForPassword(data.getPromptForPassword());
         setAutoCommit(data.isAutoCommit());
         setAutoCommitPreference(data.getAutoCommitPreference());
@@ -83,23 +86,7 @@ public class Bookmark {
         * @return String
         */
        public String getConnect() {
-               return connect;
-       }
-
-       /**
-        * Returns the driver.
-        * @return String
-        */
-       String getDriver() {
-               return driver;
-       }
-
-       /**
-        * Returns the driverFile.
-        * @return String
-        */
-       String getDriverFile() {
-               return driverFile;
+               return this.connectionUrl;
        }
 
        /**
@@ -107,7 +94,11 @@ public class Bookmark {
         * @return String
         */
        public String getPassword() {
-               return password;
+               if (this.promptForPassword) {
+                       return null;
+               } else {
+                       return this.password;
+               }
        }
 
        /**
@@ -120,35 +111,13 @@ public class Bookmark {
 
        /**
         * Sets the connect.
-        * @param connect The connect to set
+        * @param connectionUrl The connect to set
         */
-       public void setConnect(String connect) {
-               if (connect == null) {
-                       connect = ""; //$NON-NLS-1$
+       public void setConnect(String connectionUrl) {
+               if (connectionUrl == null) {
+                       connectionUrl = ""; //$NON-NLS-1$
                }
-               this.connect = connect;
-       }
-
-       /**
-        * Sets the driver.
-        * @param driver The driver to set
-        */
-       void setDriver(String driver) {
-               if (driver == null) {
-                       driver = ""; //$NON-NLS-1$
-               }
-               this.driver = driver;
-       }
-
-       /**
-        * Sets the driverFile.
-        * @param driverFile The driverFile to set
-        */
-       void setDriverFile(String driverFile) {
-               if (driverFile == null) {
-                       driverFile = ""; //$NON-NLS-1$
-               }
-               this.driverFile = driverFile;
+               this.connectionUrl = connectionUrl;
        }
 
        /**
@@ -202,10 +171,9 @@ public class Bookmark {
                if (name.equals("") && //$NON-NLS-1$
                username.equals("") && //$NON-NLS-1$
                password.equals("") && //$NON-NLS-1$
-               connect.equals("") && //$NON-NLS-1$
+               connectionUrl.equals("") && //$NON-NLS-1$
                driver.equals("") && //$NON-NLS-1$
-               type.equals("") && //$NON-NLS-1$
-               driverFile.equals("")) { //$NON-NLS-1$
+               driver == null) {
                        return true;
                }
                return false;
@@ -222,28 +190,14 @@ public class Bookmark {
                buffer.append("password=****"); //$NON-NLS-1$
                buffer.append(", "); //$NON-NLS-1$
                buffer.append("connect="); //$NON-NLS-1$
-               buffer.append(connect);
+               buffer.append(connectionUrl);
                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) {
@@ -312,7 +266,7 @@ public class Bookmark {
         }
     }
 
-    public void setSchemas(Schema[] schemas) {
+    public void setSchemaSelections(Schema[] schemas) {
         this.schemas.clear();
         for (int i = 0, length = (schemas == null) ? 0 : schemas.length;
             i < length;
@@ -327,17 +281,53 @@ public class Bookmark {
     /**
      * @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);
+    public Schema[] getSchemaSelections() {
+        List list = new ArrayList(this.schemas);
         Collections.sort(list);
         return (Schema[]) list.toArray(new Schema[list.size()]);
     }
+    
+    public Schema[] getSchemas() throws NotConnectedException, SQLException {
+       Schema[] schemas = null;
+       if (useUsernameAsSchema()) {
+               // do nothing
+       } else if (useAllSchemas()) {
+               schemas = getDatabase().getSchemas();
+       } else {
+               schemas = verifySchemas(getSchemaSelections());
+       }
+       return (schemas == null || schemas.length == 0) 
+                               ? new Schema[] { getDefaultSchema() } 
+                       : schemas;
+    }
+
+       /**
+        * @param schemaSelections
+        * @return
+        * @throws SQLException
+        * @throws NotConnectedException
+        */
+       private Schema[] verifySchemas(Schema[] schemaSelections) 
+                       throws NotConnectedException, SQLException {
+               Schema[] schemasFromDatabase = getDatabase().getSchemas();
+               List list = Arrays.asList(schemasFromDatabase);
+               for (int i = 0, length = schemaSelections == null ? 0 : schemaSelections.length; 
+                               i < length; i++) {
+                       schemaSelections[i].setExists(list.contains(schemaSelections[i]));
+               }
+               return schemaSelections;
+       }
+
+       /**
+        * @return
+        * @throws NotConnectedException
+        * @throws SQLException
+        */
+       private Schema getDefaultSchema() throws NotConnectedException, SQLException {
+               String username = getDatabase().getUsername();
+               String actual = getAdapter().getDefaultSchema(username);
+               return new Schema(actual, username, true);
+       }
 
        /**
         * @see java.lang.Object#equals(java.lang.Object)
@@ -365,7 +355,7 @@ public class Bookmark {
 
     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.quickList.put(entity.getQualifiedName(), entity);
         this.propertyChangeSupport.firePropertyChange("quickList", null, null);
         this.changed = true;
     }
@@ -375,8 +365,8 @@ public class Bookmark {
     }
     
     public void removeQuickListEntry(Entity entity) {
-        if (entity != null  && this.quickList.containsKey(entity.getCondQualifiedName())) {
-            this.quickList.remove(entity.getCondQualifiedName());
+        if (entity != null  && this.quickList.containsKey(entity.getQualifiedName())) {
+            this.quickList.remove(entity.getQualifiedName());
             this.propertyChangeSupport.firePropertyChange("quickList", null, null);
             this.changed = true;
         }
@@ -411,7 +401,9 @@ public class Bookmark {
     }
     
     public DatabaseAdapter getAdapter() {
-        return AdapterFactory.getInstance().getAdapter(getType());
+        return this.driver == null 
+                               ? null 
+                               : AdapterFactory.getInstance().getAdapter(this.driver.getType());
     }
 
     public Entity[] getEntitiesForSchema(Schema schema, String type) throws SQLException {
@@ -440,7 +432,7 @@ public class Bookmark {
     }
     
     public boolean isInQuickList(Entity entity) {
-        return this.quickList.containsKey(entity.getCondQualifiedName());
+        return this.quickList.containsKey(entity.getQualifiedName());
     }
     
     /**
@@ -523,12 +515,29 @@ public class Bookmark {
        }
        
        public void setJDBCDriver(JDBCDriver jdbcDriver) {
-               jdbcDriver = BookmarkCollection.getInstance().findDriver(jdbcDriver);
-               setDriver(jdbcDriver.getClassName());
-               setDriverFile(jdbcDriver.getJarFileName());
+               this.driver = BookmarkCollection.getInstance().findDriver(jdbcDriver);
+        this.changed = true;
        }
        
        public JDBCDriver getJDBCDriver() {
-               return BookmarkCollection.getInstance().findDriver(getDriver(), getDriverFile());
+               return this.driver;
+       }
+       public boolean useAllSchemas() {
+               return this.schemaRule == SCHEMA_RULE_USE_ALL;
+       }
+       public boolean useUsernameAsSchema() {
+               return this.schemaRule == SCHEMA_RULE_USE_DEFAULT;
+       }
+       public boolean useSelectedSchemas() {
+               return this.schemaRule == SCHEMA_RULE_USE_SELECTED;
+       }
+       public int getSchemaRule() {
+               return this.schemaRule;
+       }
+       public void setSchemaRule(int schemaRule) {
+        if (this.schemaRule != schemaRule) {
+            this.schemaRule = schemaRule;
+            this.propertyChangeSupport.firePropertyChange("schemas", null, null);
+        }
        }
 }