svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc / JDBCStoreParametersBase.java @ 47784
History | View | Annotate | Download (9.99 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
|
25 |
package org.gvsig.fmap.dal.store.jdbc; |
26 |
|
27 |
import java.text.MessageFormat; |
28 |
import org.apache.commons.lang3.StringUtils; |
29 |
import org.gvsig.fmap.dal.DataStoreParameters; |
30 |
import org.gvsig.fmap.dal.store.db.AbstractDBStoreParameters; |
31 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCLibrary; |
32 |
|
33 |
/**
|
34 |
* Parameters class for JDBC generic provider
|
35 |
*
|
36 |
*/
|
37 |
public class JDBCStoreParametersBase extends AbstractDBStoreParameters |
38 |
implements JDBCStoreParameters {
|
39 |
|
40 |
|
41 |
public JDBCStoreParametersBase() {
|
42 |
super(
|
43 |
JDBCLibrary.STOREPARAMETERS_NAME, |
44 |
JDBCLibrary.NAME |
45 |
); |
46 |
} |
47 |
|
48 |
protected JDBCStoreParametersBase(String parametersDefinitionName) { |
49 |
super(
|
50 |
parametersDefinitionName, |
51 |
JDBCLibrary.NAME |
52 |
); |
53 |
} |
54 |
|
55 |
public JDBCStoreParametersBase(String parametersDefinitionName, String providerName) { |
56 |
super(parametersDefinitionName,providerName);
|
57 |
} |
58 |
|
59 |
@Override
|
60 |
public boolean isValid() { |
61 |
return this.getHost() != null; |
62 |
} |
63 |
|
64 |
@Override
|
65 |
public String getHost() { |
66 |
return (String) this.getDynValue(HOST_PARAMTER_NAME); |
67 |
} |
68 |
|
69 |
@Override
|
70 |
public Integer getPort() { |
71 |
return (Integer) this.getDynValue(PORT_PARAMTER_NAME); |
72 |
} |
73 |
|
74 |
@Override
|
75 |
public int getBatchSize() { |
76 |
try {
|
77 |
return (int) this.getDynValue(BATCH_SIZE_PARAMETER_NAME); |
78 |
} catch(Exception ex) { |
79 |
return DEFAULT_BATCH_SIZE;
|
80 |
} |
81 |
} |
82 |
|
83 |
@Override
|
84 |
public String getDBName() { |
85 |
return (String) this.getDynValue(DBNAME_PARAMTER_NAME); |
86 |
} |
87 |
|
88 |
@Override
|
89 |
public String getUser() { |
90 |
return (String) this.getDynValue(USER_PARAMTER_NAME); |
91 |
} |
92 |
|
93 |
@Override
|
94 |
public String getPassword() { |
95 |
return (String) this.getDynValue(PASSWORD_PARAMTER_NAME); |
96 |
} |
97 |
|
98 |
@Override
|
99 |
public void setHost(String host) { |
100 |
this.setDynValue(HOST_PARAMTER_NAME, host);
|
101 |
} |
102 |
|
103 |
@Override
|
104 |
public void setPort(int port) { |
105 |
this.setDynValue(PORT_PARAMTER_NAME, new Integer(port)); |
106 |
} |
107 |
|
108 |
@Override
|
109 |
public void setPort(Integer port) { |
110 |
this.setDynValue(PORT_PARAMTER_NAME, port);
|
111 |
} |
112 |
|
113 |
@Override
|
114 |
public void setDBName(String dbName) { |
115 |
this.setDynValue(DBNAME_PARAMTER_NAME, dbName);
|
116 |
} |
117 |
|
118 |
@Override
|
119 |
public void setUser(String user) { |
120 |
this.setDynValue(USER_PARAMTER_NAME, user);
|
121 |
} |
122 |
|
123 |
@Override
|
124 |
public void setPassword(String password) { |
125 |
this.setDynValue(PASSWORD_PARAMTER_NAME, password);
|
126 |
} |
127 |
|
128 |
/**
|
129 |
* Set <code>JDBC Driver class name</code> parameter
|
130 |
*
|
131 |
* @param className
|
132 |
*/
|
133 |
@Override
|
134 |
public void setJDBCDriverClassName(String className) { |
135 |
this.setDynValue(JDBC_DRIVER_CLASS_PARAMTER_NAME, className);
|
136 |
} |
137 |
|
138 |
@Override
|
139 |
public String getJDBCDriverClassName() { |
140 |
return (String) this.getDynValue(JDBC_DRIVER_CLASS_PARAMTER_NAME); |
141 |
} |
142 |
|
143 |
@Override
|
144 |
public String getCatalog() { |
145 |
return (String) this.getDynValue(CATALOG_PARAMTER_NAME); |
146 |
} |
147 |
|
148 |
|
149 |
/**
|
150 |
* Set <code>catalog</code> parameter
|
151 |
*
|
152 |
* @param className
|
153 |
*/
|
154 |
@Override
|
155 |
public void setCatalog(String catalog) { |
156 |
this.setDynValue(CATALOG_PARAMTER_NAME, catalog);
|
157 |
} |
158 |
|
159 |
@Override
|
160 |
public String getSchema() { |
161 |
return (String) this.getDynValue(SCHEMA_PARAMTER_NAME); |
162 |
} |
163 |
|
164 |
/**
|
165 |
* Set <code>schema</code> parameter
|
166 |
*
|
167 |
* @param className
|
168 |
*/
|
169 |
@Override
|
170 |
public void setSchema(String schema) { |
171 |
this.setDynValue(SCHEMA_PARAMTER_NAME, schema);
|
172 |
} |
173 |
|
174 |
@Override
|
175 |
public String getTable() { |
176 |
return (String) this.getDynValue(TABLE_PARAMTER_NAME); |
177 |
} |
178 |
|
179 |
@Override
|
180 |
public void setTable(String table) { |
181 |
this.setDynValue(TABLE_PARAMTER_NAME, table);
|
182 |
} |
183 |
|
184 |
@Override
|
185 |
public String getFieldsString() { |
186 |
return (String) this.getDynValue(FIELDS_PARAMTER_NAME); |
187 |
} |
188 |
|
189 |
@Override
|
190 |
public String[] getFields() { |
191 |
String fields = (String) this.getDynValue(FIELDS_PARAMTER_NAME); |
192 |
if (fields == null) { |
193 |
return null; |
194 |
} |
195 |
// FIXME check for fields with spaces and special chars
|
196 |
return fields.split(","); |
197 |
} |
198 |
|
199 |
@Override
|
200 |
public void setFields(String fields) { |
201 |
this.setDynValue(FIELDS_PARAMTER_NAME, fields);
|
202 |
} |
203 |
|
204 |
@Override
|
205 |
public void setFields(String[] fields) { |
206 |
StringBuilder str = new StringBuilder(); |
207 |
for (int i = 0; i < fields.length - 1; i++) { |
208 |
str.append(fields[i]); |
209 |
str.append(",");
|
210 |
} |
211 |
str.append(fields[fields.length - 1]);
|
212 |
|
213 |
this.setDynValue(FIELDS_PARAMTER_NAME, str.toString());
|
214 |
} |
215 |
|
216 |
@Override
|
217 |
public String getSQL() { |
218 |
return (String) this.getDynValue(SQL_PARAMTER_NAME); |
219 |
} |
220 |
|
221 |
@Override
|
222 |
public void setSQL(String sql) { |
223 |
this.setDynValue(SQL_PARAMTER_NAME, sql);
|
224 |
} |
225 |
|
226 |
@Override
|
227 |
public String getBaseFilter() { |
228 |
return (String) this.getDynValue(BASEFILTER_PARAMTER_NAME); |
229 |
} |
230 |
|
231 |
@Override
|
232 |
public void setBaseFilter(String initialFilter) { |
233 |
this.setDynValue(BASEFILTER_PARAMTER_NAME, initialFilter);
|
234 |
} |
235 |
|
236 |
@Override
|
237 |
public String getBaseOrder() { |
238 |
return (String) this.getDynValue(BASEORDER_PARAMTER_NAME); |
239 |
} |
240 |
|
241 |
@Override
|
242 |
public void setBaseOrder(String order) { |
243 |
this.setDynValue(BASEORDER_PARAMTER_NAME, order);
|
244 |
} |
245 |
|
246 |
@Override
|
247 |
public String getPkFieldsString() { |
248 |
return (String) this.getDynValue(PKFIELDS_PARAMTER_NAME); |
249 |
} |
250 |
|
251 |
@Override
|
252 |
public String[] getPkFields() { |
253 |
String fields = (String) this.getDynValue(PKFIELDS_PARAMTER_NAME); |
254 |
if (fields == null) { |
255 |
return null; |
256 |
} |
257 |
// FIXME check for fields with spaces and special chars
|
258 |
return fields.split(","); |
259 |
} |
260 |
|
261 |
@Override
|
262 |
public void setPkFields(String fields) { |
263 |
this.setDynValue(PKFIELDS_PARAMTER_NAME, fields);
|
264 |
} |
265 |
|
266 |
@Override
|
267 |
public void setPkFields(String[] fields) { |
268 |
StringBuilder str = new StringBuilder(); |
269 |
for (int i = 0; i < fields.length - 1; i++) { |
270 |
str.append(fields[i]); |
271 |
str.append(",");
|
272 |
} |
273 |
str.append(fields[fields.length - 1]);
|
274 |
|
275 |
this.setDynValue(PKFIELDS_PARAMTER_NAME, str.toString());
|
276 |
} |
277 |
|
278 |
/**
|
279 |
* Return table <code>name</code> or <code>schema.tableName</code> if
|
280 |
* <code>schema</code> parameter is set.
|
281 |
*
|
282 |
* @return
|
283 |
*/
|
284 |
public String tableID() { |
285 |
if ( StringUtils.isEmpty(this.getSchema()) ) { |
286 |
return escapeName(this.getTable()); |
287 |
} |
288 |
return escapeName(this.getSchema()) + "." + escapeName(this.getTable()); |
289 |
} |
290 |
|
291 |
protected String escapeName(String name) { |
292 |
return "\"".concat(name).concat("\""); |
293 |
} |
294 |
|
295 |
/**
|
296 |
* Compound a string that can identify the source
|
297 |
*
|
298 |
* @return
|
299 |
*/
|
300 |
// @Override
|
301 |
// public String getSourceId() {
|
302 |
// if (getTable() != null) {
|
303 |
// return MessageFormat.format(
|
304 |
// "provider={0}:url=\"{1}\":table=\"{2}\":user={3}:driverclass={4}",
|
305 |
// this.getDataStoreName(),
|
306 |
// this.getUrl(),
|
307 |
// this.getTable(),
|
308 |
// this.getUser(),
|
309 |
// this.getJDBCDriverClassName()
|
310 |
// );
|
311 |
// }
|
312 |
// return MessageFormat.format(
|
313 |
// "provider={0}:url=\"{1}\":sql=\"{2}\":user={3}:driverclass={4}",
|
314 |
// this.getDataStoreName(),
|
315 |
// this.getUrl(),
|
316 |
// this.getSQL(),
|
317 |
// this.getUser(),
|
318 |
// this.getJDBCDriverClassName()
|
319 |
// );
|
320 |
// }
|
321 |
|
322 |
@Override
|
323 |
public String getUrl() { |
324 |
return (String) this.getDynValue(URL_PARAMTER_NAME); |
325 |
} |
326 |
|
327 |
/**
|
328 |
* Set <code>JDBC connection url</code> parameter
|
329 |
*
|
330 |
* @param url
|
331 |
*/
|
332 |
@Override
|
333 |
public void setUrl(String url) { |
334 |
this.setDynValue(URL_PARAMTER_NAME, url);
|
335 |
} |
336 |
|
337 |
@Override
|
338 |
public JDBCStoreParametersBase getCopy() {
|
339 |
return (JDBCStoreParametersBase) super.getCopy(); |
340 |
} |
341 |
|
342 |
@Override
|
343 |
public void setBatchSize(int batchSize) { |
344 |
this.setDynValue(BATCH_SIZE_PARAMETER_NAME, batchSize);
|
345 |
} |
346 |
|
347 |
@Override
|
348 |
public boolean isTheSameStore(DataStoreParameters params) { |
349 |
if(!(params instanceof JDBCStoreParametersBase)) { |
350 |
return false; |
351 |
} |
352 |
if(!(StringUtils.equals(((JDBCStoreParametersBase)params).getTable(), this.getTable()))){ |
353 |
return false; |
354 |
} |
355 |
if(!(StringUtils.equals(((JDBCStoreParametersBase)params).getSchema(), this.getSchema()))){ |
356 |
return false; |
357 |
} |
358 |
if(!(StringUtils.equals(((JDBCStoreParametersBase)params).getSQL(), this.getSQL()))){ |
359 |
return false; |
360 |
} |
361 |
return true; |
362 |
} |
363 |
|
364 |
@Override
|
365 |
public String getSourceId() { |
366 |
StringBuilder builder = new StringBuilder(); |
367 |
builder.append(this.getTable());
|
368 |
builder.append("(");
|
369 |
boolean needComma = false; |
370 |
if( StringUtils.isNotBlank(this.getHost()) ) { |
371 |
builder.append("host=");
|
372 |
builder.append(this.getHost());
|
373 |
needComma = true;
|
374 |
} |
375 |
if( this.getPort()>0 ) { |
376 |
if (needComma ) {
|
377 |
builder.append(", ");
|
378 |
} |
379 |
builder.append("port=");
|
380 |
builder.append(this.getPort());
|
381 |
needComma = true;
|
382 |
} |
383 |
if( StringUtils.isNotBlank(this.getDBName()) ) { |
384 |
if (needComma ) {
|
385 |
builder.append(", ");
|
386 |
} |
387 |
builder.append("db=");
|
388 |
builder.append(this.getDBName());
|
389 |
needComma = true;
|
390 |
} |
391 |
if( StringUtils.isNotBlank(this.getSchema()) ) { |
392 |
if (needComma ) {
|
393 |
builder.append(", ");
|
394 |
} |
395 |
builder.append("schema=");
|
396 |
builder.append(this.getSchema());
|
397 |
needComma = true;
|
398 |
} |
399 |
builder.append(")");
|
400 |
return builder.toString();
|
401 |
} |
402 |
|
403 |
} |