svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_daldb / src / org / gvsig / fmap / dal / store / postgresql / PostgreSQLStoreProvider.java @ 28676
History | View | Annotate | Download (18.4 KB)
1 | 26790 | jmvivo | /* 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 | 27919 | jmvivo | package org.gvsig.fmap.dal.store.postgresql; |
29 | 26790 | jmvivo | |
30 | 27595 | jmvivo | import java.security.InvalidParameterException; |
31 | 27571 | jmvivo | import java.sql.Connection; |
32 | import java.sql.PreparedStatement; |
||
33 | import java.sql.ResultSet; |
||
34 | import java.sql.SQLException; |
||
35 | 26790 | jmvivo | import java.util.ArrayList; |
36 | import java.util.Iterator; |
||
37 | import java.util.List; |
||
38 | 28046 | jmvivo | import java.util.regex.Matcher; |
39 | import java.util.regex.Pattern; |
||
40 | 26790 | jmvivo | |
41 | 27982 | jmvivo | import org.cresques.cts.IProjection; |
42 | 27906 | jmvivo | import org.gvsig.fmap.dal.DALLocator; |
43 | import org.gvsig.fmap.dal.DataManager; |
||
44 | 26790 | jmvivo | import org.gvsig.fmap.dal.DataServerExplorer; |
45 | 28136 | jmvivo | import org.gvsig.fmap.dal.DataStoreNotification; |
46 | 27525 | jmvivo | import org.gvsig.fmap.dal.DataTypes; |
47 | 26790 | jmvivo | import org.gvsig.fmap.dal.exception.CloseException; |
48 | import org.gvsig.fmap.dal.exception.DataException; |
||
49 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
50 | import org.gvsig.fmap.dal.exception.OpenException; |
||
51 | import org.gvsig.fmap.dal.exception.ReadException; |
||
52 | 27906 | jmvivo | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
53 | 26790 | jmvivo | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
54 | 27595 | jmvivo | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
55 | 26790 | jmvivo | import org.gvsig.fmap.dal.feature.FeatureQuery; |
56 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
57 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
58 | import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
||
59 | import org.gvsig.fmap.dal.feature.spi.FeatureData; |
||
60 | import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
||
61 | import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
||
62 | import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider; |
||
63 | import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices; |
||
64 | import org.gvsig.fmap.dal.resource.exception.ResourceBeginException; |
||
65 | import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
||
66 | 27571 | jmvivo | import org.gvsig.fmap.dal.store.jdbc.JDBCExecuteSQLException; |
67 | import org.gvsig.fmap.dal.store.jdbc.JDBCSQLException; |
||
68 | 27525 | jmvivo | import org.gvsig.fmap.dal.store.jdbc.JDBCStoreProvider; |
69 | 27595 | jmvivo | import org.gvsig.fmap.geom.Geometry; |
70 | import org.gvsig.fmap.geom.operation.towkb.ToWKB; |
||
71 | 27982 | jmvivo | import org.gvsig.fmap.geom.primitive.Envelope; |
72 | 26790 | jmvivo | import org.gvsig.tools.ToolsLocator; |
73 | import org.gvsig.tools.dynobject.DelegatedDynObject; |
||
74 | import org.gvsig.tools.dynobject.DynClass; |
||
75 | import org.gvsig.tools.dynobject.DynObjectManager; |
||
76 | 27595 | jmvivo | import org.gvsig.tools.exception.BaseException; |
77 | 26790 | jmvivo | import org.gvsig.tools.persistence.PersistenceException; |
78 | import org.gvsig.tools.persistence.PersistentState; |
||
79 | 27906 | jmvivo | import org.slf4j.Logger; |
80 | import org.slf4j.LoggerFactory; |
||
81 | 26790 | jmvivo | |
82 | 27906 | jmvivo | public class PostgreSQLStoreProvider extends JDBCStoreProvider implements |
83 | PostgreSQLHelperUser { |
||
84 | 26790 | jmvivo | |
85 | 27906 | jmvivo | final static private Logger logger = LoggerFactory |
86 | .getLogger(PostgreSQLStoreProvider.class); |
||
87 | |||
88 | 26790 | jmvivo | public static String NAME = "PostgreSQLStore"; |
89 | public static String DESCRIPTION = "PostgreSQL source"; |
||
90 | private static final String DYNCLASS_NAME = "PostgreSQLStore"; |
||
91 | private static DynClass DYNCLASS = null; |
||
92 | |||
93 | 27525 | jmvivo | |
94 | protected PostgreSQLStoreParameters params;
|
||
95 | 27906 | jmvivo | protected PostgreSQLHelper helper;
|
96 | 27525 | jmvivo | protected boolean directSQLMode; |
97 | |||
98 | 27571 | jmvivo | private Long totalCount = null; |
99 | |||
100 | 26790 | jmvivo | protected static void registerDynClass() { |
101 | DynObjectManager dynman = ToolsLocator.getDynObjectManager(); |
||
102 | DynClass dynClass; |
||
103 | if (DYNCLASS == null) { |
||
104 | dynClass = dynman.add(DYNCLASS_NAME, DESCRIPTION); |
||
105 | |||
106 | dynClass.extend(dynman.get(FeatureStore.DYNCLASS_NAME)); |
||
107 | DYNCLASS = dynClass; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | public PostgreSQLStoreProvider() {
|
||
112 | super();
|
||
113 | } |
||
114 | |||
115 | public PostgreSQLStoreProvider(PostgreSQLStoreParameters params)
|
||
116 | throws InitializeException {
|
||
117 | super();
|
||
118 | this.init(params);
|
||
119 | } |
||
120 | |||
121 | 27525 | jmvivo | protected void init(PostgreSQLStoreParameters params) |
122 | 26790 | jmvivo | throws InitializeException {
|
123 | 27525 | jmvivo | this.params = params;
|
124 | 26790 | jmvivo | this.dynObject = (DelegatedDynObject) ToolsLocator
|
125 | .getDynObjectManager().createDynObject(DYNCLASS); |
||
126 | |||
127 | this.dynObject.setDynValue("DefaultSRS", null); |
||
128 | this.dynObject.setDynValue("Envelope", null); |
||
129 | |||
130 | 27906 | jmvivo | helper = new PostgreSQLHelper(this, params); |
131 | 26790 | jmvivo | |
132 | 27525 | jmvivo | if (params.getSQL() != null && (params.getSQL()).trim().length() > 0) { |
133 | directSQLMode = true;
|
||
134 | 26790 | jmvivo | } |
135 | } |
||
136 | |||
137 | 27525 | jmvivo | protected String compoundCountSelect(String filter) { |
138 | if (this.directSQLMode) { |
||
139 | return null; |
||
140 | } |
||
141 | // Select
|
||
142 | StringBuilder sql = new StringBuilder(); |
||
143 | sql.append("Select count(");
|
||
144 | String[] pkFields = params.getPkFields(); |
||
145 | if (pkFields != null && pkFields.length == 1) { |
||
146 | 28424 | jmvivo | sql.append(helper.escapeFieldName(pkFields[0]));
|
147 | 27525 | jmvivo | } else {
|
148 | sql.append('*');
|
||
149 | 26790 | jmvivo | |
150 | 27525 | jmvivo | } |
151 | sql.append(") ");
|
||
152 | 27672 | jmvivo | |
153 | sql.append("from ");
|
||
154 | |||
155 | 27525 | jmvivo | sql.append(params.tableID()); |
156 | sql.append(' ');
|
||
157 | |||
158 | appendWhere(sql, filter); |
||
159 | |||
160 | return sql.toString();
|
||
161 | 26790 | jmvivo | } |
162 | |||
163 | 27525 | jmvivo | private void appendWhere(StringBuilder sql, String filter) { |
164 | 28046 | jmvivo | filter = fixFilter(filter); |
165 | 27525 | jmvivo | String initialFilter = params.getInitialFilter();
|
166 | if ((initialFilter != null && initialFilter.length() != 0) |
||
167 | || (filter != null && filter.length() != 0)) { |
||
168 | sql.append("where (");
|
||
169 | 26790 | jmvivo | |
170 | 27525 | jmvivo | if (initialFilter != null && initialFilter.length() != 0 |
171 | && filter != null && filter.length() != 0) { |
||
172 | // initialFilter + filter
|
||
173 | sql.append('(');
|
||
174 | sql.append(initialFilter); |
||
175 | sql.append(") and (");
|
||
176 | sql.append(filter); |
||
177 | sql.append(')');
|
||
178 | } else if (initialFilter != null && initialFilter.length() != 0) { |
||
179 | // initialFilter only
|
||
180 | sql.append(initialFilter); |
||
181 | } else {
|
||
182 | // filter only
|
||
183 | sql.append(filter); |
||
184 | } |
||
185 | sql.append(") ");
|
||
186 | } |
||
187 | |||
188 | 26790 | jmvivo | } |
189 | |||
190 | 28046 | jmvivo | private String fixFilter(String filter) { |
191 | if (filter == null) { |
||
192 | return null; |
||
193 | } |
||
194 | |||
195 | // Transform SRS to code
|
||
196 | // GeomFromText\s*\(\s*'[^']*'\s*,\s*('[^']*')\s*\)
|
||
197 | Pattern pattern = Pattern |
||
198 | .compile("GeomFromText\\s*\\(\\s*'[^']*'\\s*,\\s*'([^']*)'\\s*\\)");
|
||
199 | Matcher matcher = pattern.matcher(filter);
|
||
200 | StringBuilder strb = new StringBuilder(); |
||
201 | int pos = 0; |
||
202 | String srsCode;
|
||
203 | while (matcher.find(pos)) {
|
||
204 | strb.append(filter.substring(pos, matcher.start(1)));
|
||
205 | 28047 | jmvivo | srsCode = matcher.group(1).trim();
|
206 | 28046 | jmvivo | if (srsCode.startsWith("'")) { |
207 | srsCode = srsCode.substring(1);
|
||
208 | } |
||
209 | if (srsCode.endsWith("'")) { |
||
210 | srsCode = srsCode.substring(0, srsCode.length() - 1); |
||
211 | } |
||
212 | 28424 | jmvivo | strb.append(helper.getPostgisSRID(srsCode)); |
213 | 28046 | jmvivo | strb.append(filter.substring(matcher.end(1), matcher.end()));
|
214 | pos = matcher.end(); |
||
215 | |||
216 | } |
||
217 | strb.append(filter.substring(pos)); |
||
218 | |||
219 | return strb.toString();
|
||
220 | } |
||
221 | |||
222 | 28424 | jmvivo | public String compoundSelect(FeatureType type, |
223 | String filter,
|
||
224 | 27525 | jmvivo | String order,
|
225 | 28424 | jmvivo | long limit, long offset) throws DataException { |
226 | 27525 | jmvivo | StringBuilder sql = new StringBuilder(); |
227 | if (directSQLMode) {
|
||
228 | if (filter != null || order != null) { |
||
229 | 27595 | jmvivo | // FIXME Exception
|
230 | 27525 | jmvivo | throw new UnsupportedOperationException(); |
231 | } |
||
232 | sql.append(params.getSQL()); |
||
233 | sql.append(' ');
|
||
234 | 26790 | jmvivo | } else {
|
235 | 28424 | jmvivo | FeatureAttributeDescriptor[] fields = type
|
236 | .getAttributeDescriptors(); |
||
237 | 27525 | jmvivo | |
238 | // Select
|
||
239 | 26790 | jmvivo | sql.append("Select ");
|
240 | 27672 | jmvivo | for (int i = 0; i < fields.length - 1; i++) { |
241 | 28424 | jmvivo | sql.append(helper.getSqlFieldName(fields[i])); |
242 | 27672 | jmvivo | sql.append(", ");
|
243 | } |
||
244 | 28424 | jmvivo | sql.append(helper.getSqlFieldName(fields[fields.length - 1]));
|
245 | 27672 | jmvivo | sql.append(' ');
|
246 | 28424 | jmvivo | |
247 | |||
248 | 28672 | jmvivo | FeatureAttributeDescriptor[] pkFields = store
|
249 | .getProviderFeatureType(type.getId()).getPrimaryKey(); |
||
250 | |||
251 | 28424 | jmvivo | if (pkFields != null && pkFields.length > 0) { |
252 | 27672 | jmvivo | // checks for pk fields are in select
|
253 | boolean toAdd;
|
||
254 | for (int i = 0; i < pkFields.length; i++) { |
||
255 | toAdd = true;
|
||
256 | for (int j = 0; j < fields.length; j++) { |
||
257 | 28424 | jmvivo | if (pkFields[i].getName().equals(fields[j].getName())) {
|
258 | 27672 | jmvivo | toAdd = false;
|
259 | break;
|
||
260 | 26790 | jmvivo | } |
261 | 27672 | jmvivo | if (toAdd) {
|
262 | sql.append(", ");
|
||
263 | 28424 | jmvivo | sql.append(helper.getSqlFieldName(pkFields[i])); |
264 | 27672 | jmvivo | } |
265 | 26790 | jmvivo | } |
266 | } |
||
267 | 27672 | jmvivo | sql.append(' ');
|
268 | 26790 | jmvivo | } |
269 | 27525 | jmvivo | |
270 | 27672 | jmvivo | |
271 | 27525 | jmvivo | // table
|
272 | sql.append("from ");
|
||
273 | sql.append(params.tableID()); |
||
274 | sql.append(' ');
|
||
275 | |||
276 | // Where
|
||
277 | appendWhere(sql, filter); |
||
278 | |||
279 | // Order
|
||
280 | if ((params.getInitialOrder() != null && params |
||
281 | .getInitialOrder().length() != 0)
|
||
282 | || (order != null && order.length() != 0)) { |
||
283 | sql.append("order by ");
|
||
284 | |||
285 | if (order != null && order.length() != 0) { |
||
286 | // order
|
||
287 | sql.append(order); |
||
288 | } else {
|
||
289 | // initial order
|
||
290 | sql.append(params.getInitialOrder()); |
||
291 | } |
||
292 | sql.append(' ');
|
||
293 | } |
||
294 | 26790 | jmvivo | } |
295 | 27525 | jmvivo | // limit
|
296 | if (limit >= 1) { |
||
297 | sql.append("limit ");
|
||
298 | sql.append(limit); |
||
299 | sql.append(' ');
|
||
300 | } |
||
301 | 26790 | jmvivo | |
302 | 27525 | jmvivo | // offset
|
303 | if (offset >= 1) { |
||
304 | sql.append("offset ");
|
||
305 | sql.append(offset); |
||
306 | sql.append(' ');
|
||
307 | } |
||
308 | |||
309 | |||
310 | return sql.toString();
|
||
311 | 26790 | jmvivo | } |
312 | |||
313 | public FeatureStoreProvider initialize(FeatureStoreProviderServices store)
|
||
314 | throws InitializeException {
|
||
315 | super.initialize(store);
|
||
316 | this.initFeatureType();
|
||
317 | return this; |
||
318 | } |
||
319 | |||
320 | |||
321 | protected void initFeatureType() throws InitializeException { |
||
322 | 27571 | jmvivo | |
323 | EditableFeatureType edFType = null;
|
||
324 | 26790 | jmvivo | try {
|
325 | 27571 | jmvivo | edFType = this.store.createFeatureType();
|
326 | 26790 | jmvivo | |
327 | 27906 | jmvivo | helper.loadFeatureType(edFType, params); |
328 | |||
329 | 26790 | jmvivo | } catch (DataException e) {
|
330 | throw new InitializeException(this.getName(), e); |
||
331 | } |
||
332 | |||
333 | FeatureType defaultType = edFType.getNotEditableCopy(); |
||
334 | List types = new ArrayList(1); |
||
335 | types.add(defaultType); |
||
336 | this.store.setFeatureTypes(types, defaultType);
|
||
337 | 27982 | jmvivo | try {
|
338 | loadMetadata(); |
||
339 | } catch (DataException e) {
|
||
340 | throw new InitializeException(e); |
||
341 | } |
||
342 | 26790 | jmvivo | } |
343 | |||
344 | 27595 | jmvivo | public Object createNewOID() { |
345 | 26790 | jmvivo | return null; |
346 | } |
||
347 | |||
348 | |||
349 | 27595 | jmvivo | public FeatureData getFeatureDataByReference(
|
350 | FeatureReferenceProviderServices reference) throws DataException {
|
||
351 | return getFeatureDataByReference(reference, store
|
||
352 | .getDefaultFeatureType()); |
||
353 | 26790 | jmvivo | } |
354 | |||
355 | 27595 | jmvivo | public FeatureData getFeatureDataByReference(
|
356 | FeatureReferenceProviderServices reference, FeatureType featureType) |
||
357 | throws DataException {
|
||
358 | open(); |
||
359 | resourceBegin(); |
||
360 | try {
|
||
361 | StringBuilder filter = new StringBuilder(); |
||
362 | 28424 | jmvivo | FeatureAttributeDescriptor[] pk = store.getFeatureType(
|
363 | featureType.getId()).getPrimaryKey(); |
||
364 | 26790 | jmvivo | |
365 | 27595 | jmvivo | List values = new ArrayList(); |
366 | 26790 | jmvivo | |
367 | 28424 | jmvivo | int i;
|
368 | for (i = 0; i < pk.length - 1; i++) { |
||
369 | values.add(helper.dalValueToJDBC(pk[i], reference |
||
370 | .getKeyValue(pk[i].getName()))); |
||
371 | filter.append(helper.getSqlFieldName(pk[i])); |
||
372 | 27595 | jmvivo | filter.append(" = ? AND ");
|
373 | } |
||
374 | 28424 | jmvivo | values.add(helper.dalValueToJDBC(pk[i], reference.getKeyValue(pk[i] |
375 | .getName()))); |
||
376 | filter.append(helper.getSqlFieldName(pk[i])); |
||
377 | 27595 | jmvivo | filter.append(" = ? ");
|
378 | 26790 | jmvivo | |
379 | |||
380 | 28424 | jmvivo | String sql = compoundSelect(featureType, filter.toString(), null, |
381 | 1, 0); |
||
382 | 26790 | jmvivo | |
383 | 28424 | jmvivo | FeatureData data; |
384 | 27595 | jmvivo | int rsId = createResultSet(sql, values.toArray());
|
385 | try{
|
||
386 | 28424 | jmvivo | if (!resulsetNext(rsId)) {
|
387 | // FIXME Exception
|
||
388 | throw new RuntimeException("Reference Not found"); |
||
389 | } |
||
390 | data = createFeatureData(featureType); |
||
391 | 27595 | jmvivo | loadFeatureData(data, rsId); |
392 | } finally {
|
||
393 | closeResulset(rsId); |
||
394 | } |
||
395 | 27525 | jmvivo | |
396 | 27595 | jmvivo | return data;
|
397 | 26790 | jmvivo | |
398 | 27595 | jmvivo | } finally {
|
399 | resourceEnd(); |
||
400 | } |
||
401 | 26790 | jmvivo | |
402 | } |
||
403 | |||
404 | public int getFeatureReferenceOIDType() { |
||
405 | 27525 | jmvivo | return DataTypes.UNKNOWN;
|
406 | 26790 | jmvivo | } |
407 | |||
408 | public String getName() { |
||
409 | return NAME;
|
||
410 | } |
||
411 | |||
412 | public Iterator getChilds() { |
||
413 | return null; |
||
414 | } |
||
415 | |||
416 | public void open() throws OpenException { |
||
417 | 27906 | jmvivo | helper.open(); |
418 | 26790 | jmvivo | } |
419 | |||
420 | |||
421 | public boolean allowWrite() { |
||
422 | 28136 | jmvivo | return false; |
423 | 26790 | jmvivo | } |
424 | |||
425 | public void close() throws CloseException { |
||
426 | 27906 | jmvivo | helper.close(); |
427 | 26790 | jmvivo | } |
428 | |||
429 | 27525 | jmvivo | public Object getSourceId() { |
430 | return this.params.getSourceId(); |
||
431 | } |
||
432 | 26790 | jmvivo | |
433 | 27595 | jmvivo | protected ResultSet createNewResultSet(String sql, Object[] values) |
434 | 27571 | jmvivo | throws DataException {
|
435 | this.open();
|
||
436 | Connection conn =null; |
||
437 | PreparedStatement st=null; |
||
438 | ResultSet rs=null; |
||
439 | 28031 | jmvivo | this.resourceBegin();
|
440 | 27571 | jmvivo | try{
|
441 | 27906 | jmvivo | conn = this.helper.getConnection();
|
442 | 27571 | jmvivo | st = conn.prepareStatement(sql); |
443 | 26790 | jmvivo | |
444 | 27595 | jmvivo | if (values != null) { |
445 | Object value;
|
||
446 | for (int i = 0; i < values.length; i++) { |
||
447 | value = values[i]; |
||
448 | if (value instanceof Geometry) { |
||
449 | byte[] bytes; |
||
450 | try {
|
||
451 | bytes = (byte[]) ((Geometry) value) |
||
452 | .invokeOperation(ToWKB.CODE, null);
|
||
453 | } catch (BaseException e) {
|
||
454 | // FIXME
|
||
455 | throw new InvalidParameterException(); |
||
456 | } |
||
457 | 27672 | jmvivo | st.setBytes(i + 1, bytes);
|
458 | 27595 | jmvivo | } |
459 | 27672 | jmvivo | st.setObject(i + 1, value);
|
460 | 27595 | jmvivo | } |
461 | |||
462 | } |
||
463 | |||
464 | 27953 | jmvivo | try {
|
465 | 27571 | jmvivo | rs = st.executeQuery(); |
466 | } catch (SQLException e1){ |
||
467 | try {st.close(); } catch (Exception e2) { }; |
||
468 | try {conn.close();} catch (Exception e2) { }; |
||
469 | throw new JDBCExecuteSQLException(sql,e1); |
||
470 | } |
||
471 | rs.setFetchSize(5000); // TODO add to params? |
||
472 | return rs;
|
||
473 | } catch (SQLException e) { |
||
474 | // TODO throw exception ???
|
||
475 | try {rs.close(); } catch (Exception e1) { }; |
||
476 | try {st.close(); } catch (Exception e1) { }; |
||
477 | try {conn.close();} catch (Exception e1) { }; |
||
478 | throw new JDBCSQLException(e); |
||
479 | }finally{
|
||
480 | this.resourceEnd();
|
||
481 | } |
||
482 | } |
||
483 | 27525 | jmvivo | |
484 | 27571 | jmvivo | protected long getCount(String filter) throws DataException { |
485 | this.open();
|
||
486 | if (filter == null && totalCount != null) { |
||
487 | return totalCount.longValue();
|
||
488 | } |
||
489 | long count = 0; |
||
490 | String sql = compoundCountSelect(filter);
|
||
491 | resourceBegin(); |
||
492 | try {
|
||
493 | 27595 | jmvivo | ResultSet rs = createNewResultSet(sql, null); |
494 | 27571 | jmvivo | try {
|
495 | if (rs.next()) {
|
||
496 | count = rs.getLong(1);
|
||
497 | } |
||
498 | } catch (SQLException e) { |
||
499 | throw new JDBCSQLException(e); |
||
500 | } finally {
|
||
501 | closeResulset(rs); |
||
502 | } |
||
503 | } finally {
|
||
504 | resourceEnd(); |
||
505 | } |
||
506 | if (filter == null) { |
||
507 | totalCount = new Long(count); |
||
508 | } |
||
509 | return count;
|
||
510 | } |
||
511 | |||
512 | protected void resourceBegin() throws ResourceBeginException { |
||
513 | 27906 | jmvivo | this.helper.begin();
|
514 | 27571 | jmvivo | |
515 | } |
||
516 | |||
517 | protected void resourceEnd() { |
||
518 | 27906 | jmvivo | this.helper.end();
|
519 | 27571 | jmvivo | } |
520 | |||
521 | 28257 | jmvivo | protected boolean closeResource(ResourceProvider resource) { |
522 | try {
|
||
523 | this.helper.close();
|
||
524 | } catch (CloseException e) {
|
||
525 | logger.error("Exception in close Request", e);
|
||
526 | } |
||
527 | return !this.helper.isOpen(); |
||
528 | 27571 | jmvivo | } |
529 | |||
530 | |||
531 | 27595 | jmvivo | protected void loadFeatureDataValue(FeatureData data, ResultSet rs, |
532 | FeatureAttributeDescriptor attr) throws DataException {
|
||
533 | if (attr.getDataType() == DataTypes.GEOMETRY) {
|
||
534 | 28424 | jmvivo | byte[] buffer; |
535 | 27595 | jmvivo | try {
|
536 | 28424 | jmvivo | buffer = rs.getBytes(attr.getIndex() + 1);
|
537 | if (buffer == null) { |
||
538 | 27595 | jmvivo | data.set(attr.getIndex(), null);
|
539 | } else {
|
||
540 | 27982 | jmvivo | data.set(attr.getIndex(), this.helper
|
541 | 28424 | jmvivo | .getGeometry(buffer)); |
542 | 27595 | jmvivo | } |
543 | } catch (SQLException e) { |
||
544 | throw new JDBCSQLException(e); |
||
545 | 28424 | jmvivo | } catch (BaseException e) {
|
546 | 27595 | jmvivo | throw new ReadException(getName(), e); |
547 | } |
||
548 | 27571 | jmvivo | |
549 | 27595 | jmvivo | } else {
|
550 | super.loadFeatureDataValue(data, rs, attr);
|
||
551 | } |
||
552 | } |
||
553 | |||
554 | public FeatureSetProvider createSet(FeatureQuery query,
|
||
555 | FeatureType featureType) throws DataException {
|
||
556 | |||
557 | return new PostgreSQLSetProvider(this, query, featureType); |
||
558 | } |
||
559 | |||
560 | 27672 | jmvivo | public void dispose() throws CloseException { |
561 | this.close();
|
||
562 | 27906 | jmvivo | this.helper.dispose();
|
563 | 27672 | jmvivo | super.dispose();
|
564 | } |
||
565 | |||
566 | 27595 | jmvivo | |
567 | 27906 | jmvivo | public DataServerExplorer getExplorer() throws ReadException { |
568 | DataManager manager = DALLocator.getDataManager(); |
||
569 | PostgreSQLServerExplorerParameters exParams; |
||
570 | try {
|
||
571 | exParams = (PostgreSQLServerExplorerParameters) manager |
||
572 | .createServerExplorerParameters(PostgreSQLServerExplorer.NAME); |
||
573 | exParams.setHost(params.getHost()); |
||
574 | exParams.setPort(params.getPort()); |
||
575 | 27908 | jmvivo | exParams.setDBName(params.getDBName()); |
576 | 27906 | jmvivo | exParams.setUser(params.getUser()); |
577 | exParams.setPassword(params.getPassword()); |
||
578 | exParams.setCatalog(params.getCatalog()); |
||
579 | exParams.setSchema(params.getSchema()); |
||
580 | exParams.setJDBCDriverClassName(params.getJDBCDriverClassName()); |
||
581 | exParams.setUseSSL(params.getUseSSL()); |
||
582 | 27595 | jmvivo | |
583 | 27906 | jmvivo | return manager.createServerExplorer(exParams);
|
584 | } catch (DataException e) {
|
||
585 | throw new ReadException(this.getName(), e); |
||
586 | } catch (ValidateDataParametersException e) {
|
||
587 | // TODO Auto-generated catch block
|
||
588 | throw new ReadException(this.getName(), e); |
||
589 | } |
||
590 | } |
||
591 | 27595 | jmvivo | |
592 | 28676 | jmvivo | protected void loadMetadata() throws DataException { |
593 | 27982 | jmvivo | IProjection srs = params.getSRS(); |
594 | 27595 | jmvivo | |
595 | 27982 | jmvivo | if (srs == null) { |
596 | srs = store.getDefaultFeatureType().getDefaultSRS(); |
||
597 | } |
||
598 | 27906 | jmvivo | |
599 | |||
600 | 27982 | jmvivo | this.dynObject.setDynValue("DefaultSRS", srs); |
601 | 27906 | jmvivo | |
602 | 27982 | jmvivo | String defGeomName = this.store.getDefaultFeatureType() |
603 | .getDefaultGeometryAttributeName(); |
||
604 | Envelope env = null;
|
||
605 | if (defGeomName != null && defGeomName.length() > 0) { |
||
606 | env = this.helper
|
||
607 | .getFullEnvelopeOfField(this.params,
|
||
608 | defGeomName, |
||
609 | this.params.getWorkingArea());
|
||
610 | |||
611 | } |
||
612 | this.dynObject.setDynValue("Envelope", env); |
||
613 | |||
614 | } |
||
615 | |||
616 | private void clearMetadata() { |
||
617 | this.dynObject.setDynValue("DefaultSRS", null); |
||
618 | this.dynObject.setDynValue("Envelope", null); |
||
619 | } |
||
620 | |||
621 | public void closeDone() throws DataException { |
||
622 | clearMetadata(); |
||
623 | |||
624 | } |
||
625 | |||
626 | public void opendDone() throws DataException { |
||
627 | // Nothing to do
|
||
628 | } |
||
629 | |||
630 | public Envelope getEnvelope() throws DataException { |
||
631 | this.open();
|
||
632 | return (Envelope) this.dynObject.getDynValue("Envelope"); |
||
633 | } |
||
634 | |||
635 | 27595 | jmvivo | public void resourceChanged(ResourceProvider resource) { |
636 | 28136 | jmvivo | this.store.notifyChange(DataStoreNotification.RESOURCE_CHANGED,
|
637 | resource); |
||
638 | 27595 | jmvivo | } |
639 | |||
640 | public boolean canWriteGeometry(int geometryType) throws DataException { |
||
641 | 28136 | jmvivo | return false; |
642 | } |
||
643 | |||
644 | public boolean allowAutomaticValues() { |
||
645 | 27595 | jmvivo | return true; |
646 | } |
||
647 | |||
648 | 27906 | jmvivo | public void performEditing(Iterator deleteds, Iterator inserteds, |
649 | 28136 | jmvivo | Iterator updateds, Iterator originalFeatureTypesUpdated) |
650 | throws PerformEditingException {
|
||
651 | // FIXME exception
|
||
652 | throw new UnsupportedOperationException(); |
||
653 | 27906 | jmvivo | |
654 | } |
||
655 | |||
656 | 28424 | jmvivo | protected PostgreSQLHelper getHelper() {
|
657 | return helper;
|
||
658 | } |
||
659 | |||
660 | 28661 | jmvivo | protected void resetCount() { |
661 | totalCount = null;
|
||
662 | } |
||
663 | |||
664 | 28676 | jmvivo | public long getFeatureCount() throws DataException { |
665 | return getCount(null); |
||
666 | } |
||
667 | 27595 | jmvivo | |
668 | public boolean supportsAppendMode() { |
||
669 | return false; |
||
670 | } |
||
671 | |||
672 | |||
673 | 28676 | jmvivo | public void endAppend() throws DataException { |
674 | // FIXME exception
|
||
675 | throw new UnsupportedOperationException(); |
||
676 | 27595 | jmvivo | |
677 | } |
||
678 | |||
679 | 28676 | jmvivo | public void append(FeatureData featureData) throws DataException { |
680 | // FIXME exception
|
||
681 | throw new UnsupportedOperationException(); |
||
682 | 27595 | jmvivo | |
683 | } |
||
684 | |||
685 | 28676 | jmvivo | public void beginAppend() throws DataException { |
686 | // FIXME exception
|
||
687 | throw new UnsupportedOperationException(); |
||
688 | } |
||
689 | 27595 | jmvivo | |
690 | 28676 | jmvivo | // ************************************************************************************//
|
691 | 27595 | jmvivo | |
692 | |||
693 | 28676 | jmvivo | // ************************************************************************************//
|
694 | 27595 | jmvivo | |
695 | 28676 | jmvivo | |
696 | |||
697 | public PersistentState getState() throws PersistenceException { |
||
698 | 27595 | jmvivo | // TODO Auto-generated method stub
|
699 | 28676 | jmvivo | return null; |
700 | 27595 | jmvivo | } |
701 | |||
702 | 28676 | jmvivo | public void loadState(PersistentState state) throws PersistenceException { |
703 | 27595 | jmvivo | // TODO Auto-generated method stub
|
704 | |||
705 | } |
||
706 | |||
707 | 28676 | jmvivo | public void loadFromState(PersistentState state) |
708 | throws PersistenceException {
|
||
709 | // TODO Auto-generated method stub
|
||
710 | |||
711 | 28017 | jmvivo | } |
712 | |||
713 | 28095 | jpiera | /* (non-Javadoc)
|
714 | * @see org.gvsig.tools.persistence.Persistent#saveToState(org.gvsig.tools.persistence.PersistentState)
|
||
715 | */
|
||
716 | public void saveToState(PersistentState state) throws PersistenceException { |
||
717 | // TODO Auto-generated method stub
|
||
718 | 28257 | jmvivo | |
719 | 28095 | jpiera | } |
720 | |||
721 | 26790 | jmvivo | } |