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

History | View | Annotate | Download (141 KB)

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

    
26
import java.util.ArrayList;
27
import java.util.Collection;
28
import java.util.Collections;
29
import java.util.HashMap;
30
import java.util.HashSet;
31
import java.util.Iterator;
32
import java.util.List;
33
import java.util.Map;
34
import java.util.Map.Entry;
35
import java.util.Objects;
36
import java.util.Set;
37
import java.util.UUID;
38
import javax.json.JsonObject;
39
import org.apache.commons.io.FilenameUtils;
40
import org.apache.commons.io.IOUtils;
41
import org.apache.commons.lang3.StringUtils;
42
import org.apache.commons.lang3.mutable.MutableObject;
43
import org.cresques.cts.IProjection;
44
import org.gvsig.expressionevaluator.Expression;
45
import org.gvsig.expressionevaluator.ExpressionBuilder;
46
import org.gvsig.expressionevaluator.ExpressionUtils;
47
import org.gvsig.expressionevaluator.GeometryExpressionUtils;
48
import org.gvsig.fmap.dal.BaseStoresRepository;
49
import org.gvsig.fmap.dal.DALLocator;
50
import org.gvsig.fmap.dal.DataManager;
51
import static org.gvsig.fmap.dal.DataManager.DAL_STORE_ENVELOPE;
52
import static org.gvsig.fmap.dal.DataManager.DAL_USE_LARGE_SELECTION;
53
import org.gvsig.fmap.dal.DataQuery;
54
import org.gvsig.fmap.dal.DataServerExplorer;
55
import org.gvsig.fmap.dal.DataSet;
56
import org.gvsig.fmap.dal.DataStore;
57
import org.gvsig.fmap.dal.DataStoreNotification;
58
import org.gvsig.fmap.dal.DataStoreParameters;
59
import org.gvsig.fmap.dal.DataStoreProviderFactory;
60
import org.gvsig.fmap.dal.DataTransaction;
61
import org.gvsig.fmap.dal.DataTypes;
62
import org.gvsig.fmap.dal.StoresRepository;
63
import org.gvsig.fmap.dal.SupportTransactions;
64
import org.gvsig.fmap.dal.exception.CloneException;
65
import org.gvsig.fmap.dal.exception.CloseException;
66
import org.gvsig.fmap.dal.exception.CreateException;
67
import org.gvsig.fmap.dal.exception.DataException;
68
import org.gvsig.fmap.dal.exception.DataRuntimeException;
69
import org.gvsig.fmap.dal.exception.InitializeException;
70
import org.gvsig.fmap.dal.exception.OpenException;
71
import org.gvsig.fmap.dal.exception.ReadException;
72
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
73
import org.gvsig.fmap.dal.exception.WriteException;
74
import org.gvsig.fmap.dal.feature.EditableFeature;
75
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
76
import org.gvsig.fmap.dal.feature.EditableFeatureType;
77
import org.gvsig.fmap.dal.feature.Feature;
78
import static org.gvsig.fmap.dal.feature.Feature.CHECK_BASIC;
79
import static org.gvsig.fmap.dal.feature.Feature.CHECK_REQUIREDS;
80
import static org.gvsig.fmap.dal.feature.Feature.CHECK_RULES_AT_EDITING;
81
import static org.gvsig.fmap.dal.feature.Feature.CHECK_RULES_AT_FINISH;
82
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
83
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
84
import org.gvsig.fmap.dal.feature.FeatureCache;
85
import org.gvsig.fmap.dal.feature.FeatureIndex;
86
import org.gvsig.fmap.dal.feature.FeatureIndexes;
87
import org.gvsig.fmap.dal.feature.FeatureLocks;
88
import org.gvsig.fmap.dal.feature.FeatureQuery;
89
import org.gvsig.fmap.dal.feature.FeatureReference;
90
import org.gvsig.fmap.dal.feature.FeatureReferenceSelection;
91
import org.gvsig.fmap.dal.feature.FeatureSelection;
92
import org.gvsig.fmap.dal.feature.FeatureSet;
93
import org.gvsig.fmap.dal.feature.FeatureSet.DisposableFeatureSetIterable;
94
import org.gvsig.fmap.dal.feature.FeatureStore;
95
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
96
import org.gvsig.fmap.dal.feature.FeatureStoreProviderFactory;
97
import org.gvsig.fmap.dal.feature.FeatureStoreTimeSupport;
98
import org.gvsig.fmap.dal.feature.FeatureStoreTransform;
99
import org.gvsig.fmap.dal.feature.FeatureStoreTransforms;
100
import org.gvsig.fmap.dal.feature.FeatureType;
101
import org.gvsig.fmap.dal.feature.FeatureType.FeatureTypeChanged;
102
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
103
import org.gvsig.fmap.dal.feature.exception.AlreadyEditingException;
104
import org.gvsig.fmap.dal.feature.exception.CreateFeatureException;
105
import org.gvsig.fmap.dal.feature.exception.DataExportException;
106
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException;
107
import org.gvsig.fmap.dal.feature.exception.FinishEditingException;
108
import org.gvsig.fmap.dal.feature.exception.GetFeatureTypeException;
109
import org.gvsig.fmap.dal.feature.exception.IllegalFeatureException;
110
import org.gvsig.fmap.dal.feature.exception.IllegalFeatureTypeException;
111
import org.gvsig.fmap.dal.feature.exception.NeedEditingModeException;
112
import org.gvsig.fmap.dal.feature.exception.NoNewFeatureInsertException;
113
import org.gvsig.fmap.dal.feature.exception.NullFeatureTypeException;
114
import org.gvsig.fmap.dal.feature.exception.PerformEditingException;
115
import org.gvsig.fmap.dal.feature.exception.PersistenceCantFindFeatureTypeException;
116
import org.gvsig.fmap.dal.feature.exception.PersistenceStoreAlreadyLoadedException;
117
import org.gvsig.fmap.dal.feature.exception.SelectionNotAllowedException;
118
import org.gvsig.fmap.dal.feature.exception.StoreCancelEditingException;
119
import org.gvsig.fmap.dal.feature.exception.StoreDeleteEditableFeatureException;
120
import org.gvsig.fmap.dal.feature.exception.StoreDeleteFeatureException;
121
import org.gvsig.fmap.dal.feature.exception.StoreEditException;
122
import org.gvsig.fmap.dal.feature.exception.StoreInsertFeatureException;
123
import org.gvsig.fmap.dal.feature.exception.StoreUpdateFeatureException;
124
import org.gvsig.fmap.dal.feature.exception.StoreUpdateFeatureTypeException;
125
import org.gvsig.fmap.dal.feature.exception.ValidateFeaturesException;
126
import org.gvsig.fmap.dal.feature.exception.WriteNotAllowedException;
127
import org.gvsig.fmap.dal.feature.impl.dynobjectutils.DynObjectFeatureFacade;
128
import org.gvsig.fmap.dal.feature.impl.editing.memory.FeatureManager;
129
import org.gvsig.fmap.dal.feature.impl.editing.memory.FeatureTypeManager;
130
import org.gvsig.fmap.dal.feature.impl.editing.memory.SpatialManager;
131
import org.gvsig.fmap.dal.feature.impl.featurereference.FeatureReferenceFactory;
132
import org.gvsig.fmap.dal.feature.impl.featurereference.FeatureReferenceIteratorToFeatureIterator;
133
import org.gvsig.fmap.dal.feature.impl.featureset.DefaultFeatureSet;
134
import org.gvsig.fmap.dal.feature.impl.undo.DefaultFeatureCommandsStack;
135
import org.gvsig.fmap.dal.feature.impl.undo.FeatureCommandsStack;
136
import org.gvsig.fmap.dal.feature.paging.FeaturePagingHelper;
137
import org.gvsig.fmap.dal.feature.spi.DefaultFeatureProvider;
138
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
139
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
140
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
141
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices;
142
import org.gvsig.fmap.dal.feature.spi.cache.FeatureCacheProvider;
143
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProviderServices;
144
import org.gvsig.fmap.dal.impl.DefaultDataManager;
145
import org.gvsig.fmap.dal.resource.Resource;
146
import org.gvsig.fmap.dal.spi.AbstractDataStore;
147
import org.gvsig.fmap.dal.spi.DataStoreInitializer2;
148
import org.gvsig.fmap.dal.spi.DataStoreProvider;
149
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
150
import org.gvsig.fmap.dal.spi.DataTransactionServices;
151
import org.gvsig.fmap.geom.Geometry;
152
import org.gvsig.fmap.geom.GeometryUtils;
153
import org.gvsig.fmap.geom.SpatialIndex;
154
import org.gvsig.fmap.geom.primitive.Envelope;
155
import org.gvsig.metadata.MetadataLocator;
156
import org.gvsig.metadata.MetadataManager;
157
import org.gvsig.metadata.exceptions.MetadataException;
158
import org.gvsig.timesupport.Interval;
159
import org.gvsig.tools.ToolsLocator;
160
import org.gvsig.tools.dispose.DisposableIterator;
161
import org.gvsig.tools.dispose.DisposeUtils;
162
import org.gvsig.tools.dynobject.DelegatedDynObject;
163
import org.gvsig.tools.dynobject.DynClass;
164
import org.gvsig.tools.dynobject.DynObject;
165
import org.gvsig.tools.dynobject.DynObjectManager;
166
import org.gvsig.tools.dynobject.DynObject_v2;
167
import org.gvsig.tools.dynobject.DynStruct;
168
import org.gvsig.tools.dynobject.Tags;
169
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
170
import org.gvsig.tools.dynobject.exception.DynMethodException;
171
import org.gvsig.tools.exception.BaseException;
172
import org.gvsig.tools.exception.NotYetImplemented;
173
import org.gvsig.tools.identitymanagement.SimpleIdentityManager;
174
import org.gvsig.tools.observer.BaseNotification;
175
import org.gvsig.tools.observer.Observable;
176
import org.gvsig.tools.observer.Observer;
177
import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable;
178
import org.gvsig.tools.persistence.PersistenceManager;
179
import org.gvsig.tools.persistence.Persistent;
180
import org.gvsig.tools.persistence.PersistentState;
181
import org.gvsig.tools.persistence.exception.PersistenceException;
182
import org.gvsig.tools.resourcesstorage.ResourcesStorage;
183
import org.gvsig.tools.undo.RedoException;
184
import org.gvsig.tools.undo.UndoException;
185
import org.gvsig.tools.undo.command.Command;
186
import org.gvsig.tools.util.CachedValue;
187
import org.gvsig.tools.util.ChainedIterator;
188
import org.gvsig.tools.util.GetItemWithSizeIsEmptyAndIterator64;
189
import org.gvsig.tools.util.HasAFile;
190
import org.gvsig.tools.util.PropertiesSupportHelper;
191
import org.gvsig.tools.util.UnmodifiableBasicMap;
192
import org.gvsig.tools.visitor.VisitCanceledException;
193
import org.gvsig.tools.visitor.Visitor;
194

    
195
@SuppressWarnings("UseSpecificCatch")
196
public class DefaultFeatureStore extends AbstractDataStore implements
197
        DataStoreInitializer2, FeatureStoreProviderServices, FeatureStore,
