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 / DefaultFeatureStore.java @ 43981

History | View | Annotate | Download (94.6 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

    
25
package org.gvsig.fmap.dal.feature.impl;
26

    
27
import java.io.File;
28
import org.gvsig.fmap.dal.feature.impl.editing.memory.SpatialManager;
29
import org.gvsig.fmap.dal.feature.impl.editing.memory.FeatureTypeManager;
30
import org.gvsig.fmap.dal.feature.impl.editing.memory.FeatureManager;
31
import org.gvsig.fmap.dal.feature.spi.SQLBuilderBase;
32

    
33
import java.util.ArrayList;
34
import java.util.Collection;
35
import java.util.Collections;
36
import java.util.HashMap;
37
import java.util.HashSet;
38
import java.util.Iterator;
39
import java.util.List;
40
import java.util.Map;
41
import java.util.Map.Entry;
42
import java.util.Set;
43
import java.util.logging.Level;
44
import org.apache.commons.collections4.ListUtils;
45

    
46
import org.apache.commons.io.FilenameUtils;
47
import org.apache.commons.lang3.StringUtils;
48
import org.cresques.cts.IProjection;
49

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

    
170
import org.slf4j.Logger;
171
import org.slf4j.LoggerFactory;
172

    
173
public class DefaultFeatureStore extends AbstractDisposable implements
174
    DataStoreInitializer2, FeatureStoreProviderServices, FeatureStore, Observer {
175

    
176
    private static final Logger LOG = LoggerFactory
177
        .getLogger(DefaultFeatureStore.class);
178

    
179
    private static final String PERSISTENCE_DEFINITION_NAME = "FeatureStore";
180

    
181
    private DataStoreParameters parameters = null;
182
    private FeatureSelection selection;
183
    private FeatureLocks locks;
184

    
185
    private DelegateWeakReferencingObservable delegateObservable =
186
        new DelegateWeakReferencingObservable(this);
187

    
188
    private FeatureCommandsStack commands;
189
    
190
    /*
191
    TODO: Sustituir estos tres manager por un EditingManager
192
    */
193
    private FeatureTypeManager featureTypeManager;
194
    private FeatureManager featureManager;
195
    private SpatialManager spatialManager;
196

    
197
    private FeatureType defaultFeatureType = null;
198
    private List featureTypes = new ArrayList();
199

    
200
    private int mode = MODE_QUERY;
201
    private long versionOfUpdate = 0;
202
    private boolean hasStrongChanges = true;
203
    private boolean hasInserts = true;
204

    
205
    private DefaultDataManager dataManager = null;
206

    
207
    private FeatureStoreProvider provider = null;
208

    
209
    private DefaultFeatureIndexes indexes;
210

    
211
    private DefaultFeatureStoreTransforms transforms;
212

    
213
    DelegatedDynObject metadata;
214

    
215
    private Set metadataChildren;
216

    
217
    private Long featureCount = null;
218

    
219
    private long temporalOid = 0;
220

    
221
    private FeatureCacheProvider cache;
222

    
223
    StateInformation state;
224

    
225
    FeatureStoreTimeSupport timeSupport;
226

    
227

    
228
    private class StateInformation extends HashMap<Object, Object> {
229

    
230
        private static final long serialVersionUID = 4109026189635185666L;
231

    
232
        private boolean broken;
233
        private Throwable breakingsCause;
234

    
235
        @SuppressWarnings("OverridableMethodCallInConstructor")
236
        public StateInformation() {
237
            this.clear();
238
        }
239

    
240
        @Override
241
        public void clear() {
242
            this.broken = false;
243
            this.breakingsCause = null;
244
            super.clear();
245
        }
246

    
247
        public boolean isBroken() {
248
            return this.broken;
249
        }
250

    
251
        public void broken() {
252
            this.broken = true;
253
        }
254

    
255
        public Throwable getBreakingsCause() {
256
            return this.breakingsCause;
257
        }
258

    
259
        public void setBreakingsCause(Throwable cause) {
260
            if( this.breakingsCause==null ) {
261
                this.breakingsCause = cause;
262
            }
263
            this.broken = true;
264
        }
265
    }
266

    
267

    
268

    
269
    /*
270
     * TODO:
271
     *
272
     * - Comprobar que solo se pueden a?adir reglas de validacion sobre un
273
     * EditableFeatureType. - Comprobar que solo se puede hacer un update con un
274
     * featureType al que se le han cambiado las reglas de validacion cuando
275
     * hasStrongChanges=false.
276
     */
277

    
278
    public DefaultFeatureStore() {
279
        this.state = new StateInformation();
280
    }
281

    
282
    @Override
283
    public void intialize(DataManager dataManager,
284
        DataStoreParameters parameters) throws InitializeException {
285

    
286
        DynObjectManager dynManager = ToolsLocator.getDynObjectManager();
287

    
288
        this.metadata = (DelegatedDynObject) dynManager.createDynObject(
289
            FeatureStore.METADATA_DEFINITION_NAME,
290
            MetadataManager.METADATA_NAMESPACE
291
        );
292

    
293
        this.dataManager = (DefaultDataManager) dataManager;
294

    
295
        this.parameters = parameters;
296
        this.transforms = new DefaultFeatureStoreTransforms(this);
297
        try {
298
            indexes = new DefaultFeatureIndexes(this);
299
        } catch (DataException e) {
300
            throw new InitializeException(e);
301
        }
302

    
303
    }
304

    
305
    @Override
306
    public void setProvider(org.gvsig.fmap.dal.DataStoreProvider provider) {
307
        this.provider = (FeatureStoreProvider) provider;
308
        this.delegate((DynObject) provider);
309
        this.metadataChildren = new HashSet();
310
        this.metadataChildren.add(provider);
311
        loadDALFile();
312
    }
313

    
314
    @Override
315
    public DataStoreParameters getParameters() {
316
        return parameters;
317
    }
318

    
319
    public int getMode() {
320
        return this.mode;
321
    }
322

    
323
    @Override
324
    public DataManager getManager() {
325
        return this.dataManager;
326
    }
327

    
328
    @Override
329
    public Iterator getChildren() {
330
        return this.provider.getChilds();
331
    }
332

    
333
    @Override
334
    public FeatureStoreProvider getProvider() {
335
        return this.provider;
336
    }
337

    
338
    public FeatureManager getFeatureManager() {
339
        return this.featureManager;
340
    }
341

    
342
    @Override
343
    public void setFeatureTypes(List types, FeatureType defaultType) {
344
        this.featureTypes = types;
345
        this.defaultFeatureType = defaultType;
346
    }
347

    
348
    public void open() throws OpenException {
349
        if (this.mode != MODE_QUERY) {
350
            // TODO: Se puede hacer un open estando en edicion ?
351
            try {
352
                throw new IllegalStateException();
353
            } catch(Exception ex) {
354
                LOG.warn("Opening a store in editing/append mode ("+this.getFullName()+").",ex);
355
            }
356
        }
357
        this.notifyChange(DataStoreNotification.BEFORE_OPEN);
358
        this.provider.open();
359
        this.notifyChange(DataStoreNotification.AFTER_OPEN);
360
    }
361

    
362
    @Override
363
    public void refresh() throws OpenException, InitializeException {
364
        if (this.mode != MODE_QUERY) {
365
            throw new IllegalStateException();
366
        }
367
        this.notifyChange(FeatureStoreNotification.BEFORE_REFRESH);
368
        if( state.isBroken() ) {
369
            this.load(state);
370
        } else {
371
            this.featureCount = null;
372
            this.provider.refresh();
373
        }
374
        this.notifyChange(FeatureStoreNotification.AFTER_REFRESH);
375
    }
376

    
377
    public void close() throws CloseException {
378
        if (this.mode != MODE_QUERY) {
379
            // TODO: Se puede hacer un close estando en edicion ?
380
            try {
381
                throw new IllegalStateException();
382
            } catch(Exception ex) {
383
                LOG.warn("Clossing a store in editing/append mode ("+this.getFullName()+").",ex);
384
            }
385
        }
386
        this.notifyChange(DataStoreNotification.BEFORE_CLOSE);
387
        this.featureCount = null;
388
        this.provider.close();
389
        this.notifyChange(DataStoreNotification.AFTER_CLOSE);
390
    }
391

    
392
    @Override
393
    protected void doDispose() throws BaseException {
394
        if (this.mode != MODE_QUERY) {
395
            // TODO: Se puede hacer un dispose estando en edicion ?
396
            try {
397
                throw new IllegalStateException();
398
            } catch(Exception ex) {
399
                LOG.warn("Dispossing a store in editing/append mode ("+this.getFullName()+").",ex);
400
            }
401
        }
402
        this.notifyChange(DataStoreNotification.BEFORE_DISPOSE);
403
        this.disposeIndexes();
404
        if( this.provider!=null ) {
405
            this.provider.dispose();
406
        }
407
        if (this.selection != null) {
408
            this.selection.dispose();
409
            this.selection = null;
410
        }
411
        this.commands = null;
412
        this.featureCount = null;
413
        if (this.locks != null) {
414
            // this.locks.dispose();
415
            this.locks = null;
416
        }
417

    
418
        if (this.featureTypeManager != null) {
419
            this.featureTypeManager.dispose();
420
            this.featureTypeManager = null;
421
        }
422

    
423
        this.featureManager = null;
424
        this.spatialManager = null;
425

    
426
        this.parameters = null;
427
        this.notifyChange(DataStoreNotification.AFTER_DISPOSE);
428
        if (delegateObservable != null) {
429
            this.delegateObservable.deleteObservers();
430
            this.delegateObservable = null;
431
        }
432
    }
433

    
434
    @Override
435
    public boolean allowWrite() {
436
        SimpleIdentityManager identityManager = ToolsLocator.getIdentityManager();
437
        if( ! identityManager.getCurrentIdentity().isAuthorized(DataManager.WRITE_STORE_AUTHORIZATION,this.getParameters(), this.getName()) ) {
438
            return false;
439
        }
440
        return this.provider.allowWrite();
441
    }
442

    
443
    @Override
444
    public boolean canWriteGeometry(int geometryType) throws DataException {
445
        return this.provider.canWriteGeometry(geometryType, 0);
446
    }
447

    
448
    @Override
449
    public DataServerExplorer getExplorer() throws ReadException,
450
        ValidateDataParametersException {
451
        if( this.state.isBroken() ) {
452
            try {
453
                return this.provider.getExplorer();
454
            } catch(Throwable th) {
455
                return null;
456
            }
457
        } else {
458
            return this.provider.getExplorer();
459
        }
460
    }
461

    
462
    /*
463
     * public Metadata getMetadata() throws MetadataNotFoundException {
464
     * // TODO:
465
     * // Si el provider devuelbe null habria que ver de construir aqui
466
     * // los metadatos basicos, como el Envelope y el SRS.
467
     *
468
     * // TODO: Estando en edicion el Envelope deberia de
469
     * // actualizarse usando el spatialManager
470
     * return this.provider.getMetadata();
471
     * }
472
     */
473

    
474
    @Override
475
    public Envelope getEnvelope() throws DataException {
476
        if (this.mode == MODE_FULLEDIT) {
477
                // Just in case another thread tries to write in the store
478
                synchronized (this) {
479
                        return this.spatialManager.getEnvelope();
480
                        }
481
        }
482
        if (hasDynValue(DataStore.METADATA_ENVELOPE)){
483
            return (Envelope)getDynValue(DataStore.METADATA_ENVELOPE);
484
        }
485
        return this.provider.getEnvelope();
486
    }
487

    
488
    /**
489
     * @throws org.gvsig.fmap.dal.exception.DataException
490
     * @deprecated use getDefaultFeatureType().getDefaultSRS()
491
     */
492
    @Override
493
    public IProjection getSRSDefaultGeometry() throws DataException {
494
        return this.getDefaultFeatureType().getDefaultSRS();
495
    }
496

    
497
    @Override
498
    public FeatureSelection createDefaultFeatureSelection()
499
        throws DataException {
500
        return new DefaultFeatureSelection(this);
501
    }
502

    
503
    @Override
504
    public FeatureProvider createDefaultFeatureProvider(FeatureType type)
505
        throws DataException {
506
        if (type.hasOID()) {
507
            return new DefaultFeatureProvider(type,
508
                this.provider.createNewOID());
509
        }
510
        return new DefaultFeatureProvider(type);
511
    }
512

    
513
    @Override
514
    public void saveToState(PersistentState state) throws PersistenceException {
515
        /*if (this.mode != FeatureStore.MODE_QUERY) {
516
            throw new PersistenceException(new IllegalStateException(
517
                this.getName()));
518
        }*/
519
        state.set("dataStoreName", this.getName());
520
        state.set("parameters", this.parameters);
521
        state.set("selection", this.selection);
522
        state.set("transforms", this.transforms);
523
        // TODO locks persistence
524
        // state.set("locks", this.locks);
525
        // TODO indexes persistence
526
        // state.set("indexes", this.indexes);
527
        Map evaluatedAttr = new HashMap(1);
528
        Iterator iterType = featureTypes.iterator();
529
        Iterator iterAttr;
530
        FeatureType type;
531
        DefaultFeatureAttributeDescriptor attr;
532
        List attrs;
533
        while (iterType.hasNext()) {
534
            type = (FeatureType) iterType.next();
535
            attrs = new ArrayList();
536
            iterAttr = type.iterator();
537
            while (iterAttr.hasNext()) {
538
                attr = (DefaultFeatureAttributeDescriptor) iterAttr.next();
539
                if ((attr.getEvaluator() != null)
540
                    && (attr.getEvaluator() instanceof Persistent)) {
541
                    attrs.add(attr);
542
                }
543
            }
544
            if (!attrs.isEmpty()) {
545
                evaluatedAttr.put(type.getId(), attrs);
546
            }
547

    
548
        }
549

    
550
        if (evaluatedAttr.isEmpty()) {
551
            evaluatedAttr = null;
552
        }
553

    
554
        state.set("evaluatedAttributes", evaluatedAttr);
555
        state.set("defaultFeatureTypeId", defaultFeatureType.getId());
556

    
557
    }
558

    
559
    @Override
560
    public void loadFromState(final PersistentState persistentState)
561
        throws PersistenceException {
562
        if (this.provider != null) {
563
            throw new PersistenceStoreAlreadyLoadedException(this.getName());
564
        }
565
        if (this.getManager() == null) {
566
            this.dataManager = (DefaultDataManager) DALLocator.getDataManager();
567
        }
568
        state.clear();
569
        try {
570
            state.put("parameters", persistentState.get("parameters"));
571
        } catch(Throwable th) {
572
            state.setBreakingsCause(th);
573
        }
574
        try {
575
            state.put("selection", persistentState.get("selection"));
576
        } catch(Throwable th) {
577
            state.setBreakingsCause(th);
578
        }
579
        try {
580
            state.put("transforms",  persistentState.get("transforms"));
581
        } catch(Throwable th) {
582
            state.setBreakingsCause(th);
583
        }
584
        try {
585
            state.put("evaluatedAttributes",  persistentState.get("evaluatedAttributes"));
586
        } catch(Throwable th) {
587
            state.setBreakingsCause(th);
588
        }
589
        try {
590
            state.put("defaultFeatureTypeId", persistentState.getString("defaultFeatureTypeId"));
591
        } catch(Throwable th) {
592
            state.setBreakingsCause(th);
593
        }
594
        load(state);
595
    }
596

    
597
    private void load(StateInformation state) {
598
        this.featureTypes = new ArrayList();
599
        this.defaultFeatureType = null;
600
        this.featureCount = null;
601

    
602
        DataStoreParameters params = (DataStoreParameters) state.get("parameters");
603
        try {
604
            intialize(dataManager, params);
605
        } catch(Throwable th) {
606
            state.setBreakingsCause(th);
607
        }
608

    
609
        try {
610
            DataStoreProvider prov = dataManager.createProvider(
611
                getStoreProviderServices(),
612
                params
613
            );
614
            setProvider(prov);
615
        } catch(Throwable th) {
616
            state.setBreakingsCause(th);
617
        }
618

    
619
        try {
620
            selection = (FeatureSelection) state.get("selection");
621
        } catch(Throwable th) {
622
            state.setBreakingsCause(th);
623
        }
624

    
625
        try {
626
            this.transforms = (DefaultFeatureStoreTransforms) state.get("transforms");
627
            this.transforms.setFeatureStore(this);
628
            for( FeatureStoreTransform transform : this.transforms ) {
629
                try {
630
                    transform.setUp();
631
                } catch(Throwable th) {
632
                    state.setBreakingsCause(th);
633
                }
634
            }
635
        } catch(Throwable th) {
636
            state.setBreakingsCause(th);
637
        }
638

    
639
        try {
640
            Map evaluatedAttributes = (Map) state.get("evaluatedAttributes");
641
            if ((evaluatedAttributes != null) && !evaluatedAttributes.isEmpty()) {
642
                    Iterator iterEntries = evaluatedAttributes.entrySet().iterator();
643
                    while (iterEntries.hasNext()) {
644
                            Entry entry = (Entry) iterEntries.next();
645
                            List attrs = (List) entry.getValue();
646
                            if (attrs.isEmpty()) {
647
                                    continue;
648
                            }
649
                            int fTypePos = -1;
650
                            DefaultFeatureType type = null;
651
                            for (int i = 0; i < featureTypes.size(); i++) {
652
                                    type = (DefaultFeatureType) featureTypes.get(i);
653
                                    if (type.getId().equals(entry.getKey())) {
654
                                            fTypePos = i;
655
                                            break;
656
                                    }
657
                            }
658
                            if (type == null) {
659
                                    throw new PersistenceCantFindFeatureTypeException(
660
                                            getName(), (String) entry.getKey());
661
                            }
662
                            DefaultEditableFeatureType eType = (DefaultEditableFeatureType) type.getEditable();
663
                            Iterator<FeatureAttributeDescriptor> iterAttr = attrs.iterator();
664
                            while (iterAttr.hasNext()) {
665
                                    FeatureAttributeDescriptor attr = iterAttr.next();
666
                                    eType.addLike(attr);
667
                            }
668
                            featureTypes.set(fTypePos, eType.getNotEditableCopy());
669

    
670
                    }
671

    
672
            }
673
        } catch(Throwable th) {
674
            state.setBreakingsCause(th);
675
        }
676

    
677

    
678
        try {
679
            String defaultFeatureTypeId = (String) state.get("defaultFeatureTypeId");
680
            FeatureType ftype;
681

    
682
            if (defaultFeatureType == null ||
683
                    defaultFeatureType.getId() == null ||
684
                    !defaultFeatureType.getId().equals(defaultFeatureTypeId)) {
685

    
686
                    ftype = getFeatureType(defaultFeatureTypeId);
687
                    if (ftype == null) {
688
                            /*
689
                             * Un error en el m?todo de PostgreSQL getName(), hace que
690
                             * el nombre del featureType sea valor retornado por el getProviderName()
691
                             * De momento se pone este parche para apa?arlo y poder mantener compatibilidad
692
                             * con proyectos antiguos (2.1 y 2.2)
693
                             */
694
                            ftype = getFeatureType(getName());
695
                            if(ftype == null ) {
696
                                    throw new RuntimeException("Can't locate feature type");
697
                            }
698
                    }
699
                    defaultFeatureType = ftype;
700
            }
701
        } catch(Throwable th) {
702
            state.setBreakingsCause(th);
703
        }
704

    
705
        LOG.info("load() broken:{}, {}, {}.",
706
                new Object[] { state.isBroken(), this.getProviderName(), params }
707
        );
708
    }
709

    
710
        public DataStoreProviderServices getStoreProviderServices() {
711
                return this;
712
        }
713

    
714
    public static void registerPersistenceDefinition() {
715
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
716
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
717
            DynStruct definition =
718
                manager.addDefinition(DefaultFeatureStore.class,
719
                    PERSISTENCE_DEFINITION_NAME, PERSISTENCE_DEFINITION_NAME
720
                        + " Persistent definition", null, null);
721
            definition.addDynFieldString("dataStoreName").setMandatory(true)
722
                .setPersistent(true);
723

    
724
            definition.addDynFieldObject("parameters")
725
                .setClassOfValue(DynObject.class).setMandatory(true)
726
                .setPersistent(true);
727

    
728
            definition.addDynFieldObject("selection")
729
                .setClassOfValue(FeatureSelection.class).setMandatory(false)
730
                .setPersistent(true);
731

    
732
            definition.addDynFieldObject("transforms")
733
                .setClassOfValue(DefaultFeatureStoreTransforms.class)
734
                .setMandatory(true).setPersistent(true);
735

    
736
            definition.addDynFieldMap("evaluatedAttributes")
737
                .setClassOfItems(List.class) // List<DefaultFeatureAttributeDescriptor>
738
                .setMandatory(false).setPersistent(true);
739

    
740
            definition.addDynFieldString("defaultFeatureTypeId")
741
                .setMandatory(true).setPersistent(true);
742
        }
743
    }
744

    
745
    public static void registerMetadataDefinition() throws MetadataException {
746
        MetadataManager manager = MetadataLocator.getMetadataManager();
747
        if (manager.getDefinition(FeatureStore.METADATA_DEFINITION_NAME) == null) {
748
            DynStruct metadataDefinition =
749
                manager.addDefinition(FeatureStore.METADATA_DEFINITION_NAME, null);
750
            metadataDefinition.extend(manager
751
                .getDefinition(DataStore.METADATA_DEFINITION_NAME));
752
        }
753
    }
754

    
755
    //
756
    // ====================================================================
757
    // Gestion de la seleccion
758
    //
759

    
760
    @Override
761
    public void setSelection(DataSet selection) throws DataException {
762
        this.setSelection((FeatureSet) selection);
763
    }
764

    
765
    @Override
766
    public DataSet createSelection() throws DataException {
767
        return createFeatureSelection();
768
    }
769

    
770
    @Override
771
    public DataSet getSelection() throws DataException {
772
        return this.getFeatureSelection();
773
    }
774

    
775
    @Override
776
    public void setSelection(FeatureSet selection) throws DataException {
777
        setSelection(selection, true);
778
    }
779

    
780
    public void setSelection(FeatureSet selection, boolean undoable)
781
        throws DataException {
782
        if (selection == null) {
783
            if (undoable) {
784
                throw new SelectionNotAllowedException(getName());
785
            }
786

    
787
        } else {
788
            if (selection.equals(this.selection)) {
789
                return;
790
            }
791
            if (!selection.isFromStore(this)) {
792
                throw new SelectionNotAllowedException(getName());
793
            }
794
        }
795

    
796
        if (this.selection != null) {
797
            this.selection.deleteObserver(this);
798
        }
799
        if (selection == null) {
800
            if (this.selection != null) {
801
                this.selection.dispose();
802
            }
803
            this.selection = null;
804
            return;
805
        }
806
        if (selection instanceof FeatureSelection) {
807
            if (undoable && isEditing()) {
808
                commands.selectionSet(this, this.selection,
809
                    (FeatureSelection) selection);
810
            }
811
            if (this.selection != null) {
812
                this.selection.dispose();
813
            }
814
            this.selection = (FeatureSelection) selection;
815
        } else {
816
            if (undoable && isEditing()) {
817
                commands.startComplex("_selectionSet");
818
            }
819
            if (selection instanceof DefaultFeatureSelection) {
820
                DefaultFeatureSelection defSelection =
821
                    (DefaultFeatureSelection) selection;
822
                defSelection.deselectAll(undoable);
823
                defSelection.select(selection, undoable);
824
            } else {
825
                this.selection.deselectAll();
826
                this.selection.select(selection);
827
            }
828
            if (undoable && isEditing()) {
829
                commands.endComplex();
830
            }
831
        }
832
        this.selection.addObserver(this);
833

    
834
        this.notifyChange(DataStoreNotification.SELECTION_CHANGE);
835
    }
836

    
837
    @Override
838
    public FeatureSelection createFeatureSelection() throws DataException {
839
        return this.provider.createFeatureSelection();
840
    }
841

    
842
    @Override
843
    public FeatureSelection getFeatureSelection() throws DataException {
844
        if (selection == null) {
845
            this.selection = createFeatureSelection();
846
            this.selection.addObserver(this);
847
        }
848
        return selection;
849
    }
850

    
851
    //
852
    // ====================================================================
853
    // Gestion de notificaciones
854
    //
855

    
856
    @Override
857
    public void notifyChange(FeatureStoreNotification storeNotification) {
858
        try {
859
            delegateObservable.notifyObservers(storeNotification);
860
        } catch (Throwable ex) {
861
            LOG.warn("Problems notifying changes in the store '"+this.getName()+" ("+storeNotification.getType()+").",ex);
862
        }
863
    }
864

    
865
    @Override
866
    public void notifyChange(String notification) {
867
        if (delegateObservable != null) {
868
            notifyChange(new DefaultFeatureStoreNotification(this, notification));
869
        }
870

    
871
    }
872

    
873
    @Override
874
    public void notifyChange(String notification, FeatureProvider data) {
875
        Feature f = null;
876
        try {
877
            f = createFeature(data);
878
        } catch (Throwable ex) {
879
            LOG.warn("Problems creating a feature to notifying changes in the store '"+this.getName()+" ("+notification+").",ex);
880
        }
881
        notifyChange(notification, f);
882
    }
883

    
884
    public void notifyChange(String notification, Feature feature) {
885
        notifyChange(new DefaultFeatureStoreNotification(this, notification,
886
            feature));
887
    }
888

    
889
    public void notifyChange(String notification, Command command) {
890
        notifyChange(new DefaultFeatureStoreNotification(this, notification,
891
            command));
892
    }
893

    
894
    public void notifyChange(String notification, EditableFeatureType type) {
895
        notifyChange(new DefaultFeatureStoreNotification(this, notification,
896
            type));
897
    }
898

    
899
    @Override
900
    public void notifyChange(String notification, Resource resource) {
901
        notifyChange(new DefaultFeatureStoreNotification(this,
902
            DataStoreNotification.RESOURCE_CHANGED));
903
    }
904

    
905
    //
906
    // ====================================================================
907
    // Gestion de bloqueos
908
    //
909

    
910
    @Override
911
    public boolean isLocksSupported() {
912
        return this.provider.isLocksSupported();
913
    }
914

    
915
    @Override
916
    public FeatureLocks getLocks() throws DataException {
917
        if (!this.provider.isLocksSupported()) {
918
            LOG.warn("Locks not supported");
919
            return null;
920
        }
921
        if (locks == null) {
922
            this.locks = this.provider.createFeatureLocks();
923
        }
924
        return locks;
925
    }
926

    
927
    //
928
    // ====================================================================
929
    // Interface Observable
930
    //
931

    
932
    @Override
933
    public void disableNotifications() {
934
        this.delegateObservable.disableNotifications();
935

    
936
    }
937

    
938
    @Override
939
    public void enableNotifications() {
940
        this.delegateObservable.enableNotifications();
941
    }
942

    
943
    @Override
944
    public void beginComplexNotification() {
945
        this.delegateObservable.beginComplexNotification();
946

    
947
    }
948

    
949
    @Override
950
    public void endComplexNotification() {
951
        this.delegateObservable.endComplexNotification();
952

    
953
    }
954

    
955
    @Override
956
    public void addObserver(Observer observer) {
957
        if (delegateObservable != null) {
958
            this.delegateObservable.addObserver(observer);
959
        }
960
    }
961

    
962
    @Override
963
    public void deleteObserver(Observer observer) {
964
        if (delegateObservable != null) {
965
            this.delegateObservable.deleteObserver(observer);
966
        }
967
    }
968

    
969
    @Override
970
    public void deleteObservers() {
971
        this.delegateObservable.deleteObservers();
972

    
973
    }
974

    
975
    //
976
    // ====================================================================
977
    // Interface Observer
978
    //
979
    // Usado para observar:
980
    // - su seleccion
981
    // - sus bloqueos
982
    // - sus recursos
983
    //
984

    
985
    @Override
986
    public void update(Observable observable, Object notification) {
987
        if (observable instanceof FeatureSet) {
988
            if (observable == this.selection) {
989
                this.notifyChange(DataStoreNotification.SELECTION_CHANGE);
990
            } else if (observable == this.locks) {
991
                this.notifyChange(FeatureStoreNotification.LOCKS_CHANGE);
992
            }
993

    
994
        } else if (observable instanceof FeatureStoreProvider) {
995
            if (observable == this.provider) {
996

    
997
            }
998
        } else if (observable instanceof FeatureReferenceSelection) {
999
            if(notification instanceof String){
1000
                    this.notifyChange((String)notification);
1001
            }
1002
        }
1003
    }
1004

    
1005
    //
1006
    // ====================================================================
1007
    // Edicion
1008
    //
1009

    
1010
    private void newVersionOfUpdate() {
1011
        this.versionOfUpdate++;
1012
    }
1013

    
1014
    private long currentVersionOfUpdate() {
1015
        return this.versionOfUpdate;
1016
    }
1017

    
1018
    private void checkInEditingMode() throws NeedEditingModeException {
1019
        if (mode != MODE_FULLEDIT) {
1020
            throw new NeedEditingModeException(this.getName());
1021
        }
1022
    }
1023

    
1024
    private void checkNotInAppendMode() throws IllegalStateException {
1025
        if (mode == MODE_APPEND) {
1026
                        throw new IllegalStateException("Error: store "
1027
                                        + this.getFullName() + " is in append mode");
1028
        }
1029
    }
1030

    
1031
    private void checkIsOwnFeature(Feature feature)
1032
        throws IllegalFeatureException {
1033
        if (((DefaultFeature) feature).getStore() != this) {
1034
            throw new IllegalFeatureException(this.getName());
1035
        }
1036
        // FIXME: fixFeatureType no vale para el checkIsOwnFeature
1037
        // fixFeatureType((DefaultFeatureType) feature.getType());
1038
    }
1039

    
1040
    private void exitEditingMode() {
1041
        if (commands != null) {
1042
            commands.clear();
1043
            commands = null;
1044
        }
1045

    
1046
        if (featureTypeManager != null) {
1047
            featureTypeManager.dispose();
1048
            featureTypeManager = null;
1049

    
1050
        }
1051

    
1052
        // TODO implementar un dispose para estos dos
1053
        featureManager = null;
1054
        spatialManager = null;
1055

    
1056
        featureCount = null;
1057

    
1058
        mode = MODE_QUERY;
1059
        hasStrongChanges = true; // Lo deja a true por si las moscas
1060
        hasInserts = true;
1061
    }
1062

    
1063
    @Override
1064
    synchronized public void edit() throws DataException {
1065
        edit(MODE_FULLEDIT);
1066
    }
1067

    
1068
    @Override
1069
    synchronized public void edit(int mode) throws DataException {
1070
        LOG.debug("Starting editing in mode: {}", mode);
1071
        try {
1072
            if (this.mode != MODE_QUERY) {
1073
                throw new AlreadyEditingException(this.getName());
1074
            }
1075
            if (!this.provider.supportsAppendMode()) {
1076
                mode = MODE_FULLEDIT;
1077
            }
1078
            switch (mode) {
1079
            case MODE_QUERY:
1080
                throw new IllegalStateException(this.getName());
1081

    
1082
            case MODE_FULLEDIT:
1083
                if (!this.transforms.isEmpty()) {
1084
                    throw new IllegalStateException(this.getName());
1085
                }
1086
                notifyChange(FeatureStoreNotification.BEFORE_STARTEDITING);
1087
                invalidateIndexes();
1088
                featureManager = new FeatureManager();
1089
                featureTypeManager = new FeatureTypeManager(this);
1090
                spatialManager = new SpatialManager(this, provider.getEnvelope());
1091

    
1092
                commands = new DefaultFeatureCommandsStack(
1093
                        this, featureManager,
1094
                        spatialManager, featureTypeManager);
1095
                this.mode = MODE_FULLEDIT;
1096
                hasStrongChanges = false;
1097
                hasInserts = false;
1098
                notifyChange(FeatureStoreNotification.AFTER_STARTEDITING);
1099
                break;
1100
            case MODE_APPEND:
1101
                if (!this.transforms.isEmpty()) {
1102
                    throw new IllegalStateException(this.getName());
1103
                }
1104
                notifyChange(FeatureStoreNotification.BEFORE_STARTEDITING);
1105
                invalidateIndexes();
1106
                this.provider.beginAppend();
1107
                this.mode = MODE_APPEND;
1108
                hasInserts = false;
1109
                notifyChange(FeatureStoreNotification.AFTER_STARTEDITING);
1110
                break;
1111
            }
1112
        } catch (Exception e) {
1113
            throw new StoreEditException(e, this.getName());
1114
        }
1115
    }
1116

    
1117
    private void invalidateIndexes() {
1118
        setIndexesValidStatus(false);
1119
    }
1120

    
1121
    private void setIndexesValidStatus(boolean valid) {
1122
        FeatureIndexes theIndexes = getIndexes();
1123
        LOG.debug("Setting the store indexes to valid status {}: {}", (valid
1124
            ? Boolean.TRUE : Boolean.FALSE), theIndexes);
1125
        for (Iterator iterator = theIndexes.iterator(); iterator.hasNext();) {
1126
            FeatureIndex index = (FeatureIndex) iterator.next();
1127
            if (index instanceof FeatureIndexProviderServices) {
1128
                FeatureIndexProviderServices indexServices =
1129
                    (FeatureIndexProviderServices) index;
1130
                indexServices.setValid(valid);
1131
            }
1132
        }
1133
    }
1134

    
1135
    private void updateIndexes() throws FeatureIndexException {
1136
        FeatureIndexes theIndexes = getIndexes();
1137
        LOG.debug("Refilling indexes: {}", theIndexes);
1138
        for (Iterator iterator = theIndexes.iterator(); iterator.hasNext();) {
1139
            FeatureIndex index = (FeatureIndex) iterator.next();
1140
            if (index instanceof FeatureIndexProviderServices) {
1141
                FeatureIndexProviderServices indexServices =
1142
                    (FeatureIndexProviderServices) index;
1143
                indexServices.fill(true, null);
1144
            }
1145
        }
1146
    }
1147

    
1148
    private void waitForIndexes() {
1149
        FeatureIndexes theIndexes = getIndexes();
1150
        LOG.debug("Waiting for indexes to finish filling: {}", theIndexes);
1151
        for (Iterator iterator = theIndexes.iterator(); iterator.hasNext();) {
1152
            FeatureIndex index = (FeatureIndex) iterator.next();
1153
            if (index instanceof FeatureIndexProviderServices) {
1154
                FeatureIndexProviderServices indexServices =
1155
                    (FeatureIndexProviderServices) index;
1156
                indexServices.waitForIndex();
1157
            }
1158
        }
1159
    }
1160

    
1161
    private void disposeIndexes() {
1162
        FeatureIndexes theIndexes = getIndexes();
1163
        LOG.debug("Disposing indexes: {}", theIndexes);
1164
        if( theIndexes==null ) {
1165
            return;
1166
        }
1167
        for (Iterator iterator = theIndexes.iterator(); iterator.hasNext();) {
1168
            FeatureIndex index = (FeatureIndex) iterator.next();
1169
            if (index instanceof FeatureIndexProviderServices) {
1170
                FeatureIndexProviderServices indexServices =
1171
                    (FeatureIndexProviderServices) index;
1172
                indexServices.dispose();
1173
            }
1174
        }
1175
    }
1176

    
1177
    @Override
1178
    public boolean isEditing() {
1179
        return mode == MODE_FULLEDIT;
1180
    }
1181

    
1182
    @Override
1183
    public boolean isAppending() {
1184
        return mode == MODE_APPEND;
1185
    }
1186

    
1187
    @Override
1188
    synchronized public void update(EditableFeatureType type)
1189
        throws DataException {
1190
        try {
1191
            if (type == null) {
1192
                throw new NullFeatureTypeException(getName());
1193
            }
1194
            boolean typehasStrongChanges = ((DefaultEditableFeatureType) type).hasStrongChanges();
1195
            if (typehasStrongChanges) {
1196
                checkInEditingMode();
1197
            }  else if(!this.isAppending()) {
1198
                throw new NeedEditingModeException(this.getName());
1199
            }
1200
            // FIXME: Comprobar que es un featureType aceptable.
1201
            notifyChange(FeatureStoreNotification.BEFORE_UPDATE_TYPE, type);
1202
            newVersionOfUpdate();
1203
            
1204
            if (typehasStrongChanges) { 
1205
                FeatureType oldt = type.getSource().getCopy();
1206
                FeatureType newt = type.getCopy();
1207
                commands.update(newt, oldt);
1208
                hasStrongChanges = true;
1209
            } else {
1210
                boolean ok = this.featureTypes.remove(this.defaultFeatureType);
1211
                this.defaultFeatureType = type.getCopy();
1212
                if (ok) {
1213
                    this.featureTypes.add(this.defaultFeatureType);
1214
                }
1215
            }
1216
            notifyChange(FeatureStoreNotification.AFTER_UPDATE_TYPE, type);
1217
        } catch (Exception e) {
1218
            throw new StoreUpdateFeatureTypeException(e, this.getName());
1219
        }
1220
    }
1221

    
1222
    @Override
1223
    public void delete(Feature feature) throws DataException {
1224
        this.commands.delete(feature);
1225
    }
1226

    
1227
    synchronized public void doDelete(Feature feature) throws DataException {
1228
        try {
1229
            checkInEditingMode();
1230
            checkIsOwnFeature(feature);
1231
            if (feature instanceof EditableFeature) {
1232
                throw new StoreDeleteEditableFeatureException(getName());
1233
            }
1234
            notifyChange(FeatureStoreNotification.BEFORE_DELETE, feature);
1235

    
1236
            //Update the featureManager and the spatialManager
1237
            featureManager.delete(feature.getReference());
1238
            spatialManager.deleteFeature(feature);
1239

    
1240
            newVersionOfUpdate();
1241
            hasStrongChanges = true;
1242
            notifyChange(FeatureStoreNotification.AFTER_DELETE, feature);
1243
        } catch (Exception e) {
1244
            throw new StoreDeleteFeatureException(e, this.getName());
1245
        }
1246
    }
1247

    
1248
    private static EditableFeature lastChangedFeature = null;
1249

    
1250
    @Override
1251
    public synchronized void insert(EditableFeature feature)
1252
        throws DataException {
1253
        LOG.debug("In editing mode {}, insert feature: {}", mode, feature);
1254
        try {
1255
            switch (mode) {
1256
            case MODE_QUERY:
1257
                throw new NeedEditingModeException(this.getName());
1258

    
1259
            case MODE_APPEND:
1260
                checkIsOwnFeature(feature);
1261
                if (feature.getSource() != null) {
1262
                    throw new NoNewFeatureInsertException(this.getName());
1263
                }
1264
                this.featureCount = null;
1265
                notifyChange(FeatureStoreNotification.BEFORE_INSERT, feature);
1266
                feature.validate(Feature.UPDATE);
1267
                provider.append(((DefaultEditableFeature) feature).getData());
1268
                hasStrongChanges = true;
1269
                hasInserts = true;
1270
                notifyChange(FeatureStoreNotification.AFTER_INSERT, feature);
1271
                break;
1272

    
1273
            case MODE_FULLEDIT:
1274
                if (feature.getSource() != null) {
1275
                    throw new NoNewFeatureInsertException(this.getName());
1276
                }
1277
                commands.insert(feature);
1278
            }
1279
        } catch (Exception e) {
1280
            throw new StoreInsertFeatureException(e, this.getName());
1281
        }
1282
    }
1283

    
1284
    synchronized public void doInsert(EditableFeature feature)
1285
        throws DataException {
1286
        checkIsOwnFeature(feature);
1287

    
1288
        waitForIndexes();
1289

    
1290
        notifyChange(FeatureStoreNotification.BEFORE_INSERT, feature);
1291
        newVersionOfUpdate();
1292
        if ((lastChangedFeature == null)
1293
            || (lastChangedFeature.getSource() != feature.getSource())) {
1294
            lastChangedFeature = feature;
1295
            feature.validate(Feature.UPDATE);
1296
            lastChangedFeature = null;
1297
        }
1298
        //Update the featureManager and the spatialManager
1299
        ((DefaultEditableFeature) feature).setInserted(true);
1300
        DefaultFeature newFeature = (DefaultFeature) feature.getNotEditableCopy();
1301

    
1302

    
1303
        featureManager.add(newFeature);
1304
        spatialManager.insertFeature(newFeature);
1305

    
1306
        hasStrongChanges = true;
1307
        hasInserts = true;
1308
        notifyChange(FeatureStoreNotification.AFTER_INSERT, feature);
1309
    }
1310

    
1311
    @Override
1312
    public void update(EditableFeature feature)
1313
    throws DataException {
1314
        if ((feature).getSource() == null) {
1315
            insert(feature);
1316
            return;
1317
        }
1318
        commands.update(feature, feature.getSource());
1319
    }
1320

    
1321
    synchronized public void doUpdate(EditableFeature feature, Feature oldFeature)
1322
        throws DataException {
1323
        try {
1324
            checkInEditingMode();
1325
            checkIsOwnFeature(feature);
1326
            notifyChange(FeatureStoreNotification.BEFORE_UPDATE, feature);
1327
            newVersionOfUpdate();
1328
            if ((lastChangedFeature == null)
1329
                || (lastChangedFeature.getSource() != feature.getSource())) {
1330
                lastChangedFeature = feature;
1331
                feature.validate(Feature.UPDATE);
1332
                lastChangedFeature = null;
1333
            }
1334

    
1335
            //Update the featureManager and the spatialManager
1336
            Feature newf = feature.getNotEditableCopy();
1337
            featureManager.update(newf, oldFeature);
1338
            spatialManager.updateFeature(newf, oldFeature);
1339

    
1340
            hasStrongChanges = true;
1341
            notifyChange(FeatureStoreNotification.AFTER_UPDATE, feature);
1342
        } catch (Exception e) {
1343
            throw new StoreUpdateFeatureException(e, this.getName());
1344
        }
1345
    }
1346

    
1347
    @Override
1348
    synchronized public void redo() throws RedoException {
1349
        Command redo = commands.getNextRedoCommand();
1350
        try {
1351
            checkInEditingMode();
1352
        } catch (NeedEditingModeException ex) {
1353
            throw new RedoException(redo, ex);
1354
        }
1355
        notifyChange(FeatureStoreNotification.BEFORE_REDO, redo);
1356
        newVersionOfUpdate();
1357
        commands.redo();
1358
        hasStrongChanges = true;
1359
        notifyChange(FeatureStoreNotification.AFTER_REDO, redo);
1360
    }
1361

    
1362
    @Override
1363
    synchronized public void undo() throws UndoException {
1364
        Command undo = commands.getNextUndoCommand();
1365
        try {
1366
            checkInEditingMode();
1367
        } catch (NeedEditingModeException ex) {
1368
            throw new UndoException(undo, ex);
1369
        }
1370
        notifyChange(FeatureStoreNotification.BEFORE_UNDO, undo);
1371
        newVersionOfUpdate();
1372
        commands.undo();
1373
        hasStrongChanges = true;
1374
        notifyChange(FeatureStoreNotification.AFTER_UNDO, undo);
1375
    }
1376

    
1377
    @Override
1378
    public List getRedoInfos() {
1379
        if (isEditing() && (commands != null)) {
1380
            return commands.getRedoInfos();
1381
        } else {
1382
            return null;
1383
        }
1384
    }
1385

    
1386
    @Override
1387
    public List getUndoInfos() {
1388
        if (isEditing() && (commands != null)) {
1389
            return commands.getUndoInfos();
1390
        } else {
1391
            return null;
1392
        }
1393
    }
1394

    
1395
    public synchronized FeatureCommandsStack getCommandsStack()
1396
        throws DataException {
1397
        checkInEditingMode();
1398
        return commands;
1399
    }
1400

    
1401
    @Override
1402
    synchronized public void cancelEditing() throws DataException {
1403
        if( spatialManager!=null ) {
1404
            spatialManager.cancelModifies();
1405
        }
1406
        try {
1407
            switch (mode) {
1408
            case MODE_QUERY:
1409
                throw new NeedEditingModeException(this.getName());
1410

    
1411
            case MODE_APPEND:
1412
                notifyChange(FeatureStoreNotification.BEFORE_CANCELEDITING);
1413
                provider.abortAppend();
1414
                exitEditingMode();
1415
                ((FeatureSelection) this.getSelection()).deselectAll();
1416
                updateIndexes();
1417
                notifyChange(FeatureStoreNotification.AFTER_CANCELEDITING);   
1418

    
1419
            case MODE_FULLEDIT:
1420
                boolean clearSelection = this.hasStrongChanges;
1421
                if (this.selection instanceof FeatureReferenceSelection) {
1422
                    clearSelection = this.hasInserts;
1423
                }
1424
                notifyChange(FeatureStoreNotification.BEFORE_CANCELEDITING);
1425
                exitEditingMode();
1426
                if (clearSelection) {
1427
                    ((FeatureSelection) this.getSelection()).deselectAll();
1428
                }
1429
                updateIndexes();
1430
                notifyChange(FeatureStoreNotification.AFTER_CANCELEDITING);   
1431
            }
1432
        } catch (Exception e) {
1433
            throw new StoreCancelEditingException(e, this.getName());
1434
        }
1435
    }
1436

    
1437
    @Override
1438
    synchronized public void finishEditing() throws DataException {
1439
        LOG.debug("finish editing of mode: {}", mode);
1440
        try {
1441

    
1442
            /*
1443
             * Selection needs to be cleared when editing stops
1444
             * to prevent conflicts with selection remaining from
1445
             * editing mode.
1446
             */
1447
//            ((FeatureSelection) this.getSelection()).deselectAll();
1448
            Map<String,List<FeatureAttributeDescriptor>> computedFields = this.getComputedFields();
1449
            switch (mode) {
1450
            case MODE_QUERY:
1451
                throw new NeedEditingModeException(this.getName());
1452

    
1453
            case MODE_APPEND:
1454
                if( selection!=null ) {
1455
                    selection = null;
1456
                }
1457
                notifyChange(FeatureStoreNotification.BEFORE_FINISHEDITING);
1458
                provider.endAppend();
1459
                exitEditingMode();
1460
                this.updateComputedFields(computedFields);
1461
                saveDALFile();
1462
                updateIndexes();
1463
                notifyChange(FeatureStoreNotification.AFTER_FINISHEDITING);
1464
                break;
1465

    
1466
            case MODE_FULLEDIT:
1467
                if (hasStrongChanges && !this.allowWrite()) {
1468
                    throw new WriteNotAllowedException(getName());
1469
                }
1470
                if(featureManager.isSelectionCompromised() && selection!=null ) {
1471
                    selection = null;
1472
                }
1473
                notifyChange(FeatureStoreNotification.BEFORE_FINISHEDITING);
1474
                if (hasStrongChanges) {
1475
                    validateFeatures(Feature.FINISH_EDITING);
1476

    
1477
                    /*
1478
                     * This will throw a PerformEditingExceptionif the provider
1479
                     * does not accept the changes (for example, an invalid field name)
1480
                     */
1481
                    provider.performChanges(featureManager.getDeleted(),
1482
                        featureManager.getInserted(),
1483
                        featureManager.getUpdated(),
1484
                        removeCalculatedAttributes(featureTypeManager.getFeatureTypesChanged()).iterator());
1485
                    
1486
                }  
1487
                exitEditingMode();
1488
                this.updateComputedFields(computedFields);
1489
                saveDALFile();
1490
                updateIndexes();
1491
                notifyChange(FeatureStoreNotification.AFTER_FINISHEDITING);
1492
                break;
1493
            }
1494
        } catch (PerformEditingException pee) {
1495
            throw new WriteException(provider.getSourceId().toString(), pee);
1496
        } catch (Exception e) {
1497
            throw new FinishEditingException(e);
1498
        }
1499
    }
1500
    private Map<String,List<FeatureAttributeDescriptor>> getComputedFields() throws DataException {
1501
        Map<String,List<FeatureAttributeDescriptor>> r = new HashMap<>();
1502
        
1503
        List<FeatureType> theTypes = new ArrayList<>();
1504
        theTypes.addAll(this.getFeatureTypes());
1505
        theTypes.add(this.getDefaultFeatureType());
1506
        for( int n=0; n<theTypes.size(); n++ ) {
1507
            FeatureType type = theTypes.get(n);
1508
                for (FeatureAttributeDescriptor attrdesc : type) {
1509
                    FeatureAttributeEmulator emulator = attrdesc.getFeatureAttributeEmulator();
1510
                    if( emulator!= null) {
1511
                        List<FeatureAttributeDescriptor> l = r.get(type.getId());
1512
                        if (l==null) {
1513
                            l = new ArrayList<>();
1514
                            r.put(type.getId(), l);
1515
                        }
1516
                        l.add(attrdesc);
1517
                    }
1518
            }
1519
        }
1520
        return r;
1521
    }
1522
    private void updateComputedFields(Map<String,List<FeatureAttributeDescriptor>> computedFields) throws DataException {
1523

    
1524
        List<FeatureType> theTypes = new ArrayList<>();
1525
        theTypes.addAll(this.getFeatureTypes());
1526
        theTypes.add(this.getDefaultFeatureType());
1527
        for( int n=0; n<theTypes.size(); n++ ) {
1528
            DefaultFeatureType type = (DefaultFeatureType) theTypes.get(n);
1529
            List<FeatureAttributeDescriptor> x = computedFields.get(type.getId());
1530
            if(x!=null && !x.isEmpty()) {
1531
                for (FeatureAttributeDescriptor attrdesc : x) {
1532
                    if (type.get(attrdesc.getName())==null) {
1533
                        type.add(attrdesc);
1534
                    }
1535
                }
1536
            }
1537
        }
1538
        
1539
    }
1540
    private List<FeatureStoreProvider.FeatureTypeChanged> removeCalculatedAttributes(List<FeatureStoreProvider.FeatureTypeChanged> ftypes) {
1541
        // FIXME: Falta por implementar
1542
//        for (FeatureStoreProvider.FeatureTypeChanged ftype : ftypes) {
1543
//            EditableFeatureType target = (EditableFeatureType) ftype.getTarget();
1544
//            for (FeatureAttributeDescriptor attributeDescriptor : ftype.getSource().getAttributeDescriptors()) {
1545
//                if (attributeDescriptor.isComputed()) {
1546
//                    target.remove(attributeDescriptor.getName());
1547
//                }
1548
//            }
1549
//        }
1550
        return ftypes;
1551
    }
1552
    
1553

    
1554
    @SuppressWarnings("UseSpecificCatch")
1555
    private void saveDALFile() {       
1556
        try {
1557
            DataServerExplorer explorer = this.getExplorer();
1558
            if( explorer == null ) {
1559
                return;
1560
            }
1561
            File f = explorer.getResourcePath(this, "dal");
1562
            if( f == null ) {
1563
                return;
1564
            }
1565
            DALFile dalFile = DALFile.getDALFile();
1566
            dalFile.setStore(this);
1567
            if( !dalFile.isEmpty() ) {
1568
                dalFile.write(f);
1569
            }
1570
        } catch (Exception ex) {
1571
            LOG.warn("Can't save DAL File", ex);
1572
        }
1573
    }
1574
    
1575
    @SuppressWarnings("UseSpecificCatch")
1576
    private void loadDALFile() {
1577
        try {
1578
            DataServerExplorer explorer = this.getExplorer();
1579
            if( explorer == null ) {
1580
                return;
1581
            }
1582
            File f = explorer.getResourcePath(this, "dal");
1583
            if( f == null || !f.exists() ) {
1584
                return;
1585
            }
1586
            DALFile dalFile = DALFile.getDALFile(f);
1587
            if( !dalFile.isEmpty() ) {
1588
                dalFile.updateStore(this);
1589
            }
1590
        } catch (Exception ex) {
1591
            LOG.warn("Can't load DAL File", ex);
1592
        }
1593
    }
1594
    
1595
    /**
1596
     * Save changes in the provider without leaving the edit mode.
1597
     * Do not call observers to communicate a change of ediding mode.
1598
     * The operation's history is eliminated to prevent inconsistencies
1599
     * in the data.
1600
     *
1601
     * @throws DataException
1602
     */
1603
    @Override
1604
    synchronized public void commitChanges() throws DataException {
1605
      LOG.debug("commitChanges of mode: {}", mode);
1606
      if( !canCommitChanges() ) {
1607
              throw new WriteNotAllowedException(getName());
1608
      }
1609
      try {
1610
        switch (mode) {
1611
        case MODE_QUERY:
1612
          throw new NeedEditingModeException(this.getName());
1613

    
1614
        case MODE_APPEND:
1615
          this.provider.endAppend();
1616
          exitEditingMode();
1617
          invalidateIndexes();
1618
          this.provider.beginAppend();
1619
          hasInserts = false;
1620
          break;
1621

    
1622
        case MODE_FULLEDIT:
1623
          if (hasStrongChanges && !this.allowWrite()) {
1624
            throw new WriteNotAllowedException(getName());
1625
          }
1626
          if (hasStrongChanges) {
1627
            validateFeatures(Feature.FINISH_EDITING);
1628
            provider.performChanges(featureManager.getDeleted(),
1629
              featureManager.getInserted(),
1630
              featureManager.getUpdated(),
1631
              removeCalculatedAttributes(featureTypeManager.getFeatureTypesChanged()).iterator());
1632
          }
1633
          invalidateIndexes();
1634
          featureManager = new FeatureManager();
1635
          featureTypeManager = new FeatureTypeManager(this);
1636
          spatialManager = new SpatialManager(this, provider.getEnvelope());
1637

    
1638
          commands =
1639
            new DefaultFeatureCommandsStack(this, featureManager,
1640
              spatialManager, featureTypeManager);
1641
          featureCount = null;
1642
          hasStrongChanges = false;
1643
          hasInserts = false;
1644
          break;
1645
        }
1646
      } catch (Exception e) {
1647
        throw new FinishEditingException(e);
1648
      }
1649
    }
1650

    
1651
    @Override
1652
    synchronized public boolean canCommitChanges() throws DataException {
1653
        if ( !this.allowWrite()) {
1654
                return false;
1655
        }
1656
            switch (mode) {
1657
            default:
1658
        case MODE_QUERY:
1659
                return false;
1660

    
1661
        case MODE_APPEND:
1662
                return true;
1663

    
1664
        case MODE_FULLEDIT:
1665
            List types = this.getFeatureTypes();
1666
            for( int i=0; i<types.size(); i++ ) {
1667
                    Object type = types.get(i);
1668
                    if( type instanceof DefaultEditableFeatureType ) {
1669
                            if( ((DefaultEditableFeatureType)type).hasStrongChanges() ) {
1670
                                    return false;
1671
                            }
1672
                    }
1673
            }
1674
            return true;
1675
            }
1676
    }
1677

    
1678
    @Override
1679
    public void beginEditingGroup(String description)
1680
        throws NeedEditingModeException {
1681
        checkInEditingMode();
1682
        commands.startComplex(description);
1683
    }
1684

    
1685
    @Override
1686
    public void endEditingGroup() throws NeedEditingModeException {
1687
        checkInEditingMode();
1688
        commands.endComplex();
1689
    }
1690

    
1691
    @Override
1692
    public boolean isAppendModeSupported() {
1693
        return this.provider.supportsAppendMode();
1694
    }
1695

    
1696
    @Override
1697
    public void export(DataServerExplorer explorer, String provider,
1698
        NewFeatureStoreParameters params) throws DataException {
1699

    
1700
        if (this.getFeatureTypes().size() != 1) {
1701
            throw new NotYetImplemented(
1702
                "export whith more than one type not yet implemented");
1703
        }
1704
        FeatureSelection featureSelection = (FeatureSelection) getSelection();
1705
        FeatureStore target = null;
1706
        FeatureSet features = null;
1707
        DisposableIterator iterator = null;
1708
        try {
1709
            FeatureType type = this.getDefaultFeatureType();
1710
            if ((params.getDefaultFeatureType() == null)
1711
                || (params.getDefaultFeatureType().size() == 0)) {
1712
                params.setDefaultFeatureType(type.getEditable());
1713

    
1714
            }
1715
            explorer.add(provider, params, true);
1716

    
1717
            DataManager manager = DALLocator.getDataManager();
1718
            target = (FeatureStore) manager.openStore(provider, params);
1719
            FeatureType targetType = target.getDefaultFeatureType();
1720

    
1721
            target.edit(MODE_APPEND);
1722
            FeatureAttributeDescriptor[] pkattrs = type.getPrimaryKey();
1723
            if (featureSelection.getSize() > 0) {
1724
                features = this.getFeatureSelection();
1725
            } else {
1726
                if ((pkattrs != null) && (pkattrs.length > 0)) {
1727
                    FeatureQuery query = createFeatureQuery();
1728
                    for (FeatureAttributeDescriptor pkattr : pkattrs) {
1729
                        query.getOrder().add(pkattr.getName(), true);
1730
                    }
1731
                    features = this.getFeatureSet(query);
1732
                } else {
1733
                    features = this.getFeatureSet();
1734
                }
1735
            }
1736
            iterator = features.fastIterator();
1737
            while (iterator.hasNext()) {
1738
                DefaultFeature feature = (DefaultFeature) iterator.next();
1739
                target.insert(target.createNewFeature(targetType, feature));
1740
            }
1741
            target.finishEditing();
1742
            target.dispose();
1743
        } catch (Exception e) {
1744
            throw new DataExportException(e, params.toString());
1745
        } finally {
1746
            dispose(iterator);
1747
            dispose(features);
1748
            dispose(target);
1749
        }
1750
    }
1751

    
1752
    //
1753
    // ====================================================================
1754
    // Obtencion de datos
1755
    // getDataCollection, getFeatureCollection
1756
    //
1757

    
1758
    @Override
1759
    public DataSet getDataSet() throws DataException {
1760
        checkNotInAppendMode();
1761
        FeatureQuery query =
1762
            new DefaultFeatureQuery(this.getDefaultFeatureType());
1763
        return new DefaultFeatureSet(this, query);
1764
    }
1765

    
1766
    @Override
1767
    public DataSet getDataSet(DataQuery dataQuery) throws DataException {
1768
        checkNotInAppendMode();
1769
        return new DefaultFeatureSet(this, (FeatureQuery) dataQuery);
1770
    }
1771

    
1772
    @Override
1773
    public void getDataSet(Observer observer) throws DataException {
1774
        checkNotInAppendMode();
1775
        this.getFeatureSet(null, observer);
1776
    }
1777

    
1778
    @Override
1779
    public void getDataSet(DataQuery dataQuery, Observer observer)
1780
        throws DataException {
1781
        checkNotInAppendMode();
1782
        this.getFeatureSet((FeatureQuery) dataQuery, observer);
1783
    }
1784

    
1785
    @Override
1786
    public FeatureSet getFeatureSet() throws DataException {
1787
        return this.getFeatureSet((FeatureQuery)null);
1788
    }
1789

    
1790
    @Override
1791
    public FeatureSet getFeatureSet(FeatureQuery featureQuery)
1792
        throws DataException {
1793
        checkNotInAppendMode();
1794
        if( featureQuery==null ) {
1795
            featureQuery = new DefaultFeatureQuery(this.getDefaultFeatureType());
1796
        }
1797
        return new DefaultFeatureSet(this, featureQuery);
1798
    }
1799

    
1800
    @Override
1801
    public FeatureSet getFeatureSet(String filter) throws DataException {
1802
        return this.getFeatureSet(filter, null, true);
1803
    }
1804

    
1805
    @Override
1806
    public FeatureSet getFeatureSet(String filter, String sortBy) throws DataException {
1807
        return this.getFeatureSet(filter, sortBy, true);
1808
    }
1809

    
1810
    @Override
1811
    public FeatureSet getFeatureSet(String filter, String sortBy, boolean asc) throws DataException {
1812
        FeatureQuery query = this.createFeatureQuery();
1813
        if( !StringUtils.isEmpty(filter) ) {
1814
            query.setFilter(filter);
1815
        }
1816
        if( !StringUtils.isEmpty(sortBy) ) {
1817
            query.getOrder().add(sortBy, asc);
1818
        }
1819
        return this.getFeatureSet(query);
1820
    }
1821
    
1822
    @Override
1823
    public List<Feature> getFeatures(String filter)  {
1824
        return this.getFeatures(filter, null, true);
1825
    }
1826

    
1827
    @Override
1828
    public List<Feature> getFeatures(String filter, String sortBy)  {
1829
        return this.getFeatures(filter, sortBy, true);
1830
    }
1831

    
1832
    @Override
1833
    public List<Feature> getFeatures(String filter, String sortBy, boolean asc)  {
1834
        FeatureQuery query = this.createFeatureQuery();
1835
        if( !StringUtils.isEmpty(filter) ) {
1836
            query.setFilter(filter);
1837
        }
1838
        if( !StringUtils.isEmpty(sortBy) ) {
1839
            query.getOrder().add(sortBy, asc);
1840
        }
1841
        return this.getFeatures(query, 100);
1842
    }
1843
    
1844
    @Override
1845
    public List<Feature> getFeatures(FeatureQuery query)  {
1846
        return this.getFeatures(query, 100);
1847
    }
1848
    
1849
    @Override
1850
    public List<Feature> getFeatures(FeatureQuery query, int pageSize)  {
1851
        try {
1852
            FeaturePagingHelper pager = this.dataManager.createFeaturePagingHelper(this, query, pageSize);
1853
            return pager.asList();
1854
        } catch (BaseException ex) {
1855
            throw new RuntimeException("Can't create the list of features.", ex);
1856
        }
1857
    }
1858

    
1859
    @Override
1860
    public List<Feature> getFeatures() {
1861
        return this.getFeatures(null, 500);
1862
    }
1863

    
1864
    @Override
1865
    public Feature findFirst(String filter) throws DataException {
1866
        return this.findFirst(filter, null, true);
1867
    }
1868

    
1869
    @Override
1870
    public Feature findFirst(String filter, String sortBy) throws DataException {
1871
        return this.findFirst(filter, sortBy, true);
1872
    }
1873

    
1874
    @Override
1875
    public Feature findFirst(String filter, String sortBy, boolean asc) throws DataException {
1876
        FeatureSet set = this.getFeatureSet(filter, sortBy, asc);
1877
        if( set==null || set.isEmpty() ) {
1878
            return null;
1879
        }
1880
        DisposableIterator it = set.iterator();
1881
        Feature f = (Feature) it.next();
1882
        it.dispose();
1883
        return f;
1884
    }
1885
    
1886
    @Override
1887
    public void accept(Visitor visitor) throws BaseException {
1888
        FeatureSet set = getFeatureSet();
1889
        try {
1890
            set.accept(visitor);
1891
        } finally {
1892
            set.dispose();
1893
        }
1894
    }
1895

    
1896
    @Override
1897
    public void accept(Visitor visitor, DataQuery dataQuery)
1898
        throws BaseException {
1899
        FeatureSet set = getFeatureSet((FeatureQuery) dataQuery);
1900
        try {
1901
            set.accept(visitor);
1902
        } finally {
1903
            set.dispose();
1904
        }
1905
    }
1906

    
1907
    public FeatureType getFeatureType(FeatureQuery featureQuery)
1908
        throws DataException {
1909
        DefaultFeatureType fType =
1910
            (DefaultFeatureType) this.getFeatureType(featureQuery
1911
                .getFeatureTypeId());
1912
        if( featureQuery.hasAttributeNames() || featureQuery.hasConstantsAttributeNames() ) {
1913
            return fType.getSubtype(featureQuery.getAttributeNames(), featureQuery.getConstantsAttributeNames() );
1914
        }
1915
        return fType;
1916
    }
1917

    
1918
    @Override
1919
    public void getFeatureSet(Observer observer) throws DataException {
1920
        checkNotInAppendMode();
1921
        this.getFeatureSet(null, observer);
1922
    }
1923

    
1924
    @Override
1925
    public void getFeatureSet(FeatureQuery query, Observer observer)
1926
        throws DataException {
1927
        class LoadInBackGround implements Runnable {
1928

    
1929
            private final FeatureStore store;
1930
            private final FeatureQuery query;
1931
            private final Observer observer;
1932

    
1933
            public LoadInBackGround(FeatureStore store, FeatureQuery query,
1934
                Observer observer) {
1935
                this.store = store;
1936
                this.query = query;
1937
                this.observer = observer;
1938
            }
1939

    
1940
            void notify(FeatureStoreNotification theNotification) {
1941
                observer.update(store, theNotification);
1942
            }
1943

    
1944
            @Override
1945
            public void run() {
1946
                FeatureSet set = null;
1947
                try {
1948
                    set = store.getFeatureSet(query);
1949
                    notify(new DefaultFeatureStoreNotification(store,
1950
                        FeatureStoreNotification.LOAD_FINISHED, set));
1951
                } catch (Exception e) {
1952
                    notify(new DefaultFeatureStoreNotification(store,
1953
                        FeatureStoreNotification.LOAD_FINISHED, e));
1954
                } finally {
1955
                    dispose(set);
1956
                }
1957
            }
1958
        }
1959

    
1960
        checkNotInAppendMode();
1961
        if (query == null) {
1962
            query = new DefaultFeatureQuery(this.getDefaultFeatureType());
1963
        }
1964
        LoadInBackGround task = new LoadInBackGround(this, query, observer);
1965
        Thread thread = new Thread(task, "Load Feature Set in background");
1966
        thread.start();
1967
    }
1968

    
1969
    @Override
1970
    public Feature getFeatureByReference(FeatureReference reference)
1971
        throws DataException {
1972
        checkNotInAppendMode();
1973
        DefaultFeatureReference ref = (DefaultFeatureReference) reference;
1974
        FeatureType featureType;
1975
        if (ref.getFeatureTypeId() == null) {
1976
            featureType = this.getDefaultFeatureType();
1977
        } else {
1978
            featureType = this.getFeatureType(ref.getFeatureTypeId());
1979
        }
1980
        return this.getFeatureByReference(reference, featureType);
1981
    }
1982

    
1983
    @Override
1984
    public Feature getFeatureByReference(FeatureReference reference,
1985
        FeatureType featureType) throws DataException {
1986
        checkNotInAppendMode();
1987
        featureType = fixFeatureType((DefaultFeatureType) featureType);
1988
        if (this.mode == MODE_FULLEDIT) {
1989
            Feature f = featureManager.get(reference, this, featureType);
1990
            if (f != null) {
1991
                return f;
1992
            }
1993
        }
1994

    
1995
        FeatureType sourceFeatureType = featureType;
1996
        if (!this.transforms.isEmpty()) {
1997
            sourceFeatureType = this.transforms.getSourceFeatureTypeFrom(featureType);
1998
        }
1999
        // TODO comprobar que el id es de este store
2000

    
2001
        DefaultFeature feature =
2002
            new DefaultFeature(this,
2003
                this.provider.getFeatureProviderByReference(
2004
                    (FeatureReferenceProviderServices) reference, sourceFeatureType));
2005

    
2006
        if (!this.transforms.isEmpty()) {
2007
            return this.transforms.applyTransform(feature, featureType);
2008
        }
2009
        return feature;
2010
    }
2011

    
2012
    //
2013
    // ====================================================================
2014
    // Gestion de features
2015
    //
2016

    
2017
    private FeatureType fixFeatureType(DefaultFeatureType type)
2018
        throws DataException {
2019
        FeatureType original = this.getDefaultFeatureType();
2020

    
2021
        if ((type == null) || type.equals(original)) {
2022
            return original;
2023
        } else {
2024
            if (!type.isSubtypeOf(original)) {
2025
                Iterator iter = this.getFeatureTypes().iterator();
2026
                FeatureType tmpType;
2027
                boolean found = false;
2028
                while (iter.hasNext()) {
2029
                    tmpType = (FeatureType) iter.next();
2030
                    if (type.equals(tmpType)) {
2031
                        return type;
2032

    
2033
                    } else
2034
                        if (type.isSubtypeOf(tmpType)) {
2035
                            found = true;
2036
                            original = tmpType;
2037
                            break;
2038
                        }
2039

    
2040
                }
2041
                if (!found) {
2042
                    throw new IllegalFeatureTypeException(getName());
2043
                }
2044
            }
2045
        }
2046

    
2047
        // Checks that type has all fields of pk
2048
        // else add the missing attributes at the end.
2049
        if (!original.hasOID()) {
2050
            // Gets original pk attributes
2051
            DefaultEditableFeatureType edOriginal =
2052
                (DefaultEditableFeatureType) original.getEditable();
2053
            FeatureAttributeDescriptor orgAttr;
2054
            Iterator edOriginalIter = edOriginal.iterator();
2055
            while (edOriginalIter.hasNext()) {
2056
                orgAttr = (FeatureAttributeDescriptor) edOriginalIter.next();
2057
                if (!orgAttr.isPrimaryKey()) {
2058
                    edOriginalIter.remove();
2059
                }
2060
            }
2061

    
2062
            // Checks if all pk attributes are in type
2063
            Iterator typeIterator;
2064
            edOriginalIter = edOriginal.iterator();
2065
            FeatureAttributeDescriptor attr;
2066
            while (edOriginalIter.hasNext()) {
2067
                orgAttr = (FeatureAttributeDescriptor) edOriginalIter.next();
2068
                typeIterator = type.iterator();
2069
                while (typeIterator.hasNext()) {
2070
                    attr = (FeatureAttributeDescriptor) typeIterator.next();
2071
                    if (attr.getName().equals(orgAttr.getName())) {
2072
                        edOriginalIter.remove();
2073
                        break;
2074
                    }
2075
                }
2076
            }
2077

    
2078
            // add missing pk attributes if any
2079
            if (edOriginal.size() > 0) {
2080
                boolean isEditable = type instanceof DefaultEditableFeatureType;
2081
                DefaultEditableFeatureType edType =
2082
                    (DefaultEditableFeatureType) original.getEditable();
2083
                edType.clear();
2084
                edType.addAll(type);
2085
                edType.addAll(edOriginal);
2086
                if (!isEditable) {
2087
                    type = (DefaultFeatureType) edType.getNotEditableCopy();
2088
                }
2089
            }
2090

    
2091
        }
2092

    
2093
        return type;
2094
    }
2095

    
2096
    @Override
2097
    public void validateFeatures(int mode) throws DataException {
2098
        FeatureSet collection = null;
2099
        DisposableIterator iter = null;
2100
        try {
2101
            FeatureRules rules = this.getDefaultFeatureType().getRules();
2102
            if( rules==null || rules.isEmpty() ) {
2103
                return;
2104
            }
2105
            checkNotInAppendMode();
2106
            collection = this.getFeatureSet();
2107
            iter = collection.fastIterator();
2108
            long previousVersionOfUpdate = currentVersionOfUpdate();
2109
            while (iter.hasNext()) {
2110
                ((DefaultFeature) iter.next()).validate(mode);
2111
                if (previousVersionOfUpdate != currentVersionOfUpdate()) {
2112
                    throw new ConcurrentDataModificationException(getName());
2113
                }
2114
            }
2115
        } catch (Exception e) {
2116
            throw new ValidateFeaturesException(e, getName());
2117
        } finally {
2118
            DisposeUtils.disposeQuietly(iter);
2119
            DisposeUtils.disposeQuietly(collection);
2120
        }
2121
    }
2122

    
2123
    @Override
2124
    public FeatureType getDefaultFeatureType() throws DataException {
2125
        try {
2126

    
2127
            if (isEditing()) {
2128
                FeatureType auxFeatureType =
2129
                    featureTypeManager.getType(defaultFeatureType.getId());
2130
                if (auxFeatureType != null) {
2131
                    return avoidEditable(auxFeatureType);
2132
                }
2133
            }
2134
            FeatureType type = this.transforms.getDefaultFeatureType();
2135
            if (type != null) {
2136
                return avoidEditable(type);
2137
            }
2138

    
2139
            return avoidEditable(defaultFeatureType);
2140

    
2141
        } catch (Exception e) {
2142
            throw new GetFeatureTypeException(e, getName());
2143
        }
2144
    }
2145

    
2146
    private FeatureType avoidEditable(FeatureType ft) {
2147
        if (ft instanceof EditableFeatureType) {
2148
            return ((EditableFeatureType) ft).getNotEditableCopy();
2149
        } else {
2150
            return ft;
2151
        }
2152
    }
2153

    
2154
    @Override
2155
    public FeatureType getFeatureType(String featureTypeId)
2156
        throws DataException {
2157
        if (featureTypeId == null) {
2158
            return this.getDefaultFeatureType();
2159
        }
2160
        try {
2161
            if (isEditing()) {
2162
                FeatureType auxFeatureType =
2163
                    featureTypeManager.getType(featureTypeId);
2164
                if (auxFeatureType != null) {
2165
                    return auxFeatureType;
2166
                }
2167
            }
2168
            FeatureType type = this.transforms.getFeatureType(featureTypeId);
2169
            if (type != null) {
2170
                return type;
2171
            }
2172
            Iterator iter = this.featureTypes.iterator();
2173
            while (iter.hasNext()) {
2174
                type = (FeatureType) iter.next();
2175
                if (type.getId().equals(featureTypeId)) {
2176
                    return type;
2177
                }
2178
            }
2179
            return null;
2180
        } catch (Exception e) {
2181
            throw new GetFeatureTypeException(e, getName());
2182
        }
2183
    }
2184

    
2185
    public FeatureType getProviderDefaultFeatureType() {
2186
        return defaultFeatureType;
2187
    }
2188

    
2189
    @Override
2190
    public List getFeatureTypes() throws DataException {
2191
        try {
2192
            List types;
2193
            if (isEditing()) {
2194
                types = new ArrayList();
2195
                Iterator it = featureTypes.iterator();
2196
                while (it.hasNext()) {
2197
                    FeatureType type = (FeatureType) it.next();
2198
                    FeatureType typeaux =
2199
                        featureTypeManager.getType(type.getId());
2200
                    if (typeaux != null) {
2201
                        types.add(typeaux);
2202
                    } else {
2203
                        types.add(type);
2204
                    }
2205
                }
2206
                it = featureTypeManager.newsIterator();
2207
                while (it.hasNext()) {
2208
                    FeatureType type = (FeatureType) it.next();
2209
                    types.add(type);
2210
                }
2211
            } else {
2212
                types = this.transforms.getFeatureTypes();
2213
                if (types == null) {
2214
                    types = featureTypes;
2215
                }
2216
            }
2217
            return Collections.unmodifiableList(types);
2218
        } catch (Exception e) {
2219
            throw new GetFeatureTypeException(e, getName());
2220
        }
2221
    }
2222

    
2223
    public List getProviderFeatureTypes() throws DataException {
2224
        return Collections.unmodifiableList(this.featureTypes);
2225
    }
2226

    
2227
    @Override
2228
    public Feature createFeature(FeatureProvider data) throws DataException {
2229
        DefaultFeature feature = new DefaultFeature(this, data);
2230
        return feature;
2231
    }
2232

    
2233
    public Feature createFeature(FeatureProvider data, FeatureType type)
2234
        throws DataException {
2235
        // FIXME: falta por implementar
2236
        // Comprobar si es un subtipo del feature de data
2237
        // y construir un feature usando el subtipo.
2238
        // Probablemente requiera generar una copia del data.
2239
        throw new NotYetImplemented();
2240
    }
2241

    
2242
    @Override
2243
    public EditableFeature createNewFeature(FeatureType type,
2244
        Feature defaultValues) throws DataException {
2245
        try {
2246
            FeatureProvider data = createNewFeatureProvider(type);
2247
            DefaultEditableFeature feature =
2248
                new DefaultEditableFeature(this, data);
2249
            feature.initializeValues(defaultValues);
2250
            data.setNew(true);
2251

    
2252
            return feature;
2253
        } catch (Exception e) {
2254
            throw new CreateFeatureException(e, getName());
2255
        }
2256
    }
2257

    
2258
    private FeatureProvider createNewFeatureProvider(FeatureType type)
2259
        throws DataException {
2260
        type = this.fixFeatureType((DefaultFeatureType) type);
2261
        FeatureProvider data = this.provider.createFeatureProvider(type);
2262
        data.setNew(true);
2263
        if (type.hasOID() && (data.getOID() == null)) {
2264
            data.setOID(this.provider.createNewOID());
2265
        } else {
2266
            data.setOID(this.getTemporalOID());
2267
        }
2268
        return data;
2269

    
2270
    }
2271

    
2272
    @Override
2273
    public EditableFeature createNewFeature(FeatureType type,
2274
        boolean defaultValues) throws DataException {
2275
        try {
2276
            FeatureProvider data = createNewFeatureProvider(type);
2277
            DefaultEditableFeature feature =
2278
                new DefaultEditableFeature(this, data);
2279
            if (defaultValues) {
2280
                feature.initializeValues();
2281
            }
2282
            return feature;
2283
        } catch (Exception e) {
2284
            throw new CreateFeatureException(e, getName());
2285
        }
2286
    }
2287

    
2288
    @Override
2289
    public EditableFeature createNewFeature(boolean defaultValues)
2290
        throws DataException {
2291
        return this.createNewFeature(this.getDefaultFeatureType(),
2292
            defaultValues);
2293
    }
2294

    
2295
    @Override
2296
    public EditableFeature createNewFeature() throws DataException {
2297
        return this.createNewFeature(this.getDefaultFeatureType(), true);
2298
    }
2299

    
2300
    @Override
2301
    public EditableFeature createNewFeature(Feature defaultValues) throws DataException {
2302
        FeatureType ft = this.getDefaultFeatureType();
2303
        EditableFeature f = this.createNewFeature(ft, false);
2304
                for( FeatureAttributeDescriptor desc : ft ) {
2305
                        try {
2306
                                f.set(desc.getName(), defaultValues.get(desc.getName()));
2307
                        } catch(Throwable th) {
2308
                                // Ignore
2309
                        }
2310
                }
2311
        return f;
2312
    }
2313

    
2314
    @Override
2315
    public EditableFeatureType createFeatureType() {
2316
        EditableFeatureType ftype = new DefaultEditableFeatureType(this);
2317
        return ftype;
2318
    }
2319

    
2320
    @Override
2321
    public EditableFeatureType createFeatureType(String id) {
2322
        DefaultEditableFeatureType ftype = new DefaultEditableFeatureType(this, id);
2323
        return ftype;
2324
    }
2325

    
2326
    //
2327
    // ====================================================================
2328
    // Index related methods
2329
    //
2330

    
2331
    @Override
2332
    public FeatureIndexes getIndexes() {
2333
        return this.indexes;
2334
    }
2335

    
2336
    @Override
2337
    public FeatureIndex createIndex(FeatureType featureType,
2338
        String attributeName, String indexName) throws DataException {
2339
        return createIndex(null, featureType, attributeName, indexName);
2340
    }
2341

    
2342
    @Override
2343
    public FeatureIndex createIndex(String indexTypeName,
2344
        FeatureType featureType, String attributeName, String indexName)
2345
        throws DataException {
2346

    
2347
        return createIndex(indexTypeName, featureType, attributeName,
2348
            indexName, false, null);
2349
    }
2350

    
2351
    @Override
2352
    public FeatureIndex createIndex(FeatureType featureType,
2353
        String attributeName, String indexName, Observer observer)
2354
        throws DataException {
2355
        return createIndex(null, featureType, attributeName, indexName,
2356
            observer);
2357
    }
2358

    
2359
    @Override
2360
    public FeatureIndex createIndex(String indexTypeName,
2361
        FeatureType featureType, String attributeName, String indexName,
2362
        final Observer observer) throws DataException {
2363

    
2364
        return createIndex(indexTypeName, featureType, attributeName,
2365
            indexName, true, observer);
2366
    }
2367

    
2368
    private FeatureIndex createIndex(String indexTypeName,
2369
        FeatureType featureType, String attributeName, String indexName,
2370
        boolean background, final Observer observer) throws DataException {
2371

    
2372
        checkNotInAppendMode();
2373
        FeatureIndexProviderServices index;
2374
        index = dataManager.createFeatureIndexProvider(indexTypeName, this,
2375
                featureType, indexName,
2376
                featureType.getAttributeDescriptor(attributeName));
2377

    
2378
        try {
2379
            index.fill(background, observer);
2380
        } catch (FeatureIndexException e) {
2381
            throw new InitializeException(index.getName(), e);
2382
        }
2383

    
2384
        ((DefaultFeatureIndexes) getIndexes()).addIndex(index);
2385
        return index;
2386
    }
2387

    
2388
    //
2389
    // ====================================================================
2390
    // Transforms related methods
2391
    //
2392

    
2393
    @Override
2394
    public FeatureStoreTransforms getTransforms() {
2395
        return this.transforms;
2396
    }
2397

    
2398
    @Override
2399
    public FeatureQuery createFeatureQuery() {
2400
        return new DefaultFeatureQuery();
2401
    }
2402

    
2403
    @Override
2404
    public DataQuery createQuery() {
2405
        return createFeatureQuery();
2406
    }
2407

    
2408
    //
2409
    // ====================================================================
2410
    // UndoRedo related methods
2411
    //
2412

    
2413
    @Override
2414
    public boolean canRedo() {
2415
        return commands.canRedo();
2416
    }
2417

    
2418
    @Override
2419
    public boolean canUndo() {
2420
        return commands.canUndo();
2421
    }
2422

    
2423
    @Override
2424
    public void redo(int num) throws RedoException {
2425
        for (int i = 0; i < num; i++) {
2426
            redo();
2427
        }
2428
    }
2429

    
2430
    @Override
2431
    public void undo(int num) throws UndoException {
2432
        for (int i = 0; i < num; i++) {
2433
            undo();
2434
        }
2435
    }
2436

    
2437
    //
2438
    // ====================================================================
2439
    // Metadata related methods
2440
    //
2441

    
2442
    @Override
2443
    public Object getMetadataID() {
2444
        return this.provider.getSourceId();
2445
    }
2446

    
2447
    @Override
2448
    public void delegate(DynObject dynObject) {
2449
        this.metadata.delegate(dynObject);
2450
    }
2451

    
2452
    @Override
2453
    public DynClass getDynClass() {
2454
        return this.metadata.getDynClass();
2455
    }
2456

    
2457
    @Override
2458
        public Object getDynValue(String name) throws DynFieldNotFoundException {
2459
                if( this.transforms.hasDynValue(name) ) {
2460
                        return this.transforms.getDynValue(name);
2461
                }
2462
                if (this.metadata.hasDynValue(name)) {
2463
                        return this.metadata.getDynValue(name);
2464
                }
2465
                if (METADATA_PROVIDER.equalsIgnoreCase(name)) {
2466
                        return this.provider.getProviderName();
2467
                } else if (METADATA_CONTAINERNAME.equalsIgnoreCase(name)) {
2468
                        return this.provider.getSourceId();
2469
                } else if (METADATA_FEATURETYPE.equalsIgnoreCase(name)) {
2470
                        try {
2471
                                return this.getDefaultFeatureType();
2472
                        } catch (DataException e) {
2473
                                return null;
2474
                        }
2475
                }
2476
                return this.metadata.getDynValue(name);
2477
        }
2478

    
2479
    @Override
2480
    public boolean hasDynValue(String name) {
2481
                if( this.transforms.hasDynValue(name) ) {
2482
                        return true;
2483
                }
2484
        return this.metadata.hasDynValue(name);
2485
    }
2486

    
2487
    @Override
2488
    public boolean hasDynMethod(String name) {
2489
        return ((DynObject_v2)this.metadata).hasDynMethod(name);
2490
    }
2491

    
2492
    @Override
2493
    public void implement(DynClass dynClass) {
2494
        this.metadata.implement(dynClass);
2495
    }
2496

    
2497
    @Override
2498
    public Object invokeDynMethod(String name, Object[] args)
2499
        throws DynMethodException {
2500
        return this.metadata.invokeDynMethod(this, name, args);
2501
    }
2502

    
2503
    @Override
2504
    public Object invokeDynMethod(int code, Object[] args)
2505
        throws DynMethodException {
2506
        return this.metadata.invokeDynMethod(this, code, args);
2507
    }
2508

    
2509
    @Override
2510
    public void setDynValue(String name, Object value)
2511
        throws DynFieldNotFoundException {
2512
                if( this.transforms.hasDynValue(name) ) {
2513
                        this.transforms.setDynValue(name, value);
2514
                        return;
2515
                }
2516
        this.metadata.setDynValue(name, value);
2517

    
2518
    }
2519

    
2520
    /*
2521
     * (non-Javadoc)
2522
     *
2523
     * @see org.gvsig.metadata.Metadata#getMetadataChildren()
2524
     */
2525
    @Override
2526
    public Set getMetadataChildren() {
2527
        return this.metadataChildren;
2528
    }
2529

    
2530
    /*
2531
     * (non-Javadoc)
2532
     *
2533
     * @see org.gvsig.metadata.Metadata#getMetadataName()
2534
     */
2535
    @Override
2536
    public String getMetadataName() {
2537
        return this.provider.getProviderName();
2538
    }
2539

    
2540
    public FeatureTypeManager getFeatureTypeManager() {
2541
        return this.featureTypeManager;
2542
    }
2543

    
2544
    @Override
2545
    public long getFeatureCount() throws DataException {
2546
        if (featureCount == null) {
2547
            featureCount = this.provider.getFeatureCount();
2548
        }
2549
        if (this.isEditing()) {
2550
            if(this.isAppending()) {
2551
                try{
2552
                    throw new IllegalStateException();
2553
                } catch(IllegalStateException e) {
2554
                    LOG.info("Call DefaultFeatureStore.getFeatureCount editing in mode APPEND",e);
2555
                }
2556
                return -1;
2557
            } else {
2558
                return featureCount
2559
                    + this.featureManager.getDeltaSize();
2560
            }
2561
        }
2562
        return featureCount;
2563
    }
2564

    
2565
    private Long getTemporalOID() {
2566
        return this.temporalOid++;
2567
    }
2568

    
2569
    @Override
2570
    public FeatureType getProviderFeatureType(String featureTypeId) {
2571
        if (featureTypeId == null) {
2572
            return this.defaultFeatureType;
2573
        }
2574
        FeatureType type;
2575
        Iterator iter = this.featureTypes.iterator();
2576
        while (iter.hasNext()) {
2577
            type = (FeatureType) iter.next();
2578
            if (type.getId().equals(featureTypeId)) {
2579
                return type;
2580
            }
2581
        }
2582
        return null;
2583
    }
2584

    
2585
    @Override
2586
    public FeatureProvider getFeatureProviderFromFeature(Feature feature) {
2587
        return ((DefaultFeature) feature).getData();
2588
    }
2589

    
2590
    @Override
2591
    public DataStore getStore() {
2592
        return this;
2593
    }
2594

    
2595
    @Override
2596
    public FeatureStore getFeatureStore() {
2597
        return this;
2598
    }
2599

    
2600
    @Override
2601
    public void createCache(String name, DynObject parameters)
2602
        throws DataException {
2603
        cache = dataManager.createFeatureCacheProvider(name, parameters);
2604
        if (cache == null) {
2605
            throw new CreateException("FeaureCacheProvider", null);
2606
        }
2607
        cache.apply(this, provider);
2608
        provider = cache;
2609

    
2610
        featureCount = null;
2611
    }
2612

    
2613
    @Override
2614
    public FeatureCache getCache() {
2615
        return cache;
2616
    }
2617

    
2618
    @Override
2619
    public void clear() {
2620
        if (metadata != null) {
2621
            metadata.clear();
2622
        }
2623
    }
2624

    
2625
    @Override
2626
    public String getName() {
2627
        if( this.provider != null ) {
2628
            return this.provider.getName();
2629
        }
2630
        if( this.parameters instanceof HasAFile ) {
2631
            return FilenameUtils.getName(((HasAFile)this.parameters).getFile().getName());
2632
        }
2633
        return "unknow";
2634
    }
2635

    
2636
    @Override
2637
    public String getFullName() {
2638
        try {
2639
            if( this.provider!=null ) {
2640
                return this.provider.getFullName();
2641
            }
2642
            if( this.parameters instanceof HasAFile ) {
2643
                return (((HasAFile)this.parameters).getFile().getAbsolutePath());
2644
            }
2645
            return null;
2646
        } catch(Throwable th) {
2647
            return null;
2648
        }
2649
    }
2650

    
2651
    @Override
2652
    public String getProviderName() {
2653
        if( this.provider!=null ) {
2654
            return this.provider.getProviderName();
2655
        }
2656
        if( this.parameters != null ) {
2657
            return this.parameters.getDataStoreName();
2658
        }
2659
        return null;
2660

    
2661
    }
2662

    
2663
    @Override
2664
    public boolean isKnownEnvelope() {
2665
        return this.provider.isKnownEnvelope();
2666
    }
2667

    
2668
    @Override
2669
    public boolean hasRetrievedFeaturesLimit() {
2670
        return this.provider.hasRetrievedFeaturesLimit();
2671
    }
2672

    
2673
    @Override
2674
    public int getRetrievedFeaturesLimit() {
2675
        return this.provider.getRetrievedFeaturesLimit();
2676
    }
2677

    
2678
    @Override
2679
    public Interval getInterval() {
2680
        if( this.timeSupport!=null ) {
2681
            return this.timeSupport.getInterval();
2682
        }
2683
        return this.provider.getInterval();
2684
    }
2685

    
2686
    @Override
2687
    public Collection getTimes() {
2688
        if( this.timeSupport!=null ) {
2689
            return this.timeSupport.getTimes();
2690
        }
2691
        return this.provider.getTimes();
2692
    }
2693

    
2694
    @Override
2695
    public Collection getTimes(Interval interval) {
2696
        if( this.timeSupport!=null ) {
2697
            return this.timeSupport.getTimes(interval);
2698
        }
2699
        return this.provider.getTimes(interval);
2700
    }
2701

    
2702
    public void setTimeSupport(FeatureStoreTimeSupport timeSupport) {
2703
        if( this.isEditing() ) {
2704
            throw new RuntimeException("Can't add time support over attribute '"+timeSupport.getAttributeName()+"' while store is editing.");
2705
        }
2706
        if( !this.transforms.isEmpty() ) {
2707
            throw new RuntimeException("Can't add time support over attribute '"+timeSupport.getAttributeName()+"' if has transforms.");
2708
        }
2709
        FeatureType ft = this.defaultFeatureType;
2710
        FeatureAttributeDescriptor attr = ft.getAttributeDescriptor(timeSupport.getRequiredFieldNames()[0]);
2711
        if( attr == null ) {
2712
            throw new RuntimeException("Can't add time support over attribute '"+timeSupport.getAttributeName()+"', this attribute don't exists.");
2713
        }
2714
        EditableFeatureType eft = ft.getEditable();
2715
        attr = eft.getAttributeDescriptor(timeSupport.getAttributeName());
2716
        if( attr != null ) {
2717
            if( !(attr.getFeatureAttributeEmulator() instanceof FeatureStoreTimeSupport) ) {
2718
                throw new RuntimeException("Can't add time support, attribute '"+timeSupport.getAttributeName()+"'already exists.");
2719
            }
2720
            eft.remove(attr.getName());
2721
        }
2722
        EditableFeatureAttributeDescriptor attrTime = eft.add(
2723
            timeSupport.getAttributeName(), 
2724
            timeSupport.getDataType()
2725
        );
2726
        attrTime.setIsTime(true);
2727
        attrTime.setFeatureAttributeEmulator(timeSupport);
2728
        eft.setDefaultTimeAttributeName(timeSupport.getAttributeName());
2729
        this.defaultFeatureType = eft.getNotEditableCopy();
2730
        
2731
        this.timeSupport = timeSupport;
2732
    }
2733

    
2734
    @Override
2735
    @SuppressWarnings("CloneDoesntCallSuperClone")
2736
    public Object clone() throws CloneNotSupportedException {
2737

    
2738
        DataStoreParameters dsp = getParameters();
2739

    
2740
        DefaultFeatureStore cloned_store = null;
2741

    
2742
        try {
2743
            cloned_store = (DefaultFeatureStore) DALLocator.getDataManager().
2744
                openStore(this.getProviderName(), dsp);
2745
            if (transforms != null) {
2746
                cloned_store.transforms = (DefaultFeatureStoreTransforms) transforms.clone();
2747
                cloned_store.transforms.setStoreForClone(cloned_store);
2748
            }
2749
        } catch (Exception e) {
2750
            throw new CloneException(e);
2751
        }
2752
        return cloned_store;
2753

    
2754
    }
2755

    
2756
    @Override
2757
    public Feature getFeature(DynObject dynobject) {
2758
        if (dynobject instanceof DynObjectFeatureFacade){
2759
            Feature f = ((DynObjectFeatureFacade)dynobject).getFeature();
2760
            return f;
2761
        }
2762
        return null;
2763
    }
2764

    
2765
    @Override
2766
    public Iterator iterator() {
2767
        try {
2768
            return this.getFeatureSet().fastIterator();
2769
        } catch (DataException ex) {
2770
            throw new RuntimeException(ex);
2771
        }
2772
    }
2773

    
2774
    @Override
2775
    public ExpressionBuilder createExpressionBuilder() {
2776
        if( this.provider instanceof FeatureStoreProvider_v2 ) {
2777
            return ((FeatureStoreProvider_v2)this.provider).createExpression();
2778
        }
2779
        return new SQLBuilderBase();
2780
    }
2781

    
2782
    @Override
2783
    public ExpressionBuilder createExpression() {
2784
        return createExpressionBuilder();
2785
    }
2786

    
2787
    public FeatureSet features() throws DataException {
2788
        // This is to avoid jython to create a property with this name
2789
        // to access method getFeatures.
2790
        return this.getFeatureSet();
2791
    }
2792

    
2793
    @Override
2794
    public DataStoreProviderFactory getProviderFactory() {
2795
        DataStoreProviderFactory factory = dataManager.getStoreProviderFactory(parameters.getDataStoreName());
2796
        return factory;
2797
    }
2798

    
2799
    @Override
2800
    public void useCache(String providerName, DynObject parameters) throws DataException {
2801
        throw new UnsupportedOperationException();
2802
    }
2803

    
2804
    @Override
2805
    public boolean isBroken() {
2806
        return this.state.isBroken();
2807
    }
2808

    
2809
    @Override
2810
    public Throwable getBreakingsCause() {
2811
            return this.state.getBreakingsCause();
2812
    }
2813

    
2814
    @Override
2815
    public SpatialIndex wrapSpatialIndex(SpatialIndex index) {
2816
      FeatureStoreProviderFactory factory = (FeatureStoreProviderFactory) this.getProviderFactory();
2817
      if( !factory.supportNumericOID() ) {
2818
          return null;
2819
      }
2820
      SpatialIndex wrappedIndex = new WrappedSpatialIndex(index, this);
2821
      return wrappedIndex;
2822
  }
2823

    
2824
    @Override
2825
    public FeatureReference getFeatureReference(String code) {
2826
        FeatureReference featureReference = new DefaultFeatureReference(this, code);
2827
        return featureReference;
2828
    }
2829
}