root / trunk / libraries / libDataSource / src / org / gvsig / data / vectorial / AbstractFeatureStore.java @ 20599
History | View | Annotate | Download (14.2 KB)
1 |
package org.gvsig.data.vectorial; |
---|---|
2 |
|
3 |
import java.lang.ref.Reference; |
4 |
import java.util.Iterator; |
5 |
|
6 |
import org.gvsig.data.IDataCollection; |
7 |
import org.gvsig.data.IDataStoreParameters; |
8 |
import org.gvsig.data.commands.ICommand; |
9 |
import org.gvsig.data.commands.ICommandsRecord; |
10 |
import org.gvsig.data.commands.implementation.FeatureCommandsRecord; |
11 |
import org.gvsig.data.exception.CloseException; |
12 |
import org.gvsig.data.exception.InitializeException; |
13 |
import org.gvsig.data.exception.OpenException; |
14 |
import org.gvsig.data.exception.ReadException; |
15 |
import org.gvsig.data.exception.WriteException; |
16 |
import org.gvsig.data.vectorial.expansionadapter.IExpansionAdapter; |
17 |
import org.gvsig.data.vectorial.expansionadapter.MemoryExpansionAdapter; |
18 |
import org.gvsig.util.observer.ComplexNotification; |
19 |
import org.gvsig.util.observer.ComplexObservable; |
20 |
import org.gvsig.util.observer.IObservable; |
21 |
import org.gvsig.util.observer.IObserver; |
22 |
|
23 |
|
24 |
public abstract class AbstractFeatureStore implements IFeatureStore { |
25 |
|
26 |
protected IDataStoreParameters parameters;
|
27 |
protected IDataCollection selection;
|
28 |
protected ICommandsRecord commands;
|
29 |
protected boolean alterMode = false; |
30 |
protected ComplexObservable observable=new ComplexObservable(); |
31 |
protected IDataCollection locked;
|
32 |
|
33 |
protected FeatureManager featureManager;
|
34 |
protected SpatialManager spatialManager;
|
35 |
protected AttributeManager attributeManager;
|
36 |
|
37 |
|
38 |
public void init(IDataStoreParameters parameters) throws InitializeException { |
39 |
this.parameters = parameters;
|
40 |
|
41 |
} |
42 |
|
43 |
protected void initSpatialManager(){ |
44 |
spatialManager=new SpatialManager();
|
45 |
} |
46 |
|
47 |
protected void initAttributeManager() { |
48 |
IExpansionAdapter expansionAttributeAdapter=new MemoryExpansionAdapter();
|
49 |
attributeManager=new AttributeManager(expansionAttributeAdapter,getDefaultFeatureType());
|
50 |
|
51 |
} |
52 |
|
53 |
protected void initExpansionManager() { |
54 |
IExpansionAdapter expansionFeatureAdapter=new MemoryExpansionAdapter();
|
55 |
featureManager=new FeatureManager(expansionFeatureAdapter);
|
56 |
} |
57 |
|
58 |
public IDataCollection getDataCollection() throws ReadException { |
59 |
return getDataCollection(null, null, null); |
60 |
} |
61 |
|
62 |
public void getDataCollection(IObserver observer) { |
63 |
getDataCollection(null, null, null,observer); |
64 |
} |
65 |
|
66 |
public void setSelection(IDataCollection selection) { |
67 |
this.observable.notifyObservers(this, |
68 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_SELECTIONCHANGE) |
69 |
); |
70 |
this.selection = selection;
|
71 |
this.observable.notifyObservers(this, |
72 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_SELECTIONCHANGE) |
73 |
); |
74 |
|
75 |
} |
76 |
|
77 |
public IDataCollection createSelection() {
|
78 |
|
79 |
MemoryFeatureCollection selection = new MemoryFeatureCollection();
|
80 |
return selection;
|
81 |
} |
82 |
|
83 |
public IDataCollection getSelection() {
|
84 |
if( selection == null ){ |
85 |
selection = createSelection(); |
86 |
} |
87 |
return selection;
|
88 |
} |
89 |
|
90 |
public IDataCollection createLocked() {
|
91 |
MemoryFeatureCollection locked = new MemoryFeatureCollection();
|
92 |
return locked;
|
93 |
} |
94 |
|
95 |
public IDataCollection getLocked() {
|
96 |
if( locked == null ){ |
97 |
locked = createLocked(); |
98 |
} |
99 |
return locked;
|
100 |
} |
101 |
|
102 |
public void setLocked(IDataCollection locked) { |
103 |
this.observable.notifyObservers(this, |
104 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_LOCKEDCHANGE) |
105 |
); |
106 |
this.locked = locked;
|
107 |
this.observable.notifyObservers(this, |
108 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_LOCKEDCHANGE) |
109 |
); |
110 |
|
111 |
} |
112 |
public boolean isLocked(IFeatureID id) { |
113 |
return locked.contains(id);
|
114 |
} |
115 |
|
116 |
public boolean lock(IFeatureID id) { |
117 |
this.observable.notifyObservers(this, |
118 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_LOCKEDCHANGE) |
119 |
); |
120 |
locked.add(id); |
121 |
this.observable.notifyObservers(this, |
122 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_LOCKEDCHANGE) |
123 |
); |
124 |
return true; |
125 |
} |
126 |
|
127 |
|
128 |
public void startEditing() throws ReadException { |
129 |
if (!this.isEditable()) { |
130 |
throw new ReadException("AbstractFeatureStore", |
131 |
new UnsupportedOperationException("Not editable")); |
132 |
} |
133 |
this.observable.notifyObservers(this, |
134 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_STARTEDITING) |
135 |
); |
136 |
// commands.clear();
|
137 |
initExpansionManager(); |
138 |
initSpatialManager(); |
139 |
initAttributeManager(); |
140 |
commands = new FeatureCommandsRecord(featureManager, spatialManager, attributeManager);
|
141 |
|
142 |
|
143 |
alterMode = true;
|
144 |
this.observable.notifyObservers(this, |
145 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_STARTEDITING) |
146 |
); |
147 |
|
148 |
} |
149 |
|
150 |
|
151 |
public void delete(IFeatureAttributeDescriptor attributeDescriptor) { |
152 |
if( !alterMode ) {
|
153 |
throw new RuntimeException("alterMode is false"); |
154 |
} |
155 |
this.observable.notifyObservers(this, |
156 |
new AttributeStoreNotification(this,AttributeStoreNotification.BEFORE_DELETE_ATTRIBUTE,attributeDescriptor) |
157 |
); |
158 |
commands.delete(attributeDescriptor); |
159 |
this.observable.notifyObservers(this, |
160 |
new AttributeStoreNotification(this,AttributeStoreNotification.AFTER_DELETE_ATTRIBUTE,attributeDescriptor) |
161 |
); |
162 |
|
163 |
|
164 |
} |
165 |
|
166 |
public void insert(IFeatureAttributeDescriptor attributeDescriptor) { |
167 |
if( !alterMode ) {
|
168 |
throw new RuntimeException("alterMode is false"); |
169 |
} |
170 |
// attributeDescriptor.validateModification(this);
|
171 |
this.observable.notifyObservers(this, |
172 |
new AttributeStoreNotification(this,AttributeStoreNotification.BEFORE_INSERT_ATTRIBUTE,attributeDescriptor) |
173 |
); |
174 |
commands.insert(attributeDescriptor); |
175 |
this.observable.notifyObservers(this, |
176 |
new AttributeStoreNotification(this,FeatureStoreNotification.AFTER_INSERT,attributeDescriptor) |
177 |
); |
178 |
|
179 |
} |
180 |
|
181 |
public void update(IFeatureAttributeDescriptor attributeDescriptor) { |
182 |
if( !alterMode ) {
|
183 |
throw new RuntimeException("alterMode is false"); |
184 |
} |
185 |
// feature.validateModification(this);
|
186 |
this.observable.notifyObservers(this, |
187 |
new AttributeStoreNotification(this,AttributeStoreNotification.BEFORE_UPDATE_ATTRIBUTE,attributeDescriptor) |
188 |
); |
189 |
commands.update(((DefaultAttributeDescriptor)attributeDescriptor).getNewAttributeDescriptor(),attributeDescriptor); |
190 |
((DefaultAttributeDescriptor)attributeDescriptor).stopEditing(); |
191 |
this.observable.notifyObservers(this, |
192 |
new AttributeStoreNotification(this,AttributeStoreNotification.AFTER_UPDATE_ATTRIBUTE,attributeDescriptor) |
193 |
); |
194 |
} |
195 |
|
196 |
|
197 |
public void delete(IFeature feature) { |
198 |
if( !alterMode ) {
|
199 |
throw new RuntimeException("alterMode is false"); |
200 |
} |
201 |
this.observable.notifyObservers(this, |
202 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_DELETE,feature) |
203 |
); |
204 |
commands.delete(feature); |
205 |
this.observable.notifyObservers(this, |
206 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_DELETE,feature) |
207 |
); |
208 |
|
209 |
|
210 |
} |
211 |
|
212 |
public void insert(IFeature feature) { |
213 |
if( !alterMode ) {
|
214 |
throw new RuntimeException("alterMode is false"); |
215 |
} |
216 |
feature.validateModification(this);
|
217 |
this.observable.notifyObservers(this, |
218 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_INSERT,feature) |
219 |
); |
220 |
commands.insert(((Feature)feature).getNewFeature()); |
221 |
this.observable.notifyObservers(this, |
222 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_INSERT,feature) |
223 |
); |
224 |
|
225 |
} |
226 |
|
227 |
public void update(IFeature feature) { |
228 |
if( !alterMode ) {
|
229 |
throw new RuntimeException("alterMode is false"); |
230 |
} |
231 |
feature.validateModification(this);
|
232 |
this.observable.notifyObservers(this, |
233 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_UPDATE,feature) |
234 |
); |
235 |
Feature oldFeature = (Feature)feature; |
236 |
IFeature newFeature=oldFeature.getNewFeature(); |
237 |
oldFeature.stopEditing(); |
238 |
commands.update(newFeature,oldFeature); |
239 |
|
240 |
this.observable.notifyObservers(this, |
241 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_UPDATE,feature) |
242 |
); |
243 |
} |
244 |
|
245 |
public void redo() { |
246 |
if( !alterMode ) {
|
247 |
throw new RuntimeException("alterMode is false"); |
248 |
} |
249 |
ICommand redo = commands.getNextRedoCommand(); |
250 |
this.observable.notifyObservers(this, |
251 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_REDO,redo) |
252 |
); |
253 |
|
254 |
commands.redo(); |
255 |
|
256 |
this.observable.notifyObservers(this, |
257 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_REDO,redo) |
258 |
); |
259 |
|
260 |
} |
261 |
|
262 |
|
263 |
public void undo() { |
264 |
if( !alterMode ) {
|
265 |
throw new RuntimeException("alterMode is false"); |
266 |
} |
267 |
ICommand undo = commands.getNextUndoCommand(); |
268 |
this.observable.notifyObservers(this, |
269 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_UNDO,undo) |
270 |
); |
271 |
|
272 |
commands.undo(); |
273 |
|
274 |
this.observable.notifyObservers(this, |
275 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_UNDO,undo) |
276 |
); |
277 |
|
278 |
} |
279 |
|
280 |
public ICommandsRecord getCommandsRecord() {
|
281 |
if( !alterMode ) {
|
282 |
throw new RuntimeException("alterMode is false"); |
283 |
} |
284 |
return commands;
|
285 |
} |
286 |
|
287 |
protected void doCancelEditing(){ |
288 |
|
289 |
} |
290 |
|
291 |
public final void cancelEditing() { |
292 |
if( !alterMode ) {
|
293 |
throw new RuntimeException("alterMode is false"); |
294 |
} |
295 |
this.observable.notifyObservers(this, |
296 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_CANCELEDITING) |
297 |
); |
298 |
|
299 |
doCancelEditing(); |
300 |
|
301 |
commands.clear(); |
302 |
alterMode = false;
|
303 |
|
304 |
this.observable.notifyObservers(this, |
305 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_CANCELEDITING) |
306 |
); |
307 |
|
308 |
} |
309 |
|
310 |
public final void finishEditing() throws WriteException, ReadException{ |
311 |
if( !alterMode ) {
|
312 |
//FIXME: OJO arreglar esta excepci?n!!!
|
313 |
throw new RuntimeException("alterMode is false"); |
314 |
} |
315 |
|
316 |
this.observable.notifyObservers(this, |
317 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_FINISHEDITING) |
318 |
); |
319 |
|
320 |
this.validateEndEditing();
|
321 |
|
322 |
doFinishEdition(); |
323 |
|
324 |
|
325 |
commands.clear(); |
326 |
featureManager=null;
|
327 |
spatialManager=null;
|
328 |
attributeManager=null;
|
329 |
commands = null;
|
330 |
|
331 |
alterMode = false;
|
332 |
|
333 |
this.observable.notifyObservers(this, |
334 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_FINISHEDITING) |
335 |
); |
336 |
|
337 |
} |
338 |
|
339 |
protected abstract void doFinishEdition() throws WriteException, ReadException ; |
340 |
|
341 |
|
342 |
public Iterator getChilds() { |
343 |
return null; |
344 |
} |
345 |
|
346 |
public void beginComplexNotification() { |
347 |
this.observable.beginComplexNotification(new ComplexNotification()); |
348 |
|
349 |
} |
350 |
|
351 |
public void endComplexNotification() { |
352 |
this.observable.endComplexNotification(this); |
353 |
|
354 |
} |
355 |
|
356 |
public void addObserver(IObserver o) { |
357 |
this.observable.addObserver(o);
|
358 |
|
359 |
} |
360 |
|
361 |
public void addObserver(Reference ref) { |
362 |
this.observable.addObserver(ref);
|
363 |
} |
364 |
|
365 |
public void deleteObserver(IObserver o) { |
366 |
this.observable.deleteObserver(o);
|
367 |
} |
368 |
|
369 |
public void deleteObserver(Reference ref) { |
370 |
this.observable.deleteObserver(ref);
|
371 |
} |
372 |
|
373 |
public void deleteObservers() { |
374 |
this.observable.deleteObservers();
|
375 |
|
376 |
} |
377 |
|
378 |
public boolean isEditing() { |
379 |
return alterMode;
|
380 |
} |
381 |
|
382 |
protected void validateEndEditing() throws ReadException{ |
383 |
IFeatureCollection collection = (IFeatureCollection)this.getDataCollection();
|
384 |
Iterator iter = collection.iterator();
|
385 |
while (iter.hasNext()){
|
386 |
((IFeature)iter.next()).validateEnd(this);
|
387 |
} |
388 |
|
389 |
} |
390 |
|
391 |
public void disableNotifications() { |
392 |
this.observable.diableNotifications();
|
393 |
|
394 |
} |
395 |
|
396 |
public void enableNotifications() { |
397 |
this.observable.enableNotifications();
|
398 |
} |
399 |
public void getDataCollection(IFeatureType type, String filter, String order,IObserver observer) { |
400 |
LoadInBackGround task = new LoadInBackGround(this,type,filter,order,observer); |
401 |
Thread thread = new Thread(task); |
402 |
thread.run(); |
403 |
} |
404 |
private class LoadInBackGround implements Runnable{ |
405 |
|
406 |
private IFeatureStore store;
|
407 |
private IFeatureType type;
|
408 |
private String filter; |
409 |
private String order; |
410 |
|
411 |
private IObserver observer;
|
412 |
|
413 |
public IDataCollection collection = null; |
414 |
public LoadInBackGround(IFeatureStore store,IFeatureType type, String filter, String order,IObserver observer){ |
415 |
this.store = store;
|
416 |
this.type = type;
|
417 |
this.filter = filter;
|
418 |
this.order = order;
|
419 |
this.observer = observer;
|
420 |
|
421 |
} |
422 |
|
423 |
public void run() { |
424 |
try{
|
425 |
collection = getDataCollection(type, filter, order); |
426 |
} catch (Exception e) { |
427 |
this.observer.update(this.store, new FeatureStoreNotification( |
428 |
this.store,
|
429 |
IFeatureStoreNotification.LOAD_FINISHED, |
430 |
e) |
431 |
); |
432 |
} |
433 |
this.observer.update(
|
434 |
this.store, new FeatureStoreNotification( |
435 |
this.store,
|
436 |
IFeatureStoreNotification.LOAD_FINISHED, |
437 |
collection) |
438 |
); |
439 |
|
440 |
} |
441 |
|
442 |
|
443 |
} |
444 |
public IFeature createFeature(IFeatureType type) throws InitializeException { |
445 |
return this.createDefaultFeature(true); |
446 |
} |
447 |
|
448 |
public IFeature createDefaultFeature(boolean defaultValues) throws InitializeException{ |
449 |
IFeature feature; |
450 |
try {
|
451 |
feature = new CreatedFeature(getDefaultFeatureType(),defaultValues);
|
452 |
} catch (ReadException e) {
|
453 |
throw new InitializeException(this.getName(),e); |
454 |
} |
455 |
return feature;
|
456 |
} |
457 |
|
458 |
public IDataStoreParameters getParameters() {
|
459 |
return parameters;
|
460 |
} |
461 |
//
|
462 |
// protected abstract void doOpen() throws OpenException;
|
463 |
//
|
464 |
// protected abstract void doClose() throws CloseException;
|
465 |
//
|
466 |
// protected abstract void doDispose();
|
467 |
//
|
468 |
// public final void open() throws OpenException {
|
469 |
// this.observable.notifyObservers(this,
|
470 |
// new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_OPEN)
|
471 |
// );
|
472 |
// doOpen();
|
473 |
// this.observable.notifyObservers(this,
|
474 |
// new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_OPEN)
|
475 |
// );
|
476 |
//
|
477 |
// }
|
478 |
//
|
479 |
// public final void close() throws CloseException {
|
480 |
// this.observable.notifyObservers(this,
|
481 |
// new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_CLOSE)
|
482 |
// );
|
483 |
// doClose();
|
484 |
// this.observable.notifyObservers(this,
|
485 |
// new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_CLOSE)
|
486 |
// );
|
487 |
//
|
488 |
// }
|
489 |
//
|
490 |
// public final void dispose() {
|
491 |
// this.observable.notifyObservers(this,
|
492 |
// new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_DISPOSE)
|
493 |
// );
|
494 |
// doDispose();
|
495 |
// this.observable.notifyObservers(this,
|
496 |
// new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_DISPOSE)
|
497 |
// );
|
498 |
// }
|
499 |
|
500 |
|
501 |
} |