Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeatureQuery.java @ 33281

History | View | Annotate | Download (10.7 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
* MA  02110-1301, USA.
20
*
21
*/
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 {DiSiD Technologies}  {Create Parameter object to define FeatureCollections queries}
26
 */
27
package org.gvsig.fmap.dal.feature.impl;
28

    
29
import java.util.Arrays;
30
import java.util.HashMap;
31
import java.util.Iterator;
32
import java.util.List;
33
import java.util.Map;
34

    
35
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
36
import org.gvsig.fmap.dal.feature.FeatureQuery;
37
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
38
import org.gvsig.fmap.dal.feature.FeatureType;
39
import org.gvsig.tools.ToolsLocator;
40
import org.gvsig.tools.dynobject.DynStruct;
41
import org.gvsig.tools.evaluator.Evaluator;
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 String[] attributeNames;
84

    
85
    private Evaluator filter;
86

    
87
    private FeatureQueryOrder order = new FeatureQueryOrder();
88

    
89
        private long limit;
90

    
91
        private long pageSize;
92

    
93
    /**
94
     * Creates a FeatureQuery which will load all available Features of a type.
95
     *
96
     * @param featureType
97
     *            the type of Features of the query
98
     */
99
    public DefaultFeatureQuery() {
100
        super();
101
    }
102

    
103
        /**
104
         * Creates a FeatureQuery which will load all available Features of a type.
105
         *
106
         * @param featureType
107
         *            the type of Features of the query
108
         */
109
   public DefaultFeatureQuery(FeatureType featureType) {
110
       super();
111
       this.setFeatureType(featureType);
112
   }
113

    
114
    /**
115
     * Creates a FeatureQuery with the type of features, a filter and the order
116
     * for the FeatureCollection.
117
     *
118
     * @param featureType
119
     *            the type of Features of the query
120
     * @param filter
121
     *            based on the properties of the Features
122
     * @param order
123
     *            for the result
124
     */
125
    public DefaultFeatureQuery(FeatureType featureType, Evaluator filter) {
126
        super();
127
        this.setFeatureType(featureType);
128
        this.filter = filter;
129
    }
130

    
131
    /**
132
     * Creates a FeatureQuery with the type of features, a filter, the order for
133
     * the FeatureCollection and the view scale.
134
     *
135
     * @param featureType
136
     *            the type of Features of the query
137
     * @param filter
138
     *            based on the properties of the Features
139
     * @param order
140
     *            for the result
141
     * @param scale
142
     *            to view the Features.
143
     */
144
    public DefaultFeatureQuery(FeatureType featureType, Evaluator filter,
145
            double scale) {
146
        this.setFeatureType(featureType);
147
        this.filter = filter;
148
        this.setScale(scale);
149
    }
150

    
151
        /**
152
         * Creates a FeatureQuery which will load a list of attribute names of all
153
         * available Features.
154
         *
155
         * @param attributeNames
156
         *            the list of attribute names to load
157
         */
158
    public DefaultFeatureQuery(String[] attributeNames) {
159
        super();
160
        this.attributeNames = attributeNames;
161
    }
162

    
163
        /**
164
         * Creates a FeatureQuery with the list of attribute names of feature, a
165
         * filter and the order for the FeatureCollection.
166
         *
167
         * @param attributeNames
168
         *            the list of attribute names to load
169
         * @param filter
170
         *            based on the properties of the Features
171
         * @param order
172
         *            for the result
173
         */
174
    public DefaultFeatureQuery(String[] attributeNames, Evaluator filter) {
175
        super();
176
        this.attributeNames = attributeNames;
177
        this.filter = filter;
178
    }
179

    
180
        /**
181
         * Creates a FeatureQuery with the list of attribute names of feature, a
182
         * filter, the order for the FeatureCollection and the view scale.
183
         *
184
         * @param attributeNames
185
         *            the list of attribute names to load
186
         * @param filter
187
         *            based on the properties of the Features
188
         * @param order
189
         *            for the result
190
         * @param scale
191
         *            to view the Features.
192
         */
193
    public DefaultFeatureQuery(String[] attributeNames, Evaluator filter,
194
            double scale) {
195
        this.attributeNames = attributeNames;
196
        this.filter = filter;
197
        this.setScale(scale);
198
    }
199

    
200
        public double getScale() {
201
                Double scale = (Double) this.getQueryParameter(SCALE_PARAM_NAME);
202
                if (scale == null) {
203
                        return 0;
204
                }
205
                return scale.doubleValue();
206
        }
207

    
208
        public void setScale(double scale) {
209
                this.setQueryParameter(SCALE_PARAM_NAME, new Double(scale));
210
        }
211

    
212
        public Object getQueryParameter(String name) {
213
                return queryParameters.get(name);
214
        }
215

    
216
        public void setQueryParameter(String name, Object value) {
217
                queryParameters.put(name, value);
218
        }
219

    
220
    public void setFeatureType(FeatureType featureType) {
221
        this.featureTypeId = featureType.getId();
222
        Iterator iter = featureType.iterator();
223
                String attrs[] = new String[featureType.size()];
224
                FeatureAttributeDescriptor attr;
225
                int i = 0;
226
                while (iter.hasNext()) {
227
                        attr = (FeatureAttributeDescriptor) iter.next();
228
                        attrs[i] = attr.getName();
229
                        i++;
230
                }
231
                this.attributeNames = attrs;
232

    
233
    }
234

    
235
    public String[] getAttributeNames() {
236
                return attributeNames;
237
    }
238

    
239
    public void setAttributeNames(String[] attributeNames) {
240
                this.attributeNames = attributeNames;
241
    }
242

    
243
    public Evaluator getFilter() {
244
        return filter;
245
    }
246

    
247
    public void setFilter(Evaluator filter) {
248
        this.filter = filter;
249
    }
250

    
251
    public FeatureQueryOrder getOrder() {
252
        return order;
253
    }
254

    
255
        public void setOrder(FeatureQueryOrder order) {
256
                this.order = order;
257
        }
258

    
259
        public boolean hasFilter() {
260
                return this.filter != null;
261
        }
262

    
263
        public boolean hasOrder() {
264
                return this.order != null && this.order.size() > 0;
265
        }
266

    
267
        public Object clone() throws CloneNotSupportedException {
268
                DefaultFeatureQuery clone = (DefaultFeatureQuery) super.clone();
269

    
270
                // Clone attribute names array
271
                if (attributeNames != null) {
272
                clone.attributeNames = new String[attributeNames.length];
273
                System.arraycopy(attributeNames, 0, clone.attributeNames, 0,
274
                                attributeNames.length);
275
                }
276

    
277
                // Clone order
278
                if (order != null) {
279
                        clone.order = (FeatureQueryOrder) order.clone();
280
                }
281

    
282
                return clone;
283
        }
284

    
285
        public FeatureQuery getCopy() {
286
                try {
287
                        return (FeatureQuery) clone();
288
                } catch (CloneNotSupportedException e) {
289
                        // TODO Auto-generated catch block
290
                        e.printStackTrace();
291
                        return null;
292
                }
293
                // DefaultFeatureQuery aCopy = new DefaultFeatureQuery();
294
                //
295
                // aCopy.featureTypeId = this.featureTypeId;
296
                //
297
                // if (this.attributeNames != null) {
298
                // aCopy.attributeNames = (String[]) Arrays
299
                // .asList(this.attributeNames).toArray(new String[0]);
300
                // }
301
                //
302
                // aCopy.filter = this.filter;
303
                //
304
                // if (this.order != null) {
305
                // aCopy.order = this.order.getCopy();
306
                // }
307
                //
308
                // return aCopy;
309
        }
310

    
311
        public String getFeatureTypeId() {
312
                return featureTypeId;
313
        }
314

    
315
        public void setFeatureTypeId(String featureTypeId) {
316
                this.featureTypeId = featureTypeId;
317
        }
318

    
319
        public void saveToState(PersistentState state) throws PersistenceException {
320
                // FIXME: falta por terminar de implementar
321
                state.set("queryParameters", this.queryParameters);
322
                state.set("featureTypeId", this.featureTypeId);
323
                state.set("attributeNames", Arrays.asList(this.attributeNames));
324
                state.set("filter", this.filter);
325

    
326
        }
327

    
328
        public void loadFromState(PersistentState state) throws PersistenceException {
329
                // FIXME: falta por terminar de implementar
330
                this.queryParameters = (Map) state.get("queryParameters");
331
                this.featureTypeId = state.getString("featureTypeId");
332
                List attrNames = (List) state.get("attributeNames");
333
                if (attrNames == null) {
334
                        this.attributeNames = null;
335
                } else {
336
                        this.attributeNames = (String[]) attrNames
337
                                        .toArray(new String[attrNames.size()]);
338
                }
339
                this.filter = (Evaluator) state.get("filter");
340

    
341
        }
342

    
343
        /**
344
         * Register the class on PersistenceManager
345
         *
346
         */
347
        public static void registerPersistent() {
348
                DynStruct definition = ToolsLocator.getPersistenceManager().addDefinition(
349
                                DefaultFeatureQuery.class, 
350
                                "DefaultFeatureQuery", 
351
                                "DefaultFeatureQuery Persistent definition", 
352
                                null, 
353
                                null
354
                        );
355
                
356
                definition.addDynFieldMap("queryParameters")
357
                        .setClassOfItems(Object.class)
358
                        .setMandatory(true);
359
                
360
                definition.addDynFieldString("featureTypeId")
361
                        .setMandatory(true);
362
                
363
                definition.addDynFieldList("attributeNames")
364
                        .setClassOfItems(String.class)
365
                        .setMandatory(true);
366
                
367
                definition.addDynFieldObject("filter")
368
                        .setClassOfValue(Evaluator.class)
369
                        .setMandatory(true);
370

    
371
        }
372

    
373
        public long getLimit() {
374
                return limit;
375
        }
376

    
377
        public long getPageSize() {
378
                return pageSize;
379
        }
380

    
381
        public void setLimit(long limit) {
382
                this.limit = limit;
383
        }
384

    
385
        public void setPageSize(long pageSize) {
386
                this.pageSize = pageSize;
387
        }
388

    
389
}