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 @ 45647

History | View | Annotate | Download (22.3 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 44202 jjdelcerro
        long limit = this.query.getLimit();
250
        long mySize = 0;
251
252 40435 jjdelcerro
        int mode = this.getIteratorMode();
253 45530 fdiaz
        DisposableIterator iter = null;
254 44202 jjdelcerro
        switch (mode) {
255
        case DEFAULT:
256
        case ORDERED:
257 40435 jjdelcerro
            if (this.provider.isEmpty()) {
258
                return 0;
259
            }
260 44202 jjdelcerro
            mySize = provider.getSize();
261
            return (limit>0 && mySize>limit)? limit:mySize;
262
263
        case FILTERED:
264
        case ORDERED_FILTERED:
265 40435 jjdelcerro
            try {
266
                iter = this.fastIterator();
267 43980 omartinez
                while ((limit>0 && (mySize<limit)) || limit==0 ) {
268 40435 jjdelcerro
                    iter.next();
269
                    mySize++;
270
                }
271
            } catch (NoSuchElementException e) {
272 44202 jjdelcerro
273 40435 jjdelcerro
            } finally {
274 44113 jjdelcerro
                DisposeUtils.disposeQuietly(iter);
275 40435 jjdelcerro
            }
276 45530 fdiaz
            return (limit>0 && mySize>limit)? limit:mySize;
277 44202 jjdelcerro
278
        case EDITED:
279
        case ORDERD_EDITED:
280
            mySize = provider.getSize()
281 43983 jjdelcerro
                + store.getFeatureManager().getDeltaSize();
282
            return (limit>0 && mySize>limit)? limit:mySize;
283 44202 jjdelcerro
284 45530 fdiaz
        case EDITED_FILTERED:
285
        case ORDERED_EDITED_FILTER:
286
            try {
287
                iter = this.fastIterator();
288
                while ((limit>0 && (mySize<limit)) || limit==0 ) {
289
                    iter.next();
290
                    mySize++;
291
                }
292
            } catch (NoSuchElementException e) {
293
294
            } finally {
295
                DisposeUtils.disposeQuietly(iter);
296
            }
297
            return (limit>0 && mySize>limit)? limit:mySize;
298
299 44202 jjdelcerro
        default:
300
            throw new IllegalArgumentException();
301 43983 jjdelcerro
        }
302 40435 jjdelcerro
    }
303 45195 omartinez
304
    @Override
305
    public synchronized final void dispose() {
306
        // Check if we have already been disposed, and don't do it again
307
        if (!disposed) {
308
            if (DisposeUtils.release(this)) {
309
                try {
310
                    doDispose();
311
                } catch (Exception ex) {
312
                    LOG.error("Error performing dispose", ex);
313
                } finally {
314
                    disposed = true;
315
                }
316
            }
317
        }
318
    }
319 40435 jjdelcerro
320 45195 omartinez
    public void doDispose() {
321 44669 jjdelcerro
        if( this.store!=null ) {
322
            this.store.deleteObserver(this);
323 45425 jjdelcerro
            DisposeUtils.dispose(this.store);
324 44669 jjdelcerro
            this.store = null;
325
        }
326
        if( this.provider!=null ) {
327
            this.provider.dispose();
328
            this.provider = null;
329
        }
330 40435 jjdelcerro
        if (orderedData != null) {
331
            orderedData.clear();
332 44669 jjdelcerro
            this.orderedData = null;
333 40435 jjdelcerro
        }
334 44669 jjdelcerro
        this.featureToIgnoreNotification = null;
335 40435 jjdelcerro
        this.transform = null;
336
        this.query = null;
337
        this.queryForProvider = null;
338
        this.featureTypes = null;
339 44753 omartinez
        this.defaultFeatureType = null;
340 40435 jjdelcerro
        this.defatulFeatureTypeForProvider = null;
341
    }
342
343
    public void update(Observable obsevable, Object notification) {
344
        if (sourceStoreModified) {
345
            return;
346
        }
347
348
        String type = ((FeatureStoreNotification) notification).getType();
349
350
        if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_INSERT)
