Statistics
| Revision:

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 / featureset / DefaultFeatureSet.java @ 46277

History | View | Annotate | Download (24.8 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.featureset;
25
26
import java.util.ArrayList;
27
import java.util.Collections;
28
import java.util.Iterator;
29
import java.util.List;
30
import java.util.NoSuchElementException;
31
32
import org.gvsig.fmap.dal.exception.DataException;
33
import org.gvsig.fmap.dal.feature.EditableFeature;
34 44753 omartinez
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
35 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.Feature;
36
import org.gvsig.fmap.dal.feature.FeatureIndexes;
37
import org.gvsig.fmap.dal.feature.FeatureQuery;
38
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
39 43026 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureQueryOrder.FeatureQueryOrderMember;
40 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureSet;
41
import org.gvsig.fmap.dal.feature.FeatureStore;
42
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
43
import org.gvsig.fmap.dal.feature.FeatureType;
44
import org.gvsig.fmap.dal.feature.exception.ConcurrentDataModificationException;
45
import org.gvsig.fmap.dal.feature.exception.FeatureSetInitializeException;
46
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore;
47
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStoreTransforms;
48 44753 omartinez
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureType;
49 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider;
50 45195 omartinez
import org.gvsig.tools.ToolsLocator;
51 40435 jjdelcerro
import org.gvsig.tools.dispose.DisposableIterator;
52 44113 jjdelcerro
import org.gvsig.tools.dispose.DisposeUtils;
53 40435 jjdelcerro
import org.gvsig.tools.evaluator.Evaluator;
54 45195 omartinez
import org.gvsig.tools.exception.BaseException;
55 40435 jjdelcerro
import org.gvsig.tools.observer.Observable;
56
import org.gvsig.tools.observer.Observer;
57
58 43089 jjdelcerro
public class DefaultFeatureSet extends AbstractFeatureSet implements
59 40435 jjdelcerro
    FeatureSet, Observer {
60
61 44113 jjdelcerro
    protected static final int NO_CHECKED = -1;
62
    protected static final int DEFAULT = 0;
63
    protected static final int FILTERED = 1;
64
    protected static final int ORDERED = 2;
65
    protected static final int ORDERED_FILTERED = 3;
66
    protected static final int EDITED = 4;
67
    protected static final int EDITED_FILTERED = 5;
68
    protected static final int ORDERD_EDITED = 6;
69
    protected static final int ORDERED_EDITED_FILTER = 7;
70 40435 jjdelcerro
71 45425 jjdelcerro
    protected Throwable sourceStoreModifiedCause;
72 44113 jjdelcerro
    protected boolean sourceStoreModified;
73
    protected boolean ownFeaturesModified;
74
    protected DefaultFeatureStore store;
75
    protected List featureTypes;
76
    protected FeatureQuery query;
77
    protected FeatureSetProvider provider;
78
    protected long size;
79
    protected int iteratorMode;
80
    protected List orderedData;
81
    protected Feature featureToIgnoreNotification;
82
    protected DefaultFeatureStoreTransforms transform;
83
    protected FeatureQuery queryForProvider;
84 44753 omartinez
    protected FeatureType defaultFeatureType;
85 44113 jjdelcerro
    protected FeatureType defatulFeatureTypeForProvider;
86
    protected boolean ignoreChanges;
87 45195 omartinez
    private boolean disposed = false;
88 40435 jjdelcerro
89
    public DefaultFeatureSet(DefaultFeatureStore store, FeatureQuery query)
90
        throws DataException {
91 45195 omartinez
        DisposeUtils.bind(this);
92 40435 jjdelcerro
        this.featureToIgnoreNotification = null;
93
        this.iteratorMode = NO_CHECKED;
94
        this.sourceStoreModified = false;
95
        this.ownFeaturesModified = false;
96
        this.size = -1;
97
        this.orderedData = null;
98
        this.store = store;
99 45425 jjdelcerro
        DisposeUtils.bind(this.store);
100 40435 jjdelcerro
        if (this.store.isEditing()) {
101
            this.transform = this.store.getFeatureTypeManager().getTransforms();
102
        } else {
103
            this.transform =
104
                (DefaultFeatureStoreTransforms) store.getTransforms();
105
        }
106
        this.query = query;
107
        try {
108
            this.queryForProvider = (FeatureQuery) query.clone();
109
        } catch (CloneNotSupportedException e) {
110
            throw new FeatureSetInitializeException(e);
111
        }
112
113
        this.featureTypes = new ArrayList();
114
        if (this.query.getFeatureTypeId() == null
115
            && this.query.getAttributeNames() == null) {
116 44753 omartinez
            this.defaultFeatureType = this.store.getDefaultFeatureType();
117 40435 jjdelcerro
            this.featureTypes.addAll(this.store.getFeatureTypes());
118
        } else {
119 44753 omartinez
            this.defaultFeatureType = this.store.getFeatureType(this.query);
120
            List<EditableFeatureAttributeDescriptor> cols = this.query.getExtraColumn().getColumns();
121
            if (this.query!=null && cols!=null && !cols.isEmpty()) {
122
                DefaultFeatureType featureTypeExtraCols = (DefaultFeatureType) this.defaultFeatureType.getCopy();
123
                featureTypeExtraCols.setExtraColumn(this.query.getExtraColumn());
124
                this.defaultFeatureType = featureTypeExtraCols;
125
            }
126
            this.featureTypes.add(this.defaultFeatureType);
127 40435 jjdelcerro
        }
128
        if (this.transform != null && !this.transform.isEmpty()) {
129
            this.fixQueryForProvider(this.queryForProvider, this.transform);
130
        } else {
131 44753 omartinez
            this.defatulFeatureTypeForProvider = this.defaultFeatureType;
132 40435 jjdelcerro
        }
133
134
        FeatureIndexes indexes = store.getIndexes();
135
        if (this.queryForProvider.hasFilter() && indexes != null
136
            && indexes.areValid()) {
137
            this.provider =
138
                (FeatureSetProvider) indexes
139
                    .getFeatureSet(this.queryForProvider.getFilter());
140
        }
141
        if (this.provider == null) {
142
            this.provider =
143
                this.store.getProvider().createSet(this.queryForProvider,
144
                    this.defatulFeatureTypeForProvider);
145
        }
146
        this.store.addObserver(this);
147
    }
148
149
    private void fixQueryForProvider(FeatureQuery theQueryForProvider,
150
        DefaultFeatureStoreTransforms transformsToUse) throws DataException {
151 42975 jjdelcerro
        theQueryForProvider.clearAttributeNames();
152 40435 jjdelcerro
        FeatureType ftype =
153 44753 omartinez
            transformsToUse.getSourceFeatureTypeFrom(this.defaultFeatureType);
154 40435 jjdelcerro
        theQueryForProvider.setFeatureTypeId(ftype.getId());
155
        this.defatulFeatureTypeForProvider = ftype;
156 44854 omartinez
157 40435 jjdelcerro
        if (transformsToUse.isTransformsOriginalValues()) {
158 42975 jjdelcerro
            theQueryForProvider.clearFilter();
159 40435 jjdelcerro
            FeatureQueryOrder fqo = theQueryForProvider.getOrder();
160
            if (fqo != null) {
161
                fqo.clear();
162
            }
163
            return;
164
165
        }
166
167
        // Filter
168
        Evaluator filter = theQueryForProvider.getFilter();
169
        if (filter != null) {
170 44113 jjdelcerro
            boolean canUseFilter;
171 40435 jjdelcerro
            if (filter.getFieldsInfo() == null) {
172
                canUseFilter = false;
173
            } else {
174
                canUseFilter = areEvaluatorFieldsInAttributes(filter, ftype);
175
            }
176
177
            if (!canUseFilter) {
178 42975 jjdelcerro
                theQueryForProvider.clearFilter();
179 40435 jjdelcerro
            }
180
181
        }
182
183
        // Order
184
        if (theQueryForProvider.hasOrder()) {
185
            boolean canUseOrder = true;
186
            Iterator iter = theQueryForProvider.getOrder().iterator();
187
            FeatureQueryOrderMember item;
188
            while (iter.hasNext()) {
189
                item = (FeatureQueryOrderMember) iter.next();
190
                if (item.hasEvaluator()) {
191
                    if (!areEvaluatorFieldsInAttributes(item.getEvaluator(),
192
                        ftype)) {
193
                        canUseOrder = false;
194
                        break;
195
                    }
196
                } else {
197
                    if (ftype.get(item.getAttributeName()) == null) {
198
                        canUseOrder = false;
199
                        break;
200
                    }
201
                }
202
            }
203
204
            if (!canUseOrder) {
205
                theQueryForProvider.getOrder().clear();
206
            }
207
        }
208
209
    }
210
211
    private boolean areEvaluatorFieldsInAttributes(Evaluator evaluator,
212
        FeatureType fType) {
213
        if (evaluator.getFieldsInfo() == null) {
214
            return false;
215
        }
216
        String[] fieldNames = evaluator.getFieldsInfo().getFieldNames();
217
        if (fieldNames.length == 0) {
218
            return false;
219
        } else {
220 44113 jjdelcerro
            for (String fieldName : fieldNames) {
221
                if (fType.get(fieldName) == null) {
222 40435 jjdelcerro
                    return false;
223
                }
224
            }
225
        }
226
        return true;
227
    }
228
229 44854 omartinez
    @Override
230 40435 jjdelcerro
    public FeatureType getDefaultFeatureType() {
231 44753 omartinez
        return this.defaultFeatureType;
232 40435 jjdelcerro
    }
233
234 44854 omartinez
    @Override
235 40435 jjdelcerro
    public List getFeatureTypes() {
236
        return Collections.unmodifiableList(this.featureTypes);
237
    }
238
239 44854 omartinez
    @Override
240 40435 jjdelcerro
    public long getSize() throws DataException {
241
        this.checkSourceStoreModified();
242
        if (size < 0) {
243
            size = calculateSize();
244
        }
245
        return size;
246
    }
247
248
    private long calculateSize() throws DataException {
249 46277 jjdelcerro
        boolean hasLimit = this.query.hasLimit();
250 44202 jjdelcerro
        long limit = this.query.getLimit();
251
        long mySize = 0;
252
253 40435 jjdelcerro
        int mode = this.getIteratorMode();
254 45530 fdiaz
        DisposableIterator iter = null;
255 44202 jjdelcerro
        switch (mode) {
256
        case DEFAULT:
257
        case ORDERED:
258 40435 jjdelcerro
            if (this.provider.isEmpty()) {
259
                return 0;
260
            }
261 44202 jjdelcerro
            mySize = provider.getSize();
262 46277 jjdelcerro
            return (hasLimit && mySize>limit)? limit:mySize;
263 44202 jjdelcerro
264
        case FILTERED:
265
        case ORDERED_FILTERED:
266 40435 jjdelcerro
            try {
267
                iter = this.fastIterator();
268 46277 jjdelcerro
                while ((hasLimit && (mySize<limit)) || !hasLimit ) {
269 40435 jjdelcerro
                    iter.next();
270
                    mySize++;
271
                }
272
            } catch (NoSuchElementException e) {
273 44202 jjdelcerro
274 40435 jjdelcerro
            } finally {
275 44113 jjdelcerro
                DisposeUtils.disposeQuietly(iter);
276 40435 jjdelcerro
            }
277 46277 jjdelcerro
            return (limit>=0 && mySize>limit)? limit:mySize;
278 44202 jjdelcerro
279
        case EDITED:
280
        case ORDERD_EDITED:
281
            mySize = provider.getSize()
282 43983 jjdelcerro
                + store.getFeatureManager().getDeltaSize();
283 46277 jjdelcerro
            return (hasLimit && mySize>limit)? limit:mySize;
284 44202 jjdelcerro
285 45530 fdiaz
        case EDITED_FILTERED:
286
        case ORDERED_EDITED_FILTER:
287
            try {
288
                iter = this.fastIterator();
289 46277 jjdelcerro
                while ((hasLimit && (mySize<limit)) || !hasLimit ) {
290 45530 fdiaz
                    iter.next();
291
                    mySize++;
292
                }
293
            } catch (NoSuchElementException e) {
294
295
            } finally {
296
                DisposeUtils.disposeQuietly(iter);
297
            }
298 46277 jjdelcerro
            return (hasLimit && mySize>limit)? limit:mySize;
299 45530 fdiaz
300 44202 jjdelcerro
        default:
301
            throw new IllegalArgumentException();
302 43983 jjdelcerro
        }
303 40435 jjdelcerro
    }
304 45195 omartinez
305
    @Override
306
    public synchronized final void dispose() {
307
        // Check if we have already been disposed, and don't do it again
308
        if (!disposed) {
309
            if (DisposeUtils.release(this)) {
310
                try {
311
                    doDispose();
312
                } catch (Exception ex) {
313
                    LOG.error("Error performing dispose", ex);
314
                } finally {
315
                    disposed = true;
316
                }
317
            }
318
        }
319
    }
320 40435 jjdelcerro
321 45195 omartinez
    public void doDispose() {
322 44669 jjdelcerro
        if( this.store!=null ) {
323
            this.store.deleteObserver(this);
324 45425 jjdelcerro
            DisposeUtils.dispose(this.store);
325 44669 jjdelcerro
            this.store = null;
326
        }
327
        if( this.provider!=null ) {
328
            this.provider.dispose();
329
            this.provider = null;
330
        }
331 40435 jjdelcerro
        if (orderedData != null) {
332
            orderedData.clear();
333 44669 jjdelcerro
            this.orderedData = null;
334 40435 jjdelcerro
        }
335 44669 jjdelcerro
        this.featureToIgnoreNotification = null;
336 40435 jjdelcerro
        this.transform = null;
337
        this.query = null;
338
        this.queryForProvider = null;
339
        this.featureTypes = null;
340 44753 omartinez
        this.defaultFeatureType = null;
341 40435 jjdelcerro
        this.defatulFeatureTypeForProvider = null;
342
    }
343
344
    public void update(Observable obsevable, Object notification) {
345
        if (sourceStoreModified) {
346
            return;
347
        }
348
349
        String type = ((FeatureStoreNotification) notification).getType();
350
351
        if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_INSERT)
352
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_DELETE)
353
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UPDATE)) {
354
            if (this.featureToIgnoreNotification == ((FeatureStoreNotification) notification)
355
                .getFeature()) {
356
                return;
357
            }
358
            sourceStoreModified = true;
359 45425 jjdelcerro
            sourceStoreModifiedCause = new Throwable();
360 40435 jjdelcerro
            return;
361
        }
