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

History | View | Annotate | Download (20.1 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
        long limit = this.query.getLimit();
233
        long mySize = 0;
234
        
235
        int mode = this.getIteratorMode();
236
        switch (mode) {
237
        case DEFAULT:
238
        case ORDERED:
239
            if (this.provider.isEmpty()) {
240
                return 0;
241
            }
242
            mySize = provider.getSize();
243
            return (limit>0 && mySize>limit)? limit:mySize;
244

    
245
        case FILTERED:
246
        case ORDERED_FILTERED:
247
            DisposableIterator iter = null;
248
            try {
249
                iter = this.fastIterator();
250
                while ((limit>0 && (mySize<limit)) || limit==0 ) {
251
                    iter.next();
252
                    mySize++;
253
                }
254
            } catch (NoSuchElementException e) {
255

    
256
            } finally {
257
                DisposeUtils.disposeQuietly(iter);
258
            }
259
            return mySize;
260

    
261
        case EDITED:
262
        case EDITED_FILTERED:
263
        case ORDERD_EDITED:
264
        case ORDERED_EDITED_FILTER:
265
            mySize = provider.getSize()
266
                + store.getFeatureManager().getDeltaSize();
267
            return (limit>0 && mySize>limit)? limit:mySize;
268

    
269
        default:
270
            throw new IllegalArgumentException();
271
        }
272
    }
273

    
274
    public void dispose() {
275
        if( this.store!=null ) {
276
            this.store.deleteObserver(this);
277
            this.store = null;
278
        }
279
        if( this.provider!=null ) {
280
            this.provider.dispose();
281
            this.provider = null;
282
        }
283
        if (orderedData != null) {
284
            orderedData.clear();
285
            this.orderedData = null;
286
        }
287
        this.featureToIgnoreNotification = null;
288
        this.transform = null;
289
        this.query = null;
290
        this.queryForProvider = null;
291
        this.featureTypes = null;
292
        this.defatulFeatureType = null;
293
        this.defatulFeatureTypeForProvider = null;
294
    }
295

    
296
    public void update(Observable obsevable, Object notification) {
297
        if (sourceStoreModified) {
298
            return;
299
        }
300

    
301
        String type = ((FeatureStoreNotification) notification).getType();
302

    
303
        if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_INSERT)
304
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_DELETE)
305
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UPDATE)) {
306
            if (this.featureToIgnoreNotification == ((FeatureStoreNotification) notification)
307
                .getFeature()) {
308
                return;
309
            }
310
            sourceStoreModified = true;
311
            return;
312
        }
313
        if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UPDATE_TYPE)
314
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_REDO)
315
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UNDO)
316
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_REFRESH)
317
            || type
318
                .equalsIgnoreCase(FeatureStoreNotification.COMPLEX_NOTIFICATION)
319
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_CLOSE)
320
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_DISPOSE)
321
            || type.equalsIgnoreCase(FeatureStoreNotification.TRANSFORM_CHANGE)) {
322
            sourceStoreModified = true;
323
            return;
324
        }
325
        if (type.equalsIgnoreCase(FeatureStoreNotification.RESOURCE_CHANGED)) {
326
            if(!this.ignoreChanges) {
327
                sourceStoreModified = true;
328
                return;
329
            }
330
        }
331
        if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_CANCELEDITING)) {
332
            if (ownFeaturesModified) {
333
                sourceStoreModified = true;
334
                return;
335
            }
336
        }
337
    }
338
  
339
    protected void checkSourceStoreModified() {
340
        if (sourceStoreModified) {
341
                        throw new ConcurrentDataModificationException(store == null ? ""
342
                                        : store.getName());
343
        }
344
    }
345

    
346
    @Override
347
    public DisposableIterator fastIterator(long index) throws DataException {
348
        return fastIterator(index, 0);
349
    }
350
    
351
    @Override
