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

History | View | Annotate | Download (19.7 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 org.gvsig.fmap.dal.feature.impl.dynobjectutils.DynObjectSetFeatureSetFacade;
27
import java.util.ArrayList;
28
import java.util.Collections;
29
import java.util.Iterator;
30
import java.util.List;
31
import java.util.NoSuchElementException;
32

    
33
import org.gvsig.fmap.dal.DataStore;
34
import org.gvsig.fmap.dal.exception.DataException;
35
import org.gvsig.fmap.dal.feature.EditableFeature;
36
import org.gvsig.fmap.dal.feature.Feature;
37
import org.gvsig.fmap.dal.feature.FeatureIndexes;
38
import org.gvsig.fmap.dal.feature.FeatureQuery;
39
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
40
import org.gvsig.fmap.dal.feature.FeatureQueryOrder.FeatureQueryOrderMember;
41
import org.gvsig.fmap.dal.feature.FeatureSet;
42
import org.gvsig.fmap.dal.feature.FeatureStore;
43
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
44
import org.gvsig.fmap.dal.feature.FeatureType;
45
import org.gvsig.fmap.dal.feature.exception.ConcurrentDataModificationException;
46
import org.gvsig.fmap.dal.feature.exception.FeatureSetInitializeException;
47
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore;
48
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStoreTransforms;
49
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider;
50
import org.gvsig.tools.dispose.DisposableIterator;
51
import org.gvsig.tools.dynobject.DynObjectSet;
52
import org.gvsig.tools.evaluator.Evaluator;
53
import org.gvsig.tools.exception.BaseException;
54
import org.gvsig.tools.observer.Observable;
55
import org.gvsig.tools.observer.Observer;
56
import org.gvsig.tools.visitor.VisitCanceledException;
57
import org.gvsig.tools.visitor.Visitor;
58

    
59
public class DefaultFeatureSet extends AbstractFeatureSet implements
60
    FeatureSet, Observer {
61

    
62
    private static final int NO_CHECKED = -1;
63
    private static final int DEFAULT = 0;
64
    private static final int FILTERED = 1;
65
    private static final int ORDERED = 2;
66
    private static final int ORDERED_FILTERED = 3;
67
    private static final int EDITED = 4;
68
    private static final int EDITED_FILTERED = 5;
69
    private static final int ORDERD_EDITED = 6;
70
    private static final int ORDERED_EDITED_FILTER = 7;
71

    
72
    private boolean sourceStoreModified;
73
    private boolean ownFeaturesModified;
74
    DefaultFeatureStore store;
75
    private List featureTypes;
76
    FeatureQuery query;
77
    FeatureSetProvider provider;
78
    private long size;
79
    private int iteratorMode;
80
    List orderedData;
81
    private Feature featureToIgnoreNotification;
82
    DefaultFeatureStoreTransforms transform;
83
    private FeatureQuery queryForProvider;
84
    private FeatureType defatulFeatureType;
85
    private FeatureType defatulFeatureTypeForProvider;
86

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

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

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

    
139
    private void fixQueryForProvider(FeatureQuery theQueryForProvider,
140
        DefaultFeatureStoreTransforms transformsToUse) throws DataException {
141
        theQueryForProvider.clearAttributeNames();
142
        FeatureType ftype =
143
            transformsToUse.getSourceFeatureTypeFrom(this.defatulFeatureType);
144
        theQueryForProvider.setFeatureTypeId(ftype.getId());
145
        this.defatulFeatureTypeForProvider = ftype;
146

    
147
        if (transformsToUse.isTransformsOriginalValues()) {
148
            theQueryForProvider.clearFilter();
149
            FeatureQueryOrder fqo = theQueryForProvider.getOrder();
150
            if (fqo != null) {
151
                fqo.clear();
152
            }
153
            return;
154

    
155
        }
156

    
157
        // Filter
158
        Evaluator filter = theQueryForProvider.getFilter();
159
        if (filter != null) {
160
            boolean canUseFilter = true;
161
            if (filter.getFieldsInfo() == null) {
162
                canUseFilter = false;
163
            } else {
164
                canUseFilter = areEvaluatorFieldsInAttributes(filter, ftype);
165
            }
166

    
167
            if (!canUseFilter) {
168
                theQueryForProvider.clearFilter();
169
            }
170

    
171
        }
172

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

    
194
            if (!canUseOrder) {
195
                theQueryForProvider.getOrder().clear();
196
            }
197
        }
198

    
199
    }
200

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

    
215
            }
216
        }
217
        return true;
218
    }
219

    
220
    public FeatureType getDefaultFeatureType() {
221
        return this.defatulFeatureType;
222
    }
223

    
224
    public List getFeatureTypes() {
225
        return Collections.unmodifiableList(this.featureTypes);
226
    }
227

    
228
    public long getSize() throws DataException {
229
        this.checkSourceStoreModified();
230
        if (size < 0) {
231
            size = calculateSize();
232
        }
233
        return size;
234
    }
235

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

    
268
    public void dispose() {
269
        this.store.deleteObserver(this);
270
        this.provider.dispose();
271
        this.provider = null;
272

    
273
        this.featureToIgnoreNotification = null;
274
        if (orderedData != null) {
275
            orderedData.clear();
276
        }
277
        this.orderedData = null;
278
        this.store = null;
279
        this.transform = null;
280
        this.query = null;
281
        this.queryForProvider = null;
282
        this.featureTypes = null;
283
        this.defatulFeatureType = null;
284
        this.defatulFeatureTypeForProvider = null;
285
    }
286

    
287
    public void update(Observable obsevable, Object notification) {
288
        if (sourceStoreModified) {
289
            return;
290
        }
291

    
292
        String type = ((FeatureStoreNotification) notification).getType();
293

    
294
        if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_INSERT)
295
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_DELETE)
296
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UPDATE)) {
297
            if (this.featureToIgnoreNotification == ((FeatureStoreNotification) notification)
298
                .getFeature()) {
299
                return;
300
            }
301
            sourceStoreModified = true;
302
            return;
303
        }
304
        if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UPDATE_TYPE)
305
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_REDO)
306
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UNDO)
307
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_REFRESH)
308
            || type
309
                .equalsIgnoreCase(FeatureStoreNotification.COMPLEX_NOTIFICATION)
310
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_CLOSE)
311
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_DISPOSE)
312
            || type.equalsIgnoreCase(FeatureStoreNotification.RESOURCE_CHANGED)
313
            || type.equalsIgnoreCase(FeatureStoreNotification.TRANSFORM_CHANGE)) {
314
            sourceStoreModified = true;
315
            return;
316
        }
317
        if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_CANCELEDITING)
318
            && ownFeaturesModified) {
319
            sourceStoreModified = true;
320
            return;
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 FeatureStore getFeatureStore() {
596
        return store;
597
    }
598

    
599
}