/[schmitzm]/branches/2.4.x/src/skrueger/geotools/io/GtDbServerSettings.java
ViewVC logotype

Contents of /branches/2.4.x/src/skrueger/geotools/io/GtDbServerSettings.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1161 - (show annotations)
Sun Oct 24 16:44:18 2010 UTC (14 years, 4 months ago) by alfonx
Original Path: trunk/src/skrueger/geotools/io/DbServerSettings.java
File MIME type: text/plain
File size: 9720 byte(s)
* AtlasSTyler-Feature: Improved the GUI
* AtlasSTyler-Feature: Import Wizards for SHape, PostGIS and WFS
1 package skrueger.geotools.io;
2
3 import java.awt.Component;
4 import java.net.MalformedURLException;
5 import java.util.HashMap;
6 import java.util.regex.Pattern;
7
8 import org.apache.commons.lang.ArrayUtils;
9 import org.geotools.data.postgis.PostgisNGDataStoreFactory;
10 import org.geotools.jdbc.JDBCDataStore;
11 import org.geotools.jdbc.JDBCDataStoreFactory;
12
13 import schmitzm.swing.BooleanInputOption;
14 import schmitzm.swing.ManualInputOption;
15 import schmitzm.swing.ManualInputOption.Integer;
16 import schmitzm.swing.ManualInputOption.PasswordViewable;
17 import schmitzm.swing.ManualInputOption.Text;
18 import schmitzm.swing.MultipleOptionPane;
19 import schmitzm.swing.SelectionInputOption;
20 import schmitzm.swing.SelectionInputOption.Combo;
21
22 /**
23 * This class describes all settings needed to connect to a WFS server. This
24 * class extends a {@link HashMap} and contains two groups of keys:<br/>
25 * The first group of keys, are all keys provided by Geotools to create a WFS
26 * datastore, like: {@link JDBCDataStore#SCHEMA} .<br/>
27 * The second group are additional keys defined in the enum
28 * {@link DbServerSettings.Key}.<br/>
29 * This class can serialize all important parameters needed to define the
30 * connection into a {@link String} with {@link #toPropertiesString()} and
31 * re-import the String with {@link #parsePropertiesString(String)}.
32 */
33 public class DbServerSettings extends HashMap<Object, Object> {
34
35 /**
36 * params.put(JDBCDataStoreFactory.DBTYPE.key, "postgis");
37 * params.put(JDBCDataStoreFactory.HOST.key, host); // the name or ip
38 * params.put(JDBCDataStoreFactory.PORT.key, port); // the port that
39 *
40 * params.put(JDBCDataStoreFactory.DATABASE.key, database); // the
41 *
42 * // name params.put(JDBCDataStoreFactory.USER.key, username); // the user
43 * to params.put(JDBCDataStoreFactory.PASSWD.key, password); // the
44 *
45 * params.put(JDBCDataStoreFactory.SCHEMA, schema);
46 *
47 * params.put(JDBCDataStoreFactory.EXPOSE_PK.key, true); *
48 */
49
50 public enum Key {
51 }
52
53 public enum DbType {
54 postgis("postgresql");
55
56 private final String protocolString;
57
58 DbType(String protocolString) {
59 this.protocolString = protocolString;
60 }
61
62 public String getProtocolString() {
63 return protocolString;
64 };
65 }
66
67 public void setPort(java.lang.Integer port) {
68 put(JDBCDataStoreFactory.PORT.key, port);
69 }
70
71 public java.lang.Integer getPort() {
72 java.lang.Integer port = (java.lang.Integer) get(JDBCDataStoreFactory.PORT.key);
73 if (port == null)
74 return (java.lang.Integer) JDBCDataStoreFactory.PORT.sample;
75 return port;
76 }
77
78 public void setSchema(String schema) {
79 put(JDBCDataStoreFactory.SCHEMA.key, schema);
80 }
81
82 public String getSchema() {
83 return (String) get(JDBCDataStoreFactory.SCHEMA.key);
84 }
85
86 public void setExposePrimaryKey(Boolean exportPk) {
87 put(JDBCDataStoreFactory.EXPOSE_PK.key, exportPk);
88 }
89
90 public Boolean getExposePrimaryKey() {
91 Boolean expk = (Boolean) get(JDBCDataStoreFactory.EXPOSE_PK.key);
92 if (expk == null)
93 return (Boolean) JDBCDataStoreFactory.EXPOSE_PK.sample;
94 return expk;
95 }
96
97 public void setHost(String host) {
98 put(JDBCDataStoreFactory.HOST.key, host);
99 }
100
101 public String getHost() {
102 return (String) get(JDBCDataStoreFactory.HOST.key);
103 }
104
105 public void setPassword(String password) {
106 put(JDBCDataStoreFactory.PASSWD.key, password);
107 }
108
109 public String getPassword() {
110 return (String) get(JDBCDataStoreFactory.PASSWD.key);
111 }
112
113 public void setUsername(String username) {
114 put(JDBCDataStoreFactory.USER.key, username);
115 }
116
117 public String getUsername() {
118 return (String) get(JDBCDataStoreFactory.USER.key);
119 }
120
121 public void setDatabase(String db) {
122 put(JDBCDataStoreFactory.DATABASE.key, db);
123 }
124
125 public String getDatabase() {
126 return (String) get(JDBCDataStoreFactory.DATABASE.key);
127 }
128
129 /**
130 * Character used to separate the parameters when serializing settings to a
131 * String
132 */
133 private static final String DELIMITER = "|";
134
135 public DbServerSettings(DbType dbType) {
136 setDbType(dbType);
137 setHost("localhost");
138 setSchema("public");
139 }
140
141 public void setDbType(DbType dbType) {
142 put(PostgisNGDataStoreFactory.DBTYPE.key, dbType.toString());
143
144 if (dbType == DbType.postgis) {
145 if (getPort() == null) {
146 // For a PostGIS DBMS automatically set the port to 5432
147 setPort(5432);
148 }
149 if (getUsername() == null) {
150 // For a PostGIS DBMS automatically set the user to postgres
151 setUsername("postgres");
152 }
153
154 }
155
156 }
157
158 public DbType getDbType() {
159 String dbt = (String) get(PostgisNGDataStoreFactory.DBTYPE.key);
160 if (dbt != null)
161 return DbType.valueOf(dbt);
162 return null;
163 }
164
165 public DbServerSettings() {
166 }
167
168 /**
169 * @return <code>true</code>, if all parameters look OK, without actually
170 * opening any connection.
171 */
172 public boolean isWellDefined() {
173 if (getDbType() == null)
174 return false;
175
176 if (getHost() == null)
177 return false;
178
179 if (getUsername() == null)
180 return false;
181
182 if (getPassword() == null)
183 return false;
184
185 if (getPort() == null)
186 return false;
187
188 if (getSchema() == null)
189 return false;
190
191 return true;
192 }
193
194 /**
195 * @return transforms the settings to a String that can be stored in a
196 * .properties line. @see #parsePropertiesString
197 */
198 public String toPropertiesString() {
199
200 StringBuffer serialized = new StringBuffer(100);
201
202 serialized.append(getDbType().toString());
203 serialized.append(DELIMITER);
204
205 serialized.append(getHost().toString());
206 serialized.append(DELIMITER);
207
208 serialized.append(getPort().toString());
209 serialized.append(DELIMITER);
210
211 serialized.append(getUsername().toString());
212 serialized.append(DELIMITER);
213
214 serialized.append(getPassword().toString());
215 serialized.append(DELIMITER);
216
217 serialized.append(getDatabase().toString());
218 serialized.append(DELIMITER);
219
220 serialized.append(getExposePrimaryKey().toString());
221 serialized.append(DELIMITER);
222
223 serialized.append(getSchema().toString());
224 serialized.append(DELIMITER);
225
226 return serialized.toString();
227 }
228
229 /**
230 * @return transforms the settings to a String that can be stored in a
231 * .properties line. @see #parsePropertiesString
232 * @throws MalformedURLException
233 */
234 public static DbServerSettings parsePropertiesString(String propString)
235 throws MalformedURLException {
236
237 if (propString == null || propString.isEmpty())
238 throw new IllegalArgumentException("parameter to parse was empty");
239
240 String[] split = propString.split(Pattern.quote(DELIMITER));
241
242 // BaseUrl
243 try {
244 DbType dbt = DbType.valueOf(split[0]);
245
246 DbServerSettings dbServer = new DbServerSettings(dbt);
247
248 dbServer.setHost(split[1]);
249 dbServer.setPort(java.lang.Integer.valueOf(split[2]));
250 dbServer.setUsername(split[3]);
251 dbServer.setPassword(split[4]);
252 dbServer.setDatabase(split[5]);
253 dbServer.setExposePrimaryKey(Boolean.valueOf(split[6]));
254 dbServer.setSchema(split[7]);
255
256 return dbServer;
257 } catch (Exception e) {
258 throw new IllegalArgumentException(e);
259 }
260
261 }
262
263 @Override
264 public String toString() {
265
266 StringBuffer s = new StringBuffer();
267
268 if (getDbType() != null) {
269 s.append(getDbType().getProtocolString() + "://");
270 }
271
272 s.append(getHost());
273
274 if (getPort() != 5432) {
275 s.append(":" + getPort());
276 }
277
278 s.append("/" + getDatabase());
279
280 return s.toString();
281 }
282
283 public void setCachedTypeNames(String[] cachedTypeNames) {
284 this.cachedTypeNames = cachedTypeNames;
285 }
286
287 public String[] getCachedTypeNames() {
288 return cachedTypeNames;
289 }
290
291 private String[] cachedTypeNames = null;
292
293 public String getTitle() {
294 // TODO
295 return "" + getDbType();
296 }
297
298 /**
299 * Opens a GUI that asks the use define a DB connection.
300 *
301 * @param dbServer
302 * <code>null</code> to create a new instance, or an instance to
303 * edit.
304 * @return <code>null</code> if the user cancelled the creation of a new
305 * {@link DbServerSettings}, otherwise the edited instance.
306 */
307 public static DbServerSettings createOrEdit(Component owner,
308 DbServerSettings dbServer) {
309 boolean newCreated = false;
310
311 if (dbServer == null) {
312 newCreated = true;
313 dbServer = new DbServerSettings(DbType.postgis);
314 }
315
316 Combo<DbType> dpTypeInput = new SelectionInputOption.Combo<DbType>(
317 "Database type", true, DbType.values(), ArrayUtils.indexOf(
318 DbType.values(), dbServer.getDbType()), DbType.values());
319
320 Text hostInput = new ManualInputOption.Text("Hostname", true,
321 dbServer.getHost());
322
323 Integer portInput = new ManualInputOption.Integer("Port", true,
324 dbServer.getPort());
325
326 Text databaseInput = new ManualInputOption.Text("Database", true,
327 dbServer.getDatabase());
328
329 Text schemaInput = new ManualInputOption.Text("Schema", true,
330 dbServer.getSchema());
331
332 Text userInput = new ManualInputOption.Text("Username", true,
333 dbServer.getUsername());
334
335 PasswordViewable passwdInput = new ManualInputOption.PasswordViewable(
336 "Passwort", true, dbServer.getPassword());
337
338 BooleanInputOption exposePkInput = new BooleanInputOption(
339 "Expose primary keys", dbServer.getExposePrimaryKey());
340
341 Object[] input = MultipleOptionPane.showMultipleInputDialog(owner,
342 "DB Connection paramters", dpTypeInput, hostInput, portInput,
343 databaseInput, schemaInput, userInput, passwdInput,
344 exposePkInput);
345
346 if (input == null) {
347 if (newCreated)
348 return null;
349 else
350 return dbServer;
351 } else {
352 dbServer.setDbType((DbType) input[0]);
353 dbServer.setHost((String) input[1]);
354 dbServer.setPort((java.lang.Integer) input[2]);
355 dbServer.setDatabase((String) input[3]);
356 dbServer.setSchema((String) input[4]);
357 dbServer.setUsername((String) input[5]);
358 dbServer.setPassword(String.valueOf((char[]) input[6]));
359 dbServer.setExposePrimaryKey((Boolean) input[7]);
360 }
361
362 return dbServer;
363 }
364 }

Properties

Name Value
svn:eol-style native
svn:keywords Id URL
svn:mime-type text/plain

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26