198
        SupportTransactions, Observer {
199

    
200
    public static long sample_feature_cache_timeout_ms = 15000;
201
    
202
    private static final String PERSISTENCE_DEFINITION_NAME = "FeatureStore";
203

    
204
    private DataStoreParameters parameters = null;
205
    private FeatureSelection selection;
206
    private FeatureLocks locks;
207

    
208
    private DelegateWeakReferencingObservable delegateObservable
209
            = new DelegateWeakReferencingObservable(this);
210

    
211
    private FeatureCommandsStack commands;
212

    
213
    /*
214
    TODO: Sustituir estos tres manager por un EditingManager
215
     */
216
    private FeatureTypeManager featureTypeManager;
217
    private FeatureManager featureManager;
218
    private SpatialManager spatialManager;
219

    
220
    private FeatureType defaultFeatureType = null;
221
    private List<FeatureType> featureTypes = new ArrayList<>();
222

    
223
    private int mode = MODE_QUERY;
224
    private int lastMode = MODE_QUERY;
225
    
226
    private long versionOfUpdate = 0;
227
    private boolean hasStrongChanges = true;
228

    
229
    private DefaultDataManager dataManager = null;
230

    
231
    private FeatureStoreProvider provider = null;
232

    
233
    private DefaultFeatureIndexes indexes;
234

    
235
    private DefaultFeatureStoreTransforms transforms;
236

    
237
    /*friend*/ DelegatedDynObject metadata;
238

    
239
    private Set metadataChildren;
240

    
241
    private Long featureCount = null;
242

    
243
    private long temporalOid = 0;
244

    
245
    private FeatureCacheProvider cache;
246

    
247
    private final StateInformation state;
248

    
249
    private FeatureStoreTimeSupport timeSupport;
250

    
251
    private PropertiesSupportHelper propertiesSupportHelper;
252
    private DataTransaction transaction;
253

    
254
    private String editingSessionCode;
255
    private String lastEditingSessionCode;
256
    
257
    private CachedValue<Feature> sampleFeatureCache;
258
    
259
    private final Observer transactionObserver;
260

    
261
    private class StateInformation extends HashMap<Object, Object> {
262

    
263
        private static final long serialVersionUID = 4109026189635185666L;
264

    
265
        private boolean broken;
266
        private Throwable breakingsCause;
267

    
268
        @SuppressWarnings("OverridableMethodCallInConstructor")
269
        public StateInformation() {
270
            this.clear();
271
        }
272

    
273
        @Override
274
        public void clear() {
275
            this.broken = false;
276
            this.breakingsCause = null;
277
            super.clear();
278
        }
279

    
280
        public boolean isBroken() {
281
            return this.broken;
282
        }
283

    
284
        public void broken() {
285
            this.broken = true;
286
        }
287

    
288
        public Throwable getBreakingsCause() {
289
            return this.breakingsCause;
290
        }
291

    
292
        public void setBreakingsCause(Throwable cause) {
293
            if (this.breakingsCause == null) {
294
                this.breakingsCause = cause;
295
            }
296
            this.broken = true;
297
        }
298
    }
299

    
300
    /*
301
     * TODO:
302
     *
303
     * - Comprobar que solo se pueden a?adir reglas de validacion sobre un
304
     * EditableFeatureType. - Comprobar que solo se puede hacer un update con un
305
     * featureType al que se le han cambiado las reglas de validacion cuando
306
     * hasStrongChanges=false.
307
     */
308
    public DefaultFeatureStore() {
309
        this.state = new StateInformation();
310
        this.sampleFeatureCache = null;
311
        this.transactionObserver = new Observer() {
312
            @Override
313
            public void update(Observable o, Object o1) {
314
                if( o1 instanceof BaseNotification && (
315
                        ((BaseNotification)o1).isOfType("ROLLBACK") ||
316
                        ((BaseNotification)o1).isOfType("COMMIT")
317
                        ) ) {
318
                    featureCount = null;
319
                }
320
            }
321
        };
322
    }
323

    
324
    @Override
325
    protected DataManager getDataManager() {
326
        return this.dataManager;
327
    }
328

    
329
    @Override
330
    public void intialize(DataManager dataManager,
331
            DataStoreParameters parameters) throws InitializeException {
332

    
333
        DynObjectManager dynManager = ToolsLocator.getDynObjectManager();
334

    
335
        this.metadata = (DelegatedDynObject) dynManager.createDynObject(
336
                FeatureStore.METADATA_DEFINITION_NAME,
337
                MetadataManager.METADATA_NAMESPACE
338
        );
339

    
340
        this.dataManager = (DefaultDataManager) dataManager;
341

    
342
        this.parameters = parameters;
343
        this.transforms = new DefaultFeatureStoreTransforms(this);
344
        try {
345
            indexes = new DefaultFeatureIndexes(this);
346
        } catch (DataException e) {
347
            throw new InitializeException(e);
348
        }
349

    
350
    }
351

    
352
    @Override
353
    public void setProvider(org.gvsig.fmap.dal.DataStoreProvider provider) {
354
        this.provider = (FeatureStoreProvider) provider;
355
        this.delegate((DynObject) provider);
356
        this.metadataChildren = new HashSet();
357
        this.metadataChildren.add(provider);
358
        if (!this.ignoreDALResource) {
359
            loadDALFile();
360

    
361
            // Habria que crear un metodo en el proveedor para que de prioidad
362
            // a los parametros frente a lo que se a leido en el fichero dal
363
            // DataStoreProvider arreglalo segun los parametros del usuario
364
            // fixFeatureTypeFromParameters
365
            this.provider.fixFeatureTypeFromParameters();
366
            try {
367
                if (defaultFeatureType != null) {
368
                    FeatureAttributeDescriptor attrGeom = defaultFeatureType.getDefaultGeometryAttribute();
369
                    if (attrGeom != null) {
370
                        DefaultFeatureAttributeDescriptor gattr = (DefaultFeatureAttributeDescriptor) attrGeom;
371
                        IProjection srs = (IProjection) this.getDynValue(METADATA_CRS);
372
                        if (srs != null && srs != gattr.getSRS()) {
373
                            gattr.setSRSForced(srs);
374
                        }
375
                    }
376
                }
377
            } catch (Throwable th) {
378
                LOGGER.warn("Can't patch DAL file", th);
379
            }
380
        }
381
    }
382

    
383
    @Override
384
    public DataStoreParameters getParameters() {
385
        if (this.parameters == null) {
386
            LOGGER.warn("Store parametes are null");
387
        }
388
        return parameters;
389
    }
390

    
391
    @Override
392
    public int getMode() {
393
        return this.mode;
394
    }
395

    
396
    @Override
397
    public DataManager getManager() {
398
        return this.dataManager;
399
    }
400

    
401
    @Override
402
    public UnmodifiableBasicMap<String, DataStore> getChildren() {
403
        UnmodifiableBasicMap<String, DataStore> children = this.provider.getChildren();
404
        if (children == null) {
405
            return UnmodifiableBasicMap.EMPTY_UNMODIFIABLEBASICMAP;
406
        }
407
        return children;
408
    }
409

    
410
    @Override
411
    public FeatureStoreProvider getProvider() {
412
        return this.provider;
413
    }
414

    
415
    public FeatureManager getFeatureManager() {
416
        return this.featureManager;
417
    }
418

    
419
    @Override
420
    public void setFeatureTypes(List types, FeatureType defaultType) {
421
        this.featureTypes = types;
422
        this.defaultFeatureType = defaultType;
423
    }
424

    
425
    public void open() throws OpenException {
426
        if (this.mode != MODE_QUERY) {
427
            // TODO: Se puede hacer un open estando en edicion ?
428
            try {
429
                throw new IllegalStateException();
430
            } catch (Exception ex) {
431
                LOGGER.warn("Opening a store in editing/append mode (" + this.getFullName() + ").", ex);
432
            }
433
        }
434
        if (this.notifyChange(DataStoreNotification.BEFORE_OPEN).isCanceled()) {
435
            return;
436
        }
437
        this.provider.open();
438
        this.notifyChange(DataStoreNotification.AFTER_OPEN);
439
    }
440

    
441
    @Override
442
    public void refresh() throws OpenException, InitializeException {
443
        if (this.mode != MODE_QUERY) {
444
            throw new IllegalStateException();
445
        }
446
        if (this.notifyChange(FeatureStoreNotification.BEFORE_REFRESH).isCanceled()) {
447
            return;
448
        }
449
        if (state.isBroken()) {
450
            this.load(state);
451
        } else {
452
            this.featureCount = null;
453
            this.provider.refresh();
454
        }
455
        this.resourcesStorage = null;
456
        loadDALFile();
457
        this.notifyChange(FeatureStoreNotification.AFTER_REFRESH);
458
    }
459

    
460
    public void close() throws CloseException {
461
        if (this.mode != MODE_QUERY) {
462
            // TODO: Se puede hacer un close estando en edicion ?
463
            try {
464
                throw new IllegalStateException();
465
            } catch (Exception ex) {
466
                LOGGER.warn("Clossing a store in editing/append mode (" + this.getFullName() + ").", ex);
467
            }
468
        }
469
        if (this.notifyChange(DataStoreNotification.BEFORE_CLOSE).isCanceled()) {
470
            return;
471
        }
472
        this.featureCount = null;
473
        this.provider.close();
474
        this.notifyChange(DataStoreNotification.AFTER_CLOSE);
475
    }
476

    
477
    @Override
478
    protected void doDispose() throws BaseException {
479
        if (this.mode != MODE_QUERY) {
480
            // TODO: Se puede hacer un dispose estando en edicion ?
481
            try {
482
                throw new IllegalStateException();
483
            } catch (Exception ex) {
484
                LOGGER.warn("Dispossing a store in editing/append mode (" + this.getFullName() + ").", ex);
485
            }
486
        }
487
        if (this.notifyChange(DataStoreNotification.BEFORE_DISPOSE).isCanceled()) {
488
            return;
489
        }
490
        this.disposeIndexes();
491
        if (this.provider != null) {
492
            this.provider.dispose();
493
        }
494
        if (this.selection != null) {
495
            this.selection.dispose();
496
            this.selection = null;
497
        }
498
        this.commands = null;
499
        this.featureCount = null;
500
        if (this.locks != null) {
501
            // this.locks.dispose();
502
            this.locks = null;
503
        }
504

    
505
        if (this.featureTypeManager != null) {
506
            this.featureTypeManager.dispose();
507
            this.featureTypeManager = null;
508
        }
509

    
510
        this.featureManager = null;
511
        this.spatialManager = null;
512

    
513
        this.parameters = null;
514
        this.notifyChange(DataStoreNotification.AFTER_DISPOSE);
515
        if (delegateObservable != null) {
516
            this.delegateObservable.deleteObservers();
517
            this.delegateObservable = null;
518
        }
519
        DisposeUtils.disposeQuietly(this.resourcesStorage);
520
        if( this.transaction!=null ) {
521
            this.transaction.deleteObserver(transactionObserver);
522
            this.transaction = null;
523
        }
524
    }
525

    
526
    @Override
527
    public boolean allowWrite() {
528
        SimpleIdentityManager identityManager = ToolsLocator.getIdentityManager();
529
        if (!identityManager.getCurrentIdentity().isAuthorized(DataManager.WRITE_STORE_AUTHORIZATION, this.getParameters(), this.getName())) {
530
            return false;
531
        }
532
        return this.provider.allowWrite();
533
    }
534

    
535
    @Override
536
    public boolean canWriteGeometry(int geometryType) throws DataException {
537
        return this.provider.canWriteGeometry(geometryType, 0);
538
    }
539

    
540
    @Override
541
    public DataServerExplorer getExplorer() throws ReadException,
542
            ValidateDataParametersException {
543
        if (this.state.isBroken()) {
544
            try {
545
                return this.provider.getExplorer();
546
            } catch (Throwable th) {
547
                return null;
548
            }
549
        } else {
550
            return this.provider.getExplorer();
551
        }
552
    }
553

    
554
    /*
555
     * public Metadata getMetadata() throws MetadataNotFoundException {
556
     * // TODO:
557
     * // Si el provider devuelbe null habria que ver de construir aqui
558
     * // los metadatos basicos, como el Envelope y el SRS.
559
     *
560
     * // TODO: Estando en edicion el Envelope deberia de
561
     * // actualizarse usando el spatialManager
562
     * return this.provider.getMetadata();
563
     * }
564
     */
565
    @Override
566
    public Envelope getEnvelope() throws DataException {
567
        if (this.mode == MODE_FULLEDIT) {
568
            // Just in case another thread tries to write in the store
569
            synchronized (this) {
570
                return this.spatialManager.getEnvelope();
571
            }
572
        }
573
        try {
574
            FeatureType featureType = this.getDefaultFeatureTypeQuietly();
575
            if( featureType!=null ) {
576
                Tags tags = featureType.getTags();
577
                if( tags.has(DAL_STORE_ENVELOPE) ) {
578
                    String geom_s = tags.getString(DAL_STORE_ENVELOPE,null);
579
                    if( StringUtils.isNotBlank(geom_s) ) {
580
                        Geometry geom = GeometryUtils.createFrom(geom_s);
581
                        if( geom!=null ) {
582
                            return geom.getEnvelope();
583
                        }
584
                    }
585
                }
586
            }
587
        } catch(Throwable th) {
588
            LOGGER.debug("Can't retrieve envelope from featrure type tag '"+DAL_STORE_ENVELOPE+"'.", th);
589
        }
590
        
591
        if (hasDynValue(DataStore.METADATA_ENVELOPE)) {
592
            return (Envelope) getDynValue(DataStore.METADATA_ENVELOPE);
593
        }
594
        Envelope envelope = this.provider.getEnvelope();
595
        if (envelope != null) {
596
            return envelope;
597
        }
598
        FeatureAttributeDescriptor attrdesc = this.getDefaultFeatureType().getDefaultGeometryAttribute();
599
        if (attrdesc == null || !attrdesc.isComputed()) {
600
            return null;
601
        }
602
        final int index = attrdesc.getIndex();
603
        final MutableObject<Envelope> envelopeValue = new MutableObject<>();
604
        try {
605
            this.accept((Object obj) -> {
606
                Feature f = (Feature) obj;
607
                Geometry g = (Geometry) f.get(index);
608
                if (g == null) {
609
                    return;
610
                }
611
                if (envelopeValue.getValue() == null) {
612
                    envelopeValue.setValue(g.getEnvelope());
613
                } else {
614
                    envelopeValue.getValue().add(g);
615
                }
616
            });
617
        } catch (Throwable th) {
618
            LOGGER.warn("Can't calculate envelope", th);
619
            return null;
620
        }
621
        return envelopeValue.getValue();
622
    }
623

    
624
    /**
625
     * @throws org.gvsig.fmap.dal.exception.DataException
626
     * @deprecated use getDefaultFeatureType().getDefaultSRS()
627
     */
628
    @Override
629
    public IProjection getSRSDefaultGeometry() throws DataException {
630
        return this.getDefaultFeatureType().getDefaultSRS();
631
    }
632

    
633
    @Override
634
    public FeatureSelection createDefaultFeatureSelection()
635
            throws DataException {
636
        return new DefaultFeatureSelection(this);
637
    }
638

    
639
    @Override
640
    public FeatureProvider createDefaultFeatureProvider(FeatureType type)
641
            throws DataException {
642
        if (type.hasOID()) {
643
            return new DefaultFeatureProvider(type,
644
                    this.provider.createNewOID());
645
        }
646
        return new DefaultFeatureProvider(type);
647
    }
648

    
649
    @Override
650
    public void saveToState(PersistentState state) throws PersistenceException {
651
        /*if (this.mode != FeatureStore.MODE_QUERY) {
652
            throw new PersistenceException(new IllegalStateException(
653
                this.getName()));
654
        }*/
655
        state.set("dataStoreName", this.getName());
656
        state.set("parameters", this.parameters);
657
        state.set("selection", this.selection);
658
        state.set("transforms", this.transforms);
659
        // TODO locks persistence
660
        // state.set("locks", this.locks);
661
        // TODO indexes persistence
662
        // state.set("indexes", this.indexes);
663
        Map evaluatedAttr = new HashMap(1);
664
        Iterator iterType = featureTypes.iterator();
665
        Iterator iterAttr;
666
        FeatureType type;
667
        DefaultFeatureAttributeDescriptor attr;
668
        List attrs;
669
        while (iterType.hasNext()) {
670
            type = (FeatureType) iterType.next();
671
            attrs = new ArrayList();
672
            iterAttr = type.iterator();
673
            while (iterAttr.hasNext()) {
674
                attr = (DefaultFeatureAttributeDescriptor) iterAttr.next();
675
                if ((attr.getEvaluator() != null)
676
                        && (attr.getEvaluator() instanceof Persistent)) {
677
                    attrs.add(attr);
678
                }
679
            }
680
            if (!attrs.isEmpty()) {
681
                evaluatedAttr.put(type.getId(), attrs);
682
            }
683

    
684
        }
685

    
686
        if (evaluatedAttr.isEmpty()) {
687
            evaluatedAttr = null;
688
        }
689

    
690
        state.set("evaluatedAttributes", evaluatedAttr);
691
        state.set("defaultFeatureTypeId", defaultFeatureType.getId());
692

    
693
    }
694

    
695
    @Override
696
    public void loadFromState(final PersistentState persistentState)
697
            throws PersistenceException {
698
        if (this.provider != null) {
699
            throw new PersistenceStoreAlreadyLoadedException(this.getName());
700
        }
701
        if (this.getManager() == null) {
702
            this.dataManager = (DefaultDataManager) DALLocator.getDataManager();
703
        }
704
        state.clear();
705
        try {
706
            state.put("parameters", persistentState.get("parameters"));
707
        } catch (Throwable th) {
708
            state.setBreakingsCause(th);
709
        }
710
        try {
711
            state.put("selection", persistentState.get("selection"));
712
        } catch (Throwable th) {
713
            state.setBreakingsCause(th);
714
        }
715
        try {
716
            state.put("transforms", persistentState.get("transforms"));
717
        } catch (Throwable th) {
718
            state.setBreakingsCause(th);
719
        }
720
        try {
721
            state.put("evaluatedAttributes", persistentState.get("evaluatedAttributes"));
722
        } catch (Throwable th) {
723
            state.setBreakingsCause(th);
724
        }
725
        try {
726
            state.put("defaultFeatureTypeId", persistentState.getString("defaultFeatureTypeId"));
727
        } catch (Throwable th) {
728
            state.setBreakingsCause(th);
729
        }
730
        load(state);
731
        ((DefaultDataManager) this.getDataManager()).addObservers(this);
732
    }
733

    
734
    private void load(StateInformation state) {
735
        this.featureTypes = new ArrayList();
736
        this.defaultFeatureType = null;
737
        this.featureCount = null;
738

    
739
        DataStoreParameters params = (DataStoreParameters) state.get("parameters");
740
        try {
741
            intialize(dataManager, params);
742
        } catch (Throwable th) {
743
            state.setBreakingsCause(th);
744
        }
745

    
746
        try {
747
            DataStoreProvider prov = dataManager.createProvider(
748
                    getStoreProviderServices(),
749
                    params
750
            );
751
            setProvider(prov);
752
        } catch (Throwable th) {
753
            LOGGER.warn("Can't load store from state.", th);
754
            state.setBreakingsCause(th);
755
        }
756
        try {
757
            selection = (FeatureSelection) state.get("selection");
758
        } catch (Throwable th) {
759
            state.setBreakingsCause(th);
760
        }
761

    
762
        try {
763
            this.transforms = (DefaultFeatureStoreTransforms) state.get("transforms");
764
            this.transforms.setFeatureStore(this);
765
            for (FeatureStoreTransform transform : this.transforms) {
766
                try {
767
                    transform.setUp();
768
                } catch (Throwable th) {
769
                    state.setBreakingsCause(th);
770
                }
771
            }
772
        } catch (Throwable th) {
773
            state.setBreakingsCause(th);
774
        }
775

    
776
        try {
777
            Map evaluatedAttributes = (Map) state.get("evaluatedAttributes");
778
            if ((evaluatedAttributes != null) && !evaluatedAttributes.isEmpty()) {
779
                Iterator iterEntries = evaluatedAttributes.entrySet().iterator();
780
                while (iterEntries.hasNext()) {
781
                    Entry entry = (Entry) iterEntries.next();
782
                    List attrs = (List) entry.getValue();
783
                    if (attrs.isEmpty()) {
784
                        continue;
785
                    }
786
                    int fTypePos = -1;
787
                    DefaultFeatureType type = null;
788
                    for (int i = 0; i < featureTypes.size(); i++) {
789
                        type = (DefaultFeatureType) featureTypes.get(i);
790
                        if (type.getId().equals(entry.getKey())) {
791
                            fTypePos = i;
792
                            break;
793
                        }
794
                    }
795
                    if (type == null) {
796
                        throw new PersistenceCantFindFeatureTypeException(
797
                                getName(), (String) entry.getKey());
798
                    }
799
                    DefaultEditableFeatureType eType = (DefaultEditableFeatureType) type.getEditable();
800
                    Iterator<FeatureAttributeDescriptor> iterAttr = attrs.iterator();
801
                    while (iterAttr.hasNext()) {
802
                        FeatureAttributeDescriptor attr = iterAttr.next();
803
                        eType.addLike(attr);
804
                    }
805
                    featureTypes.set(fTypePos, eType.getNotEditableCopy());
806

    
807
                }
808

    
809
            }
810
        } catch (Throwable th) {
811
            state.setBreakingsCause(th);
812
        }
813

    
814
        try {
815
            String defaultFeatureTypeId = (String) state.get("defaultFeatureTypeId");
816
            FeatureType ftype;
817

    
818
            if (defaultFeatureType == null
819
                    || defaultFeatureType.getId() == null
820
                    || !defaultFeatureType.getId().equals(defaultFeatureTypeId)) {
821

    
822
                ftype = getFeatureType(defaultFeatureTypeId);
823
                if (ftype == null) {
824
                    /*
825
                             * Un error en el m?todo de PostgreSQL getName(), hace que
826
                             * el nombre del featureType sea valor retornado por el getProviderName()
827
                             * De momento se pone este parche para apa?arlo y poder mantener compatibilidad
828
                             * con proyectos antiguos (2.1 y 2.2)
829
                     */
830
                    ftype = getFeatureType(getName());
831
                    if (ftype == null) {
832
                        throw new RuntimeException("Can't locate feature type");
833
                    }
834
                }
835
                defaultFeatureType = ftype;
836
            }
837
        } catch (Throwable th) {
838
            state.setBreakingsCause(th);
839
        }
840

    
841
        LOGGER.debug("load() broken:{}, {}, {}.",
842
                new Object[]{state.isBroken(), this.getProviderName(), params}
843
        );
844
    }
845

    
846
    public DataStoreProviderServices getStoreProviderServices() {
847
        return this;
848
    }
849

    
850
    public static void selfRegister(List<Exception> exs) {
851
        registerPersistenceDefinition();
852
        try {
853
            registerMetadataDefinition();
854
        } catch (MetadataException e) {
855
            exs.add(e);
856
        }
857
        try {
858
            DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager();
859
            dynObjectManager.registerTag(
860
                    DAL_USE_LARGE_SELECTION,
861
                    "Indicates whether the store should use a memory-based or a disk-based selection."
862
            ).setType(DataTypes.BOOLEAN);
863
            dynObjectManager.registerTag(
864
                    DAL_STORE_ENVELOPE,
865
                    "If specified, this geometry will be used to calculate the store envelope."
866
            ).setType(DataTypes.STRING);
867
        } catch (Exception e) {
868
            exs.add(e);
869
        }
870
    }
871

    
872
    private static void registerPersistenceDefinition() {
873
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
874
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
875
            DynStruct definition
876
                    = manager.addDefinition(DefaultFeatureStore.class,
877
                            PERSISTENCE_DEFINITION_NAME, PERSISTENCE_DEFINITION_NAME
878
                            + " Persistent definition", null, null);
879
            definition.addDynFieldString("dataStoreName").setMandatory(true)
880
                    .setPersistent(true);
881

    
882
            definition.addDynFieldObject("parameters")
883
                    .setClassOfValue(DynObject.class).setMandatory(true)
884
                    .setPersistent(true);
885

    
886
            definition.addDynFieldObject("selection")
887
                    .setClassOfValue(FeatureSelection.class).setMandatory(false)
888
                    .setPersistent(true);
889

    
890
            definition.addDynFieldObject("transforms")
891
                    .setClassOfValue(DefaultFeatureStoreTransforms.class)
892
                    .setMandatory(true).setPersistent(true);
893

    
894
            definition.addDynFieldMap("evaluatedAttributes")
895
                    .setClassOfItems(List.class) // List<DefaultFeatureAttributeDescriptor>
896
                    .setMandatory(false).setPersistent(true);
897

    
898
            definition.addDynFieldString("defaultFeatureTypeId")
899
                    .setMandatory(true).setPersistent(true);
900
        }
901
    }
902

    
903
    private static void registerMetadataDefinition() throws MetadataException {
904
        MetadataManager manager = MetadataLocator.getMetadataManager();
905
        if (manager.getDefinition(FeatureStore.METADATA_DEFINITION_NAME) == null) {
906
            DynStruct metadataDefinition
907
                    = manager.addDefinition(FeatureStore.METADATA_DEFINITION_NAME, null);
908
            metadataDefinition.extend(manager
909
                    .getDefinition(DataStore.METADATA_DEFINITION_NAME));
910
        }
911
    }
912

    
913
    //
914
    // ====================================================================
915
    // Gestion de la seleccion
916
    //
917
    @Override
918
    public void setSelection(DataSet selection) throws DataException {
919
        this.setSelection((FeatureSet) selection);
920
    }
921

    
922
    @Override
923
    public DataSet createSelection() throws DataException {
924
        return createFeatureSelection();
925
    }
926

    
927
    @Override
928
    public DataSet getSelection() throws DataException {
929
        return this.getFeatureSelection();
930
    }
931

    
932
    @Override
933
    public void setSelection(FeatureSet selection) throws DataException {
934
        setSelection(selection, true);
935
    }
936

    
937
    public void setSelection(FeatureSet selection, boolean undoable)
