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