001    package com.sptci.rwt;
002    
003    import java.io.Serializable;
004    
005    import java.util.Collection;
006    import java.util.Collections;
007    import java.util.Map;
008    import java.util.TreeMap;
009    
010    /**
011     * A simple bean used to represent different types of database engines
012     * that the application can connect to.
013     *
014     * <p>&copy; Copyright 2007 <a href='http://sptci.com/' target='_new'>Sans Pareil Technologies, Inc.</a></p>
015     * @author Rakesh Vidyadharan 2007-09-24
016     * @version $Id: DatabaseType.java 4123 2008-05-25 21:49:01Z rakesh $
017     */
018    public class DatabaseType implements Serializable
019    {
020      /**
021       * A map that stores saved {@link ConnectionData} identified by a
022       * unique name.
023       */
024      private Map<String,ConnectionData> savedConnections =
025        new TreeMap<String,ConnectionData>();
026    
027      /**
028       * The name of the database engine represented by this instance.
029       */
030      private String name;
031    
032      /**
033       * The encoded JDBC url pattern to use to build a connection url to the
034       * database.
035       */
036      private String urlPattern;
037    
038      /**
039       * The fully qualified name of the JDBC driver class.
040       */
041      private String driver;
042    
043      /**
044       * Default constructor.  Not publicly instantiable.
045       */
046      protected DatabaseType() {}
047    
048      /**
049       * Check to see if a saved connection exists with the specified name.
050       *
051       * @param name The name to check for uniqueness.
052       */
053      public boolean hasName( final String name )
054      {
055        return savedConnections.containsKey( name );
056      }
057    
058      /**
059       * Return a collection of all the names used to store connections.
060       *
061       * @return The collection of unique names for saved connections.
062       */
063      public Collection<String> getNames()
064      {
065        return Collections.unmodifiableCollection( savedConnections.keySet() );
066      }
067    
068      /**
069       * Return the connection data uniquely identified by the specified name.
070       *
071       * @param name The unique name to use to fetch the connection data.
072       * @return The matching connection data object or <code>null</code> if
073       *   no such mapping exists.
074       */
075      public ConnectionData getConnectionData( final String name )
076      {
077        return savedConnections.get( name );
078      }
079    
080      /**
081       * Return a collection of all the saved connection data objects.
082       *
083       * @return The map of all saved name-connection combinations.
084       */
085      public Map<String,ConnectionData> getConnectionData()
086      {
087        return Collections.unmodifiableMap( savedConnections );
088      }
089    
090      /**
091       * Add the specified connection data to the {@link #savedConnections} map.
092       * If such a mapping already exists, the existing mapping is updated.
093       *
094       * @see #hasName
095       * @param name The unique name to use to identify this connection.
096       * @param data The connection data to be saved.
097       */
098      void add( final String name, final ConnectionData data )
099      {
100        savedConnections.put( name, data );
101      }
102    
103      /**
104       * Remove the saved connection identified by the specified name.
105       *
106       * @param name The unique name to use to identify the connection to remove.
107       */
108      void delete( final String name )
109      {
110        savedConnections.remove( name );
111      }
112      
113      /**
114       * Returns {@link #name}.
115       *
116       * @return The value/reference of/to name.
117       */
118      public String getName()
119      {
120        return name;
121      }
122      
123      /**
124       * Set {@link #name}.
125       *
126       * @param name The value to set.
127       */
128      protected void setName( final String name )
129      {
130        this.name = name;
131      }
132      
133      /**
134       * Returns {@link #urlPattern}.
135       *
136       * @return The value/reference of/to urlPattern.
137       */
138      public String getUrlPattern()
139      {
140        return urlPattern;
141      }
142      
143      /**
144       * Set {@link #urlPattern}.
145       *
146       * @param urlPattern The value to set.
147       */
148      protected void setUrlPattern( final String urlPattern )
149      {
150        this.urlPattern = urlPattern;
151      }
152      
153      /**
154       * Returns {@link #driver}.
155       *
156       * @return The value/reference of/to driver.
157       */
158      public String getDriver()
159      {
160        return driver;
161      }
162      
163      /**
164       * Set {@link #driver}.
165       *
166       * @param driver The value to set.
167       */
168      protected void setDriver( final String driver )
169      {
170        this.driver = driver;
171      }
172    }