gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.fmap.dal.raster / org.gvsig.fmap.dal.raster.impl / src / main / java / org / gvsig / fmap / dal / raster / impl / DefaultRasterStore.java @ 6069
History | View | Annotate | Download (16.5 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2016 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.fmap.dal.raster.impl; |
24 |
|
25 |
import java.util.Collection; |
26 |
import java.util.Iterator; |
27 |
import java.util.Set; |
28 |
|
29 |
import org.gvsig.fmap.dal.DALLocator; |
30 |
import org.gvsig.fmap.dal.DataManager; |
31 |
import org.gvsig.fmap.dal.DataQuery; |
32 |
import org.gvsig.fmap.dal.DataServerExplorer; |
33 |
import org.gvsig.fmap.dal.DataSet; |
34 |
import org.gvsig.fmap.dal.DataStore; |
35 |
import org.gvsig.fmap.dal.DataStoreNotification; |
36 |
import org.gvsig.fmap.dal.DataStoreParameters; |
37 |
import org.gvsig.fmap.dal.exception.CloneException; |
38 |
import org.gvsig.fmap.dal.exception.DataException; |
39 |
import org.gvsig.fmap.dal.exception.InitializeException; |
40 |
import org.gvsig.fmap.dal.exception.OpenException; |
41 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
42 |
import org.gvsig.fmap.dal.feature.exception.PersistenceStoreAlreadyLoadedException; |
43 |
import org.gvsig.fmap.dal.impl.DefaultDataManager; |
44 |
import org.gvsig.fmap.dal.raster.api.RasterQuery; |
45 |
import org.gvsig.fmap.dal.raster.api.RasterSet; |
46 |
import org.gvsig.fmap.dal.raster.api.RasterStore; |
47 |
import org.gvsig.fmap.dal.raster.api.RasterStoreNotification; |
48 |
import org.gvsig.fmap.dal.raster.spi.RasterStoreProvider; |
49 |
import org.gvsig.fmap.dal.raster.spi.RasterStoreProviderServices; |
50 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
51 |
import org.gvsig.fmap.dal.spi.DataStoreInitializer; |
52 |
import org.gvsig.fmap.dal.spi.DataStoreProvider; |
53 |
import org.gvsig.fmap.geom.primitive.Envelope; |
54 |
import org.gvsig.metadata.MetadataLocator; |
55 |
import org.gvsig.metadata.MetadataManager; |
56 |
import org.gvsig.metadata.exceptions.MetadataException; |
57 |
import org.gvsig.timesupport.Interval; |
58 |
import org.gvsig.tools.ToolsLocator; |
59 |
import org.gvsig.tools.dispose.impl.AbstractDisposable; |
60 |
import org.gvsig.tools.dynobject.DelegatedDynObject; |
61 |
import org.gvsig.tools.dynobject.DynClass; |
62 |
import org.gvsig.tools.dynobject.DynObject; |
63 |
import org.gvsig.tools.dynobject.DynObjectManager; |
64 |
import org.gvsig.tools.dynobject.DynStruct; |
65 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
66 |
import org.gvsig.tools.dynobject.exception.DynMethodException; |
67 |
import org.gvsig.tools.exception.BaseException; |
68 |
import org.gvsig.tools.observer.Observable; |
69 |
import org.gvsig.tools.observer.Observer; |
70 |
import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable; |
71 |
import org.gvsig.tools.persistence.PersistenceManager; |
72 |
import org.gvsig.tools.persistence.PersistentState; |
73 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
74 |
import org.gvsig.tools.visitor.Visitor; |
75 |
import org.slf4j.Logger; |
76 |
import org.slf4j.LoggerFactory; |
77 |
|
78 |
/**
|
79 |
* Implements RasterStore
|
80 |
* @author dmartinezizquierdo
|
81 |
*
|
82 |
*/
|
83 |
public class DefaultRasterStore extends AbstractDisposable implements |
84 |
DataStoreInitializer, RasterStoreProviderServices, RasterStore, Observer {
|
85 |
|
86 |
private static final Logger LOG = LoggerFactory |
87 |
.getLogger(DefaultRasterStore.class); |
88 |
|
89 |
private static final String PERSISTENCE_DEFINITION_NAME = "RasterStore"; |
90 |
|
91 |
private DataStoreParameters parameters = null; |
92 |
private RasterStoreProvider provider = null; |
93 |
private DelegatedDynObject metadata;
|
94 |
private DefaultDataManager dataManager = null; |
95 |
|
96 |
private DelegateWeakReferencingObservable delegateObservable =
|
97 |
new DelegateWeakReferencingObservable(this); |
98 |
|
99 |
@Override
|
100 |
public void intializePhase1(DataManager dataManager, |
101 |
DataStoreParameters parameters) throws InitializeException {
|
102 |
|
103 |
DynObjectManager dynManager = ToolsLocator.getDynObjectManager(); |
104 |
|
105 |
this.metadata =
|
106 |
(DelegatedDynObject) dynManager.createDynObject( |
107 |
METADATA_DEFINITION_NAME, MetadataManager.METADATA_NAMESPACE); |
108 |
|
109 |
this.dataManager = (DefaultDataManager) dataManager;
|
110 |
|
111 |
this.parameters = parameters;
|
112 |
|
113 |
} |
114 |
|
115 |
@Override
|
116 |
public void intializePhase2(DataStoreProvider provider) |
117 |
throws InitializeException {
|
118 |
this.provider = (RasterStoreProvider) provider;
|
119 |
try {
|
120 |
provider.open(); |
121 |
} catch (OpenException e) {
|
122 |
throw new InitializeException("Can't open provider: ", e); |
123 |
} |
124 |
this.delegate(provider);
|
125 |
} |
126 |
|
127 |
/**
|
128 |
* Registers persistence
|
129 |
*/
|
130 |
public static void registerPersistenceDefinition() { |
131 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
132 |
if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) { |
133 |
DynStruct definition = |
134 |
manager.addDefinition(DefaultRasterStore.class, |
135 |
PERSISTENCE_DEFINITION_NAME, PERSISTENCE_DEFINITION_NAME |
136 |
+ " Persistent definition", null, null); |
137 |
definition.addDynFieldString("dataStoreName").setMandatory(true) |
138 |
.setPersistent(true);
|
139 |
|
140 |
definition.addDynFieldObject("parameters")
|
141 |
.setClassOfValue(DynObject.class).setMandatory(true)
|
142 |
.setPersistent(true);
|
143 |
} |
144 |
} |
145 |
|
146 |
/**
|
147 |
* Registers metadata
|
148 |
* @throws MetadataException
|
149 |
*/
|
150 |
public static void registerMetadataDefinition() throws MetadataException { |
151 |
MetadataManager manager = MetadataLocator.getMetadataManager(); |
152 |
if (manager.getDefinition(METADATA_DEFINITION_NAME) == null) { |
153 |
DynStruct metadataDefinition = |
154 |
manager.addDefinition(METADATA_DEFINITION_NAME, null);
|
155 |
metadataDefinition.extend(manager |
156 |
.getDefinition(DataStore.METADATA_DEFINITION_NAME)); |
157 |
} |
158 |
} |
159 |
|
160 |
/**
|
161 |
* @return dataManager
|
162 |
*/
|
163 |
public DataManager getManager() {
|
164 |
return this.dataManager; |
165 |
} |
166 |
|
167 |
|
168 |
@Override
|
169 |
public String getName() { |
170 |
return this.provider.getName(); |
171 |
} |
172 |
|
173 |
@Override
|
174 |
public String getFullName() { |
175 |
try {
|
176 |
return this.provider.getFullName(); |
177 |
} catch(Throwable th) { |
178 |
return null; |
179 |
} |
180 |
} |
181 |
|
182 |
@Override
|
183 |
public DataStoreParameters getParameters() {
|
184 |
return parameters;
|
185 |
} |
186 |
|
187 |
@Override
|
188 |
public String getProviderName() { |
189 |
return this.provider.getProviderName(); |
190 |
} |
191 |
|
192 |
@Override
|
193 |
public void refresh() throws DataException { |
194 |
this.notifyChange(RasterStoreNotification.BEFORE_REFRESH);
|
195 |
this.provider.refresh();
|
196 |
this.notifyChange(RasterStoreNotification.AFTER_REFRESH);
|
197 |
} |
198 |
|
199 |
@Override
|
200 |
public DataSet getDataSet() throws DataException { |
201 |
return this.getRasterSet(); |
202 |
} |
203 |
|
204 |
@Override
|
205 |
public DataSet getDataSet(DataQuery dataQuery) throws DataException { |
206 |
return this.getRasterSet((RasterQuery) dataQuery); |
207 |
} |
208 |
|
209 |
@Override
|
210 |
public void getDataSet(Observer observer) throws DataException { |
211 |
this.getRasterSet(null,observer); |
212 |
} |
213 |
|
214 |
@Override
|
215 |
public void getDataSet(DataQuery dataQuery, Observer observer) |
216 |
throws DataException {
|
217 |
this.getRasterSet((RasterQuery)dataQuery,observer);
|
218 |
} |
219 |
|
220 |
@Override
|
221 |
public RasterSet getRasterSet(RasterQuery rasterQuery) throws DataException { |
222 |
return new DefaultRasterSet(this, rasterQuery); |
223 |
} |
224 |
|
225 |
@Override
|
226 |
public RasterSet getRasterSet() throws DataException { |
227 |
return new DefaultRasterSet(this, null); |
228 |
} |
229 |
|
230 |
/**
|
231 |
* Adds an observer to the DataSet
|
232 |
* @param observer
|
233 |
* @throws DataException
|
234 |
*/
|
235 |
public void getRasterSet(Observer observer) throws DataException { |
236 |
RasterSet set=this.getRasterSet();
|
237 |
set.addObserver(observer); |
238 |
} |
239 |
|
240 |
/**
|
241 |
* Adds an observer to the queried DataSet
|
242 |
* @param rasterQuery
|
243 |
* @param observer
|
244 |
* @throws DataException
|
245 |
*/
|
246 |
public void getRasterSet(RasterQuery rasterQuery, Observer observer) |
247 |
throws DataException {
|
248 |
RasterSet set=this.getRasterSet(rasterQuery);
|
249 |
set.addObserver(observer); |
250 |
} |
251 |
|
252 |
@Override
|
253 |
public void accept(Visitor visitor) throws BaseException { |
254 |
RasterSet set = getRasterSet(); |
255 |
try {
|
256 |
set.accept(visitor); |
257 |
} finally {
|
258 |
set.dispose(); |
259 |
} |
260 |
} |
261 |
|
262 |
@Override
|
263 |
public void accept(Visitor visitor, DataQuery dataQuery) |
264 |
throws BaseException {
|
265 |
RasterSet set = getRasterSet((RasterQuery)dataQuery); |
266 |
try {
|
267 |
set.accept(visitor); |
268 |
} finally {
|
269 |
set.dispose(); |
270 |
} |
271 |
|
272 |
} |
273 |
|
274 |
@Override
|
275 |
public DataSet getSelection() throws DataException { |
276 |
// TODO Auto-generated method stub
|
277 |
return null; |
278 |
} |
279 |
|
280 |
@Override
|
281 |
public void setSelection(DataSet selection) throws DataException { |
282 |
// TODO Auto-generated method stub
|
283 |
|
284 |
} |
285 |
|
286 |
@Override
|
287 |
public DataSet createSelection() throws DataException { |
288 |
// TODO Auto-generated method stub
|
289 |
return null; |
290 |
} |
291 |
|
292 |
@Override
|
293 |
public Iterator<?> getChildren() { |
294 |
return this.provider.getChilds(); |
295 |
} |
296 |
|
297 |
@Override
|
298 |
public DataServerExplorer getExplorer()
|
299 |
throws DataException, ValidateDataParametersException {
|
300 |
return this.provider.getExplorer(); |
301 |
} |
302 |
|
303 |
@Override
|
304 |
public DataQuery createQuery() {
|
305 |
return createRasterQuery();
|
306 |
} |
307 |
|
308 |
@Override
|
309 |
public Interval getInterval() {
|
310 |
return this.provider.getInterval(); |
311 |
} |
312 |
|
313 |
@Override
|
314 |
public Collection<?> getTimes() { |
315 |
return this.provider.getTimes(); |
316 |
} |
317 |
|
318 |
@Override
|
319 |
public Collection<?> getTimes(Interval interval) { |
320 |
return this.provider.getTimes(interval); |
321 |
} |
322 |
|
323 |
@Override
|
324 |
public void disableNotifications() { |
325 |
this.delegateObservable.disableNotifications();
|
326 |
} |
327 |
|
328 |
@Override
|
329 |
public void enableNotifications() { |
330 |
this.delegateObservable.enableNotifications();
|
331 |
|
332 |
} |
333 |
|
334 |
@Override
|
335 |
public void beginComplexNotification() { |
336 |
this.delegateObservable.beginComplexNotification();
|
337 |
|
338 |
} |
339 |
|
340 |
@Override
|
341 |
public void endComplexNotification() { |
342 |
this.delegateObservable.endComplexNotification();
|
343 |
} |
344 |
|
345 |
@Override
|
346 |
public void addObserver(Observer observer) { |
347 |
if (delegateObservable != null) { |
348 |
this.delegateObservable.addObserver(observer);
|
349 |
} |
350 |
} |
351 |
|
352 |
@Override
|
353 |
public void deleteObserver(Observer observer) { |
354 |
if (delegateObservable != null) { |
355 |
this.delegateObservable.deleteObserver(observer);
|
356 |
} |
357 |
|
358 |
} |
359 |
|
360 |
@Override
|
361 |
public void deleteObservers() { |
362 |
this.delegateObservable.deleteObservers();
|
363 |
} |
364 |
|
365 |
@Override
|
366 |
public void saveToState(PersistentState state) throws PersistenceException { |
367 |
|
368 |
state.set("dataStoreName", this.getName()); |
369 |
state.set("parameters", this.parameters); |
370 |
} |
371 |
|
372 |
@Override
|
373 |
public void loadFromState(PersistentState state) |
374 |
throws PersistenceException {
|
375 |
if (this.provider != null) { |
376 |
throw new PersistenceStoreAlreadyLoadedException(this.getName()); |
377 |
} |
378 |
if (this.getManager() == null) { |
379 |
this.dataManager = (DefaultDataManager) DALLocator.getDataManager();
|
380 |
} |
381 |
|
382 |
DataStoreParameters params = |
383 |
(DataStoreParameters) state.get("parameters");
|
384 |
|
385 |
try {
|
386 |
|
387 |
this.dataManager.intializeDataStore(this, params); |
388 |
|
389 |
} catch (InitializeException e) {
|
390 |
throw new PersistenceException(e); |
391 |
} catch (DataException e) {
|
392 |
throw new PersistenceException(e); |
393 |
} |
394 |
|
395 |
} |
396 |
|
397 |
//
|
398 |
// ====================================================================
|
399 |
// Metadata related methods
|
400 |
//
|
401 |
|
402 |
@Override
|
403 |
public Set<?> getMetadataChildren() throws MetadataException { |
404 |
// TODO Auto-generated method stub
|
405 |
return null; |
406 |
} |
407 |
|
408 |
@Override
|
409 |
public Object getMetadataID() throws MetadataException { |
410 |
return this.provider.getSourceId(); |
411 |
} |
412 |
|
413 |
@Override
|
414 |
public String getMetadataName() throws MetadataException { |
415 |
return this.provider.getProviderName(); |
416 |
} |
417 |
|
418 |
@Override
|
419 |
public DynClass getDynClass() {
|
420 |
return this.metadata.getDynClass(); |
421 |
} |
422 |
|
423 |
@Override
|
424 |
public void implement(DynClass dynClass) { |
425 |
this.metadata.implement(dynClass);
|
426 |
} |
427 |
|
428 |
@Override
|
429 |
public void delegate(DynObject dynObject) { |
430 |
this.metadata.delegate(dynObject);
|
431 |
} |
432 |
|
433 |
@Override
|
434 |
public Object getDynValue(String name) throws DynFieldNotFoundException { |
435 |
if (this.metadata.hasDynValue(name)) { |
436 |
return this.metadata.getDynValue(name); |
437 |
} |
438 |
if (METADATA_PROVIDER.equalsIgnoreCase(name)) {
|
439 |
return this.provider.getProviderName(); |
440 |
} else if (METADATA_CONTAINERNAME.equalsIgnoreCase(name)) { |
441 |
return this.provider.getSourceId(); |
442 |
} |
443 |
return this.metadata.getDynValue(name); |
444 |
} |
445 |
|
446 |
@Override
|
447 |
public void setDynValue(String name, Object value) |
448 |
throws DynFieldNotFoundException {
|
449 |
|
450 |
this.metadata.setDynValue(name, value);
|
451 |
|
452 |
} |
453 |
|
454 |
@Override
|
455 |
public boolean hasDynValue(String name) { |
456 |
return this.metadata.hasDynValue(name); |
457 |
} |
458 |
|
459 |
@Override
|
460 |
public Object invokeDynMethod(String name, Object[] args) |
461 |
throws DynMethodException {
|
462 |
return this.metadata.invokeDynMethod(this, name, args); |
463 |
} |
464 |
|
465 |
@Override
|
466 |
public Object invokeDynMethod(int code, Object[] args) |
467 |
throws DynMethodException {
|
468 |
return this.metadata.invokeDynMethod(this, code, args); |
469 |
} |
470 |
|
471 |
@Override
|
472 |
public void clear() { |
473 |
if (metadata != null) { |
474 |
metadata.clear(); |
475 |
} |
476 |
} |
477 |
|
478 |
@Override
|
479 |
public RasterQuery createRasterQuery() {
|
480 |
return new DefaultRasterQuery(); |
481 |
} |
482 |
|
483 |
@Override
|
484 |
public DataStore getStore() {
|
485 |
return this; |
486 |
} |
487 |
|
488 |
@Override
|
489 |
public void update(Observable observable, Object notification) { |
490 |
if (observable instanceof DataSet) { |
491 |
this.notifyChange(RasterStoreNotification.DATASET_CHANGED);
|
492 |
|
493 |
} else{
|
494 |
if (observable instanceof RasterStoreProvider) { |
495 |
if (observable == this.provider) { |
496 |
this.notifyChange(RasterStoreNotification.STORE_PROVIDER_CHANGED);
|
497 |
} |
498 |
} |
499 |
} |
500 |
} |
501 |
|
502 |
@Override
|
503 |
protected void doDispose() throws BaseException { |
504 |
|
505 |
this.notifyChange(DataStoreNotification.BEFORE_DISPOSE);
|
506 |
this.provider.dispose();
|
507 |
this.parameters = null; |
508 |
this.notifyChange(DataStoreNotification.AFTER_DISPOSE);
|
509 |
if (delegateObservable != null) { |
510 |
this.delegateObservable.deleteObservers();
|
511 |
this.delegateObservable = null; |
512 |
} |
513 |
} |
514 |
|
515 |
@Override
|
516 |
public Object clone() throws CloneNotSupportedException{ |
517 |
DataStoreParameters dsp = getParameters(); |
518 |
|
519 |
DefaultRasterStore cloned_store = null;
|
520 |
|
521 |
try {
|
522 |
cloned_store = (DefaultRasterStore) DALLocator.getDataManager(). |
523 |
openStore(this.getProviderName(), dsp);
|
524 |
} catch (Exception e) { |
525 |
throw new CloneException(e); |
526 |
} |
527 |
return cloned_store;
|
528 |
|
529 |
} |
530 |
|
531 |
//
|
532 |
// ====================================================================
|
533 |
// Gestion de notificaciones
|
534 |
//
|
535 |
/**
|
536 |
* Notifies change
|
537 |
* @param notification
|
538 |
*/
|
539 |
public void notifyChange(String notification) { |
540 |
if (delegateObservable != null) { |
541 |
notifyChange(new DefaultRasterStoreNotification(this, notification)); |
542 |
} |
543 |
|
544 |
} |
545 |
|
546 |
@Override
|
547 |
public void notifyChange(String notification, ResourceProvider data) { |
548 |
notifyChange(new DefaultRasterStoreNotification(this, |
549 |
DataStoreNotification.RESOURCE_CHANGED)); |
550 |
} |
551 |
|
552 |
/**
|
553 |
* Notifies change
|
554 |
* @param storeNotification
|
555 |
*/
|
556 |
public void notifyChange(DefaultRasterStoreNotification storeNotification) { |
557 |
delegateObservable.notifyObservers(storeNotification); |
558 |
} |
559 |
|
560 |
/**
|
561 |
* Gets this provider
|
562 |
* @return RasterStoreProvider
|
563 |
*/
|
564 |
public RasterStoreProvider getProvider(){
|
565 |
return this.provider; |
566 |
} |
567 |
|
568 |
@Override
|
569 |
public Envelope getEnvelope() throws DataException { |
570 |
return this.getRasterSet().getEnvelope(); |
571 |
} |
572 |
} |