Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.spi / src / main / java / org / gvsig / fmap / dal / feature / spi / AbstractFeatureStoreProvider.java @ 40435

History | View | Annotate | Download (14.6 KB)

1 40435 jjdelcerro
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 {{Company}}   {{Task}}
26
 */
27
28
package org.gvsig.fmap.dal.feature.spi;
29
30
import java.util.Collection;
31
import java.util.Iterator;
32
33
import org.gvsig.fmap.dal.DALLocator;
34
import org.gvsig.fmap.dal.DataServerExplorer;
35
import org.gvsig.fmap.dal.DataStoreParameters;
36
import org.gvsig.fmap.dal.exception.CloseException;
37
import org.gvsig.fmap.dal.exception.DataException;
38
import org.gvsig.fmap.dal.exception.InitializeException;
39
import org.gvsig.fmap.dal.exception.OpenException;
40
import org.gvsig.fmap.dal.exception.ReadException;
41
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
42
import org.gvsig.fmap.dal.feature.FeatureLocks;
43
import org.gvsig.fmap.dal.feature.FeatureReference;
44
import org.gvsig.fmap.dal.feature.FeatureSelection;
45
import org.gvsig.fmap.dal.feature.FeatureStore;
46
import org.gvsig.fmap.dal.feature.FeatureType;
47
import org.gvsig.fmap.dal.resource.ResourceAction;
48
import org.gvsig.fmap.dal.resource.ResourceManager;
49
import org.gvsig.fmap.dal.resource.spi.ResourceManagerProviderServices;
50
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
51
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
52
import org.gvsig.fmap.geom.primitive.Envelope;
53
import org.gvsig.timesupport.Interval;
54
import org.gvsig.tools.dispose.impl.AbstractDisposable;
55
import org.gvsig.tools.dynobject.DelegatedDynObject;
56
import org.gvsig.tools.dynobject.DynClass;
57
import org.gvsig.tools.dynobject.DynObject;
58
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
59
import org.gvsig.tools.dynobject.exception.DynMethodException;
60
import org.gvsig.tools.exception.BaseException;
61
62
/**
63
 * Abstract implementation of {@link FeatureStoreProvider}
64
 *
65
 */
