svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / feature / impl / DefaultFeatureQuery.java @ 43558
History | View | Annotate | Download (18.3 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 |
package org.gvsig.fmap.dal.feature.impl; |
25 |
|
26 |
import java.util.ArrayList; |
27 |
import java.util.HashMap; |
28 |
import java.util.List; |
29 |
import java.util.Map; |
30 |
import java.util.logging.Level; |
31 |
import java.util.logging.Logger; |
32 |
import org.apache.commons.lang3.ArrayUtils; |
33 |
import org.gvsig.fmap.dal.DALLocator; |
34 |
import org.gvsig.fmap.dal.DataTypes; |
35 |
import org.gvsig.fmap.dal.exception.DataException; |
36 |
import org.gvsig.fmap.dal.exception.InitializeException; |
37 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
38 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
39 |
import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
40 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
41 |
import org.gvsig.fmap.dal.feature.FeatureType; |
42 |
import org.gvsig.tools.ToolsLocator; |
43 |
import org.gvsig.tools.dynobject.DynStruct; |
44 |
import org.gvsig.tools.evaluator.AndEvaluator; |
45 |
import org.gvsig.tools.evaluator.Evaluator; |
46 |
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo; |
47 |
import org.gvsig.tools.persistence.PersistentState; |
48 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
49 |
|
50 |
/**
|
51 |
* Defines the properties of a collection of Features, as a result of a query
|
52 |
* through a FeatureStore.
|
53 |
* <p>
|
54 |
* A FeatureQuery is always defined by a FeatureType, or by the list of
|
55 |
* attribute names of the FeatureStore to return.
|
56 |
* </p>
|
57 |
* <p>
|
58 |
* The filter allows to select Features whose properties have values with the
|
59 |
* characteristics defined by the filter.
|
60 |
* </p>
|
61 |
* <p>
|
62 |
* The order is used to set the order of the result FeatureCollection, based on
|
63 |
* the values of the properties of the Features.
|
64 |
* </p>
|
65 |
* <p>
|
66 |
* The scale parameter can be used by the FeatureStore as a hint about the
|
67 |
* quality or resolution of the data needed to view or operate with the data
|
68 |
* returned. As an example, the FeatureStore may use the scale to return only a
|
69 |
* representative subset of the data, or maybe to return Features with less
|
70 |
* detail, like a point or a line instead of a polygon.
|
71 |
* </p>
|
72 |
* <p>
|
73 |
* If an implementation of FeatureStore is able to get other parameters to
|
74 |
* customize the behavior of the getDataCollection methods, there is an option
|
75 |
* to set more parameters through the setAttribute method.
|
76 |
* </p>
|
77 |
*
|
78 |
* @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
|
79 |
*/
|
80 |
public class DefaultFeatureQuery implements FeatureQuery { |
81 |
|
82 |
public static final String SCALE_PARAM_NAME = "Scale"; |
83 |
|
84 |
private Map queryParameters = new HashMap(); |
85 |
|
86 |
private String featureTypeId = null; |
87 |
|
88 |
private List attributeNames = new ArrayList(); |
89 |
|
90 |
private List constantsAttributeNames = new ArrayList(); |
91 |
|
92 |
private Evaluator filter;
|
93 |
|
94 |
/**
|
95 |
* This boolean value is used to know if the current filter
|
96 |
* has been added using the {@link FeatureQuery#addFilter(Evaluator)}
|
97 |
* method or the {@link FeatureQuery#setFilter(Evaluator)} method.
|
98 |
*/
|
99 |
// private boolean isAddFilter = true;
|
100 |
|
101 |
private FeatureQueryOrder order = new FeatureQueryOrder(); |
102 |
|
103 |
private long limit; |
104 |
|
105 |
private long pageSize; |
106 |
|
107 |
/**
|
108 |
* Creates a FeatureQuery which will load all available Features of a type.
|
109 |
*
|
110 |
*/
|
111 |
public DefaultFeatureQuery() {
|
112 |
super();
|
113 |
} |
114 |
|
115 |
/**
|
116 |
* Creates a FeatureQuery which will load all available Features of a type.
|
117 |
*
|
118 |
* @param featureType
|
119 |
* the type of Features of the query
|
120 |
*/
|
121 |
public DefaultFeatureQuery(FeatureType featureType) {
|
122 |
super();
|
123 |
this.setFeatureType(featureType);
|
124 |
} |
125 |
|
126 |
/**
|
127 |
* Creates a FeatureQuery with the type of features, a filter and the order
|
128 |
* for the FeatureCollection.
|
129 |
*
|
130 |
* @param featureType
|
131 |
* the type of Features of the query
|
132 |
* @param filter
|
133 |
* based on the properties of the Features
|
134 |
*/
|
135 |
public DefaultFeatureQuery(FeatureType featureType, Evaluator filter) {
|
136 |
super();
|
137 |
this.setFeatureType(featureType);
|
138 |
this.filter = filter;
|
139 |
} |
140 |
|
141 |
/**
|
142 |
* Creates a FeatureQuery with the type of features, a filter, the order for
|
143 |
* the FeatureCollection and the view scale.
|
144 |
*
|
145 |
* @param featureType
|
146 |
* the type of Features of the query
|
147 |
* @param filter
|
148 |
* based on the properties of the Features
|
149 |
* @param scale
|
150 |
* to view the Features.
|
151 |
*/
|
152 |
public DefaultFeatureQuery(FeatureType featureType, Evaluator filter,
|
153 |
double scale) {
|
154 |
this.setFeatureType(featureType);
|
155 |
this.filter = filter;
|
156 |
this.setScale(scale);
|
157 |
} |
158 |
|
159 |
/**
|
160 |
* Creates a FeatureQuery which will load a list of attribute names of all
|
161 |
* available Features.
|
162 |
*
|
163 |
* @param attributeNames
|
164 |
* the list of attribute names to load
|
165 |
*/
|
166 |
public DefaultFeatureQuery(String[] attributeNames) { |
167 |
super();
|
168 |
setAttributeNames(attributeNames); |
169 |
} |
170 |
|
171 |
/**
|
172 |
* Creates a FeatureQuery with the list of attribute names of feature, a
|
173 |
* filter and the order for the FeatureCollection.
|
174 |
*
|
175 |
* @param attributeNames
|
176 |
* the list of attribute names to load
|
177 |
* @param filter
|
178 |
* based on the properties of the Features
|
179 |
*/
|
180 |
public DefaultFeatureQuery(String[] attributeNames, Evaluator filter) { |
181 |
super();
|
182 |
setAttributeNames(attributeNames); |
183 |
this.filter = filter;
|
184 |
} |
185 |
|
186 |
/**
|
187 |
* Creates a FeatureQuery with the list of attribute names of feature, a
|
188 |
* filter, the order for the FeatureCollection and the view scale.
|
189 |
*
|
190 |
* @param attributeNames
|
191 |
* the list of attribute names to load
|
192 |
* @param filter
|
193 |
* based on the properties of the Features
|
194 |
* @param scale
|
195 |
* to view the Features.
|
196 |
*/
|
197 |
public DefaultFeatureQuery(String[] attributeNames, Evaluator filter, |
198 |
double scale) {
|
199 |
setAttributeNames(attributeNames); |
200 |
this.filter = filter;
|
201 |
this.setScale(scale);
|
202 |
} |
203 |
|
204 |
@Override
|
205 |
public double getScale() { |
206 |
Double scale = (Double) this.getQueryParameter(SCALE_PARAM_NAME); |
207 |
if (scale == null) { |
208 |
return 0; |
209 |
} |
210 |
return scale;
|
211 |
} |
212 |
|
213 |
@Override
|
214 |
public void setScale(double scale) { |
215 |
this.setQueryParameter(SCALE_PARAM_NAME, scale);
|
216 |
} |
217 |
|
218 |
@Override
|
219 |
public Object getQueryParameter(String name) { |
220 |
return queryParameters.get(name);
|
221 |
} |
222 |
|
223 |
@Override
|
224 |
public void setQueryParameter(String name, Object value) { |
225 |
queryParameters.put(name, value); |
226 |
} |
227 |
|
228 |
@Override
|
229 |
public void setFeatureType(FeatureType featureType) { |
230 |
this.featureTypeId = featureType.getId();
|
231 |
} |
232 |
|
233 |
@Override
|
234 |
public String[] getAttributeNames() { |
235 |
return (String[])attributeNames.toArray(new String[attributeNames.size()]); |
236 |
} |
237 |
|
238 |
@Override
|
239 |
public void setAttributeNames(String[] attributeNames) { |
240 |
this.attributeNames.clear();
|
241 |
if (attributeNames != null){ |
242 |
for (int i=0 ; i<attributeNames.length ; i++){ |
243 |
this.attributeNames.add(attributeNames[i]);
|
244 |
} |
245 |
} |
246 |
} |
247 |
|
248 |
@Override
|
249 |
public void retrievesAllAttributes() { |
250 |
this.attributeNames.clear();
|
251 |
} |
252 |
|
253 |
@Override
|
254 |
public void addAttributeName(String attributeName){ |
255 |
//If the attribute exists finish the method
|
256 |
for (int i=0 ; i<attributeNames.size() ; i++){ |
257 |
if (attributeNames.get(i).equals(attributeName)){
|
258 |
return;
|
259 |
} |
260 |
} |
261 |
this.attributeNames.add(attributeName);
|
262 |
} |
263 |
|
264 |
@Override
|
265 |
public void addEssentialAttributeNames(FeatureStore store) { |
266 |
FeatureType storeType; |
267 |
try {
|
268 |
storeType = store.getDefaultFeatureType(); |
269 |
} catch (DataException ex) {
|
270 |
throw new RuntimeException("Can't access to the default feature type of tghe store", ex); |
271 |
} |
272 |
FeatureAttributeDescriptor[] pks = storeType.getPrimaryKey();
|
273 |
if( storeType.hasOID() || ArrayUtils.isEmpty(pks) ) {
|
274 |
FeatureAttributeDescriptor attrInt = null;
|
275 |
FeatureAttributeDescriptor attrStr = null;
|
276 |
FeatureAttributeDescriptor attrNotGeom = null;
|
277 |
for (FeatureAttributeDescriptor attr : storeType) {
|
278 |
if( attrInt == null && (attr.getType()==DataTypes.INT || attr.getType()==DataTypes.LONG) ) { |
279 |
attrInt = attr; |
280 |
} else if( attrStr == null && attr.getType()==DataTypes.STRING ) { |
281 |
attrStr = attr; |
282 |
} else if( attrNotGeom == null && attr.getType()!=DataTypes.GEOMETRY ) { |
283 |
attrNotGeom = attr; |
284 |
} |
285 |
} |
286 |
if( attrInt != null ) { |
287 |
this.addAttributeName(attrInt.getName());
|
288 |
} else if( attrStr != null ) { |
289 |
this.addAttributeName(attrStr.getName());
|
290 |
} else if( attrNotGeom != null ) { |
291 |
this.addAttributeName(attrNotGeom.getName());
|
292 |
} else {
|
293 |
this.addAttributeName(storeType.getAttributeDescriptor(0).getName()); |
294 |
} |
295 |
} else {
|
296 |
for(FeatureAttributeDescriptor attr : pks ) {
|
297 |
this.addAttributeName(attr.getName());
|
298 |
} |
299 |
} |
300 |
} |
301 |
|
302 |
@Override
|
303 |
public void addPrimaryKeyAttributeNames(FeatureStore store) { |
304 |
FeatureType storeType; |
305 |
try {
|
306 |
storeType = store.getDefaultFeatureType(); |
307 |
} catch (DataException ex) {
|
308 |
throw new RuntimeException("Can't access to the default feature type of tghe store", ex); |
309 |
} |
310 |
for(FeatureAttributeDescriptor attr : storeType.getPrimaryKey() ) {
|
311 |
this.addAttributeName(attr.getName());
|
312 |
} |
313 |
} |
314 |
|
315 |
@Override
|
316 |
public boolean hasAttributeNames() { |
317 |
return !this.attributeNames.isEmpty(); |
318 |
} |
319 |
|
320 |
@Override
|
321 |
public void clearAttributeNames() { |
322 |
this.attributeNames = new ArrayList(); |
323 |
} |
324 |
|
325 |
@Override
|
326 |
public Evaluator getFilter() {
|
327 |
return filter;
|
328 |
} |
329 |
|
330 |
@Override
|
331 |
public void setFilter(String filter) { |
332 |
try {
|
333 |
this.setFilter(DALLocator.getDataManager().createExpresion(filter));
|
334 |
} catch (Exception ex) { |
335 |
throw new RuntimeException("Can't create filter from '"+filter+"'",ex); |
336 |
} |
337 |
} |
338 |
|
339 |
@Override
|
340 |
public void setFilter(Evaluator filter) { |
341 |
this.filter = filter;
|
342 |
addFilterAttributes(filter); |
343 |
// isAddFilter = false;
|
344 |
} |
345 |
|
346 |
@Override
|
347 |
public void addFilter(String filter) { |
348 |
try {
|
349 |
this.addFilter(DALLocator.getDataManager().createExpresion(filter));
|
350 |
} catch (Exception ex) { |
351 |
throw new RuntimeException("Can't create filter from '"+filter+"'",ex); |
352 |
} |
353 |
} |
354 |
|
355 |
@Override
|
356 |
public void addFilter(Evaluator evaluator) { |
357 |
if( evaluator == null ) { |
358 |
return;
|
359 |
} |
360 |
// if (isAddFilter){
|
361 |
if (this.filter == null){ |
362 |
this.filter = evaluator;
|
363 |
}else{
|
364 |
if (evaluator != null){ |
365 |
if (this.filter instanceof AndEvaluator){ |
366 |
((AndEvaluator)this.filter).addEvaluator(evaluator);
|
367 |
}else{
|
368 |
this.filter = new AndEvaluator(this.filter); |
369 |
((AndEvaluator)this.filter).addEvaluator(evaluator);
|
370 |
} |
371 |
} |
372 |
} |
373 |
// }else{
|
374 |
// this.filter = evaluator;
|
375 |
// }
|
376 |
addFilterAttributes(evaluator); |
377 |
// isAddFilter = true;
|
378 |
} |
379 |
|
380 |
@Override
|
381 |
public void clearFilter() { |
382 |
this.filter = null; |
383 |
} |
384 |
|
385 |
private void addFilterAttributes(Evaluator evaluator){ |
386 |
if (evaluator != null){ |
387 |
EvaluatorFieldsInfo fieldsInfo = evaluator.getFieldsInfo(); |
388 |
if (fieldsInfo == null){ |
389 |
// FieldsInfo is not available in this evaluator
|
390 |
return;
|
391 |
} |
392 |
String[] fieldNames = fieldsInfo.getFieldNames(); |
393 |
if (fieldNames== null){ |
394 |
// fieldNames is not available in this evaluator
|
395 |
return;
|
396 |
} |
397 |
|
398 |
for (int i=0 ; i<fieldNames.length ; i++){ |
399 |
addAttributeName(fieldNames[i]); |
400 |
} |
401 |
} |
402 |
} |
403 |
|
404 |
@Override
|
405 |
public FeatureQueryOrder getOrder() {
|
406 |
return order;
|
407 |
} |
408 |
|
409 |
@Override
|
410 |
public void setOrder(FeatureQueryOrder order) { |
411 |
this.order = order;
|
412 |
} |
413 |
|
414 |
@Override
|
415 |
public boolean hasFilter() { |
416 |
return this.filter != null; |
417 |
} |
418 |
|
419 |
@Override
|
420 |
public boolean hasOrder() { |
421 |
return this.order != null && this.order.size() > 0; |
422 |
} |
423 |
|
424 |
@Override
|
425 |
public Object clone() throws CloneNotSupportedException { |
426 |
DefaultFeatureQuery clone = (DefaultFeatureQuery) super.clone();
|
427 |
|
428 |
// Clone attribute names array
|
429 |
if (attributeNames != null) { |
430 |
clone.attributeNames = new ArrayList(); |
431 |
for (int i=0 ; i<attributeNames.size() ; i++){ |
432 |
clone.attributeNames.add(attributeNames.get(i)); |
433 |
} |
434 |
} |
435 |
|
436 |
// Clone order
|
437 |
if (order != null) { |
438 |
clone.order = (FeatureQueryOrder) order.clone(); |
439 |
} |
440 |
|
441 |
return clone;
|
442 |
} |
443 |
|
444 |
@Override
|
445 |
public FeatureQuery getCopy() {
|
446 |
try {
|
447 |
return (FeatureQuery) clone();
|
448 |
} catch (CloneNotSupportedException e) { |
449 |
// TODO Auto-generated catch block
|
450 |
e.printStackTrace(); |
451 |
return null; |
452 |
} |
453 |
// DefaultFeatureQuery aCopy = new DefaultFeatureQuery();
|
454 |
//
|
455 |
// aCopy.featureTypeId = this.featureTypeId;
|
456 |
//
|
457 |
// if (this.attributeNames != null) {
|
458 |
// aCopy.attributeNames = (String[]) Arrays
|
459 |
// .asList(this.attributeNames).toArray(new String[0]);
|
460 |
// }
|
461 |
//
|
462 |
// aCopy.filter = this.filter;
|
463 |
//
|
464 |
// if (this.order != null) {
|
465 |
// aCopy.order = this.order.getCopy();
|
466 |
// }
|
467 |
//
|
468 |
// return aCopy;
|
469 |
} |
470 |
|
471 |
@Override
|
472 |
public String getFeatureTypeId() { |
473 |
return featureTypeId;
|
474 |
} |
475 |
|
476 |
@Override
|
477 |
public void setFeatureTypeId(String featureTypeId) { |
478 |
this.featureTypeId = featureTypeId;
|
479 |
} |
480 |
|
481 |
@Override
|
482 |
public void saveToState(PersistentState state) throws PersistenceException { |
483 |
// FIXME: falta por terminar de implementar
|
484 |
state.set("queryParameters", this.queryParameters); |
485 |
state.set("featureTypeId", this.featureTypeId); |
486 |
state.set("attributeNames", this.attributeNames); |
487 |
// state.set("filter", this.filter);
|
488 |
state.set("limit", this.limit); |
489 |
state.set("pageSize", this.pageSize); |
490 |
|
491 |
} |
492 |
|
493 |
@Override
|
494 |
public void loadFromState(PersistentState state) throws PersistenceException { |
495 |
// FIXME: falta por terminar de implementar
|
496 |
this.queryParameters = (Map) state.get("queryParameters"); |
497 |
this.featureTypeId = state.getString("featureTypeId"); |
498 |
this.attributeNames = state.getList("attributeNames"); |
499 |
this.filter = (Evaluator) state.get("filter"); |
500 |
this.limit = state.getLong("limit"); |
501 |
this.pageSize = state.getLong("pageSize"); |
502 |
|
503 |
} |
504 |
|
505 |
/**
|
506 |
* Register the class on PersistenceManager
|
507 |
*
|
508 |
*/
|
509 |
public static void registerPersistent() { |
510 |
DynStruct definition = |
511 |
ToolsLocator.getPersistenceManager() |
512 |
.addDefinition(DefaultFeatureQuery.class, |
513 |
"DefaultFeatureQuery",
|
514 |
"DefaultFeatureQuery Persistent definition",
|
515 |
null,
|
516 |
null);
|
517 |
|
518 |
definition.addDynFieldMap("queryParameters")
|
519 |
.setClassOfItems(Object.class)
|
520 |
.setMandatory(true);
|
521 |
|
522 |
definition.addDynFieldString("featureTypeId").setMandatory(false); |
523 |
|
524 |
definition.addDynFieldList("attributeNames")
|
525 |
.setClassOfItems(String.class)
|
526 |
.setMandatory(false);
|
527 |
|
528 |
definition.addDynFieldObject("filter")
|
529 |
.setClassOfValue(Evaluator.class) |
530 |
.setMandatory(false);
|
531 |
|
532 |
definition.addDynFieldObject("order")
|
533 |
.setClassOfValue(FeatureQueryOrder.class) |
534 |
.setMandatory(false);
|
535 |
|
536 |
definition.addDynFieldLong("limit").setMandatory(false); |
537 |
|
538 |
definition.addDynFieldLong("pageSize").setMandatory(false); |
539 |
|
540 |
} |
541 |
|
542 |
@Override
|
543 |
public long getLimit() { |
544 |
return limit;
|
545 |
} |
546 |
|
547 |
@Override
|
548 |
public long getPageSize() { |
549 |
return pageSize;
|
550 |
} |
551 |
|
552 |
@Override
|
553 |
public void setLimit(long limit) { |
554 |
this.limit = limit;
|
555 |
} |
556 |
|
557 |
@Override
|
558 |
public void setPageSize(long pageSize) { |
559 |
this.pageSize = pageSize;
|
560 |
} |
561 |
|
562 |
@Override
|
563 |
public String[] getConstantsAttributeNames() { |
564 |
return (String[])constantsAttributeNames.toArray(new String[constantsAttributeNames.size()]); |
565 |
} |
566 |
|
567 |
@Override
|
568 |
public void setConstantsAttributeNames(String[] constantsAttributeNames) { |
569 |
this.constantsAttributeNames.clear();
|
570 |
if (constantsAttributeNames != null){ |
571 |
for (int i=0 ; i<constantsAttributeNames.length ; i++){ |
572 |
this.constantsAttributeNames.add(constantsAttributeNames[i]);
|
573 |
} |
574 |
} |
575 |
} |
576 |
|
577 |
@Override
|
578 |
public void addConstantAttributeName(String attributeName) { |
579 |
//If the attribute exists finish the method
|
580 |
for (int i=0 ; i<constantsAttributeNames.size() ; i++){ |
581 |
if (constantsAttributeNames.get(i).equals(attributeName)){
|
582 |
return;
|
583 |
} |
584 |
} |
585 |
this.constantsAttributeNames.add(attributeName);
|
586 |
} |
587 |
|
588 |
@Override
|
589 |
public boolean hasConstantsAttributeNames() { |
590 |
return !this.constantsAttributeNames.isEmpty(); |
591 |
} |
592 |
|
593 |
@Override
|
594 |
public void clearConstantsAttributeNames() { |
595 |
this.constantsAttributeNames = new ArrayList(); |
596 |
} |
597 |
|
598 |
} |