Statistics
| Revision:

root / trunk / libraries / libDataSource / src / org / gvsig / data / vectorial / AbstractFeatureStore.java @ 19399

History | View | Annotate | Download (11.4 KB)

1
package org.gvsig.data.vectorial;
2

    
3
import java.util.Iterator;
4

    
5
import org.gvsig.data.ComplexObservable;
6
import org.gvsig.data.IDataCollection;
7
import org.gvsig.data.IDataStoreParameters;
8
import org.gvsig.data.IObserver;
9
import org.gvsig.data.commands.ICommand;
10
import org.gvsig.data.commands.ICommandsRecord;
11
import org.gvsig.data.commands.implementation.FeatureCommandsRecord;
12
import org.gvsig.data.vectorial.expansionadapter.AttributeManager;
13
import org.gvsig.data.vectorial.expansionadapter.FeatureManager;
14
import org.gvsig.data.vectorial.expansionadapter.IExpansionAdapter;
15
import org.gvsig.data.vectorial.expansionadapter.MemoryExpansionAdapter;
16

    
17

    
18
public abstract class AbstractFeatureStore implements IFeatureStore {
19

    
20
        protected IDataStoreParameters parameters;
21
        protected IDataCollection selection;
22
        protected ICommandsRecord commands;
23
        protected boolean alterMode = false;
24
        protected ComplexObservable observable=new ComplexObservable();
25
        protected IDataCollection locked;
26

    
27
        protected FeatureManager featureManager;
28
        protected SpatialManager spatialManager;
29
        protected AttributeManager attributeManager;
30

    
31

    
32
        public void init(IDataStoreParameters parameters) {
33
                this.parameters = parameters;
34
                initExpansionManager();
35
                initSpatialManager();
36

    
37
                initAttributeManager();
38

    
39
                commands = new FeatureCommandsRecord(featureManager, spatialManager, attributeManager);
40
        }
41

    
42
        protected void initSpatialManager(){
43
                spatialManager=new SpatialManager();
44
        }
45

    
46
        protected void initAttributeManager() {
47
                IExpansionAdapter expansionAttributeAdapter=new MemoryExpansionAdapter();
48
                attributeManager=new AttributeManager(expansionAttributeAdapter);
49

    
50
        }
51

    
52
        protected void initExpansionManager() {
53
                IExpansionAdapter expansionFeatureAdapter=new MemoryExpansionAdapter();
54
                featureManager=new FeatureManager(expansionFeatureAdapter);
55
        }
56

    
57
        public IDataCollection getDataCollection() {
58
                return getDataCollection(null, null, null);
59
        }
60

    
61
        public void getDataCollection(IObserver observer) {
62
                getDataCollection(null, null, null,observer);
63
        }
64

    
65
        public void setSelection(IDataCollection selection) {
66
                this.observable.notifyObservers(
67
                                new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_SELECTIONCHANGE)
68
                        );
69
                this.selection = selection;
70
                this.observable.notifyObservers(
71
                                new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_SELECTIONCHANGE)
72
                        );
73

    
74
        }
75

    
76
        public IDataCollection createSelection() {
77
                MemoryFeatureCollection selection = new MemoryFeatureCollection();
78
                return selection;
79
        }
80

    
81
        public IDataCollection getSelection() {
82
                if( selection == null ){
83
                        selection = createSelection();
84
                }
85
                return selection;
86
        }
87

    
88
        public IDataCollection createLocked() {
89
                MemoryFeatureCollection locked = new MemoryFeatureCollection();
90
                return locked;
91
        }
92

    
93
        public IDataCollection getLocked() {
94
                if( locked == null ){
95
                        locked = createLocked();
96
                }
97
                return locked;
98
        }
99

    
100
        public void setLocked(IDataCollection locked) {
101
                this.observable.notifyObservers(
102
                                new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_LOCKEDCHANGE)
103
                        );
104
                this.locked = locked;
105
                this.observable.notifyObservers(
106
                                new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_LOCKEDCHANGE)
107
                        );
108

    
109
        }
110
        public boolean isLocked(IFeatureID id) {
111
        return locked.contains(id);
112
    }
113

    
114
    public boolean lock(IFeatureID id) {
115
            this.observable.notifyObservers(
116
                                new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_LOCKEDCHANGE)
117
                        );
118
                locked.add(id);
119
                this.observable.notifyObservers(
120
                                new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_LOCKEDCHANGE)
121
                        );
122
                return true;
123
    }
