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 / JDBCStoreParameters.java @ 46338
History | View | Annotate | Download (8.29 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
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 | 40435 | jjdelcerro | |
25 | package org.gvsig.fmap.dal.store.jdbc; |
||
26 | |||
27 | import java.text.MessageFormat; |
||
28 | 43362 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
29 | 46338 | fdiaz | import org.gvsig.fmap.dal.DataStoreParameters; |
30 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.store.db.DBStoreParameters; |
31 | |||
32 | /**
|
||
33 | * Parameters class for JDBC generic provider
|
||
34 | *
|
||
35 | */
|
||
36 | public class JDBCStoreParameters extends DBStoreParameters |
||
37 | implements JDBCConnectionParameters {
|
||
38 | |||
39 | 45687 | jjdelcerro | public static final String BATCHSIZE_PARAMTER_NAME = "batchSize"; |
40 | |||
41 | 43020 | jjdelcerro | // public static final String PARAMETERS_DEFINITION_NAME = "JDBCStoreParameters";
|
42 | 40435 | jjdelcerro | |
43 | public JDBCStoreParameters() {
|
||
44 | 43020 | jjdelcerro | super(
|
45 | JDBCLibrary.NAME + "StoreParameters",
|
||
46 | JDBCLibrary.NAME |
||
47 | ); |
||
48 | 40435 | jjdelcerro | } |
49 | |||
50 | protected JDBCStoreParameters(String parametersDefinitionName) { |
||
51 | 43020 | jjdelcerro | super(
|
52 | parametersDefinitionName, |
||
53 | JDBCLibrary.NAME |
||
54 | ); |
||
55 | 40435 | jjdelcerro | } |
56 | |||
57 | 43020 | jjdelcerro | public JDBCStoreParameters(String parametersDefinitionName, String providerName) { |
58 | 40435 | jjdelcerro | super(parametersDefinitionName,providerName);
|
59 | } |
||
60 | |||
61 | public boolean isValid() { |
||
62 | return this.getHost() != null; |
||
63 | } |
||
64 | |||
65 | public String getHost() { |
||
66 | return (String) this.getDynValue(HOST_PARAMTER_NAME); |
||
67 | } |
||
68 | |||
69 | public Integer getPort() { |
||
70 | return (Integer) this.getDynValue(PORT_PARAMTER_NAME); |
||
71 | } |
||
72 | |||
73 | 45507 | jjdelcerro | @Override
|
74 | public int getBatchSize() { |
||
75 | try {
|
||
76 | return (int) this.getDynValue(BATCH_SIZE_PARAMETER_NAME); |
||
77 | } catch(Exception ex) { |
||
78 | return DEFAULT_BATCH_SIZE;
|
||
79 | } |
||
80 | } |
||
81 | |||
82 | 40435 | jjdelcerro | public String getDBName() { |
83 | return (String) this.getDynValue(DBNAME_PARAMTER_NAME); |
||
84 | } |
||
85 | |||
86 | public String getUser() { |
||
87 | return (String) this.getDynValue(USER_PARAMTER_NAME); |
||
88 | } |
||
89 | |||
90 | public String getPassword() { |
||
91 | return (String) this.getDynValue(PASSWORD_PARAMTER_NAME); |
||
92 | } |
||
93 | |||
94 | public void setHost(String host) { |
||
95 | this.setDynValue(HOST_PARAMTER_NAME, host);
|
||
96 | } |
||
97 | |||
98 | public void setPort(int port) { |
||
99 | this.setDynValue(PORT_PARAMTER_NAME, new Integer(port)); |
||
100 | } |
||
101 | |||
102 | public void setPort(Integer port) { |
||
103 | this.setDynValue(PORT_PARAMTER_NAME, port);
|
||
104 | } |
||
105 | |||
106 | public void setDBName(String dbName) { |
||
107 | this.setDynValue(DBNAME_PARAMTER_NAME, dbName);
|
||
108 | } |
||
109 | |||
110 | public void setUser(String user) { |
||
111 | this.setDynValue(USER_PARAMTER_NAME, user);
|
||
112 | } |
||
113 | |||
114 | public void setPassword(String password) { |
||
115 | this.setDynValue(PASSWORD_PARAMTER_NAME, password);
|
||
116 | } |
||
117 | |||
118 | /**
|
||
119 | * Set <code>JDBC Driver class name</code> parameter
|
||
120 | *
|
||
121 | * @param className
|
||
122 | */
|
||
123 | public void setJDBCDriverClassName(String className) { |
||
124 | this.setDynValue(JDBC_DRIVER_CLASS_PARAMTER_NAME, className);
|
||
125 | } |
||
126 | |||
127 | public String getJDBCDriverClassName() { |
||
128 | return (String) this.getDynValue(JDBC_DRIVER_CLASS_PARAMTER_NAME); |
||
129 | } |
||
130 | |||
131 | public String getCatalog() { |
||
132 | return (String) this.getDynValue(CATALOG_PARAMTER_NAME); |
||
133 | } |
||
134 | |||
135 | |||
136 | /**
|
||
137 | * Set <code>catalog</code> parameter
|
||
138 | *
|
||
139 | * @param className
|
||
140 | */
|
||
141 | public void setCatalog(String catalog) { |
||
142 | this.setDynValue(CATALOG_PARAMTER_NAME, catalog);
|
||
143 | } |
||
144 | |||
145 | public String getSchema() { |
||
146 | return (String) this.getDynValue(SCHEMA_PARAMTER_NAME); |
||
147 | } |
||
148 | |||
149 | /**
|
||
150 | * Set <code>schema</code> parameter
|
||
151 | *
|
||
152 | * @param className
|
||
153 | */
|
||
154 | public void setSchema(String schema) { |
||
155 | this.setDynValue(SCHEMA_PARAMTER_NAME, schema);
|
||
156 | } |
||
157 | |||
158 | public String getTable() { |
||
159 | return (String) this.getDynValue(TABLE_PARAMTER_NAME); |
||
160 | } |
||
161 | |||
162 | public void setTable(String table) { |
||
163 | this.setDynValue(TABLE_PARAMTER_NAME, table);
|
||
164 | } |
||
165 | |||
166 | public String getFieldsString() { |
||
167 | return (String) this.getDynValue(FIELDS_PARAMTER_NAME); |
||
168 | } |
||
169 | |||
170 | public String[] getFields() { |
||
171 | String fields = (String) this.getDynValue(FIELDS_PARAMTER_NAME); |
||
172 | if (fields == null) { |
||
173 | return null; |
||
174 | } |
||
175 | // FIXME check for fields with spaces and special chars
|
||
176 | return fields.split(","); |
||
177 | } |
||
178 | |||
179 | public void setFields(String fields) { |
||
180 | this.setDynValue(FIELDS_PARAMTER_NAME, fields);
|
||
181 | } |
||
182 | |||
183 | public void setFields(String[] fields) { |
||
184 | StringBuilder str = new StringBuilder(); |
||
185 | for (int i = 0; i < fields.length - 1; i++) { |
||
186 | str.append(fields[i]); |
||
187 | str.append(",");
|
||
188 | } |
||
189 | str.append(fields[fields.length - 1]);
|
||
190 | |||
191 | this.setDynValue(FIELDS_PARAMTER_NAME, str.toString());
|
||
192 | } |
||
193 | |||
194 | public String getSQL() { |
||
195 | return (String) this.getDynValue(SQL_PARAMTER_NAME); |
||
196 | } |
||
197 | |||
198 | public void setSQL(String sql) { |
||
199 | this.setDynValue(SQL_PARAMTER_NAME, sql);
|
||
200 | } |
||
201 | |||
202 | public String getBaseFilter() { |
||
203 | return (String) this.getDynValue(BASEFILTER_PARAMTER_NAME); |
||
204 | } |
||
205 | |||
206 | public void setBaseFilter(String initialFilter) { |
||
207 | this.setDynValue(BASEFILTER_PARAMTER_NAME, initialFilter);
|
||
208 | } |
||
209 | |||
210 | public String getBaseOrder() { |
||
211 | return (String) this.getDynValue(BASEORDER_PARAMTER_NAME); |
||
212 | } |
||
213 | |||
214 | public void setBaseOrder(String order) { |
||
215 | this.setDynValue(BASEORDER_PARAMTER_NAME, order);
|
||
216 | } |
||
217 | |||
218 | public String getPkFieldsString() { |
||
219 | return (String) this.getDynValue(PKFIELDS_PARAMTER_NAME); |
||
220 | } |
||
221 | |||
222 | public String[] getPkFields() { |
||
223 | String fields = (String) this.getDynValue(PKFIELDS_PARAMTER_NAME); |
||
224 | if (fields == null) { |
||
225 | return null; |
||
226 | } |
||
227 | // FIXME check for fields with spaces and special chars
|
||
228 | return fields.split(","); |
||
229 | } |
||
230 | |||
231 | public void setPkFields(String fields) { |
||
232 | this.setDynValue(PKFIELDS_PARAMTER_NAME, fields);
|
||
233 | } |
||
234 | |||
235 | public void setPkFields(String[] fields) { |
||
236 | StringBuilder str = new StringBuilder(); |
||
237 | for (int i = 0; i < fields.length - 1; i++) { |
||
238 | str.append(fields[i]); |
||
239 | str.append(",");
|
||
240 | } |
||
241 | str.append(fields[fields.length - 1]);
|
||
242 | |||
243 | this.setDynValue(PKFIELDS_PARAMTER_NAME, str.toString());
|
||
244 | } |
||
245 | |||
246 | /**
|
||
247 | * Return table <code>name</code> or <code>schema.tableName</code> if
|
||
248 | * <code>schema</code> parameter is set.
|
||
249 | *
|
||
250 | * @return
|
||
251 | */
|
||
252 | public String tableID() { |
||
253 | 43362 | jjdelcerro | if ( StringUtils.isEmpty(this.getSchema()) ) { |
254 | 40435 | jjdelcerro | return escapeName(this.getTable()); |
255 | } |
||
256 | return escapeName(this.getSchema()) + "." + escapeName(this.getTable()); |
||
257 | } |
||
258 | |||
259 | protected String escapeName(String name) { |
||
260 | return "\"".concat(name).concat("\""); |
||
261 | } |
||
262 | |||
263 | /**
|
||
264 | * Compound a string that can identify the source
|
||
265 | *
|
||
266 | * @return
|
||
267 | */
|
||
268 | public String getSourceId() { |
||
269 | if (getTable() != null) { |
||
270 | return MessageFormat.format( |
||
271 | "provider={0}:url=\"{1}\":table=\"{2}\":user={3}:driverclass={4}",
|
||
272 | this.getDataStoreName(),
|
||
273 | this.getUrl(),
|
||
274 | this.getTable(),
|
||
275 | this.getUser(),
|
||
276 | this.getJDBCDriverClassName()
|
||
277 | ); |
||
278 | } |
||
279 | return MessageFormat.format( |
||
280 | "provider={0}:url=\"{1}\":sql=\"{2}\":user={3}:driverclass={4}",
|
||
281 | this.getDataStoreName(),
|
||
282 | this.getUrl(),
|
||
283 | this.getSQL(),
|
||
284 | this.getUser(),
|
||
285 | this.getJDBCDriverClassName()
|
||
286 | ); |
||
287 | } |
||
288 | |||
289 | public String getUrl() { |
||
290 | return (String) this.getDynValue(URL_PARAMTER_NAME); |
||
291 | } |
||
292 | |||
293 | /**
|
||
294 | * Set <code>JDBC connection url</code> parameter
|
||
295 | *
|
||
296 | * @param url
|
||
297 | */
|
||
298 | public void setUrl(String url) { |
||
299 | this.setDynValue(URL_PARAMTER_NAME, url);
|
||
300 | } |
||
301 | |||
302 | 43020 | jjdelcerro | @Override
|
303 | public JDBCStoreParameters getCopy() {
|
||
304 | return (JDBCStoreParameters) super.getCopy(); |
||
305 | } |
||
306 | 45687 | jjdelcerro | |
307 | public void setBatchSize(int batchSize) { |
||
308 | this.setDynValue(BATCH_SIZE_PARAMETER_NAME, batchSize);
|
||
309 | } |
||
310 | 43020 | jjdelcerro | |
311 | 46338 | fdiaz | @Override
|
312 | public boolean isTheSameStore(DataStoreParameters params) { |
||
313 | if(!(params instanceof JDBCStoreParameters)) { |
||
314 | return false; |
||
315 | } |
||
316 | if(!(StringUtils.equals(((JDBCStoreParameters)params).getTable(), this.getTable()))){ |
||
317 | return false; |
||
318 | } |
||
319 | if(!(StringUtils.equals(((JDBCStoreParameters)params).getSchema(), this.getSchema()))){ |
||
320 | return false; |
||
321 | } |
||
322 | if(!(StringUtils.equals(((JDBCStoreParameters)params).getSQL(), this.getSQL()))){ |
||
323 | return false; |
||
324 | } |
||
325 | return true; |
||
326 | } |
||
327 | |||
328 | 40435 | jjdelcerro | } |