improved syntax highlighting (php tag/return keyword/brackets and operators)
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / model / BookmarkCollection.java
1 package com.quantum.model;
2
3 import java.beans.PropertyChangeListener;
4 import java.beans.PropertyChangeSupport;
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.IOException;
8 import java.util.ArrayList;
9 import java.util.Collections;
10 import java.util.HashSet;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Properties;
14 import java.util.Set;
15 import java.util.Vector;
16
17 import org.w3c.dom.Element;
18 import org.w3c.dom.Node;
19 import org.w3c.dom.NodeList;
20
21 import com.quantum.IQuantumConstants;
22 import com.quantum.Messages;
23 import com.quantum.model.xml.ModelToXMLConverter;
24 import com.quantum.sql.metadata.MetaDataXMLInterface;
25
26 /**
27  * The collection of database bookmarks that the Quantum plug-in knows about.
28  * This collection is loaded by the QuantumPlugin class before any Quantum plugin 
29  * extension is invoked.
30  * 
31  * @author BC
32  */
33 public class BookmarkCollection {
34     
35     private static BookmarkCollection instance = new BookmarkCollection();
36     private List bookmarks = new Vector();
37     private boolean changed = false;
38     private PropertyChangeSupport support = new PropertyChangeSupport(this);
39         private Set drivers = Collections.synchronizedSet(new HashSet());
40     
41     private BookmarkCollection() {
42     }
43
44     /**
45      * Singleton accessor
46      */
47     public static BookmarkCollection getInstance() {
48         return BookmarkCollection.instance;
49     }
50     
51     /**
52      * Imports the bookmars from a properties file.  This load method is 
53      * provided for backwards compatability only; we no longer persist 
54      * bookmarks as properties files.
55      * @param file
56      */
57     public void load(File file) throws IOException {
58         Properties props = new Properties();
59         FileInputStream in = new FileInputStream(file);
60         props.load(in);
61         in.close();
62         fromProperties(true, props);
63     }
64      
65     private void fromProperties(boolean overwrite, Properties props) {
66         List newBookmarks = new Vector();
67         int i = 0;
68         while (true) {
69             Bookmark bookmark = new Bookmark();
70             String name = props.getProperty(i + ".name"); //$NON-NLS-1$
71             if (name == null) {
72                 break;
73             }
74             bookmark.setName(name);
75             bookmark.setUsername(props.getProperty(i + ".username")); //$NON-NLS-1$
76             bookmark.setPassword(props.getProperty(i + ".password")); //$NON-NLS-1$
77             bookmark.setConnect(props.getProperty(i + ".connect")); //$NON-NLS-1$
78             String schema = props.getProperty(i + ".schema"); //$NON-NLS-1$
79             if (schema != null) {
80                 bookmark.addSchema(schema);
81             }
82             if (!bookmark.isEmpty()) {
83                 newBookmarks.add(bookmark);
84             }
85             i++;
86             String driver = props.getProperty(i + ".driver"); //$NON-NLS-1$
87             String driverFile = props.getProperty(i + ".driverLocation"); //$NON-NLS-1$
88             String type = props.getProperty(i + ".type"); //$NON-NLS-1$
89             this.drivers.add(new JDBCDriver(driver, driverFile, type));
90         }
91         if (overwrite) {
92             this.bookmarks = newBookmarks;
93         } else {
94             this.bookmarks.addAll(newBookmarks);
95         }
96     }
97     /**
98      * Finds a Bookmark with the specified name.
99      * 
100      * @param name
101      * @return the bookmark with the specified name, or null if no bookmark can be found
102      */
103     public Bookmark find(String name){
104         Bookmark result = null;
105         if (name != null) {
106             for (Iterator i = this.bookmarks.iterator(); result == null && i.hasNext(); ) {
107                 Bookmark temp = (Bookmark) i.next();
108                 if (name.equals(temp.getName())) {
109                     result = temp;
110                 }
111             }
112         }
113         return result;
114     }
115
116     /**
117      * Exports a Bookmark data to an XMLDocument Element
118      * The complementary function is importXML()
119      * @param root  The Element to fill up with the bookmark info
120      */
121     public void exportXML(Element root) {
122         System.out.println("Bookmarks: Saving to Element"); //$NON-NLS-1$
123         Element bookmarkRoot = MetaDataXMLInterface.createElementText(root,"bookmarks", ""); //$NON-NLS-1$ //$NON-NLS-2$
124         for (Iterator i = this.drivers.iterator(); i.hasNext(); ) {
125             JDBCDriver driver = (JDBCDriver) i.next();
126             ModelToXMLConverter.getInstance().convert(bookmarkRoot, driver);
127         }
128         for (int i = 0; i < bookmarks.size(); i++) {
129             Bookmark b = (Bookmark) bookmarks.get(i);
130             ModelToXMLConverter.getInstance().convert(bookmarkRoot, b);
131         }
132     }
133
134     /**
135      * Imports a Bookmark data from an XMLDocument Element
136      * The complementary function is exportXML()
137      * @param root  The Element from which to load
138      */
139     public void importXML(Element root) {
140         this.changed = true;
141         System.out.println("Bookmarks: Loading from Element"); //$NON-NLS-1$
142         importDrivers(root);
143         Vector newBookmarks = importBookmarks(root);
144         this.bookmarks.addAll(newBookmarks);
145         this.support.firePropertyChange("bookmarks", null, null);
146     }
147
148     /**
149          * @param root
150          * @return
151          */
152         private void importDrivers(Element root) {
153         NodeList nodes = root.getElementsByTagName("jdbcDriver"); //$NON-NLS-1$
154         for (int i = 0; i < nodes.getLength(); i++) {
155                 Element driver = (Element) nodes.item(i);
156
157                 if (!"".equals(driver.getAttribute("type"))) {
158                         addDriver(new JDBCDriver(
159                                 driver.getAttribute("className"),
160                                 driver.getAttribute("jarFileName"),
161                                 driver.getAttribute("type"),
162                                 driver.getAttribute("name"),
163                                 driver.getAttribute("version")));
164                 }
165                 
166         }
167         }
168
169         /**
170          * @param driver
171          */
172         public void addDriver(JDBCDriver driver) {
173                 if (!this.drivers.contains(driver)) {
174                         this.drivers.add(driver);
175                         this.support.firePropertyChange("drivers", null, driver);
176                 }
177         }
178
179         /**
180          * @param root
181          * @return
182          */
183         private Vector importBookmarks(Element root) {
184                 Vector newBookmarks = new Vector();
185         NodeList nodes = root.getElementsByTagName("bookmark"); //$NON-NLS-1$
186         for (int i = 0; i < nodes.getLength(); i++) {
187             Bookmark bookmark = new Bookmark();
188             Element column = (Element) nodes.item(i);
189             
190             String name = MetaDataXMLInterface.getElementText(column,"name"); //$NON-NLS-1$
191             if (name == null) break;
192             bookmark.setName(name);
193             
194             MetaDataXMLInterface.getElementText(column,"name"); //$NON-NLS-1$
195             bookmark.setUsername(MetaDataXMLInterface.getElementText(column,"username")); //$NON-NLS-1$
196             bookmark.setPassword(MetaDataXMLInterface.getElementText(column,"password")); //$NON-NLS-1$
197             bookmark.setPromptForPassword(Boolean.TRUE.toString().equalsIgnoreCase(
198                 MetaDataXMLInterface.getElementText(column,"prompt"))); //$NON-NLS-1$
199             bookmark.setConnect(MetaDataXMLInterface.getElementText(column,"connect")); //$NON-NLS-1$
200                         bookmark.setAutoCommit(Boolean.TRUE.toString().equalsIgnoreCase(
201                                 MetaDataXMLInterface.getElementText(column,"autoCommit", "True"))); //$NON-NLS-1$
202                         bookmark.setAutoCommitPreference(MetaDataXMLInterface.getElementText(column,"autoCommitPreference", IQuantumConstants.autoCommitTrue)); //$NON-NLS-1$
203                         
204                         backwardCompatibility(bookmark, column);
205
206             String driverClassName = MetaDataXMLInterface.getElementText(column,"driver"); //$NON-NLS-1$
207             String driverFile = MetaDataXMLInterface.getElementText(column,"driverLocation"); //$NON-NLS-1$
208             String type = MetaDataXMLInterface.getElementText(column,"type"); //$NON-NLS-1$
209             
210             bookmark.setJDBCDriver(new JDBCDriver(driverClassName, driverFile, type));
211             
212             NodeList children = column.getElementsByTagName("Other_Schemas");
213             if (children.getLength() > 0) {
214                 importSchemas((Element) children.item(0), bookmark);
215             }
216             
217             
218             System.out.println(bookmark.toString());
219             if (!bookmark.isEmpty()) {
220                 newBookmarks.addElement(bookmark);
221             }
222             importQuickList(bookmark, column);
223             importQueryList(bookmark, column);
224         }
225                 return newBookmarks;
226         }
227
228         /**
229          * Earlier versions of the xml file expected one schema element under the 
230          * bookmark element.  This method sees if it exists.
231          * 
232          * @param bookmark
233          * @param element
234          */
235         private void backwardCompatibility(Bookmark bookmark, Element element) {
236                 NodeList children = element.getChildNodes();
237                 for (int i = 0, length = children.getLength(); i < length; i++) {
238                         Node node = children.item(i);
239                         if (node.getNodeType() == Node.ELEMENT_NODE && 
240                                         "schema".equals(((Element) node).getTagName())) {
241                                 String schema = MetaDataXMLInterface.extractText(element,"");
242                                 if (schema != null && schema.trim().length() > 0) {
243                                         bookmark.addSchema(schema);
244                                 }
245                         }
246                 }
247         }
248
249         private void importSchemas(Element otherSchemas, Bookmark bookmark) {
250         List list = new ArrayList();
251         NodeList children = otherSchemas.getChildNodes();
252         for (int i = 0, length = children.getLength(); i < length; i++) {
253                 Node node = children.item(i);
254                 if (node.getNodeType() == Node.ELEMENT_NODE
255                                 && "schema".equalsIgnoreCase(((Element) node).getTagName())) {
256                     list.add(new Schema(
257                                 MetaDataXMLInterface.extractText((Element) node, "")));
258                 }
259         }
260         
261         String schemaRule = otherSchemas.getAttribute("schemaRule");
262         if ("useAll".equals(schemaRule)) {
263                 bookmark.setSchemaRule(Bookmark.SCHEMA_RULE_USE_ALL);
264         } else if ("useDefault".equals(schemaRule)) {
265                 bookmark.setSchemaRule(Bookmark.SCHEMA_RULE_USE_DEFAULT);
266         } else {
267                 bookmark.setSchemaRule(Bookmark.SCHEMA_RULE_USE_SELECTED);
268         }
269         bookmark.setSchemaSelections((Schema[]) list.toArray(new Schema[list.size()]));
270     }
271
272         private void importQuickList(Bookmark bookmark, Element bookmarkElement) {
273         NodeList quickList = bookmarkElement.getElementsByTagName("quickList");
274         for (int j = 0,
275             length = (quickList == null) ? 0 : quickList.getLength();
276             j < length;
277             j++) {
278             
279             Element element = (Element) quickList.item(j);
280             NodeList childNodes = element.getChildNodes();
281                 
282             for (int k = 0,
283                 length2 = (childNodes == null) ? 0 : childNodes.getLength();
284                 k < length2;
285                 k++) {
286                 if (Node.ELEMENT_NODE == childNodes.item(k).getNodeType()) {
287                     Element entity = (Element) childNodes.item(k);
288                     bookmark.addQuickListEntry(entity.getTagName(), 
289                         entity.getAttribute("schema"), entity.getAttribute("name"));
290                 }
291             }
292         }
293     }
294     
295     private void importQueryList(Bookmark bookmark, Element bookmarkElement) {
296         NodeList queryList = bookmarkElement.getElementsByTagName("queryList");
297         for (int i = 0,
298             length = (queryList == null) ? 0 : queryList.getLength();
299             i < length;
300             i++) {
301             
302             Element element = (Element) queryList.item(i);
303             NodeList childNodes = element.getElementsByTagName("query");
304                 
305             for (int k = 0,
306                 length2 = (childNodes == null) ? 0 : childNodes.getLength();
307                 k < length2;
308                 k++) {
309
310                 Element query = (Element) childNodes.item(k);
311                 bookmark.addQuery(MetaDataXMLInterface.getElementText(query,"queryString"));
312         
313             }
314         }
315     }
316     
317     public void addBookmark(Bookmark b) {
318         this.changed = true;
319         if (!bookmarks.contains(b)) {
320             Bookmark[] original = getBookmarks();
321             bookmarks.add(b);
322             this.support.firePropertyChange("bookmarks", original, getBookmarks());
323         }
324     }
325     public void removeBookmark(Bookmark b) {
326         this.changed = true;
327         if (bookmarks.contains(b)) {
328             Bookmark[] original = getBookmarks();
329             bookmarks.remove(b);
330             this.support.firePropertyChange("bookmarks", original, getBookmarks());
331         }
332     }
333     
334     public Bookmark[] getBookmarks() {
335         return (Bookmark[]) this.bookmarks.toArray(new Bookmark[this.bookmarks.size()]);
336     }
337     
338     public JDBCDriver[] getJDBCDrivers() {
339         return (JDBCDriver[]) this.drivers.toArray(new JDBCDriver[this.drivers.size()]);
340     }
341     /**
342      * @return
343      */
344     public boolean isAnythingChanged() {
345         boolean anythingChanged = this.changed;
346         for (Iterator i = this.bookmarks.iterator(); !anythingChanged && i.hasNext();) {
347             Bookmark bookmark = (Bookmark) i.next();
348             anythingChanged |= bookmark.isChanged();
349         }
350         return anythingChanged;
351     }
352     
353     public boolean isChanged() {
354         return this.changed;
355     }
356
357     /**
358      * @param b
359      */
360     public void setChanged(boolean changed) {
361         this.changed = changed;
362     }
363
364     /**
365      * @param listener
366      */
367     public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
368         this.support.addPropertyChangeListener(listener);
369     }
370
371     /**
372      * @param listener
373      */
374     public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
375         this.support.removePropertyChangeListener(listener);
376     }
377
378         /**
379          * @param string
380          * @return
381          */
382         public String getCopyName(String name) {
383                 
384                 String copyName = Messages.getString("BookmarkView.CopyOf") + name;
385                 int i = 1;
386                 while (find(copyName) != null)
387                 {
388                         copyName = Messages.getString("BookmarkView.CopyOf") + name + "(" + String.valueOf(i) + ")";
389                         i++;
390                 }
391                 
392                 return copyName;
393         }
394
395         /**
396          * @param driver
397          * @param driverFile
398          */
399         public JDBCDriver findDriver(String driverClassName, String driverFile, String type) {
400                 JDBCDriver temp = new JDBCDriver(driverClassName, driverFile, type);
401                 return findDriver(temp);
402         }
403
404         /**
405          * @param temp
406          * @return
407          */
408         public JDBCDriver findDriver(JDBCDriver temp) {
409                 JDBCDriver result = null;
410                 for (Iterator i = this.drivers.iterator(); result == null && i.hasNext();) {
411                         JDBCDriver driver = (JDBCDriver) i.next();
412                         if (temp.equals(driver)) {
413                                 result = driver;
414                         }
415                 }
416                 if (result == null) {
417                         addDriver(temp);
418                         result = temp;
419                 }
420                 return result;
421         }
422
423
424 }