1 package com.quantum.model;
3 import java.beans.PropertyChangeListener;
4 import java.beans.PropertyChangeSupport;
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;
15 import java.util.Vector;
17 import org.w3c.dom.Element;
18 import org.w3c.dom.Node;
19 import org.w3c.dom.NodeList;
21 import com.quantum.IQuantumConstants;
22 import com.quantum.Messages;
23 import com.quantum.model.xml.ModelToXMLConverter;
24 import com.quantum.sql.metadata.MetaDataXMLInterface;
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.
33 public class BookmarkCollection {
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());
41 private BookmarkCollection() {
47 public static BookmarkCollection getInstance() {
48 return BookmarkCollection.instance;
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.
57 public void load(File file) throws IOException {
58 Properties props = new Properties();
59 FileInputStream in = new FileInputStream(file);
62 fromProperties(true, props);
65 private void fromProperties(boolean overwrite, Properties props) {
66 List newBookmarks = new Vector();
69 Bookmark bookmark = new Bookmark();
70 String name = props.getProperty(i + ".name"); //$NON-NLS-1$
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$
80 bookmark.addSchema(schema);
82 if (!bookmark.isEmpty()) {
83 newBookmarks.add(bookmark);
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));
92 this.bookmarks = newBookmarks;
94 this.bookmarks.addAll(newBookmarks);
98 * Finds a Bookmark with the specified name.
101 * @return the bookmark with the specified name, or null if no bookmark can be found
103 public Bookmark find(String name){
104 Bookmark result = null;
106 for (Iterator i = this.bookmarks.iterator(); result == null && i.hasNext(); ) {
107 Bookmark temp = (Bookmark) i.next();
108 if (name.equals(temp.getName())) {
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
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);
128 for (int i = 0; i < bookmarks.size(); i++) {
129 Bookmark b = (Bookmark) bookmarks.get(i);
130 ModelToXMLConverter.getInstance().convert(bookmarkRoot, b);
135 * Imports a Bookmark data from an XMLDocument Element
136 * The complementary function is exportXML()
137 * @param root The Element from which to load
139 public void importXML(Element root) {
141 System.out.println("Bookmarks: Loading from Element"); //$NON-NLS-1$
143 Vector newBookmarks = importBookmarks(root);
144 this.bookmarks.addAll(newBookmarks);
145 this.support.firePropertyChange("bookmarks", null, null);
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);
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")));
172 public void addDriver(JDBCDriver driver) {
173 if (!this.drivers.contains(driver)) {
174 this.drivers.add(driver);
175 this.support.firePropertyChange("drivers", null, driver);
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);
190 String name = MetaDataXMLInterface.getElementText(column,"name"); //$NON-NLS-1$
191 if (name == null) break;
192 bookmark.setName(name);
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$
204 backwardCompatibility(bookmark, column);
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$
210 bookmark.setJDBCDriver(new JDBCDriver(driverClassName, driverFile, type));
212 NodeList children = column.getElementsByTagName("Other_Schemas");
213 if (children.getLength() > 0) {
214 importSchemas((Element) children.item(0), bookmark);
218 System.out.println(bookmark.toString());
219 if (!bookmark.isEmpty()) {
220 newBookmarks.addElement(bookmark);
222 importQuickList(bookmark, column);
223 importQueryList(bookmark, column);
229 * Earlier versions of the xml file expected one schema element under the
230 * bookmark element. This method sees if it exists.
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);
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())) {
257 MetaDataXMLInterface.extractText((Element) node, "")));
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);
267 bookmark.setSchemaRule(Bookmark.SCHEMA_RULE_USE_SELECTED);
269 bookmark.setSchemaSelections((Schema[]) list.toArray(new Schema[list.size()]));
272 private void importQuickList(Bookmark bookmark, Element bookmarkElement) {
273 NodeList quickList = bookmarkElement.getElementsByTagName("quickList");
275 length = (quickList == null) ? 0 : quickList.getLength();
279 Element element = (Element) quickList.item(j);
280 NodeList childNodes = element.getChildNodes();
283 length2 = (childNodes == null) ? 0 : childNodes.getLength();
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"));
295 private void importQueryList(Bookmark bookmark, Element bookmarkElement) {
296 NodeList queryList = bookmarkElement.getElementsByTagName("queryList");
298 length = (queryList == null) ? 0 : queryList.getLength();
302 Element element = (Element) queryList.item(i);
303 NodeList childNodes = element.getElementsByTagName("query");
306 length2 = (childNodes == null) ? 0 : childNodes.getLength();
310 Element query = (Element) childNodes.item(k);
311 bookmark.addQuery(MetaDataXMLInterface.getElementText(query,"queryString"));
317 public void addBookmark(Bookmark b) {
319 if (!bookmarks.contains(b)) {
320 Bookmark[] original = getBookmarks();
322 this.support.firePropertyChange("bookmarks", original, getBookmarks());
325 public void removeBookmark(Bookmark b) {
327 if (bookmarks.contains(b)) {
328 Bookmark[] original = getBookmarks();
330 this.support.firePropertyChange("bookmarks", original, getBookmarks());
334 public Bookmark[] getBookmarks() {
335 return (Bookmark[]) this.bookmarks.toArray(new Bookmark[this.bookmarks.size()]);
338 public JDBCDriver[] getJDBCDrivers() {
339 return (JDBCDriver[]) this.drivers.toArray(new JDBCDriver[this.drivers.size()]);
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();
350 return anythingChanged;
353 public boolean isChanged() {
360 public void setChanged(boolean changed) {
361 this.changed = changed;
367 public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
368 this.support.addPropertyChangeListener(listener);
374 public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
375 this.support.removePropertyChangeListener(listener);
382 public String getCopyName(String name) {
384 String copyName = Messages.getString("BookmarkView.CopyOf") + name;
386 while (find(copyName) != null)
388 copyName = Messages.getString("BookmarkView.CopyOf") + name + "(" + String.valueOf(i) + ")";
399 public JDBCDriver findDriver(String driverClassName, String driverFile, String type) {
400 JDBCDriver temp = new JDBCDriver(driverClassName, driverFile, type);
401 return findDriver(temp);
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)) {
416 if (result == null) {