362
        if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UPDATE_TYPE)
363
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_REDO)
364
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UNDO)
365
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_REFRESH)
366 45425 jjdelcerro
            || type.equalsIgnoreCase(FeatureStoreNotification.COMPLEX_NOTIFICATION)
367
//            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_CLOSE)
368
//            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_DISPOSE)
369 40435 jjdelcerro
            || type.equalsIgnoreCase(FeatureStoreNotification.TRANSFORM_CHANGE)) {
370
            sourceStoreModified = true;
371 45425 jjdelcerro
            sourceStoreModifiedCause = new Throwable();
372 40435 jjdelcerro
            return;
373
        }
374 44097 omartinez
        if (type.equalsIgnoreCase(FeatureStoreNotification.RESOURCE_CHANGED)) {
375
            if(!this.ignoreChanges) {
376
                sourceStoreModified = true;
377 45425 jjdelcerro
                sourceStoreModifiedCause = new Throwable();
378 44097 omartinez
                return;
379
            }
380 40435 jjdelcerro
        }
381 44097 omartinez
        if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_CANCELEDITING)) {
382
            if (ownFeaturesModified) {
383
                sourceStoreModified = true;
384 45425 jjdelcerro
                sourceStoreModifiedCause = new Throwable();
385 44097 omartinez
                return;
386
            }
387
        }