938
            throws DataException {
939
        if (selection == null) {
940
            if (undoable) {
941
                throw new SelectionNotAllowedException(getName());
942
            }
943

    
944
        } else {
945
            if (selection.equals(this.selection)) {
946
                return;
947
            }
948
            if (!selection.isFromStore(this)) {
949
                throw new SelectionNotAllowedException(getName());
950
            }
951
        }
952

    
953
        if (this.selection != null) {
954
            this.selection.deleteObserver(this);
955
        }
956
        if (selection == null) {
957
            if (this.selection != null) {
958
                this.selection.dispose();
959
            }
960
            this.selection = null;
961
            return;
962
        }
963
        if (selection instanceof FeatureSelection) {
964
            if (undoable && isEditing()) {
965
                commands.selectionSet(this, this.selection,
966
                        (FeatureSelection) selection);
967
            }
968
            if (this.selection != null) {
969
                this.selection.dispose();
970
            }
971
            this.selection = (FeatureSelection) selection;
972
        } else {
973
            if (undoable && isEditing()) {
974
                commands.startComplex("_selectionSet");
975
            }
976
//            if (selection instanceof DefaultFeatureSelection) {
977
//                // FIXME: Estas lineas parece que no tienen sentido.
978
//                DefaultFeatureSelection defSelection = (DefaultFeatureSelection) selection;
979
//                defSelection.deselectAll(undoable);
980
//                defSelection.select(selection, undoable);
981
//            } else {
982
                if( this.selection == null ) {
983
                    this.selection = createDefaultFeatureSelection();
984
                }
985
                this.selection.deselectAll();
986
                this.selection.select(selection);
987
//            }
988
            if (undoable && isEditing()) {
989
                commands.endComplex();
990
            }
991
        }
992
        this.getFeatureSelection().addObserver(this);
993

    
994
        this.notifyChange(DataStoreNotification.SELECTION_CHANGE);
995
    }
996

    
997
    @Override
998
    public FeatureSelection createFeatureSelection() throws DataException {
999
        FeatureType featureType = this.getDefaultFeatureTypeQuietly();
1000
        if( featureType!=null ) {
1001
            Tags tags = featureType.getTags();
1002
            if( tags.has(DataManager.DAL_USE_LARGE_SELECTION) ) {
1003
                boolean useLargeSelection = tags.getBoolean(DataManager.DAL_USE_LARGE_SELECTION,true);
1004
                if( useLargeSelection ) {
1005
                    return createLargeFeatureSelection();
1006
                }
1007
                return this.provider.createFeatureSelection();
1008
            }
1009
        }
1010
        long maxSize = dataManager.getMaxSizeForSmallFeatureSelection();
1011
        if (this.provider.getFeatureCount() > maxSize) {
1012
            return createLargeFeatureSelection();
1013
        }
1014
        return this.provider.createFeatureSelection();
1015
    }
1016

    
1017
    @Override
1018
    public FeatureSelection createLargeFeatureSelection() throws DataException {
1019
        return new LargeFeatureSelection(this);
1020

    
1021
    }
1022

    
1023
    @Override
1024
    public FeatureSelection createMemoryFeatureSelection() throws DataException {
1025
        return this.provider.createFeatureSelection();
1026
    }
1027

    
1028
    @Override
1029
    public FeatureSelection getFeatureSelection() throws DataException {
1030
        if (selection == null) {
1031
            this.selection = createFeatureSelection();
1032
            this.selection.addObserver(this);
1033
        }
1034
        return selection;
1035
    }
1036

    
1037
    @Override
1038
    public boolean isFeatureSelectionEmpty() {
1039
        if( selection == null ) {
1040
            return true;
1041
        }
1042
        return selection.isEmpty();
1043
    }
1044
    
1045
    //
1046
    // ====================================================================
1047
    // Gestion de notificaciones
1048
    //
1049
    @Override
1050
    public FeatureStoreNotification notifyChange(FeatureStoreNotification storeNotification) {
1051
        if (delegateObservable != null) {
1052
            try {
1053
                delegateObservable.notifyObservers(storeNotification);
1054
            } catch (Throwable ex) {
1055
                LOGGER.warn("Problems notifying changes in the store '" + this.getName() + " (" + storeNotification.getType() + ").", ex);
1056
            }
1057
        }
1058
        return storeNotification;
1059
    }
1060

    
1061
    @Override
1062
    public FeatureStoreNotification notifyChange(String notification) {
1063
        return notifyChange(
1064
                new DefaultFeatureStoreNotification(
1065
                        this, notification, 
1066
                        this.editingSessionCode==null?this.lastEditingSessionCode:this.editingSessionCode,
1067
                        this.mode == FeatureStore.MODE_QUERY?this.lastMode:this.mode
1068
                )
1069
        );
1070
    }
1071

    
1072
    public FeatureStoreNotification notifyChange(String notification, String editingSessionCode) {
1073
        return notifyChange(new DefaultFeatureStoreNotification(this, notification, editingSessionCode));
1074
    }
1075

    
1076
    public FeatureStoreNotification notifyChange(String notification, String editingSessionCode, int editMode) {
1077
        return notifyChange(new DefaultFeatureStoreNotification(this, notification, editingSessionCode, editMode));
1078
    }
1079

    
1080
    public FeatureStoreNotification notifyChange(String notification,
1081
//            String editingSessionCode,
1082
            Iterator<FeatureReference> deleteds,
1083
            Iterator<EditableFeature> inserteds,
1084
            Iterator<EditableFeature> updateds,
1085
            Iterator<FeatureTypeChanged> featureTypesChanged,
1086
            boolean isSelectionCompromised) {
1087
        return notifyChange(
1088
                new DefaultFeatureStoreNotification(
1089
                        this, notification, 
1090
                        this.editingSessionCode==null?this.lastEditingSessionCode:this.editingSessionCode,
1091
                        this.mode == FeatureStore.MODE_QUERY?this.lastMode:this.mode,
1092
                        deleteds, inserteds, updateds, featureTypesChanged, isSelectionCompromised
1093
                )
1094
        );
1095
    }
1096

    
1097
    @Override
1098
    public FeatureStoreNotification notifyChange(String notification, FeatureProvider data) {
1099
        Feature f = null;
1100
        if (data != null) {
1101
            try {
1102
                f = createFeature(data);
1103
            } catch (Throwable ex) {
1104
                LOGGER.warn("Problems creating a feature to notifying changes in the store '" + this.getName() + " (" + notification + ").", ex);
1105
            }
1106
        }
1107
        return notifyChange(notification, f);
1108
    }
1109

    
1110
    public FeatureStoreNotification notifyChange(String notification, Feature feature) {
1111
        return notifyChange(
1112
                new DefaultFeatureStoreNotification(
1113
                        this, notification, 
1114
                        this.editingSessionCode==null?this.lastEditingSessionCode:this.editingSessionCode,
1115
                        this.mode == FeatureStore.MODE_QUERY?this.lastMode:this.mode,
1116
                        feature
1117
                )
1118
        );
1119
    }
1120

    
1121
    public FeatureStoreNotification notifyChange(String notification, Expression expression) {
1122
        return notifyChange(
1123
                new DefaultFeatureStoreNotification(
1124
                        this, notification, 
1125
                        this.editingSessionCode==null?this.lastEditingSessionCode:this.editingSessionCode,
1126
                        this.mode == FeatureStore.MODE_QUERY?this.lastMode:this.mode,
1127
                        expression
1128
                )
1129
        );
1130
    }
1131

    
1132
    public FeatureStoreNotification notifyChange(String notification, Command command) {
1133
        return notifyChange(
1134
                new DefaultFeatureStoreNotification(
1135
                        this, notification, 
1136
                        this.editingSessionCode==null?this.lastEditingSessionCode:this.editingSessionCode,
1137
                        this.mode == FeatureStore.MODE_QUERY?this.lastMode:this.mode,
1138
                        command
1139
                )
1140
        );
1141
    }
1142

    
1143
    public FeatureStoreNotification notifyChange(String notification, EditableFeatureType type) {
1144
        return notifyChange(
1145
                new DefaultFeatureStoreNotification(
1146
                        this, notification, 
1147
                        this.editingSessionCode==null?this.lastEditingSessionCode:this.editingSessionCode,
1148
                        this.mode == FeatureStore.MODE_QUERY?this.lastMode:this.mode,
1149
                        type
1150
                )
1151
        );
1152
    }
1153

    
1154
    @Override
1155
    public FeatureStoreNotification notifyChange(String notification, Resource resource) {
1156
        return notifyChange(
1157
                new DefaultFeatureStoreNotification(
1158
                        this, DataStoreNotification.RESOURCE_CHANGED
1159
                )
1160
        );
1161
    }
1162

    
1163
    //
1164
    // ====================================================================
1165
    // Gestion de bloqueos
1166
    //
1167
    @Override
1168
    public boolean isLocksSupported() {
1169
        return this.provider.isLocksSupported();
1170
    }
1171

    
1172
    @Override
1173
    public FeatureLocks getLocks() throws DataException {
1174
        if (!this.provider.isLocksSupported()) {
1175
            LOGGER.warn("Locks not supported");
1176
            return null;
1177
        }
1178
        if (locks == null) {
1179
            this.locks = this.provider.createFeatureLocks();
1180
        }
1181
        return locks;
1182
    }
1183

    
1184
    //
1185
    // ====================================================================
1186
    // Interface Observable
1187
    //
1188
    @Override
1189
    public void disableNotifications() {
1190
        this.delegateObservable.disableNotifications();
1191

    
1192
    }
1193

    
1194
    @Override
1195
    public void enableNotifications() {
1196
        this.delegateObservable.enableNotifications();
1197
    }
1198

    
1199
    @Override
1200
    public void beginComplexNotification() {
1201
        this.delegateObservable.beginComplexNotification();
1202

    
1203
    }
1204

    
1205
    @Override
1206
    public void endComplexNotification() {
1207
        this.delegateObservable.endComplexNotification();
1208

    
1209
    }
1210

    
1211
    @Override
1212
    public void addObserver(Observer observer) {
1213
        if (delegateObservable != null) {
1214
            this.delegateObservable.addObserver(observer);
1215
        }
1216
    }
1217

    
1218
    @Override
1219
    public void deleteObserver(Observer observer) {
1220
        if (delegateObservable != null) {
1221
            this.delegateObservable.deleteObserver(observer);
1222
        }
1223
    }
1224

    
1225
    @Override
1226
    public void deleteObservers() {
1227
        this.delegateObservable.deleteObservers();
1228

    
1229
    }
1230

    
1231
    //
1232
    // ====================================================================
1233
    // Interface Observer
1234
    //
1235
    // Usado para observar:
1236
    // - su seleccion
1237
    // - sus bloqueos
1238
    // - sus recursos
1239
    //
1240
    @Override
1241
    public void update(Observable observable, Object notification) {
1242
        if (observable instanceof FeatureSet) {
1243
            if (observable == this.selection) {
1244
                this.notifyChange(DataStoreNotification.SELECTION_CHANGE);
1245
            } else if (observable == this.locks) {
1246
                this.notifyChange(FeatureStoreNotification.LOCKS_CHANGE);
1247
            }
1248

    
1249
        } else if (observable instanceof FeatureStoreProvider) {
1250
            if (observable == this.provider) {
1251

    
1252
            }
1253
        } else if (observable instanceof FeatureReferenceSelection) {
1254
            if (notification instanceof String) {
1255
                this.notifyChange((String) notification);
1256
            }
1257
        }
1258
    }
1259

    
1260
    //
1261
    // ====================================================================
1262
    // Edicion
1263
    //
1264
    private void newVersionOfUpdate() {
1265
        this.versionOfUpdate++;
1266
    }
1267

    
1268
    private long currentVersionOfUpdate() {
1269
        return this.versionOfUpdate;
1270
    }
1271

    
1272
    private void checkInEditingMode() throws NeedEditingModeException {
1273
        if (mode != MODE_FULLEDIT) {
1274
            throw new NeedEditingModeException(this.getName());
1275
        }
1276
    }
1277

    
1278
    private void checkNotInAppendMode() throws IllegalStateException {
1279
        if (mode == MODE_APPEND) {
1280
            throw new IllegalStateException("Error: store "
1281
                    + this.getFullName() + " is in append mode");
1282
        }
1283
    }
1284

    
1285
    private void checkIsOwnFeature(Feature feature)
1286
            throws IllegalFeatureException {
1287
        if (((DefaultFeature) feature).getStore() != this) {
1288
            throw new IllegalFeatureException(this.getName());
1289
        }
1290
        // FIXME: fixFeatureType no vale para el checkIsOwnFeature
1291
        // fixFeatureType((DefaultFeatureType) feature.getType());
1292
    }
1293

    
1294
    private void exitEditingMode() {
1295
        if (commands != null) {
1296
            try {
1297
                commands.clear();
1298
            } catch (Exception ex) {
1299
                LOGGER.trace("Can't clear commands", ex);
1300
            }
1301
            commands = null;
1302
        }
1303

    
1304
        if (featureTypeManager != null) {
1305
            DisposeUtils.disposeQuietly(featureTypeManager);
1306
            featureTypeManager = null;
1307

    
1308
        }
1309

    
1310
        // TODO implementar un dispose para estos dos
1311
        featureManager = null;
1312
        spatialManager = null;
1313

    
1314
        featureCount = null;
1315

    
1316
        this.lastMode = this.mode;
1317
        mode = MODE_QUERY;
1318
        hasStrongChanges = true; // Lo deja a true por si las moscas
1319

    
1320
        this.lastEditingSessionCode = this.editingSessionCode;
1321
        this.editingSessionCode = null;
1322
    }
1323

    
1324
    @Override
1325
    synchronized public void edit() throws DataException {
1326
        edit(MODE_FULLEDIT);
1327
    }
1328

    
1329
    @Override
1330
    synchronized public void edit(int mode) throws DataException {
1331
        LOGGER.debug("Starting editing in mode: {}", mode);
1332
        String newSessionCode = this.createUniqueID();
1333
        try {
1334
            if (this.mode != MODE_QUERY) {
1335
                throw new AlreadyEditingException(this.getName());
1336
            }
1337
            if (!this.provider.supportsAppendMode()) {
1338
                mode = MODE_FULLEDIT;
1339
            }
1340
            switch (mode) {
1341
                case MODE_QUERY:
1342
                    throw new IllegalStateException(this.getName());
1343

    
1344
                case MODE_FULLEDIT:
1345
                    if (!this.transforms.isEmpty()) {
1346
                        throw new IllegalStateException(this.getName());
1347
                    }
1348
                    if (notifyChange(FeatureStoreNotification.BEFORE_STARTEDITING,
1349
                            newSessionCode, mode).isCanceled()) {
1350
                        return;
1351
                    }
1352
                    this.editingSessionCode = newSessionCode;
1353
                    invalidateIndexes();
1354
                    featureManager = new FeatureManager(this);
1355
                    featureTypeManager = new FeatureTypeManager(this);
1356
                    spatialManager = new SpatialManager(this, provider.getEnvelope());
1357

    
1358
                    commands = new DefaultFeatureCommandsStack(
1359
                            this, featureManager,
1360
                            spatialManager, featureTypeManager);
1361
                    this.mode = MODE_FULLEDIT;
1362
                    hasStrongChanges = false;
1363
                    notifyChange(FeatureStoreNotification.AFTER_STARTEDITING, newSessionCode, this.mode);
1364
                    break;
1365

    
1366
                case MODE_APPEND:
1367
                    if (!this.transforms.isEmpty()) {
1368
                        throw new IllegalStateException(this.getName());
1369
                    }
1370
                    if (notifyChange(FeatureStoreNotification.BEFORE_STARTEDITING,
1371
                            newSessionCode, mode).isCanceled()) {
1372
                        return;
1373
                    }
1374
                    this.editingSessionCode = newSessionCode;
1375
                    invalidateIndexes();
1376
                    this.provider.beginAppend();
1377
                    this.mode = MODE_APPEND;
1378
                    notifyChange(FeatureStoreNotification.AFTER_STARTEDITING,
1379
                            newSessionCode, this.mode);
1380
                    break;
1381
                case MODE_PASS_THROUGH:
1382
                    if (!this.provider.supportsPassThroughMode()) {
1383
                        throw new IllegalStateException(this.getName());
1384
                    }
1385
                    if (!this.transforms.isEmpty()) {
1386
                        throw new IllegalStateException(this.getName());
1387
                    }
1388
                    if (notifyChange(FeatureStoreNotification.BEFORE_STARTEDITING,
1389
                            newSessionCode, mode).isCanceled()) {
1390
                        return;
1391
                    }
1392
                    this.editingSessionCode = newSessionCode;
1393
                    invalidateIndexes();
1394
                    this.mode = MODE_PASS_THROUGH;
1395
                    notifyChange(FeatureStoreNotification.AFTER_STARTEDITING,
1396
                            newSessionCode, this.mode);
1397
                    break;
1398

    
1399
            }
1400
        } catch (Exception e) {
1401
            try {
1402
                if (this.mode != MODE_QUERY) {
1403
                    exitEditingMode();
1404
                }
1405
                notifyChange(FeatureStoreNotification.FAILED_STARTEDITING,
1406
                        newSessionCode, mode);
1407
            } catch (Throwable th) {
1408
                LOGGER.warn("Can't cleanup after error in start editing.", th);
1409
            }
1410
            throw new StoreEditException(e, this.getName());
1411
        }
1412
    }
1413

    
1414
    private void invalidateIndexes() {
1415
        setIndexesValidStatus(false);
1416
    }
1417

    
1418
    private void setIndexesValidStatus(boolean valid) {
1419
        FeatureIndexes theIndexes = getIndexes();
1420
        LOGGER.debug("Setting the store indexes to valid status {}: {}", (valid
1421
                ? Boolean.TRUE : Boolean.FALSE), theIndexes);
1422
        for (Iterator iterator = theIndexes.iterator(); iterator.hasNext();) {
1423
            FeatureIndex index = (FeatureIndex) iterator.next();
1424
            if (index instanceof FeatureIndexProviderServices) {
1425
                FeatureIndexProviderServices indexServices
1426
                        = (FeatureIndexProviderServices) index;
1427
                indexServices.setValid(valid);
1428
            }
1429
        }
1430
    }
1431

    
1432
    private void updateIndexes() throws FeatureIndexException {
1433
        FeatureIndexes theIndexes = getIndexes();
1434
        LOGGER.debug("Refilling indexes: {}", theIndexes);
1435
        for (Iterator iterator = theIndexes.iterator(); iterator.hasNext();) {
1436
            FeatureIndex index = (FeatureIndex) iterator.next();
1437
            if (index instanceof FeatureIndexProviderServices) {
1438
                FeatureIndexProviderServices indexServices
1439
                        = (FeatureIndexProviderServices) index;
1440
                indexServices.fill(true, null);
1441
            }
1442
        }
1443
    }
1444

    
1445
    private void waitForIndexes() {
1446
        FeatureIndexes theIndexes = getIndexes();
1447
        LOGGER.debug("Waiting for indexes to finish filling: {}", theIndexes);
1448
        for (Iterator iterator = theIndexes.iterator(); iterator.hasNext();) {
1449
            FeatureIndex index = (FeatureIndex) iterator.next();
1450
            if (index instanceof FeatureIndexProviderServices) {
1451
                FeatureIndexProviderServices indexServices
1452
                        = (FeatureIndexProviderServices) index;
1453
                indexServices.waitForIndex();
1454
            }
1455
        }
1456
    }
