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

History | View | Annotate | Download (19.9 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.feature.impl.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
import org.gvsig.fmap.dal.feature.Feature;
35
import org.gvsig.fmap.dal.feature.FeatureIndexes;
36
import org.gvsig.fmap.dal.feature.FeatureQuery;
37
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
38
import org.gvsig.fmap.dal.feature.FeatureQueryOrder.FeatureQueryOrderMember;
39
import org.gvsig.fmap.dal.feature.FeatureSet;
40
import org.gvsig.fmap.dal.feature.FeatureStore;
41
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
42
import org.gvsig.fmap.dal.feature.FeatureType;
43
import org.gvsig.fmap.dal.feature.exception.ConcurrentDataModificationException;
44
import org.gvsig.fmap.dal.feature.exception.FeatureSetInitializeException;
45
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore;
46
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStoreTransforms;
47
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider;
48
import org.gvsig.tools.dispose.DisposableIterator;
49
import org.gvsig.tools.dispose.DisposeUtils;
50
import org.gvsig.tools.evaluator.Evaluator;
51
import org.gvsig.tools.observer.Observable;
52
import org.gvsig.tools.observer.Observer;
53

    
54
public class DefaultFeatureSet extends AbstractFeatureSet implements
55
    FeatureSet, Observer {
56

    
57
    protected static final int NO_CHECKED = -1;
58
    protected static final int DEFAULT = 0;
59
    protected static final int FILTERED = 1;
60
    protected static final int ORDERED = 2;
61
    protected static final int ORDERED_FILTERED = 3;
62
    protected static final int EDITED = 4;
63
    protected static final int EDITED_FILTERED = 5;
64
    protected static final int ORDERD_EDITED = 6;
65
    protected static final int ORDERED_EDITED_FILTER = 7;
66

    
67
    protected boolean sourceStoreModified;
68
    protected boolean ownFeaturesModified;
69
    protected DefaultFeatureStore store;
70
    protected List featureTypes;
71
    protected FeatureQuery query;
72
    protected FeatureSetProvider provider;
73
    protected long size;
74
    protected int iteratorMode;
75
    protected List orderedData;
76
    protected Feature featureToIgnoreNotification;
77
    protected DefaultFeatureStoreTransforms transform;
78
    protected FeatureQuery queryForProvider;
79
    protected FeatureType defatulFeatureType;
80
    protected FeatureType defatulFeatureTypeForProvider;
81
    protected boolean ignoreChanges;
82

    
83
    public DefaultFeatureSet(DefaultFeatureStore store, FeatureQuery query)
84
        throws DataException {
85
        this.featureToIgnoreNotification = null;
86
        this.iteratorMode = NO_CHECKED;
87
        this.sourceStoreModified = false;
88
        this.ownFeaturesModified = false;
89
        this.size = -1;
90
        this.orderedData = null;
91
        this.store = store;
92
        if (this.store.isEditing()) {
93
            this.transform = this.store.getFeatureTypeManager().getTransforms();
94
        } else {
95
            this.transform =
96
                (DefaultFeatureStoreTransforms) store.getTransforms();
97
        }
98
        this.query = query;
99
        try {
100
            this.queryForProvider = (FeatureQuery) query.clone();
101
        } catch (CloneNotSupportedException e) {
102
            throw new FeatureSetInitializeException(e);
103
        }
104

    
105
        this.featureTypes = new ArrayList();
106
        if (this.query.getFeatureTypeId() == null
107
            && this.query.getAttributeNames() == null) {
108
            this.defatulFeatureType = this.store.getDefaultFeatureType();
109
            this.featureTypes.addAll(this.store.getFeatureTypes());
110
        } else {
111
            this.defatulFeatureType = this.store.getFeatureType(this.query);
112
            this.featureTypes.add(this.defatulFeatureType);
113
        }
114
        if (this.transform != null && !this.transform.isEmpty()) {
115
            this.fixQueryForProvider(this.queryForProvider, this.transform);
116
        } else {
117
            this.defatulFeatureTypeForProvider = this.defatulFeatureType;
118
        }
119

    
120
        FeatureIndexes indexes = store.getIndexes();
121
        if (this.queryForProvider.hasFilter() && indexes != null
122
            && indexes.areValid()) {
123
            this.provider =
124
                (FeatureSetProvider) indexes
125
                    .getFeatureSet(this.queryForProvider.getFilter());
126
        }
127
        if (this.provider == null) {
128
            this.provider =
129
                this.store.getProvider().createSet(this.queryForProvider,
130
                    this.defatulFeatureTypeForProvider);
131
        }
132
        this.store.addObserver(this);
133
    }
134

    
135
    private void fixQueryForProvider(FeatureQuery theQueryForProvider,
136
        DefaultFeatureStoreTransforms transformsToUse) throws DataException {
137
        theQueryForProvider.clearAttributeNames();
138
        FeatureType ftype =
139
            transformsToUse.getSourceFeatureTypeFrom(this.defatulFeatureType);
140
        theQueryForProvider.setFeatureTypeId(ftype.getId());
141
        this.defatulFeatureTypeForProvider = ftype;
142

    
143
        if (transformsToUse.isTransformsOriginalValues()) {
144
            theQueryForProvider.clearFilter();
145
            FeatureQueryOrder fqo = theQueryForProvider.getOrder();
146
            if (fqo != null) {
147
                fqo.clear();
148
            }
149
            return;
150

    
151
        }
152

    
153
        // Filter
154
        Evaluator filter = theQueryForProvider.getFilter();
155
        if (filter != null) {
156
            boolean canUseFilter;
157
            if (filter.getFieldsInfo() == null) {
158
                canUseFilter = false;
159
            } else {
160
                canUseFilter = areEvaluatorFieldsInAttributes(filter, ftype);
161
            }
162

    
163
            if (!canUseFilter) {
164
                theQueryForProvider.clearFilter();
165
            }
166

    
167
        }
168

    
169
        // Order
170
        if (theQueryForProvider.hasOrder()) {
171
            boolean canUseOrder = true;
172
            Iterator iter = theQueryForProvider.getOrder().iterator();
173
            FeatureQueryOrderMember item;
174
            while (iter.hasNext()) {
175
                item = (FeatureQueryOrderMember) iter.next();
176
                if (item.hasEvaluator()) {
177
                    if (!areEvaluatorFieldsInAttributes(item.getEvaluator(),
178
                        ftype)) {
179
                        canUseOrder = false;
180
                        break;
181
                    }
182
                } else {
183
                    if (ftype.get(item.getAttributeName()) == null) {
184
                        canUseOrder = false;
185
                        break;
186
                    }
187
                }
188
            }
189

    
190
            if (!canUseOrder) {
191
                theQueryForProvider.getOrder().clear();
192
            }
193
        }
194

    
195
    }
196

    
197
    private boolean areEvaluatorFieldsInAttributes(Evaluator evaluator,
198
        FeatureType fType) {
199
        if (evaluator.getFieldsInfo() == null) {
200
            return false;
201
        }
202
        String[] fieldNames = evaluator.getFieldsInfo().getFieldNames();
203
        if (fieldNames.length == 0) {
204
            return false;
205
        } else {
206
            for (String fieldName : fieldNames) {
207
                if (fType.get(fieldName) == null) {
208
                    return false;
209
                }
210
            }
211
        }
212
        return true;
213
    }
214

    
215
    public FeatureType getDefaultFeatureType() {
216
        return this.defatulFeatureType;
217
    }
218

    
219
    public List getFeatureTypes() {
220
        return Collections.unmodifiableList(this.featureTypes);
221
    }
222

    
223
    public long getSize() throws DataException {
224
        this.checkSourceStoreModified();
225
        if (size < 0) {
226
            size = calculateSize();
227
        }
228
        return size;
229
    }
230

    
231
    private long calculateSize() throws DataException {
232
        int mode = this.getIteratorMode();
233
        long limit = this.query.getLimit();
234
        if ((mode & EDITED) != EDITED) {
235
            if (this.provider.isEmpty()) {
236
                return 0;
237
            }
238
        }
239
        if ((mode & FILTERED) == FILTERED) {
240
            long mySize = 0;
241
            DisposableIterator iter = null;
242
            try {
243
                iter = this.fastIterator();
244
                while ((limit>0 && (mySize<limit)) || limit==0 ) {
245
                    iter.next();
246
                    mySize++;
247
                }
248
            } catch (NoSuchElementException e) {
249
                return mySize;
250
            } finally {
251
                DisposeUtils.disposeQuietly(iter);
252
            }
253
        } else if ((mode & EDITED) == EDITED) {
254
            long mySize = provider.getSize()
255
                + store.getFeatureManager().getDeltaSize();
256
            return (limit>0 && mySize>limit)? limit:mySize;
257
        }
258
        long mySize = provider.getSize();
259
        return (limit>0 && mySize>limit)? limit:mySize;
260
    }
261

    
262
    public void dispose() {
263
        this.store.deleteObserver(this);
264
        this.provider.dispose();
265
        this.provider = null;
266

    
267
        this.featureToIgnoreNotification = null;
268
        if (orderedData != null) {
269
            orderedData.clear();
270
        }
271
        this.orderedData = null;
272
        this.store = null;
273
        this.transform = null;
274
        this.query = null;
275
        this.queryForProvider = null;
276
        this.featureTypes = null;
277
        this.defatulFeatureType = null;
278
        this.defatulFeatureTypeForProvider = null;
279
    }
280

    
281
    public void update(Observable obsevable, Object notification) {
282
        if (sourceStoreModified) {
283
            return;
284
        }
285

    
286
        String type = ((FeatureStoreNotification) notification).getType();
287

    
288
        if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_INSERT)
289
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_DELETE)
290
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UPDATE)) {
291
            if (this.featureToIgnoreNotification == ((FeatureStoreNotification) notification)
292
                .getFeature()) {
293
                return;
294
            }
295
            sourceStoreModified = true;
296
            return;
297
        }
