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

History | View | Annotate | Download (17.8 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
        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 void update(Observable obsevable, Object notification) {
285
        if (sourceStoreModified) {
286
            return;
287
        }
288

    
289
        String type = ((FeatureStoreNotification) notification).getType();
290

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

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

    
341
        switch (mode) {
342
        case DEFAULT:
343
            return new FastDefaultIterator(this, index, elements);
344

    
345
        case FILTERED:
346
            return new FastFilteredIterator(this, index);
347

    
348
        case ORDERED:
349
            if (this.orderedData != null) {
350
                return new FastOrderedIterator(this, index);
351
            } else {
352
                return new FastOrderedIterator(this, new FastDefaultIterator(this, 0, elements), index);
353
            }
354

    
355
        case ORDERED_FILTERED:
356
            if (this.orderedData != null) {
357
                return new FastOrderedIterator(this, index);
358
            } else {
359
                return new FastOrderedIterator(this, new FastFilteredIterator(
360
                    this, 0), index);
361
            }
362

    
363
        case EDITED:
364
            return new FastEditedIterator(this, index);
365

    
366
        case EDITED_FILTERED:
367
            return new FastEditedFilteredIterator(this, index);
368

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

    
377
        case ORDERED_EDITED_FILTER:
378
            if (this.orderedData != null) {
379
                return new FastOrderedIterator(this, index);
380
            } else {
381
                return new FastOrderedIterator(this,
382
                    new FastEditedFilteredIterator(this, 0), index);
383
            }
384
        default:
385
            throw new IllegalArgumentException();
386
        }
387
    }
388

    
389

    
390
    @Override
391
    public DisposableIterator iterator(long index) throws DataException {
392
        return iterator(index,0);
393
    }
394
    
395
    @Override
396
    public DisposableIterator iterator(long index, long elements) throws DataException {        
397
        if (index < 0) {
398
            throw new IndexOutOfBoundsException("The index (" + index
399
                + ") is less than 0");
400
        }
401
        int mode = this.getIteratorMode();
402

    
403
        switch (mode) {
404
        case DEFAULT:
405
            return new DefaultIterator(this, index, elements);
406

    
407
        case FILTERED:
408
            return new FilteredIterator(this, index);
409

    
410
        case ORDERED:
411
            if (orderedData != null) {
412
                return new OrderedIterator(this, index);
413

    
414
            } else {
415
                return new OrderedIterator(this, new DefaultIterator(this, 0, elements),index);
416
            }
417

    
418
        case ORDERED_FILTERED:
419
            return new OrderedIterator(this, new FilteredIterator(this, 0),
420
                index);
421

    
422
        case EDITED:
423
            return new EditedIterator(this, index);
424

    
425
        case EDITED_FILTERED:
426
            return new EditedFilteredIterator(this, index);
427

    
428
        case ORDERD_EDITED:
429
            return new OrderedIterator(this, new EditedIterator(this, 0), index);
430

    
431
        case ORDERED_EDITED_FILTER:
432
            return new OrderedIterator(this,
433
                new EditedFilteredIterator(this, 0), index);
434

    
435
        default:
436
            throw new IllegalArgumentException();
437
        }
438

    
439
    }
440

    
441
    private boolean providerCanOrder() {
442
        return this.provider.canOrder();
443
    }
444

    
445
    private boolean providerCanFilter() {
446
        return this.provider.canFilter();
447
    }
448

    
449
    private int getIteratorMode() {
450

    
451
        if (this.iteratorMode != NO_CHECKED) {
452
            return this.iteratorMode;
453
        }
454

    
455
        // TODO Tener en cuenta las transformaciones ???
456

    
457
        if (store.isEditing() && store.getFeatureManager().hasChanges()) {
458
            if (this.query.hasOrder()) { // En edicion siempre ordeno yo.
459
                if (this.query.hasFilter()) {
460
                    return ORDERED_EDITED_FILTER;
461
                } else {
462
                    return ORDERD_EDITED;
463
                }
464
            } else {
465
                if (this.query.hasFilter()) {
466
                    return EDITED_FILTERED;
467
                } else {
468
                    return EDITED;
469
                }
470
            }
471
        } else {
472
            boolean useMyFilter = this.query.hasFilter();
473
            boolean useMyOrder = this.query.hasOrder();
474
            if (this.providerCanOrder() && this.transform.isEmpty()) {
475
                useMyOrder = false;
476
            }
477
            if (this.providerCanFilter() && this.transform.isEmpty()) {
478
                useMyFilter = false;
479
            }
480

    
481
            if (useMyOrder) {
482
                if (useMyFilter) {
483
                    return ORDERED_FILTERED;// ORDERED_FILTERED;
484
                } else {
485
                    return ORDERED;// ORDERED;
486
                }
487
            } else {
488
                if (useMyFilter) {
489
                    return FILTERED;// FILTERED;
490
                } else {
491
                    return DEFAULT;// DEFAULT;
492
                }
493
            }
494
        }
495

    
496
    }
497

    
498
    public void delete(Feature feature) throws DataException {
499
        this.featureToIgnoreNotification = feature;
500
        this.store.delete(feature);
501
        if (this.size > 0) {
502
            this.size--;
503
        }
504
        this.featureToIgnoreNotification = null;
505
        this.ownFeaturesModified = true;
506
    }
507

    
508
    public void insert(EditableFeature feature) throws DataException {
509
        this.featureToIgnoreNotification = feature;
510
        this.store.insert(feature);
511
        if (this.size >= 0) {
512
            this.size++;
513
        }
514
        this.featureToIgnoreNotification = null;
515
        this.ownFeaturesModified = true;
516
    }
517

    
518
    public void update(EditableFeature feature) throws DataException {
519
        this.featureToIgnoreNotification = feature;
520
        this.store.update(feature);
521
        this.featureToIgnoreNotification = null;
522
        this.ownFeaturesModified = true;
523
    }
524

    
525
    public FeatureStore getFeatureStore() {
526
        return store;
527
    }
528

    
529
}