184f790d92c90a1eea3a182704c490d9992d4a18
[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             bookmark.setDriver(props.getProperty(i + ".driver")); //$NON-NLS-1$
79             String schema = props.getProperty(i + ".schema"); //$NON-NLS-1$
80             if (schema != null) {
81                 bookmark.addSchema(schema);
82             }
83             String type = props.getProperty(i + ".type"); //$NON-NLS-1$
84             if (type != null) {
85                 bookmark.setType(type);
86             } else {
87                 bookmark.setType(""); //$NON-NLS-1$
88             }
89             String driverFile = props.getProperty(i + ".driverLocation"); //$NON-NLS-1$
90             if (driverFile != null) {
91                 bookmark.setDriverFile(driverFile);
92             } else {
93                 bookmark.setDriverFile(""); //$NON-NLS-1$
94             }
95             System.out.println(bookmark.toString());
96             if (!bookmark.isEmpty()) {
97                 newBookmarks.add(bookmark);
98             }
99             i++;
100             this.drivers.add(new JDBCDriver(bookmark.getDriver(), driverFile));
101         }
102         if (overwrite) {
103             this.bookmarks = newBookmarks;
104         } else {
105             this.bookmarks.addAll(newBookmarks);
106         }
107     }
108     /**
109      * Finds a Bookmark with the specified name.
110      * 
111      * @param name
112      * @return the bookmark with the specified name, or null if no bookmark can be found
113      */
114     public Bookmark find(String name){
115         Bookmark result = null;
116         if (name != null) {
117             for (Iterator i = this.bookmarks.iterator(); result == null && i.hasNext(); ) {
118                 Bookmark temp = (Bookmark) i.next();
119                 if (name.equals(temp.getName())) {
120                     result = temp;
121                 }
122             }
123         }
124         return result;
125     }
126
127     /**
128      * Exports a Bookmark data to an XMLDocument Element
129      * The complementary function is importXML()
130      * @param root  The Element to fill up with the bookmark info
131      */
132     public void exportXML(Element root) {
133         System.out.println("Bookmarks: Saving to Element"); //$NON-NLS-1$
134         Element bookmarkRoot = MetaDataXMLInterface.createElementText(root,"bookmarks", ""); //$NON-NLS-1$ //$NON-NLS-2$
135         for (Iterator i = this.drivers.iterator(); i.hasNext(); ) {
136             JDBCDriver driver = (JDBCDriver) i.next();
137             ModelToXMLConverter.getInstance().convert(bookmarkRoot, driver);
138         }
139         for (int i = 0; i < bookmarks.size(); i++) {
140             Bookmark b = (Bookmark) bookmarks.get(i);
141             ModelToXMLConverter.getInstance().convert(bookmarkRoot, b);
142         }
143     }
144
145     /**
146      * Imports a Bookmark data from an XMLDocument Element
147      * The complementary function is exportXML()
148      * @param root  The Element from which to load
149      */
150     public void importXML(Element root) {
151         this.changed = true;
152         System.out.println("Bookmarks: Loading from Element"); //$NON-NLS-1$
153         importDrivers(root);
154         Vector newBookmarks = importBookmarks(root);
155         this.bookmarks.addAll(newBookmarks);
156         this.support.firePropertyChange("bookmarks", null, null);
157     }
158
159     /**
160          * @param root
161          * @return
162          */
163         private void importDrivers(Element root) {
164         NodeList nodes = root.getElementsByTagName("jdbcDriver"); //$NON-NLS-1$
165         for (int i = 0; i < nodes.getLength(); i++) {
166                 Element driver = (Element) nodes.item(i);
167                 addDriver(new JDBCDriver(
168                         driver.getAttribute("className"),
169                         driver.getAttribute("jarFileName"),
170                         driver.getAttribute("name"),
171                         driver.getAttribute("version")));
172         }
173         }
174
175         /**
176          * @param driver
177          */
178         public void addDriver(JDBCDriver driver) {
179                 if (!this.drivers.contains(driver)) {
180                         this.drivers.add(driver);
181                         this.support.firePropertyChange("drivers", null, driver);
182                 }
183         }
184
185         /**
186          * @param root
187          * @return
188          */
189         private Vector importBookmarks(Element root) {
190                 Vector newBookmarks = new Vector();
191         NodeList nodes = root.getElementsByTagName("bookmark"); //$NON-NLS-1$
192         for (int i = 0; i < nodes.getLength(); i++) {
193             Bookmark bookmark = new Bookmark();
194             Element column = (Element) nodes.item(i);
195             
196             String name = MetaDataXMLInterface.getElementText(column,"name"); //$NON-NLS-1$
197             if (name == null) break;
198             bookmark.setName(name);
199             
200             MetaDataXMLInterface.getElementText(column,"name"); //$NON-NLS-1$
201             bookmark.setUsername(MetaDataXMLInterface.getElementText(column,"username")); //$NON-NLS-1$
202             bookmark.setPassword(MetaDataXMLInterface.getElementText(column,"password")); //$NON-NLS-1$
203             bookmark.setPromptForPassword(Boolean.TRUE.toString().equalsIgnoreCase(
204                 MetaDataXMLInterface.getElementText(column,"prompt"))); //$NON-NLS-1$
205             bookmark.setConnect(MetaDataXMLInterface.getElementText(column,"connect")); //$NON-NLS-1$
206                         bookmark.setAutoCommit(Boolean.TRUE.toString().equalsIgnoreCase(
207                                 MetaDataXMLInterface.getElementText(column,"autoCommit", "True"))); //$NON-NLS-1$
208                         bookmark.setAutoCommitPreference(MetaDataXMLInterface.getElementText(column,"autoCommitPreference", IQuantumConstants.autoCommitTrue)); //$NON-NLS-1$
209             bookmark.addSchema(MetaDataXMLInterface.getElementText(column,"schema")); //$NON-NLS-1$
210             bookmark.setType(MetaDataXMLInterface.getElementText(column,"type")); //$NON-NLS-1$
211             NodeList children = column.getElementsByTagName(Messages.getString("ExportXMLAction.OtherSchemas"));
212
213             String driverClassName = MetaDataXMLInterface.getElementText(column,"driver"); //$NON-NLS-1$
214             String driverFile = MetaDataXMLInterface.getElementText(column,"driverLocation"); //$NON-NLS-1$
215             
216             bookmark.setJDBCDriver(new JDBCDriver(driverClassName, driverFile));
217             
218             if (children.getLength() > 0) {
219                 importSchemas((Element) children.item(0), bookmark);
220             }
221             System.out.println(bookmark.toString());
222             if (!bookmark.isEmpty()) {
223                 newBookmarks.addElement(bookmark);
224             }
225             importQuickList(bookmark, column);
226             importQueryList(bookmark, column);
227         }
228                 return newBookmarks;
229         }
230
231         private void importSchemas(Element otherSchemas, Bookmark bookmark) {
232         Vector vector = MetaDataXMLInterface.getVectorText(otherSchemas, Messages.getString("ExportXMLAction.SchemaName"));
233         List list = new ArrayList();
234         for (Iterator i = vector.iterator(); i.hasNext();) {
235             String schemaName = (String) i.next();
236             list.add(new Schema(schemaName));
237         }
238         bookmark.setSchemas((Schema[]) list.toArray(new Schema[list.size()]));
239     }
240
241     private void importQuickList(Bookmark bookmark, Element bookmarkElement) {
242         NodeList quickList = bookmarkElement.getElementsByTagName("quickList");
243         for (int j = 0,
244             length = (quickList == null) ? 0 : quickList.getLength();
245             j < length;
246             j++) {
247             
248             Element element = (Element) quickList.item(j);
249             NodeList childNodes = element.getChildNodes();
250                 
251             for (int k = 0,
252                 length2 = (childNodes == null) ? 0 : childNodes.getLength();
253                 k < length2;
254                 k++) {
255                 if (Node.ELEMENT_NODE == childNodes.item(k).getNodeType()) {
256                     Element entity = (Element) childNodes.item(k);
257                     bookmark.addQuickListEntry(entity.getTagName(), 
258                         entity.getAttribute("schema"), entity.getAttribute("name"));
259                 }
260             }
261         }
262     }
263     
264     private void importQueryList(Bookmark bookmark, Element bookmarkElement) {
265         NodeList queryList = bookmarkElement.getElementsByTagName("queryList");
266         for (int i = 0,
267             length = (queryList == null) ? 0 : queryList.getLength();
268             i < length;
269             i++) {
270             
271             Element element = (Element) queryList.item(i);
272             NodeList childNodes = element.getElementsByTagName("query");
273                 
274             for (int k = 0,
275                 length2 = (childNodes == null) ? 0 : childNodes.getLength();
276                 k < length2;
277                 k++) {
278
279                 Element query = (Element) childNodes.item(k);
280                 bookmark.addQuery(MetaDataXMLInterface.getElementText(query,"queryString"));
281         
282             }
283         }
284     }
285     
286     public void addBookmark(Bookmark b) {
287         this.changed = true;
288         if (!bookmarks.contains(b)) {
289             Bookmark[] original = getBookmarks();
290             bookmarks.add(b);
291             this.support.firePropertyChange("bookmarks", original, getBookmarks());
292         }
293     }
294     public void removeBookmark(Bookmark b) {
295         this.changed = true;
296         if (bookmarks.contains(b)) {
297             Bookmark[] original = getBookmarks();
298             bookmarks.remove(b);
299             this.support.firePropertyChange("bookmarks", original, getBookmarks());
300         }
301     }
302     
303     public Bookmark[] getBookmarks() {
304         return (Bookmark[]) this.bookmarks.toArray(new Bookmark[this.bookmarks.size()]);
305     }
306     
307     public JDBCDriver[] getJDBCDrivers() {
308         return (JDBCDriver[]) this.drivers.toArray(new JDBCDriver[this.drivers.size()]);
309     }
310     /**
311      * @return
312      */
313     public boolean isAnythingChanged() {
314         boolean anythingChanged = this.changed;
315         for (Iterator i = this.bookmarks.iterator(); !anythingChanged && i.hasNext();) {
316             Bookmark bookmark = (Bookmark) i.next();
317             anythingChanged |= bookmark.isChanged();
318         }
319         return anythingChanged;
320     }
321     
322     public boolean isChanged() {
323         return this.changed;
324     }
325
326     /**
327      * @param b
328      */
329     public void setChanged(boolean changed) {
330         this.changed = changed;
331     }
332
333     /**
334      * @param listener
335      */
336     public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
337         this.support.addPropertyChangeListener(listener);
338     }
339
340     /**
341      * @param listener
342      */
343     public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
344         this.support.removePropertyChangeListener(listener);
345     }
346
347         /**
348          * @param string
349          * @return
350          */
351         public String getCopyName(String name) {
352                 
353                 String copyName = Messages.getString("BookmarkView.CopyOf") + name;
354                 int i = 1;
355                 while (find(copyName) != null)
356                 {
357                         copyName = Messages.getString("BookmarkView.CopyOf") + name + "(" + String.valueOf(i) + ")";
358                         i++;
359                 }
360                 
361                 return copyName;
362         }
363
364         /**
365          * @param driver
366          * @param driverFile
367          */
368         public JDBCDriver findDriver(String driverClassName, String driverFile) {
369                 JDBCDriver temp = new JDBCDriver(driverClassName, driverFile);
370                 return findDriver(temp);
371         }
372
373         /**
374          * @param temp
375          * @return
376          */
377         public JDBCDriver findDriver(JDBCDriver temp) {
378                 JDBCDriver result = null;
379                 for (Iterator i = this.drivers.iterator(); result == null && i.hasNext();) {
380                         JDBCDriver driver = (JDBCDriver) i.next();
381                         if (temp.equals(driver)) {
382                                 result = driver;
383                         }
384                 }
385                 if (result == null) {
386                         addDriver(temp);
387                         result = temp;
388                 }
389                 return result;
390         }
391
392
393 }