298
        if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UPDATE_TYPE)
299
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_REDO)
300
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UNDO)
301
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_REFRESH)
302
            || type
303
                .equalsIgnoreCase(FeatureStoreNotification.COMPLEX_NOTIFICATION)
304
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_CLOSE)
305
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_DISPOSE)
306
            || type.equalsIgnoreCase(FeatureStoreNotification.TRANSFORM_CHANGE)) {
307
            sourceStoreModified = true;
308
            return;
309
        }
310
        if (type.equalsIgnoreCase(FeatureStoreNotification.RESOURCE_CHANGED)) {
311
            if(!this.ignoreChanges) {
312
                sourceStoreModified = true;
313
                return;
314
            }
315
        }
316
        if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_CANCELEDITING)) {
317
            if (ownFeaturesModified) {
318
                sourceStoreModified = true;
319
                return;
320
            }
321
        }
322
    }
323
  
324
    protected void checkSourceStoreModified() {
325
        if (sourceStoreModified) {
326
                        throw new ConcurrentDataModificationException(store == null ? ""
327
                                        : store.getName());
328
        }
329
    }
330

    
331
    @Override
332
    public DisposableIterator fastIterator(long index) throws DataException {
333
        return fastIterator(index, 0);
334
    }
335
    
336
    @Override
