Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeatureStore.java @ 39409

History | View | Annotate | Download (69.4 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 IVER T.I. S.A.   {{Task}}
26
 */
27

    
28
package org.gvsig.fmap.dal.feature.impl;
29

    
30
import java.util.ArrayList;
31
import java.util.Collection;
32
import java.util.Collections;
33
import java.util.HashMap;
34
import java.util.HashSet;
35
import java.util.Iterator;
36
import java.util.List;
37
import java.util.Map;
38
import java.util.Map.Entry;
39
import java.util.Set;
40

    
41
import org.cresques.cts.IProjection;
42
import org.slf4j.Logger;
43
import org.slf4j.LoggerFactory;
44

    
45
import org.gvsig.fmap.dal.DALLocator;
46
import org.gvsig.fmap.dal.DataManager;
47
import org.gvsig.fmap.dal.DataQuery;
48
import org.gvsig.fmap.dal.DataServerExplorer;
49
import org.gvsig.fmap.dal.DataSet;
50
import org.gvsig.fmap.dal.DataStore;
51
import org.gvsig.fmap.dal.DataStoreNotification;
52
import org.gvsig.fmap.dal.DataStoreParameters;
53
import org.gvsig.fmap.dal.exception.CloneException;
54
import org.gvsig.fmap.dal.exception.CloseException;
55
import org.gvsig.fmap.dal.exception.CreateException;
56
import org.gvsig.fmap.dal.exception.DataException;
57
import org.gvsig.fmap.dal.exception.InitializeException;
58
import org.gvsig.fmap.dal.exception.OpenException;
59
import org.gvsig.fmap.dal.exception.ReadException;
60
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
61
import org.gvsig.fmap.dal.feature.EditableFeature;
62
import org.gvsig.fmap.dal.feature.EditableFeatureType;
63
import org.gvsig.fmap.dal.feature.Feature;
64
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
65
import org.gvsig.fmap.dal.feature.FeatureCache;
66
import org.gvsig.fmap.dal.feature.FeatureIndex;
67
import org.gvsig.fmap.dal.feature.FeatureIndexes;
68
import org.gvsig.fmap.dal.feature.FeatureLocks;
69
import org.gvsig.fmap.dal.feature.FeatureQuery;
70
import org.gvsig.fmap.dal.feature.FeatureReference;
71
import org.gvsig.fmap.dal.feature.FeatureReferenceSelection;
72
import org.gvsig.fmap.dal.feature.FeatureSelection;
73
import org.gvsig.fmap.dal.feature.FeatureSet;
74
import org.gvsig.fmap.dal.feature.FeatureStore;
75
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
76
import org.gvsig.fmap.dal.feature.FeatureStoreTransforms;
77
import org.gvsig.fmap.dal.feature.FeatureType;
78
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
79
import org.gvsig.fmap.dal.feature.exception.AlreadyEditingException;
80
import org.gvsig.fmap.dal.feature.exception.ConcurrentDataModificationException;
81
import org.gvsig.fmap.dal.feature.exception.CreateFeatureException;
82
import org.gvsig.fmap.dal.feature.exception.DataExportException;
83
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException;
84
import org.gvsig.fmap.dal.feature.exception.FinishEditingException;
85
import org.gvsig.fmap.dal.feature.exception.GetFeatureTypeException;
86
import org.gvsig.fmap.dal.feature.exception.IllegalFeatureException;
87
import org.gvsig.fmap.dal.feature.exception.IllegalFeatureTypeException;
88
import org.gvsig.fmap.dal.feature.exception.NeedEditingModeException;
89
import org.gvsig.fmap.dal.feature.exception.NoNewFeatureInsertException;
90
import org.gvsig.fmap.dal.feature.exception.NullFeatureTypeException;
91
import org.gvsig.fmap.dal.feature.exception.PersistenceCantFindDefaultFeatureTypeException;
92
import org.gvsig.fmap.dal.feature.exception.PersistenceCantFindFeatureTypeException;
93
import org.gvsig.fmap.dal.feature.exception.PersistenceStoreAlreadyLoadedException;
94
import org.gvsig.fmap.dal.feature.exception.SelectionNotAllowedException;
95
import org.gvsig.fmap.dal.feature.exception.StoreCancelEditingException;
96
import org.gvsig.fmap.dal.feature.exception.StoreDeleteEditableFeatureException;
97
import org.gvsig.fmap.dal.feature.exception.StoreDeleteFeatureException;
98
import org.gvsig.fmap.dal.feature.exception.StoreEditException;
99
import org.gvsig.fmap.dal.feature.exception.StoreInsertFeatureException;
100
import org.gvsig.fmap.dal.feature.exception.StoreUpdateFeatureException;
101
import org.gvsig.fmap.dal.feature.exception.StoreUpdateFeatureTypeException;
102
import org.gvsig.fmap.dal.feature.exception.ValidateFeaturesException;
103
import org.gvsig.fmap.dal.feature.exception.WriteNotAllowedException;
104
import org.gvsig.fmap.dal.feature.impl.expansionadapter.MemoryExpansionAdapter;
105
import org.gvsig.fmap.dal.feature.impl.featureset.DefaultFeatureSet;
106
import org.gvsig.fmap.dal.feature.impl.undo.DefaultFeatureCommandsStack;
107
import org.gvsig.fmap.dal.feature.impl.undo.FeatureCommandsStack;
108
import org.gvsig.fmap.dal.feature.spi.DefaultFeatureProvider;
109
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
110
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
111
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
112
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices;
113
import org.gvsig.fmap.dal.feature.spi.cache.FeatureCacheProvider;
114
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProviderServices;
115
import org.gvsig.fmap.dal.impl.DefaultDataManager;
116
import org.gvsig.fmap.dal.resource.Resource;
117
import org.gvsig.fmap.dal.spi.DataStoreInitializer;
118
import org.gvsig.fmap.dal.spi.DataStoreProvider;
119
import org.gvsig.fmap.geom.primitive.Envelope;
120
import org.gvsig.metadata.MetadataLocator;
121
import org.gvsig.metadata.MetadataManager;
122
import org.gvsig.metadata.exceptions.MetadataException;
123
import org.gvsig.timesupport.Interval;
124
import org.gvsig.tools.ToolsLocator;
125
import org.gvsig.tools.dispose.DisposableIterator;
126
import org.gvsig.tools.dispose.impl.AbstractDisposable;
127
import org.gvsig.tools.dynobject.DelegatedDynObject;
128
import org.gvsig.tools.dynobject.DynClass;
129
import org.gvsig.tools.dynobject.DynObject;
130
import org.gvsig.tools.dynobject.DynObjectManager;
131
import org.gvsig.tools.dynobject.DynStruct;
132
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
133
import org.gvsig.tools.dynobject.exception.DynMethodException;
134
import org.gvsig.tools.exception.BaseException;
135
import org.gvsig.tools.exception.NotYetImplemented;
136
import org.gvsig.tools.observer.Observable;
137
import org.gvsig.tools.observer.Observer;
138
import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable;
139
import org.gvsig.tools.persistence.PersistenceManager;
140
import org.gvsig.tools.persistence.Persistent;
141
import org.gvsig.tools.persistence.PersistentState;
142
import org.gvsig.tools.persistence.exception.PersistenceException;
143
import org.gvsig.tools.undo.RedoException;
144
import org.gvsig.tools.undo.UndoException;
145
import org.gvsig.tools.undo.command.Command;
146
import org.gvsig.tools.visitor.Visitor;
147

    
148
public final class DefaultFeatureStore extends AbstractDisposable implements
149
    DataStoreInitializer, FeatureStoreProviderServices, FeatureStore, Observer {
150

    
151
    private static final Logger LOG = LoggerFactory
152
        .getLogger(DefaultFeatureStore.class);
153

    
154
    private static final String PERSISTENCE_DEFINITION_NAME = "FeatureStore";
155

    
156
    private DataStoreParameters parameters = null;
157
    private FeatureSelection selection;
158
    private FeatureLocks locks;
159

    
160
    private DelegateWeakReferencingObservable delegateObservable =
161
        new DelegateWeakReferencingObservable(this);
162

    
163
    private FeatureCommandsStack commands;
164
    private FeatureTypeManager featureTypeManager;
165
    private FeatureManager featureManager;
166
    private SpatialManager spatialManager;
167

    
168
    private FeatureType defaultFeatureType = null;
169
    private List featureTypes = new ArrayList();
170

    
171
    private int mode = MODE_QUERY;
172
    private long versionOfUpdate = 0;
173
    private boolean hasStrongChanges = true;
174
    private boolean hasInserts = true;
175

    
176
    private DefaultDataManager dataManager = null;
177

    
178
    private FeatureStoreProvider provider = null;
179

    
180
    private DefaultFeatureIndexes indexes;
181

    
182
    private DefaultFeatureStoreTransforms transforms;
183

    
184
    DelegatedDynObject metadata;
185
    
186
    private Set metadataChildren;
187

    
188
    private Long featureCount = null;
189

    
190
    private long temporalOid = 0;
191

    
192
    private FeatureCacheProvider cache;
193

    
194
    /*
195
     * TODO:
196
     * 
197
     * - Comprobar que solo se pueden a�adir reglas de validacion sobre un
198
     * EditableFeatureType. - Comprobar que solo se puede hacer un update con un
199
     * featureType al que se le han cambiado las reglas de validacion cuando
200
     * hasStrongChanges=false.
201
     */
202

    
203
    public DefaultFeatureStore() {
204

    
205
    }
206

    
207
    public void intializePhase1(DataManager dataManager,
208
        DataStoreParameters parameters) throws InitializeException {
209

    
210
        DynObjectManager dynManager = ToolsLocator.getDynObjectManager();
211

    
212
        this.metadata =
213
            (DelegatedDynObject) dynManager.createDynObject(
214
                METADATA_DEFINITION_NAME, MetadataManager.METADATA_NAMESPACE);
215

    
216
        this.dataManager = (DefaultDataManager) dataManager;
217

    
218
        this.parameters = parameters;
219
        this.transforms = new DefaultFeatureStoreTransforms(this);
220
        try {
221
            indexes = new DefaultFeatureIndexes(this);
222
        } catch (DataException e) {
223
            throw new InitializeException(e);
224
        }
225

    
226
    }
227

    
228
    public void intializePhase2(DataStoreProvider provider) {
229
        this.provider = (FeatureStoreProvider) provider;
230
        this.delegate(provider);
231
        this.metadataChildren = new HashSet();
232
        this.metadataChildren.add(provider);
233
    }
234

    
235
    public DataStoreParameters getParameters() {
236
        return parameters;
237
    }
238

    
239
    public int getMode() {
240
        return this.mode;
241
    }
242

    
243
    public DataManager getManager() {
244
        return this.dataManager;
245
    }
246

    
247
    public Iterator getChildren() {
248
        return this.provider.getChilds();
249
    }
250

    
251
    public FeatureStoreProvider getProvider() {
252
        return this.provider;
253
    }
254

    
255
    public FeatureManager getFeatureManager() {
256
        return this.featureManager;
257
    }
258

    
259
    public void setFeatureTypes(List types, FeatureType defaultType) {
260
        this.featureTypes = types;
261
        this.defaultFeatureType = defaultType;
262
    }
263

    
264
    public void open() throws OpenException {
265
        // TODO: Se puede hacer un open estando en edicion ?
266
        this.notifyChange(DataStoreNotification.BEFORE_OPEN);
267
        this.provider.open();
268
        this.notifyChange(DataStoreNotification.AFTER_OPEN);
269
    }
270

    
271
    public void refresh() throws OpenException, InitializeException {
272
        if (this.mode != MODE_QUERY) {
273
            throw new IllegalStateException();
274
        }
275
        this.notifyChange(FeatureStoreNotification.BEFORE_REFRESH);
276
        this.featureCount = null;
277
        this.provider.refresh();
278
        this.notifyChange(FeatureStoreNotification.AFTER_REFRESH);
279
    }
280

    
281
    public void close() throws CloseException {
282
        // TODO: Se puede hacer un close estando en edicion ?
283
        this.notifyChange(DataStoreNotification.BEFORE_CLOSE);
284
        this.featureCount = null;
285
        this.provider.close();
286
        this.notifyChange(DataStoreNotification.AFTER_CLOSE);
287
    }
288

    
289
    protected void doDispose() throws BaseException {
290
        this.notifyChange(DataStoreNotification.BEFORE_DISPOSE);
291
        this.disposeIndexes();
292
        this.provider.dispose();
293
        if (this.selection != null) {
294
            this.selection.dispose();
295
            this.selection = null;
296
        }
297
        this.commands = null;
298
        this.featureCount = null;
299
        if (this.locks != null) {
300
            // this.locks.dispose();
301
            this.locks = null;
302
        }
303

    
304
        if (this.featureTypeManager != null) {
305
            this.featureTypeManager.dispose();
306
            this.featureTypeManager = null;
307
        }
308

    
309
        this.featureManager = null;
310
        this.spatialManager = null;
311

    
312
        this.parameters = null;
313
        this.notifyChange(DataStoreNotification.AFTER_DISPOSE);
314
        if (delegateObservable != null) {
315
            this.delegateObservable.deleteObservers();
316
            this.delegateObservable = null;
317
        }
318
    }
319

    
320
    public boolean allowWrite() {
321
        return this.provider.allowWrite();
322
    }
323

    
324
    public boolean canWriteGeometry(int geometryType) throws DataException {
325
        return this.provider.canWriteGeometry(geometryType, 0);
326
    }
327

    
328
    public DataServerExplorer getExplorer() throws ReadException,
329
        ValidateDataParametersException {
330
        return this.provider.getExplorer();
331
    }
332

    
333
    /*
334
     * public Metadata getMetadata() throws MetadataNotFoundException {
335
     * // TODO:
336
     * // Si el provider devuelbe null habria que ver de construir aqui
337
     * // los metadatos basicos, como el Envelope y el SRS.
338
     * 
339
     * // TODO: Estando en edicion el Envelope deberia de
340
     * // actualizarse usando el spatialManager
341
     * return this.provider.getMetadata();
342
     * }
343
     */
344

    
345
    public Envelope getEnvelope() throws DataException {
346
        if (this.mode == MODE_FULLEDIT) {
347
                // Just in case another thread tries to write in the store
348
                synchronized (this) {                                
349
                        return this.spatialManager.getEnvelope();
350
                        }
351
        }
352
        if (hasDynValue(DataStore.METADATA_ENVELOPE)){
353
            return (Envelope)getDynValue(DataStore.METADATA_ENVELOPE);
354
        }
355
        return this.provider.getEnvelope();
356
    }
357

    
358
    /**
359
     * @deprecated use getDefaultFeatureType().getDefaultSRS()
360
     */
361
    public IProjection getSRSDefaultGeometry() throws DataException {
362
        return this.getDefaultFeatureType().getDefaultSRS();
363
    }
364

    
365
    public FeatureSelection createDefaultFeatureSelection()
366
        throws DataException {
367
        return new DefaultFeatureSelection(this);
368
    }
369

    
370
    public FeatureProvider createDefaultFeatureProvider(FeatureType type)
371
        throws DataException {
372
        if (type.hasOID()) {
373
            return new DefaultFeatureProvider(type,
374
                this.provider.createNewOID());
375
        }
376
        return new DefaultFeatureProvider(type);
377
    }
378

    
379
    public void saveToState(PersistentState state) throws PersistenceException {
380
        if (this.mode != FeatureStore.MODE_QUERY) {
381
            throw new PersistenceException(new IllegalStateException(
382
                this.getName()));
383
        }
384
        state.set("dataStoreName", this.getName());
385
        state.set("parameters", this.parameters);
386
        state.set("selection", this.selection);
387
        state.set("transforms", this.transforms);
388
        // TODO locks persistence
389
        // state.set("locks", this.locks);
390
        // TODO indexes persistence
391
        // state.set("indexes", this.indexes);
392
        Map evaluatedAttr = new HashMap(1);
393
        Iterator iterType = featureTypes.iterator();
394
        Iterator iterAttr;
395
        FeatureType type;
396
        DefaultFeatureAttributeDescriptor attr;
397
        List attrs;
398
        while (iterType.hasNext()) {
399
            type = (FeatureType) iterType.next();
400
            attrs = new ArrayList();
401
            iterAttr = type.iterator();
402
            while (iterAttr.hasNext()) {
403
                attr = (DefaultFeatureAttributeDescriptor) iterAttr.next();
404
                if ((attr.getEvaluator() != null)
405
                    && (attr.getEvaluator() instanceof Persistent)) {
406
                    attrs.add(attr);
407
                }
408
            }
409
            if (!attrs.isEmpty()) {
410
                evaluatedAttr.put(type.getId(), attrs);
411
            }
412

    
413
        }
414

    
415
        if (evaluatedAttr.isEmpty()) {
416
            evaluatedAttr = null;
417
        }
418

    
419
        state.set("evaluatedAttributes", evaluatedAttr);
420
        state.set("defaultFeatureTypeId", defaultFeatureType.getId());
421

    
422
    }
423

    
424
    public void loadFromState(PersistentState state)
425
        throws PersistenceException {
426
        if (this.provider != null) {
427
            throw new PersistenceStoreAlreadyLoadedException(this.getName());
428
        }
429
        if (this.getManager() == null) {
430
            this.dataManager = (DefaultDataManager) DALLocator.getDataManager();
431
        }
432

    
433
        DataStoreParameters params =
434
            (DataStoreParameters) state.get("parameters");
435

    
436
        try {
437

    
438
            this.dataManager.intializeDataStore(this, params);
439
            this.selection = (FeatureSelection) state.get("selection");
440
            this.transforms =
441
                (DefaultFeatureStoreTransforms) state.get("transforms");
442
            Map evaluatedAttributes = (Map) state.get("evaluatedAttributes");
443
            if ((evaluatedAttributes != null) && !evaluatedAttributes.isEmpty()) {
444
                List attrs;
445
                Iterator iterEntries =
446
                    evaluatedAttributes.entrySet().iterator();
447
                Entry entry;
448
                while (iterEntries.hasNext()) {
449
                    entry = (Entry) iterEntries.next();
450
                    attrs = (List) entry.getValue();
451
                    if (attrs.isEmpty()) {
452
                        continue;
453
                    }
454
                    int fTypePos = -1;
455
                    DefaultFeatureType type = null;
456
                    for (int i = 0; i < featureTypes.size(); i++) {
457
                        type = (DefaultFeatureType) featureTypes.get(i);
458
                        if (type.getId().equals(entry.getKey())) {
459
                            fTypePos = i;
460
                            break;
461
                        }
462
                    }
463
                    if (fTypePos < 0) {
464
                        throw new PersistenceCantFindFeatureTypeException(
465
                            this.getName(), (String) entry.getKey());
466
                    }
467
                    DefaultEditableFeatureType eType =
468
                        (DefaultEditableFeatureType) type.getEditable();
469
                    Iterator iterAttr = attrs.iterator();
470
                    FeatureAttributeDescriptor attr;
471
                    while (iterAttr.hasNext()) {
472
                        attr = (FeatureAttributeDescriptor) iterAttr.next();
473
                        eType.addLike(attr);
474
                    }
475
                    featureTypes.set(fTypePos, eType.getNotEditableCopy());
476

    
477
                }
478

    
479
            }
480

    
481
            String defFTypeid = state.getString("defaultFeatureTypeId");
482
            FeatureType ftype = null;
483
            if (!this.defaultFeatureType.getId().equals(
484
                state.getString("defaultFeatureTypeId"))) {
485

    
486
                ftype = getFeatureType(defFTypeid);
487
                if (ftype == null) {
488
                    throw new PersistenceCantFindDefaultFeatureTypeException(
489
                        this.getName(), defFTypeid);
490
                }
491
                this.defaultFeatureType = ftype;
492
            }
493

    
494
        } catch (InitializeException e) {
495
            throw new PersistenceException(e);
496
        } catch (DataException e) {
497
            throw new PersistenceException(e);
498
        }
499

    
500
    }
501

    
502
    public static void registerPersistenceDefinition() {
503
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
504
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
505
            DynStruct definition =
506
                manager.addDefinition(DefaultFeatureStore.class,
507
                    PERSISTENCE_DEFINITION_NAME, PERSISTENCE_DEFINITION_NAME
508
                        + " Persistent definition", null, null);
509
            definition.addDynFieldString("dataStoreName").setMandatory(true)
510
                .setPersistent(true);
511

    
512
            definition.addDynFieldObject("parameters")
513
                .setClassOfValue(DynObject.class).setMandatory(true)
514
                .setPersistent(true);
515

    
516
            definition.addDynFieldObject("selection")
517
                .setClassOfValue(FeatureSelection.class).setMandatory(false)
518
                .setPersistent(true);
519

    
520
            definition.addDynFieldObject("transforms")
521
                .setClassOfValue(DefaultFeatureStoreTransforms.class)
522
                .setMandatory(true).setPersistent(true);
523

    
524
            definition.addDynFieldMap("evaluatedAttributes")
525
                .setClassOfItems(List.class) // List<DefaultFeatureAttributeDescriptor>
526
                .setMandatory(false).setPersistent(true);
527

    
528
            definition.addDynFieldString("defaultFeatureTypeId")
529
                .setMandatory(true).setPersistent(true);
530
        }
531
    }
532

    
533
    public static void registerMetadataDefinition() throws MetadataException {
534
        MetadataManager manager = MetadataLocator.getMetadataManager();
535
        if (manager.getDefinition(METADATA_DEFINITION_NAME) == null) {
536
            DynStruct metadataDefinition =
537
                manager.addDefinition(METADATA_DEFINITION_NAME, null);
538
            metadataDefinition.extend(manager
539
                .getDefinition(DataStore.METADATA_DEFINITION_NAME));
540
        }
541
    }
542

    
543
    //
544
    // ====================================================================
545
    // Gestion de la seleccion
546
    //
547

    
548
    public void setSelection(DataSet selection) throws DataException {
549
        this.setSelection((FeatureSet) selection);
550
    }
551

    
552
    public DataSet createSelection() throws DataException {
553
        return createFeatureSelection();
554
    }
555

    
556
    public DataSet getSelection() throws DataException {
557
        return this.getFeatureSelection();
558
    }
559

    
560
    public void setSelection(FeatureSet selection) throws DataException {
561
        setSelection(selection, true);
562
    }
563

    
564
    /**
565
     * @see #setSelection(FeatureSet)
566
     * @param undoable
567
     *            if the action must be undoable
568
     */
569
    public void setSelection(FeatureSet selection, boolean undoable)
570
        throws DataException {
571
        if (selection == null) {
572
            if (undoable) {
573
                throw new SelectionNotAllowedException(getName());
574
            }
575

    
576
        } else {
577
            if (selection.equals(this.selection)) {
578
                return;
579
            }
580
            if (!selection.isFromStore(this)) {
581
                throw new SelectionNotAllowedException(getName());
582
            }
583
        }
584

    
585
        if (this.selection != null) {
586
            this.selection.deleteObserver(this);
587
        }
588
        if (selection == null) {
589
            if (this.selection != null) {
590
                this.selection.dispose();
591
            }
592
            this.selection = null;
593
            return;
594
        }
595
        if (selection instanceof FeatureSelection) {
596
            if (undoable && isEditing()) {
597
                commands.selectionSet(this, this.selection,
598
                    (FeatureSelection) selection);
599
            }
600
            if (this.selection != null) {
601
                this.selection.dispose();
602
            }
603
            this.selection = (FeatureSelection) selection;
604
        } else {
605
            if (undoable && isEditing()) {
606
                commands.startComplex("_selectionSet");
607
            }
608
            if (selection instanceof DefaultFeatureSelection) {
609
                DefaultFeatureSelection defSelection =
610
                    (DefaultFeatureSelection) selection;
611
                defSelection.deselectAll(undoable);
612
                defSelection.select(selection, undoable);
613
            } else {
614
                this.selection.deselectAll();
615
                this.selection.select(selection);
616
            }
617
            if (undoable && isEditing()) {
618
                commands.endComplex();
619
            }
620
        }
621
        this.selection.addObserver(this);
622

    
623
        this.notifyChange(DataStoreNotification.SELECTION_CHANGE);
624
    }
625

    
626
    public FeatureSelection createFeatureSelection() throws DataException {
627
        return this.provider.createFeatureSelection();
628
    }
629

    
630
    public FeatureSelection getFeatureSelection() throws DataException {
631
        if (selection == null) {
632
            this.selection = createFeatureSelection();
633
            this.selection.addObserver(this);
634
        }
635
        return selection;
636
    }
637

    
638
    //
639
    // ====================================================================
640
    // Gestion de notificaciones
641
    //
642

    
643
    public void notifyChange(String notification) {
644
        if (delegateObservable != null) {
645
            notifyChange(new DefaultFeatureStoreNotification(this, notification));
646
        }
647

    
648
    }
649

    
650
    public void notifyChange(String notification, FeatureProvider data) {
651
        try {
652
            notifyChange(notification, createFeature(data));
653
        } catch (DataException ex) {
654
            LOG.error("Error notifying about the notification: " + notification
655
                + ", with the data: " + data, ex);
656
        }
657
    }
658

    
659
    public void notifyChange(String notification, Feature feature) {
660
        notifyChange(new DefaultFeatureStoreNotification(this, notification,
661
            feature));
662
    }
663

    
664
    public void notifyChange(String notification, Command command) {
665
        notifyChange(new DefaultFeatureStoreNotification(this, notification,
666
            command));
667
    }
668

    
669
    public void notifyChange(String notification, EditableFeatureType type) {
670
        notifyChange(new DefaultFeatureStoreNotification(this, notification,
671
            type));
672
    }
673

    
674
    public void notifyChange(FeatureStoreNotification storeNotification) {
675
        delegateObservable.notifyObservers(storeNotification);
676
    }
677

    
678
    public void notifyChange(String notification, Resource resource) {
679
        notifyChange(new DefaultFeatureStoreNotification(this,
680
            DataStoreNotification.RESOURCE_CHANGED));
681
    }
682

    
683
    //
684
    // ====================================================================
685
    // Gestion de bloqueos
686
    //
687

    
688
    public boolean isLocksSupported() {
689
        return this.provider.isLocksSupported();
690
    }
691

    
692
    public FeatureLocks getLocks() throws DataException {
693
        if (!this.provider.isLocksSupported()) {
694
            LOG.warn("Locks not supported");
695
            return null;
696
        }
697
        if (locks == null) {
698
            this.locks = this.provider.createFeatureLocks();
699
        }
700
        return locks;
701
    }
702

    
703
    //
704
    // ====================================================================
705
    // Interface Observable
706
    //
707

    
708
    public void disableNotifications() {
709
        this.delegateObservable.disableNotifications();
710

    
711
    }
712

    
713
    public void enableNotifications() {
714
        this.delegateObservable.enableNotifications();
715
    }
716

    
717
    public void beginComplexNotification() {
718
        this.delegateObservable.beginComplexNotification();
719

    
720
    }
721

    
722
    public void endComplexNotification() {
723
        this.delegateObservable.endComplexNotification();
724

    
725
    }
726

    
727
    public void addObserver(Observer observer) {
728
        if (delegateObservable != null) {
729
            this.delegateObservable.addObserver(observer);
730
        }
731
    }
732

    
733
    public void deleteObserver(Observer observer) {
734
        if (delegateObservable != null) {
735
            this.delegateObservable.deleteObserver(observer);
736
        }
737
    }
738

    
739
    public void deleteObservers() {
740
        this.delegateObservable.deleteObservers();
741

    
742
    }
743

    
744
    //
745
    // ====================================================================
746
    // Interface Observer
747
    //
748
    // Usado para observar:
749
    // - su seleccion
750
    // - sus bloqueos
751
    // - sus recursos
752
    //
753

    
754
    public void update(Observable observable, Object notification) {
755
        if (observable instanceof FeatureSet) {
756
            if (observable == this.selection) {
757
                this.notifyChange(DataStoreNotification.SELECTION_CHANGE);
758
            } else
759
                if (observable == this.locks) {
760
                    this.notifyChange(FeatureStoreNotification.LOCKS_CHANGE);
761
                }
762

    
763
        } else
764
            if (observable instanceof FeatureStoreProvider) {
765
                if (observable == this.provider) {
766

    
767
                }
768

    
769
            }
770
    }
771

    
772
    //
773
    // ====================================================================
774
    // Edicion
775
    //
776

    
777
    private void newVersionOfUpdate() {
778
        this.versionOfUpdate++;
779
    }
780

    
781
    private long currentVersionOfUpdate() {
782
        return this.versionOfUpdate;
783
    }
784

    
785
    private void checkInEditingMode() throws NeedEditingModeException {
786
        if (mode != MODE_FULLEDIT) {
787
            throw new NeedEditingModeException(this.getName());
788
        }
789
    }
790

    
791
    private void checkNotInAppendMode() throws IllegalStateException {
792
        if (mode == MODE_APPEND) {
793
                        throw new IllegalStateException("Error: store "
794
                                        + this.getFullName() + " is in append mode");
795
        }
796
    }
797

    
798
    private void checkIsOwnFeature(Feature feature)
799
        throws IllegalFeatureException {
800
        if (((DefaultFeature) feature).getStore() != this) {
801
            throw new IllegalFeatureException(this.getName());
802
        }
803
        // FIXME: fixFeatureType no vale para el checkIsOwnFeature
804
        // fixFeatureType((DefaultFeatureType) feature.getType());
805
    }
806

    
807
    private void exitEditingMode() {
808
        if (commands != null) {
809
            commands.clear();
810
            commands = null;
811
        }
812

    
813
        if (featureTypeManager != null) {
814
            featureTypeManager.dispose();
815
            featureTypeManager = null;
816

    
817
        }
818

    
819
        // TODO implementar un dispose para estos dos
820
        featureManager = null;
821
        spatialManager = null;
822

    
823
        featureCount = null;
824

    
825
        mode = MODE_QUERY;
826
        hasStrongChanges = true; // Lo deja a true por si las moscas
827
        hasInserts = true;
828
    }
829

    
830
    synchronized public void edit() throws DataException {
831
        edit(MODE_FULLEDIT);
832
    }
833

    
834
    synchronized public void edit(int mode) throws DataException {
835
        LOG.debug("Starting editing in mode: {}", new Integer(mode));
836
        try {
837
            if (this.mode != MODE_QUERY) {
838
                throw new AlreadyEditingException(this.getName());
839
            }
840
            if (!this.provider.supportsAppendMode()) {
841
                mode = MODE_FULLEDIT;
842
            }
843
            switch (mode) {
844
            case MODE_QUERY:
845
                throw new IllegalStateException(this.getName());
846

    
847
            case MODE_FULLEDIT:
848
                if (!this.transforms.isEmpty()) {
849
                    throw new IllegalStateException(this.getName());
850
                }
851
                notifyChange(FeatureStoreNotification.BEFORE_STARTEDITING);
852
                invalidateIndexes();
853
                featureManager =
854
                    new FeatureManager(new MemoryExpansionAdapter());
855
                featureTypeManager =
856
                    new FeatureTypeManager(this, new MemoryExpansionAdapter());
857
                spatialManager =
858
                    new SpatialManager(this, provider.getEnvelope());
859

    
860
                commands =
861
                    new DefaultFeatureCommandsStack(this, featureManager,
862
                        spatialManager, featureTypeManager);
863
                this.mode = MODE_FULLEDIT;
864
                hasStrongChanges = false;
865
                hasInserts = false;
866
                notifyChange(FeatureStoreNotification.AFTER_STARTEDITING);
867
                break;
868
            case MODE_APPEND:
869
                if (!this.transforms.isEmpty()) {
870
                    throw new IllegalStateException(this.getName());
871
                }
872
                notifyChange(FeatureStoreNotification.BEFORE_STARTEDITING);
873
                invalidateIndexes();
874
                this.provider.beginAppend();
875
                this.mode = MODE_APPEND;
876
                hasInserts = false;
877
                notifyChange(FeatureStoreNotification.AFTER_STARTEDITING);
878
                break;
879
            }
880
        } catch (Exception e) {
881
            throw new StoreEditException(e, this.getName());
882
        }
883
    }
884

    
885
    private void invalidateIndexes() {
886
        setIndexesValidStatus(false);
887
    }
888

    
889
    private void setIndexesValidStatus(boolean valid) {
890
        FeatureIndexes indexes = getIndexes();
891
        LOG.debug("Setting the store indexes to valid status {}: {}", (valid
892
            ? Boolean.TRUE : Boolean.FALSE), indexes);
893
        for (Iterator iterator = indexes.iterator(); iterator.hasNext();) {
894
            FeatureIndex index = (FeatureIndex) iterator.next();
895
            if (index instanceof FeatureIndexProviderServices) {
896
                FeatureIndexProviderServices indexServices =
897
                    (FeatureIndexProviderServices) index;
898
                indexServices.setValid(valid);
899
            }
900
        }
901
    }
902

    
903
    private void updateIndexes() throws FeatureIndexException {
904
        FeatureIndexes indexes = getIndexes();
905
        LOG.debug("Refilling indexes: {}", indexes);
906
        for (Iterator iterator = indexes.iterator(); iterator.hasNext();) {
907
            FeatureIndex index = (FeatureIndex) iterator.next();
908
            if (index instanceof FeatureIndexProviderServices) {
909
                FeatureIndexProviderServices indexServices =
910
                    (FeatureIndexProviderServices) index;
911
                indexServices.fill(true, null);
912
            }
913
        }
914
    }
915

    
916
    private void waitForIndexes() {
917
        FeatureIndexes indexes = getIndexes();
918
        LOG.debug("Waiting for indexes to finish filling: {}", indexes);
919
        for (Iterator iterator = indexes.iterator(); iterator.hasNext();) {
920
            FeatureIndex index = (FeatureIndex) iterator.next();
921
            if (index instanceof FeatureIndexProviderServices) {
922
                FeatureIndexProviderServices indexServices =
923
                    (FeatureIndexProviderServices) index;
924
                indexServices.waitForIndex();
925
            }
926
        }
927
    }
928

    
929
    private void disposeIndexes() {
930
        FeatureIndexes indexes = getIndexes();
931
        LOG.debug("Disposing indexes: {}", indexes);
932
        for (Iterator iterator = indexes.iterator(); iterator.hasNext();) {
933
            FeatureIndex index = (FeatureIndex) iterator.next();
934
            if (index instanceof FeatureIndexProviderServices) {
935
                FeatureIndexProviderServices indexServices =
936
                    (FeatureIndexProviderServices) index;
937
                indexServices.dispose();
938
            }
939
        }
940
    }
941

    
942
    public boolean isEditing() {
943
        return mode == MODE_FULLEDIT;
944
    }
945

    
946
    public boolean isAppending() {
947
        return mode == MODE_APPEND;
948
    }
949

    
950
    synchronized public void update(EditableFeatureType type)
951
        throws DataException {
952
        try {
953
            checkInEditingMode();
954
            if (type == null) {
955
                throw new NullFeatureTypeException(getName());
956
            }
957
            // FIXME: Comprobar que es un featureType aceptable.
958
            notifyChange(FeatureStoreNotification.BEFORE_UPDATE_TYPE, type);
959
            newVersionOfUpdate();
960

    
961
            FeatureType oldt = type.getSource().getCopy();
962
            FeatureType newt = type.getCopy();
963
            commands.update(newt, oldt);
964

    
965
            if (((DefaultEditableFeatureType) type).hasStrongChanges()) {
966
                hasStrongChanges = true;
967
            }
968
            notifyChange(FeatureStoreNotification.AFTER_UPDATE_TYPE, type);
969
        } catch (Exception e) {
970
            throw new StoreUpdateFeatureTypeException(e, this.getName());
971
        }
972
    }
973

    
974
    public void delete(Feature feature) throws DataException {       
975
        this.commands.delete(feature);
976
    }
977
    
978
    synchronized public void doDelete(Feature feature) throws DataException {
979
        try {
980
            checkInEditingMode();
981
            checkIsOwnFeature(feature);
982
            if (feature instanceof EditableFeature) {
983
                throw new StoreDeleteEditableFeatureException(getName());
984
            }
985
            notifyChange(FeatureStoreNotification.BEFORE_DELETE, feature);            
986
            
987
            //Update the featureManager and the spatialManager
988
            featureManager.delete(feature.getReference());
989
            spatialManager.deleteFeature(feature);
990
            
991
            newVersionOfUpdate();
992
            hasStrongChanges = true;
993
            notifyChange(FeatureStoreNotification.AFTER_DELETE, feature);
994
        } catch (Exception e) {
995
            throw new StoreDeleteFeatureException(e, this.getName());
996
        }
997
    }
998

    
999
    private static EditableFeature lastChangedFeature = null;
1000

    
1001
    public synchronized void insert(EditableFeature feature) 
1002
        throws DataException {
1003
        LOG.debug("In editing mode {}, insert feature: {}", new Integer(mode),
1004
            feature);
1005
        try {
1006
            switch (mode) {
1007
            case MODE_QUERY:
1008
                throw new NeedEditingModeException(this.getName());
1009

    
1010
            case MODE_APPEND:
1011
                checkIsOwnFeature(feature);
1012
                if (feature.getSource() != null) {
1013
                    throw new NoNewFeatureInsertException(this.getName());
1014
                }
1015
                this.featureCount = null;
1016
                notifyChange(FeatureStoreNotification.BEFORE_INSERT, feature);
1017
                feature.validate(Feature.UPDATE);
1018
                provider.append(((DefaultEditableFeature) feature).getData());
1019
                hasStrongChanges = true;
1020
                hasInserts = true;
1021
                notifyChange(FeatureStoreNotification.AFTER_INSERT, feature);
1022
                break;
1023

    
1024
            case MODE_FULLEDIT: 
1025
                if (feature.getSource() != null) {
1026
                    throw new NoNewFeatureInsertException(this.getName());
1027
                }
1028
                commands.insert(feature);               
1029
            }
1030
        } catch (Exception e) {
1031
            throw new StoreInsertFeatureException(e, this.getName());
1032
        }
1033
    }
1034
    
1035
    synchronized public void doInsert(EditableFeature feature)
1036
        throws DataException {
1037
        checkIsOwnFeature(feature);
1038
   
1039
        waitForIndexes();
1040

    
1041
        notifyChange(FeatureStoreNotification.BEFORE_INSERT, feature);
1042
        newVersionOfUpdate();
1043
        if ((lastChangedFeature == null)
1044
            || (lastChangedFeature.getSource() != feature.getSource())) {
1045
            lastChangedFeature = feature;
1046
            feature.validate(Feature.UPDATE);
1047
            lastChangedFeature = null;
1048
        }
1049
        //Update the featureManager and the spatialManager
1050
        ((DefaultEditableFeature) feature).setInserted(true);
1051
        DefaultFeature newFeature = (DefaultFeature) feature.getNotEditableCopy();
1052
        
1053
        
1054
        featureManager.add(newFeature);
1055
        spatialManager.insertFeature(newFeature);
1056
        
1057
        hasStrongChanges = true;
1058
        hasInserts = true;
1059
        notifyChange(FeatureStoreNotification.AFTER_INSERT, feature);       
1060
    }
1061
    
1062
    public void update(EditableFeature feature)
1063
    throws DataException {        
1064
        if ((feature).getSource() == null) {
1065
            insert(feature);
1066
            return;
1067
        }
1068
        commands.update(feature, feature.getSource());
1069
    }
1070

    
1071
    synchronized public void doUpdate(EditableFeature feature, Feature oldFeature)
1072
        throws DataException {
1073
        try {          
1074
            checkInEditingMode();
1075
            checkIsOwnFeature(feature);
1076
            notifyChange(FeatureStoreNotification.BEFORE_UPDATE, feature);
1077
            newVersionOfUpdate();
1078
            if ((lastChangedFeature == null)
1079
                || (lastChangedFeature.getSource() != feature.getSource())) {
1080
                lastChangedFeature = feature;
1081
                feature.validate(Feature.UPDATE);
1082
                lastChangedFeature = null;
1083
            }
1084
            
1085
            //Update the featureManager and the spatialManager
1086
            Feature newf = feature.getNotEditableCopy();
1087
            featureManager.update(newf, oldFeature);
1088
            spatialManager.updateFeature(newf, oldFeature);
1089
   
1090
            hasStrongChanges = true;
1091
            notifyChange(FeatureStoreNotification.AFTER_UPDATE, feature);
1092
        } catch (Exception e) {
1093
            throw new StoreUpdateFeatureException(e, this.getName());
1094
        }
1095
    }
1096

    
1097
    synchronized public void redo() throws RedoException {
1098
        Command redo = commands.getNextRedoCommand();
1099
        try {
1100
            checkInEditingMode();
1101
        } catch (NeedEditingModeException ex) {
1102
            throw new RedoException(redo, ex);
1103
        }
1104
        notifyChange(FeatureStoreNotification.BEFORE_REDO, redo);
1105
        newVersionOfUpdate();
1106
        commands.redo();
1107
        hasStrongChanges = true;
1108
        notifyChange(FeatureStoreNotification.AFTER_REDO, redo);
1109
    }
1110

    
1111
    synchronized public void undo() throws UndoException {
1112
        Command undo = commands.getNextUndoCommand();
1113
        try {
1114
            checkInEditingMode();
1115
        } catch (NeedEditingModeException ex) {
1116
            throw new UndoException(undo, ex);
1117
        }
1118
        notifyChange(FeatureStoreNotification.BEFORE_UNDO, undo);
1119
        newVersionOfUpdate();
1120
        commands.undo();
1121
        hasStrongChanges = true;
1122
        notifyChange(FeatureStoreNotification.AFTER_UNDO, undo);
1123
    }
1124

    
1125
    public List getRedoInfos() {
1126
        if (isEditing() && (commands != null)) {
1127
            return commands.getRedoInfos();
1128
        } else {
1129
            return null;
1130
        }
1131
    }
1132

    
1133
    public List getUndoInfos() {
1134
        if (isEditing() && (commands != null)) {
1135
            return commands.getUndoInfos();
1136
        } else {
1137
            return null;
1138
        }
1139
    }
1140

    
1141
    public synchronized FeatureCommandsStack getCommandsStack()
1142
        throws DataException {
1143
        checkInEditingMode();
1144
        return commands;
1145
    }
1146

    
1147
    synchronized public void cancelEditing() throws DataException {
1148
        spatialManager.cancelModifies();
1149
        try {
1150
            checkInEditingMode();
1151

    
1152
            boolean clearSelection = this.hasStrongChanges;
1153
            if (this.selection instanceof FeatureReferenceSelection) {
1154
                clearSelection = this.hasInserts;
1155
            }
1156
            notifyChange(FeatureStoreNotification.BEFORE_CANCELEDITING);
1157
            exitEditingMode();
1158
            if (clearSelection) {
1159
                ((FeatureSelection) this.getSelection()).deselectAll();
1160
            }
1161
            updateIndexes();
1162
            notifyChange(FeatureStoreNotification.AFTER_CANCELEDITING);
1163
        } catch (Exception e) {
1164
            throw new StoreCancelEditingException(e, this.getName());
1165
        }
1166
    }
1167

    
1168
    synchronized public void finishEditing() throws DataException {
1169
        LOG.debug("finish editing of mode: {}", new Integer(mode));
1170
        try {
1171
            
1172
            /*
1173
             * Selection needs to be cleared when editing stops
1174
             * to prevent conflicts with selection remaining from
1175
             * editing mode.
1176
             */
1177
            ((FeatureSelection) this.getSelection()).deselectAll();
1178
            
1179
            switch (mode) {
1180
            case MODE_QUERY:
1181
                throw new NeedEditingModeException(this.getName());
1182

    
1183
            case MODE_APPEND:
1184
                notifyChange(FeatureStoreNotification.BEFORE_FINISHEDITING);
1185
                provider.endAppend();
1186
                exitEditingMode();
1187
                updateIndexes();
1188
                notifyChange(FeatureStoreNotification.AFTER_FINISHEDITING);
1189
                break;
1190

    
1191
            case MODE_FULLEDIT:
1192
                if (hasStrongChanges && !this.allowWrite()) {
1193
                    throw new WriteNotAllowedException(getName());
1194
                }
1195
                notifyChange(FeatureStoreNotification.BEFORE_FINISHEDITING);
1196
                if (hasStrongChanges) {
1197
                    validateFeatures(Feature.FINISH_EDITING);
1198
                    provider.performChanges(featureManager.getDeleted(),
1199
                        featureManager.getInserted(),
1200
                        featureManager.getUpdated(),
1201
                        featureTypeManager.getFeatureTypesChanged());
1202
                }
1203
                exitEditingMode();
1204
                updateIndexes();
1205
                notifyChange(FeatureStoreNotification.AFTER_FINISHEDITING);
1206
                break;
1207
            }
1208
        } catch (Exception e) {
1209
            throw new FinishEditingException(e);
1210
        }
1211
    }
1212

    
1213
    public void beginEditingGroup(String description)
1214
        throws NeedEditingModeException {
1215
        checkInEditingMode();
1216
        commands.startComplex(description);
1217
    }
1218

    
1219
    public void endEditingGroup() throws NeedEditingModeException {
1220
        checkInEditingMode();
1221
        commands.endComplex();
1222
    }
1223

    
1224
    public boolean isAppendModeSupported() {
1225
        return this.provider.supportsAppendMode();
1226
    }
1227

    
1228
    public void export(DataServerExplorer explorer, String provider,
1229
        NewFeatureStoreParameters params) throws DataException {
1230

    
1231
        if (this.getFeatureTypes().size() != 1) {
1232
            throw new NotYetImplemented(
1233
                "export whith more than one type not yet implemented");
1234
        }
1235
        FeatureSelection featureSelection = (FeatureSelection) getSelection();
1236
        FeatureStore target = null;
1237
        FeatureSet features = null;
1238
        DisposableIterator iterator = null;
1239
        try {
1240
            FeatureType type = this.getDefaultFeatureType();
1241
            if ((params.getDefaultFeatureType() == null)
1242
                || (params.getDefaultFeatureType().size() == 0)) {
1243
                params.setDefaultFeatureType(type.getEditable());
1244

    
1245
            }
1246
            explorer.add(provider, params, true);
1247

    
1248
            DataManager manager = DALLocator.getDataManager();
1249
            target = (FeatureStore) manager.openStore(provider, params);
1250
            FeatureType targetType = target.getDefaultFeatureType();
1251

    
1252
            target.edit(MODE_APPEND);
1253
            FeatureAttributeDescriptor[] pk = type.getPrimaryKey();
1254
            if (featureSelection.getSize() > 0) {
1255
                features = this.getFeatureSelection();
1256
            } else {
1257
                if ((pk != null) && (pk.length > 0)) {
1258
                    FeatureQuery query = createFeatureQuery();
1259
                    for (int i = 0; i < pk.length; i++) {
1260
                        query.getOrder().add(pk[i].getName(), true);
1261
                    }
1262
                    features = this.getFeatureSet(query);
1263
                } else {
1264
                    features = this.getFeatureSet();
1265
                }
1266
            }
1267
            iterator = features.fastIterator();
1268
            while (iterator.hasNext()) {
1269
                DefaultFeature feature = (DefaultFeature) iterator.next();
1270
                target.insert(target.createNewFeature(targetType, feature));
1271
            }
1272
            target.finishEditing();
1273
            target.dispose();
1274
        } catch (Exception e) {
1275
            throw new DataExportException(e, params.toString());
1276
        } finally {
1277
            dispose(iterator);
1278
            dispose(features);
1279
            dispose(target);
1280
        }
1281
    }
1282

    
1283
    //
1284
    // ====================================================================
1285
    // Obtencion de datos
1286
    // getDataCollection, getFeatureCollection
1287
    //
1288

    
1289
    public DataSet getDataSet() throws DataException {
1290
        checkNotInAppendMode();
1291
        FeatureQuery query =
1292
            new DefaultFeatureQuery(this.getDefaultFeatureType());
1293
        return new DefaultFeatureSet(this, query);
1294
    }
1295

    
1296
    public DataSet getDataSet(DataQuery dataQuery) throws DataException {
1297
        checkNotInAppendMode();
1298
        return new DefaultFeatureSet(this, (FeatureQuery) dataQuery);
1299
    }
1300

    
1301
    public void getDataSet(Observer observer) throws DataException {
1302
        checkNotInAppendMode();
1303
        this.getFeatureSet(null, observer);
1304
    }
1305

    
1306
    public void getDataSet(DataQuery dataQuery, Observer observer)
1307
        throws DataException {
1308
        checkNotInAppendMode();
1309
        this.getFeatureSet((FeatureQuery) dataQuery, observer);
1310
    }
1311

    
1312
    public FeatureSet getFeatureSet() throws DataException {
1313
        checkNotInAppendMode();
1314
        FeatureQuery query =
1315
            new DefaultFeatureQuery(this.getDefaultFeatureType());
1316
        return new DefaultFeatureSet(this, query);
1317
    }
1318

    
1319
    public FeatureSet getFeatureSet(FeatureQuery featureQuery)
1320
        throws DataException {
1321
        checkNotInAppendMode();
1322
        return new DefaultFeatureSet(this, featureQuery);
1323
    }
1324

    
1325
    public void accept(Visitor visitor) throws BaseException {
1326
        FeatureSet set = getFeatureSet();
1327
        try {
1328
            set.accept(visitor);
1329
        } finally {
1330
            set.dispose();
1331
        }
1332
    }
1333

    
1334
    public void accept(Visitor visitor, DataQuery dataQuery)
1335
        throws BaseException {
1336
        FeatureSet set = getFeatureSet((FeatureQuery) dataQuery);
1337
        try {
1338
            set.accept(visitor);
1339
        } finally {
1340
            set.dispose();
1341
        }
1342
    }
1343

    
1344
    public FeatureType getFeatureType(FeatureQuery featureQuery)
1345
        throws DataException {
1346
        DefaultFeatureType fType =
1347
            (DefaultFeatureType) this.getFeatureType(featureQuery
1348
                .getFeatureTypeId());
1349
        if ((featureQuery.getAttributeNames() != null)
1350
            && (featureQuery.getAttributeNames().length > 0)) {
1351
            return fType.getSubtype(featureQuery.getAttributeNames());
1352
        }
1353
        return fType;
1354
    }
1355

    
1356
    public void getFeatureSet(Observer observer) throws DataException {
1357
        checkNotInAppendMode();
1358
        this.getFeatureSet(null, observer);
1359
    }
1360

    
1361
    public void getFeatureSet(FeatureQuery query, Observer observer)
1362
        throws DataException {
1363
        class LoadInBackGround implements Runnable {
1364

    
1365
            private FeatureStore store;
1366
            private FeatureQuery query;
1367
            private Observer observer;
1368

    
1369
            public LoadInBackGround(FeatureStore store, FeatureQuery query,
1370
                Observer observer) {
1371
                this.store = store;
1372
                this.query = query;
1373
                this.observer = observer;
1374
            }
1375

    
1376
            void notify(FeatureStoreNotification theNotification) {
1377
                observer.update(store, theNotification);
1378
                return;
1379
            }
1380

    
1381
            public void run() {
1382
                FeatureSet set = null;
1383
                try {
1384
                    set = store.getFeatureSet(query);
1385
                    notify(new DefaultFeatureStoreNotification(store,
1386
                        FeatureStoreNotification.LOAD_FINISHED, set));
1387
                } catch (Exception e) {
1388
                    notify(new DefaultFeatureStoreNotification(store,
1389
                        FeatureStoreNotification.LOAD_FINISHED, e));
1390
                } finally {
1391
                    dispose(set);
1392
                }
1393
            }
1394
        }
1395

    
1396
        checkNotInAppendMode();
1397
        if (query == null) {
1398
            query = new DefaultFeatureQuery(this.getDefaultFeatureType());
1399
        }
1400
        LoadInBackGround task = new LoadInBackGround(this, query, observer);
1401
        Thread thread = new Thread(task, "Load Feature Set in background");
1402
        thread.start();
1403
    }
1404

    
1405
    public Feature getFeatureByReference(FeatureReference reference)
1406
        throws DataException {
1407
        checkNotInAppendMode();
1408
        DefaultFeatureReference ref = (DefaultFeatureReference) reference;
1409
        FeatureType featureType;
1410
        if (ref.getFeatureTypeId() == null) {
1411
            featureType = this.getDefaultFeatureType();
1412
        } else {
1413
            featureType = this.getFeatureType(ref.getFeatureTypeId());
1414
        }
1415
        return this.getFeatureByReference(reference, featureType);
1416
    }
1417

    
1418
    public Feature getFeatureByReference(FeatureReference reference,
1419
        FeatureType featureType) throws DataException {
1420
        checkNotInAppendMode();
1421
        featureType = fixFeatureType((DefaultFeatureType) featureType);
1422
        if (!this.transforms.isEmpty()) {
1423

    
1424
            featureType = this.transforms.getSourceFeatureTypeFrom(featureType);
1425

    
1426
        }
1427
        // TODO comprobar que el id es de este store
1428

    
1429
        if (this.mode == MODE_FULLEDIT) {
1430
            Feature f = featureManager.get(reference, this, featureType);
1431
            if (f != null) {
1432
                return f;
1433
            }
1434
        }
1435
    
1436
        
1437
        DefaultFeature feature =
1438
            new DefaultFeature(this,
1439
                this.provider.getFeatureProviderByReference(
1440
                    (FeatureReferenceProviderServices) reference, featureType));
1441

    
1442
        if (!this.transforms.isEmpty()) {
1443
            return this.transforms.applyTransform(feature, featureType);
1444
        }
1445
        return feature;
1446
    }
1447

    
1448
    //
1449
    // ====================================================================
1450
    // Gestion de features
1451
    //
1452

    
1453
    private FeatureType fixFeatureType(DefaultFeatureType type)
1454
        throws DataException {
1455
        FeatureType original = this.getDefaultFeatureType();
1456

    
1457
        if ((type == null) || type.equals(original)) {
1458
            return original;
1459
        } else {
1460
            if (!type.isSubtypeOf(original)) {
1461
                Iterator iter = this.getFeatureTypes().iterator();
1462
                FeatureType tmpType;
1463
                boolean found = false;
1464
                while (iter.hasNext()) {
1465
                    tmpType = (FeatureType) iter.next();
1466
                    if (type.equals(tmpType)) {
1467
                        return type;
1468

    
1469
                    } else
1470
                        if (type.isSubtypeOf(tmpType)) {
1471
                            found = true;
1472
                            original = tmpType;
1473
                            break;
1474
                        }
1475

    
1476
                }
1477
                if (!found) {
1478
                    throw new IllegalFeatureTypeException(getName());
1479
                }
1480
            }
1481
        }
1482

    
1483
        // Checks that type has all fields of pk
1484
        // else add the missing attributes at the end.
1485
        if (!original.hasOID()) {
1486
            // Gets original pk attributes
1487
            DefaultEditableFeatureType edOriginal =
1488
                (DefaultEditableFeatureType) original.getEditable();
1489
            FeatureAttributeDescriptor orgAttr;
1490
            Iterator edOriginalIter = edOriginal.iterator();
1491
            while (edOriginalIter.hasNext()) {
1492
                orgAttr = (FeatureAttributeDescriptor) edOriginalIter.next();
1493
                if (!orgAttr.isPrimaryKey()) {
1494
                    edOriginalIter.remove();
1495
                }
1496
            }
1497

    
1498
            // Checks if all pk attributes are in type
1499
            Iterator typeIterator;
1500
            edOriginalIter = edOriginal.iterator();
1501
            FeatureAttributeDescriptor attr;
1502
            while (edOriginalIter.hasNext()) {
1503
                orgAttr = (FeatureAttributeDescriptor) edOriginalIter.next();
1504
                typeIterator = type.iterator();
1505
                while (typeIterator.hasNext()) {
1506
                    attr = (FeatureAttributeDescriptor) typeIterator.next();
1507
                    if (attr.getName().equals(orgAttr.getName())) {
1508
                        edOriginalIter.remove();
1509
                        break;
1510
                    }
1511
                }
1512
            }
1513

    
1514
            // add missing pk attributes if any
1515
            if (edOriginal.size() > 0) {
1516
                boolean isEditable = type instanceof DefaultEditableFeatureType;
1517
                DefaultEditableFeatureType edType =
1518
                    (DefaultEditableFeatureType) original.getEditable();
1519
                edType.clear();
1520
                edType.addAll(type);
1521
                edType.addAll(edOriginal);
1522
                if (!isEditable) {
1523
                    type = (DefaultFeatureType) edType.getNotEditableCopy();
1524
                }
1525
            }
1526

    
1527
        }
1528

    
1529
        return type;
1530
    }
1531

    
1532
    public void validateFeatures(int mode) throws DataException {
1533
        FeatureSet collection = null;
1534
        DisposableIterator iter = null;
1535
        try {
1536
            checkNotInAppendMode();
1537
            collection = this.getFeatureSet();
1538
            iter = collection.fastIterator();
1539
            long previousVersionOfUpdate = currentVersionOfUpdate();
1540
            while (iter.hasNext()) {
1541
                ((DefaultFeature) iter.next()).validate(mode);
1542
                if (previousVersionOfUpdate != currentVersionOfUpdate()) {
1543
                    throw new ConcurrentDataModificationException(getName());
1544
                }
1545
            }
1546
        } catch (Exception e) {
1547
            throw new ValidateFeaturesException(e, getName());
1548
        } finally {
1549
            dispose(iter);
1550
            dispose(collection);
1551
        }
1552
    }
1553

    
1554
    public FeatureType getDefaultFeatureType() throws DataException {
1555
        try {
1556
            
1557
            if (isEditing()) {
1558
                FeatureType auxFeatureType =
1559
                    featureTypeManager.getType(defaultFeatureType.getId());
1560
                if (auxFeatureType != null) {
1561
                    return avoidEditable(auxFeatureType);
1562
                }
1563
            }
1564
            FeatureType type = this.transforms.getDefaultFeatureType();
1565
            if (type != null) {
1566
                return avoidEditable(type);
1567
            }
1568
            
1569
            return avoidEditable(defaultFeatureType);
1570
            
1571
        } catch (Exception e) {
1572
            throw new GetFeatureTypeException(e, getName());
1573
        }
1574
    }
1575
    
1576
    private FeatureType avoidEditable(FeatureType ft) {
1577
        if (ft instanceof EditableFeatureType) {
1578
            return ((EditableFeatureType) ft).getNotEditableCopy();
1579
        } else {
1580
            return ft;
1581
        }
1582
    }
1583

    
1584
    public FeatureType getFeatureType(String featureTypeId)
1585
        throws DataException {
1586
        if (featureTypeId == null) {
1587
            return this.getDefaultFeatureType();
1588
        }
1589
        try {
1590
            if (isEditing()) {
1591
                FeatureType auxFeatureType =
1592
                    featureTypeManager.getType(featureTypeId);
1593
                if (auxFeatureType != null) {
1594
                    return auxFeatureType;
1595
                }
1596
            }
1597
            FeatureType type = this.transforms.getFeatureType(featureTypeId);
1598
            if (type != null) {
1599
                return type;
1600
            }
1601
            Iterator iter = this.featureTypes.iterator();
1602
            while (iter.hasNext()) {
1603
                type = (FeatureType) iter.next();
1604
                if (type.getId().equals(featureTypeId)) {
1605
                    return type;
1606
                }
1607
            }
1608
            return null;
1609
        } catch (Exception e) {
1610
            throw new GetFeatureTypeException(e, getName());
1611
        }
1612
    }
1613

    
1614
    public FeatureType getProviderDefaultFeatureType() {
1615
        return defaultFeatureType;
1616
    }
1617

    
1618
    public List getFeatureTypes() throws DataException {
1619
        try {
1620
            List types;
1621
            if (isEditing()) {
1622
                types = new ArrayList();
1623
                Iterator it = featureTypes.iterator();
1624
                while (it.hasNext()) {
1625
                    FeatureType type = (FeatureType) it.next();
1626
                    FeatureType typeaux =
1627
                        featureTypeManager.getType(type.getId());
1628
                    if (typeaux != null) {
1629
                        types.add(typeaux);
1630
                    } else {
1631
                        types.add(type);
1632
                    }
1633
                }
1634
                it = featureTypeManager.newsIterator();
1635
                while (it.hasNext()) {
1636
                    FeatureType type = (FeatureType) it.next();
1637
                    types.add(type);
1638
                }
1639
            } else {
1640
                types = this.transforms.getFeatureTypes();
1641
                if (types == null) {
1642
                    types = featureTypes;
1643
                }
1644
            }
1645
            return Collections.unmodifiableList(types);
1646
        } catch (Exception e) {
1647
            throw new GetFeatureTypeException(e, getName());
1648
        }
1649
    }
1650

    
1651
    public List getProviderFeatureTypes() throws DataException {
1652
        return Collections.unmodifiableList(this.featureTypes);
1653
    }
1654

    
1655
    public Feature createFeature(FeatureProvider data) throws DataException {
1656
        DefaultFeature feature = new DefaultFeature(this, data);
1657
        return feature;
1658
    }
1659

    
1660
    public Feature createFeature(FeatureProvider data, FeatureType type)
1661
        throws DataException {
1662
        // FIXME: falta por implementar
1663
        // Comprobar si es un subtipo del feature de data
1664
        // y construir un feature usando el subtipo.
1665
        // Probablemente requiera generar una copia del data.
1666
        throw new NotYetImplemented();
1667
    }
1668

    
1669
    public EditableFeature createNewFeature(FeatureType type,
1670
        Feature defaultValues) throws DataException {
1671
        try {
1672
            FeatureProvider data = createNewFeatureProvider(type);
1673
            DefaultEditableFeature feature =
1674
                new DefaultEditableFeature(this, data);
1675
            feature.initializeValues(defaultValues);
1676
            data.setNew(true);
1677
            
1678
            return feature;
1679
        } catch (Exception e) {
1680
            throw new CreateFeatureException(e, getName());
1681
        }
1682
    }
1683

    
1684
    private FeatureProvider createNewFeatureProvider(FeatureType type)
1685
        throws DataException {
1686
        type = this.fixFeatureType((DefaultFeatureType) type);
1687
        FeatureProvider data = this.provider.createFeatureProvider(type);
1688
        data.setNew(true);
1689
        if (type.hasOID() && (data.getOID() == null)) {
1690
            data.setOID(this.provider.createNewOID());
1691
        } else {
1692
            data.setOID(this.getTemporalOID());
1693
        }
1694
        return data;
1695

    
1696
    }
1697

    
1698
    public EditableFeature createNewFeature(FeatureType type,
1699
        boolean defaultValues) throws DataException {
1700
        try {
1701
            FeatureProvider data = createNewFeatureProvider(type);
1702
            DefaultEditableFeature feature =
1703
                new DefaultEditableFeature(this, data);
1704
            if (defaultValues) {
1705
                feature.initializeValues();
1706
            }
1707
            return feature;
1708
        } catch (Exception e) {
1709
            throw new CreateFeatureException(e, getName());
1710
        }
1711
    }
1712

    
1713
    public EditableFeature createNewFeature(boolean defaultValues)
1714
        throws DataException {
1715
        return this.createNewFeature(this.getDefaultFeatureType(),
1716
            defaultValues);
1717
    }
1718

    
1719
    public EditableFeature createNewFeature() throws DataException {
1720
        return this.createNewFeature(this.getDefaultFeatureType(), true);
1721
    }
1722

    
1723
    public EditableFeatureType createFeatureType() {
1724
        DefaultEditableFeatureType ftype = new DefaultEditableFeatureType();
1725
        return ftype;
1726
    }
1727

    
1728
    public EditableFeatureType createFeatureType(String id) {
1729
        DefaultEditableFeatureType ftype = new DefaultEditableFeatureType(id);
1730
        return ftype;
1731
    }
1732

    
1733
    //
1734
    // ====================================================================
1735
    // Index related methods
1736
    //
1737

    
1738
    public FeatureIndexes getIndexes() {
1739
        return this.indexes;
1740
    }
1741

    
1742
    public FeatureIndex createIndex(FeatureType featureType,
1743
        String attributeName, String indexName) throws DataException {
1744
        return createIndex(null, featureType, attributeName, indexName);
1745
    }
1746

    
1747
    public FeatureIndex createIndex(String indexTypeName,
1748
        FeatureType featureType, String attributeName, String indexName)
1749
        throws DataException {
1750

    
1751
        return createIndex(indexTypeName, featureType, attributeName,
1752
            indexName, false, null);
1753
    }
1754

    
1755
    public FeatureIndex createIndex(FeatureType featureType,
1756
        String attributeName, String indexName, Observer observer)
1757
        throws DataException {
1758
        return createIndex(null, featureType, attributeName, indexName,
1759
            observer);
1760
    }
1761

    
1762
    public FeatureIndex createIndex(String indexTypeName,
1763
        FeatureType featureType, String attributeName, String indexName,
1764
        final Observer observer) throws DataException {
1765

    
1766
        return createIndex(indexTypeName, featureType, attributeName,
1767
            indexName, true, observer);
1768
    }
1769

    
1770
    private FeatureIndex createIndex(String indexTypeName,
1771
        FeatureType featureType, String attributeName, String indexName,
1772
        boolean background, final Observer observer) throws DataException {
1773

    
1774
        checkNotInAppendMode();
1775
        FeatureIndexProviderServices index = null;
1776
        index =
1777
            dataManager.createFeatureIndexProvider(indexTypeName, this,
1778
                featureType, indexName,
1779
                featureType.getAttributeDescriptor(attributeName));
1780

    
1781
        try {
1782
            index.fill(background, observer);
1783
        } catch (FeatureIndexException e) {
1784
            throw new InitializeException(index.getName(), e);
1785
        }
1786

    
1787
        ((DefaultFeatureIndexes) getIndexes()).addIndex(index);
1788
        return index;
1789
    }
1790

    
1791
    //
1792
    // ====================================================================
1793
    // Transforms related methods
1794
    //
1795

    
1796
    public FeatureStoreTransforms getTransforms() {
1797
        return this.transforms;
1798
    }
1799

    
1800
    public FeatureQuery createFeatureQuery() {
1801
        return new DefaultFeatureQuery();
1802
    }
1803

    
1804
    public DataQuery createQuery() {
1805
        return createFeatureQuery();
1806
    }
1807

    
1808
    //
1809
    // ====================================================================
1810
    // UndoRedo related methods
1811
    //
1812

    
1813
    public boolean canRedo() {
1814
        return commands.canRedo();
1815
    }
1816

    
1817
    public boolean canUndo() {
1818
        return commands.canUndo();
1819
    }
1820

    
1821
    public void redo(int num) throws RedoException {
1822
        for (int i = 0; i < num; i++) {
1823
            redo();
1824
        }
1825
    }
1826

    
1827
    public void undo(int num) throws UndoException {
1828
        for (int i = 0; i < num; i++) {
1829
            undo();
1830
        }
1831
    }
1832

    
1833
    //
1834
    // ====================================================================
1835
    // Metadata related methods
1836
    //
1837

    
1838
    public Object getMetadataID() {
1839
        return this.provider.getSourceId();
1840
    }
1841

    
1842
    public void delegate(DynObject dynObject) {
1843
        this.metadata.delegate(dynObject);
1844
    }
1845

    
1846
    public DynClass getDynClass() {
1847
        return this.metadata.getDynClass();
1848
    }
1849

    
1850
        public Object getDynValue(String name) throws DynFieldNotFoundException {
1851
                if( this.transforms.hasDynValue(name) ) {
1852
                        return this.transforms.getDynValue(name);
1853
                }
1854
                if (this.metadata.hasDynValue(name)) {
1855
                        return this.metadata.getDynValue(name);
1856
                }
1857
                if (METADATA_PROVIDER.equalsIgnoreCase(name)) {
1858
                        return this.provider.getProviderName();
1859
                } else if (METADATA_CONTAINERNAME.equalsIgnoreCase(name)) {
1860
                        return this.provider.getSourceId();
1861
                } else if (METADATA_FEATURETYPE.equalsIgnoreCase(name)) {
1862
                        try {
1863
                                return this.getDefaultFeatureType();
1864
                        } catch (DataException e) {
1865
                                return null;
1866
                        }
1867
                }
1868
                return this.metadata.getDynValue(name);
1869
        }
1870

    
1871
    public boolean hasDynValue(String name) {
1872
                if( this.transforms.hasDynValue(name) ) {
1873
                        return true;
1874
                }
1875
        return this.metadata.hasDynValue(name);
1876
    }
1877

    
1878
    public void implement(DynClass dynClass) {
1879
        this.metadata.implement(dynClass);
1880
    }
1881

    
1882
    public Object invokeDynMethod(String name, DynObject context)
1883
        throws DynMethodException {
1884
        return this.metadata.invokeDynMethod(this, name, context);
1885
    }
1886

    
1887
    public Object invokeDynMethod(int code, DynObject context)
1888
        throws DynMethodException {
1889
        return this.metadata.invokeDynMethod(this, code, context);
1890
    }
1891

    
1892
    public void setDynValue(String name, Object value)
1893
        throws DynFieldNotFoundException {
1894
                if( this.transforms.hasDynValue(name) ) {
1895
                        this.transforms.setDynValue(name, value);
1896
                        return;
1897
                }
1898
        this.metadata.setDynValue(name, value);
1899

    
1900
    }
1901

    
1902
    /*
1903
     * (non-Javadoc)
1904
     * 
1905
     * @see org.gvsig.metadata.Metadata#getMetadataChildren()
1906
     */
1907
    public Set getMetadataChildren() {
1908
        return this.metadataChildren;
1909
    }
1910

    
1911
    /*
1912
     * (non-Javadoc)
1913
     * 
1914
     * @see org.gvsig.metadata.Metadata#getMetadataName()
1915
     */
1916
    public String getMetadataName() {
1917
        return this.provider.getProviderName();
1918
    }
1919

    
1920
    public FeatureTypeManager getFeatureTypeManager() {
1921
        return this.featureTypeManager;
1922
    }
1923

    
1924
    public long getFeatureCount() throws DataException {
1925
        if (featureCount == null) {
1926
            featureCount = new Long(this.provider.getFeatureCount());
1927
        }
1928
        if (this.isEditing() && !this.isAppending()) {
1929
            return featureCount.longValue()
1930
                - this.featureManager.getDeltaSize();
1931
        }
1932
        return featureCount.longValue();
1933
    }
1934

    
1935
    private Long getTemporalOID() {
1936
        return new Long(this.temporalOid++);
1937
    }
1938

    
1939
    public FeatureType getProviderFeatureType(String featureTypeId) {
1940
        if (featureTypeId == null) {
1941
            return this.defaultFeatureType;
1942
        }
1943
        FeatureType type;
1944
        Iterator iter = this.featureTypes.iterator();
1945
        while (iter.hasNext()) {
1946
            type = (FeatureType) iter.next();
1947
            if (type.getId().equals(featureTypeId)) {
1948
                return type;
1949
            }
1950
        }
1951
        return null;
1952
    }
1953

    
1954
    public FeatureProvider getFeatureProviderFromFeature(Feature feature) {
1955
        return ((DefaultFeature) feature).getData();
1956
    }
1957

    
1958
    public DataStore getStore() {
1959
        return this;
1960
    }
1961

    
1962
    public FeatureStore getFeatureStore() {
1963
        return this;
1964
    }
1965

    
1966
    public void createCache(String name, DynObject parameters)
1967
        throws DataException {
1968
        cache = dataManager.createFeatureCacheProvider(name, parameters);
1969
        if (cache == null) {
1970
            throw new CreateException("FeaureCacheProvider", null);
1971
        }
1972
        cache.apply(this, provider);
1973
        provider = cache;
1974

    
1975
        featureCount = null;
1976
    }
1977

    
1978
    public FeatureCache getCache() {
1979
        return cache;
1980
    }
1981

    
1982
    public void clear() {
1983
        if (metadata != null) {
1984
            metadata.clear();
1985
        }
1986
    }
1987

    
1988
    public String getName() {
1989
        return this.provider.getName();
1990
    }
1991

    
1992
    public String getFullName() {
1993
        return this.provider.getFullName();
1994
    }
1995

    
1996
    public String getProviderName() {
1997
        return this.provider.getProviderName();
1998
    }
1999

    
2000
    public boolean isKnownEnvelope() {
2001
        return this.provider.isKnownEnvelope();
2002
    }
2003

    
2004
    public boolean hasRetrievedFeaturesLimit() {
2005
        return this.provider.hasRetrievedFeaturesLimit();
2006
    }
2007

    
2008
    public int getRetrievedFeaturesLimit() {
2009
        return this.provider.getRetrievedFeaturesLimit();
2010
    }
2011

    
2012
    public Interval getInterval() {  
2013
        return this.provider.getInterval();
2014
    }
2015

    
2016
    public Collection getTimes() {      
2017
        return this.provider.getTimes();
2018
    }
2019

    
2020
    public Collection getTimes(Interval interval) {       
2021
        return this.provider.getTimes(interval);
2022
    }
2023
    
2024
    /* (non-Javadoc)
2025
     * @see java.lang.Object#clone()
2026
     */
2027
    public Object clone() throws CloneNotSupportedException {
2028
        
2029
        DataStoreParameters dsp = getParameters();
2030
        
2031
        DefaultFeatureStore cloned_store = null;
2032
        
2033
        try {
2034
            cloned_store = (DefaultFeatureStore) DALLocator.getDataManager().
2035
                openStore(this.getProviderName(), dsp);
2036
            if (transforms != null) {
2037
                cloned_store.transforms = (DefaultFeatureStoreTransforms) transforms.clone();
2038
                cloned_store.transforms.setStoreForClone(this);
2039
            }
2040
        } catch (Exception e) {
2041
            throw new CloneException(e);
2042
        }   
2043
        return cloned_store;
2044
        
2045
    }
2046
}