388 40435 jjdelcerro
    }
389 43358 jjdelcerro
390 40435 jjdelcerro
    protected void checkSourceStoreModified() {
391
        if (sourceStoreModified) {
392 45425 jjdelcerro
            ConcurrentDataModificationException ex = new ConcurrentDataModificationException(
393
                    store == null ? "": store.getName()
394
            );
395
            ex.initCause(sourceStoreModifiedCause);
396
            throw ex;
397 40435 jjdelcerro
        }
398
    }
399
400 43358 jjdelcerro
    @Override
401 40435 jjdelcerro
    public DisposableIterator fastIterator(long index) throws DataException {
402 43358 jjdelcerro
        return fastIterator(index, 0);
403
    }
404
405
    @Override
406
    public DisposableIterator fastIterator(long index, long elements) throws DataException {
407 40435 jjdelcerro
        if (index < 0) {
408
            throw new IndexOutOfBoundsException("The index (" + index
409
                + ") is less than 0");
410
        }
411 43913 jjdelcerro
        DisposableIterator it;
412 40435 jjdelcerro
        int mode = this.getIteratorMode();
413
414
        switch (mode) {
415
        case DEFAULT:
416 43913 jjdelcerro
            it = new FastDefaultIterator(this, index, elements);
417
            break;
418 40435 jjdelcerro
419
        case FILTERED:
420 45989 fdiaz
            it = new FastFilteredIterator(this, index, elements);
421 43913 jjdelcerro
            break;
422 40435 jjdelcerro
423
        case ORDERED:
424 45989 fdiaz
            if(this.provider.canOrder() && this.provider.canIterateFromIndex()){
425
                it = new FastDefaultIterator(this, index, elements);
426 40435 jjdelcerro
            } else {
427 45989 fdiaz
                if (this.orderedData != null) {
428
                    it = new FastOrderedIterator(this, index);
429
                } else {
430
                    it = new FastOrderedIterator(this, new FastDefaultIterator(this, 0, -1), index);
431
                }
432 40435 jjdelcerro
            }
433 43913 jjdelcerro
            break;
434
435 40435 jjdelcerro
        case ORDERED_FILTERED:
436 45989 fdiaz
            if(this.provider.canOrder() && this.provider.canFilter() && this.provider.canIterateFromIndex()){
437
                it = new FastFilteredIterator(this, index, elements);
438 40435 jjdelcerro
            } else {
439 45989 fdiaz
                if (this.orderedData != null) {
440
                    it = new FastOrderedIterator(this, index);
441
                } else {
442
                    it = new FastOrderedIterator(this, new FastFilteredIterator(
443
                        this, 0, -1), index);
444
                }
445 40435 jjdelcerro
            }
446 45989 fdiaz
447 43913 jjdelcerro
            break;
448 40435 jjdelcerro
449
        case EDITED:
450 45989 fdiaz
            it = new FastEditedIterator(this, index, elements);
451 43913 jjdelcerro
            break;
452 40435 jjdelcerro
453
        case EDITED_FILTERED:
454 45989 fdiaz
            it = new FastEditedFilteredIterator(this, index, elements);
455 43913 jjdelcerro
            break;
456 40435 jjdelcerro
457
        case ORDERD_EDITED:
458 45989 fdiaz
            if(this.provider.canOrder() && this.provider.canIterateFromIndex() && !this.store.getFeatureManager().hasDeleteds()){
459
                it = new FastEditedIterator(this, index, elements);
460 40435 jjdelcerro
            } else {
461 45989 fdiaz
                if (this.orderedData != null) {
462
                    it = new FastOrderedIterator(this, index);
463
                } else {
464
                    it = new FastOrderedIterator(this, new FastEditedIterator(
465
                        this, 0, -1), index);
466
                }
467 40435 jjdelcerro
            }
468 43913 jjdelcerro
            break;
469 40435 jjdelcerro
470
        case ORDERED_EDITED_FILTER:
471 45989 fdiaz
            if(this.provider.canOrder() && this.provider.canFilter() && this.provider.canIterateFromIndex() && !this.store.getFeatureManager().hasDeleteds()){
472
                it = new FastEditedFilteredIterator(this, index, elements);
473 40435 jjdelcerro
            } else {
474 45989 fdiaz
                if (this.orderedData != null) {
475
                    it = new FastOrderedIterator(this, index);
476
                } else {
477
                    it = new FastOrderedIterator(this,
478
                        new FastEditedFilteredIterator(this, 0, -1), index);
479
                }
480 40435 jjdelcerro
            }
481 43913 jjdelcerro
            break;
482
483 40435 jjdelcerro
        default:
484
            throw new IllegalArgumentException();
485
        }
486 43913 jjdelcerro
        if( this.query!=null && this.query.getLimit()>0 ) {
487
            it = new LimitIterator(it,this.query.getLimit());
488
        }
489
        return it;
490 40435 jjdelcerro
    }