337
    public DisposableIterator fastIterator(long index, long elements) throws DataException {
338
        if (index < 0) {
339
            throw new IndexOutOfBoundsException("The index (" + index
340
                + ") is less than 0");
341
        }
342
        DisposableIterator it;
343
        int mode = this.getIteratorMode();
344

    
345
        switch (mode) {
346
        case DEFAULT:
347
            it = new FastDefaultIterator(this, index, elements);
348
            break;
349

    
350
        case FILTERED:
351
            it = new FastFilteredIterator(this, index);
352
            break;
353

    
354
        case ORDERED:
355
            if (this.orderedData != null) {
356
                it = new FastOrderedIterator(this, index);
357
            } else {
358
                it = new FastOrderedIterator(this, new FastDefaultIterator(this, 0, elements), index);
359
            }
360
            break;
361
            
362
        case ORDERED_FILTERED:
363
            if (this.orderedData != null) {
364
                it = new FastOrderedIterator(this, index);
365
            } else {
366
                it = new FastOrderedIterator(this, new FastFilteredIterator(
367
                    this, 0), index);
368
            }
369
            break;
370

    
371
        case EDITED:
372
            it = new FastEditedIterator(this, index);
373
            break;
374

    
375
        case EDITED_FILTERED:
376
            it = new FastEditedFilteredIterator(this, index);
377
            break;
378

    
379
        case ORDERD_EDITED:
380
            if (this.orderedData != null) {
381
                it = new FastOrderedIterator(this, index);
382
            } else {
383
                it = new FastOrderedIterator(this, new FastEditedIterator(
384
                    this, 0), index);
385
            }
386
            break;
387

    
388
        case ORDERED_EDITED_FILTER:
389
            if (this.orderedData != null) {
390
                it = new FastOrderedIterator(this, index);
391
            } else {
392
                it = new FastOrderedIterator(this,
393
                    new FastEditedFilteredIterator(this, 0), index);
394
            }
395
            break;
396
            
397
        default:
398
            throw new IllegalArgumentException();
399
        }
400
        if( this.query!=null && this.query.getLimit()>0 ) {
401
            it = new LimitIterator(it,this.query.getLimit());
402
        }
403
        return it;
404
    }