1457

    
1458
    private void disposeIndexes() {
1459
        FeatureIndexes theIndexes = getIndexes();
1460
        LOGGER.debug("Disposing indexes: {}", theIndexes);
1461
        if (theIndexes == null) {
1462
            return;
1463
        }
1464
        for (Iterator iterator = theIndexes.iterator(); iterator.hasNext();) {
1465
            FeatureIndex index = (FeatureIndex) iterator.next();
1466
            if (index instanceof FeatureIndexProviderServices) {
1467
                FeatureIndexProviderServices indexServices
1468
                        = (FeatureIndexProviderServices) index;
1469
                indexServices.dispose();
1470
            }
1471
        }
1472
    }
1473

    
1474
    @Override
1475
    public boolean isEditing() {
1476
        return mode == MODE_FULLEDIT;
1477
    }
1478

    
1479
    @Override
1480
    public boolean isAppending() {
1481
        return mode == MODE_APPEND;
1482
    }
1483

    
1484
    @Override
1485
    synchronized public void update(EditableFeatureType type)
1486
            throws DataException {
1487
        try {
1488
            if (type == null) {
1489
                throw new NullFeatureTypeException(getName());
1490
            }
1491

    
1492
            switch (this.mode) {
1493
                case MODE_QUERY:
1494
                    if (type.hasOnlyMetadataChanges(this.defaultFeatureType)) {
1495
                        if (notifyChange(FeatureStoreNotification.BEFORE_UPDATE_TYPE, type).isCanceled()) {
1496
                            return;
1497
                        }
1498
                        FeatureType theType = type.getNotEditableCopy();
1499
                        if (defaultFeatureType.getId().equals(theType.getId())) {
1500
                            defaultFeatureType = theType;
1501
                        }
1502
                        List newtypes = new ArrayList();
1503
                        for (FeatureType featureType : this.featureTypes) {
1504
                            if (featureType.getId().equals(theType.getId())) {
1505
                                newtypes.add(theType);
1506
                            } else {
1507
                                newtypes.add(featureType);
1508
                            }
1509
                        }
1510
                        this.featureTypes = newtypes;
1511
                        saveDALFile();
1512
                        notifyChange(FeatureStoreNotification.AFTER_UPDATE_TYPE, type);
1513
                    }
1514

    
1515
                    break;
1516
                case MODE_FULLEDIT:
1517
                    if (notifyChange(FeatureStoreNotification.BEFORE_UPDATE_TYPE, type).isCanceled()) {
1518
                        return;
1519
                    }
1520
                    newVersionOfUpdate();
1521

    
1522
                    FeatureType oldt = type.getSource().getCopy();
1523
                    FeatureType newt = type.getCopy();
1524
                    commands.update(newt, oldt);
1525
                    hasStrongChanges = ((DefaultEditableFeatureType) type).hasStrongChanges();
1526
                    notifyChange(FeatureStoreNotification.AFTER_UPDATE_TYPE, type);
1527
                    break;
1528
                case MODE_APPEND:
1529
                case MODE_PASS_THROUGH:
1530
                    throw new NeedEditingModeException(this.getName());
1531

    
1532
            }
1533
        } catch (Exception e) {
1534
            throw new StoreUpdateFeatureTypeException(e, this.getName());
1535
        }
1536
    }
1537

    
1538
    @Override
1539
    public void delete(Feature feature) throws DataException {
1540
        switch (this.mode) {
1541
            case MODE_PASS_THROUGH:
1542
                checkIsOwnFeature(feature);
1543
                if (notifyChange(FeatureStoreNotification.BEFORE_DELETE, feature).isCanceled()) {
1544
                    return;
1545
                }
1546
                this.provider.passThroughDelete((FeatureReferenceProviderServices) feature.getReference());
1547
                notifyChange(FeatureStoreNotification.AFTER_DELETE, feature);
1548
                break;
1549
            default:
1550
                this.commands.delete(feature);
1551
                break;
1552

    
1553
        }
1554
    }
1555

    
1556
    @Override
1557
    public void delete(String filter) {
1558
        if (StringUtils.isBlank(filter)) {
1559
            return;
1560
        }
1561
        this.delete(ExpressionUtils.createExpression(filter));
1562
    }
1563

    
1564
    @Override
1565
    public void delete(Expression filter) {
1566
        if (filter == null) {
1567
            return;
1568
        }
1569
        boolean pendingFinishEditing = false;
1570
        DisposableFeatureSetIterable features = null;
1571
        try {
1572
            switch (this.mode) {
1573
                case MODE_QUERY:
1574
                    pendingFinishEditing = true;
1575
                    this.edit();
1576
                    break;
1577
                case MODE_APPEND:
1578
                    throw new IllegalStateException("Delete not allowed in append mode.");
1579
                case MODE_FULLEDIT:
1580
                    break;
1581
                case MODE_PASS_THROUGH:
1582
                    if (notifyChange(FeatureStoreNotification.BEFORE_DELETE, filter).isCanceled()) {
1583
                        return;
1584
                    }
1585
                    this.provider.passThroughDelete(filter);
1586
                    notifyChange(FeatureStoreNotification.AFTER_DELETE, filter);                    
1587
                    return;
1588
                default:
1589
                    throw new IllegalStateException("Mode " + this.mode + " not supported.");
1590
            }
1591

    
1592
            FeatureSet fset = this.getFeatureSet(filter);
1593
            features = fset.iterable();
1594
            for (Feature f : features) {
1595
                fset.delete(f);
1596
            }
1597
        } catch (DataException ex) {
1598
            throw new DataRuntimeException(ex.getFormatString(), ex.getMessageKey(), ex.getCode()) {
1599
            };
1600
        } catch (Exception ex) {
1601
            throw new RuntimeException("Can't delete features (" + filter.getPhrase() + ").", ex);
1602
        } finally {
1603
            if (pendingFinishEditing) {
1604
                this.finishEditingQuietly();
1605
            }
1606
            DisposeUtils.disposeQuietly(features);
1607
        }
1608
    }
1609

    
1610
    synchronized public void doDelete(Feature feature) throws DataException {
1611
        if (feature == null) {
1612
            throw new IllegalArgumentException("feature argument can't be null.");
1613
        }
1614
        try {
1615
            checkInEditingMode();
1616
            checkIsOwnFeature(feature);
1617
            if (feature instanceof EditableFeature && !((EditableFeature)feature).isUpdatable()) {
1618
                //La feature no est? persistida en disco
1619
                throw new StoreDeleteEditableFeatureException(getName());
1620
            }
1621
            if (notifyChange(FeatureStoreNotification.BEFORE_DELETE, feature).isCanceled()) {
1622
                return;
1623
            }
1624

    
1625
            //Update the featureManager and the spatialManager
1626
            featureManager.delete(feature);
1627
            spatialManager.deleteFeature(feature);
1628

    
1629
            newVersionOfUpdate();
1630
            hasStrongChanges = true;
1631
            notifyChange(FeatureStoreNotification.AFTER_DELETE, feature);
1632
        } catch (Exception e) {
1633
            throw new StoreDeleteFeatureException(e, this.getName());
1634
        }
1635
    }
1636

    
1637
    @Override
1638
    public synchronized void insert(FeatureSet set) throws DataException {
1639
        switch (mode) {
1640
            case MODE_QUERY:
1641
                throw new NeedEditingModeException(this.getName());
1642

    
1643
            case MODE_APPEND:
1644
            case MODE_FULLEDIT:
1645
            case MODE_PASS_THROUGH:
1646
            try {
1647
                set.accept((Object obj) -> {
1648
                    EditableFeature ef = createNewFeature((Feature) obj);
1649
                    insert(ef);
1650
                });
1651
            } catch (BaseException ex) {
1652
                throw new StoreInsertFeatureException(ex, this.getName());
1653
            }
1654
            break;
1655
        }
1656
    }
1657

    
1658
    private static EditableFeature lastChangedFeature = null;
1659

    
1660
    @Override
1661
    public synchronized void insert(EditableFeature feature)
1662
            throws DataException {
1663
        LOGGER.debug("In editing mode {}, insert feature: {}", mode, feature);
1664
        try {
1665
            switch (mode) {
1666
                case MODE_QUERY:
1667
                    throw new NeedEditingModeException(this.getName());
1668

    
1669
                case MODE_APPEND:
1670
                    checkIsOwnFeature(feature);
1671
                    if (feature.isUpdatable()) {
1672
                        throw new NoNewFeatureInsertException(this.getName());
1673
                    }
1674
                    if (notifyChange(FeatureStoreNotification.BEFORE_INSERT, feature).isCanceled()) {
1675
                        return;
1676
                    }
1677
                    this.featureCount = null;
1678
                    feature.validate(CHECK_RULES_AT_EDITING);
1679
                    provider.append(((DefaultEditableFeature) feature).getData());
1680
                    hasStrongChanges = true;
1681
                    notifyChange(FeatureStoreNotification.AFTER_INSERT, feature);
1682
                    break;
1683

    
1684
                case MODE_FULLEDIT:
1685
                    if (feature.isUpdatable()) {
1686
                        throw new NoNewFeatureInsertException(this.getName());
1687
                    }
1688
                    feature.validate(CHECK_RULES_AT_EDITING);
1689
                    commands.insert(feature);
1690
                    break;
1691

    
1692
                case MODE_PASS_THROUGH:
1693
                    checkIsOwnFeature(feature);
1694
                    if (feature.isUpdatable()) {
1695
                        throw new NoNewFeatureInsertException(this.getName());
1696
                    }
1697
                    if (notifyChange(FeatureStoreNotification.BEFORE_INSERT, feature).isCanceled()) {
1698
                        return;
1699
                    }
1700
                    feature.validate(CHECK_RULES_AT_EDITING);
1701
                    this.provider.passThroughInsert(((DefaultEditableFeature) feature).getData());
1702
                    notifyChange(FeatureStoreNotification.AFTER_INSERT, feature);
1703
                    break;
1704
            }
1705
        } catch (Exception e) {
1706
            throw new StoreInsertFeatureException(e, this.getName());
1707
        }
1708
    }
1709

    
1710
    synchronized public void doInsert(EditableFeature feature)
1711
            throws DataException {
1712
        checkIsOwnFeature(feature);
1713

    
1714
        waitForIndexes();
1715

    
1716
        if (notifyChange(FeatureStoreNotification.BEFORE_INSERT, feature).isCanceled()) {
1717
            return;
1718
        }
1719
        newVersionOfUpdate();
1720
        if ((lastChangedFeature == null)
1721
                || (lastChangedFeature.getSource() != feature.getSource())) {
1722
            lastChangedFeature = feature;
1723
            feature.validate(CHECK_RULES_AT_EDITING);
1724
            lastChangedFeature = null;
1725
        }
1726
        //Update the featureManager and the spatialManager
1727
        ((DefaultFeature) feature).setInserted(true);
1728
        DefaultFeature newFeature = (DefaultFeature) feature.getNotEditableCopy();
1729

    
1730
        featureManager.add(feature);
1731
        spatialManager.insertFeature(newFeature);
1732

    
1733
        hasStrongChanges = true;
1734
        notifyChange(FeatureStoreNotification.AFTER_INSERT, feature);
1735
    }
1736

    
1737
    @Override
1738
    public void update(EditableFeature feature)
1739
            throws DataException {
1740
        switch (this.mode) {
1741
            case MODE_PASS_THROUGH:
1742
                checkIsOwnFeature(feature);
1743
                if (!feature.isUpdatable()) {
1744
                    throw new NoNewFeatureInsertException(this.getName());
1745
                }
1746
                if (notifyChange(FeatureStoreNotification.BEFORE_UPDATE, feature).isCanceled()) {
1747
                    return;
1748
                }
1749
                feature.validate(CHECK_RULES_AT_EDITING);
1750
                this.provider.passThroughUpdate(((DefaultEditableFeature) feature).getData());
1751
                notifyChange(FeatureStoreNotification.AFTER_UPDATE, feature);
1752
                break;
1753
            case MODE_FULLEDIT:
1754
                if (feature.isUpdatable()) {
1755
                    commands.update(feature, feature.getSource());
1756
                    return;
1757
                }
1758
                // FIXME: Deberiamos lanzar aqui un error en lugar de hacer el insert.
1759
                //        O lanzar un mensaje al log?
1760
                insert(feature);
1761
                break;
1762
            default:
1763
                throw new NeedEditingModeException(this.getName());
1764
        }
1765
    }
1766

    
1767
    @Override
1768
    public void update(Object... parameters) throws DataException {
1769
        if (parameters.length == 1) {
1770
            Object param0 = parameters[0];
1771
            if (param0 instanceof EditableFeature) {
1772
                this.update((EditableFeature) param0);
1773
            } else if (param0 instanceof EditableFeatureType) {
1774
                this.update((EditableFeatureType) param0);
1775
            } else {
1776
                throw new IllegalArgumentException("Type of first parameter isn't supported");
1777
            }
1778
            return;
1779
        }
1780

    
1781
        Expression filter = null;
1782
        long end = parameters.length;
1783
        if (parameters.length % 2 == 1) { //IMPAR
1784
            Object param = parameters[parameters.length - 1];
1785
            if (param != null) {
1786
                if (param instanceof Expression) {
1787
                    filter = (Expression) param;
1788
                } else {
1789
                    filter = ExpressionUtils.createExpression(param.toString());
1790
                }
1791
            }
1792
        } else {
1793
            end = parameters.length - 1;
1794
        }
1795

    
1796
        switch (this.mode) {
1797
            case MODE_PASS_THROUGH:
1798
                this.provider.passThroughUpdate(
1799
                        //                    this.getName(), 
1800
                        parameters,
1801
                        filter);
1802
                break;
1803
            case MODE_FULLEDIT:
1804
                FeatureSet set = this.getFeatureSet(filter);
1805
                DisposableIterator it = set.fastIterator();
1806
                while (it.hasNext()) {
1807
                    Feature feature = (Feature) it.next();
1808
                    EditableFeature ef = feature.getEditable();
1809
                    for (int i = 0; i < end; i += 2) {
1810
                        String name = (String) parameters[i];
1811
                        Object value = parameters[i + 1];
1812
                        ef.set(name, value);
1813
                    }
1814
                    set.update(ef);
1815
                }
1816
                DisposeUtils.disposeQuietly(it);
1817
                DisposeUtils.disposeQuietly(set);
1818
                break;
1819
            default:
1820
                throw new NeedEditingModeException(this.getName());
1821
        }
1822
    }
1823

    
1824
    synchronized public void doUpdate(EditableFeature feature, Feature oldFeature)
1825
            throws DataException {
1826
        try {
1827
            checkInEditingMode();
1828
            checkIsOwnFeature(feature);
1829
            if (notifyChange(FeatureStoreNotification.BEFORE_UPDATE, feature).isCanceled()) {
1830
                return;
1831
            }
1832
            newVersionOfUpdate();
1833
            if ((lastChangedFeature == null)
1834
                    || (lastChangedFeature.getSource() != feature.getSource())) {
1835
                lastChangedFeature = feature;
1836
                feature.validate(CHECK_RULES_AT_EDITING);
1837
                lastChangedFeature = null;
1838
            }
1839

    
1840
            //Update the featureManager and the spatialManager
1841
            Feature newf = feature.getNotEditableCopy();
1842
            featureManager.update(feature, oldFeature);
1843
            spatialManager.updateFeature(newf, oldFeature);
1844

    
1845
            hasStrongChanges = true;
1846
            notifyChange(FeatureStoreNotification.AFTER_UPDATE, feature);
1847
        } catch (Exception e) {
1848
            throw new StoreUpdateFeatureException(e, this.getName());
1849
        }
1850
    }
1851

    
1852
    @Override
1853
    synchronized public void redo() throws RedoException {
1854
        Command redo = commands.getNextRedoCommand();
1855
        try {
1856
            checkInEditingMode();
1857
        } catch (NeedEditingModeException ex) {
1858
            throw new RedoException(redo, ex);
1859
        }
1860
        if (notifyChange(FeatureStoreNotification.BEFORE_REDO, redo).isCanceled()) {
1861
            return;
1862
        }
1863
        newVersionOfUpdate();
1864
        commands.redo();
1865
        hasStrongChanges = true;
1866
        notifyChange(FeatureStoreNotification.AFTER_REDO, redo);
1867
    }
1868

    
1869
    @Override
1870
    synchronized public void undo() throws UndoException {
1871
        Command undo = commands.getNextUndoCommand();
1872
        try {
1873
            checkInEditingMode();
1874
        } catch (NeedEditingModeException ex) {
1875
            throw new UndoException(undo, ex);
1876
        }
1877
        if (notifyChange(FeatureStoreNotification.BEFORE_UNDO, undo).isCanceled()) {
1878
            return;
1879
        }
1880
        newVersionOfUpdate();
1881
        commands.undo();
1882
        hasStrongChanges = true;
1883
        notifyChange(FeatureStoreNotification.AFTER_UNDO, undo);
1884
    }
1885

    
1886
    @Override
1887
    public List getRedoInfos() {
1888
        if (isEditing() && (commands != null)) {
1889
            return commands.getRedoInfos();
1890
        } else {
1891
            return null;
1892
        }
1893
    }
1894

    
1895
    @Override
1896
    public List getUndoInfos() {
1897
        if (isEditing() && (commands != null)) {
1898
            return commands.getUndoInfos();
1899
        } else {
1900
            return null;
1901
        }
1902
    }
1903

    
1904
    public synchronized FeatureCommandsStack getCommandsStack()
1905
            throws DataException {
1906
        checkInEditingMode();
1907
        return commands;
1908
    }
1909

    
1910
    @Override
1911
    public boolean cancelEditingQuietly() {
1912
        try {
1913
            this.cancelEditing();
1914
            return true;
1915
        } catch (Exception ex) {
1916
            LOGGER.debug("Can't cancel editing", ex);
1917
            return false;
1918
        }
1919
    }
1920

    
1921
    @Override
