root / branches / v2_0_0_prep / libraries / libFMap_daldb / src / org / gvsig / fmap / dal / store / postgesql / PostgreSQLStoreProvider.java @ 27525
History | View | Annotate | Download (11 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 2
|
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 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2009 IVER T.I {{Task}}
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.fmap.dal.store.postgesql; |
29 |
|
30 |
import java.util.ArrayList; |
31 |
import java.util.Iterator; |
32 |
import java.util.List; |
33 |
|
34 |
import org.gvsig.fmap.dal.DataServerExplorer; |
35 |
import org.gvsig.fmap.dal.DataTypes; |
36 |
import org.gvsig.fmap.dal.exception.CloseException; |
37 |
import org.gvsig.fmap.dal.exception.DataException; |
38 |
import org.gvsig.fmap.dal.exception.InitializeException; |
39 |
import org.gvsig.fmap.dal.exception.OpenException; |
40 |
import org.gvsig.fmap.dal.exception.ReadException; |
41 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
42 |
import org.gvsig.fmap.dal.feature.Feature; |
43 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
44 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
45 |
import org.gvsig.fmap.dal.feature.FeatureType; |
46 |
import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
47 |
import org.gvsig.fmap.dal.feature.spi.FeatureData; |
48 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
49 |
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
50 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider; |
51 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices; |
52 |
import org.gvsig.fmap.dal.resource.exception.ResourceBeginException; |
53 |
import org.gvsig.fmap.dal.resource.exception.ResourceException; |
54 |
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
55 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
56 |
import org.gvsig.fmap.dal.store.jdbc.JDBCResource; |
57 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreProvider; |
58 |
import org.gvsig.tools.ToolsLocator; |
59 |
import org.gvsig.tools.dynobject.DelegatedDynObject; |
60 |
import org.gvsig.tools.dynobject.DynClass; |
61 |
import org.gvsig.tools.dynobject.DynObjectManager; |
62 |
import org.gvsig.tools.persistence.PersistenceException; |
63 |
import org.gvsig.tools.persistence.PersistentState; |
64 |
|
65 |
public class PostgreSQLStoreProvider extends JDBCStoreProvider |
66 |
implements ResourceConsumer {
|
67 |
|
68 |
public static String NAME = "PostgreSQLStore"; |
69 |
public static String DESCRIPTION = "PostgreSQL source"; |
70 |
private static final String DYNCLASS_NAME = "PostgreSQLStore"; |
71 |
private static DynClass DYNCLASS = null; |
72 |
|
73 |
|
74 |
protected PostgreSQLStoreParameters params;
|
75 |
protected JDBCResource resource;
|
76 |
protected boolean directSQLMode; |
77 |
|
78 |
protected static void registerDynClass() { |
79 |
DynObjectManager dynman = ToolsLocator.getDynObjectManager(); |
80 |
DynClass dynClass; |
81 |
if (DYNCLASS == null) { |
82 |
dynClass = dynman.add(DYNCLASS_NAME, DESCRIPTION); |
83 |
|
84 |
dynClass.extend(dynman.get(FeatureStore.DYNCLASS_NAME)); |
85 |
DYNCLASS = dynClass; |
86 |
} |
87 |
} |
88 |
|
89 |
public PostgreSQLStoreProvider() {
|
90 |
super();
|
91 |
} |
92 |
|
93 |
public PostgreSQLStoreProvider(PostgreSQLStoreParameters params)
|
94 |
throws InitializeException {
|
95 |
super();
|
96 |
this.init(params);
|
97 |
} |
98 |
|
99 |
protected void init(PostgreSQLStoreParameters params) |
100 |
throws InitializeException {
|
101 |
this.params = params;
|
102 |
this.dynObject = (DelegatedDynObject) ToolsLocator
|
103 |
.getDynObjectManager().createDynObject(DYNCLASS); |
104 |
|
105 |
this.dynObject.setDynValue("DefaultSRS", null); |
106 |
this.dynObject.setDynValue("Envelope", null); |
107 |
|
108 |
resource = (PostgreSQLResource) this.createResource(
|
109 |
PostgreSQLResource.NAME, |
110 |
new Object[] { params.getHost(), |
111 |
params.getPort(), params.getDBName(), params.getUser(), |
112 |
params.getPassword(), params.getUseSSL() }); |
113 |
resource.addConsumer(this);
|
114 |
|
115 |
if (params.getSQL() != null && (params.getSQL()).trim().length() > 0) { |
116 |
directSQLMode = true;
|
117 |
} |
118 |
} |
119 |
|
120 |
protected String compoundCountSelect(String filter) { |
121 |
if (this.directSQLMode) { |
122 |
return null; |
123 |
} |
124 |
// Select
|
125 |
StringBuilder sql = new StringBuilder(); |
126 |
sql.append("Select count(");
|
127 |
String[] pkFields = params.getPkFields(); |
128 |
if (pkFields != null && pkFields.length == 1) { |
129 |
sql.append(pkFields[1]);
|
130 |
} else {
|
131 |
sql.append('*');
|
132 |
|
133 |
} |
134 |
sql.append(") ");
|
135 |
sql.append(params.tableID()); |
136 |
sql.append(' ');
|
137 |
|
138 |
appendWhere(sql, filter); |
139 |
|
140 |
return sql.toString();
|
141 |
} |
142 |
|
143 |
private void appendWhere(StringBuilder sql, String filter) { |
144 |
String initialFilter = params.getInitialFilter();
|
145 |
if ((initialFilter != null && initialFilter.length() != 0) |
146 |
|| (filter != null && filter.length() != 0)) { |
147 |
sql.append("where (");
|
148 |
|
149 |
if (initialFilter != null && initialFilter.length() != 0 |
150 |
&& filter != null && filter.length() != 0) { |
151 |
// initialFilter + filter
|
152 |
sql.append('(');
|
153 |
sql.append(initialFilter); |
154 |
sql.append(") and (");
|
155 |
sql.append(filter); |
156 |
sql.append(')');
|
157 |
} else if (initialFilter != null && initialFilter.length() != 0) { |
158 |
// initialFilter only
|
159 |
sql.append(initialFilter); |
160 |
} else {
|
161 |
// filter only
|
162 |
sql.append(filter); |
163 |
} |
164 |
sql.append(") ");
|
165 |
} |
166 |
|
167 |
} |
168 |
|
169 |
protected String compoundSelect(String[] fields, String filter, |
170 |
String order,
|
171 |
long limit, long offset) { |
172 |
StringBuilder sql = new StringBuilder(); |
173 |
if (directSQLMode) {
|
174 |
if (filter != null || order != null) { |
175 |
// FIXME
|
176 |
throw new UnsupportedOperationException(); |
177 |
} |
178 |
sql.append(params.getSQL()); |
179 |
sql.append(' ');
|
180 |
} else {
|
181 |
|
182 |
// Select
|
183 |
sql.append("Select ");
|
184 |
if (fields == null || fields.length == 0) { |
185 |
sql.append("* ");
|
186 |
|
187 |
} else {
|
188 |
for (int i = 0; i < fields.length - 1; i++) { |
189 |
sql.append(fields[i]); |
190 |
sql.append(", ");
|
191 |
} |
192 |
sql.append(fields[fields.length - 1]);
|
193 |
String[] pkFields = params.getPkFields(); |
194 |
if (pkFields != null && pkFields.length > 0) { |
195 |
// checks for pk fields are in select
|
196 |
boolean toAdd;
|
197 |
for (int i = 0; i < pkFields.length; i++) { |
198 |
toAdd = true;
|
199 |
for (int j = 0; j < fields.length; j++) { |
200 |
if (pkFields[i].equals(fields[j])) {
|
201 |
toAdd = false;
|
202 |
break;
|
203 |
} |
204 |
if (toAdd) {
|
205 |
sql.append(", ");
|
206 |
sql.append(pkFields[i]); |
207 |
} |
208 |
} |
209 |
} |
210 |
sql.append(' ');
|
211 |
} |
212 |
|
213 |
} |
214 |
|
215 |
// table
|
216 |
sql.append("from ");
|
217 |
sql.append(params.tableID()); |
218 |
sql.append(' ');
|
219 |
|
220 |
|
221 |
// Where
|
222 |
appendWhere(sql, filter); |
223 |
|
224 |
// Order
|
225 |
if ((params.getInitialOrder() != null && params |
226 |
.getInitialOrder().length() != 0)
|
227 |
|| (order != null && order.length() != 0)) { |
228 |
sql.append("order by ");
|
229 |
|
230 |
if (order != null && order.length() != 0) { |
231 |
// order
|
232 |
sql.append(order); |
233 |
} else {
|
234 |
// initial order
|
235 |
sql.append(params.getInitialOrder()); |
236 |
} |
237 |
sql.append(' ');
|
238 |
} |
239 |
} |
240 |
// limit
|
241 |
if (limit >= 1) { |
242 |
sql.append("limit ");
|
243 |
sql.append(limit); |
244 |
sql.append(' ');
|
245 |
} |
246 |
|
247 |
// offset
|
248 |
if (offset >= 1) { |
249 |
sql.append("offset ");
|
250 |
sql.append(offset); |
251 |
sql.append(' ');
|
252 |
} |
253 |
|
254 |
|
255 |
return sql.toString();
|
256 |
} |
257 |
|
258 |
public FeatureStoreProvider initialize(FeatureStoreProviderServices store)
|
259 |
throws InitializeException {
|
260 |
super.initialize(store);
|
261 |
this.initFeatureType();
|
262 |
return this; |
263 |
} |
264 |
|
265 |
|
266 |
protected void initFeatureType() throws InitializeException { |
267 |
try {
|
268 |
open(); |
269 |
resource.begin(); |
270 |
} catch (DataException e) {
|
271 |
throw new InitializeException(this.getName(), e); |
272 |
} |
273 |
|
274 |
EditableFeatureType edFType = this.store.createFeatureType();
|
275 |
try {
|
276 |
|
277 |
String sql = compoundSelect(params.getFields(), null, null, |
278 |
1, 0); |
279 |
PostgreSQLUtils.getInstance().loadFeatureType( |
280 |
resource.getConnection(), edFType, params, sql); |
281 |
|
282 |
} catch (DataException e) {
|
283 |
throw new InitializeException(this.getName(), e); |
284 |
} finally {
|
285 |
resource.end(); |
286 |
} |
287 |
|
288 |
FeatureType defaultType = edFType.getNotEditableCopy(); |
289 |
List types = new ArrayList(1); |
290 |
types.add(defaultType); |
291 |
this.store.setFeatureTypes(types, defaultType);
|
292 |
} |
293 |
|
294 |
public DataServerExplorer getExplorer() throws ReadException { |
295 |
// TODO Auto-generated method stub
|
296 |
return null; |
297 |
} |
298 |
|
299 |
public void performEditing(Iterator deleteds, Iterator inserteds, |
300 |
Iterator updateds) throws PerformEditingException { |
301 |
// TODO Auto-generated method stub
|
302 |
|
303 |
} |
304 |
|
305 |
public boolean closeResourceRequested(ResourceProvider resource) { |
306 |
// TODO Auto-generated method stub
|
307 |
return false; |
308 |
} |
309 |
|
310 |
public void resourceChanged(ResourceProvider resource) { |
311 |
// TODO Auto-generated method stub
|
312 |
} |
313 |
|
314 |
public void append(Feature feature) throws DataException { |
315 |
// TODO Auto-generated method stub
|
316 |
|
317 |
} |
318 |
|
319 |
public void beginAppend() throws DataException { |
320 |
// TODO Auto-generated method stub
|
321 |
|
322 |
} |
323 |
|
324 |
public boolean canWriteGeometry(int geometryType) throws DataException { |
325 |
// TODO Auto-generated method stub
|
326 |
return true; |
327 |
} |
328 |
|
329 |
public Object createNewOID() { |
330 |
// TODO Auto-generated method stub
|
331 |
return null; |
332 |
} |
333 |
|
334 |
public FeatureSetProvider createSet(FeatureQuery query, FeatureType featureType)
|
335 |
throws DataException {
|
336 |
|
337 |
// TODO Auto-generated method stub
|
338 |
return null; |
339 |
} |
340 |
|
341 |
public void endAppend() throws DataException { |
342 |
// TODO Auto-generated method stub
|
343 |
|
344 |
} |
345 |
|
346 |
public FeatureData getFeatureDataByReference(
|
347 |
FeatureReferenceProviderServices reference) throws DataException {
|
348 |
// TODO Auto-generated method stub
|
349 |
return null; |
350 |
} |
351 |
|
352 |
public FeatureData getFeatureDataByReference(
|
353 |
FeatureReferenceProviderServices reference, FeatureType featureType) |
354 |
throws DataException {
|
355 |
// TODO Auto-generated method stub
|
356 |
return null; |
357 |
} |
358 |
|
359 |
public int getFeatureReferenceOIDType() { |
360 |
return DataTypes.UNKNOWN;
|
361 |
} |
362 |
|
363 |
public String getName() { |
364 |
return NAME;
|
365 |
} |
366 |
|
367 |
public boolean supportsAppendMode() { |
368 |
// TODO Auto-generated method stub
|
369 |
return false; |
370 |
} |
371 |
|
372 |
public PersistentState getState() throws PersistenceException { |
373 |
// TODO Auto-generated method stub
|
374 |
return null; |
375 |
} |
376 |
|
377 |
public void loadState(PersistentState state) throws PersistenceException { |
378 |
// TODO Auto-generated method stub
|
379 |
|
380 |
} |
381 |
|
382 |
public void setState(PersistentState state) throws PersistenceException { |
383 |
// TODO Auto-generated method stub
|
384 |
|
385 |
} |
386 |
|
387 |
public Iterator getChilds() { |
388 |
// TODO Auto-generated method stub
|
389 |
return null; |
390 |
} |
391 |
|
392 |
public void open() throws OpenException { |
393 |
if (resource.isOpen()) {
|
394 |
return;
|
395 |
} |
396 |
try {
|
397 |
resource.begin(); |
398 |
} catch (ResourceBeginException e1) {
|
399 |
throw new OpenException(this.getName(), e1); |
400 |
} |
401 |
try {
|
402 |
resource.connect(); |
403 |
|
404 |
// TODO load metadata if is needed
|
405 |
} catch (DataException e) {
|
406 |
throw new OpenException(this.getName(), e); |
407 |
} finally {
|
408 |
resource.end(); |
409 |
} |
410 |
} |
411 |
|
412 |
|
413 |
public boolean allowWrite() { |
414 |
return (!directSQLMode) && params.getPkFields() != null |
415 |
&& params.getPkFields().length > 0;
|
416 |
} |
417 |
|
418 |
public void close() throws CloseException { |
419 |
super.close();
|
420 |
try {
|
421 |
this.resource.closeRequest();
|
422 |
} catch (ResourceException e) {
|
423 |
throw new CloseException(this.getName(), e); |
424 |
} |
425 |
} |
426 |
|
427 |
public Object getSourceId() { |
428 |
return this.params.getSourceId(); |
429 |
} |
430 |
|
431 |
|
432 |
|
433 |
} |