491
492 43913 jjdelcerro
    private class LimitIterator implements DisposableIterator {
493 40435 jjdelcerro
494 43913 jjdelcerro
        private final DisposableIterator it;
495
        private final long limit;
496
        private int count;
497
498
        private LimitIterator(DisposableIterator it, long limit) {
499
            this.it = it;
500
            this.limit = limit;
501
            this.count = 0;
502
        }
503
504
        @Override
505
        public void dispose() {
506
            this.it.dispose();
507
        }
508
509
        @Override
510
        public boolean hasNext() {
511
            if( this.count>=this.limit ) {
512
                return false;
513
            }
514
            return this.it.hasNext();
515
        }
516
517
        @Override
518
        public Object next() {
519
            if( this.count>=this.limit ) {
520
                return null;
521
            }
522
            this.count++;
523
            return this.it.next();
524
        }
525
526
        @Override
527
        public void remove() {
528
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
529
        }
530
531
    }
532
533 43358 jjdelcerro
    @Override
534 40435 jjdelcerro
    public DisposableIterator iterator(long index) throws DataException {
535 43358 jjdelcerro
        return iterator(index,0);
536
    }
537
538
    @Override
539
    public DisposableIterator iterator(long index, long elements) throws DataException {
540 40435 jjdelcerro
        if (index < 0) {
541
            throw new IndexOutOfBoundsException("The index (" + index
542
                + ") is less than 0");
543
        }
544 43913 jjdelcerro
        DisposableIterator it;
545 40435 jjdelcerro
        int mode = this.getIteratorMode();
546
547
        switch (mode) {
548
        case DEFAULT:
549 43913 jjdelcerro
            it = new DefaultIterator(this, index, elements);
550
            break;
551 40435 jjdelcerro
552
        case FILTERED:
553 45989 fdiaz
            it = new FilteredIterator(this, index, elements);
554 43913 jjdelcerro
            break;
555 40435 jjdelcerro
556
        case ORDERED:
557 45989 fdiaz
            if(this.provider.canOrder() && this.provider.canIterateFromIndex()){
558
                it = new DefaultIterator(this, index, elements);
559
            } else {
560
                if (orderedData != null) {
561
                    it = new OrderedIterator(this, index);
562 40435 jjdelcerro
563 45989 fdiaz
                } else {
564
                    it = new OrderedIterator(this, new DefaultIterator(this, 0, elements),index);
565
                }
566 40435 jjdelcerro
            }
567 43913 jjdelcerro
            break;
568 40435 jjdelcerro
569
        case ORDERED_FILTERED:
570 45989 fdiaz
            if(this.provider.canOrder() && this.provider.canFilter() && this.provider.canIterateFromIndex()){
571
                it = new FilteredIterator(this, index, elements);
572
            } else {
573
                if (orderedData != null) {
574
                    it = new OrderedIterator(this, index);
575
                } else {
576
                    it = new OrderedIterator(this, new FilteredIterator(this, 0, -1),index);
577
                }
578
            }
579 43913 jjdelcerro
            break;
580 40435 jjdelcerro
581
        case EDITED:
582 45989 fdiaz
            it = new EditedIterator(this, index, elements);
583 43913 jjdelcerro
            break;
584 40435 jjdelcerro
585
        case EDITED_FILTERED:
586 45989 fdiaz
            it = new EditedFilteredIterator(this, index, elements);
587 43913 jjdelcerro
            break;
588 40435 jjdelcerro
589
        case ORDERD_EDITED:
590 45989 fdiaz
            if(this.provider.canOrder() && this.provider.canIterateFromIndex() && !this.store.getFeatureManager().hasDeleteds()){
591
                it = new EditedIterator(this, index, elements);
592
            } else {
593
                if (orderedData != null) {
594
                    it = new OrderedIterator(this, index);
595
                } else {
596
                    it = new OrderedIterator(this,
597
                        new EditedIterator(this, 0, -1), index);
598
                }
599
            }
600 43913 jjdelcerro
            break;
601 40435 jjdelcerro
602
        case ORDERED_EDITED_FILTER:
603 45989 fdiaz
            if(this.provider.canOrder() && this.providerCanFilter() && this.provider.canIterateFromIndex() && !this.store.getFeatureManager().hasDeleteds()){
604
                it = new EditedFilteredIterator(this, index, elements);
605
            } else {
606
                if (orderedData != null) {
607
                    it = new OrderedIterator(this, index);
608
                } else {
609
                    it = new OrderedIterator(this,
610
                        new EditedFilteredIterator(this, 0, -1), index);
611
                }
612
            }
613 43913 jjdelcerro
            break;
614 40435 jjdelcerro
615
        default:
616
            throw new IllegalArgumentException();
617
        }
618
619 43913 jjdelcerro
        if( this.query!=null && this.query.getLimit()>0 ) {
620
            it = new LimitIterator(it,this.query.getLimit());
621
        }
622
        return it;
623 40435 jjdelcerro
    }
