/[schmitzm]/branches/2.3.x/src/skrueger/geotools/io/DbServerSettings.java
ViewVC logotype

Diff of /branches/2.3.x/src/skrueger/geotools/io/DbServerSettings.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1163 by alfonx, Sun Oct 24 22:55:43 2010 UTC revision 1164 by alfonx, Mon Oct 25 07:25:26 2010 UTC
# Line 32  import schmitzm.swing.SelectionInputOpti Line 32  import schmitzm.swing.SelectionInputOpti
32   * connection into a {@link String} with {@link #toPropertiesString()} and   * connection into a {@link String} with {@link #toPropertiesString()} and
33   * re-import the String with {@link #parsePropertiesString(String)}.   * re-import the String with {@link #parsePropertiesString(String)}.
34   */   */
35  public class DbServerSettings extends HashMap<Object, Object> {  public class DbServerSettings extends ServerSettings<Object, Object> {
36    
37            public enum DbType {
38                    postgis("postgresql");
39    
40                    private final String protocolString;
41    
42                    DbType(String protocolString) {
43                            this.protocolString = protocolString;
44                    }
45    
46                    public String getProtocolString() {
47                            return protocolString;
48                    };
49            }
50    
51          /**          /**
52           * params.put(JDBCDataStoreFactory.DBTYPE.key, "postgis");           * params.put(JDBCDataStoreFactory.DBTYPE.key, "postgis");
# Line 52  public class DbServerSettings extends Ha Line 66  public class DbServerSettings extends Ha
66          public enum Key {          public enum Key {
67          }          }
68    
69          public enum DbType {          /**
70                  postgis("postgresql");           * Opens a GUI that asks the use define a DB connection.
71             *
72             * @param dbServer
73             *            <code>null</code> to create a new instance, or an instance to
74             *            edit.
75             * @return <code>null</code> if the user cancelled the creation of a new
76             *         {@link DbServerSettings}, otherwise the edited instance.
77             */
78            public static DbServerSettings createOrEdit(Component owner,
79                            DbServerSettings dbServer) {
80                    boolean newCreated = false;
81    
82                  private final String protocolString;                  if (dbServer == null) {
83                            newCreated = true;
84                            dbServer = new DbServerSettings(DbType.postgis);
85                    }
86    
87                  DbType(String protocolString) {                  Combo<DbType> dpTypeInput = new SelectionInputOption.Combo<DbType>(
88                          this.protocolString = protocolString;                                  "Database type", true, DbType.values(), ArrayUtils.indexOf(
89                                                    DbType.values(), dbServer.getDbType()), DbType.values());
90    
91                    Text hostInput = new ManualInputOption.Text("Hostname", true,
92                                    dbServer.getHost());
93    
94                    Integer portInput = new ManualInputOption.Integer("Port", true,
95                                    dbServer.getPort());
96    
97                    Text databaseInput = new ManualInputOption.Text("Database", true,
98                                    dbServer.getDatabase());
99    
100                    Text schemaInput = new ManualInputOption.Text("Schema", true,
101                                    dbServer.getSchema());
102    
103                    Text userInput = new ManualInputOption.Text("Username", true,
104                                    dbServer.getUsername());
105    
106                    PasswordViewable passwdInput = new ManualInputOption.PasswordViewable(
107                                    "Password", true, dbServer.getPassword());
108    
109                    BooleanInputOption exposePkInput = new BooleanInputOption(
110                                    "Expose primary keys", dbServer.getExposePrimaryKey());
111    
112                    Object[] input = MultipleOptionPane.showMultipleInputDialog(owner,
113                                    "DB Connection paramters", dpTypeInput, hostInput, portInput,
114                                    databaseInput, schemaInput, userInput, passwdInput,
115                                    exposePkInput);
116    
117                    if (input == null) {
118                            if (newCreated)
119                                    return null;
120                            else
121                                    return dbServer;
122                    } else {
123                            dbServer.setDbType((DbType) input[0]);
124                            dbServer.setHost((String) input[1]);
125                            dbServer.setPort((java.lang.Integer) input[2]);
126                            dbServer.setDatabase((String) input[3]);
127                            dbServer.setSchema((String) input[4]);
128                            dbServer.setUsername((String) input[5]);
129                            dbServer.setPassword(String.valueOf((char[]) input[6]));
130                            dbServer.setExposePrimaryKey((Boolean) input[7]);
131                  }                  }
132    
133                  public String getProtocolString() {                  return dbServer;
134                          return protocolString;  
                 };  
135          }          }
136    
137          public void setPort(java.lang.Integer port) {          /**
138                  put(JDBCDataStoreFactory.PORT.key, port);           * @return transforms the settings to a String that can be stored in a
139             *         .properties line. @see #parsePropertiesString
140             * @throws MalformedURLException
141             */
142            public static DbServerSettings parsePropertiesString(String propString)
143                            throws MalformedURLException {
144    
145                    if (propString == null || propString.isEmpty())
146                            throw new IllegalArgumentException("parameter to parse was empty");
147                    try {
148    
149                            String[] split = propString.split(Pattern.quote(DELIMITER));
150    
151                            int i = 0;
152                            DbServerSettings dbServer = new DbServerSettings(
153                                            DbType.valueOf(split[i++]));
154    
155                            dbServer.setTitle(split[i++]);
156                            dbServer.setHost(split[i++]);
157                            dbServer.setPort(java.lang.Integer.valueOf(split[i++]));
158                            dbServer.setUsername(StringUtils.stripToNull(split[i++]));
159                            dbServer.setPassword(split[i++]);
160                            dbServer.setDatabase(split[i++]);
161                            dbServer.setExposePrimaryKey(Boolean.valueOf(split[i++]));
162                            dbServer.setSchema(stringOrNull(split[i++]));
163    
164                            return dbServer;
165                    } catch (Exception e) {
166                            Log.warn("couldn't parse " + propString, e);
167                            return null;
168                    }
169          }          }
170    
171          public java.lang.Integer getPort() {          private String[] cachedTypeNames = null;
172                  java.lang.Integer port = (java.lang.Integer) get(JDBCDataStoreFactory.PORT.key);  
173                  if (port == null)          public DbServerSettings() {
                         return (java.lang.Integer) JDBCDataStoreFactory.PORT.sample;  
                 return port;  
174          }          }
175    
176          public void setSchema(String schema) {          public DbServerSettings(DbType dbType) {
177                  put(JDBCDataStoreFactory.SCHEMA.key, schema);                  setDbType(dbType);
178                    setHost("localhost");
179                    setSchema("public");
180          }          }
181    
182          public String getSchema() {          public String[] getCachedTypeNames() {
183                  return (String) get(JDBCDataStoreFactory.SCHEMA.key);                  return cachedTypeNames;
184          }          }
185    
186          public void setExposePrimaryKey(Boolean exportPk) {          public String getDatabase() {
187                  put(JDBCDataStoreFactory.EXPOSE_PK.key, exportPk);                  return (String) get(JDBCDataStoreFactory.DATABASE.key);
188            }
189    
190            public DbType getDbType() {
191                    String dbt = (String) get(PostgisNGDataStoreFactory.DBTYPE.key);
192                    if (dbt != null)
193                            return DbType.valueOf(dbt);
194                    return null;
195          }          }
196    
197          public Boolean getExposePrimaryKey() {          public Boolean getExposePrimaryKey() {
# Line 96  public class DbServerSettings extends Ha Line 201  public class DbServerSettings extends Ha
201                  return expk;                  return expk;
202          }          }
203    
         public void setHost(String host) {  
                 put(JDBCDataStoreFactory.HOST.key, host);  
         }  
   
204          public String getHost() {          public String getHost() {
205                  return (String) get(JDBCDataStoreFactory.HOST.key);                  return (String) get(JDBCDataStoreFactory.HOST.key);
206          }          }
207    
         public void setPassword(String password) {  
                 put(JDBCDataStoreFactory.PASSWD.key, password);  
         }  
   
208          public String getPassword() {          public String getPassword() {
209                  return (String) get(JDBCDataStoreFactory.PASSWD.key);                  return (String) get(JDBCDataStoreFactory.PASSWD.key);
210          }          }
211    
212          public void setUsername(String username) {          public java.lang.Integer getPort() {
213                  put(JDBCDataStoreFactory.USER.key, username);                  java.lang.Integer port = (java.lang.Integer) get(JDBCDataStoreFactory.PORT.key);
214                    if (port == null)
215                            return (java.lang.Integer) JDBCDataStoreFactory.PORT.sample;
216                    return port;
217            }
218    
219            public String getSchema() {
220                    return (String) get(JDBCDataStoreFactory.SCHEMA.key);
221          }          }
222    
223          public String getUsername() {          public String getUsername() {
224                  return (String) get(JDBCDataStoreFactory.USER.key);                  return (String) get(JDBCDataStoreFactory.USER.key);
225          }          }
226    
227          public void setDatabase(String db) {          /**
228                  put(JDBCDataStoreFactory.DATABASE.key, db);           * @return <code>true</code>, if all parameters look OK, without actually
229          }           *         opening any connection.
230             */
231            public boolean isWellDefined() {
232                    if (getDbType() == null)
233                            return false;
234    
235          public String getDatabase() {                  if (getHost() == null)
236                  return (String) get(JDBCDataStoreFactory.DATABASE.key);                          return false;
237    
238                    if (getUsername() == null)
239                            return false;
240    
241                    if (getPassword() == null)
242                            return false;
243    
244                    if (getPort() == null)
245                            return false;
246    
247                    if (getSchema() == null)
248                            return false;
249    
250                    return true;
251          }          }
252    
253          /**          public void setCachedTypeNames(String[] cachedTypeNames) {
254           * Character used to separate the parameters when serializing settings to a                  this.cachedTypeNames = cachedTypeNames;
255           * String          }
          */  
         private static final String DELIMITER = "|";  
256    
257          public DbServerSettings(DbType dbType) {          public void setDatabase(String db) {
258                  setDbType(dbType);                  put(JDBCDataStoreFactory.DATABASE.key, db);
                 setHost("localhost");  
                 setSchema("public");  
259          }          }
260    
261          public void setDbType(DbType dbType) {          public void setDbType(DbType dbType) {
# Line 157  public class DbServerSettings extends Ha Line 275  public class DbServerSettings extends Ha
275    
276          }          }
277    
278          public DbType getDbType() {          public void setExposePrimaryKey(Boolean exportPk) {
279                  String dbt = (String) get(PostgisNGDataStoreFactory.DBTYPE.key);                  put(JDBCDataStoreFactory.EXPOSE_PK.key, exportPk);
                 if (dbt != null)  
                         return DbType.valueOf(dbt);  
                 return null;  
280          }          }
281    
282          public DbServerSettings() {          public void setHost(String host) {
283                    put(JDBCDataStoreFactory.HOST.key, host);
284          }          }
285    
286          /**          public void setPassword(String password) {
287           * @return <code>true</code>, if all parameters look OK, without actually                  put(JDBCDataStoreFactory.PASSWD.key, password);
288           *         opening any connection.          }
          */  
         public boolean isWellDefined() {  
                 if (getDbType() == null)  
                         return false;  
   
                 if (getHost() == null)  
                         return false;  
   
                 if (getUsername() == null)  
                         return false;  
   
                 if (getPassword() == null)  
                         return false;  
289    
290                  if (getPort() == null)          public void setPort(java.lang.Integer port) {
291                          return false;                  put(JDBCDataStoreFactory.PORT.key, port);
292            }
293    
294                  if (getSchema() == null)          public void setSchema(String schema) {
295                          return false;                  put(JDBCDataStoreFactory.SCHEMA.key, schema);
296            }
297    
298                  return true;          public void setUsername(String username) {
299                    put(JDBCDataStoreFactory.USER.key, username);
300          }          }
301    
302          /**          /**
# Line 204  public class DbServerSettings extends Ha Line 310  public class DbServerSettings extends Ha
310                  serialized.append(getDbType().toString());                  serialized.append(getDbType().toString());
311                  serialized.append(DELIMITER);                  serialized.append(DELIMITER);
312    
313                    serialized.append(getTitle());
314                    serialized.append(DELIMITER);
315                    
316                  serialized.append(getHost());                  serialized.append(getHost());
317                  serialized.append(DELIMITER);                  serialized.append(DELIMITER);
318    
# Line 223  public class DbServerSettings extends Ha Line 332  public class DbServerSettings extends Ha
332                  serialized.append(DELIMITER);                  serialized.append(DELIMITER);
333    
334                  serialized.append(getSchema().toString());                  serialized.append(getSchema().toString());
335                  serialized.append(DELIMITER);                  // serialized.append(DELIMITER);
336    
337                  return serialized.toString();                  return serialized.toString();
338          }          }
339    
         /**  
          * @return transforms the settings to a String that can be stored in a  
          *         .properties line. @see #parsePropertiesString  
          * @throws MalformedURLException  
          */  
         public static DbServerSettings parsePropertiesString(String propString)  
                         throws MalformedURLException {  
   
                 if (propString == null || propString.isEmpty())  
                         throw new IllegalArgumentException("parameter to parse was empty");  
                 try {  
   
                         String[] split = propString.split(Pattern.quote(DELIMITER));  
   
                         DbType dbt = DbType.valueOf(split[0]);  
   
                         DbServerSettings dbServer = new DbServerSettings(dbt);  
   
                         dbServer.setHost(split[1]);  
                         dbServer.setPort(java.lang.Integer.valueOf(split[2]));  
                         dbServer.setUsername(StringUtils.stripToNull(split[3]));  
                         dbServer.setPassword(split[4].equals("null") ? null : split[4]);  
                         dbServer.setDatabase(split[5]);  
                         dbServer.setExposePrimaryKey(Boolean.valueOf(split[6]));  
                         dbServer.setSchema(split[7]);  
   
                         return dbServer;  
                 } catch (Exception e) {  
                         Log.warn("couldn't parse " + propString);  
                         return null;  
                 }  
         }  
   
340          @Override          @Override
341          public String toString() {          public String toString() {
342    
# Line 280  public class DbServerSettings extends Ha Line 356  public class DbServerSettings extends Ha
356    
357                  return s.toString();                  return s.toString();
358          }          }
   
         public void setCachedTypeNames(String[] cachedTypeNames) {  
                 this.cachedTypeNames = cachedTypeNames;  
         }  
   
         public String[] getCachedTypeNames() {  
                 return cachedTypeNames;  
         }  
   
         private String[] cachedTypeNames = null;  
   
         public String getTitle() {  
                 // TODO  
                 return "" + getDbType();  
         }  
   
         /**  
          * Opens a GUI that asks the use define a DB connection.  
          *  
          * @param dbServer  
          *            <code>null</code> to create a new instance, or an instance to  
          *            edit.  
          * @return <code>null</code> if the user cancelled the creation of a new  
          *         {@link DbServerSettings}, otherwise the edited instance.  
          */  
         public static DbServerSettings createOrEdit(Component owner,  
                         DbServerSettings dbServer) {  
                 boolean newCreated = false;  
   
                 if (dbServer == null) {  
                         newCreated = true;  
                         dbServer = new DbServerSettings(DbType.postgis);  
                 }  
   
                 Combo<DbType> dpTypeInput = new SelectionInputOption.Combo<DbType>(  
                                 "Database type", true, DbType.values(), ArrayUtils.indexOf(  
                                                 DbType.values(), dbServer.getDbType()), DbType.values());  
   
                 Text hostInput = new ManualInputOption.Text("Hostname", true,  
                                 dbServer.getHost());  
   
                 Integer portInput = new ManualInputOption.Integer("Port", true,  
                                 dbServer.getPort());  
   
                 Text databaseInput = new ManualInputOption.Text("Database", true,  
                                 dbServer.getDatabase());  
   
                 Text schemaInput = new ManualInputOption.Text("Schema", true,  
                                 dbServer.getSchema());  
   
                 Text userInput = new ManualInputOption.Text("Username", true,  
                                 dbServer.getUsername());  
   
                 PasswordViewable passwdInput = new ManualInputOption.PasswordViewable(  
                                 "Password", true, dbServer.getPassword());  
   
                 BooleanInputOption exposePkInput = new BooleanInputOption(  
                                 "Expose primary keys", dbServer.getExposePrimaryKey());  
   
                 Object[] input = MultipleOptionPane.showMultipleInputDialog(owner,  
                                 "DB Connection paramters", dpTypeInput, hostInput, portInput,  
                                 databaseInput, schemaInput, userInput, passwdInput,  
                                 exposePkInput);  
   
                 if (input == null) {  
                         if (newCreated)  
                                 return null;  
                         else  
                                 return dbServer;  
                 } else {  
                         dbServer.setDbType((DbType) input[0]);  
                         dbServer.setHost((String) input[1]);  
                         dbServer.setPort((java.lang.Integer) input[2]);  
                         dbServer.setDatabase((String) input[3]);  
                         dbServer.setSchema((String) input[4]);  
                         dbServer.setUsername((String) input[5]);  
                         dbServer.setPassword(String.valueOf((char[]) input[6]));  
                         dbServer.setExposePrimaryKey((Boolean) input[7]);  
                 }  
   
                 return dbServer;  
   
         }  
359  }  }

Legend:
Removed from v.1163  
changed lines
  Added in v.1164

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26