1922
    synchronized public void cancelEditing() throws DataException {
1923
        if (spatialManager != null) {
1924
            spatialManager.cancelModifies();
1925
        }
1926
        try {
1927
            switch (mode) {
1928
                case MODE_QUERY:
1929
                    throw new NeedEditingModeException(this.getName());
1930

    
1931
                case MODE_APPEND:
1932
                    if (notifyChange(FeatureStoreNotification.BEFORE_CANCELEDITING).isCanceled()) {
1933
                        return;
1934
                    }
1935
                    provider.abortAppend();
1936
                    exitEditingMode();
1937
                    ((FeatureSelection) this.getSelection()).deselectAll();
1938
                    updateIndexes();
1939
                    notifyChange(FeatureStoreNotification.AFTER_CANCELEDITING);
1940
                    break;
1941

    
1942
                case MODE_FULLEDIT:
1943
                    boolean clearSelection = this.hasStrongChanges;
1944
                    if (this.selection instanceof FeatureReferenceSelection) {
1945
                        clearSelection = this.hasStrongChanges || this.featureManager.hasNews();
1946
                    }
1947
                    if (notifyChange(FeatureStoreNotification.BEFORE_CANCELEDITING).isCanceled()) {
1948
                        return;
1949
                    }
1950
                    exitEditingMode();
1951
                    if (clearSelection) {
1952
                        ((FeatureSelection) this.getSelection()).deselectAll();
1953
                    }
1954
                    updateIndexes();
1955
                    notifyChange(FeatureStoreNotification.AFTER_CANCELEDITING);
1956
                    break;
1957

    
1958
                case MODE_PASS_THROUGH:
1959
                    if (notifyChange(FeatureStoreNotification.BEFORE_CANCELEDITING).isCanceled()) {
1960
                        return;
1961
                    }
1962
                    exitEditingMode();
1963
                    ((FeatureSelection) this.getSelection()).deselectAll();
1964
                    updateIndexes();
1965
                    notifyChange(FeatureStoreNotification.AFTER_CANCELEDITING);
1966
                    break;
1967
            }
1968
        } catch (Exception e) {
1969
            throw new StoreCancelEditingException(e, this.getName());
1970
        }
1971
    }
1972

    
1973
    @Override
1974
    public boolean finishEditingQuietly() {
1975
        try {
1976
            this.finishEditing();
1977
            return true;
1978
        } catch (Exception ex) {
1979
            LOGGER.debug("Can't finish editing", ex);
1980
            return false;
1981
        }
1982
    }
1983

    
1984
    @Override
1985
    synchronized public void finishEditing() throws DataException {
1986
        LOGGER.debug("finish editing of mode: {}", mode);
1987
        try {
1988
            Map<String, List<FeatureAttributeDescriptor>> computedFields = this.getComputedFields();
1989
            switch (mode) {
1990
                case MODE_QUERY:
1991
                    throw new NeedEditingModeException(this.getName());
1992

    
1993
                case MODE_APPEND:
1994
                    if (selection != null) {
1995
                        selection = null;
1996
                    }
1997
                    if (notifyChange(FeatureStoreNotification.BEFORE_FINISHEDITING).isCanceled()) {
1998
                        return;
1999
                    }
2000
                    saveDALFile();
2001
                    provider.endAppend();
2002
                    exitEditingMode();
2003
                    this.updateComputedFields(computedFields);
2004
                    loadDALFile();
2005
                    updateIndexes();
2006
                    notifyChange(FeatureStoreNotification.AFTER_FINISHEDITING);
2007
                    break;
2008

    
2009
                case MODE_FULLEDIT:
2010
                    if (featureManager.hasChanges() || featureTypeManager.hasChanges()) {
2011
                        if (hasStrongChanges && !this.allowWrite()) {
2012
                            throw new WriteNotAllowedException(getName());
2013
                        }
2014
                        if (notifyChange(FeatureStoreNotification.PREPARING_FINISHEDITING).isCanceled()) {
2015
                            return;
2016
                        }
2017
                        if (hasStrongChanges) {
2018
                            validateFeaturesAtFinishEditing();
2019
                        }
2020
                        if (notifyChange(FeatureStoreNotification.BEFORE_FINISHEDITING,
2021
                                featureManager.getDeleted(),
2022
                                featureManager.getInsertedFeatures(),
2023
                                featureManager.getUpdatedFeatures(),
2024
                                featureTypeManager.getFeatureTypesChanged().iterator(),
2025
                                featureManager.isSelectionCompromised()).isCanceled()) {
2026
                            return;
2027
                        }
2028
                        saveDALFile();
2029
                        if (featureManager.isSelectionCompromised() && selection != null) {
2030
                            selection = null;
2031
                        }
2032
                        if (hasStrongChanges) {
2033
                            // This will throw a PerformEditingException if the provider
2034
                            // does not accept the changes (for example, an invalid field name)
2035
                            provider.performChanges(featureManager.getDeleted(),
2036
                                    featureManager.getInserted(),
2037
                                    featureManager.getUpdated(),
2038
                                    removeCalculatedAttributes(featureTypeManager.getFeatureTypesChanged()).iterator());
2039

    
2040
                        }
2041
                        this.updateComputedFields(computedFields);
2042
                        exitEditingMode();
2043
                        loadDALFile();
2044
                        updateIndexes();
2045
                        notifyChange(FeatureStoreNotification.AFTER_FINISHEDITING);
2046
                    } else {
2047
                        exitEditingMode();
2048
                    }
2049
                    break;
2050
                case MODE_PASS_THROUGH:
2051
                    if (selection != null) {
2052
                        selection = null;
2053
                    }
2054
                    if (notifyChange(FeatureStoreNotification.BEFORE_FINISHEDITING).isCanceled()) {
2055
                        return;
2056
                    }
2057
                    exitEditingMode();
2058
                    updateIndexes();
2059
                    notifyChange(FeatureStoreNotification.AFTER_FINISHEDITING);
2060
                    break;
2061
            }
2062
        } catch (ValidateFeaturesException | WriteNotAllowedException ex) {
2063
            // Don't notify failed.
2064
            throw ex;
2065
        } catch (PerformEditingException pee) {
2066
            notifyChange(FeatureStoreNotification.FAILED_FINISHEDITING);
2067
            throw new WriteException(provider.getSourceId().toString(), pee);
2068
        } catch (Exception e) {
2069
            notifyChange(FeatureStoreNotification.FAILED_FINISHEDITING);
2070
            throw new FinishEditingException(e);
2071
        }
2072
    }
2073

    
2074
    @Override
2075
    public String getEditingSession() {
2076
        return this.editingSessionCode;
2077
    }
2078

    
2079
    private Map<String, List<FeatureAttributeDescriptor>> getComputedFields() throws DataException {
2080
        Map<String, List<FeatureAttributeDescriptor>> r = new HashMap<>();
2081

    
2082
        List<FeatureType> theTypes = new ArrayList<>();
2083
        theTypes.addAll(this.getFeatureTypes());
2084
        theTypes.add(this.getDefaultFeatureType());
2085
        for (int n = 0; n < theTypes.size(); n++) {
2086
            FeatureType type = theTypes.get(n);
2087
            for (FeatureAttributeDescriptor attrdesc : type) {
2088
                FeatureAttributeEmulator emulator = attrdesc.getFeatureAttributeEmulator();
2089
                if (emulator != null) {
2090
                    List<FeatureAttributeDescriptor> l = r.get(type.getId());
2091
                    if (l == null) {
2092
                        l = new ArrayList<>();
2093
                        r.put(type.getId(), l);
2094
                    }
2095
                    l.add(attrdesc);
2096
                }
2097
            }
2098
        }
2099
        return r;
2100
    }
2101

    
2102
    private void updateComputedFields(Map<String, List<FeatureAttributeDescriptor>> computedFields) throws DataException {
2103

    
2104
        List<FeatureType> theTypes = new ArrayList<>();
2105
        theTypes.addAll(this.getFeatureTypes());
2106
        theTypes.add(this.getDefaultFeatureType());
2107
        for (int n = 0; n < theTypes.size(); n++) {
2108
            DefaultFeatureType type = (DefaultFeatureType) theTypes.get(n);
2109
            List<FeatureAttributeDescriptor> x = computedFields.get(type.getId());
2110
            if (x != null && !x.isEmpty()) {
2111
                for (FeatureAttributeDescriptor attrdesc : x) {
2112
                    if (type.get(attrdesc.getName()) == null) {
2113
                        type.add(attrdesc);
2114
                    }
2115
                }
2116
            }
2117
        }
2118

    
2119
    }
2120

    
2121
    private List<FeatureTypeChanged> removeCalculatedAttributes(List<FeatureTypeChanged> ftypes) {
2122
        // FIXME: Falta por implementar
2123
//        for (FeatureStoreProvider.FeatureTypeChanged ftype : ftypes) {
2124
//            EditableFeatureType target = (EditableFeatureType) ftype.getTarget();
2125
//            for (FeatureAttributeDescriptor attributeDescriptor : ftype.getSource().getAttributeDescriptors()) {
2126
//                if (attributeDescriptor.isComputed()) {
2127
//                    target.remove(attributeDescriptor.getName());
2128
//                }
2129
//            }
2130
//        }
2131
        return ftypes;
2132
    }
2133

    
2134
    private void saveDALFile() {
2135
        if( this.ignoreDALResource ) {
2136
            return;
2137
        }
2138
        org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource resource = null;
2139
        ResourcesStorage theResourcesStorage = null;
2140
        try {
2141
            theResourcesStorage = this.getResourcesStorage();
2142
            if (theResourcesStorage == null || theResourcesStorage.isReadOnly()) {
2143
                return;
2144
            }
2145
            resource = theResourcesStorage.getResource("dal");
2146
            if (resource == null || resource.isReadOnly()) {
2147
                return;
2148
            }
2149
            DALFile dalFile = DALFile.getDALFile();
2150
            dalFile.setStore(this);
2151
            if (!dalFile.isEmpty()) {
2152
                dalFile.write(resource);
2153
            }
2154
        } catch (Throwable ex) {
2155
            LOGGER.warn("Can't save DAL resource", ex);
2156
        } finally {
2157
            IOUtils.closeQuietly(resource);
2158
            DisposeUtils.disposeQuietly(theResourcesStorage);
2159
        }
2160
    }
2161

    
2162
    private void loadDALFile() {
2163
        if( this.ignoreDALResource ) {
2164
            return;
2165
        }
2166
        org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource resource = null;
2167
        ResourcesStorage theResourcesStorage = null;
2168
        try {
2169
            theResourcesStorage = this.getResourcesStorage();
2170
            if (theResourcesStorage == null) {
2171
                return;
2172
            }
2173
            resource = theResourcesStorage.getResource("dal");
2174
            if (resource == null || !resource.exists()) {
2175
                return;
2176
            }
2177
            DALFile dalFile = DALFile.getDALFile(resource);
2178
            if (!dalFile.isEmpty()) {
2179
                dalFile.updateStore(this);
2180
            }
2181
        } catch (Throwable ex) {
2182
            if (resource == null || theResourcesStorage == null) {
2183
                if (theResourcesStorage == null) {
2184
                    LOGGER.warn("Can't load DAL resource (resname=null, resurl=null, storage=null)", ex);
2185
                } else {
2186
                    LOGGER.warn("Can't load DAL resource (resname=null, resurl=null, storage=" + theResourcesStorage.getClass().getName() + ").", ex);
2187
                }
2188
            } else {
2189
                LOGGER.warn("Can't load DAL resource (resname=" + resource.getName() + ", resurl=" + Objects.toString(resource.getURL()) + ", storage=" + theResourcesStorage.getClass().getName() + ").", ex);
2190
            }
2191
        } finally {
2192
            IOUtils.closeQuietly(resource);
2193
            DisposeUtils.disposeQuietly(theResourcesStorage);
2194
        }
2195
    }
2196

    
2197
    /**
2198
     * Save changes in the provider without leaving the edit mode. Do not call
2199
     * observers to communicate a change of ediding mode. The operation's
2200
     * history is eliminated to prevent inconsistencies in the data.
2201
     *
2202
     * @throws DataException
2203
     */
2204
    @Override
2205
    synchronized public void commitChanges() throws DataException {
2206
        LOGGER.debug("commitChanges of mode: {}", mode);
2207
        if (!canCommitChanges()) {
2208
            throw new WriteNotAllowedException(getName());
2209
        }
2210
        try {
2211
            switch (mode) {
2212
                case MODE_QUERY:
2213
                    throw new NeedEditingModeException(this.getName());
2214

    
2215
                case MODE_APPEND:
2216
                    this.provider.endAppend();
2217
                    exitEditingMode();
2218
                    invalidateIndexes();
2219
                    this.provider.beginAppend();
2220
                    break;
2221

    
2222
                case MODE_FULLEDIT:
2223
                    if (hasStrongChanges && !this.allowWrite()) {
2224
                        throw new WriteNotAllowedException(getName());
2225
                    }
2226
                    // FIXME: OOhh!!!! no se disparan eventos, VCSGis no se entera de estos cambios.
2227
                    if (hasStrongChanges) {
2228
                        validateFeaturesAtFinishEditing();
2229
                        provider.performChanges(featureManager.getDeleted(),
2230
                                featureManager.getInserted(),
2231
                                featureManager.getUpdated(),
2232
                                removeCalculatedAttributes(featureTypeManager.getFeatureTypesChanged()).iterator());
2233
                    }
2234
                    invalidateIndexes();
2235
                    featureManager = new FeatureManager(this);
2236
                    featureTypeManager = new FeatureTypeManager(this);
2237
                    spatialManager = new SpatialManager(this, provider.getEnvelope());
2238

    
2239
                    commands
2240
                            = new DefaultFeatureCommandsStack(this, featureManager,
2241
                                    spatialManager, featureTypeManager);
2242
                    featureCount = null;
2243
                    hasStrongChanges = false;
2244
                    break;
2245
            }
2246
        } catch (Exception e) {
2247
            throw new FinishEditingException(e);
2248
        }
2249
    }
2250

    
2251
    @Override
2252
    synchronized public boolean canCommitChanges() throws DataException {
2253
        if (!this.allowWrite()) {
2254
            return false;
2255
        }
2256
        switch (mode) {
2257
            default:
2258
            case MODE_QUERY:
2259
                return false;
2260

    
2261
            case MODE_APPEND:
2262
                return true;
2263

    
2264
            case MODE_FULLEDIT:
2265
                List types = this.getFeatureTypes();
2266
                for (int i = 0; i < types.size(); i++) {
2267
                    Object type = types.get(i);
2268
                    if (type instanceof DefaultEditableFeatureType) {
2269
                        if (((DefaultEditableFeatureType) type).hasStrongChanges()) {
2270
                            return false;
2271
                        }
2272
                    }
2273
                }
2274
                return true;
2275
        }
2276
    }
2277

    
2278
    @Override
2279
    public void beginEditingGroup(String description)
2280
            throws NeedEditingModeException {
2281
        checkInEditingMode();
2282
        commands.startComplex(description);
2283
    }
2284

    
2285
    @Override
2286
    public void endEditingGroup() throws NeedEditingModeException {
2287
        checkInEditingMode();
2288
        commands.endComplex();
2289
    }
2290

    
2291
    @Override
2292
    public boolean isAppendModeSupported() {
2293
        return this.provider.supportsAppendMode();
2294
    }
2295

    
2296
    @Override
2297
    public void export(DataServerExplorer explorer, String provider,
2298
            NewFeatureStoreParameters params, String name) throws DataException {
2299

    
2300
        if (this.getFeatureTypes().size() != 1) {
2301
            throw new NotYetImplemented(
2302
                    "export whith more than one type not yet implemented");
2303
        }
2304
        FeatureSelection featureSelection = (FeatureSelection) getSelection();
2305
        FeatureStore target = null;
2306
        FeatureSet features = null;
2307
        DisposableIterator iterator = null;
2308
        try {
2309
            FeatureType type = this.getDefaultFeatureType();
2310
            if ((params.getDefaultFeatureType() == null)
2311
                    || (params.getDefaultFeatureType().size() == 0)) {
2312
                params.setDefaultFeatureType(type.getEditable());
2313

    
2314
            }
2315
            explorer.add(provider, params, true);
2316
            DataManager manager = DALLocator.getDataManager();
2317

    
2318
            DataStoreParameters openParams = explorer.get(name); //OpenFeatureStoreParameters) manager.createStoreParameters(explorer.getProviderName());
2319
//            ToolsLocator.getDynObjectManager().copy(params, openParams);
2320

    
2321
            target = (FeatureStore) manager.openStore(provider, openParams);
2322
            FeatureType targetType = target.getDefaultFeatureType();
2323

    
2324
            target.edit(MODE_APPEND);
2325
            FeatureAttributeDescriptor[] pkattrs = type.getPrimaryKey();
2326
            if (featureSelection.getSize() > 0) {
2327
                features = this.getFeatureSelection();
2328
            } else {
2329
                if ((pkattrs != null) && (pkattrs.length > 0)) {
2330
                    FeatureQuery query = createFeatureQuery();
2331
                    for (FeatureAttributeDescriptor pkattr : pkattrs) {
2332
                        query.getOrder().add(pkattr.getName(), true);
2333
                    }
2334
                    features = this.getFeatureSet(query);
2335
                } else {
2336
                    features = this.getFeatureSet();
2337
                }
2338
            }
2339
            iterator = features.fastIterator();
2340
            while (iterator.hasNext()) {
2341
                DefaultFeature feature = (DefaultFeature) iterator.next();
2342
                target.insert(target.createNewFeature(targetType, feature));
2343
            }
2344
            target.finishEditing();
2345
            target.dispose();
2346
        } catch (Exception e) {
2347
            throw new DataExportException(e, params.toString());
2348
        } finally {
2349
            dispose(iterator);
2350
            dispose(features);
2351
            dispose(target);
2352
        }
2353
    }
2354

    
2355
    @Override
2356
    public void copyTo(final FeatureStore target) {
2357
        boolean finishEditingAtEnd = false;
2358
        try {
2359
            if (!target.isEditing() && !target.isAppending()) {
2360
                finishEditingAtEnd = true;
2361
                target.edit(MODE_APPEND);
2362
            }
2363
            this.accept((Object obj) -> {
2364
                Feature f_src = (Feature) obj;
2365
                EditableFeature f_dst = target.createNewFeature(f_src);
2366
                target.insert(f_dst);
2367
            });
2368
            if (finishEditingAtEnd) {
2369
                target.finishEditing();
2370
            }
2371

    
2372
        } catch (Exception ex) {
2373
            try {
2374
                if (finishEditingAtEnd) {
2375
                    target.cancelEditing();
2376
                }
2377
            } catch (Exception ex1) {
2378
            }
2379
            throw new RuntimeException("Can't copy store.", ex);
2380
        }
2381

    
2382
    }
2383

    
2384
    //
2385
    // ====================================================================
2386
    // Obtencion de datos
2387
    // getDataCollection, getFeatureCollection
2388
    //
2389
    @Override
2390
    public DataSet getDataSet() throws DataException {
2391
        return this.getFeatureSet((FeatureQuery)null);
2392
    }
2393

    
2394
    @Override
2395
    public DataSet getDataSet(DataQuery dataQuery) throws DataException {
2396
        return this.getFeatureSet((FeatureQuery)dataQuery);
2397
    }
2398

    
2399
    @Override
2400
    public void getDataSet(Observer observer) throws DataException {
2401
        checkNotInAppendMode();
2402
        this.getFeatureSet(null, observer);
2403
    }
2404

    
2405
    @Override
2406
    public void getDataSet(DataQuery dataQuery, Observer observer)
2407
            throws DataException {
2408
        checkNotInAppendMode();
2409
        this.getFeatureSet((FeatureQuery) dataQuery, observer);
2410
    }