405

    
406
    private class LimitIterator implements DisposableIterator {
407

    
408
        private final DisposableIterator it;
409
        private final long limit;
410
        private int count;
411

    
412
        private LimitIterator(DisposableIterator it, long limit) {
413
            this.it = it;
414
            this.limit = limit;
415
            this.count = 0;
416
        }
417

    
418
        @Override
419
        public void dispose() {
420
            this.it.dispose();
421
        }
422

    
423
        @Override
424
        public boolean hasNext() {
425
            if( this.count>=this.limit ) {
426
                return false;
427
            }
428
            return this.it.hasNext();
429
        }
430

    
431
        @Override
432
        public Object next() {
433
            if( this.count>=this.limit ) {
434
                return null;
435
            }
436
            this.count++;
437
            return this.it.next();
438
        }
439

    
440
        @Override
441
        public void remove() {
442
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
443
        }
444
        
445
    }
446

    
447
    @Override
448
    public DisposableIterator iterator(long index) throws DataException {
449
        return iterator(index,0);
450
    }
451
    
452
    @Override
453
    public DisposableIterator iterator(long index, long elements) throws DataException {        
454
        if (index < 0) {
455
            throw new IndexOutOfBoundsException("The index (" + index
456
                + ") is less than 0");
457
        }
458
        DisposableIterator it;
459
        int mode = this.getIteratorMode();
460

    
461
        switch (mode) {
462
        case DEFAULT:
463
            it = new DefaultIterator(this, index, elements);
464
            break;
465

    
466
        case FILTERED:
467
            it = new FilteredIterator(this, index);
468
            break;
469

    
470
        case ORDERED:
471
            if (orderedData != null) {
472
                it = new OrderedIterator(this, index);
473

    
474
            } else {
475
                it = new OrderedIterator(this, new DefaultIterator(this, 0, elements),index);
476
            }
477
            break;
478

    
479
        case ORDERED_FILTERED:
480
            it = new OrderedIterator(this, new FilteredIterator(this, 0),
481
                index);
482
            break;
483

    
484
        case EDITED:
485
            it = new EditedIterator(this, index);
486
            break;
487

    
488
        case EDITED_FILTERED:
489
            it = new EditedFilteredIterator(this, index);
490
            break;
491

    
492
        case ORDERD_EDITED:
493
            it = new OrderedIterator(this, new EditedIterator(this, 0), index);
494
            break;
495

    
496
        case ORDERED_EDITED_FILTER:
497
            it = new OrderedIterator(this,
498
                new EditedFilteredIterator(this, 0), index);
499
            break;
500

    
501
        default:
502
            throw new IllegalArgumentException();
503
        }
504

    
505
        if( this.query!=null && this.query.getLimit()>0 ) {
506
            it = new LimitIterator(it,this.query.getLimit());
507
        }
508
        return it;
509
    }
