latest quantum sources 2.3.2
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / model / BookmarkCollection.java
index 58a854e..882f111 100644 (file)
@@ -6,20 +6,23 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Properties;
+import java.util.Set;
 import java.util.Vector;
 
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
 import com.quantum.IQuantumConstants;
 import com.quantum.Messages;
 import com.quantum.model.xml.ModelToXMLConverter;
 import com.quantum.sql.metadata.MetaDataXMLInterface;
 
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-
 /**
  * The collection of database bookmarks that the Quantum plug-in knows about.
  * This collection is loaded by the QuantumPlugin class before any Quantum plugin 
@@ -33,6 +36,7 @@ public class BookmarkCollection {
     private List bookmarks = new Vector();
     private boolean changed = false;
     private PropertyChangeSupport support = new PropertyChangeSupport(this);
+       private Set drivers = Collections.synchronizedSet(new HashSet());
     
     private BookmarkCollection() {
     }
@@ -71,28 +75,18 @@ public class BookmarkCollection {
             bookmark.setUsername(props.getProperty(i + ".username")); //$NON-NLS-1$
             bookmark.setPassword(props.getProperty(i + ".password")); //$NON-NLS-1$
             bookmark.setConnect(props.getProperty(i + ".connect")); //$NON-NLS-1$
-            bookmark.setDriver(props.getProperty(i + ".driver")); //$NON-NLS-1$
             String schema = props.getProperty(i + ".schema"); //$NON-NLS-1$
             if (schema != null) {
                 bookmark.addSchema(schema);
             }
-            String type = props.getProperty(i + ".type"); //$NON-NLS-1$
-            if (type != null) {
-                bookmark.setType(type);
-            } else {
-                bookmark.setType(""); //$NON-NLS-1$
-            }
-            String driverFile = props.getProperty(i + ".driverLocation"); //$NON-NLS-1$
-            if (driverFile != null) {
-                bookmark.setDriverFile(driverFile);
-            } else {
-                bookmark.setDriverFile(""); //$NON-NLS-1$
-            }
-            System.out.println(bookmark.toString());
             if (!bookmark.isEmpty()) {
                 newBookmarks.add(bookmark);
             }
             i++;
+            String driver = props.getProperty(i + ".driver"); //$NON-NLS-1$
+            String driverFile = props.getProperty(i + ".driverLocation"); //$NON-NLS-1$
+            String type = props.getProperty(i + ".type"); //$NON-NLS-1$
+            this.drivers.add(new JDBCDriver(driver, driverFile, type));
         }
         if (overwrite) {
             this.bookmarks = newBookmarks;
@@ -127,6 +121,10 @@ public class BookmarkCollection {
     public void exportXML(Element root) {
         System.out.println("Bookmarks: Saving to Element"); //$NON-NLS-1$
         Element bookmarkRoot = MetaDataXMLInterface.createElementText(root,"bookmarks", ""); //$NON-NLS-1$ //$NON-NLS-2$
+        for (Iterator i = this.drivers.iterator(); i.hasNext(); ) {
+            JDBCDriver driver = (JDBCDriver) i.next();
+            ModelToXMLConverter.getInstance().convert(bookmarkRoot, driver);
+        }
         for (int i = 0; i < bookmarks.size(); i++) {
             Bookmark b = (Bookmark) bookmarks.get(i);
             ModelToXMLConverter.getInstance().convert(bookmarkRoot, b);
@@ -141,7 +139,49 @@ public class BookmarkCollection {
     public void importXML(Element root) {
         this.changed = true;
         System.out.println("Bookmarks: Loading from Element"); //$NON-NLS-1$
-        Vector newBookmarks = new Vector();
+        importDrivers(root);
+        Vector newBookmarks = importBookmarks(root);
+        this.bookmarks.addAll(newBookmarks);
+        this.support.firePropertyChange("bookmarks", null, null);
+    }
+
+    /**
+        * @param root
+        * @return
+        */
+       private void importDrivers(Element root) {
+        NodeList nodes = root.getElementsByTagName("jdbcDriver"); //$NON-NLS-1$
+        for (int i = 0; i < nodes.getLength(); i++) {
+               Element driver = (Element) nodes.item(i);
+
+               if (!"".equals(driver.getAttribute("type"))) {
+                       addDriver(new JDBCDriver(
+                               driver.getAttribute("className"),
+                               driver.getAttribute("jarFileName"),
+                               driver.getAttribute("type"),
+                               driver.getAttribute("name"),
+                               driver.getAttribute("version")));
+               }
+               
+        }
+       }
+
+       /**
+        * @param driver
+        */
+       public void addDriver(JDBCDriver driver) {
+               if (!this.drivers.contains(driver)) {
+                       this.drivers.add(driver);
+                       this.support.firePropertyChange("drivers", null, driver);
+               }
+       }
+
+       /**
+        * @param root
+        * @return
+        */
+       private Vector importBookmarks(Element root) {
+               Vector newBookmarks = new Vector();
         NodeList nodes = root.getElementsByTagName("bookmark"); //$NON-NLS-1$
         for (int i = 0; i < nodes.getLength(); i++) {
             Bookmark bookmark = new Bookmark();
@@ -160,14 +200,21 @@ public class BookmarkCollection {
                        bookmark.setAutoCommit(Boolean.TRUE.toString().equalsIgnoreCase(
                                MetaDataXMLInterface.getElementText(column,"autoCommit", "True"))); //$NON-NLS-1$
                        bookmark.setAutoCommitPreference(MetaDataXMLInterface.getElementText(column,"autoCommitPreference", IQuantumConstants.autoCommitTrue)); //$NON-NLS-1$
-            bookmark.setDriver(MetaDataXMLInterface.getElementText(column,"driver")); //$NON-NLS-1$
-            bookmark.addSchema(MetaDataXMLInterface.getElementText(column,"schema")); //$NON-NLS-1$
-            bookmark.setType(MetaDataXMLInterface.getElementText(column,"type")); //$NON-NLS-1$
-            bookmark.setDriverFile(MetaDataXMLInterface.getElementText(column,"driverLocation")); //$NON-NLS-1$
-            NodeList children = column.getElementsByTagName(Messages.getString("ExportXMLAction.OtherSchemas"));
+                       
+                       backwardCompatibility(bookmark, column);
+
+            String driverClassName = MetaDataXMLInterface.getElementText(column,"driver"); //$NON-NLS-1$
+            String driverFile = MetaDataXMLInterface.getElementText(column,"driverLocation"); //$NON-NLS-1$
+            String type = MetaDataXMLInterface.getElementText(column,"type"); //$NON-NLS-1$
+            
+            bookmark.setJDBCDriver(new JDBCDriver(driverClassName, driverFile, type));
+            
+            NodeList children = column.getElementsByTagName("Other_Schemas");
             if (children.getLength() > 0) {
                 importSchemas((Element) children.item(0), bookmark);
             }
+            
+            
             System.out.println(bookmark.toString());
             if (!bookmark.isEmpty()) {
                 newBookmarks.addElement(bookmark);
@@ -175,21 +222,54 @@ public class BookmarkCollection {
             importQuickList(bookmark, column);
             importQueryList(bookmark, column);
         }
-        this.bookmarks.addAll(newBookmarks);
-        this.support.firePropertyChange("bookmarks", null, null);
-    }
+               return newBookmarks;
+       }
+
+       /**
+        * Earlier versions of the xml file expected one schema element under the 
+        * bookmark element.  This method sees if it exists.
+        * 
+        * @param bookmark
+        * @param element
+        */
+       private void backwardCompatibility(Bookmark bookmark, Element element) {
+               NodeList children = element.getChildNodes();
+               for (int i = 0, length = children.getLength(); i < length; i++) {
+                       Node node = children.item(i);
+                       if (node.getNodeType() == Node.ELEMENT_NODE && 
+                                       "schema".equals(((Element) node).getTagName())) {
+                               String schema = MetaDataXMLInterface.extractText(element,"");
+                               if (schema != null && schema.trim().length() > 0) {
+                                       bookmark.addSchema(schema);
+                               }
+                       }
+               }
+       }
 
-    private void importSchemas(Element otherSchemas, Bookmark bookmark) {
-        Vector vector = MetaDataXMLInterface.getVectorText(otherSchemas, Messages.getString("ExportXMLAction.SchemaName"));
+       private void importSchemas(Element otherSchemas, Bookmark bookmark) {
         List list = new ArrayList();
-        for (Iterator i = vector.iterator(); i.hasNext();) {
-            String schemaName = (String) i.next();
-            list.add(new Schema(schemaName));
+        NodeList children = otherSchemas.getChildNodes();
+        for (int i = 0, length = children.getLength(); i < length; i++) {
+               Node node = children.item(i);
+               if (node.getNodeType() == Node.ELEMENT_NODE
+                               && "schema".equalsIgnoreCase(((Element) node).getTagName())) {
+                   list.add(new Schema(
+                               MetaDataXMLInterface.extractText((Element) node, "")));
+               }
         }
-        bookmark.setSchemas((Schema[]) list.toArray(new Schema[list.size()]));
+        
+        String schemaRule = otherSchemas.getAttribute("schemaRule");
+        if ("useAll".equals(schemaRule)) {
+               bookmark.setSchemaRule(Bookmark.SCHEMA_RULE_USE_ALL);
+        } else if ("useDefault".equals(schemaRule)) {
+               bookmark.setSchemaRule(Bookmark.SCHEMA_RULE_USE_DEFAULT);
+        } else {
+               bookmark.setSchemaRule(Bookmark.SCHEMA_RULE_USE_SELECTED);
+        }
+        bookmark.setSchemaSelections((Schema[]) list.toArray(new Schema[list.size()]));
     }
 
-    private void importQuickList(Bookmark bookmark, Element bookmarkElement) {
+       private void importQuickList(Bookmark bookmark, Element bookmarkElement) {
         NodeList quickList = bookmarkElement.getElementsByTagName("quickList");
         for (int j = 0,
             length = (quickList == null) ? 0 : quickList.getLength();
@@ -254,6 +334,10 @@ public class BookmarkCollection {
     public Bookmark[] getBookmarks() {
         return (Bookmark[]) this.bookmarks.toArray(new Bookmark[this.bookmarks.size()]);
     }
+    
+    public JDBCDriver[] getJDBCDrivers() {
+       return (JDBCDriver[]) this.drivers.toArray(new JDBCDriver[this.drivers.size()]);
+    }
     /**
      * @return
      */
@@ -308,5 +392,33 @@ public class BookmarkCollection {
                return copyName;
        }
 
+       /**
+        * @param driver
+        * @param driverFile
+        */
+       public JDBCDriver findDriver(String driverClassName, String driverFile, String type) {
+               JDBCDriver temp = new JDBCDriver(driverClassName, driverFile, type);
+               return findDriver(temp);
+       }
+
+       /**
+        * @param temp
+        * @return
+        */
+       public JDBCDriver findDriver(JDBCDriver temp) {
+               JDBCDriver result = null;
+               for (Iterator i = this.drivers.iterator(); result == null && i.hasNext();) {
+                       JDBCDriver driver = (JDBCDriver) i.next();
+                       if (temp.equals(driver)) {
+                               result = driver;
+                       }
+               }
+               if (result == null) {
+                       addDriver(temp);
+                       result = temp;
+               }
+               return result;
+       }
+
 
 }