2411

    
2412
    @Override
2413
    public FeatureSet getFeatureSet() throws DataException {
2414
        return this.getFeatureSet((FeatureQuery) null);
2415
    }
2416

    
2417
    @Override
2418
    public FeatureSet getFeatureSet(FeatureQuery featureQuery)
2419
            throws DataException {
2420
        checkNotInAppendMode();
2421
        if (featureQuery == null) {
2422
            featureQuery = new DefaultFeatureQuery(this.getDefaultFeatureType());
2423
        } else if( featureQuery.hasAggregateFunctions() || featureQuery.hasGroupByColumns() ) {
2424
            // Si tenemos datos por persistir en la bbdd (bien por que estamos en modo 
2425
            // append(batchsize) o por que estamos en modo fulledit y hay cambios
2426
            // realizados, las agrupaciones y funciones de agregado, como las gestiona
2427
            // la bbdd, pueden no dar resultados correctos. Asi que no dejamos hacer
2428
            // estas operaciones.
2429
            if( this.mode==MODE_APPEND  ) {
2430
                throw new UnsupportedOperationException("Can't create a set with aggregate functions or group by columns in append mode");
2431
            }
2432
            if( this.mode == MODE_FULLEDIT ) {
2433
                if( this.featureManager==null || this.featureManager.getPendingChangesCount()>0 ) {
2434
                   throw new UnsupportedOperationException("Can't create a set with aggregate functions or group by columns with editing changes");
2435
                }
2436
            }
2437
        }
2438
        return new DefaultFeatureSet(this, featureQuery);
2439
    }
2440

    
2441
    @Override
2442
    public FeatureSet getFeatureSet(String filter) throws DataException {
2443
        return this.getFeatureSet(filter, null, true);
2444
    }
2445

    
2446
    @Override
2447
    public FeatureSet getFeatureSet(String filter, String sortBy) throws DataException {
2448
        return this.getFeatureSet(filter, sortBy, true);
2449
    }
2450

    
2451
    @Override
2452
    public FeatureSet getFeatureSet(Expression filter) throws DataException {
2453
        return this.getFeatureSet(filter, null, true);
2454
    }
2455

    
2456
    @Override
2457
    public FeatureSet getFeatureSet(Expression filter, String sortBy) throws DataException {
2458
        return this.getFeatureSet(filter, sortBy, true);
2459
    }
2460

    
2461
    @Override
2462
    public FeatureSet getFeatureSet(Expression filter, String sortBy, boolean asc) throws DataException {
2463
        FeatureQuery query = this.createFeatureQuery(filter, sortBy, asc);
2464
        return this.getFeatureSet(query);
2465
    }
2466

    
2467
    @Override
2468
    public FeatureSet getFeatureSet(String filter, String sortBy, boolean asc) throws DataException {
2469
        FeatureQuery query = this.createFeatureQuery(filter, sortBy, asc);
2470
        return this.getFeatureSet(query);
2471
    }
2472

    
2473
    @Override
2474
    public List<Feature> getFeatures(String filter) {
2475
        return this.getFeatures(filter, null, true);
2476
    }
2477

    
2478
    @Override
2479
    public List<Feature> getFeatures(String filter, String sortBy) {
2480
        return this.getFeatures(filter, sortBy, true);
2481
    }
2482

    
2483
    @Override
2484
    public List<Feature> getFeatures(String filter, String sortBy, boolean asc) {
2485
        FeatureQuery query = this.createFeatureQuery(filter, sortBy, asc);
2486
        return this.getFeatures(query, 0);
2487
    }
2488

    
2489
    @Override
2490
    public List<Feature> getFeatures(Expression filter) {
2491
        return this.getFeatures(filter, null, true);
2492
    }
2493

    
2494
    @Override
2495
    public List<Feature> getFeatures(Expression filter, String sortBy) {
2496
        return this.getFeatures(filter, sortBy, true);
2497
    }
2498

    
2499
    @Override
2500
    public List<Feature> getFeatures(Expression filter, String sortBy, boolean asc) {
2501
        FeatureQuery query = this.createFeatureQuery(filter, sortBy, asc);
2502
        return this.getFeatures(query, 0);
2503
    }
2504

    
2505
    @Override
2506
    public List<Feature> getFeatures(FeatureQuery query) {
2507
        return this.getFeatures(query, 0);
2508
    }
2509

    
2510
    @Override
2511
    public List<Feature> getFeatures(FeatureQuery query, int pageSize) {
2512
        try {
2513
            if (pageSize <= 0) {
2514
                pageSize = 100;
2515
            }
2516
            FeaturePagingHelper pager = this.dataManager.createFeaturePagingHelper(this, query, pageSize);
2517
            return pager.asList();
2518
        } catch (BaseException ex) {
2519
            throw new RuntimeException("Can't create the list of features.", ex);
2520
        }
2521
    }
2522
        
2523
    @Override
2524
    public List<Feature> getFeatures() {
2525
        return this.getFeatures(null, 0);
2526
    }
2527

    
2528
    @Override
2529
    public GetItemWithSizeIsEmptyAndIterator64<Feature> getFeatures64() {
2530
        return this.getFeatures64(null, 0);
2531
    }
2532

    
2533
    @Override
2534
    public GetItemWithSizeIsEmptyAndIterator64<Feature> getFeatures64(String filter) {
2535
        return this.getFeatures64(filter, null, true);
2536
    }
2537

    
2538
    @Override
2539
    public GetItemWithSizeIsEmptyAndIterator64<Feature> getFeatures64(String filter, String sortBy, boolean asc) {
2540
        FeatureQuery query = this.createFeatureQuery(filter, sortBy, asc);
2541
        return this.getFeatures64(query, 0);
2542
    }
2543

    
2544
    @Override
2545
    public GetItemWithSizeIsEmptyAndIterator64<Feature> getFeatures64(FeatureQuery query, int pageSize) {
2546
        try {
2547
            if (pageSize <= 0) {
2548
                pageSize = 100;
2549
            }
2550
            FeaturePagingHelper pager = this.dataManager.createFeaturePagingHelper(this, query, pageSize);
2551
            return pager;
2552
        } catch (BaseException ex) {
2553
            throw new RuntimeException("Can't create the list of features.", ex);
2554
        }
2555
    }
2556

    
2557
    @Override
2558
    public Feature first() throws DataException {
2559
        return this.findFirst((FeatureQuery) null);
2560
    }
2561

    
2562
    @Override
2563
    public Feature findFirst(String filter) throws DataException {
2564
        return this.findFirst(filter, (String) null, true);
2565
    }
2566

    
2567
    @Override
2568
    public Feature findFirst(String filter, String sortBy) throws DataException {
2569
        return this.findFirst(filter, sortBy, true);
2570
    }
2571

    
2572
    @Override
2573
    public Feature findFirst(String filter, String sortBy, boolean asc) throws DataException {
2574
        FeatureQuery query = this.createFeatureQuery(filter, sortBy, asc);
2575
        return findFirst(query);
2576
    }
2577

    
2578
    @Override
2579
    public Feature findFirst(String filter, Expression sortBy, boolean asc) throws DataException {
2580
        FeatureQuery query = this.createFeatureQuery(filter, sortBy, asc);
2581
        return findFirst(query);
2582
    }
2583

    
2584
    @Override
2585
    public Feature findFirst(Expression filter) throws DataException {
2586
        return this.findFirst(filter, (String) null, true);
2587
    }
2588

    
2589
    @Override
2590
    public Feature findFirst(Expression filter, String sortBy) throws DataException {
2591
        return this.findFirst(filter, sortBy, true);
2592
    }
2593

    
2594
    @Override
2595
    public Feature findFirst(Expression filter, String sortBy, boolean asc) throws DataException {
2596
        FeatureQuery query = this.createFeatureQuery(filter, sortBy, asc);
2597
        return findFirst(query);
2598
    }
2599

    
2600
    @Override
2601
    public Feature findFirst(Expression filter, Expression sortBy, boolean asc) throws DataException {
2602
        FeatureQuery query = this.createFeatureQuery(filter, sortBy, asc);
2603
        return findFirst(query);
2604
    }
2605

    
2606
    @Override
2607
    public Feature findFirst(FeatureQuery query) throws DataException {
2608
        if (query == null) {
2609
            query = this.createFeatureQuery();
2610
        } else {
2611
            query = query.getCopy();
2612
        }
2613
        query.setLimit(1);
2614
        final MutableObject<Feature> feature = new MutableObject<>();
2615
        try {
2616
            this.accept((Object obj) -> {
2617
                feature.setValue((Feature) obj);
2618
                throw new VisitCanceledException();
2619
            }, query);
2620
        } catch (VisitCanceledException ex) {
2621

    
2622
        } catch (DataException ex) {
2623
            throw ex;
2624
        } catch (Exception ex) {
2625
            throw new RuntimeException("", ex);
2626
        }
2627
        return feature.getValue();
2628
    }
2629

    
2630
    @Override
2631
    public void accept(Visitor visitor) throws BaseException {
2632
        this.accept(visitor, null);
2633
    }
2634

    
2635
    @Override
2636
    public void accept(Visitor visitor, DataQuery dataQuery)
2637
            throws BaseException {
2638
        FeatureSet set = getFeatureSet((FeatureQuery) dataQuery);
2639
        try {
2640
            set.accept(visitor);
2641
        } finally {
2642
            set.dispose();
2643
        }
2644
    }
2645

    
2646
    public FeatureType getFeatureType(FeatureQuery featureQuery)
2647
            throws DataException {
2648
        DefaultFeatureType fType
2649
                = (DefaultFeatureType) this.getFeatureType(featureQuery
2650
                        .getFeatureTypeId());
2651
        if (featureQuery.hasAttributeNames()
2652
                || featureQuery.hasConstantsAttributeNames()
2653
                || fType.hasRequiredFields()) {
2654
            if (featureQuery.hasGroupByColumns()) {
2655
                return fType.getSubtype(featureQuery.getAttributeNames(), featureQuery.getConstantsAttributeNames(), false);
2656
            } else {
2657
                return fType.getSubtype(featureQuery.getAttributeNames(), featureQuery.getConstantsAttributeNames());
2658
            }
2659
        }
2660
        return fType;
2661
    }
2662

    
2663
    @Override
2664
    public void getFeatureSet(Observer observer) throws DataException {
2665
        checkNotInAppendMode();
2666
        this.getFeatureSet(null, observer);
2667
    }
2668

    
2669
    @Override
2670
    public void getFeatureSet(FeatureQuery query, Observer observer)
2671
            throws DataException {
2672
        class LoadInBackGround implements Runnable {
2673

    
2674
            private final FeatureStore store;
2675
            private final FeatureQuery query;
2676
            private final Observer observer;
2677

    
2678
            public LoadInBackGround(FeatureStore store, FeatureQuery query,
2679
                    Observer observer) {
2680
                this.store = store;
2681
                this.query = query;
2682
                this.observer = observer;
2683
            }
2684

    
2685
            void notify(FeatureStoreNotification theNotification) {
2686
                observer.update(store, theNotification);
2687
            }
2688

    
2689
            @Override
2690
            public void run() {
2691
                FeatureSet set = null;
2692
                try {
2693
                    set = store.getFeatureSet(query);
2694
                    notify(new DefaultFeatureStoreNotification(store,
2695
                            FeatureStoreNotification.LOAD_FINISHED, set));
2696
                } catch (Exception e) {
2697
                    notify(new DefaultFeatureStoreNotification(store,
2698
                            FeatureStoreNotification.LOAD_FINISHED, e));
2699
                } finally {
2700
                    dispose(set);
2701
                }
2702
            }
2703
        }
2704

    
2705
        checkNotInAppendMode();
2706
        if (query == null) {
2707
            query = new DefaultFeatureQuery(this.getDefaultFeatureType());
2708
        }
2709
        LoadInBackGround task = new LoadInBackGround(this, query, observer);
2710
        Thread thread = new Thread(task, "Load Feature Set in background");
2711
        thread.start();
2712
    }
2713

    
2714
    @Override
2715
    public Feature getFeatureByReference(FeatureReference reference)
2716
            throws DataException {
2717
        checkNotInAppendMode();
2718
        FeatureReferenceProviderServices ref = (FeatureReferenceProviderServices) reference;
2719
        FeatureType featureType;
2720
        if (ref.getFeatureTypeId() == null) {
2721
            featureType = this.getDefaultFeatureType();
2722
        } else {
2723
            featureType = this.getFeatureType(ref.getFeatureTypeId());
2724
        }
2725
        return this.getFeatureByReference(reference, featureType);
2726
    }
2727

    
2728
    @Override
2729
    public Feature getFeatureByReference(FeatureReference reference,
2730
            FeatureType featureType) throws DataException {
2731
        checkNotInAppendMode();
2732
        featureType = fixFeatureType((DefaultFeatureType) featureType);
2733
        if (this.mode == MODE_FULLEDIT) {
2734
            Feature f = featureManager.get(reference, this, featureType);
2735
            if (f != null) {
2736
                return f;
2737
            }
2738
        }
2739

    
2740
        FeatureType sourceFeatureType = featureType;
2741
        if (!this.transforms.isEmpty()) {
2742
            sourceFeatureType = this.transforms.getSourceFeatureTypeFrom(featureType);
2743
        }
2744
        // TODO comprobar que el id es de este store
2745

    
2746
        DefaultFeature feature
2747
                = new DefaultFeature(this,
2748
                        this.provider.getFeatureProviderByReference(
2749
                                (FeatureReferenceProviderServices) reference, sourceFeatureType));
2750

    
2751
        if (!this.transforms.isEmpty()) {
2752
            return this.transforms.applyTransform(feature, featureType);
2753
        }
2754
        return feature;
2755
    }
2756

    
2757
    //
2758
    // ====================================================================
2759
    // Gestion de features
2760
    //
2761
    private FeatureType fixFeatureType(DefaultFeatureType type)
2762
            throws DataException {
2763
        FeatureType original = this.getDefaultFeatureType();
2764

    
2765
        if ((type == null) || type.equals(original)) {
2766
            return original;
2767
        } else {
2768
            if (!type.isSubtypeOf(original)) {
2769
                Iterator iter = this.getFeatureTypes().iterator();
2770
                FeatureType tmpType;
2771
                boolean found = false;
2772
                while (iter.hasNext()) {
2773
                    tmpType = (FeatureType) iter.next();
2774
                    if (type.equals(tmpType)) {
2775
                        return type;
2776

    
2777
                    } else if (type.isSubtypeOf(tmpType)) {
2778
                        found = true;
2779
                        original = tmpType;
2780
                        break;
2781
                    }
2782

    
2783
                }
2784
                if (!found) {
2785
                    throw new IllegalFeatureTypeException(getName());
2786
                }
2787
            }
2788
        }
2789

    
2790
        // Checks that type has all fields of pk
2791
        // else add the missing attributes at the end.
2792
        if (!original.hasOID()) {
2793
            // Gets original pk attributes
2794
            DefaultEditableFeatureType edOriginal
2795
                    = (DefaultEditableFeatureType) original.getEditable();
2796
            FeatureAttributeDescriptor orgAttr;
2797
            Iterator edOriginalIter = edOriginal.iterator();
2798
            while (edOriginalIter.hasNext()) {
2799
                orgAttr = (FeatureAttributeDescriptor) edOriginalIter.next();
2800
                if (!orgAttr.isPrimaryKey()) {
2801
                    edOriginalIter.remove();
2802
                }
2803
            }
2804

    
2805
            // Checks if all pk attributes are in type
2806
            Iterator typeIterator;
2807
            edOriginalIter = edOriginal.iterator();
2808
            FeatureAttributeDescriptor attr;
2809
            while (edOriginalIter.hasNext()) {
2810
                orgAttr = (FeatureAttributeDescriptor) edOriginalIter.next();
2811
                typeIterator = type.iterator();
2812
                while (typeIterator.hasNext()) {
2813
                    attr = (FeatureAttributeDescriptor) typeIterator.next();
2814
                    if (attr.getName().equals(orgAttr.getName())) {
2815
                        edOriginalIter.remove();
2816
                        break;
2817
                    }
2818
                }
2819
            }
2820

    
2821
            // add missing pk attributes if any
2822
            if (edOriginal.size() > 0) {
2823
                boolean isEditable = type instanceof DefaultEditableFeatureType;
2824
                DefaultEditableFeatureType edType
2825
                        = (DefaultEditableFeatureType) original.getEditable();
2826
                edType.clear();
2827
                edType.addAll(type);
2828
                edType.addAll(edOriginal);
2829
                if (!isEditable) {
2830
                    type = (DefaultFeatureType) edType.getNotEditableCopy();
2831
                }
2832
            }
2833

    
2834
        }
2835

    
2836
        return type;
2837
    }
2838

    
2839
    private void validateFeaturesAtFinishEditing() throws ValidateFeaturesException {
2840
        try {
2841
            checkInEditingMode();
2842
            FeatureType type = this.getDefaultFeatureTypeQuietly();
2843
            DefaultFeatureRules rules = (DefaultFeatureRules) this.getDefaultFeatureType().getRules();
2844

    
2845
            int checks = type.isCheckFeaturesAtFinishEditing() ? CHECK_REQUIREDS : 0;
2846
            checks = rules.isEmpty() ? checks : checks | CHECK_RULES_AT_FINISH;
2847
            if (checks == 0) {
2848
                return;
2849
            }
2850

    
2851
            Iterator<EditableFeature> features = new ChainedIterator<>(
2852
                    featureManager.getInsertedFeatures(),
2853
                    featureManager.getUpdatedFeatures()
2854
            );
2855
            while (features.hasNext()) {
2856
                EditableFeature feature = features.next();
2857
                rules.validate(feature, checks);
2858
            }
2859
        } catch (Exception ex) {
2860
            throw new ValidateFeaturesException(this.getName(), ex);
2861
        }
2862
    }
2863

    
2864
    @Override
2865
    public FeatureType getDefaultFeatureType() throws DataException {
2866
        try {
2867

    
2868
            if (isEditing()) {
2869
                FeatureType auxFeatureType
2870
                        = featureTypeManager.getType(defaultFeatureType.getId());
2871
                if (auxFeatureType != null) {
2872
                    return avoidEditable(auxFeatureType);
2873
                }
2874
            }
2875
            FeatureType type = this.transforms.getDefaultFeatureType();
2876
            if (type != null) {
2877
                return avoidEditable(type);
2878
            }
2879

    
2880
            return avoidEditable(defaultFeatureType);
2881

    
2882
        } catch (Exception e) {
2883
            throw new GetFeatureTypeException(e, getName());
2884
        }
2885
    }
2886

    
2887
    @Override
2888
    public FeatureType getDefaultFeatureTypeQuietly() {
2889
        try {
2890
            return this.getDefaultFeatureType();
2891
        } catch (Exception ex) {
2892
            return null;
2893
        }
2894
    }
