Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeatureStore.java @ 38249

History | View | Annotate | Download (68 KB)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
175
    private DefaultDataManager dataManager = null;
176

    
177
    private FeatureStoreProvider provider = null;
178

    
179
    private DefaultFeatureIndexes indexes;
180

    
181
    private DefaultFeatureStoreTransforms transforms;
182

    
183
    DelegatedDynObject metadata;
184
    
185
    private Set metadataChildren;
186

    
187
    private Long featureCount = null;
188

    
189
    private long temporalOid = 0;
190

    
191
    private FeatureCacheProvider cache;
192

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

    
202
    public DefaultFeatureStore() {
203

    
204
    }
205

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

    
209
        DynObjectManager dynManager = ToolsLocator.getDynObjectManager();
210

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

    
215
        this.dataManager = (DefaultDataManager) dataManager;
216

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

    
225
    }
226

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
344
    public Envelope getEnvelope() throws DataException {
345
        if (this.mode == MODE_FULLEDIT) {
346
            return this.spatialManager.getEnvelope();
347
        }
348
        if (hasDynValue(DataStore.METADATA_ENVELOPE)){
349
            return (Envelope)getDynValue(DataStore.METADATA_ENVELOPE);
350
        }
351
        return this.provider.getEnvelope();
352
    }
353

    
354
    /**
355
     * @deprecated use getDefaultFeatureType().getDefaultSRS()
356
     */
357
    public IProjection getSRSDefaultGeometry() throws DataException {
358
        return this.getDefaultFeatureType().getDefaultSRS();
359
    }
360

    
361
    public FeatureSelection createDefaultFeatureSelection()
362
        throws DataException {
363
        return new DefaultFeatureSelection(this);
364
    }
365

    
366
    public FeatureProvider createDefaultFeatureProvider(FeatureType type)
367
        throws DataException {
368
        if (type.hasOID()) {
369
            return new DefaultFeatureProvider(type,
370
                this.provider.createNewOID());
371
        }
372
        return new DefaultFeatureProvider(type);
373
    }
374

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

    
409
        }
410

    
411
        if (evaluatedAttr.isEmpty()) {
412
            evaluatedAttr = null;
413
        }
414

    
415
        state.set("evaluatedAttributes", evaluatedAttr);
416
        state.set("defaultFeatureTypeId", defaultFeatureType.getId());
417

    
418
    }
419

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

    
429
        DataStoreParameters params =
430
            (DataStoreParameters) state.get("parameters");
431

    
432
        try {
433

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

    
473
                }
474

    
475
            }
476

    
477
            String defFTypeid = state.getString("defaultFeatureTypeId");
478
            FeatureType ftype = null;
479
            if (!this.defaultFeatureType.getId().equals(
480
                state.getString("defaultFeatureTypeId"))) {
481

    
482
                ftype = getFeatureType(defFTypeid);
483
                if (ftype == null) {
484
                    throw new PersistenceCantFindDefaultFeatureTypeException(
485
                        this.getName(), defFTypeid);
486
                }
487
                this.defaultFeatureType = ftype;
488
            }
489

    
490
        } catch (InitializeException e) {
491
            throw new PersistenceException(e);
492
        } catch (DataException e) {
493
            throw new PersistenceException(e);
494
        }
495

    
496
    }
497

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

    
508
            definition.addDynFieldObject("parameters")
509
                .setClassOfValue(DynObject.class).setMandatory(true)
510
                .setPersistent(true);
511

    
512
            definition.addDynFieldObject("selection")
513
                .setClassOfValue(FeatureSelection.class).setMandatory(false)
514
                .setPersistent(true);
515

    
516
            definition.addDynFieldObject("transforms")
517
                .setClassOfValue(DefaultFeatureStoreTransforms.class)
518
                .setMandatory(true).setPersistent(true);
519

    
520
            definition.addDynFieldMap("evaluatedAttributes")
521
                .setClassOfItems(List.class) // List<DefaultFeatureAttributeDescriptor>
522
                .setMandatory(false).setPersistent(true);
523

    
524
            definition.addDynFieldString("defaultFeatureTypeId")
525
                .setMandatory(true).setPersistent(true);
526
        }
527
    }
528

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

    
539
    //
540
    // ====================================================================
541
    // Gestion de la seleccion
542
    //
543

    
544
    public void setSelection(DataSet selection) throws DataException {
545
        this.setSelection((FeatureSet) selection);
546
    }
547

    
548
    public DataSet createSelection() throws DataException {
549
        return createFeatureSelection();
550
    }
551

    
552
    public DataSet getSelection() throws DataException {
553
        return this.getFeatureSelection();
554
    }
555

    
556
    public void setSelection(FeatureSet selection) throws DataException {
557
        setSelection(selection, true);
558
    }
559

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

    
572
        } else {
573
            if (selection.equals(this.selection)) {
574
                return;
575
            }
576
            if (!selection.isFromStore(this)) {
577
                throw new SelectionNotAllowedException(getName());
578
            }
579
        }
580

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

    
619
        this.notifyChange(DataStoreNotification.SELECTION_CHANGE);
620
    }
621

    
622
    public FeatureSelection createFeatureSelection() throws DataException {
623
        return this.provider.createFeatureSelection();
624
    }