352
    public DisposableIterator fastIterator(long index, long elements) throws DataException {
353
        if (index < 0) {
354
            throw new IndexOutOfBoundsException("The index (" + index
355
                + ") is less than 0");
356
        }
357
        DisposableIterator it;
358
        int mode = this.getIteratorMode();
359

    
360
        switch (mode) {
361
        case DEFAULT:
362
            it = new FastDefaultIterator(this, index, elements);
363
            break;
364

    
365
        case FILTERED:
366
            it = new FastFilteredIterator(this, index);
367
            break;
368

    
369
        case ORDERED:
370
            if (this.orderedData != null) {
371
                it = new FastOrderedIterator(this, index);
372
            } else {
373
                it = new FastOrderedIterator(this, new FastDefaultIterator(this, 0, elements), index);
374
            }
375
            break;
376
            
377
        case ORDERED_FILTERED:
378
            if (this.orderedData != null) {
379
                it = new FastOrderedIterator(this, index);
380
            } else {
381
                it = new FastOrderedIterator(this, new FastFilteredIterator(
382
                    this, 0), index);
383
            }
384
            break;
385

    
386
        case EDITED:
387
            it = new FastEditedIterator(this, index);
388
            break;
389

    
390
        case EDITED_FILTERED:
391
            it = new FastEditedFilteredIterator(this, index);
392
            break;
393

    
394
        case ORDERD_EDITED:
395
            if (this.orderedData != null) {
396
                it = new FastOrderedIterator(this, index);
397
            } else {
398
                it = new FastOrderedIterator(this, new FastEditedIterator(
399
                    this, 0), index);
400
            }
401
            break;
402

    
403
        case ORDERED_EDITED_FILTER:
404
            if (this.orderedData != null) {
405
                it = new FastOrderedIterator(this, index);
406
            } else {
407
                it = new FastOrderedIterator(this,
408
                    new FastEditedFilteredIterator(this, 0), index);
409
            }
410
            break;
411
            
412
        default:
413
            throw new IllegalArgumentException();
414
        }
415
        if( this.query!=null && this.query.getLimit()>0 ) {
416
            it = new LimitIterator(it,this.query.getLimit());
417
        }
418
        return it;
419
    }
420

    
421
    private class LimitIterator implements DisposableIterator {
422

    
423
        private final DisposableIterator it;
424
        private final long limit;
425
        private int count;
426

    
427
        private LimitIterator(DisposableIterator it, long limit) {
428
            this.it = it;
429
            this.limit = limit;
430
            this.count = 0;
431
        }
432

    
433
        @Override
434
        public void dispose() {
435
            this.it.dispose();
436
        }
437

    
438
        @Override
439
        public boolean hasNext() {
440
            if( this.count>=this.limit ) {
441
                return false;
442
            }
443
            return this.it.hasNext();
444
        }
445

    
446
        @Override
447
        public Object next() {
448
            if( this.count>=this.limit ) {
449
                return null;
450
            }
451
            this.count++;
452
            return this.it.next();
453
        }
454

    
455
        @Override
456
        public void remove() {
457
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
458
        }
459
        
460
    }
461

    
462
    @Override
463
    public DisposableIterator iterator(long index) throws DataException {
464
        return iterator(index,0);
465
    }
466
    
467
    @Override