351
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_DELETE)
352
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UPDATE)) {
353
            if (this.featureToIgnoreNotification == ((FeatureStoreNotification) notification)
354
                .getFeature()) {
355
                return;
356
            }
357
            sourceStoreModified = true;
358 45425 jjdelcerro
            sourceStoreModifiedCause = new Throwable();
359 40435 jjdelcerro
            return;
360
        }
361
        if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UPDATE_TYPE)
362
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_REDO)
363
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UNDO)
364
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_REFRESH)
365 45425 jjdelcerro
            || type.equalsIgnoreCase(FeatureStoreNotification.COMPLEX_NOTIFICATION)
366
//            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_CLOSE)
367
//            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_DISPOSE)
368 40435 jjdelcerro
            || type.equalsIgnoreCase(FeatureStoreNotification.TRANSFORM_CHANGE)) {
369
            sourceStoreModified = true;
370 45425 jjdelcerro
            sourceStoreModifiedCause = new Throwable();
371 40435 jjdelcerro
            return;
372
        }
373 44097 omartinez
        if (type.equalsIgnoreCase(FeatureStoreNotification.RESOURCE_CHANGED)) {
374
            if(!this.ignoreChanges) {
375
                sourceStoreModified = true;
376 45425 jjdelcerro
                sourceStoreModifiedCause = new Throwable();
377 44097 omartinez
                return;
378
            }
379 40435 jjdelcerro
        }
380 44097 omartinez
        if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_CANCELEDITING)) {
381
            if (ownFeaturesModified) {
382
                sourceStoreModified = true;
383 45425 jjdelcerro
                sourceStoreModifiedCause = new Throwable();
384 44097 omartinez
                return;
385
            }
386
        }
387 40435 jjdelcerro
    }
388 43358 jjdelcerro
389 40435 jjdelcerro
    protected void checkSourceStoreModified() {
390
        if (sourceStoreModified) {
391 45425 jjdelcerro
            ConcurrentDataModificationException ex = new ConcurrentDataModificationException(
392
                    store == null ? "": store.getName()
393
            );
394
            ex.initCause(sourceStoreModifiedCause);
395
            throw ex;
396 40435 jjdelcerro
        }
397
    }
398
399 43358 jjdelcerro
    @Override
400 40435 jjdelcerro
    public DisposableIterator fastIterator(long index) throws DataException {
401 43358 jjdelcerro
        return fastIterator(index, 0);
402
    }
403
404
    @Override
405
    public DisposableIterator fastIterator(long index, long elements) throws DataException {
406 40435 jjdelcerro
        if (index < 0) {
407
            throw new IndexOutOfBoundsException("The index (" + index
408
                + ") is less than 0");
409
        }
410 43913 jjdelcerro
        DisposableIterator it;
411 40435 jjdelcerro
        int mode = this.getIteratorMode();
412
413
        switch (mode) {
414
        case DEFAULT:
415 43913 jjdelcerro
            it = new FastDefaultIterator(this, index, elements);
416
            break;
417 40435 jjdelcerro
418
        case FILTERED:
419 43913 jjdelcerro
            it = new FastFilteredIterator(this, index);
420
            break;
421 40435 jjdelcerro
422
        case ORDERED:
423
            if (this.orderedData != null) {
424 43913 jjdelcerro
                it = new FastOrderedIterator(this, index);
425 40435 jjdelcerro
            } else {
426 43913 jjdelcerro
                it = new FastOrderedIterator(this, new FastDefaultIterator(this, 0, elements), index);
427 40435 jjdelcerro
            }
428 43913 jjdelcerro
            break;
429
430 40435 jjdelcerro
        case ORDERED_FILTERED:
431
            if (this.orderedData != null) {
432 43913 jjdelcerro
                it = new FastOrderedIterator(this, index);
433 40435 jjdelcerro
            } else {
434 43913 jjdelcerro
                it = new FastOrderedIterator(this, new FastFilteredIterator(
435 40435 jjdelcerro
                    this, 0), index);
436
            }
437 43913 jjdelcerro
            break;
438 40435 jjdelcerro
439
        case EDITED:
440 43913 jjdelcerro
            it = new FastEditedIterator(this, index);
441
            break;
442 40435 jjdelcerro
443
        case EDITED_FILTERED:
444 43913 jjdelcerro
            it = new FastEditedFilteredIterator(this, index);
445
            break;
446 40435 jjdelcerro
447
        case ORDERD_EDITED:
448
            if (this.orderedData != null) {
449 43913 jjdelcerro
                it = new FastOrderedIterator(this, index);
450 40435 jjdelcerro
            } else {
451 43913 jjdelcerro
                it = new FastOrderedIterator(this, new FastEditedIterator(
452 40435 jjdelcerro
                    this, 0), index);
453
            }
454 43913 jjdelcerro
            break;
455 40435 jjdelcerro
456
        case ORDERED_EDITED_FILTER:
457
            if (this.orderedData != null) {
458 43913 jjdelcerro
                it = new FastOrderedIterator(this, index);
459 40435 jjdelcerro
            } else {
460 43913 jjdelcerro
                it = new FastOrderedIterator(this,
461 40435 jjdelcerro
                    new FastEditedFilteredIterator(this, 0), index);
462
            }
463 43913 jjdelcerro
            break;
464
465 40435 jjdelcerro
        default:
466
            throw new IllegalArgumentException();
467
        }
468 43913 jjdelcerro
        if( this.query!=null && this.query.getLimit()>0 ) {
469
            it = new LimitIterator(it,this.query.getLimit());
470
        }
471
        return it;
472 40435 jjdelcerro
    }
