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

History | View | Annotate | Download (18.6 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.DataStore;
33
import org.gvsig.fmap.dal.exception.DataException;
34
import org.gvsig.fmap.dal.feature.EditableFeature;
35
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
import org.gvsig.fmap.dal.feature.FeatureQueryOrder.FeatureQueryOrderMember;
40
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
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider;
49
import org.gvsig.tools.dispose.DisposableIterator;
50
import org.gvsig.tools.dynobject.DynObjectSet;
51
import org.gvsig.tools.evaluator.Evaluator;
52
import org.gvsig.tools.exception.BaseException;
53
import org.gvsig.tools.observer.Observable;
54
import org.gvsig.tools.observer.Observer;
55
import org.gvsig.tools.visitor.VisitCanceledException;
56
import org.gvsig.tools.visitor.Visitor;
57
import org.gvsig.tools.visitor.impl.AbstractIndexedVisitable;
58

    
59
public class DefaultFeatureSet extends AbstractIndexedVisitable 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.setAttributeNames(null);
142
        FeatureType ftype =
143
            transformsToUse.getSourceFeatureTypeFrom(this.defatulFeatureType);
144
        theQueryForProvider.setFeatureTypeId(ftype.getId());
145
        this.defatulFeatureTypeForProvider = ftype;
146

    
147
        if (transformsToUse.isTransformsOriginalValues()) {
148
            theQueryForProvider.setFilter(null);
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.setFilter(null);
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
        if ((mode & EDITED) != EDITED) {
239
            if (this.provider.isEmpty()) {
240
                return 0;
241
            }
242
        }
243
        if ((mode & FILTERED) == FILTERED) {
244
            long mySize = 0;
245
            DisposableIterator iter = null;
246
            try {
247
                iter = this.fastIterator();
248
                while (true) {
249
                    iter.next();
250
                    mySize++;
251
                }
252
            } catch (NoSuchElementException e) {
253
                return mySize;
254
            } finally {
255
                iter.dispose();
256
            }
257
        } else
258
            if ((mode & EDITED) == EDITED) {
259
                return provider.getSize()
260
                    + store.getFeatureManager().getDeltaSize();
261
            }
262
        return provider.getSize();
263
    }
264

    
265
    public void dispose() {
266
        this.store.deleteObserver(this);
267
        this.provider.dispose();
268
        this.provider = null;
269

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

    
284
    public boolean isFromStore(DataStore store) {
285
        return this.store.equals(store);
286
    }
287

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

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

    
295
        if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_INSERT)
296
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_DELETE)
297
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UPDATE)) {
298
            if (this.featureToIgnoreNotification == ((FeatureStoreNotification) notification)
299
                .getFeature()) {
300
                return;
301
            }
302
            sourceStoreModified = true;
303
            return;
304
        }
305
        if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UPDATE_TYPE)
306
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_REDO)
307
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UNDO)
308
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_REFRESH)
309
            || type
310
                .equalsIgnoreCase(FeatureStoreNotification.COMPLEX_NOTIFICATION)
311
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_CLOSE)
312
            || type.equalsIgnoreCase(FeatureStoreNotification.AFTER_DISPOSE)
313
            || type.equalsIgnoreCase(FeatureStoreNotification.RESOURCE_CHANGED)
314
            || type.equalsIgnoreCase(FeatureStoreNotification.TRANSFORM_CHANGE)) {
315
            sourceStoreModified = true;
316
            return;
317
        }
318
        if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_CANCELEDITING)
319
            && ownFeaturesModified) {
320
            sourceStoreModified = true;
321
            return;
322
        }
323
    }
324

    
325
    protected void doAccept(Visitor visitor, long firstValueIndex)
326
        throws VisitCanceledException, BaseException {
327
        DisposableIterator iterator = fastIterator(firstValueIndex);
328

    
329
        try {
330
            while (iterator.hasNext()) {
331
                Feature feature = (Feature) iterator.next();
332
                visitor.visit(feature);
333
            }
334
        } finally {
335
            iterator.dispose();
336
        }
337
    }
338

    
339
    protected void checkSourceStoreModified() {
340
        if (sourceStoreModified) {
341
                        throw new ConcurrentDataModificationException(store == null ? ""
342
                                        : store.getName());
343
        }
344
    }
345

    
346
    public boolean isEmpty() throws DataException {
347
        checkSourceStoreModified();
348
        return this.getSize() == 0;
349
    }
350

    
351
    public DisposableIterator fastIterator() throws DataException {
352
        return this.fastIterator(0);
353
    }
354

    
355
    public DisposableIterator fastIterator(long index) throws DataException {
356
        if (index < 0) {
357
            throw new IndexOutOfBoundsException("The index (" + index
358
                + ") is less than 0");
359
        }
360
        int mode = this.getIteratorMode();
361

    
362
        switch (mode) {
363
        case DEFAULT:
364
            return new FastDefaultIterator(this, index);
365

    
366
        case FILTERED:
367
            return new FastFilteredIterator(this, index);
368

    
369
        case ORDERED:
370
            if (this.orderedData != null) {
371
                return new FastOrderedIterator(this, index);
372
            } else {
373
                return new FastOrderedIterator(this, new FastDefaultIterator(
374
                    this, 0), index);
375
            }
376

    
377
        case ORDERED_FILTERED:
378
            if (this.orderedData != null) {
379
                return new FastOrderedIterator(this, index);
380
            } else {
381
                return new FastOrderedIterator(this, new FastFilteredIterator(
382
                    this, 0), index);
383
            }
384

    
385
        case EDITED:
386
            return new FastEditedIterator(this, index);
387

    
388
        case EDITED_FILTERED:
389
            return new FastEditedFilteredIterator(this, index);
390

    
391
        case ORDERD_EDITED:
392
            if (this.orderedData != null) {
393
                return new FastOrderedIterator(this, index);
394
            } else {
395
                return new FastOrderedIterator(this, new FastEditedIterator(
396
                    this, 0), index);
397
            }
398

    
399
        case ORDERED_EDITED_FILTER:
400
            if (this.orderedData != null) {
401
                return new FastOrderedIterator(this, index);
402
            } else {
403
                return new FastOrderedIterator(this,
404
                    new FastEditedFilteredIterator(this, 0), index);
405
            }
406
        default:
407
            throw new IllegalArgumentException();
408
        }
409
    }
