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 @ 6334
History | View | Annotate | Download (18.1 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.List; |
28 |
import java.util.Set; |
29 |
|
30 |
import org.gvsig.fmap.dal.DALLocator; |
31 |
import org.gvsig.fmap.dal.DataManager; |
32 |
import org.gvsig.fmap.dal.DataQuery; |
33 |
import org.gvsig.fmap.dal.DataServerExplorer; |
34 |
import org.gvsig.fmap.dal.DataSet; |
35 |
import org.gvsig.fmap.dal.DataStore; |
36 |
import org.gvsig.fmap.dal.DataStoreNotification; |
37 |
import org.gvsig.fmap.dal.DataStoreParameters; |
38 |
import org.gvsig.fmap.dal.exception.CloneException; |
39 |
import org.gvsig.fmap.dal.exception.DataException; |
40 |
import org.gvsig.fmap.dal.exception.InitializeException; |
41 |
import org.gvsig.fmap.dal.exception.OpenException; |
42 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
43 |
import org.gvsig.fmap.dal.feature.exception.PersistenceStoreAlreadyLoadedException; |
44 |
import org.gvsig.fmap.dal.impl.DefaultDataManager; |
45 |
import org.gvsig.fmap.dal.raster.api.BandAttributeDescriptor; |
46 |
import org.gvsig.fmap.dal.raster.api.BandDescriptor; |
47 |
import org.gvsig.fmap.dal.raster.api.BandQuery; |
48 |
import org.gvsig.fmap.dal.raster.api.RasterQuery; |
49 |
import org.gvsig.fmap.dal.raster.api.RasterSet; |
50 |
import org.gvsig.fmap.dal.raster.api.RasterStore; |
51 |
import org.gvsig.fmap.dal.raster.api.RasterStoreNotification; |
52 |
import org.gvsig.fmap.dal.raster.spi.RasterStoreProvider; |
53 |
import org.gvsig.fmap.dal.raster.spi.RasterStoreProviderServices; |
54 |
import org.gvsig.fmap.dal.resource.Resource; |
55 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
56 |
import org.gvsig.fmap.dal.spi.DataStoreInitializer; |
57 |
import org.gvsig.fmap.dal.spi.DataStoreProvider; |
58 |
import org.gvsig.fmap.geom.primitive.Envelope; |
59 |
import org.gvsig.metadata.MetadataLocator; |
60 |
import org.gvsig.metadata.MetadataManager; |
61 |
import org.gvsig.metadata.exceptions.MetadataException; |
62 |
import org.gvsig.raster.lib.buffer.api.BandInfo; |
63 |
import org.gvsig.timesupport.Interval; |
64 |
import org.gvsig.tools.ToolsLocator; |
65 |
import org.gvsig.tools.dispose.impl.AbstractDisposable; |
66 |
import org.gvsig.tools.dynobject.DelegatedDynObject; |
67 |
import org.gvsig.tools.dynobject.DynClass; |
68 |
import org.gvsig.tools.dynobject.DynObject; |
69 |
import org.gvsig.tools.dynobject.DynObjectManager; |
70 |
import org.gvsig.tools.dynobject.DynStruct; |
71 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
72 |
import org.gvsig.tools.dynobject.exception.DynMethodException; |
73 |
import org.gvsig.tools.exception.BaseException; |
74 |
import org.gvsig.tools.observer.Observable; |
75 |
import org.gvsig.tools.observer.Observer; |
76 |
import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable; |
77 |
import org.gvsig.tools.persistence.PersistenceManager; |
78 |
import org.gvsig.tools.persistence.PersistentState; |
79 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
80 |
import org.gvsig.tools.visitor.Visitor; |
81 |
|
82 |
import org.slf4j.Logger; |
83 |
import org.slf4j.LoggerFactory; |
84 |
|
85 |
/**
|
86 |
* Implements RasterStore
|
87 |
* @author dmartinezizquierdo
|
88 |
*
|
89 |
*/
|
90 |
public class DefaultRasterStore extends AbstractDisposable implements |
91 |
DataStoreInitializer, RasterStoreProviderServices, RasterStore, Observer {
|
92 |
|
93 |
private static final Logger LOG = LoggerFactory |
94 |
.getLogger(DefaultRasterStore.class); |
95 |
|
96 |
private static final String PERSISTENCE_DEFINITION_NAME = "RasterStore"; |
97 |
private static final String METADATA_DEFINITION_NAME= "RasterStore"; |
98 |
|
99 |
private DataStoreParameters parameters = null; |
100 |
private RasterStoreProvider provider = null; |
101 |
private DelegatedDynObject metadata;
|
102 |
private DefaultDataManager dataManager = null; |
103 |
|
104 |
private DelegateWeakReferencingObservable delegateObservable =
|
105 |
new DelegateWeakReferencingObservable(this); |
106 |
|
107 |
@Override
|
108 |
public void intializePhase1(DataManager dataManager, |
109 |
DataStoreParameters parameters) throws InitializeException {
|
110 |
|
111 |
DynObjectManager dynManager = ToolsLocator.getDynObjectManager(); |
112 |
|
113 |
this.metadata =
|
114 |
(DelegatedDynObject) dynManager.createDynObject( |
115 |
METADATA_DEFINITION_NAME, MetadataManager.METADATA_NAMESPACE); |
116 |
|
117 |
this.dataManager = (DefaultDataManager) dataManager;
|
118 |
|
119 |
this.parameters = parameters;
|
120 |
|
121 |
} |
122 |
|
123 |
@Override
|
124 |
public void intializePhase2(DataStoreProvider provider) |
125 |
throws InitializeException {
|
126 |
this.provider = (RasterStoreProvider) provider;
|
127 |
try {
|
128 |
provider.open(); |
129 |
} catch (OpenException e) {
|
130 |
throw new InitializeException("Can't open provider: ", e); |
131 |
} |
132 |
this.delegate(provider);
|
133 |
} |
134 |
|
135 |
/**
|
136 |
* Registers persistence
|
137 |
*/
|
138 |
public static void registerPersistenceDefinition() { |
139 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
140 |
if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) { |
141 |
DynStruct definition = |
142 |
manager.addDefinition(DefaultRasterStore.class, |
143 |
PERSISTENCE_DEFINITION_NAME, PERSISTENCE_DEFINITION_NAME |
144 |
+ " Persistent definition", null, null); |
145 |
definition.addDynFieldString("dataStoreName").setMandatory(true) |
146 |
.setPersistent(true);
|
147 |
|
148 |
definition.addDynFieldObject("parameters")
|
149 |
.setClassOfValue(DynObject.class).setMandatory(true)
|
150 |
.setPersistent(true);
|
151 |
} |
152 |
} |
153 |
|
154 |
/**
|
155 |
* Registers metadata
|
156 |
* @throws MetadataException
|
157 |
*/
|
158 |
public static void registerMetadataDefinition() throws MetadataException { |
159 |
MetadataManager manager = MetadataLocator.getMetadataManager(); |
160 |
if (manager.getDefinition(METADATA_DEFINITION_NAME) == null) { |
161 |
DynStruct metadataDefinition = |
162 |
manager.addDefinition(METADATA_DEFINITION_NAME, null);
|
163 |
metadataDefinition.extend(manager |
164 |
.getDefinition(DataStore.METADATA_DEFINITION_NAME)); |
165 |
} |
166 |
} |
167 |
|
168 |
/**
|
169 |
* @return dataManager
|
170 |
*/
|
171 |
public DataManager getManager() {
|
172 |
return this.dataManager; |
173 |
} |
174 |
|
175 |
|
176 |
@Override
|
177 |
public String getName() { |
178 |
return this.provider.getName(); |
179 |
} |
180 |
|
181 |
@Override
|
182 |
public String getFullName() { |
183 |
return this.provider.getFullName(); |
184 |
} |
185 |
|
186 |
@Override
|
187 |
public DataStoreParameters getParameters() {
|
188 |
return parameters;
|
189 |
} |
190 |
|
191 |
@Override
|
192 |
public String getProviderName() { |
193 |
return this.provider.getProviderName(); |
194 |
} |
195 |
|
196 |
@Override
|
197 |
public void refresh() throws DataException { |
198 |
this.notifyChange(RasterStoreNotification.BEFORE_REFRESH);
|
199 |
this.provider.refresh();
|
200 |
this.notifyChange(RasterStoreNotification.AFTER_REFRESH);
|
201 |
} |
202 |
|
203 |
@Override
|
204 |
public DataSet getDataSet() throws DataException { |
205 |
return this.getRasterSet(); |
206 |
} |
207 |
|
208 |
@Override
|
209 |
public DataSet getDataSet(DataQuery dataQuery) throws DataException { |
210 |
return this.getRasterSet((RasterQuery) dataQuery); |
211 |
} |
212 |
|
213 |
@Override
|
214 |
public void getDataSet(Observer observer) throws DataException { |
215 |
this.getRasterSet(null,observer); |
216 |
} |
217 |
|
218 |
@Override
|
219 |
public void getDataSet(DataQuery dataQuery, Observer observer) |
220 |
throws DataException {
|
221 |
this.getRasterSet((RasterQuery)dataQuery,observer);
|
222 |
} |
223 |
|
224 |
@Override
|
225 |
public RasterSet getRasterSet(RasterQuery rasterQuery) throws DataException { |
226 |
return new DefaultRasterSet(this, rasterQuery); |
227 |
} |
228 |
|
229 |
@Override
|
230 |
public RasterSet getRasterSet() throws DataException { |
231 |
return new DefaultRasterSet(this, null); |
232 |
} |
233 |
|
234 |
/**
|
235 |
* Adds an observer to the DataSet
|
236 |
* @param observer
|
237 |
* @throws DataException
|
238 |
*/
|
239 |
public void getRasterSet(Observer observer) throws DataException { |
240 |
RasterSet set=this.getRasterSet();
|
241 |
set.addObserver(observer); |
242 |
} |
243 |
|
244 |
/**
|
245 |
* Adds an observer to the queried DataSet
|
246 |
* @param rasterQuery
|
247 |
* @param observer
|
248 |
* @throws DataException
|
249 |
*/
|
250 |
public void getRasterSet(RasterQuery rasterQuery, Observer observer) |
251 |
throws DataException {
|
252 |
RasterSet set=this.getRasterSet(rasterQuery);
|
253 |
set.addObserver(observer); |
254 |
} |
255 |
|
256 |
@Override
|
257 |
public void accept(Visitor visitor) throws BaseException { |
258 |
RasterSet set = getRasterSet(); |
259 |
try {
|
260 |
set.accept(visitor); |
261 |
} finally {
|
262 |
set.dispose(); |
263 |
} |
264 |
} |
265 |
|
266 |
@Override
|
267 |
public void accept(Visitor visitor, DataQuery dataQuery) |
268 |
throws BaseException {
|
269 |
RasterSet set = getRasterSet((RasterQuery)dataQuery); |
270 |
try {
|
271 |
set.accept(visitor); |
272 |
} finally {
|
273 |
set.dispose(); |
274 |
} |
275 |
|
276 |
} |
277 |
|
278 |
@Override
|
279 |
public DataSet getSelection() throws DataException { |
280 |
// TODO Auto-generated method stub
|
281 |
return null; |
282 |
} |
283 |
|
284 |
@Override
|
285 |
public void setSelection(DataSet selection) throws DataException { |
286 |
// TODO Auto-generated method stub
|
287 |
|
288 |
} |
289 |
|
290 |
@Override
|
291 |
public DataSet createSelection() throws DataException { |
292 |
// TODO Auto-generated method stub
|
293 |
return null; |
294 |
} |
295 |
|
296 |
@Override
|
297 |
public Iterator<?> getChildren() { |
298 |
return this.provider.getChilds(); |
299 |
} |
300 |
|
301 |
@Override
|
302 |
public DataServerExplorer getExplorer()
|
303 |
throws DataException, ValidateDataParametersException {
|
304 |
return this.provider.getExplorer(); |
305 |
} |
306 |
|
307 |
@Override
|
308 |
public DataQuery createQuery() {
|
309 |
return createRasterQuery();
|
310 |
} |
311 |
|
312 |
@Override
|
313 |
public Interval getInterval() {
|
314 |
return this.provider.getInterval(); |
315 |
} |
316 |
|
317 |
@Override
|
318 |
public Collection<?> getTimes() { |
319 |
return this.provider.getTimes(); |
320 |
} |
321 |
|
322 |
@Override
|
323 |
public Collection<?> getTimes(Interval interval) { |
324 |
return this.provider.getTimes(interval); |
325 |
} |
326 |
|
327 |
@Override
|
328 |
public void disableNotifications() { |
329 |
this.delegateObservable.disableNotifications();
|
330 |
} |
331 |
|
332 |
@Override
|
333 |
public void enableNotifications() { |
334 |
this.delegateObservable.enableNotifications();
|
335 |
|
336 |
} |
337 |
|
338 |
@Override
|
339 |
public void beginComplexNotification() { |
340 |
this.delegateObservable.beginComplexNotification();
|
341 |
|
342 |
} |
343 |
|
344 |
@Override
|
345 |
public void endComplexNotification() { |
346 |
this.delegateObservable.endComplexNotification();
|
347 |
} |
348 |
|
349 |
@Override
|
350 |
public void addObserver(Observer observer) { |
351 |
if (delegateObservable != null) { |
352 |
this.delegateObservable.addObserver(observer);
|
353 |
} |
354 |
} |
355 |
|
356 |
@Override
|
357 |
public void deleteObserver(Observer observer) { |
358 |
if (delegateObservable != null) { |
359 |
this.delegateObservable.deleteObserver(observer);
|
360 |
} |
361 |
|
362 |
} |
363 |
|
364 |
@Override
|
365 |
public void deleteObservers() { |
366 |
this.delegateObservable.deleteObservers();
|
367 |
} |
368 |
|
369 |
@Override
|
370 |
public void saveToState(PersistentState state) throws PersistenceException { |
371 |
|
372 |
state.set("dataStoreName", this.getName()); |
373 |
state.set("parameters", this.parameters); |
374 |
} |
375 |
|
376 |
@Override
|
377 |
public void loadFromState(PersistentState state) |
378 |
throws PersistenceException {
|
379 |
if (this.provider != null) { |
380 |
throw new PersistenceStoreAlreadyLoadedException(this.getName()); |
381 |
} |
382 |
if (this.getManager() == null) { |
383 |
this.dataManager = (DefaultDataManager) DALLocator.getDataManager();
|
384 |
} |
385 |
|
386 |
DataStoreParameters params = |
387 |
(DataStoreParameters) state.get("parameters");
|
388 |
|
389 |
try {
|
390 |
|
391 |
this.dataManager.intializeDataStore(this, params); |
392 |
|
393 |
} catch (InitializeException e) {
|
394 |
throw new PersistenceException(e); |
395 |
} catch (DataException e) {
|
396 |
throw new PersistenceException(e); |
397 |
} |
398 |
|
399 |
} |
400 |
|
401 |
//
|
402 |
// ====================================================================
|
403 |
// Metadata related methods
|
404 |
//
|
405 |
|
406 |
@Override
|
407 |
public Set<?> getMetadataChildren() throws MetadataException { |
408 |
// TODO Auto-generated method stub
|
409 |
return null; |
410 |
} |
411 |
|
412 |
@Override
|
413 |
public Object getMetadataID() throws MetadataException { |
414 |
return this.provider.getSourceId(); |
415 |
} |
416 |
|
417 |
@Override
|
418 |
public String getMetadataName() throws MetadataException { |
419 |
return this.provider.getProviderName(); |
420 |
} |
421 |
|
422 |
@Override
|
423 |
public DynClass getDynClass() {
|
424 |
return this.metadata.getDynClass(); |
425 |
} |
426 |
|
427 |
@Override
|
428 |
public void implement(DynClass dynClass) { |
429 |
this.metadata.implement(dynClass);
|
430 |
} |
431 |
|
432 |
@Override
|
433 |
public void delegate(DynObject dynObject) { |
434 |
this.metadata.delegate(dynObject);
|
435 |
} |
436 |
|
437 |
@Override
|
438 |
public Object getDynValue(String name) throws DynFieldNotFoundException { |
439 |
if (this.metadata.hasDynValue(name)) { |
440 |
return this.metadata.getDynValue(name); |
441 |
} |
442 |
if (METADATA_PROVIDER.equalsIgnoreCase(name)) {
|
443 |
return this.provider.getProviderName(); |
444 |
} else if (METADATA_CONTAINERNAME.equalsIgnoreCase(name)) { |
445 |
return this.provider.getSourceId(); |
446 |
} |
447 |
return this.metadata.getDynValue(name); |
448 |
} |
449 |
|
450 |
@Override
|
451 |
public void setDynValue(String name, Object value) |
452 |
throws DynFieldNotFoundException {
|
453 |
|
454 |
this.metadata.setDynValue(name, value);
|
455 |
|
456 |
} |
457 |
|
458 |
@Override
|
459 |
public boolean hasDynValue(String name) { |
460 |
return this.metadata.hasDynValue(name); |
461 |
} |
462 |
|
463 |
@Override
|
464 |
public Object invokeDynMethod(String name, Object[] args) |
465 |
throws DynMethodException {
|
466 |
return this.metadata.invokeDynMethod(this, name, args); |
467 |
} |
468 |
|
469 |
@Override
|
470 |
public Object invokeDynMethod(int code, Object[] args) |
471 |
throws DynMethodException {
|
472 |
return this.metadata.invokeDynMethod(this, code, args); |
473 |
} |
474 |
|
475 |
@Override
|
476 |
public void clear() { |
477 |
if (metadata != null) { |
478 |
metadata.clear(); |
479 |
} |
480 |
} |
481 |
|
482 |
@Override
|
483 |
public RasterQuery createRasterQuery() {
|
484 |
return new DefaultRasterQuery(); |
485 |
} |
486 |
|
487 |
@Override
|
488 |
public DataStore getStore() {
|
489 |
return this; |
490 |
} |
491 |
|
492 |
@Override
|
493 |
public void update(Observable observable, Object notification) { |
494 |
if (observable instanceof DataSet) { |
495 |
this.notifyChange(RasterStoreNotification.DATASET_CHANGED);
|
496 |
|
497 |
} else{
|
498 |
if (observable instanceof RasterStoreProvider) { |
499 |
if (observable == this.provider) { |
500 |
this.notifyChange(RasterStoreNotification.STORE_PROVIDER_CHANGED);
|
501 |
} |
502 |
} |
503 |
} |
504 |
} |
505 |
|
506 |
@Override
|
507 |
protected void doDispose() throws BaseException { |
508 |
|
509 |
this.notifyChange(DataStoreNotification.BEFORE_DISPOSE);
|
510 |
this.provider.dispose();
|
511 |
this.parameters = null; |
512 |
this.notifyChange(DataStoreNotification.AFTER_DISPOSE);
|
513 |
if (delegateObservable != null) { |
514 |
this.delegateObservable.deleteObservers();
|
515 |
this.delegateObservable = null; |
516 |
} |
517 |
} |
518 |
|
519 |
@Override
|
520 |
public Object clone() throws CloneNotSupportedException{ |
521 |
DataStoreParameters dsp = getParameters(); |
522 |
|
523 |
DefaultRasterStore cloned_store = null;
|
524 |
|
525 |
try {
|
526 |
cloned_store = (DefaultRasterStore) DALLocator.getDataManager(). |
527 |
openStore(this.getProviderName(), dsp);
|
528 |
} catch (Exception e) { |
529 |
throw new CloneException(e); |
530 |
} |
531 |
return cloned_store;
|
532 |
|
533 |
} |
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 |
|
573 |
@Override
|
574 |
public BandInfo getBandInfo(int band) { |
575 |
return this.provider.getBandInfo(band); |
576 |
} |
577 |
|
578 |
@Override
|
579 |
public BandDescriptor getBandDescriptor(int band) { |
580 |
return this.provider.getBandDescriptor(band); |
581 |
} |
582 |
|
583 |
@Override
|
584 |
public BandQuery createBandQuery(int band) { |
585 |
return new DefaultBandQuery(band, getBandDescriptor(band)); |
586 |
} |
587 |
|
588 |
@Override
|
589 |
public int getBands() { |
590 |
return this.provider.getBands(); |
591 |
} |
592 |
|
593 |
@Override
|
594 |
public BandDescriptor createBandDescriptor(int band, List<BandAttributeDescriptor> attributes) { |
595 |
return new DefaultBandDescriptor(band, attributes); |
596 |
} |
597 |
|
598 |
@Override
|
599 |
public BandAttributeDescriptor createBandAttributeDescriptor(int band, String name, |
600 |
String description, List<Object> values, String units) { |
601 |
return new DefaultBandAttributeDescriptor(band, name, description, values, units); |
602 |
} |
603 |
|
604 |
@Override
|
605 |
public BandAttributeDescriptor createBandAttributeDescriptor(int band, String name, String description, |
606 |
List<Object> values) { |
607 |
return new DefaultBandAttributeDescriptor(band, name, description, values); |
608 |
} |
609 |
|
610 |
@Override
|
611 |
public void notifyChange(String notification, Resource resource) { |
612 |
notifyChange(new DefaultRasterStoreNotification(this, |
613 |
DataStoreNotification.RESOURCE_CHANGED)); |
614 |
} |
615 |
|
616 |
@Override
|
617 |
public RasterStore getRasterStore() {
|
618 |
return this; |
619 |
} |
620 |
|
621 |
} |