473
474 43913 jjdelcerro
    private class LimitIterator implements DisposableIterator {
475 40435 jjdelcerro
476 43913 jjdelcerro
        private final DisposableIterator it;
477
        private final long limit;
478
        private int count;
479
480
        private LimitIterator(DisposableIterator it, long limit) {
481
            this.it = it;
482
            this.limit = limit;
483
            this.count = 0;
484
        }
485
486
        @Override
487
        public void dispose() {
488
            this.it.dispose();
489
        }
490
491
        @Override
492
        public boolean hasNext() {
493
            if( this.count>=this.limit ) {
494
                return false;
495
            }
496
            return this.it.hasNext();
497
        }
498
499
        @Override
500
        public Object next() {
501
            if( this.count>=this.limit ) {
502
                return null;
503
            }
504
            this.count++;
505
            return this.it.next();
506
        }
507
508
        @Override
509
        public void remove() {
510
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
511
        }
512
513
    }
514
515 43358 jjdelcerro
    @Override
516 40435 jjdelcerro
    public DisposableIterator iterator(long index) throws DataException {
517 43358 jjdelcerro
        return iterator(index,0);
518
    }
519
520
    @Override
521
    public DisposableIterator iterator(long index, long elements) throws DataException {
522 40435 jjdelcerro
        if (index < 0) {
523
            throw new IndexOutOfBoundsException("The index (" + index
524
                + ") is less than 0");
525
        }
526 43913 jjdelcerro
        DisposableIterator it;
527 40435 jjdelcerro
        int mode = this.getIteratorMode();
528
529
        switch (mode) {
530
        case DEFAULT:
531 43913 jjdelcerro
            it = new DefaultIterator(this, index, elements);
532
            break;
533 40435 jjdelcerro
534
        case FILTERED:
535 43913 jjdelcerro
            it = new FilteredIterator(this, index);
536
            break;
537 40435 jjdelcerro
538
        case ORDERED:
539
            if (orderedData != null) {
540 43913 jjdelcerro
                it = new OrderedIterator(this, index);
541 40435 jjdelcerro
542
            } else {
543 43913 jjdelcerro
                it = new OrderedIterator(this, new DefaultIterator(this, 0, elements),index);
544 40435 jjdelcerro
            }
545 43913 jjdelcerro
            break;
546 40435 jjdelcerro
547
        case ORDERED_FILTERED:
548 43913 jjdelcerro
            it = new OrderedIterator(this, new FilteredIterator(this, 0),
549 40435 jjdelcerro
                index);
550 43913 jjdelcerro
            break;
551 40435 jjdelcerro
552
        case EDITED:
553 43913 jjdelcerro
            it = new EditedIterator(this, index);
554
            break;
555 40435 jjdelcerro
556
        case EDITED_FILTERED:
557 43913 jjdelcerro
            it = new EditedFilteredIterator(this, index);
558
            break;
559 40435 jjdelcerro
560
        case ORDERD_EDITED:
561 43913 jjdelcerro
            it = new OrderedIterator(this, new EditedIterator(this, 0), index);
562
            break;
563 40435 jjdelcerro
564
        case ORDERED_EDITED_FILTER:
565 43913 jjdelcerro
            it = new OrderedIterator(this,
566 40435 jjdelcerro
                new EditedFilteredIterator(this, 0), index);
567 43913 jjdelcerro
            break;
568 40435 jjdelcerro
569
        default:
570
            throw new IllegalArgumentException();
571
        }
572
573 43913 jjdelcerro
        if( this.query!=null && this.query.getLimit()>0 ) {
574
            it = new LimitIterator(it,this.query.getLimit());
575
        }
576
        return it;
577 40435 jjdelcerro
    }