124

    
125

    
126
        public void startEditing() {
127
                this.observable.notifyObservers(
128
                                new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_STARTEDITING)
129
                        );
130
                commands.clear();
131
                alterMode = true;
132
                this.observable.notifyObservers(
133
                                new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_STARTEDITING)
134
                        );
135

    
136
        }
137

    
138

    
139
        public void delete(IFeatureAttributeDescriptor attributeDescriptor) {
140
                if( !alterMode ) {
141
                        throw new RuntimeException("alterMode is false");
142
                }
143
                this.observable.notifyObservers(
144
                                new AttributeStoreNotification(this,AttributeStoreNotification.BEFORE_DELETE_ATTRIBUTE,attributeDescriptor)
145
                        );
146
                commands.delete(attributeDescriptor);
147
                this.observable.notifyObservers(
148
                                new AttributeStoreNotification(this,AttributeStoreNotification.AFTER_DELETE_ATTRIBUTE,attributeDescriptor)
149
                        );
150

    
151

    
152
        }
153

    
154
        public void insert(IFeatureAttributeDescriptor attributeDescriptor) {
155
                if( !alterMode ) {
156
                        throw new RuntimeException("alterMode is false");
157
                }
158
//                attributeDescriptor.validateModification(this);
159
                this.observable.notifyObservers(
160
                                new AttributeStoreNotification(this,AttributeStoreNotification.BEFORE_INSERT_ATTRIBUTE,attributeDescriptor)
161
                        );
162
                commands.insert(attributeDescriptor);
163
                this.observable.notifyObservers(
164
                                new AttributeStoreNotification(this,FeatureStoreNotification.AFTER_INSERT,attributeDescriptor)
165
                        );
166

    
167
        }
168

    
169
        public void update(IFeatureAttributeDescriptor attributeDescriptor, IFeatureAttributeDescriptor oldAttributeDescriptor) {
170
                if( !alterMode ) {
171
                        throw new RuntimeException("alterMode is false");
172
                }
173
//                feature.validateModification(this);
174
                this.observable.notifyObservers(
175
                                new AttributeStoreNotification(this,AttributeStoreNotification.BEFORE_UPDATE_ATTRIBUTE,attributeDescriptor)
176
                        );
177
                commands.update(attributeDescriptor,oldAttributeDescriptor);
178
                this.observable.notifyObservers(
179
                                new AttributeStoreNotification(this,AttributeStoreNotification.AFTER_UPDATE_ATTRIBUTE,attributeDescriptor)
180
                        );
181
        }
182

    
183

    
184
        public void delete(IFeature feature) {
185
                if( !alterMode ) {
186
                        throw new RuntimeException("alterMode is false");
187
                }
188
                this.observable.notifyObservers(
189
                                new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_DELETE,feature)
190
                        );
191
                commands.delete(feature);
192
                this.observable.notifyObservers(
193
                                new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_DELETE,feature)
194
                        );
195

    
196

    
197
        }
198

    
199
        public void insert(IFeature feature) {
200
                if( !alterMode ) {
201
                        throw new RuntimeException("alterMode is false");
202
                }
203
                feature.validateModification(this);
204
                this.observable.notifyObservers(
205
                                new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_INSERT,feature)
206
                        );
207
                commands.insert(feature);
208
                this.observable.notifyObservers(
209
                                new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_INSERT,feature)
210
                        );
211

    
212
        }
213

    
214
        public void update(IFeature feature, IFeature oldFeature) {
215
                if( !alterMode ) {
216
                        throw new RuntimeException("alterMode is false");
217
                }
218
                feature.validateModification(this);
219
                this.observable.notifyObservers(
220
                                new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_UPDATE,feature)
221
                        );
222
                commands.update(feature,oldFeature);
223
                this.observable.notifyObservers(
224
                                new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_UPDATE,feature)
225
                        );
226
        }
227

    
228
        public void redo() {
229
                if( !alterMode ) {
230
                        throw new RuntimeException("alterMode is false");
231
                }
232
                ICommand redo = commands.getNextRedoCommand();
233
                this.observable.notifyObservers(
234
                        new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_REDO,redo)
235
                );
236

    
237
                commands.redo();
238

    
239
                this.observable.notifyObservers(
240
                                new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_REDO,redo)
241
                        );
242

    
243
        }
244

    
245
        public void undo() {
246
                if( !alterMode ) {
247
                        throw new RuntimeException("alterMode is false");
248
                }
249
                ICommand undo = commands.getNextUndoCommand();
250
                this.observable.notifyObservers(
251
                        new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_UNDO,undo)
252
                );