468
    public DisposableIterator iterator(long index, long elements) throws DataException {        
469
        if (index < 0) {
470
            throw new IndexOutOfBoundsException("The index (" + index
471
                + ") is less than 0");
472
        }
473
        DisposableIterator it;
474
        int mode = this.getIteratorMode();
475

    
476
        switch (mode) {
477
        case DEFAULT:
478
            it = new DefaultIterator(this, index, elements);
479
            break;
480

    
481
        case FILTERED:
482
            it = new FilteredIterator(this, index);
483
            break;
484

    
485
        case ORDERED:
486
            if (orderedData != null) {
487
                it = new OrderedIterator(this, index);
488

    
489
            } else {
490
                it = new OrderedIterator(this, new DefaultIterator(this, 0, elements),index);
491
            }
492
            break;
493

    
494
        case ORDERED_FILTERED:
495
            it = new OrderedIterator(this, new FilteredIterator(this, 0),
496
                index);
497
            break;
498

    
499
        case EDITED:
500
            it = new EditedIterator(this, index);
501
            break;
502

    
503
        case EDITED_FILTERED:
504
            it = new EditedFilteredIterator(this, index);
505
            break;
506

    
507
        case ORDERD_EDITED:
508
            it = new OrderedIterator(this, new EditedIterator(this, 0), index);
509
            break;
510

    
511
        case ORDERED_EDITED_FILTER:
512
            it = new OrderedIterator(this,
513
                new EditedFilteredIterator(this, 0), index);
514
            break;
515

    
516
        default:
517
            throw new IllegalArgumentException();
518
        }
519

    
520
        if( this.query!=null && this.query.getLimit()>0 ) {
521
            it = new LimitIterator(it,this.query.getLimit());
522
        }
523
        return it;
524
    }
525

    
526
    private boolean providerCanOrder() {
527
        return this.provider.canOrder();
528
    }
529

    
530
    private boolean providerCanFilter() {
531
        return this.provider.canFilter();
532
    }
533

    
534
    private int getIteratorMode() {
535

    
536
        if (this.iteratorMode != NO_CHECKED) {
537
            return this.iteratorMode;
538
        }
539

    
540
        // TODO Tener en cuenta las transformaciones ???
541

    
542
        if (store.isEditing() && (store.getFeatureTypeManager().hasChanges() || store.getFeatureManager().hasChanges())) {
543
            if (this.query.hasOrder()) { // En edicion siempre ordeno yo.
544
                if (this.query.hasFilter()) {
545
                    return ORDERED_EDITED_FILTER;
546
                } else {
547
                    return ORDERD_EDITED;
548
                }
549
            } else {
550
                if (this.query.hasFilter()) {
551
                    return EDITED_FILTERED;
552
                } else {
553
                    return EDITED;
554
                }
555
            }
556
        } else {
557
            boolean useMyFilter = this.query.hasFilter();
558
            boolean useMyOrder = this.query.hasOrder();
559
            if (this.providerCanOrder() && this.transform.isEmpty()) {
560
                useMyOrder = false;
561
            }
562
            if (this.providerCanFilter() && this.transform.isEmpty()) {
563
                useMyFilter = false;
564
            }
565

    
566
            if (useMyOrder) {
567
                if (useMyFilter) {
568
                    return ORDERED_FILTERED;
569
                } else {
570
                    return ORDERED;
571
                }
572
            } else {
573
                if (useMyFilter) {
574
                    return FILTERED;
575
                } else {
576
                    return DEFAULT;
577
                }
578
            }
579
        }
580

    
581
    }
582

    
583
    public void delete(Feature feature) throws DataException {
584
        this.featureToIgnoreNotification = feature;
585
        this.store.delete(feature);
586
        if (this.size > 0) {
587
            this.size--;
588
        }
589
        this.featureToIgnoreNotification = null;
590
        this.ownFeaturesModified = true;
591
    }
592

    
593
    public void insert(EditableFeature feature) throws DataException {
594
        this.featureToIgnoreNotification = feature;
595
        this.store.insert(feature);
596
        if (this.size >= 0) {
597
            this.size++;
598
        }
599
        this.featureToIgnoreNotification = null;
600
        this.ownFeaturesModified = true;
601
    }
602

    
603
    public void update(EditableFeature feature) throws DataException {
604
        this.featureToIgnoreNotification = feature;
605
        this.store.update(feature);
606
        this.featureToIgnoreNotification = null;
607
        this.ownFeaturesModified = true;
608
    }
609
    
610
    public void commitChanges() throws DataException {
611
        this.ignoreChanges = true;
612
        this.store.commitChanges();
613
        this.ignoreChanges = false;
614
        
615
    }
616

    
617
    public FeatureStore getFeatureStore() {
618
        return store;
619
    }
620

    
621
}