578
579
    private boolean providerCanOrder() {
580
        return this.provider.canOrder();
581
    }
582
583
    private boolean providerCanFilter() {
584
        return this.provider.canFilter();
585
    }
586
587
    private int getIteratorMode() {
588
589
        if (this.iteratorMode != NO_CHECKED) {
590
            return this.iteratorMode;
591
        }
592
593
        // TODO Tener en cuenta las transformaciones ???
594
595 43981 omartinez
        if (store.isEditing() && (store.getFeatureTypeManager().hasChanges() || store.getFeatureManager().hasChanges())) {
596 40435 jjdelcerro
            if (this.query.hasOrder()) { // En edicion siempre ordeno yo.
597
                if (this.query.hasFilter()) {
598
                    return ORDERED_EDITED_FILTER;
599
                } else {
600
                    return ORDERD_EDITED;
601
                }
602
            } else {
603
                if (this.query.hasFilter()) {
604
                    return EDITED_FILTERED;
605
                } else {
606
                    return EDITED;
607
                }
608
            }
609
        } else {
610
            boolean useMyFilter = this.query.hasFilter();
611
            boolean useMyOrder = this.query.hasOrder();
612
            if (this.providerCanOrder() && this.transform.isEmpty()) {
613
                useMyOrder = false;
614
            }
615
            if (this.providerCanFilter() && this.transform.isEmpty()) {
616
                useMyFilter = false;
617
            }
618
619
            if (useMyOrder) {
620
                if (useMyFilter) {
621 44191 jjdelcerro
                    return ORDERED_FILTERED;
622 40435 jjdelcerro
                } else {
623 44191 jjdelcerro
                    return ORDERED;
624 40435 jjdelcerro
                }
625
            } else {
626
                if (useMyFilter) {
627 44191 jjdelcerro
                    return FILTERED;
628 40435 jjdelcerro
                } else {
629 44191 jjdelcerro
                    return DEFAULT;
630 40435 jjdelcerro
                }
631
            }
632
        }
633
634
    }
635
636 44854 omartinez
    @Override
637 40435 jjdelcerro
    public void delete(Feature feature) throws DataException {
638
        this.featureToIgnoreNotification = feature;
639
        this.store.delete(feature);
640
        if (this.size > 0) {
641
            this.size--;
642
        }
643
        this.featureToIgnoreNotification = null;
644
        this.ownFeaturesModified = true;
645
    }
646
647 44854 omartinez
    @Override
648 40435 jjdelcerro
    public void insert(EditableFeature feature) throws DataException {
649
        this.featureToIgnoreNotification = feature;
650
        this.store.insert(feature);
651
        if (this.size >= 0) {
652
            this.size++;
653
        }
654
        this.featureToIgnoreNotification = null;
655
        this.ownFeaturesModified = true;
656
    }
657
658 44854 omartinez
    @Override
659 40435 jjdelcerro
    public void update(EditableFeature feature) throws DataException {
660
        this.featureToIgnoreNotification = feature;
661
        this.store.update(feature);
662
        this.featureToIgnoreNotification = null;
663
        this.ownFeaturesModified = true;
664
    }
665 44097 omartinez
666 44854 omartinez
    @Override
667 44097 omartinez
    public void commitChanges() throws DataException {
668
        this.ignoreChanges = true;
669
        this.store.commitChanges();
670
        this.ignoreChanges = false;
671
672
    }
673 40435 jjdelcerro
674 44854 omartinez
    @Override
675 40435 jjdelcerro
    public FeatureStore getFeatureStore() {
676
        return store;
677
    }
678 43358 jjdelcerro
679 40435 jjdelcerro
}