410

    
411
    public DisposableIterator iterator() throws DataException {
412
        return this.iterator(0);
413
    }
414

    
415
    public DisposableIterator iterator(long index) throws DataException {
416
        if (index < 0) {
417
            throw new IndexOutOfBoundsException("The index (" + index
418
                + ") is less than 0");
419
        }
420
        int mode = this.getIteratorMode();
421

    
422
        switch (mode) {
423
        case DEFAULT:
424
            return new DefaultIterator(this, index);
425

    
426
        case FILTERED:
427
            return new FilteredIterator(this, index);
428

    
429
        case ORDERED:
430
            if (orderedData != null) {
431
                return new OrderedIterator(this, index);
432

    
433
            } else {
434
                return new OrderedIterator(this, new DefaultIterator(this, 0),
435
                    index);
436
            }
437

    
438
        case ORDERED_FILTERED:
439
            return new OrderedIterator(this, new FilteredIterator(this, 0),
440
                index);
441

    
442
        case EDITED:
443
            return new EditedIterator(this, index);
444

    
445
        case EDITED_FILTERED:
446
            return new EditedFilteredIterator(this, index);
447

    
448
        case ORDERD_EDITED:
449
            return new OrderedIterator(this, new EditedIterator(this, 0), index);
450

    
451
        case ORDERED_EDITED_FILTER:
452
            return new OrderedIterator(this,
453
                new EditedFilteredIterator(this, 0), index);
454

    
455
        default:
456
            throw new IllegalArgumentException();
457
        }
458

    
459
    }
460

    
461
    private boolean providerCanOrder() {
462
        return this.provider.canOrder();
463
    }
464

    
465
    private boolean providerCanFilter() {
466
        return this.provider.canFilter();
467
    }
468

    
469
    private int getIteratorMode() {
470

    
471
        if (this.iteratorMode != NO_CHECKED) {
472
            return this.iteratorMode;
473
        }
474

    
475
        // TODO Tener en cuenta las transformaciones ???
476

    
477
        if (store.isEditing() && store.getFeatureManager().hasChanges()) {
478
            if (this.query.hasOrder()) { // En edicion siempre ordeno yo.
479
                if (this.query.hasFilter()) {
480
                    return ORDERED_EDITED_FILTER;
481
                } else {
482
                    return ORDERD_EDITED;
483
                }
484
            } else {
485
                if (this.query.hasFilter()) {
486
                    return EDITED_FILTERED;
487
                } else {
488
                    return EDITED;
489
                }
490
            }
491
        } else {
492
            boolean useMyFilter = this.query.hasFilter();
493
            boolean useMyOrder = this.query.hasOrder();
494
            if (this.providerCanOrder() && this.transform.isEmpty()) {
495
                useMyOrder = false;
496
            }
497
            if (this.providerCanFilter() && this.transform.isEmpty()) {
498
                useMyFilter = false;
499
            }
500

    
501
            if (useMyOrder) {
502
                if (useMyFilter) {
503
                    return ORDERED_FILTERED;// ORDERED_FILTERED;
504
                } else {
505
                    return ORDERED;// ORDERED;
506
                }
507
            } else {
508
                if (useMyFilter) {
509
                    return FILTERED;// FILTERED;
510
                } else {
511
                    return DEFAULT;// DEFAULT;
512
                }
513
            }
514
        }
515

    
516
    }
517

    
518
    public void delete(Feature feature) throws DataException {
519
        this.featureToIgnoreNotification = feature;
520
        this.store.delete(feature);
521
        if (this.size > 0) {
522
            this.size--;
523
        }
524
        this.featureToIgnoreNotification = null;
525
        this.ownFeaturesModified = true;
526
    }
527

    
528
    public void insert(EditableFeature feature) throws DataException {
529
        this.featureToIgnoreNotification = feature;
530
        this.store.insert(feature);
531
        if (this.size >= 0) {
532
            this.size++;
533
        }
534
        this.featureToIgnoreNotification = null;
535
        this.ownFeaturesModified = true;
536
    }
537

    
538
    public void update(EditableFeature feature) throws DataException {
539
        this.featureToIgnoreNotification = feature;
540
        this.store.update(feature);
541
        this.featureToIgnoreNotification = null;
542
        this.ownFeaturesModified = true;
543
    }
544

    
545
    public DynObjectSet getDynObjectSet() {
546
        return this.getDynObjectSet(true);
547
    }
548

    
549
    public DynObjectSet getDynObjectSet(boolean fast) {
550
        return new DynObjectSetFeatureSetFacade(this, store, fast);
551
    }
552

    
553
    public FeatureStore getFeatureStore() {
554
        return store;
555
    }
556
}