Made the help plugin 3.x compatible
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / model / Bookmark.java
1 package com.quantum.model;
2
3 import java.beans.PropertyChangeListener;
4 import java.beans.PropertyChangeSupport;
5 import java.sql.Connection;
6 import java.sql.SQLException;
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.HashSet;
10 import java.util.Hashtable;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Set;
14
15 import com.quantum.IQuantumConstants;
16 import com.quantum.QuantumPlugin;
17 import com.quantum.adapters.AdapterFactory;
18 import com.quantum.adapters.DatabaseAdapter;
19 import com.quantum.sql.ConnectionEstablisher;
20 import com.quantum.sql.MultiSQLServer;
21
22 import org.eclipse.jface.preference.IPreferenceStore;
23
24 /**
25  * Class Bookmark holds the "static" information of a bookmark, that is the data that
26  * is saved and loaded from the external file and describes a bookmark. This info will
27  * be filled up by the end user.
28  * 
29  * @author root
30  */
31 public class Bookmark {
32     
33     private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
34         private String name = ""; //$NON-NLS-1$
35         private String username = ""; //$NON-NLS-1$
36     private String password = ""; //$NON-NLS-1$
37     private String connect = ""; //$NON-NLS-1$
38     private String driver = ""; //$NON-NLS-1$
39     private String type = ""; //$NON-NLS-1$
40     private String driverFile = ""; //$NON-NLS-1$
41     
42     /**
43      * A quick list is a list of favourite tables that a person might want to view
44      * without having to look at the entire list of tables.
45      */
46     private Map quickList = new Hashtable();
47         private Set schemas = new HashSet();   
48         private Connection connection = null;
49     private ConnectionEstablisher connectionEstablisher;
50     private boolean changed = true;
51     private List queries = Collections.synchronizedList(new ArrayList());
52     private boolean promptForPassword = false;
53     private boolean autoCommit = true;
54     private String autoCommitPreference = IQuantumConstants.autoCommitTrue;
55         
56         public Bookmark() {
57         this(MultiSQLServer.getInstance());
58         }
59     
60     public Bookmark(ConnectionEstablisher connectionEstablisher) {
61         this.connectionEstablisher = connectionEstablisher;
62     }
63
64         public Bookmark(Bookmark data) {
65                 this();
66                 setName(data.getName());
67                 setUsername(data.getUsername());
68                 setPassword(data.getPassword());
69                 setConnect(data.getConnect());
70                 setDriver(data.getDriver());
71                 setType(data.getType());
72                 setDriverFile(data.getDriverFile());
73         setPromptForPassword(data.getPromptForPassword());
74         setAutoCommit(data.isAutoCommit());
75         setAutoCommitPreference(data.getAutoCommitPreference());
76         
77         this.schemas.addAll(data.schemas);
78         this.quickList = new Hashtable(data.quickList);
79         }
80
81         /**
82          * Returns the JDBC URL.
83          * @return String
84          */
85         public String getConnect() {
86                 return connect;
87         }
88
89         /**
90          * Returns the driver.
91          * @return String
92          */
93         public String getDriver() {
94                 return driver;
95         }
96
97         /**
98          * Returns the driverFile.
99          * @return String
100          */
101         public String getDriverFile() {
102                 return driverFile;
103         }
104
105         /**
106          * Returns the password.
107          * @return String
108          */
109         public String getPassword() {
110                 return password;
111         }
112
113         /**
114          * Returns the username.
115          * @return String
116          */
117         public String getUsername() {
118                 return username;
119         }
120
121         /**
122          * Sets the connect.
123          * @param connect The connect to set
124          */
125         public void setConnect(String connect) {
126                 if (connect == null) {
127                         connect = ""; //$NON-NLS-1$
128                 }
129                 this.connect = connect;
130         }
131
132         /**
133          * Sets the driver.
134          * @param driver The driver to set
135          */
136         public void setDriver(String driver) {
137                 if (driver == null) {
138                         driver = ""; //$NON-NLS-1$
139                 }
140                 this.driver = driver;
141         }
142
143         /**
144          * Sets the driverFile.
145          * @param driverFile The driverFile to set
146          */
147         public void setDriverFile(String driverFile) {
148                 if (driverFile == null) {
149                         driverFile = ""; //$NON-NLS-1$
150                 }
151                 this.driverFile = driverFile;
152         }
153
154         /**
155          * Sets the password.
156          * @param password The password to set
157          */
158         public void setPassword(String password) {
159                 if (password == null) {
160                         password = ""; //$NON-NLS-1$
161                 }
162                 this.password = password;
163         }
164
165         /**
166          * Sets the username.
167          * @param username The username to set
168          */
169         public void setUsername(String username) {
170                 if (username == null) {
171                         username = ""; //$NON-NLS-1$
172                 }
173                 this.username = username;
174         }
175
176         /**
177          * Returns the name.
178          * @return String
179          */
180         public String getName() {
181                 return name;
182         }
183
184         /**
185          * Sets the name.
186          * @param name The name to set
187          */
188         public void setName(String name) {
189                 if (name == null) {
190                         name = ""; //$NON-NLS-1$
191                 }
192         if (!name.equals(this.name)) {
193          
194             String oldName = this.name;
195             this.name = name;
196             this.propertyChangeSupport.firePropertyChange("name", oldName, this.name);
197             this.changed = true;
198         }
199         }
200
201         public boolean isEmpty() {
202                 if (name.equals("") && //$NON-NLS-1$
203                 username.equals("") && //$NON-NLS-1$
204                 password.equals("") && //$NON-NLS-1$
205                 connect.equals("") && //$NON-NLS-1$
206                 driver.equals("") && //$NON-NLS-1$
207                 type.equals("") && //$NON-NLS-1$
208                 driverFile.equals("")) { //$NON-NLS-1$
209                         return true;
210                 }
211                 return false;
212         }
213         public String toString() {
214                 StringBuffer buffer = new StringBuffer();
215                 buffer.append("["); //$NON-NLS-1$
216                 buffer.append("name="); //$NON-NLS-1$
217                 buffer.append(name);
218                 buffer.append(", "); //$NON-NLS-1$
219                 buffer.append("username="); //$NON-NLS-1$
220                 buffer.append(username);
221                 buffer.append(", "); //$NON-NLS-1$
222                 buffer.append("password=****"); //$NON-NLS-1$
223                 buffer.append(", "); //$NON-NLS-1$
224                 buffer.append("connect="); //$NON-NLS-1$
225                 buffer.append(connect);
226                 buffer.append(", "); //$NON-NLS-1$
227                 buffer.append("driver="); //$NON-NLS-1$
228                 buffer.append(driver);
229                 buffer.append(", "); //$NON-NLS-1$
230                 buffer.append("type="); //$NON-NLS-1$
231                 buffer.append(type);
232                 buffer.append(", "); //$NON-NLS-1$
233                 buffer.append("driverFile="); //$NON-NLS-1$
234                 buffer.append(driverFile);
235                 buffer.append("]"); //$NON-NLS-1$
236                 return buffer.toString();
237         }
238         
239         public String getType() {
240                 return type;
241         }
242
243         public void setType(String type) {
244                 this.type = type;
245         }
246
247     public Connection connect(PasswordFinder passwordFinder) throws ConnectionException {
248         boolean isConnected = isConnected();
249         if (this.connection == null) {
250             this.connection = this.connectionEstablisher.connect(this, passwordFinder);
251         }
252         
253         if (isConnected() != isConnected) {
254             this.propertyChangeSupport.firePropertyChange(
255                 "connected", isConnected, isConnected());
256         }
257         return this.connection;
258     }
259
260     /**
261          * Returns the connection object. 
262          * @return the Connection object associated with the current JDBC source.
263          * 
264          */
265     public Connection getConnection() throws NotConnectedException {
266         if (this.connection == null) {
267             throw new NotConnectedException();
268         }
269         return this.connection;
270     }
271
272     /**
273          * @return true if the BookmarkNode is connected to a JDBC source
274          */
275     public boolean isConnected() {
276         return (connection != null);
277     }
278
279     /**
280          * Sets the connection member. From that moment on the BookmarkNode is "connected" (open)
281          * @param connection : a valid connection to a JDBC source
282          */
283     public void setConnection(Connection connection) {
284         this.connection = connection;
285     }
286
287     public void disconnect() throws ConnectionException {
288         boolean isConnected = isConnected();
289         try {
290             if (this.connection != null) {
291                 this.connectionEstablisher.disconnect(this.connection);
292             }
293         } finally {
294             this.connection = null;
295             if (isConnected() != isConnected) {
296                 this.propertyChangeSupport.firePropertyChange(
297                     "connected", isConnected, isConnected());
298             }
299         }
300     }
301     public void addSchema(String schema) {
302         if (schema != null && schema.trim().length() > 0) {
303             addSchema(new Schema(schema));
304         }
305     }
306
307     public void addSchema(Schema qualifier) {
308         if (qualifier != null) {
309             this.schemas.add(qualifier);
310             this.changed = true;
311             this.propertyChangeSupport.firePropertyChange("schemas", null, null);
312         }
313     }
314
315     public void setSchemas(Schema[] schemas) {
316         this.schemas.clear();
317         for (int i = 0, length = (schemas == null) ? 0 : schemas.length;
318             i < length;
319             i++) {
320             this.schemas.add(schemas[i]);
321             
322         }
323         this.changed = true;
324         this.propertyChangeSupport.firePropertyChange("schemas", null, null);
325     }
326
327     /**
328      * @return a list of all the schemas that have been set up.
329      */
330     public Schema[] getSchemas() {
331         Set set = new HashSet();
332         set.addAll(this.schemas);
333         if (set.isEmpty()) {
334             set.add(new Schema(getAdapter().getDefaultSchema(this.username), 
335                 this.username, true));
336         }
337         List list = new ArrayList(set);
338         Collections.sort(list);
339         return (Schema[]) list.toArray(new Schema[list.size()]);
340     }
341
342         /**
343          * @see java.lang.Object#equals(java.lang.Object)
344          */
345         public boolean equals(Object obj) {
346                 if (!(obj instanceof Bookmark)) return false;
347                 String name = ((Bookmark)obj).getName();
348                 if (name.equals(this.getName())) return true;
349                 return false;
350         }
351
352     /**
353      * @param listener
354      */
355     public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
356         this.propertyChangeSupport.addPropertyChangeListener(listener);
357     }
358
359     /**
360      * @param listener
361      */
362     public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
363         this.propertyChangeSupport.removePropertyChangeListener(listener);
364     }
365
366     public void addQuickListEntry(String type, String schemaName, String name) {
367         Entity entity = EntityFactory.getInstance().create(this, schemaName, name, type);
368         this.quickList.put(entity.getCondQualifiedName(), entity);
369         this.propertyChangeSupport.firePropertyChange("quickList", null, null);
370         this.changed = true;
371     }
372     
373     public void addQuickListEntry(Entity entity) {
374         addQuickListEntry(entity.getType(), entity.getSchema(), entity.getName());
375     }
376     
377     public void removeQuickListEntry(Entity entity) {
378         if (entity != null  && this.quickList.containsKey(entity.getCondQualifiedName())) {
379             this.quickList.remove(entity.getCondQualifiedName());
380             this.propertyChangeSupport.firePropertyChange("quickList", null, null);
381             this.changed = true;
382         }
383     }
384     
385     public Entity[] getQuickListEntries() {
386         return (Entity[]) this.quickList.values().toArray(new Entity[this.quickList.size()]);
387     }
388     
389     public boolean hasQuickList() {
390         return !this.quickList.isEmpty();
391     }
392     /**
393      * @return
394      */
395     public boolean isChanged() {
396         return changed;
397     }
398
399     /**
400      * @param b
401      */
402     public void setChanged(boolean b) {
403         changed = b;
404     }
405     
406     public Database getDatabase() throws NotConnectedException {
407         if (!isConnected()) {
408             throw new NotConnectedException();
409         }
410         return new Database(this);
411     }
412     
413     public DatabaseAdapter getAdapter() {
414         return AdapterFactory.getInstance().getAdapter(getType());
415     }
416
417     public Entity[] getEntitiesForSchema(Schema schema, String type) throws SQLException {
418         try {
419             Entity[] entities = getDatabase().getEntities(this, schema, type);
420             return entities;
421         } catch (NotConnectedException e) {
422             return new Entity[0];
423         }
424     }
425     
426     public Entity getEntity(Schema schema, String name) throws SQLException {
427         Entity result = null;
428         if (schema != null && name != null) {
429             Entity[] entities = getEntitiesForSchema(schema, null);
430             for (int i = 0, length = (entities == null) ? 0 : entities.length;
431                 result == null && i < length;
432                 i++) {
433                 if (schema.equals(entities[i].getSchema()) &&
434                     name.equals(entities[i].getName())) {
435                     result = entities[i];
436                 }
437             }
438         }
439         return result;
440     }
441     
442     public boolean isInQuickList(Entity entity) {
443         return this.quickList.containsKey(entity.getCondQualifiedName());
444     }
445     
446     /**
447      * 
448      * @param queryString
449      */
450     public void addQuery(String queryString) {
451         if (this.queries.contains(queryString)) {
452             this.queries.remove(queryString);
453         }
454         this.queries.add(queryString);
455         
456         int size = getQueryHistorySize();
457         
458         while (this.queries.size() > size) {
459             this.queries.remove(0);
460         }
461         this.propertyChangeSupport.firePropertyChange("queries", null, null);
462         this.changed = true;
463     }
464     
465     public String[] getQueries() {
466         return (String[]) this.queries.toArray(new String[this.queries.size()]);
467     }
468     
469     private int getQueryHistorySize() {
470         IPreferenceStore store =
471             QuantumPlugin.getDefault().getPreferenceStore();
472         return store.getInt(getClass().getName() + ".queryHistorySize"); //$NON-NLS-1$
473     }
474     /**
475      * @return
476      */
477     public boolean getPromptForPassword() {
478         return promptForPassword;
479     }
480
481     /**
482      * @param b
483      */
484     public void setPromptForPassword(boolean b) {
485         promptForPassword = b;
486     }
487
488         /**
489          * @return
490          */
491         public boolean isAutoCommit() {
492                 return autoCommit;
493         }
494
495         /**
496          * @return
497          */
498         public String getAutoCommitPreference() {
499                 return autoCommitPreference;
500         }
501
502         /**
503          * @param b
504          */
505         public void setAutoCommit(boolean b) {
506                 autoCommit = b;
507         }
508
509         /**
510          * @param string
511          */
512         public void setAutoCommitPreference(String string) {
513                 autoCommitPreference = string;
514         }
515
516         // Returns true or false indicating whether this bookmark must be set to AutoCommit on connection or not 
517         public boolean getDefaultAutoCommit(){
518                 if (autoCommitPreference.equals(IQuantumConstants.autoCommitTrue)) return true;
519                 else if (autoCommitPreference.equals(IQuantumConstants.autoCommitFalse)) return false;
520                 else if (autoCommitPreference.equals(IQuantumConstants.autoCommitSaved)) return autoCommit;
521                 
522                 return true;    
523         }
524
525 }