510

    
511
    private boolean providerCanOrder() {
512
        return this.provider.canOrder();
513
    }
514

    
515
    private boolean providerCanFilter() {
516
        return this.provider.canFilter();
517
    }
518

    
519
    private int getIteratorMode() {
520

    
521
        if (this.iteratorMode != NO_CHECKED) {
522
            return this.iteratorMode;
523
        }
524

    
525
        // TODO Tener en cuenta las transformaciones ???
526

    
527
        if (store.isEditing() && (store.getFeatureTypeManager().hasChanges() || store.getFeatureManager().hasChanges())) {
528
            if (this.query.hasOrder()) { // En edicion siempre ordeno yo.
529
                if (this.query.hasFilter()) {
530
                    return ORDERED_EDITED_FILTER;
531
                } else {
532
                    return ORDERD_EDITED;
533
                }
534
            } else {
535
                if (this.query.hasFilter()) {
536
                    return EDITED_FILTERED;
537
                } else {
538
                    return EDITED;
539
                }
540
            }
541
        } else {
542
            boolean useMyFilter = this.query.hasFilter();
543
            boolean useMyOrder = this.query.hasOrder();
544
            if (this.providerCanOrder() && this.transform.isEmpty()) {
545
                useMyOrder = false;
546
            }
547
            if (this.providerCanFilter() && this.transform.isEmpty()) {
548
                useMyFilter = false;
549
            }
550

    
551
            if (useMyOrder) {
552
                if (useMyFilter) {
553
                    return ORDERED_FILTERED;// ORDERED_FILTERED;
554
                } else {
555
                    return ORDERED;// ORDERED;
556
                }
557
            } else {
558
                if (useMyFilter) {
559
                    return FILTERED;// FILTERED;
560
                } else {
561
                    return DEFAULT;// DEFAULT;
562
                }
563
            }
564
        }
565

    
566
    }
567

    
568
    public void delete(Feature feature) throws DataException {
569
        this.featureToIgnoreNotification = feature;
570
        this.store.delete(feature);
571
        if (this.size > 0) {
572
            this.size--;
573
        }
574
        this.featureToIgnoreNotification = null;
575
        this.ownFeaturesModified = true;
576
    }
577

    
578
    public void insert(EditableFeature feature) throws DataException {
579
        this.featureToIgnoreNotification = feature;
580
        this.store.insert(feature);
581
        if (this.size >= 0) {
582
            this.size++;
583
        }
584
        this.featureToIgnoreNotification = null;
585
        this.ownFeaturesModified = true;
586
    }
587

    
588
    public void update(EditableFeature feature) throws DataException {
589
        this.featureToIgnoreNotification = feature;
590
        this.store.update(feature);
591
        this.featureToIgnoreNotification = null;
592
        this.ownFeaturesModified = true;
593
    }
594
    
595
    public void commitChanges() throws DataException {
596
        this.ignoreChanges = true;
597
        this.store.commitChanges();
598
        this.ignoreChanges = false;
599
        
600
    }
601

    
602
    public FeatureStore getFeatureStore() {
603
        return store;
604
    }
605

    
606
}