625

    
626
    public FeatureSelection getFeatureSelection() throws DataException {
627
        if (selection == null) {
628
            this.selection = createFeatureSelection();
629
            this.selection.addObserver(this);
630
        }
631
        return selection;
632
    }
633

    
634
    //
635
    // ====================================================================
636
    // Gestion de notificaciones
637
    //
638

    
639
    public void notifyChange(String notification) {
640
        if (delegateObservable != null) {
641
            notifyChange(new DefaultFeatureStoreNotification(this, notification));
642
        }
643

    
644
    }
645

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

    
655
    public void notifyChange(String notification, Feature feature) {
656
        notifyChange(new DefaultFeatureStoreNotification(this, notification,
657
            feature));
658
    }
659

    
660
    public void notifyChange(String notification, Command command) {
661
        notifyChange(new DefaultFeatureStoreNotification(this, notification,
662
            command));
663
    }
664

    
665
    public void notifyChange(String notification, EditableFeatureType type) {
666
        notifyChange(new DefaultFeatureStoreNotification(this, notification,
667
            type));
668
    }
669

    
670
    public void notifyChange(FeatureStoreNotification storeNotification) {
671
        delegateObservable.notifyObservers(storeNotification);
672
    }
673

    
674
    public void notifyChange(String notification, Resource resource) {
675
        notifyChange(new DefaultFeatureStoreNotification(this,
676
            DataStoreNotification.RESOURCE_CHANGED));
677
    }
678

    
679
    //
680
    // ====================================================================
681
    // Gestion de bloqueos
682
    //
683

    
684
    public boolean isLocksSupported() {
685
        return this.provider.isLocksSupported();
686
    }
687

    
688
    public FeatureLocks getLocks() throws DataException {
689
        if (!this.provider.isLocksSupported()) {
690
            LOG.warn("Locks not supporteds");
691
            return null;
692
        }
693
        if (locks == null) {
694
            this.locks = this.provider.createFeatureLocks();
695
        }
696
        return locks;
697
    }
698

    
699
    //
700
    // ====================================================================
701
    // Interface Observable
702
    //
703

    
704
    public void disableNotifications() {
705
        this.delegateObservable.disableNotifications();
706

    
707
    }
708

    
709
    public void enableNotifications() {
710
        this.delegateObservable.enableNotifications();
711
    }
712

    
713
    public void beginComplexNotification() {
714
        this.delegateObservable.beginComplexNotification();
715

    
716
    }
717

    
718
    public void endComplexNotification() {
719
        this.delegateObservable.endComplexNotification();
720

    
721
    }
722

    
723
    public void addObserver(Observer observer) {
724
        if (delegateObservable != null) {
725
            this.delegateObservable.addObserver(observer);
726
        }
727
    }
728

    
729
    public void deleteObserver(Observer observer) {
730
        if (delegateObservable != null) {
731
            this.delegateObservable.deleteObserver(observer);
732
        }
733
    }
734

    
735
    public void deleteObservers() {
736
        this.delegateObservable.deleteObservers();
737

    
738
    }
739

    
740
    //
741
    // ====================================================================
742
    // Interface Observer
743
    //
744
    // Usado para observar:
745
    // - su seleccion
746
    // - sus bloqueos
747
    // - sus recursos
748
    //
749

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

    
759
        } else
760
            if (observable instanceof FeatureStoreProvider) {
761
                if (observable == this.provider) {
762

    
763
                }
764

    
765
            }
766
    }
767

    
768
    //
769
    // ====================================================================
770
    // Edicion
771
    //
772

    
773
    private void newVersionOfUpdate() {
774
        this.versionOfUpdate++;
775
    }
776

    
777
    private long currentVersionOfUpdate() {
778
        return this.versionOfUpdate;
779
    }
780

    
781
    private void checkInEditingMode() throws NeedEditingModeException {
782
        if (mode != MODE_FULLEDIT) {
783
            throw new NeedEditingModeException(this.getName());
784
        }
785
    }
786

    
787
    private void checkNotInAppendMode() throws IllegalStateException {
788
        if (mode == MODE_APPEND) {
789
                        throw new IllegalStateException("Error: store "
790
                                        + this.getFullName() + " is in append mode");
791
        }
792
    }
793

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

    
803
    private void exitEditingMode() {
804
        if (commands != null) {
805
            commands.clear();
806
            commands = null;
807
        }
808

    
809
        if (featureTypeManager != null) {
810
            featureTypeManager.dispose();
811
            featureTypeManager = null;
812

    
813
        }
814

    
815
        // TODO implementar un dispose para estos dos
816
        featureManager = null;
817
        spatialManager = null;
818

    
819
        featureCount = null;
820

    
821
        mode = MODE_QUERY;
822
        hasStrongChanges = true; // Lo deja a true por si las moscas
823
        hasInserts = true;
824
    }
825

    
826
    synchronized public void edit() throws DataException {
827
        edit(MODE_FULLEDIT);
828
    }
829

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

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

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

    
881
    private void invalidateIndexes() {
882
        setIndexesValidStatus(false);
883
    }
884

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

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

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

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

    
938
    public boolean isEditing() {
939
        return mode == MODE_FULLEDIT;
940
    }
941

    
942
    public boolean isAppending() {
943
        return mode == MODE_APPEND;
944
    }
945

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

    
957
            FeatureType oldt = type.getSource().getCopy();
