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