624
625
    private boolean providerCanOrder() {
626
        return this.provider.canOrder();
627
    }
628
629
    private boolean providerCanFilter() {
630
        return this.provider.canFilter();
631
    }
632
633
    private int getIteratorMode() {
634
635
        if (this.iteratorMode != NO_CHECKED) {
636
            return this.iteratorMode;
637
        }
638
639
        // TODO Tener en cuenta las transformaciones ???
640
641 43981 omartinez
        if (store.isEditing() && (store.getFeatureTypeManager().hasChanges() || store.getFeatureManager().hasChanges())) {
642 40435 jjdelcerro
            if (this.query.hasOrder()) { // En edicion siempre ordeno yo.
643
                if (this.query.hasFilter()) {
644
                    return ORDERED_EDITED_FILTER;
645
                } else {
646
                    return ORDERD_EDITED;
647
                }
648
            } else {
649
                if (this.query.hasFilter()) {
650
                    return EDITED_FILTERED;
651
                } else {
652
                    return EDITED;
653
                }
654
            }
655
        } else {
656
            boolean useMyFilter = this.query.hasFilter();
657
            boolean useMyOrder = this.query.hasOrder();
658
            if (this.providerCanOrder() && this.transform.isEmpty()) {
659
                useMyOrder = false;
660
            }
661
            if (this.providerCanFilter() && this.transform.isEmpty()) {
662
                useMyFilter = false;
663
            }
664
665
            if (useMyOrder) {
666
                if (useMyFilter) {
667 44191 jjdelcerro
                    return ORDERED_FILTERED;
668 40435 jjdelcerro
                } else {
669 44191 jjdelcerro
                    return ORDERED;
670 40435 jjdelcerro
                }
671
            } else {
672
                if (useMyFilter) {
673 44191 jjdelcerro
                    return FILTERED;
674 40435 jjdelcerro
                } else {
675 44191 jjdelcerro
                    return DEFAULT;
676 40435 jjdelcerro
                }
677
            }
678
        }
679
680
    }
