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 @ 45023
History | View | Annotate | Download (25.6 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
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 | 40559 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * 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 | 40559 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
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 | 43358 | jjdelcerro | import org.apache.commons.lang3.ArrayUtils; |
31 | 43913 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
32 | 44023 | jjdelcerro | import org.gvsig.expressionevaluator.Expression; |
33 | 44712 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionUtils; |
34 | 44829 | omartinez | import org.gvsig.fmap.dal.DALLocator; |
35 | import org.gvsig.fmap.dal.DataManager; |
||
36 | 43358 | jjdelcerro | import org.gvsig.fmap.dal.DataTypes; |
37 | import org.gvsig.fmap.dal.exception.DataException; |
||
38 | 44829 | omartinez | import org.gvsig.fmap.dal.exception.InitializeException; |
39 | 44712 | jjdelcerro | import org.gvsig.fmap.dal.expressionevaluator.ExpressionEvaluator; |
40 | 43358 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
41 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureQuery; |
42 | import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
||
43 | 43358 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureStore; |
44 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureType; |
45 | 44023 | jjdelcerro | import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultExpressionEvaluator; |
46 | 40435 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
47 | import org.gvsig.tools.dynobject.DynStruct; |
||
48 | import org.gvsig.tools.evaluator.AndEvaluator; |
||
49 | import org.gvsig.tools.evaluator.Evaluator; |
||
50 | import org.gvsig.tools.evaluator.EvaluatorFieldsInfo; |
||
51 | import org.gvsig.tools.persistence.PersistentState; |
||
52 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
53 | 44829 | omartinez | import org.slf4j.Logger; |
54 | import org.slf4j.LoggerFactory; |
||
55 | import org.gvsig.fmap.dal.feature.FeatureExtraColumns; |
||
56 | 40435 | jjdelcerro | |
57 | /**
|
||
58 | * Defines the properties of a collection of Features, as a result of a query
|
||
59 | * through a FeatureStore.
|
||
60 | * <p>
|
||
61 | * A FeatureQuery is always defined by a FeatureType, or by the list of
|
||
62 | * attribute names of the FeatureStore to return.
|
||
63 | * </p>
|
||
64 | * <p>
|
||
65 | * The filter allows to select Features whose properties have values with the
|
||
66 | * characteristics defined by the filter.
|
||
67 | * </p>
|
||
68 | * <p>
|
||
69 | * The order is used to set the order of the result FeatureCollection, based on
|
||
70 | * the values of the properties of the Features.
|
||
71 | * </p>
|
||
72 | * <p>
|
||
73 | * The scale parameter can be used by the FeatureStore as a hint about the
|
||
74 | * quality or resolution of the data needed to view or operate with the data
|
||
75 | * returned. As an example, the FeatureStore may use the scale to return only a
|
||
76 | * representative subset of the data, or maybe to return Features with less
|
||
77 | * detail, like a point or a line instead of a polygon.
|
||
78 | * </p>
|
||
79 | * <p>
|
||
80 | * If an implementation of FeatureStore is able to get other parameters to
|
||
81 | * customize the behavior of the getDataCollection methods, there is an option
|
||
82 | * to set more parameters through the setAttribute method.
|
||
83 | * </p>
|
||
84 | 42975 | jjdelcerro | *
|
85 | 40435 | jjdelcerro | * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
|
86 | */
|
||
87 | public class DefaultFeatureQuery implements FeatureQuery { |
||
88 | 44829 | omartinez | |
89 | private static final Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureQuery.class); |
||
90 | 40435 | jjdelcerro | |
91 | public static final String SCALE_PARAM_NAME = "Scale"; |
||
92 | |||
93 | 44712 | jjdelcerro | private Map<String,Object> queryParameters = new HashMap(); |
94 | 40435 | jjdelcerro | |
95 | private String featureTypeId = null; |
||
96 | |||
97 | 44712 | jjdelcerro | private List<String> attributeNames = new ArrayList(); |
98 | 40435 | jjdelcerro | |
99 | 44712 | jjdelcerro | private List<String> constantsAttributeNames = new ArrayList(); |
100 | 41212 | jjdelcerro | |
101 | 40435 | jjdelcerro | private Evaluator filter;
|
102 | |||
103 | private FeatureQueryOrder order = new FeatureQueryOrder(); |
||
104 | |||
105 | private long limit; |
||
106 | |||
107 | private long pageSize; |
||
108 | |||
109 | 44712 | jjdelcerro | private List<String> groupByColumns; |
110 | |||
111 | private Map<String,String> aggregateFunctions; |
||
112 | |||
113 | 44829 | omartinez | private FeatureExtraColumns extraColumn = new DefaultFeatureExtraColumns(); |
114 | 44754 | omartinez | |
115 | 40435 | jjdelcerro | /**
|
116 | * Creates a FeatureQuery which will load all available Features of a type.
|
||
117 | 42975 | jjdelcerro | *
|
118 | 40435 | jjdelcerro | */
|
119 | public DefaultFeatureQuery() {
|
||
120 | super();
|
||
121 | } |
||
122 | |||
123 | /**
|
||
124 | * Creates a FeatureQuery which will load all available Features of a type.
|
||
125 | 42975 | jjdelcerro | *
|
126 | 40435 | jjdelcerro | * @param featureType
|
127 | * the type of Features of the query
|
||
128 | */
|
||
129 | public DefaultFeatureQuery(FeatureType featureType) {
|
||
130 | super();
|
||
131 | this.setFeatureType(featureType);
|
||
132 | } |
||
133 | |||
134 | /**
|
||
135 | * Creates a FeatureQuery with the type of features, a filter and the order
|
||
136 | * for the FeatureCollection.
|
||
137 | 42975 | jjdelcerro | *
|
138 | 40435 | jjdelcerro | * @param featureType
|
139 | * the type of Features of the query
|
||
140 | * @param filter
|
||
141 | * based on the properties of the Features
|
||
142 | */
|
||
143 | public DefaultFeatureQuery(FeatureType featureType, Evaluator filter) {
|
||
144 | super();
|
||
145 | this.setFeatureType(featureType);
|
||
146 | this.filter = filter;
|
||
147 | } |
||
148 | |||
149 | /**
|
||
150 | * Creates a FeatureQuery with the type of features, a filter, the order for
|
||
151 | * the FeatureCollection and the view scale.
|
||
152 | 42975 | jjdelcerro | *
|
153 | 40435 | jjdelcerro | * @param featureType
|
154 | * the type of Features of the query
|
||
155 | * @param filter
|
||
156 | * based on the properties of the Features
|
||
157 | * @param scale
|
||
158 | * to view the Features.
|
||
159 | */
|
||
160 | public DefaultFeatureQuery(FeatureType featureType, Evaluator filter,
|
||
161 | double scale) {
|
||
162 | this.setFeatureType(featureType);
|
||
163 | this.filter = filter;
|
||
164 | this.setScale(scale);
|
||
165 | } |
||
166 | |||
167 | /**
|
||
168 | * Creates a FeatureQuery which will load a list of attribute names of all
|
||
169 | * available Features.
|
||
170 | 42975 | jjdelcerro | *
|
171 | 40435 | jjdelcerro | * @param attributeNames
|
172 | * the list of attribute names to load
|
||
173 | */
|
||
174 | public DefaultFeatureQuery(String[] attributeNames) { |
||
175 | super();
|
||
176 | setAttributeNames(attributeNames); |
||
177 | } |
||
178 | |||
179 | /**
|
||
180 | * Creates a FeatureQuery with the list of attribute names of feature, a
|
||
181 | * filter and the order for the FeatureCollection.
|
||
182 | 42975 | jjdelcerro | *
|
183 | 40435 | jjdelcerro | * @param attributeNames
|
184 | * the list of attribute names to load
|
||
185 | * @param filter
|
||
186 | * based on the properties of the Features
|
||
187 | */
|
||
188 | public DefaultFeatureQuery(String[] attributeNames, Evaluator filter) { |
||
189 | super();
|
||
190 | setAttributeNames(attributeNames); |
||
191 | this.filter = filter;
|
||
192 | } |
||
193 | |||
194 | /**
|
||
195 | * Creates a FeatureQuery with the list of attribute names of feature, a
|
||
196 | * filter, the order for the FeatureCollection and the view scale.
|
||
197 | 42975 | jjdelcerro | *
|
198 | 40435 | jjdelcerro | * @param attributeNames
|
199 | * the list of attribute names to load
|
||
200 | * @param filter
|
||
201 | * based on the properties of the Features
|
||
202 | * @param scale
|
||
203 | * to view the Features.
|
||
204 | */
|
||
205 | public DefaultFeatureQuery(String[] attributeNames, Evaluator filter, |
||
206 | double scale) {
|
||
207 | setAttributeNames(attributeNames); |
||
208 | this.filter = filter;
|
||
209 | this.setScale(scale);
|
||
210 | } |
||
211 | |||
212 | 43358 | jjdelcerro | @Override
|
213 | 40435 | jjdelcerro | public double getScale() { |
214 | Double scale = (Double) this.getQueryParameter(SCALE_PARAM_NAME); |
||
215 | if (scale == null) { |
||
216 | return 0; |
||
217 | } |
||
218 | 43358 | jjdelcerro | return scale;
|
219 | 40435 | jjdelcerro | } |
220 | |||
221 | 43358 | jjdelcerro | @Override
|
222 | 40435 | jjdelcerro | public void setScale(double scale) { |
223 | 43358 | jjdelcerro | this.setQueryParameter(SCALE_PARAM_NAME, scale);
|
224 | 40435 | jjdelcerro | } |
225 | |||
226 | 43358 | jjdelcerro | @Override
|
227 | 40435 | jjdelcerro | public Object getQueryParameter(String name) { |
228 | return queryParameters.get(name);
|
||
229 | } |
||
230 | |||
231 | 43358 | jjdelcerro | @Override
|
232 | 40435 | jjdelcerro | public void setQueryParameter(String name, Object value) { |
233 | queryParameters.put(name, value); |
||
234 | } |
||
235 | |||
236 | 43358 | jjdelcerro | @Override
|
237 | 40435 | jjdelcerro | public void setFeatureType(FeatureType featureType) { |
238 | this.featureTypeId = featureType.getId();
|
||
239 | } |
||
240 | |||
241 | 43358 | jjdelcerro | @Override
|
242 | 40435 | jjdelcerro | public String[] getAttributeNames() { |
243 | return (String[])attributeNames.toArray(new String[attributeNames.size()]); |
||
244 | } |
||
245 | |||
246 | 43358 | jjdelcerro | @Override
|
247 | 40435 | jjdelcerro | public void setAttributeNames(String[] attributeNames) { |
248 | this.attributeNames.clear();
|
||
249 | if (attributeNames != null){ |
||
250 | for (int i=0 ; i<attributeNames.length ; i++){ |
||
251 | this.attributeNames.add(attributeNames[i]);
|
||
252 | 42975 | jjdelcerro | } |
253 | 40435 | jjdelcerro | } |
254 | } |
||
255 | 42975 | jjdelcerro | |
256 | 43358 | jjdelcerro | @Override
|
257 | 43558 | jjdelcerro | public void retrievesAllAttributes() { |
258 | this.attributeNames.clear();
|
||
259 | } |
||
260 | |||
261 | @Override
|
||
262 | 40435 | jjdelcerro | public void addAttributeName(String attributeName){ |
263 | //If the attribute exists finish the method
|
||
264 | for (int i=0 ; i<attributeNames.size() ; i++){ |
||
265 | if (attributeNames.get(i).equals(attributeName)){
|
||
266 | return;
|
||
267 | 42975 | jjdelcerro | } |
268 | } |
||
269 | 40435 | jjdelcerro | this.attributeNames.add(attributeName);
|
270 | } |
||
271 | |||
272 | 43358 | jjdelcerro | @Override
|
273 | public void addEssentialAttributeNames(FeatureStore store) { |
||
274 | FeatureType storeType; |
||
275 | try {
|
||
276 | storeType = store.getDefaultFeatureType(); |
||
277 | } catch (DataException ex) {
|
||
278 | throw new RuntimeException("Can't access to the default feature type of tghe store", ex); |
||
279 | } |
||
280 | FeatureAttributeDescriptor[] pks = storeType.getPrimaryKey();
|
||
281 | if( storeType.hasOID() || ArrayUtils.isEmpty(pks) ) {
|
||
282 | FeatureAttributeDescriptor attrInt = null;
|
||
283 | FeatureAttributeDescriptor attrStr = null;
|
||
284 | FeatureAttributeDescriptor attrNotGeom = null;
|
||
285 | for (FeatureAttributeDescriptor attr : storeType) {
|
||
286 | if( attrInt == null && (attr.getType()==DataTypes.INT || attr.getType()==DataTypes.LONG) ) { |
||
287 | attrInt = attr; |
||
288 | } else if( attrStr == null && attr.getType()==DataTypes.STRING ) { |
||
289 | attrStr = attr; |
||
290 | } else if( attrNotGeom == null && attr.getType()!=DataTypes.GEOMETRY ) { |
||
291 | attrNotGeom = attr; |
||
292 | } |
||
293 | } |
||
294 | if( attrInt != null ) { |
||
295 | this.addAttributeName(attrInt.getName());
|
||
296 | } else if( attrStr != null ) { |
||
297 | this.addAttributeName(attrStr.getName());
|
||
298 | } else if( attrNotGeom != null ) { |
||
299 | this.addAttributeName(attrNotGeom.getName());
|
||
300 | } else {
|
||
301 | this.addAttributeName(storeType.getAttributeDescriptor(0).getName()); |
||
302 | } |
||
303 | } else {
|
||
304 | for(FeatureAttributeDescriptor attr : pks ) {
|
||
305 | this.addAttributeName(attr.getName());
|
||
306 | } |
||
307 | } |
||
308 | } |
||
309 | |||
310 | @Override
|
||
311 | public void addPrimaryKeyAttributeNames(FeatureStore store) { |
||
312 | FeatureType storeType; |
||
313 | try {
|
||
314 | storeType = store.getDefaultFeatureType(); |
||
315 | } catch (DataException ex) {
|
||
316 | throw new RuntimeException("Can't access to the default feature type of tghe store", ex); |
||
317 | } |
||
318 | for(FeatureAttributeDescriptor attr : storeType.getPrimaryKey() ) {
|
||
319 | this.addAttributeName(attr.getName());
|
||
320 | } |
||
321 | } |
||
322 | |||
323 | @Override
|
||
324 | 41212 | jjdelcerro | public boolean hasAttributeNames() { |
325 | return !this.attributeNames.isEmpty(); |
||
326 | } |
||
327 | |||
328 | 43358 | jjdelcerro | @Override
|
329 | 41212 | jjdelcerro | public void clearAttributeNames() { |
330 | this.attributeNames = new ArrayList(); |
||
331 | } |
||
332 | |||
333 | 43358 | jjdelcerro | @Override
|
334 | 40435 | jjdelcerro | public Evaluator getFilter() {
|
335 | return filter;
|
||
336 | } |
||
337 | 42975 | jjdelcerro | |
338 | 42971 | jjdelcerro | @Override
|
339 | 44712 | jjdelcerro | public Expression getExpressionFilter() { |
340 | if( this.filter instanceof ExpressionEvaluator ) { |
||
341 | return ((ExpressionEvaluator)this.filter).getExpression(); |
||
342 | } |
||
343 | return null; |
||
344 | } |
||
345 | |||
346 | @Override
|
||
347 | 44023 | jjdelcerro | public void setFilter(Expression filter) { |
348 | 44259 | jjdelcerro | if( filter == null ) { |
349 | this.clearFilter();
|
||
350 | return;
|
||
351 | } |
||
352 | 44023 | jjdelcerro | Evaluator x = new DefaultExpressionEvaluator(filter);
|
353 | this.setFilter(x);
|
||
354 | } |
||
355 | |||
356 | @Override
|
||
357 | 42971 | jjdelcerro | public void setFilter(String filter) { |
358 | 43913 | jjdelcerro | if( StringUtils.isEmpty(filter) ) {
|
359 | this.clearFilter();
|
||
360 | return;
|
||
361 | } |
||
362 | 42971 | jjdelcerro | try {
|
363 | 44712 | jjdelcerro | this.setFilter(ExpressionUtils.createExpression(filter));
|
364 | 42971 | jjdelcerro | } catch (Exception ex) { |
365 | throw new RuntimeException("Can't create filter from '"+filter+"'",ex); |
||
366 | } |
||
367 | } |
||
368 | 40435 | jjdelcerro | |
369 | 43358 | jjdelcerro | @Override
|
370 | 40435 | jjdelcerro | public void setFilter(Evaluator filter) { |
371 | 43913 | jjdelcerro | if( filter == null ) { |
372 | this.clearFilter();
|
||
373 | return;
|
||
374 | } |
||
375 | 40435 | jjdelcerro | this.filter = filter;
|
376 | addFilterAttributes(filter); |
||
377 | } |
||
378 | |||
379 | 42795 | jjdelcerro | @Override
|
380 | 42971 | jjdelcerro | public void addFilter(String filter) { |
381 | 43913 | jjdelcerro | if( StringUtils.isEmpty(filter) ) {
|
382 | return;
|
||
383 | } |
||
384 | 44712 | jjdelcerro | this.addFilter(ExpressionUtils.createExpression(filter));
|
385 | 42971 | jjdelcerro | } |
386 | 42975 | jjdelcerro | |
387 | 42971 | jjdelcerro | @Override
|
388 | 44023 | jjdelcerro | public void addFilter(Expression filter) { |
389 | Evaluator x = new DefaultExpressionEvaluator(filter);
|
||
390 | this.addFilter(x);
|
||
391 | } |
||
392 | |||
393 | @Override
|
||
394 | 40435 | jjdelcerro | public void addFilter(Evaluator evaluator) { |
395 | 43913 | jjdelcerro | if (evaluator == null) { |
396 | 42795 | jjdelcerro | return;
|
397 | } |
||
398 | 43913 | jjdelcerro | if (this.filter == null) { |
399 | this.filter = evaluator;
|
||
400 | } else {
|
||
401 | if (this.filter instanceof AndEvaluator) { |
||
402 | ((AndEvaluator) this.filter).addEvaluator(evaluator);
|
||
403 | } else {
|
||
404 | this.filter = new AndEvaluator(this.filter); |
||
405 | ((AndEvaluator) this.filter).addEvaluator(evaluator);
|
||
406 | 40435 | jjdelcerro | } |
407 | 43913 | jjdelcerro | } |
408 | 40435 | jjdelcerro | addFilterAttributes(evaluator); |
409 | } |
||
410 | 42975 | jjdelcerro | |
411 | 43358 | jjdelcerro | @Override
|
412 | 42975 | jjdelcerro | public void clearFilter() { |
413 | this.filter = null; |
||
414 | } |
||
415 | |||
416 | 40435 | jjdelcerro | private void addFilterAttributes(Evaluator evaluator){ |
417 | if (evaluator != null){ |
||
418 | EvaluatorFieldsInfo fieldsInfo = evaluator.getFieldsInfo(); |
||
419 | if (fieldsInfo == null){ |
||
420 | // FieldsInfo is not available in this evaluator
|
||
421 | return;
|
||
422 | } |
||
423 | String[] fieldNames = fieldsInfo.getFieldNames(); |
||
424 | if (fieldNames== null){ |
||
425 | // fieldNames is not available in this evaluator
|
||
426 | return;
|
||
427 | } |
||
428 | 42975 | jjdelcerro | |
429 | 40435 | jjdelcerro | for (int i=0 ; i<fieldNames.length ; i++){ |
430 | addAttributeName(fieldNames[i]); |
||
431 | } |
||
432 | } |
||
433 | } |
||
434 | |||
435 | 43358 | jjdelcerro | @Override
|
436 | 40435 | jjdelcerro | public FeatureQueryOrder getOrder() {
|
437 | return order;
|
||
438 | } |
||
439 | |||
440 | 43358 | jjdelcerro | @Override
|
441 | 40435 | jjdelcerro | public void setOrder(FeatureQueryOrder order) { |
442 | this.order = order;
|
||
443 | } |
||
444 | |||
445 | 43358 | jjdelcerro | @Override
|
446 | 40435 | jjdelcerro | public boolean hasFilter() { |
447 | return this.filter != null; |
||
448 | } |
||
449 | |||
450 | 43358 | jjdelcerro | @Override
|
451 | 40435 | jjdelcerro | public boolean hasOrder() { |
452 | return this.order != null && this.order.size() > 0; |
||
453 | } |
||
454 | |||
455 | 43358 | jjdelcerro | @Override
|
456 | 40435 | jjdelcerro | public Object clone() throws CloneNotSupportedException { |
457 | DefaultFeatureQuery clone = (DefaultFeatureQuery) super.clone();
|
||
458 | |||
459 | // Clone attribute names array
|
||
460 | if (attributeNames != null) { |
||
461 | clone.attributeNames = new ArrayList(); |
||
462 | for (int i=0 ; i<attributeNames.size() ; i++){ |
||
463 | clone.attributeNames.add(attributeNames.get(i)); |
||
464 | 42975 | jjdelcerro | } |
465 | 40435 | jjdelcerro | } |
466 | |||
467 | // Clone order
|
||
468 | if (order != null) { |
||
469 | clone.order = (FeatureQueryOrder) order.clone(); |
||
470 | } |
||
471 | 44801 | omartinez | |
472 | clone.extraColumn = extraColumn.getCopy(); |
||
473 | 44829 | omartinez | |
474 | if( this.filter instanceof ExpressionEvaluator ) { |
||
475 | Expression exp = ((ExpressionEvaluator)this.filter).getExpression(); |
||
476 | clone.filter = new DefaultExpressionEvaluator(exp);
|
||
477 | } |
||
478 | 40435 | jjdelcerro | return clone;
|
479 | } |
||
480 | |||
481 | 43358 | jjdelcerro | @Override
|
482 | 40435 | jjdelcerro | public FeatureQuery getCopy() {
|
483 | try {
|
||
484 | return (FeatureQuery) clone();
|
||
485 | } catch (CloneNotSupportedException e) { |
||
486 | // TODO Auto-generated catch block
|
||
487 | e.printStackTrace(); |
||
488 | return null; |
||
489 | } |
||
490 | // DefaultFeatureQuery aCopy = new DefaultFeatureQuery();
|
||
491 | //
|
||
492 | // aCopy.featureTypeId = this.featureTypeId;
|
||
493 | //
|
||
494 | // if (this.attributeNames != null) {
|
||
495 | // aCopy.attributeNames = (String[]) Arrays
|
||
496 | // .asList(this.attributeNames).toArray(new String[0]);
|
||
497 | // }
|
||
498 | //
|
||
499 | // aCopy.filter = this.filter;
|
||
500 | //
|
||
501 | // if (this.order != null) {
|
||
502 | // aCopy.order = this.order.getCopy();
|
||
503 | // }
|
||
504 | //
|
||
505 | // return aCopy;
|
||
506 | } |
||
507 | |||
508 | 43358 | jjdelcerro | @Override
|
509 | 40435 | jjdelcerro | public String getFeatureTypeId() { |
510 | return featureTypeId;
|
||
511 | } |
||
512 | |||
513 | 43358 | jjdelcerro | @Override
|
514 | 40435 | jjdelcerro | public void setFeatureTypeId(String featureTypeId) { |
515 | this.featureTypeId = featureTypeId;
|
||
516 | } |
||
517 | |||
518 | 43358 | jjdelcerro | @Override
|
519 | 40435 | jjdelcerro | public void saveToState(PersistentState state) throws PersistenceException { |
520 | // FIXME: falta por terminar de implementar
|
||
521 | state.set("queryParameters", this.queryParameters); |
||
522 | state.set("featureTypeId", this.featureTypeId); |
||
523 | state.set("attributeNames", this.attributeNames); |
||
524 | 44829 | omartinez | |
525 | ArrayList<Expression> filterList = new ArrayList<>(); |
||
526 | if (this.filter instanceof DefaultExpressionEvaluator) { |
||
527 | DefaultExpressionEvaluator filterExpression = (DefaultExpressionEvaluator) this.filter;
|
||
528 | filterList.add(filterExpression.getExpression()); |
||
529 | } else if (this.filter instanceof AndEvaluator) { |
||
530 | AndEvaluator filterAnd = (AndEvaluator) this.filter;
|
||
531 | List<Evaluator> evaluators = filterAnd.getEvaluators();
|
||
532 | for (Evaluator evaluator : evaluators) {
|
||
533 | if (evaluator instanceof DefaultExpressionEvaluator) { |
||
534 | DefaultExpressionEvaluator expressionEvaluator = (DefaultExpressionEvaluator) evaluator; |
||
535 | filterList.add(expressionEvaluator.getExpression()); |
||
536 | } else {
|
||
537 | filterList.clear(); |
||
538 | LOGGER.warn(StringUtils.join("Filters in this FeatureQuery will not persist:", this.toString())); |
||
539 | break;
|
||
540 | } |
||
541 | } |
||
542 | } else {
|
||
543 | filterList.clear(); |
||
544 | LOGGER.warn(StringUtils.join("Filters in this FeatureQuery will not persist:", this.toString())); |
||
545 | } |
||
546 | |||
547 | state.set("filter", filterList);
|
||
548 | 40435 | jjdelcerro | state.set("limit", this.limit); |
549 | state.set("pageSize", this.pageSize); |
||
550 | 44829 | omartinez | |
551 | state.set("order", this.order); |
||
552 | state.set("groupByColumns", this.groupByColumns); |
||
553 | state.set("aggregateFunctions", this.aggregateFunctions); |
||
554 | state.set("extraColumn", this.extraColumn); |
||
555 | |||
556 | 40435 | jjdelcerro | |
557 | } |
||
558 | 44829 | omartinez | |
559 | 40435 | jjdelcerro | |
560 | 43358 | jjdelcerro | @Override
|
561 | 40435 | jjdelcerro | public void loadFromState(PersistentState state) throws PersistenceException { |
562 | // FIXME: falta por terminar de implementar
|
||
563 | this.queryParameters = (Map) state.get("queryParameters"); |
||
564 | this.featureTypeId = state.getString("featureTypeId"); |
||
565 | this.attributeNames = state.getList("attributeNames"); |
||
566 | 44829 | omartinez | List<Expression> filterList = state.getList("filter"); |
567 | String stateFilter = ""; |
||
568 | DataManager dataManager = DALLocator.getDataManager(); |
||
569 | if (filterList.size() == 0) { |
||
570 | this.filter = null; |
||
571 | } else if (filterList.size() == 1) { |
||
572 | Expression expression = filterList.get(0); |
||
573 | Evaluator evaluator; |
||
574 | try {
|
||
575 | evaluator = dataManager.createFilter(expression); |
||
576 | } catch (InitializeException ex) {
|
||
577 | LOGGER.warn("Can't create evaluator", ex);
|
||
578 | evaluator = null;
|
||
579 | } |
||
580 | this.filter = evaluator;
|
||
581 | } else {
|
||
582 | AndEvaluator andEvaluator = null;
|
||
583 | for (Expression expression : filterList) { |
||
584 | Evaluator evaluator; |
||
585 | try {
|
||
586 | evaluator = dataManager.createFilter(expression); |
||
587 | |||
588 | if (andEvaluator == null) { |
||
589 | andEvaluator = new AndEvaluator(evaluator);
|
||
590 | } else {
|
||
591 | andEvaluator.addEvaluator(evaluator); |
||
592 | } |
||
593 | } catch (InitializeException ex) {
|
||
594 | LOGGER.warn("Can't create AndEvaluator", ex);//TODO evaluator a null |
||
595 | break;
|
||
596 | } |
||
597 | this.filter = evaluator;
|
||
598 | |||
599 | } |
||
600 | } |
||
601 | 40435 | jjdelcerro | this.limit = state.getLong("limit"); |
602 | this.pageSize = state.getLong("pageSize"); |
||
603 | 44829 | omartinez | |
604 | |||
605 | this.order = (FeatureQueryOrder) state.get("order"); |
||
606 | this.groupByColumns = (List) state.getList("groupByColumns"); |
||
607 | this.aggregateFunctions = (Map) state.getMap("aggregateFunctions"); |
||
608 | this.extraColumn = (FeatureExtraColumns) state.get("extraColumn"); |
||
609 | 40435 | jjdelcerro | |
610 | 44829 | omartinez | |
611 | 40435 | jjdelcerro | } |
612 | |||
613 | /**
|
||
614 | * Register the class on PersistenceManager
|
||
615 | 42975 | jjdelcerro | *
|
616 | 40435 | jjdelcerro | */
|
617 | public static void registerPersistent() { |
||
618 | DynStruct definition = |
||
619 | ToolsLocator.getPersistenceManager() |
||
620 | .addDefinition(DefaultFeatureQuery.class, |
||
621 | "DefaultFeatureQuery",
|
||
622 | "DefaultFeatureQuery Persistent definition",
|
||
623 | null,
|
||
624 | null);
|
||
625 | |||
626 | definition.addDynFieldMap("queryParameters")
|
||
627 | 44829 | omartinez | .setClassOfItems(Object.class)
|
628 | .setMandatory(true);
|
||
629 | 40435 | jjdelcerro | |
630 | definition.addDynFieldString("featureTypeId").setMandatory(false); |
||
631 | |||
632 | definition.addDynFieldList("attributeNames")
|
||
633 | 44829 | omartinez | .setClassOfItems(String.class)
|
634 | .setMandatory(false);
|
||
635 | 40435 | jjdelcerro | |
636 | 44829 | omartinez | definition.addDynFieldList("filter")
|
637 | .setClassOfItems(Expression.class)
|
||
638 | .setMandatory(false);
|
||
639 | 40435 | jjdelcerro | |
640 | definition.addDynFieldObject("order")
|
||
641 | 44829 | omartinez | .setClassOfValue(FeatureQueryOrder.class) |
642 | .setMandatory(false);
|
||
643 | 40435 | jjdelcerro | |
644 | definition.addDynFieldLong("limit").setMandatory(false); |
||
645 | |||
646 | definition.addDynFieldLong("pageSize").setMandatory(false); |
||
647 | 44829 | omartinez | |
648 | |||
649 | definition.addDynFieldList("groupByColumns")
|
||
650 | .setClassOfItems(String.class);
|
||
651 | 40435 | jjdelcerro | |
652 | 44829 | omartinez | definition.addDynFieldMap("aggregateFunctions")
|
653 | .setClassOfItems(String.class)
|
||
654 | .setClassOfValue(String.class);
|
||
655 | |||
656 | definition.addDynFieldObject("extraColumn")
|
||
657 | .setClassOfValue(DefaultFeatureExtraColumns.class); |
||
658 | |||
659 | |||
660 | 40435 | jjdelcerro | } |
661 | |||
662 | 43358 | jjdelcerro | @Override
|
663 | 40435 | jjdelcerro | public long getLimit() { |
664 | return limit;
|
||
665 | } |
||
666 | |||
667 | 43358 | jjdelcerro | @Override
|
668 | 40435 | jjdelcerro | public long getPageSize() { |
669 | return pageSize;
|
||
670 | } |
||
671 | |||
672 | 43358 | jjdelcerro | @Override
|
673 | 40435 | jjdelcerro | public void setLimit(long limit) { |
674 | this.limit = limit;
|
||
675 | } |
||
676 | |||
677 | 43358 | jjdelcerro | @Override
|
678 | 40435 | jjdelcerro | public void setPageSize(long pageSize) { |
679 | this.pageSize = pageSize;
|
||
680 | } |
||
681 | |||
682 | 43358 | jjdelcerro | @Override
|
683 | 41212 | jjdelcerro | public String[] getConstantsAttributeNames() { |
684 | return (String[])constantsAttributeNames.toArray(new String[constantsAttributeNames.size()]); |
||
685 | } |
||
686 | |||
687 | 43358 | jjdelcerro | @Override
|
688 | 41212 | jjdelcerro | public void setConstantsAttributeNames(String[] constantsAttributeNames) { |
689 | this.constantsAttributeNames.clear();
|
||
690 | if (constantsAttributeNames != null){ |
||
691 | for (int i=0 ; i<constantsAttributeNames.length ; i++){ |
||
692 | this.constantsAttributeNames.add(constantsAttributeNames[i]);
|
||
693 | 42975 | jjdelcerro | } |
694 | 41212 | jjdelcerro | } |
695 | } |
||
696 | 42975 | jjdelcerro | |
697 | 43358 | jjdelcerro | @Override
|
698 | 41212 | jjdelcerro | public void addConstantAttributeName(String attributeName) { |
699 | //If the attribute exists finish the method
|
||
700 | for (int i=0 ; i<constantsAttributeNames.size() ; i++){ |
||
701 | if (constantsAttributeNames.get(i).equals(attributeName)){
|
||
702 | return;
|
||
703 | 42975 | jjdelcerro | } |
704 | } |
||
705 | 41212 | jjdelcerro | this.constantsAttributeNames.add(attributeName);
|
706 | } |
||
707 | |||
708 | 43358 | jjdelcerro | @Override
|
709 | 41212 | jjdelcerro | public boolean hasConstantsAttributeNames() { |
710 | return !this.constantsAttributeNames.isEmpty(); |
||
711 | } |
||
712 | |||
713 | 43358 | jjdelcerro | @Override
|
714 | 41212 | jjdelcerro | public void clearConstantsAttributeNames() { |
715 | this.constantsAttributeNames = new ArrayList(); |
||
716 | } |
||
717 | |||
718 | 44712 | jjdelcerro | @Override
|
719 | 44727 | jjdelcerro | public boolean isAGroupByColumn(String name) { |
720 | for (String columnName : groupByColumns) { |
||
721 | if( StringUtils.equalsIgnoreCase(name, columnName) ) {
|
||
722 | return true; |
||
723 | } |
||
724 | } |
||
725 | return false; |
||
726 | } |
||
727 | |||
728 | @Override
|
||
729 | 44712 | jjdelcerro | public List<String> getGroupByColumns() { |
730 | if( this.groupByColumns == null ) { |
||
731 | this.groupByColumns = new ArrayList<>(); |
||
732 | 44374 | jjdelcerro | } |
733 | 44712 | jjdelcerro | return this.groupByColumns; |
734 | } |
||
735 | 44374 | jjdelcerro | |
736 | 44712 | jjdelcerro | @Override
|
737 | public Map<String, String> getAggregateFunctions() { |
||
738 | if( this.aggregateFunctions == null ) { |
||
739 | this.aggregateFunctions = new HashMap<>(); |
||
740 | 44374 | jjdelcerro | } |
741 | 44712 | jjdelcerro | return this.aggregateFunctions; |
742 | } |
||
743 | 44374 | jjdelcerro | |
744 | 44712 | jjdelcerro | @Override
|
745 | 44727 | jjdelcerro | public String getAggregate(String name) { |
746 | String fn = this.aggregateFunctions.get(name); |
||
747 | if( StringUtils.isAlphanumeric(fn) ) {
|
||
748 | return fn + "(\""+ name + "\")"; |
||
749 | } |
||
750 | return fn;
|
||
751 | } |
||
752 | |||
753 | @Override
|
||
754 | 44712 | jjdelcerro | public boolean hasAggregateFunctions() { |
755 | return this.aggregateFunctions == null || this.aggregateFunctions.isEmpty(); |
||
756 | } |
||
757 | |||
758 | @Override
|
||
759 | public boolean hasGroupByColumns() { |
||
760 | return this.groupByColumns != null && !this.groupByColumns.isEmpty(); |
||
761 | } |
||
762 | |||
763 | @Override
|
||
764 | public void copyFrom(FeatureQuery query) { |
||
765 | DefaultFeatureQuery other = (DefaultFeatureQuery) query; |
||
766 | 44829 | omartinez | this.queryParameters = new HashMap(); |
767 | 44712 | jjdelcerro | this.queryParameters.putAll(other.queryParameters);
|
768 | |||
769 | this.featureTypeId = other.featureTypeId;
|
||
770 | |||
771 | this.attributeNames.clear();
|
||
772 | this.attributeNames.addAll(other.attributeNames);
|
||
773 | |||
774 | this.constantsAttributeNames.clear();
|
||
775 | this.constantsAttributeNames.addAll(other.constantsAttributeNames);
|
||
776 | |||
777 | this.filter = other.filter;
|
||
778 | |||
779 | this.order.copyFrom(other.order);
|
||
780 | |||
781 | this.limit = other.limit;
|
||
782 | |||
783 | this.pageSize = other.pageSize;
|
||
784 | |||
785 | if( this.groupByColumns!=null && other.groupByColumns!=null ) { |
||
786 | this.groupByColumns.clear();
|
||
787 | this.groupByColumns.addAll(other.groupByColumns);
|
||
788 | } else if( other.groupByColumns!=null ) { |
||
789 | this.groupByColumns = new ArrayList<>(); |
||
790 | this.groupByColumns.addAll(other.groupByColumns);
|
||
791 | } else if( this.groupByColumns!=null ) { |
||
792 | this.groupByColumns = null; |
||
793 | } |
||
794 | |||
795 | 44765 | omartinez | if( this.aggregateFunctions!=null && other.aggregateFunctions!=null ) { |
796 | 44712 | jjdelcerro | this.aggregateFunctions.clear();
|
797 | this.aggregateFunctions.putAll(other.aggregateFunctions);
|
||
798 | 44765 | omartinez | } else if( other.aggregateFunctions!=null ) { |
799 | 44712 | jjdelcerro | this.aggregateFunctions = new HashMap<>(); |
800 | this.aggregateFunctions.putAll(other.aggregateFunctions);
|
||
801 | 44765 | omartinez | } else if( this.aggregateFunctions!=null ) { |
802 | this.aggregateFunctions=null; |
||
803 | 44712 | jjdelcerro | } |
804 | 44791 | omartinez | this.extraColumn.copyFrom(other.extraColumn);
|
805 | 44712 | jjdelcerro | } |
806 | |||
807 | 44829 | omartinez | public FeatureExtraColumns getExtraColumn() {
|
808 | 44754 | omartinez | return this.extraColumn; |
809 | } |
||
810 | |||
811 | 40435 | jjdelcerro | } |