2895

    
2896
    private FeatureType avoidEditable(FeatureType ft) {
2897
        if (ft instanceof EditableFeatureType) {
2898
            return ((EditableFeatureType) ft).getNotEditableCopy();
2899
        } else {
2900
            return ft;
2901
        }
2902
    }
2903

    
2904
    @Override
2905
    public FeatureType getFeatureType(String featureTypeId)
2906
            throws DataException {
2907
        if (featureTypeId == null) {
2908
            return this.getDefaultFeatureType();
2909
        }
2910
        try {
2911
            if (isEditing()) {
2912
                FeatureType auxFeatureType
2913
                        = featureTypeManager.getType(featureTypeId);
2914
                if (auxFeatureType != null) {
2915
                    return auxFeatureType;
2916
                }
2917
            }
2918
            FeatureType type = this.transforms.getFeatureType(featureTypeId);
2919
            if (type != null) {
2920
                return type;
2921
            }
2922
            Iterator iter = this.featureTypes.iterator();
2923
            while (iter.hasNext()) {
2924
                type = (FeatureType) iter.next();
2925
                if (type.getId().equals(featureTypeId)) {
2926
                    return type;
2927
                }
2928
            }
2929
            return null;
2930
        } catch (Exception e) {
2931
            throw new GetFeatureTypeException(e, getName());
2932
        }
2933
    }
2934

    
2935
    public FeatureType getProviderDefaultFeatureType() {
2936
        return defaultFeatureType;
2937
    }
2938

    
2939
    @Override
2940
    public List getFeatureTypes() throws DataException {
2941
        try {
2942
            List types;
2943
            if (isEditing()) {
2944
                types = new ArrayList();
2945
                for (FeatureType type : featureTypes) {
2946
                    FeatureType typeaux = featureTypeManager.getType(type.getId());
2947
                    if (typeaux != null) {
2948
                        types.add(typeaux);
2949
                    } else {
2950
                        types.add(type);
2951
                    }
2952
                }
2953
                Iterator it = featureTypeManager.newsIterator();
2954
                while (it.hasNext()) {
2955
                    FeatureType type = (FeatureType) it.next();
2956
                    types.add(type);
2957
                }
2958
            } else {
2959
                types = this.transforms.getFeatureTypes();
2960
                if (types == null) {
2961
                    types = featureTypes;
2962
                }
2963
            }
2964
            return Collections.unmodifiableList(types);
2965
        } catch (Exception e) {
2966
            throw new GetFeatureTypeException(e, getName());
2967
        }
2968
    }
2969

    
2970
    public List getProviderFeatureTypes() throws DataException {
2971
        return Collections.unmodifiableList(this.featureTypes);
2972
    }
2973

    
2974
    @Override
2975
    public Feature createFeature(FeatureProvider data) throws DataException {
2976
        DefaultFeature feature = new DefaultFeature(this, data);
2977
        return feature;
2978
    }
2979

    
2980
    public Feature createFeature(FeatureProvider data, FeatureType type)
2981
            throws DataException {
2982
        // FIXME: falta por implementar
2983
        // Comprobar si es un subtipo del feature de data
2984
        // y construir un feature usando el subtipo.
2985
        // Probablemente requiera generar una copia del data.
2986
        throw new NotYetImplemented();
2987
    }
2988

    
2989
    @Override
2990
    public EditableFeature createNewFeature(FeatureType type,
2991
            Feature defaultValues) throws DataException {
2992
        try {
2993
            FeatureProvider data = createNewFeatureProvider(type);
2994
            DefaultEditableFeature feature
2995
                    = new DefaultEditableFeature(this, data);
2996
            feature.initializeValues(defaultValues);
2997
            data.setNew(true);
2998

    
2999
            return feature;
3000
        } catch (Exception e) {
3001
            throw new CreateFeatureException(e, getName());
3002
        }
3003
    }
3004

    
3005
    private FeatureProvider createNewFeatureProvider(FeatureType type)
3006
            throws DataException {
3007
        type = this.fixFeatureType((DefaultFeatureType) type);
3008
        FeatureProvider data = this.provider.createFeatureProvider(type);
3009
        data.setNew(true);
3010
        if (type.hasOID() && (data.getOID() == null)) {
3011
            data.setOID(this.provider.createNewOID());
3012
        } else {
3013
            data.setOID(this.getTemporalOID());
3014
        }
3015
        return data;
3016

    
3017
    }
3018

    
3019
    @Override
3020
    public EditableFeature createNewFeature(FeatureType type,
3021
            boolean defaultValues) throws DataException {
3022
        try {
3023
            FeatureProvider data = createNewFeatureProvider(type);
3024
            DefaultEditableFeature feature
3025
                    = new DefaultEditableFeature(this, data);
3026
            if (defaultValues) {
3027
                feature.initializeValues();
3028
            }
3029
            return feature;
3030
        } catch (Exception e) {
3031
            throw new CreateFeatureException(e, getName());
3032
        }
3033
    }
3034

    
3035
    @Override
3036
    public EditableFeature createNewFeature(boolean defaultValues)
3037
            throws DataException {
3038
        return this.createNewFeature(this.getDefaultFeatureType(),
3039
                defaultValues);
3040
    }
3041

    
3042
    @Override
3043
    public EditableFeature createNewFeature() throws DataException {
3044
        return this.createNewFeature(this.getDefaultFeatureType(), true);
3045
    }
3046

    
3047
    @Override
3048
    public EditableFeature createNewFeature(Feature defaultValues) throws DataException {
3049
        FeatureType ft = this.getDefaultFeatureType();
3050
        EditableFeature f = this.createNewFeature(ft, false);
3051
        f.copyFrom(defaultValues);
3052
        return f;
3053
    }
3054

    
3055
    @Override
3056
    public EditableFeature createNewFeature(JsonObject defaultValues) throws DataException {
3057
        FeatureType ft = this.getDefaultFeatureType();
3058
        EditableFeature f = this.createNewFeature(ft, false);
3059
        f.copyFrom(defaultValues);
3060
        return f;
3061
    }
3062

    
3063
    @Override
3064
    public EditableFeatureType createFeatureType() {
3065
        EditableFeatureType ftype = new DefaultEditableFeatureType(this);
3066
        return ftype;
3067
    }
3068

    
3069
    @Override
3070
    public EditableFeatureType createFeatureType(String id) {
3071
        DefaultEditableFeatureType ftype = new DefaultEditableFeatureType(this, id);
3072
        return ftype;
3073
    }
3074

    
3075
    //
3076
    // ====================================================================
3077
    // Index related methods
3078
    //
3079
    @Override
3080
    public FeatureIndexes getIndexes() {
3081
        return this.indexes;
3082
    }
3083

    
3084
    @Override
3085
    public FeatureIndex createIndex(FeatureType featureType,
3086
            String attributeName, String indexName) throws DataException {
3087
        return createIndex(null, featureType, attributeName, indexName);
3088
    }
3089

    
3090
    @Override
3091
    public FeatureIndex createIndex(String indexTypeName,
3092
            FeatureType featureType, String attributeName, String indexName)
3093
            throws DataException {
3094

    
3095
        return createIndex(indexTypeName, featureType, attributeName,
3096
                indexName, false, null);
3097
    }
3098

    
3099
    @Override
3100
    public FeatureIndex createIndex(FeatureType featureType,
3101
            String attributeName, String indexName, Observer observer)
3102
            throws DataException {
3103
        return createIndex(null, featureType, attributeName, indexName,
3104
                observer);
3105
    }
3106

    
3107
    @Override
3108
    public FeatureIndex createIndex(String indexTypeName,
3109
            FeatureType featureType, String attributeName, String indexName,
3110
            final Observer observer) throws DataException {
3111

    
3112
        return createIndex(indexTypeName, featureType, attributeName,
3113
                indexName, true, observer);
3114
    }
3115

    
3116
    private FeatureIndex createIndex(String indexTypeName,
3117
            FeatureType featureType, String attributeName, String indexName,
3118
            boolean background, final Observer observer) throws DataException {
3119

    
3120
        checkNotInAppendMode();
3121
        FeatureIndexProviderServices index;
3122
        index = dataManager.createFeatureIndexProvider(indexTypeName, this,
3123
                featureType, indexName,
3124
                featureType.getAttributeDescriptor(attributeName));
3125

    
3126
        try {
3127
            index.fill(background, observer);
3128
        } catch (FeatureIndexException e) {
3129
            throw new InitializeException(index.getName(), e);
3130
        }
3131

    
3132
        ((DefaultFeatureIndexes) getIndexes()).addIndex(index);
3133
        return index;
3134
    }
3135

    
3136
    //
3137
    // ====================================================================
3138
    // Transforms related methods
3139
    //
3140
    @Override
3141
    public FeatureStoreTransforms getTransforms() {
3142
        return this.transforms;
3143
    }
3144

    
3145
    @Override
3146
    public FeatureQuery createFeatureQuery() {
3147
        return new DefaultFeatureQuery(this.getName());
3148
    }
3149

    
3150
    @Override
3151
    public FeatureQuery createFeatureQuery(Expression filter, String sortBy, boolean asc) {
3152
        FeatureQuery query = null;
3153
        if (filter != null) {
3154
            query = this.createFeatureQuery();
3155
            query.setFilter(filter);
3156
        }
3157
        if (!StringUtils.isBlank(sortBy)) {
3158
            if (query == null) {
3159
                query = this.createFeatureQuery();
3160
            }
3161
            if (StringUtils.containsAny(sortBy, "(", ")")) {
3162
                throw new IllegalArgumentException("Incorrect sortBy expression");
3163
            }
3164
            String[] attrnames;
3165
            if (sortBy.contains(",")) {
3166
                attrnames = StringUtils.split(sortBy, ",");
3167
            } else {
3168
                attrnames = new String[]{sortBy};
3169
            }
3170
            for (String attrname : attrnames) {
3171
                attrname = attrname.trim();
3172
                if (attrname.startsWith("-")) {
3173
                    query.getOrder().add(attrname.substring(1).trim(), false);
3174
                } else if (attrname.endsWith("-")) {
3175
                    query.getOrder().add(attrname.substring(0, sortBy.length() - 1).trim(), false);
3176
                } else if (attrname.startsWith("+")) {
3177
                    query.getOrder().add(attrname.substring(1).trim(), true);
3178
                } else if (attrname.endsWith("-")) {
3179
                    query.getOrder().add(attrname.substring(0, sortBy.length() - 1).trim(), true);
3180
                } else {
3181
                    query.getOrder().add(attrname, asc);
3182
                }
3183
            }
3184
        }
3185
        if (query != null) {
3186
            query.retrievesAllAttributes();
3187
        }
3188
        return query;
3189
    }
3190

    
3191
    @Override
3192
    public FeatureQuery createFeatureQuery(String filter) {
3193
        return this.createFeatureQuery(
3194
                ExpressionUtils.createExpression(filter),
3195
                (String) null,
3196
                true
3197
        );
3198
    }
3199

    
3200
    @Override
3201
    public FeatureQuery createFeatureQuery(Expression filter) {
3202
        return this.createFeatureQuery(
3203
                filter,
3204
                (String) null,
3205
                true
3206
        );
3207
    }
3208

    
3209
    @Override
3210
    public FeatureQuery createFeatureQuery(String filter, String sortBy, boolean asc) {
3211
        if (StringUtils.isBlank(filter)) {
3212
            return this.createFeatureQuery(
3213
                    (Expression) null,
3214
                    sortBy,
3215
                    asc
3216
            );
3217
        } else {
3218
            return this.createFeatureQuery(
3219
                    ExpressionUtils.createExpression(filter),
3220
                    sortBy,
3221
                    asc
3222
            );
3223
        }
3224
    }
3225

    
3226
    @Override
3227
    public FeatureQuery createFeatureQuery(Expression filter, Expression sortBy, boolean asc) {
3228
        FeatureQuery query = null;
3229
        if (filter != null) {
3230
            query = this.createFeatureQuery();
3231
            query.setFilter(filter);
3232
        }
3233
        if (sortBy != null) {
3234
            if (query == null) {
3235
                query = this.createFeatureQuery();
3236
            }
3237
            query.getOrder().add(sortBy, asc);
3238
        }
3239

    
3240
        if (query != null) {
3241
            query.retrievesAllAttributes();
3242
        }
3243
        return query;
3244
    }
3245

    
3246
    @Override
3247
    public FeatureQuery createFeatureQuery(String filter, Expression sortBy, boolean asc) {
3248
        if (StringUtils.isBlank(filter)) {
3249
            return this.createFeatureQuery(
3250
                    (Expression) null,
3251
                    sortBy,
3252
                    asc
3253
            );
3254
        } else {
3255
            return this.createFeatureQuery(
3256
                    ExpressionUtils.createExpression(filter),
3257
                    sortBy,
3258
                    asc
3259
            );
3260
        }
3261
    }
3262

    
3263
    @Override
3264
    public DataQuery createQuery() {
3265
        return createFeatureQuery();
3266
    }
3267

    
3268
    //
3269
    // ====================================================================
3270
    // UndoRedo related methods
3271
    //
3272
    @Override
3273
    public boolean canRedo() {
3274
        return commands.canRedo();
3275
    }
3276

    
3277
    @Override
3278
    public boolean canUndo() {
3279
        return commands.canUndo();
3280
    }
3281

    
3282
    @Override
3283
    public void redo(int num) throws RedoException {
3284
        for (int i = 0; i < num; i++) {
3285
            redo();
3286
        }
3287
    }
3288

    
3289
    @Override
3290
    public void undo(int num) throws UndoException {
3291
        for (int i = 0; i < num; i++) {
3292
            undo();
3293
        }
3294
    }
3295

    
3296
    //
3297
    // ====================================================================
3298
    // Metadata related methods
3299
    //
3300
    @Override
3301
    public Object getMetadataID() {
3302
        return this.provider.getSourceId();
3303
    }
3304

    
3305
    @Override
3306
    public void delegate(DynObject dynObject) {
3307
        this.metadata.delegate(dynObject);
3308
    }
3309

    
3310
    @Override
3311
    public DynClass getDynClass() {
3312
        return this.metadata.getDynClass();
3313
    }
3314

    
3315
    @Override
3316
    public Object getDynValue(String name) throws DynFieldNotFoundException {
3317
        try {
3318
            if (this.transforms.hasDynValue(name)) {
3319
                return this.transforms.getDynValue(name);
3320
            }
3321
            if (this.metadata.hasDynValue(name)) {
3322
                return this.metadata.getDynValue(name);
3323
            }
3324
            if (METADATA_PROVIDER.equalsIgnoreCase(name)) {
3325
                return this.provider.getProviderName();
3326
            } else if (METADATA_CONTAINERNAME.equalsIgnoreCase(name)) {
3327
                return this.provider.getSourceId();
3328
            } else if (METADATA_FEATURETYPE.equalsIgnoreCase(name)) {
3329
                try {
3330
                    return this.getDefaultFeatureType();
3331
                } catch (DataException e) {
3332
                    return null;
3333
                }
3334
            }
3335
            return this.metadata.getDynValue(name);
3336
        } catch (Exception ex) {
3337
            LOGGER.debug("Can't retrieve the value of '" + name + "' in store '" + this.getName() + "'.", ex);
3338
            return null;
3339
        }
3340
    }
3341

    
3342
    @Override
3343
    public boolean hasDynValue(String name) {
3344
        if (this.transforms.hasDynValue(name)) {
3345
            return true;
3346
        }
3347
        return this.metadata.hasDynValue(name);
3348
    }
3349

    
3350
    @Override
3351
    public boolean hasDynMethod(String name) {
3352
        return ((DynObject_v2) this.metadata).hasDynMethod(name);
3353
    }
3354

    
3355
    @Override
3356
    public void implement(DynClass dynClass) {
3357
        this.metadata.implement(dynClass);
3358
    }
3359

    
3360
    @Override
3361
    public Object invokeDynMethod(String name, Object[] args)
3362
            throws DynMethodException {
3363
        return this.metadata.invokeDynMethod(this, name, args);
3364
    }
3365

    
3366
    @Override
3367
    public Object invokeDynMethod(int code, Object[] args)
3368
            throws DynMethodException {
3369
        return this.metadata.invokeDynMethod(this, code, args);
3370
    }
3371

    
3372
    @Override
3373
    public void setDynValue(String name, Object value)
3374
            throws DynFieldNotFoundException {
3375
        if (this.transforms.hasDynValue(name)) {
3376
            this.transforms.setDynValue(name, value);
3377
            return;
3378
        }
3379
        this.metadata.setDynValue(name, value);
3380

    
3381
    }
3382

    
3383
    /*
3384
     * (non-Javadoc)
3385
     *
3386
     * @see org.gvsig.metadata.Metadata#getMetadataChildren()
3387
     */
3388
    @Override
3389
    public Set getMetadataChildren() {
3390
        return this.metadataChildren;
3391
    }
3392

    
3393
    /*
3394
     * (non-Javadoc)
3395
     *
3396
     * @see org.gvsig.metadata.Metadata#getMetadataName()
3397
     */
3398
    @Override
3399
    public String getMetadataName() {
3400
        return this.provider.getProviderName();
3401
    }
3402

    
3403
    public FeatureTypeManager getFeatureTypeManager() {
3404
        return this.featureTypeManager;
3405
    }
3406

    
3407
    @Override
3408
    public long getFeatureCount() throws DataException {
3409
        if (featureCount == null) {
3410
            featureCount = this.provider.getFeatureCount();
3411
        }
3412
        if (this.isEditing()) {
3413
            if (this.isAppending()) {
3414
                try {
3415
                    throw new IllegalStateException();
3416
                } catch (IllegalStateException e) {
3417
                    LOGGER.info("Call DefaultFeatureStore.getFeatureCount editing in mode APPEND", e);
3418
                }
3419
                return -1;
3420
            } else {
3421
                return featureCount
3422
                        + this.featureManager.getDeltaSize();
3423
            }
3424
        }
3425
        return featureCount;
3426
    }
3427

    
3428
    private Long getTemporalOID() {
3429
        return this.temporalOid++;
3430
    }
3431

    
3432
    @Override
3433
    public FeatureType getProviderFeatureType(String featureTypeId) {
3434
        if (featureTypeId == null) {
3435
            return this.defaultFeatureType;
3436
        }
3437
        FeatureType type;
3438
        Iterator iter = this.featureTypes.iterator();
3439
        while (iter.hasNext()) {
3440
            type = (FeatureType) iter.next();
3441
            if (type.getId().equals(featureTypeId)) {
3442
                return type;
3443
            }
3444
        }
3445
        return null;
3446
    }
3447

    
3448
    @Override
3449
    public FeatureProvider getFeatureProviderFromFeature(Feature feature) {
3450
        return ((DefaultFeature) feature).getData();
3451
    }
3452

    
3453
    @Override
3454
    public DataStore getStore() {
3455
        return this;
3456
    }
3457

    
3458
    @Override
3459
    public FeatureStore getFeatureStore() {
3460
        return this;
3461
    }