681
682 44854 omartinez
    @Override
683 40435 jjdelcerro
    public void delete(Feature feature) throws DataException {
684
        this.featureToIgnoreNotification = feature;
685
        this.store.delete(feature);
686
        if (this.size > 0) {
687
            this.size--;
688
        }
689
        this.featureToIgnoreNotification = null;
690
        this.ownFeaturesModified = true;
691
    }
692
693 44854 omartinez
    @Override
694 40435 jjdelcerro
    public void insert(EditableFeature feature) throws DataException {
695
        this.featureToIgnoreNotification = feature;
696
        this.store.insert(feature);
697
        if (this.size >= 0) {
698
            this.size++;
699
        }
700
        this.featureToIgnoreNotification = null;
701
        this.ownFeaturesModified = true;
702
    }
703
704 44854 omartinez
    @Override
705 40435 jjdelcerro
    public void update(EditableFeature feature) throws DataException {
706
        this.featureToIgnoreNotification = feature;
707
        this.store.update(feature);
708
        this.featureToIgnoreNotification = null;
709
        this.ownFeaturesModified = true;
710
    }
711 44097 omartinez
712 44854 omartinez
    @Override
713 44097 omartinez
    public void commitChanges() throws DataException {
714
        this.ignoreChanges = true;
715
        this.store.commitChanges();
716
        this.ignoreChanges = false;
717
718
    }
719 40435 jjdelcerro
720 44854 omartinez
    @Override
721 40435 jjdelcerro
    public FeatureStore getFeatureStore() {
722
        return store;
723
    }
724 43358 jjdelcerro
725 40435 jjdelcerro
}