66
public abstract class AbstractFeatureStoreProvider extends AbstractDisposable
67
                implements FeatureStoreProvider {
68
69
        private FeatureStoreProviderServices store;
70
        private DelegatedDynObject metadata;
71
        private DataStoreParameters parameters;
72
73
        /**
74
         * Default Constructor.
75
         *
76
         * @param params
77
         * @param storeServices
78
         * @param metadata
79
         */
80
        protected AbstractFeatureStoreProvider(DataStoreParameters params,
81
                        DataStoreProviderServices storeServices, DynObject metadata) {
82
                this.store = (FeatureStoreProviderServices) storeServices;
83
                this.metadata = (DelegatedDynObject) metadata;
84
                this.parameters = params;
85
        }
86
87
        /**
88
         * Constructor when cannot create metada in constrution time. <br>
89
         * <br>
90
         * <strong>Note: </strong> Don't use it if not is necesary. Set metada
91
         * <strong>as soon as posible</strong> by
92
         * {@link AbstractFeatureStoreProvider#setMetadata(DynObject)}
93
         *
94
         * @param params
95
         * @param storeServices
96
         */
97
        protected AbstractFeatureStoreProvider(DataStoreParameters params,
98
                        DataStoreProviderServices storeServices) {
99
                this.store = (FeatureStoreProviderServices) storeServices;
100
                this.metadata = null;
101
                this.parameters = params;
102
        }
103
104
        public final FeatureProvider getFeatureProviderByReference(
105
                        final FeatureReferenceProviderServices reference)
106
                        throws DataException {
107
                this.open();
108
                FeatureProvider featureProvider = (FeatureProvider) getResource()
109
                                .execute(new ResourceAction() {
110
                                        public Object run() throws Exception {
111
                                                return internalGetFeatureProviderByReference(reference);
112
                                        }
113
                                });
114
115
                if (featureProvider == null) {
116
                        throw new FeatureProviderNotFoundException(reference);
117
                }
118
119
                return featureProvider;
120
        }
121
122
        public final FeatureProvider getFeatureProviderByReference(
123
                        final FeatureReferenceProviderServices reference,
124
                        final FeatureType featureType) throws DataException {
125
                this.open();
126
                FeatureProvider featureProvider = (FeatureProvider) getResource()
127
                                .execute(new ResourceAction() {
128
                        public Object run() throws Exception {
129
                                return internalGetFeatureProviderByReference(reference,
130
                                                featureType);
131
                        }
132
                });
133
134
                if (featureProvider == null) {
135
                        throw new FeatureProviderNotFoundException(reference);
136
                }
137
138
                return featureProvider;
139
        }
140
141
        /**
142
         * Returns a {@link FeatureProvider} by reference, using the default
143
         * {@link FeatureType}. This method may be rewritten by the child classes as
144
         * an implementation of the
145
         * {@link #getFeatureProviderByReference(FeatureReferenceProviderServices)}
146
         * method.
147
         *
148
         * @param reference
149
         *            the reference to the {@link FeatureProvider}
150
         * @return the {@link FeatureProvider} being referenced
151
         * @throws DataException
152
         *             if there is an error loading the {@link FeatureProvider}
153
         */
154
        protected FeatureProvider internalGetFeatureProviderByReference(
155
                        FeatureReferenceProviderServices reference) throws DataException {
156
                return internalGetFeatureProviderByReference(reference,
157
                                getStoreServices().getDefaultFeatureType());
158
        }
159
160
        /**
161
         * Set metada container if this not set at construction time and only in one
162
         * time. In other case an Exception will be throw
163
         *
164
         * @param metadata
165
         */
166
        protected void setMetadata(DynObject metadata) {
167
                if (this.metadata != null) {
168
                        // FIXME Exception
169
                        throw new IllegalStateException();
170
                }
171
                this.metadata = (DelegatedDynObject) metadata;
172
        }
173
174
        /**
175
         * @return the parameters
176
         */
177
        public DataStoreParameters getParameters() {
178
                return parameters;
179
        }
180
181
        /**
182
         * Create or get a resource of <code>type</code> for <code>params</code> in
183
         * {@link ResourceManager}
184
         *
185
         * @param type
186
         * @param params
187
         * @return
188
         * @throws InitializeException
189
         */
190
        protected ResourceProvider createResource(String type, Object[] params)
191
                        throws InitializeException {
192
                ResourceManagerProviderServices manager = (ResourceManagerProviderServices) DALLocator
193
                                .getResourceManager();
194
                ResourceProvider resource = manager.createAddResource(type, params);
195
                return resource;
196
        }
197
198
        /*
199
         * (non-Javadoc)
200
         *
201
         * @see
202
         * org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#getStoreServices()
203
         */
204
        public FeatureStoreProviderServices getStoreServices() {
205
                return this.store;
206
        }
207
208
        public FeatureStore getFeatureStore() {
209
                return this.store.getFeatureStore();
210
        }
211
212
        /**
213
         * unsupported by default, override this otherwise
214
         *
215
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#allowWrite()
216
         */
217
        public boolean allowWrite() {
218
                return false;
219
        }
220
221
        /**
222
         * unsupported by default, override this otherwise
223
         *
224
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#performChanges(Iterator,
225
         *      Iterator, Iterator, Iterator)
226
         */
227
228
        public void performChanges(Iterator deleteds, Iterator inserteds,
229
                        Iterator updateds, Iterator featureTypesChanged)
230
                        throws DataException {
231
                // FIXME exception
232
                throw new UnsupportedOperationException();
233
234
        }
235
236
        /**
237
         * unsupported by default, override this otherwise
238
         *
239
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#isLocksSupported()
240
         */
241
        public boolean isLocksSupported() {
242
                return false;
243
        }
244
245
        /**
246
         * Default Factory of {@link FeatureProvider}. Create a new default
247
         * {@link FeatureProvider} instance.<br>
248
         *
249
         * Override this if you need an special implemtation of
250
         * {@link FeatureProvider}.
251
         *
252
         * @return
253
         * @throws DataException
254
         *
255
         * @see {@link org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#createFeatureProvider(FeatureType)}
256
         */
257
258
        public FeatureProvider createFeatureProvider(FeatureType type)
259
                        throws DataException {
260
                return this.store.createDefaultFeatureProvider(type);
261
        }
262
263
        /**
264
         * unsupported by default (return <code>null</code>), override this
265
         * otherwise
266
         *
267
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#createFeatureLocks()
268
         */
269
        public FeatureLocks createFeatureLocks() throws DataException {
270
                return null;
271
        }
272
273
        /**
274
         * Default Factory of {@link FeatureSelection}. Create a new default
275
         * {@link FeatureSelection} instance.<br>
276
         *
277
         * Override this if you need an special implemtation of
278
         * {@link FeatureSelection}.
279
         *
280
         * @return
281
         * @throws DataException
282
         *
283
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#createFeatureSelection()
284
         */
285
        public FeatureSelection createFeatureSelection() throws DataException {
286
                return this.store.createDefaultFeatureSelection();
287
        }
288
289
        /**
290
         * do nothing by default, override this otherwise
291
         *
292
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#refresh()
293
         */
294
        public void refresh() throws OpenException {
295
                // Do nothing by default
296
        }
297
298
        /**
299
         * do nothing by default, override this otherwise
300
         *
301
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#close()
302
         */
303
        public void close() throws CloseException {
304
                // Do nothing by default
305
        }
306
307
        protected void doDispose() throws BaseException {
308
                this.metadata = null;
309
                this.store = null;
310
        }
311
312
        /**
313
         * unsupported geometry by default (return <code>null</code>), override this
314
         * otherwise
315
         *
316
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#getEnvelope()
317
         */
318
        public Envelope getEnvelope() throws DataException {
319
                return null;
320
        }
321
322
        /**
323
         * unsupported geometry write by default (return <code>false</code>),
324
         * override this otherwise
325
         *
326
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#canWriteGeometry(int,
327
         *      int)
328
         */
329
        public boolean canWriteGeometry(int geometryType, int geometrySubType)
330
                        throws DataException {
331
                return false;
332
        }
333
334
        // --- Metadata methods ---
335
336
        public void delegate(DynObject dynObject) {
337
                if (this.metadata == null) {
338
                        return;
339
                }
340
                this.metadata.delegate(dynObject);
341
        }
342
343
        public DynClass getDynClass() {
344
                if (this.metadata == null) {
345
                        return null;
346
                }
347
                return this.metadata.getDynClass();
348
        }
349
350
        public Object getDynValue(String name) throws DynFieldNotFoundException {
351
                if (this.metadata == null) {
352
                        return null;
353
                }
354
                // TODO this.open??
355
                return this.metadata.getDynValue(name);
356
        }
357
358
        public boolean hasDynValue(String name) {
359
                if (this.metadata == null) {
360
                        return false;
361
                }
362
                // TODO this.open??
363
                return this.metadata.hasDynValue(name);
364
        }
365
366
        public void implement(DynClass dynClass) {
367
                if (this.metadata == null) {
368
                        return;
369
                }
370
                this.metadata.implement(dynClass);
371
372
        }
373
374
        public Object invokeDynMethod(int code, DynObject context)
375
                        throws DynMethodException {
376
                if (this.metadata == null) {
377
                        return null;
378
                }
379
                // TODO this.open??
380
                return this.metadata.invokeDynMethod(this, code, context);
381
        }
382
383
        public Object invokeDynMethod(String name, DynObject context)
384
                        throws DynMethodException {
385
                if (this.metadata == null) {
386
                        return null;
387
                }
388
                // TODO this.open??
389
                return this.metadata.invokeDynMethod(this, name, context);
390
        }
391
392
        public void setDynValue(String name, Object value)
393
                        throws DynFieldNotFoundException {
394
                if (this.metadata == null) {
395
                        return;
396
                }
397
                // TODO this.open??
398
                this.metadata.setDynValue(name, value);
399
        }
400
401
        // --- end Metadata methods ---
402
403
        /**
404
         * unsupported by default, override this otherwise
405
         *
406
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#allowAutomaticValues()
407
         */
408
        public boolean allowAutomaticValues() {
409
                return false;
410
411
        }
412
413
        /**
414
         * unsupported by default, override this otherwise
415
         *
416
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#append(org.gvsig.
417
         *      fmap.dal.feature.spi.FeatureProvider)
418
         */
419
        public void append(FeatureProvider featureProvider) throws DataException {
420
                // FIXME exception
421
                throw new UnsupportedOperationException();
422
        }
423
424
        /**
425
         * unsupported by default, override this otherwise
426
         *
427
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#beginAppend()
428
         */
429
        public void beginAppend() throws DataException {
430
                // FIXME exception
431
                throw new UnsupportedOperationException();
432
        }
433
434
        /**
435
         * unsupported by default, override this otherwise
436
         *
437
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#endAppend()
438
         */
439
        public void endAppend() throws DataException {
440
                // FIXME exception
441
                throw new UnsupportedOperationException();
442
        }
443
444
        /**
445
         * unsupported by default, override this otherwise
446
         *
447
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#supportsAppendMode()
448
         */
449
        public boolean supportsAppendMode() {
450
                return false;
451
        }
452
453
        /**
454
         * unsupported by default (return null), override this otherwise
455
         *
456
         * @see org.gvsig.fmap.dal.spi.DataStoreProvider#getChilds()
457
         */
458
        public Iterator getChilds() {
459
                return null;
460
        }
461
462
        /**
463
         * unsupported by default (return null), override this otherwise
464
         *
465
         * @see org.gvsig.fmap.dal.spi.DataStoreProvider#getExplorer()
466
         */
467
        public DataServerExplorer getExplorer() throws ReadException,
468
                        ValidateDataParametersException {
469
                return null;
470
        }
471
472
        public void clear() {
473
                if (metadata != null) {
474
                        metadata.clear();
475
                }
476
        }
477
478
        /**
479
         * Returns a {@link FeatureProvider} by reference, using the provided
480
         * {@link FeatureType}. This is the child classes implementation of the
481
         * {@link #getFeatureProviderByReference(FeatureReferenceProviderServices)}
482
         * method.
483
         *
484
         * @param reference
485
         *            the reference to the {@link FeatureProvider}
486
         * @param featureType
487
         *            the type of feature to load
488
         * @return the {@link FeatureProvider} being referenced
489
         * @throws DataException
490
         *             if there is an error loading the {@link FeatureProvider}
491
         */
492
        protected abstract FeatureProvider internalGetFeatureProviderByReference(
493
                        FeatureReferenceProviderServices reference, FeatureType featureType)
494
                        throws DataException;
495
496
        public static class FeatureProviderNotFoundException extends DataException {
497
498
                private static final long serialVersionUID = 5161749797695723151L;
499
500
                public FeatureProviderNotFoundException(FeatureReference reference) {
501
                        super("Cannot retreive FeatureProvider for reference %(reference)",
502
                                        "_FeatureProviderNotFoundException", serialVersionUID);
503
                        setValue("reference", reference.toString());
504
                }
505
        }
506
507
        public boolean isKnownEnvelope(){
508
            return true;
509
        }
510
511
    public boolean hasRetrievedFeaturesLimit(){
512
        return false;
513
    }
514
515
    public int getRetrievedFeaturesLimit(){
516
        return -1;
517
    }
518
519
    public Interval getInterval() {
520
        return null;
521
    }
522
523
    public Collection getTimes() {
524
        // TODO Auto-generated method stub
525
        return null;
526
    }
527
528
    public Collection getTimes(Interval interval) {
529
        // TODO Auto-generated method stub
530
        return null;
531
    }
532
}