253

    
254
                commands.undo();
255

    
256
                this.observable.notifyObservers(
257
                                new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_UNDO,undo)
258
                        );
259

    
260
        }
261

    
262
        public ICommandsRecord getCommandsRecord() {
263
                if( !alterMode ) {
264
                        throw new RuntimeException("alterMode is false");
265
                }
266
                return commands;
267
        }
268

    
269
        public void cancelEditing() {
270
                if( !alterMode ) {
271
                        throw new RuntimeException("alterMode is false");
272
                }
273
                this.observable.notifyObservers(
274
                        new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_CANCELEDITING)
275
                );
276

    
277
                commands.clear();
278
                alterMode = false;
279

    
280
                this.observable.notifyObservers(
281
                        new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_CANCELEDITING)
282
                );
283

    
284
        }
285

    
286
        public void finishEditing() {
287
                if( !alterMode ) {
288
                        throw new RuntimeException("alterMode is false");
289
                }
290

    
291
                this.observable.notifyObservers(
292
                        new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_FINISHEDITING)
293
                );
294

    
295
                this.validateEndEditing();
296

    
297

    
298
                commands.clear();
299
                alterMode = false;
300

    
301
                this.observable.notifyObservers(
302
                        new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_FINISHEDITING)
303
                );
304

    
305
        }
306

    
307

    
308
        public Iterator getChilds() {
309
                return null;
310
        }
311

    
312
        public void beginComplexNotification() {
313
                this.observable.beginComplexNotification();
314

    
315
        }
316

    
317
        public void endComplexNotification() {
318
                this.observable.endComplexNotification();
319

    
320
        }
321

    
322
        public void addObserver(IObserver o) {
323
                this.observable.addObserver(o);
324

    
325
        }
326

    
327
        public void deleteObserver(IObserver o) {
328
                this.observable.deleteObserver(o);
329

    
330
        }
331

    
332
        public void deleteObservers() {
333
                this.observable.deleteObservers();
334

    
335
        }
336

    
337
        public boolean isEditing() {
338
                return alterMode;
339
        }
340

    
341
        protected void validateEndEditing(){
342
                IFeatureCollection collection = (IFeatureCollection)this.getDataCollection();
343
                Iterator iter = collection.iterator();
344
                while (iter.hasNext()){
345
                        ((IFeature)iter.next()).validateEnd(this);
346
                }
347

    
348
        }
349

    
350
        public void disableNotifications() {
351
                this.observable.diableNotifications();
352

    
353
        }
354

    
355
        public void enableNotifications() {
356
                this.observable.enableNotifications();
357
        }
358
        public void getDataCollection(IFeatureType type, String filter, String order,IObserver observer) {
359
            LoadInBackGround task = new LoadInBackGround(this,type,filter,order,observer);
360
            Thread thread = new Thread(task);
361
            thread.run();
362
    }
363
         private class LoadInBackGround implements Runnable{
364

    
365
                    private IFeatureStore store;
366
                    private IFeatureType type;
367
                    private String filter;
368
                    private String order;
369

    
370
                    private IObserver observer;
371

    
372
                        public IDataCollection collection = null;
373
                    public LoadInBackGround(IFeatureStore store,IFeatureType type, String filter, String order,IObserver observer){
374
                            this.store = store;
375
                            this.type = type;
376
                            this.filter = filter;
377
                            this.order = order;
378
                            this.observer = observer;
379

    
380
                    }
381

    
382
                        public void run() {
383
                                try{
384
                                        collection = getDataCollection(type, filter, order);
385
                                } catch (Exception e) {
386
                                        this.observer.update(this.store, new FeatureStoreNotification(
387
                                                                        this.store,
388
                                                                        IFeatureStoreNotification.LOAD_FINISHED,
389
                                                                        e)
390
                                                        );
391
                                }
392
                                this.observer.update(
393
                                                this.store, new FeatureStoreNotification(
394
                                                                        this.store,
395
                                                                        IFeatureStoreNotification.LOAD_FINISHED,
396
                                                                        collection)
397
                                                );
398

    
399
                        }
400

    
401

    
402
            }
403
         public IFeature createFeature(IFeatureType type) {
404
                 IFeature feature=new MemoryFeature(type,true);
405
                 return feature;
406
         }
407

    
408
         public IFeature createDefaultFeature(boolean defaultValues) {
409
                 IFeature feature=new MemoryFeature(getDefaultFeatureType(),defaultValues);
410
                 return feature;
411
         }
412

    
413
}