958
            FeatureType newt = type.getNotEditableCopy();
959
            commands.update(newt, oldt);
960

    
961
            if (((DefaultEditableFeatureType) type).hasStrongChanges()) {
962
                hasStrongChanges = true;
963
            }
964
            notifyChange(FeatureStoreNotification.AFTER_UPDATE_TYPE, type);
965
        } catch (Exception e) {
966
            throw new StoreUpdateFeatureTypeException(e, this.getName());
967
        }
968
    }
969

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

    
995
    private static EditableFeature lastChangedFeature = null;
996

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

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

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

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

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

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

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

    
1121
    public List getRedoInfos() {
1122
        if (isEditing() && (commands != null)) {
1123
            return commands.getRedoInfos();
1124
        } else {
1125
            return null;
1126
        }
1127
    }
1128

    
1129
    public List getUndoInfos() {
1130
        if (isEditing() && (commands != null)) {
1131
            return commands.getUndoInfos();
1132
        } else {
1133
            return null;
1134
        }
1135
    }
1136

    
1137
    public synchronized FeatureCommandsStack getCommandsStack()
1138
        throws DataException {
1139
        checkInEditingMode();
1140
        return commands;
1141
    }
1142

    
1143
    synchronized public void cancelEditing() throws DataException {
1144
        spatialManager.cancelModifies();
1145
        try {
1146
            checkInEditingMode();
1147

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

    
1164
    synchronized public void finishEditing() throws DataException {
1165
        LOG.debug("finish editing of mode: {}", new Integer(mode));
1166
        try {
1167
            switch (mode) {
1168
            case MODE_QUERY:
1169
                throw new NeedEditingModeException(this.getName());
1170

    
1171
            case MODE_APPEND:
1172
                notifyChange(FeatureStoreNotification.BEFORE_FINISHEDITING);
1173
                provider.endAppend();
1174
                exitEditingMode();
1175
                updateIndexes();
1176
                notifyChange(FeatureStoreNotification.AFTER_FINISHEDITING);
1177
                break;
1178

    
1179
            case MODE_FULLEDIT:
1180
                if (hasStrongChanges && !this.allowWrite()) {
1181
                    throw new WriteNotAllowedException(getName());
1182
                }
1183
                notifyChange(FeatureStoreNotification.BEFORE_FINISHEDITING);
1184
                if (hasStrongChanges) {
1185
                    validateFeatures(Feature.FINISH_EDITING);
1186
                    provider.performChanges(featureManager.getDeleted(),
1187
                        featureManager.getInserted(),
1188
                        featureManager.getUpdated(),
1189
                        featureTypeManager.getFeatureTypesChanged());
1190
                }
1191
                exitEditingMode();
1192
                updateIndexes();
1193
                notifyChange(FeatureStoreNotification.AFTER_FINISHEDITING);
1194
                break;
1195
            }
1196
        } catch (Exception e) {
1197
            throw new FinishEditingException(e);
1198
        }
1199
    }
1200

    
1201
    public void beginEditingGroup(String description)
1202
        throws NeedEditingModeException {
1203
        checkInEditingMode();
1204
        commands.startComplex(description);
1205
    }
1206

    
1207
    public void endEditingGroup() throws NeedEditingModeException {
1208
        checkInEditingMode();
1209
        commands.endComplex();
1210
    }
1211

    
1212
    public boolean isAppendModeSupported() {
1213
        return this.provider.supportsAppendMode();
1214
    }
1215

    
1216
    public void export(DataServerExplorer explorer, String provider,
1217
        NewFeatureStoreParameters params) throws DataException {
1218

    
1219
        if (this.getFeatureTypes().size() != 1) {
1220
            throw new NotYetImplemented(
1221
                "export whith more than one type not yet implemented");
1222
        }
1223
        FeatureSelection featureSelection = (FeatureSelection) getSelection();
1224
        FeatureStore target = null;
1225
        FeatureSet features = null;
1226
        DisposableIterator iterator = null;
1227
        try {
1228
            FeatureType type = this.getDefaultFeatureType();
1229
            if ((params.getDefaultFeatureType() == null)
1230
                || (params.getDefaultFeatureType().size() == 0)) {
1231
                params.setDefaultFeatureType(type.getEditable());
1232

    
1233
            }
1234
            explorer.add(provider, params, true);
1235

    
1236
            DataManager manager = DALLocator.getDataManager();
1237
            target = (FeatureStore) manager.openStore(provider, params);
1238
            FeatureType targetType = target.getDefaultFeatureType();
1239

    
1240
            target.edit(MODE_APPEND);
1241
            FeatureAttributeDescriptor[] pk = type.getPrimaryKey();
1242
            if (featureSelection.getSize() > 0) {
1243
                features = this.getFeatureSelection();
1244
            } else {
1245
                if ((pk != null) && (pk.length > 0)) {
1246
                    FeatureQuery query = createFeatureQuery();
1247
                    for (int i = 0; i < pk.length; i++) {
1248
                        query.getOrder().add(pk[i].getName(), true);
1249
                    }
1250
                    features = this.getFeatureSet(query);
1251
                } else {
1252
                    features = this.getFeatureSet();
1253
                }
1254
            }
1255
            iterator = features.fastIterator();
1256
            while (iterator.hasNext()) {
1257
                DefaultFeature feature = (DefaultFeature) iterator.next();
1258
                target.insert(target.createNewFeature(targetType, feature));
1259
            }
1260
            target.finishEditing();
1261
            target.dispose();
1262
        } catch (Exception e) {
1263
            throw new DataExportException(e, params.toString());
1264
        } finally {
1265
            dispose(iterator);
1266
            dispose(features);
1267
            dispose(target);
1268
        }
1269
    }
1270

    
1271
    //
1272
    // ====================================================================
1273
    // Obtencion de datos
1274
    // getDataCollection, getFeatureCollection
1275
    //
1276

    
1277
    public DataSet getDataSet() throws DataException {
1278
        checkNotInAppendMode();
1279
        FeatureQuery query =
1280
            new DefaultFeatureQuery(this.getDefaultFeatureType());
1281
        return new DefaultFeatureSet(this, query);
1282
    }
1283

    
1284
    public DataSet getDataSet(DataQuery dataQuery) throws DataException {
1285
        checkNotInAppendMode();
1286
        return new DefaultFeatureSet(this, (FeatureQuery) dataQuery);
1287
    }
1288

    
1289
    public void getDataSet(Observer observer) throws DataException {
1290
        checkNotInAppendMode();
1291
        this.getFeatureSet(null, observer);
1292
    }
1293

    
1294
    public void getDataSet(DataQuery dataQuery, Observer observer)
1295
        throws DataException {
1296
        checkNotInAppendMode();
1297
        this.getFeatureSet((FeatureQuery) dataQuery, observer);
1298
    }
1299

    
1300
    public FeatureSet getFeatureSet() throws DataException {
1301
        checkNotInAppendMode();
1302
        FeatureQuery query =
1303
            new DefaultFeatureQuery(this.getDefaultFeatureType());
1304
        return new DefaultFeatureSet(this, query);
1305
    }
1306

    
1307
    public FeatureSet getFeatureSet(FeatureQuery featureQuery)
1308
        throws DataException {
1309
        checkNotInAppendMode();
1310
        return new DefaultFeatureSet(this, featureQuery);
1311
    }
1312

    
1313
    public void accept(Visitor visitor) throws BaseException {
1314
        FeatureSet set = getFeatureSet();
1315
        try {
1316
            set.accept(visitor);
1317
        } finally {
1318
            set.dispose();
1319
        }
1320
    }
1321

    
1322
    public void accept(Visitor visitor, DataQuery dataQuery)
1323
        throws BaseException {
1324
        FeatureSet set = getFeatureSet((FeatureQuery) dataQuery);
1325
        try {
1326
            set.accept(visitor);
1327
        } finally {
1328
            set.dispose();
1329
        }
1330
    }
1331

    
1332
    public FeatureType getFeatureType(FeatureQuery featureQuery)
1333
        throws DataException {
1334
        DefaultFeatureType fType =
1335
            (DefaultFeatureType) this.getFeatureType(featureQuery
1336
                .getFeatureTypeId());
1337
        if ((featureQuery.getAttributeNames() != null)
1338
            && (featureQuery.getAttributeNames().length > 0)) {
1339
            return fType.getSubtype(featureQuery.getAttributeNames());
1340
        }
1341
        return fType;
1342
    }
1343

    
1344
    public void getFeatureSet(Observer observer) throws DataException {
1345
        checkNotInAppendMode();
1346
        this.getFeatureSet(null, observer);
1347
    }
1348

    
1349
    public void getFeatureSet(FeatureQuery query, Observer observer)
1350
        throws DataException {
1351
        class LoadInBackGround implements Runnable {
1352

    
1353
            private FeatureStore store;
1354
            private FeatureQuery query;
1355
            private Observer observer;
1356

    
1357
            public LoadInBackGround(FeatureStore store, FeatureQuery query,
1358
                Observer observer) {
1359
                this.store = store;
1360
                this.query = query;
1361
                this.observer = observer;
1362
            }
1363

    
1364
            void notify(FeatureStoreNotification theNotification) {
1365
                observer.update(store, theNotification);
1366
                return;
1367
            }
1368

    
1369
            public void run() {
1370
                FeatureSet set = null;
1371
                try {
1372
                    set = store.getFeatureSet(query);
1373
                    notify(new DefaultFeatureStoreNotification(store,
1374
                        FeatureStoreNotification.LOAD_FINISHED, set));
1375
                } catch (Exception e) {
1376
                    notify(new DefaultFeatureStoreNotification(store,
1377
                        FeatureStoreNotification.LOAD_FINISHED, e));
1378
                } finally {
1379
                    dispose(set);
1380
                }
1381
            }
1382
        }
1383

    
1384
        checkNotInAppendMode();
1385
        if (query == null) {
1386
            query = new DefaultFeatureQuery(this.getDefaultFeatureType());
1387
        }
1388
        LoadInBackGround task = new LoadInBackGround(this, query, observer);
1389
        Thread thread = new Thread(task, "Load Feature Set in background");
1390
        thread.start();
1391
    }
1392

    
1393
    public Feature getFeatureByReference(FeatureReference reference)
1394
        throws DataException {
1395
        checkNotInAppendMode();
1396
        DefaultFeatureReference ref = (DefaultFeatureReference) reference;
1397
        FeatureType featureType;
1398
        if (ref.getFeatureTypeId() == null) {
1399
            featureType = this.getDefaultFeatureType();
1400
        } else {
1401
            featureType = this.getFeatureType(ref.getFeatureTypeId());
1402
        }
1403
        return this.getFeatureByReference(reference, featureType);
1404
    }
1405

    
1406
    public Feature getFeatureByReference(FeatureReference reference,
1407
        FeatureType featureType) throws DataException {
1408
        checkNotInAppendMode();
1409
        featureType = fixFeatureType((DefaultFeatureType) featureType);
1410
        if (!this.transforms.isEmpty()) {
1411

    
1412
            featureType = this.transforms.getSourceFeatureTypeFrom(featureType);
1413

    
1414
        }
1415
        // TODO comprobar que el id es de este store
1416

    
1417
        if (this.mode == MODE_FULLEDIT) {
1418
            Feature f = featureManager.get(reference, this, featureType);
1419
            if (f != null) {
1420
                return f;
1421
            }
1422
        }
1423
    
1424
        
1425
        DefaultFeature feature =
1426
            new DefaultFeature(this,
1427
                this.provider.getFeatureProviderByReference(
1428
                    (FeatureReferenceProviderServices) reference, featureType));
1429

    
1430
        if (!this.transforms.isEmpty()) {
1431
            return this.transforms.applyTransform(feature, featureType);
1432
        }
1433
        return feature;
1434
    }
1435

    
1436
    //
1437
    // ====================================================================
1438
    // Gestion de features
1439
    //
1440

    
1441
    private FeatureType fixFeatureType(DefaultFeatureType type)
1442
        throws DataException {
1443
        FeatureType original = this.getDefaultFeatureType();
1444

    
1445
        if ((type == null) || type.equals(original)) {
1446
            return original;
1447
        } else {
1448
            if (!type.isSubtypeOf(original)) {
1449
                Iterator iter = this.getFeatureTypes().iterator();
1450
                FeatureType tmpType;
1451
                boolean found = false;
1452
                while (iter.hasNext()) {
1453
                    tmpType = (FeatureType) iter.next();
1454
                    if (type.equals(tmpType)) {
1455
                        return type;
1456

    
1457
                    } else
1458
                        if (type.isSubtypeOf(tmpType)) {
1459
                            found = true;
1460
                            original = tmpType;
1461
                            break;
1462
                        }
1463

    
1464
                }
1465
                if (!found) {
1466
                    throw new IllegalFeatureTypeException(getName());
1467
                }
1468
            }
1469
        }
1470

    
1471
        // Checks that type has all fields of pk
1472
        // else add the missing attributes at the end.
1473
        if (!original.hasOID()) {
1474
            // Gets original pk attributes
1475
            DefaultEditableFeatureType edOriginal =
1476
                (DefaultEditableFeatureType) original.getEditable();
1477
            FeatureAttributeDescriptor orgAttr;
1478
            Iterator edOriginalIter = edOriginal.iterator();
1479
            while (edOriginalIter.hasNext()) {
1480
                orgAttr = (FeatureAttributeDescriptor) edOriginalIter.next();
1481
                if (!orgAttr.isPrimaryKey()) {
1482
                    edOriginalIter.remove();
1483
                }
1484
            }
1485

    
1486
            // Checks if all pk attributes are in type
1487
            Iterator typeIterator;
1488
            edOriginalIter = edOriginal.iterator();
1489
            FeatureAttributeDescriptor attr;
1490
            while (edOriginalIter.hasNext()) {
1491
                orgAttr = (FeatureAttributeDescriptor) edOriginalIter.next();
1492
                typeIterator = type.iterator();
1493
                while (typeIterator.hasNext()) {
1494
                    attr = (FeatureAttributeDescriptor) typeIterator.next();
1495
                    if (attr.getName().equals(orgAttr.getName())) {
1496
                        edOriginalIter.remove();
1497
                        break;
1498
                    }
1499
                }
1500
            }
1501

    
1502
            // add missing pk attributes if any
1503
            if (edOriginal.size() > 0) {
1504
                boolean isEditable = type instanceof DefaultEditableFeatureType;
1505
                DefaultEditableFeatureType edType =
1506
                    (DefaultEditableFeatureType) original.getEditable();
1507
                edType.clear();
1508
                edType.addAll(type);
1509
                edType.addAll(edOriginal);
1510
                if (!isEditable) {
1511
                    type = (DefaultFeatureType) edType.getNotEditableCopy();
1512
                }
1513
            }
1514

    
1515
        }
1516

    
1517
        return type;
1518
    }
1519

    
1520
    public void validateFeatures(int mode) throws DataException {
1521
        FeatureSet collection = null;
1522
        DisposableIterator iter = null;
1523
        try {
1524
            checkNotInAppendMode();
1525
            collection = this.getFeatureSet();
1526
            iter = collection.fastIterator();
1527
            long previousVersionOfUpdate = currentVersionOfUpdate();
1528
            while (iter.hasNext()) {
1529
                ((DefaultFeature) iter.next()).validate(mode);
1530
                if (previousVersionOfUpdate != currentVersionOfUpdate()) {
1531
                    throw new ConcurrentDataModificationException(getName());
1532
                }
1533
            }
1534
        } catch (Exception e) {
1535
            throw new ValidateFeaturesException(e, getName());
1536
        } finally {
1537
            dispose(iter);
1538
            dispose(collection);
1539
        }
1540
    }
1541

    
1542
    public FeatureType getDefaultFeatureType() throws DataException {
1543
        try {
1544
            if (isEditing()) {
1545
                FeatureType auxFeatureType =
1546
                    featureTypeManager.getType(defaultFeatureType.getId());
1547
                if (auxFeatureType != null) {
1548
                    return auxFeatureType;
1549
                }
1550
            }
1551
            FeatureType type = this.transforms.getDefaultFeatureType();
1552
            if (type != null) {
1553
                return type;
1554
            }
1555
            return defaultFeatureType;
1556
        } catch (Exception e) {
1557
            throw new GetFeatureTypeException(e, getName());
1558
        }
1559
    }
1560

    
1561
    public FeatureType getFeatureType(String featureTypeId)
1562
        throws DataException {
1563
        if (featureTypeId == null) {
1564
            return this.getDefaultFeatureType();
1565
        }
1566
        try {
1567
            if (isEditing()) {
1568
                FeatureType auxFeatureType =
1569
                    featureTypeManager.getType(featureTypeId);
1570
                if (auxFeatureType != null) {
1571
                    return auxFeatureType;
1572
                }
1573
            }
1574
            FeatureType type = this.transforms.getFeatureType(featureTypeId);
1575
            if (type != null) {
1576
                return type;
1577
            }
1578
            Iterator iter = this.featureTypes.iterator();
1579
            while (iter.hasNext()) {
1580
                type = (FeatureType) iter.next();
1581
                if (type.getId().equals(featureTypeId)) {
1582
                    return type;
1583
                }
1584
            }
1585
            return null;
1586
        } catch (Exception e) {
1587
            throw new GetFeatureTypeException(e, getName());
1588
        }
1589
    }
1590

    
1591
    public FeatureType getProviderDefaultFeatureType() {
1592
        return defaultFeatureType;
1593
    }
1594

    
1595
    public List getFeatureTypes() throws DataException {
1596
        try {
1597
            List types;
1598
            if (isEditing()) {
1599
                types = new ArrayList();
1600
                Iterator it = featureTypes.iterator();
1601
                while (it.hasNext()) {
1602
                    FeatureType type = (FeatureType) it.next();
1603
                    FeatureType typeaux =
1604
                        featureTypeManager.getType(type.getId());
1605
                    if (typeaux != null) {
1606
                        types.add(typeaux);
1607
                    } else {
1608
                        types.add(type);
1609
                    }
1610
                }
1611
                it = featureTypeManager.newsIterator();
1612
                while (it.hasNext()) {
1613
                    FeatureType type = (FeatureType) it.next();
1614
                    types.add(type);
1615
                }
1616
            } else {
1617
                types = this.transforms.getFeatureTypes();
1618
                if (types == null) {
1619
                    types = featureTypes;
1620
                }
1621
            }
1622
            return Collections.unmodifiableList(types);
1623
        } catch (Exception e) {
1624
            throw new GetFeatureTypeException(e, getName());
1625
        }
1626
    }
1627

    
1628
    public List getProviderFeatureTypes() throws DataException {
1629
        return Collections.unmodifiableList(this.featureTypes);
1630
    }
1631

    
1632
    public Feature createFeature(FeatureProvider data) throws DataException {
1633
        DefaultFeature feature = new DefaultFeature(this, data);
1634
        return feature;
1635
    }
1636

    
1637
    public Feature createFeature(FeatureProvider data, FeatureType type)
1638
        throws DataException {
1639
        // FIXME: falta por implementar
1640
        // Comprobar si es un subtipo del feature de data
1641
        // y construir un feature usando el subtipo.
1642
        // Probablemente requiera generar una copia del data.
1643
        throw new NotYetImplemented();
1644
    }
1645

    
1646
    public EditableFeature createNewFeature(FeatureType type,
1647
        Feature defaultValues) throws DataException {
1648
        try {
1649
            FeatureProvider data = createNewFeatureProvider(type);
1650
            DefaultEditableFeature feature =
1651
                new DefaultEditableFeature(this, data);
1652
            feature.initializeValues(defaultValues);
1653
            data.setNew(true);
1654
            
1655
            return feature;
1656
        } catch (Exception e) {
1657
            throw new CreateFeatureException(e, getName());
1658
        }
1659
    }
1660

    
1661
    private FeatureProvider createNewFeatureProvider(FeatureType type)
1662
        throws DataException {
1663
        type = this.fixFeatureType((DefaultFeatureType) type);
1664
        FeatureProvider data = this.provider.createFeatureProvider(type);
1665
        data.setNew(true);
1666
        if (type.hasOID() && (data.getOID() == null)) {
1667
            data.setOID(this.provider.createNewOID());
1668
        } else {
1669
            data.setOID(this.getTemporalOID());
1670
        }
1671
        return data;
1672

    
1673
    }
1674

    
1675
    public EditableFeature createNewFeature(FeatureType type,
1676
        boolean defaultValues) throws DataException {
1677
        try {
1678
            FeatureProvider data = createNewFeatureProvider(type);
1679
            DefaultEditableFeature feature =
1680
                new DefaultEditableFeature(this, data);
1681
            if (defaultValues) {
1682
                feature.initializeValues();
1683
            }
1684
            return feature;
1685
        } catch (Exception e) {
1686
            throw new CreateFeatureException(e, getName());
1687
        }
1688
    }
1689

    
1690
    public EditableFeature createNewFeature(boolean defaultValues)
1691
        throws DataException {
1692
        return this.createNewFeature(this.getDefaultFeatureType(),
1693
            defaultValues);
1694
    }
1695

    
1696
    public EditableFeature createNewFeature() throws DataException {
1697
        return this.createNewFeature(this.getDefaultFeatureType(), true);
1698
    }
1699

    
1700
    public EditableFeatureType createFeatureType() {
1701
        DefaultEditableFeatureType ftype = new DefaultEditableFeatureType();
1702
        return ftype;
1703
    }
1704

    
1705
    public EditableFeatureType createFeatureType(String id) {
1706
        DefaultEditableFeatureType ftype = new DefaultEditableFeatureType(id);
1707
        return ftype;
1708
    }
1709

    
1710
    //
1711
    // ====================================================================
1712
    // Index related methods
1713
    //
1714

    
1715
    public FeatureIndexes getIndexes() {
1716
        return this.indexes;
1717
    }
1718

    
1719
    public FeatureIndex createIndex(FeatureType featureType,
1720
        String attributeName, String indexName) throws DataException {
1721
        return createIndex(null, featureType, attributeName, indexName);
1722
    }
1723

    
1724
    public FeatureIndex createIndex(String indexTypeName,
1725
        FeatureType featureType, String attributeName, String indexName)
1726
        throws DataException {
1727

    
1728
        return createIndex(indexTypeName, featureType, attributeName,
1729
            indexName, false, null);
1730
    }
1731

    
1732
    public FeatureIndex createIndex(FeatureType featureType,
1733
        String attributeName, String indexName, Observer observer)
1734
        throws DataException {
1735
        return createIndex(null, featureType, attributeName, indexName,
1736
            observer);
1737
    }
1738

    
1739
    public FeatureIndex createIndex(String indexTypeName,
1740
        FeatureType featureType, String attributeName, String indexName,
1741
        final Observer observer) throws DataException {
1742

    
1743
        return createIndex(indexTypeName, featureType, attributeName,
1744
            indexName, true, observer);
1745
    }
1746

    
1747
    private FeatureIndex createIndex(String indexTypeName,
1748
        FeatureType featureType, String attributeName, String indexName,
1749
        boolean background, final Observer observer) throws DataException {
1750

    
1751
        checkNotInAppendMode();
1752
        FeatureIndexProviderServices index = null;
1753
        index =
1754
            dataManager.createFeatureIndexProvider(indexTypeName, this,
1755
                featureType, indexName,
1756
                featureType.getAttributeDescriptor(attributeName));
1757

    
1758
        try {
1759
            index.fill(background, observer);
1760
        } catch (FeatureIndexException e) {
1761
            throw new InitializeException(index.getName(), e);
1762
        }
1763

    
1764
        ((DefaultFeatureIndexes) getIndexes()).addIndex(index);
1765
        return index;
1766
    }
1767

    
1768
    //
1769
    // ====================================================================
1770
    // Transforms related methods
1771
    //
1772

    
1773
    public FeatureStoreTransforms getTransforms() {
1774
        return this.transforms;
1775
    }
1776

    
1777
    public FeatureQuery createFeatureQuery() {
1778
        return new DefaultFeatureQuery();
1779
    }
1780

    
1781
    public DataQuery createQuery() {
1782
        return createFeatureQuery();
1783
    }
1784

    
1785
    //
1786
    // ====================================================================
1787
    // UndoRedo related methods
1788
    //
1789

    
1790
    public boolean canRedo() {
1791
        return commands.canRedo();
1792
    }
1793

    
1794
    public boolean canUndo() {
1795
        return commands.canUndo();
1796
    }
1797

    
1798
    public void redo(int num) throws RedoException {
1799
        for (int i = 0; i < num; i++) {
1800
            redo();
1801
        }
1802
    }
1803

    
1804
    public void undo(int num) throws UndoException {
1805
        for (int i = 0; i < num; i++) {
1806
            undo();
1807
        }
1808
    }
1809

    
1810
    //
1811
    // ====================================================================
1812
    // Metadata related methods
1813
    //
1814

    
1815
    public Object getMetadataID() {
1816
        return this.provider.getSourceId();
1817
    }
1818

    
1819
    public void delegate(DynObject dynObject) {
1820
        this.metadata.delegate(dynObject);
1821
    }
1822

    
1823
    public DynClass getDynClass() {
1824
        return this.metadata.getDynClass();
1825
    }
1826

    
1827
        public Object getDynValue(String name) throws DynFieldNotFoundException {
1828
                if( this.transforms.hasDynValue(name) ) {
1829
                        return this.transforms.getDynValue(name);
1830
                }
1831
                if (this.metadata.hasDynValue(name)) {
1832
                        return this.metadata.getDynValue(name);
1833
                }
1834
                if (METADATA_PROVIDER.equalsIgnoreCase(name)) {
1835
                        return this.provider.getProviderName();
1836
                } else if (METADATA_CONTAINERNAME.equalsIgnoreCase(name)) {
1837
                        return this.provider.getSourceId();
1838
                } else if (METADATA_FEATURETYPE.equalsIgnoreCase(name)) {
1839
                        try {
1840
                                return this.getDefaultFeatureType();
1841
                        } catch (DataException e) {
1842
                                return null;
1843
                        }
1844
                }
1845
                return this.metadata.getDynValue(name);
1846
        }
1847

    
1848
    public boolean hasDynValue(String name) {
1849
                if( this.transforms.hasDynValue(name) ) {
1850
                        return true;
1851
                }
1852
        return this.metadata.hasDynValue(name);
1853
    }
1854

    
1855
    public void implement(DynClass dynClass) {
1856
        this.metadata.implement(dynClass);
1857
    }
1858

    
1859
    public Object invokeDynMethod(String name, DynObject context)
1860
        throws DynMethodException {
1861
        return this.metadata.invokeDynMethod(this, name, context);
1862
    }
1863

    
1864
    public Object invokeDynMethod(int code, DynObject context)
1865
        throws DynMethodException {
1866
        return this.metadata.invokeDynMethod(this, code, context);
1867
    }
1868

    
1869
    public void setDynValue(String name, Object value)
1870
        throws DynFieldNotFoundException {
1871
                if( this.transforms.hasDynValue(name) ) {
1872
                        this.transforms.setDynValue(name, value);
1873
                        return;
1874
                }
1875
        this.metadata.setDynValue(name, value);
1876

    
1877
    }
1878

    
1879
    /*
1880
     * (non-Javadoc)
1881
     * 
1882
     * @see org.gvsig.metadata.Metadata#getMetadataChildren()
1883
     */
1884
    public Set getMetadataChildren() {
1885
        return this.metadataChildren;
1886
    }
1887

    
1888
    /*
1889
     * (non-Javadoc)
1890
     * 
1891
     * @see org.gvsig.metadata.Metadata#getMetadataName()
1892
     */
1893
    public String getMetadataName() {
1894
        return this.provider.getProviderName();
1895
    }
1896

    
1897
    public FeatureTypeManager getFeatureTypeManager() {
1898
        return this.featureTypeManager;
1899
    }
1900

    
1901
    public long getFeatureCount() throws DataException {
1902
        if (featureCount == null) {
1903
            featureCount = new Long(this.provider.getFeatureCount());
1904
        }
1905
        if (this.isEditing() && !this.isAppending()) {
1906
            return featureCount.longValue()
1907
                - this.featureManager.getDeltaSize();
1908
        }
1909
        return featureCount.longValue();
1910
    }
1911

    
1912
    private Long getTemporalOID() {
1913
        return new Long(this.temporalOid++);
1914
    }
1915

    
1916
    public FeatureType getProviderFeatureType(String featureTypeId) {
1917
        if (featureTypeId == null) {
1918
            return this.defaultFeatureType;
1919
        }
1920
        FeatureType type;
1921
        Iterator iter = this.featureTypes.iterator();
1922
        while (iter.hasNext()) {
1923
            type = (FeatureType) iter.next();
1924
            if (type.getId().equals(featureTypeId)) {
1925
                return type;
1926
            }
1927
        }
1928
        return null;
1929
    }
1930

    
1931
    public FeatureProvider getFeatureProviderFromFeature(Feature feature) {
1932
        return ((DefaultFeature) feature).getData();
1933
    }
1934

    
1935
    public DataStore getStore() {
1936
        return this;
1937
    }
1938

    
1939
    public FeatureStore getFeatureStore() {
1940
        return this;
1941
    }
1942

    
1943
    public void createCache(String name, DynObject parameters)
1944
        throws DataException {
1945
        cache = dataManager.createFeatureCacheProvider(name, parameters);
1946
        if (cache == null) {
1947
            throw new CreateException("FeaureCacheProvider", null);
1948
        }
1949
        cache.apply(this, provider);
1950
        provider = cache;
1951

    
1952
        featureCount = null;
1953
    }
1954

    
1955
    public FeatureCache getCache() {
1956
        return cache;
1957
    }
1958

    
1959
    public void clear() {
1960
        if (metadata != null) {
1961
            metadata.clear();
1962
        }
1963
    }
1964

    
1965
    public String getName() {
1966
        return this.provider.getName();
1967
    }
1968

    
1969
    public String getFullName() {
1970
        return this.provider.getFullName();
1971
    }
1972

    
1973
    public String getProviderName() {
1974
        return this.provider.getProviderName();
1975
    }
1976

    
1977
    public boolean isKnownEnvelope() {
1978
        return this.provider.isKnownEnvelope();
1979
    }
1980

    
1981
    public boolean hasRetrievedFeaturesLimit() {
1982
        return this.provider.hasRetrievedFeaturesLimit();
1983
    }
1984

    
1985
    public int getRetrievedFeaturesLimit() {
1986
        return this.provider.getRetrievedFeaturesLimit();
1987
    }
1988

    
1989
    public Interval getInterval() {  
1990
        return this.provider.getInterval();
1991
    }
1992

    
1993
    public Collection getTimes() {      
1994
        return this.provider.getTimes();
1995
    }
1996

    
1997
    public Collection getTimes(Interval interval) {       
1998
        return this.provider.getTimes(interval);
1999
    }
2000
}