3462

    
3463
    @Override
3464
    public void createCache(String name, DynObject parameters)
3465
            throws DataException {
3466
        cache = dataManager.createFeatureCacheProvider(name, parameters);
3467
        if (cache == null) {
3468
            throw new CreateException("FeaureCacheProvider", null);
3469
        }
3470
        cache.apply(this, provider);
3471
        provider = cache;
3472

    
3473
        featureCount = null;
3474
    }
3475

    
3476
    @Override
3477
    public FeatureCache getCache() {
3478
        return cache;
3479
    }
3480

    
3481
    @Override
3482
    public void clear() {
3483
        if (metadata != null) {
3484
            metadata.clear();
3485
        }
3486
    }
3487

    
3488
    @Override
3489
    public String getName() {
3490
        if (this.provider != null) {
3491
            return this.provider.getName();
3492
        }
3493
        if (this.parameters instanceof HasAFile) {
3494
            return FilenameUtils.getName(((HasAFile) this.parameters).getFile().getName());
3495
        }
3496
        return "unknow";
3497
    }
3498

    
3499
    @Override
3500
    public String getFullName() {
3501
        try {
3502
            if (this.provider != null) {
3503
                return this.provider.getFullName();
3504
            }
3505
            if (this.parameters instanceof HasAFile) {
3506
                return (((HasAFile) this.parameters).getFile().getAbsolutePath());
3507
            }
3508
            return null;
3509
        } catch (Throwable th) {
3510
            return null;
3511
        }
3512
    }
3513

    
3514
    @Override
3515
    public String getProviderName() {
3516
        if (this.provider != null) {
3517
            return this.provider.getProviderName();
3518
        }
3519
        if (this.parameters != null) {
3520
            return this.parameters.getDataStoreName();
3521
        }
3522
        return null;
3523

    
3524
    }
3525

    
3526
    @Override
3527
    public boolean isKnownEnvelope() {
3528
        return this.provider.isKnownEnvelope();
3529
    }
3530

    
3531
    @Override
3532
    public boolean hasRetrievedFeaturesLimit() {
3533
        return this.provider.hasRetrievedFeaturesLimit();
3534
    }
3535

    
3536
    @Override
3537
    public int getRetrievedFeaturesLimit() {
3538
        return this.provider.getRetrievedFeaturesLimit();
3539
    }
3540

    
3541
    @Override
3542
    public Interval getInterval() {
3543
        if (this.timeSupport != null) {
3544
            return this.timeSupport.getInterval();
3545
        }
3546
        try {
3547
            FeatureType type = this.getDefaultFeatureType();
3548
            FeatureAttributeDescriptor attr = type.getDefaultTimeAttribute();
3549
            if (attr != null) {
3550
                Interval interval = attr.getInterval();
3551
                if (interval != null) {
3552
                    return interval;
3553
                }
3554
            }
3555
        } catch (DataException ex) {
3556
        }
3557
        return this.provider.getInterval();
3558
    }
3559

    
3560
    @Override
3561
    public Collection getTimes() {
3562
        if (this.timeSupport != null) {
3563
            return this.timeSupport.getTimes();
3564
        }
3565
        return this.provider.getTimes();
3566
    }
3567

    
3568
    @Override
3569
    public Collection getTimes(Interval interval) {
3570
        if (this.timeSupport != null) {
3571
            return this.timeSupport.getTimes(interval);
3572
        }
3573
        return this.provider.getTimes(interval);
3574
    }
3575

    
3576
    public void setTimeSupport(FeatureStoreTimeSupport timeSupport) {
3577
        if (this.isEditing()) {
3578
            throw new RuntimeException("Can't add time support over attribute '" + timeSupport.getAttributeName() + "' while store is editing.");
3579
        }
3580
        if (!this.transforms.isEmpty()) {
3581
            throw new RuntimeException("Can't add time support over attribute '" + timeSupport.getAttributeName() + "' if has transforms.");
3582
        }
3583
        FeatureType ft = this.defaultFeatureType;
3584
        FeatureAttributeDescriptor attr = ft.getAttributeDescriptor(timeSupport.getRequiredFieldNames()[0]);
3585
        if (attr == null) {
3586
            throw new RuntimeException("Can't add time support over attribute '" + timeSupport.getAttributeName() + "', this attribute don't exists.");
3587
        }
3588
        EditableFeatureType eft = ft.getEditable();
3589
        attr = eft.getAttributeDescriptor(timeSupport.getAttributeName());
3590
        if (attr != null) {
3591
            if (!(attr.getFeatureAttributeEmulator() instanceof FeatureStoreTimeSupport)) {
3592
                throw new RuntimeException("Can't add time support, attribute '" + timeSupport.getAttributeName() + "'already exists.");
3593
            }
3594
            eft.remove(attr.getName());
3595
        }
3596
        EditableFeatureAttributeDescriptor attrTime = eft.add(
3597
                timeSupport.getAttributeName(),
3598
                timeSupport.getDataType()
3599
        );
3600
        attrTime.setIsTime(true);
3601
        attrTime.setFeatureAttributeEmulator(timeSupport);
3602
        eft.setDefaultTimeAttributeName(timeSupport.getAttributeName());
3603
        this.defaultFeatureType = eft.getNotEditableCopy();
3604

    
3605
        this.timeSupport = timeSupport;
3606
    }
3607

    
3608
    @Override
3609
    @SuppressWarnings("CloneDoesntCallSuperClone")
3610
    public Object clone() throws CloneNotSupportedException {
3611

    
3612
        DataStoreParameters dsp = getParameters();
3613

    
3614
        DefaultFeatureStore cloned_store = null;
3615

    
3616
        try {
3617
            cloned_store = (DefaultFeatureStore) DALLocator.getDataManager().
3618
                    openStore(this.getProviderName(), dsp);
3619
            if (transforms != null) {
3620
                cloned_store.transforms = (DefaultFeatureStoreTransforms) transforms.clone();
3621
                cloned_store.transforms.setStoreForClone(cloned_store);
3622
            }
3623
        } catch (Exception e) {
3624
            throw new CloneException(e);
3625
        }
3626
        return cloned_store;
3627

    
3628
    }
3629

    
3630
    @Override
3631
    public Feature getFeature(DynObject dynobject) {
3632
        if (dynobject instanceof DynObjectFeatureFacade) {
3633
            Feature f = ((DynObjectFeatureFacade) dynobject).getFeature();
3634
            return f;
3635
        }
3636
        return null;
3637
    }
3638

    
3639
    @Override
3640
    public Iterator iterator() {
3641
        FeatureSet fset = null;
3642
        try {
3643
            fset = this.getFeatureSet();
3644
            return fset.fastIterator();
3645
        } catch (DataException ex) {
3646
            throw new RuntimeException(ex);
3647
        } finally {
3648
            DisposeUtils.disposeQuietly(fset);
3649
        }
3650
    }
3651

    
3652
    @Override
3653
    public long size64() {
3654
        FeatureSet fset = null;
3655
        try {
3656
            fset = this.getFeatureSet();
3657
            return fset.getSize();
3658
        } catch (DataException ex) {
3659
            throw new RuntimeException(ex);
3660
        } finally {
3661
            DisposeUtils.disposeQuietly(fset);
3662
        }
3663
    }
3664

    
3665
    @Override
3666
    public ExpressionBuilder createExpressionBuilder() {
3667
        ExpressionBuilder builder = GeometryExpressionUtils.createExpressionBuilder();
3668
        return builder;
3669
    }
3670

    
3671
    @Override
3672
    public ExpressionBuilder createExpression() {
3673
        return createExpressionBuilder();
3674
    }
3675

    
3676
    public FeatureSet features() throws DataException {
3677
        // This is to avoid jython to create a property with this name
3678
        // to access method getFeatures.
3679
        return this.getFeatureSet();
3680
    }
3681

    
3682
    @Override
3683
    public DataStoreProviderFactory getProviderFactory() {
3684
        DataStoreProviderFactory factory = dataManager.getStoreProviderFactory(parameters.getDataStoreName());
3685
        return factory;
3686
    }
3687

    
3688
    @Override
3689
    public void useCache(String providerName, DynObject parameters) throws DataException {
3690
        throw new UnsupportedOperationException();
3691
    }
3692

    
3693
    @Override
3694
    public boolean isBroken() {
3695
        return this.state.isBroken();
3696
    }
3697

    
3698
    @Override
3699
    public Throwable getBreakingsCause() {
3700
        return this.state.getBreakingsCause();
3701
    }
3702

    
3703
    @Override
3704
    public SpatialIndex wrapSpatialIndex(SpatialIndex index) {
3705
        FeatureStoreProviderFactory factory = (FeatureStoreProviderFactory) this.getProviderFactory();
3706
        if (!factory.supportNumericOID()) {
3707
            return null;
3708
        }
3709
        SpatialIndex wrappedIndex = new WrappedSpatialIndex(index, this);
3710
        return wrappedIndex;
3711
    }
3712

    
3713
    @Override
3714
    public FeatureReference getFeatureReference(String code) {
3715
        FeatureReference featureReference = FeatureReferenceFactory.createFromCode(this, code);
3716
        return featureReference;
3717
    }
3718

    
3719
    @Override
3720
    public long getPendingChangesCount() {
3721
        if (this.featureManager == null) {
3722
            return 0;
3723
        }
3724
        return this.featureManager.getPendingChangesCount();
3725
    }
3726

    
3727
    private ResourcesStorage resourcesStorage;
3728

    
3729
    @Override
3730
    public ResourcesStorage getResourcesStorage() {
3731
        if (this.resourcesStorage != null) {
3732
            DisposeUtils.bind(this.resourcesStorage);
3733
            return this.resourcesStorage;
3734
        }
3735
        ResourcesStorage theResourcesStorage;
3736
        try {
3737
            theResourcesStorage = this.provider.getResourcesStorage();
3738
            if (theResourcesStorage != null) {
3739
                this.resourcesStorage = theResourcesStorage;
3740
                DisposeUtils.bind(this.resourcesStorage);
3741
                return theResourcesStorage;
3742
            }
3743
        } catch (Throwable th) {
3744

    
3745
        }
3746
        try {
3747
            DataServerExplorer explorer = this.getExplorer();
3748
            if (explorer == null) {
3749
                return null;
3750
            }
3751
            theResourcesStorage = explorer.getResourcesStorage(this);
3752
            explorer.dispose();
3753
            this.resourcesStorage = theResourcesStorage;
3754
            DisposeUtils.bind(this.resourcesStorage);
3755
            return theResourcesStorage;
3756
        } catch (Exception ex) {
3757
            LOGGER.trace("Can't create resources storage", ex);
3758
            return null;
3759
        }
3760
    }
3761

    
3762
    @Override
3763
    public StoresRepository getStoresRepository() {
3764
        final StoresRepository mainRepository = this.dataManager.getStoresRepository();
3765
        StoresRepository localRepository = this.provider.getStoresRepository();
3766
        if (localRepository == null) {
3767
            return mainRepository;
3768
        }
3769
        StoresRepository repository = new BaseStoresRepository(this.getName());
3770
        repository.addRepository(localRepository);
3771
        repository.addRepository(mainRepository);
3772
        return repository;
3773
    }
3774

    
3775
    @Override
3776
    public Feature getSampleFeature() {
3777
        if( sampleFeatureCache==null )  {
3778
            this.sampleFeatureCache = new CachedValue<Feature>(sample_feature_cache_timeout_ms) {
3779
                @Override
3780
                protected void reload() {
3781
                    Feature sampleFeature;
3782
                    long t1 = System.currentTimeMillis();
3783
                    try {                        
3784
                        FeatureSelection theSelection = getFeatureSelection();
3785
                        if (theSelection != null && !theSelection.isEmpty()) {
3786
                            sampleFeature = theSelection.first();
3787
                        } else {
3788
                            sampleFeature = first();
3789
                        }
3790
                        if (sampleFeature == null) {
3791
                            sampleFeature = createNewFeature();
3792
                        }
3793
                    } catch (DataException ex) {
3794
                        sampleFeature = null;
3795
                    }
3796
                    long t2 = System.currentTimeMillis();
3797
                    if( (t2 - t1)>5000 ) {
3798
                        // Mas de 5 seg es muy costoso, subimos mucho el tiempo de cache.
3799
                        this.setExpireTime(((60*60)*2)*1000); // 2h
3800
                    }
3801
                    this.setValue(sampleFeature);
3802
                }
3803
            };
3804
        }
3805
        return this.sampleFeatureCache.get();
3806
    }
3807

    
3808
    @Override
3809
    public boolean supportReferences() {
3810
        try {
3811
            return this.getDefaultFeatureType().supportReferences();
3812
        } catch (Exception ex) {
3813
            return false;
3814
        }
3815
    }
3816

    
3817
    private Boolean temporary = null;
3818
    
3819
    @Override
3820
    public boolean isTemporary() {
3821
        if(temporary != null) {
3822
            return this.temporary;
3823
        }
3824
        if (this.provider == null) {
3825
            return true;
3826
        }
3827
        return this.provider.isTemporary();
3828
    }
3829
    
3830
    @Override
3831
    public void setTemporary(Boolean temporary){
3832
        this.temporary = temporary;
3833
    }
3834

    
3835
    public FeatureType getOriginalFeatureType(FeatureType featureType) {
3836
        // FIXME this don't work for Store.fType.size() > 1
3837
        FeatureTypeManager manager = this.featureTypeManager;
3838
        if (manager == null) {
3839
            return null;
3840
        }
3841
        FeatureType originalFeatureType = manager.getOriginalFeatureType();
3842
        if (originalFeatureType == null) {
3843
            return null;
3844
        }
3845
        return originalFeatureType.getCopy();
3846
    }
3847

    
3848
    @Override
3849
    public Object getProperty(String name) {
3850
        if (this.propertiesSupportHelper == null) {
3851
            return null;
3852
        }
3853
        return this.propertiesSupportHelper.getProperty(name);
3854
    }
3855

    
3856
    @Override
3857
    public void setProperty(String name, Object value) {
3858
        if (this.propertiesSupportHelper == null) {
3859
            this.propertiesSupportHelper = new PropertiesSupportHelper();
3860
        }
3861
        this.propertiesSupportHelper.setProperty(name, value);
3862
    }
3863

    
3864
    @Override
3865
    public Map<String, Object> getProperties() {
3866
        if (this.propertiesSupportHelper == null) {
3867
            return Collections.EMPTY_MAP;
3868
        }
3869
        return this.propertiesSupportHelper.getProperties();
3870
    }
3871

    
3872
    @Override
3873
    public Feature getOriginalFeature(FeatureReference id) {
3874
        if (this.featureManager == null) {
3875
            return null;
3876
        }
3877
        return featureManager.getOriginal(id);
3878
    }
3879

    
3880
    @Override
3881
    public Feature getOriginalFeature(Feature feature) {
3882
        if (feature == null) {
3883
            return null;
3884
        }
3885
        return getOriginalFeature(feature.getReference());
3886
    }
3887

    
3888
    @Override
3889
    public boolean isFeatureModified(FeatureReference id) {
3890
        if (this.featureManager == null) {
3891
            return false;
3892
        }
3893
        return featureManager.isFeatureModified(id);
3894
    }
3895

    
3896
    @Override
3897
    public boolean isFeatureModified(Feature feature) {
3898
        if (feature == null) {
3899
            return false;
3900
        }
3901
        return isFeatureModified(feature.getReference());
3902
    }
3903

    
3904
    @Override
3905
    public void setTransaction(DataTransaction transaction) {
3906
        if( this.transaction!=null ) {
3907
            this.transaction.deleteObserver(transactionObserver);
3908
        }
3909
        this.transaction = transaction;
3910
        if (transaction instanceof DataTransactionServices) {
3911
            this.provider.setTransaction((DataTransactionServices) transaction);
3912
        }
3913
        if( transaction!=null ) {
3914
            transaction.addObserver(transactionObserver);
3915
        }
3916
    }
3917

    
3918
    @Override
3919
    public DataTransaction getTransaction() {
3920
        return transaction;
3921
    }
3922

    
3923
    @Override
3924
    public String toString() {
3925
        try {
3926
            return String.format("%s %x %s", this.getClass().getSimpleName(), this.hashCode(), this.getFullName());
3927
        } catch (Exception e) {
3928
            return super.toString();
3929
        }
3930
    }
3931

    
3932
    public String createUniqueID() {
3933
        UUID x = UUID.randomUUID();
3934
        String s = x.toString();
3935
        return s;
3936
    }
3937

    
3938
    @Override
3939
    public List<FeatureReference> getEditedFeatures() {
3940
        if( this.featureManager == null ) {
3941
            return Collections.EMPTY_LIST;
3942
        }
3943
        List<FeatureReference> references = this.featureManager.getAddedAndUpdatedFeatures();
3944
        if( references==null ) {
3945
            return Collections.EMPTY_LIST;
3946
        }
3947
        return references;
3948
    }
3949
    
3950
    public List<FeatureReference> getEditedFeaturesNotValidated() {
3951

    
3952
        try {
3953
            if (this.featureManager == null) {
3954
                return Collections.EMPTY_LIST;
3955
            }
3956
            
3957
            FeatureType type = this.getDefaultFeatureTypeQuietly();
3958
            DefaultFeatureRules rules = (DefaultFeatureRules) this.getDefaultFeatureType().getRules();
3959
            
3960
//            int checks = type.isCheckFeaturesAtFinishEditing() ? CHECK_REQUIREDS | CHECK_BASIC : 0;
3961
            int checks = CHECK_REQUIREDS | CHECK_BASIC;
3962
            if(type.isCheckFeaturesAtFinishEditing()){
3963
                checks = rules.isEmpty() ? checks : checks | CHECK_RULES_AT_FINISH;
3964
            }
3965
            if (checks == 0) {
3966
                return Collections.EMPTY_LIST;
3967
            }
3968
            List<FeatureReference> references = this.featureManager
3969
                    .getAddedAndUpdatedFeaturesNotValidated(rules, checks);
3970
            if (references == null) {
3971
                return Collections.EMPTY_LIST;
3972
            }
3973
            return references;
3974
        } catch (DataException ex) {
3975
            return null;
3976
        }
3977

    
3978
    }
3979

    
3980
    @Override
3981
    public Iterator<Feature> getFeaturesIterator(Iterator<FeatureReference> references) {
3982
        return new FeatureReferenceIteratorToFeatureIterator(this, references);
3983
    }
3984

    
3985
    @Override
3986
    public Iterable<Feature> getFeaturesIterable(Iterator<FeatureReference> references) {
3987
        return () -> new FeatureReferenceIteratorToFeatureIterator(this, references);
3988
    }
3989

    
3990
    @Override
3991
    public boolean isFeatureSelectionAvailable() {
3992
        try {
3993
            FeatureType type = this.getDefaultFeatureType();
3994
            return type.supportReferences();
3995
        } catch (DataException ex) {
3996
            return false;
3997
        }
3998
    }
3999
}