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 @ 40767

History | View | Annotate | Download (14.6 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

    
25
package org.gvsig.fmap.dal.feature.spi;
26

    
27
import java.util.Collection;
28
import java.util.Iterator;
29

    
30
import org.gvsig.fmap.dal.DALLocator;
31
import org.gvsig.fmap.dal.DataServerExplorer;
32
import org.gvsig.fmap.dal.DataStoreParameters;
33
import org.gvsig.fmap.dal.exception.CloseException;
34
import org.gvsig.fmap.dal.exception.DataException;
35
import org.gvsig.fmap.dal.exception.InitializeException;
36
import org.gvsig.fmap.dal.exception.OpenException;
37
import org.gvsig.fmap.dal.exception.ReadException;
38
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
39
import org.gvsig.fmap.dal.feature.FeatureLocks;
40
import org.gvsig.fmap.dal.feature.FeatureReference;
41
import org.gvsig.fmap.dal.feature.FeatureSelection;
42
import org.gvsig.fmap.dal.feature.FeatureStore;
43
import org.gvsig.fmap.dal.feature.FeatureType;
44
import org.gvsig.fmap.dal.resource.ResourceAction;
45
import org.gvsig.fmap.dal.resource.ResourceManager;
46
import org.gvsig.fmap.dal.resource.spi.ResourceManagerProviderServices;
47
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
48
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
49
import org.gvsig.fmap.geom.primitive.Envelope;
50
import org.gvsig.timesupport.Interval;
51
import org.gvsig.tools.dispose.impl.AbstractDisposable;
52
import org.gvsig.tools.dynobject.DelegatedDynObject;
53
import org.gvsig.tools.dynobject.DynClass;
54
import org.gvsig.tools.dynobject.DynObject;
55
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
56
import org.gvsig.tools.dynobject.exception.DynMethodException;
57
import org.gvsig.tools.exception.BaseException;
58

    
59
/**
60
 * Abstract implementation of {@link FeatureStoreProvider}
61
 *
62
 */
63
public abstract class AbstractFeatureStoreProvider extends AbstractDisposable
64
                implements FeatureStoreProvider {
65

    
66
        private FeatureStoreProviderServices store;
67
        private DelegatedDynObject metadata;
68
        private DataStoreParameters parameters;
69

    
70
        /**
71
         * Default Constructor.
72
         *
73
         * @param params
74
         * @param storeServices
75
         * @param metadata
76
         */
77
        protected AbstractFeatureStoreProvider(DataStoreParameters params,
78
                        DataStoreProviderServices storeServices, DynObject metadata) {
79
                this.store = (FeatureStoreProviderServices) storeServices;
80
                this.metadata = (DelegatedDynObject) metadata;
81
                this.parameters = params;
82
        }
83

    
84
        /**
85
         * Constructor when cannot create metada in constrution time. <br>
86
         * <br>
87
         * <strong>Note: </strong> Don't use it if not is necesary. Set metada
88
         * <strong>as soon as posible</strong> by
89
         * {@link AbstractFeatureStoreProvider#setMetadata(DynObject)}
90
         * 
91
         * @param params
92
         * @param storeServices
93
         */
94
        protected AbstractFeatureStoreProvider(DataStoreParameters params,
95
                        DataStoreProviderServices storeServices) {
96
                this.store = (FeatureStoreProviderServices) storeServices;
97
                this.metadata = null;
98
                this.parameters = params;
99
        }
100

    
101
        public final FeatureProvider getFeatureProviderByReference(
102
                        final FeatureReferenceProviderServices reference)
103
                        throws DataException {
104
                this.open();
105
                FeatureProvider featureProvider = (FeatureProvider) getResource()
106
                                .execute(new ResourceAction() {
107
                                        public Object run() throws Exception {
108
                                                return internalGetFeatureProviderByReference(reference);
109
                                        }
110
                                });
111

    
112
                if (featureProvider == null) {
113
                        throw new FeatureProviderNotFoundException(reference);
114
                }
115

    
116
                return featureProvider;
117
        }
118

    
119
        public final FeatureProvider getFeatureProviderByReference(
120
                        final FeatureReferenceProviderServices reference,
121
                        final FeatureType featureType) throws DataException {
122
                this.open();
123
                FeatureProvider featureProvider = (FeatureProvider) getResource()
124
                                .execute(new ResourceAction() {
125
                        public Object run() throws Exception {
126
                                return internalGetFeatureProviderByReference(reference,
127
                                                featureType);
128
                        }
129
                });
130

    
131
                if (featureProvider == null) {
132
                        throw new FeatureProviderNotFoundException(reference);
133
                }
134

    
135
                return featureProvider;
136
        }
137

    
138
        /**
139
         * Returns a {@link FeatureProvider} by reference, using the default
140
         * {@link FeatureType}. This method may be rewritten by the child classes as
141
         * an implementation of the
142
         * {@link #getFeatureProviderByReference(FeatureReferenceProviderServices)}
143
         * method.
144
         * 
145
         * @param reference
146
         *            the reference to the {@link FeatureProvider}
147
         * @return the {@link FeatureProvider} being referenced
148
         * @throws DataException
149
         *             if there is an error loading the {@link FeatureProvider}
150
         */
151
        protected FeatureProvider internalGetFeatureProviderByReference(
152
                        FeatureReferenceProviderServices reference) throws DataException {
153
                return internalGetFeatureProviderByReference(reference,
154
                                getStoreServices().getDefaultFeatureType());
155
        }
156

    
157
        /**
158
         * Set metada container if this not set at construction time and only in one
159
         * time. In other case an Exception will be throw
160
         *
161
         * @param metadata
162
         */
163
        protected void setMetadata(DynObject metadata) {
164
                if (this.metadata != null) {
165
                        // FIXME Exception
166
                        throw new IllegalStateException();
167
                }
168
                this.metadata = (DelegatedDynObject) metadata;
169
        }
170

    
171
        /**
172
         * @return the parameters
173
         */
174
        public DataStoreParameters getParameters() {
175
                return parameters;
176
        }
177

    
178
        /**
179
         * Create or get a resource of <code>type</code> for <code>params</code> in
180
         * {@link ResourceManager}
181
         *
182
         * @param type
183
         * @param params
184
         * @return
185
         * @throws InitializeException
186
         */
187
        protected ResourceProvider createResource(String type, Object[] params)
188
                        throws InitializeException {
189
                ResourceManagerProviderServices manager = (ResourceManagerProviderServices) DALLocator
190
                                .getResourceManager();
191
                ResourceProvider resource = manager.createAddResource(type, params);
192
                return resource;
193
        }
194

    
195
        /*
196
         * (non-Javadoc)
197
         *
198
         * @see
199
         * org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#getStoreServices()
200
         */
201
        public FeatureStoreProviderServices getStoreServices() {
202
                return this.store;
203
        }
204

    
205
        public FeatureStore getFeatureStore() {
206
                return this.store.getFeatureStore();
207
        }
208

    
209
        /**
210
         * unsupported by default, override this otherwise
211
         *
212
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#allowWrite()
213
         */
214
        public boolean allowWrite() {
215
                return false;
216
        }
217

    
218
        /**
219
         * unsupported by default, override this otherwise
220
         *
221
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#performChanges(Iterator,
222
         *      Iterator, Iterator, Iterator)
223
         */
224

    
225
        public void performChanges(Iterator deleteds, Iterator inserteds,
226
                        Iterator updateds, Iterator featureTypesChanged)
227
                        throws DataException {
228
                // FIXME exception
229
                throw new UnsupportedOperationException();
230

    
231
        }
232

    
233
        /**
234
         * unsupported by default, override this otherwise
235
         *
236
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#isLocksSupported()
237
         */
238
        public boolean isLocksSupported() {
239
                return false;
240
        }
241

    
242
        /**
243
         * Default Factory of {@link FeatureProvider}. Create a new default
244
         * {@link FeatureProvider} instance.<br>
245
         *
246
         * Override this if you need an special implemtation of
247
         * {@link FeatureProvider}.
248
         *
249
         * @return
250
         * @throws DataException
251
         *
252
         * @see {@link org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#createFeatureProvider(FeatureType)}
253
         */
254

    
255
        public FeatureProvider createFeatureProvider(FeatureType type)
256
                        throws DataException {
257
                return this.store.createDefaultFeatureProvider(type);
258
        }
259

    
260
        /**
261
         * unsupported by default (return <code>null</code>), override this
262
         * otherwise
263
         *
264
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#createFeatureLocks()
265
         */
266
        public FeatureLocks createFeatureLocks() throws DataException {
267
                return null;
268
        }
269

    
270
        /**
271
         * Default Factory of {@link FeatureSelection}. Create a new default
272
         * {@link FeatureSelection} instance.<br>
273
         *
274
         * Override this if you need an special implemtation of
275
         * {@link FeatureSelection}.
276
         *
277
         * @return
278
         * @throws DataException
279
         *
280
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#createFeatureSelection()
281
         */
282
        public FeatureSelection createFeatureSelection() throws DataException {
283
                return this.store.createDefaultFeatureSelection();
284
        }
285

    
286
        /**
287
         * do nothing by default, override this otherwise
288
         *
289
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#refresh()
290
         */
291
        public void refresh() throws OpenException {
292
                // Do nothing by default
293
        }
294

    
295
        /**
296
         * do nothing by default, override this otherwise
297
         *
298
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#close()
299
         */
300
        public void close() throws CloseException {
301
                // Do nothing by default
302
        }
303

    
304
        protected void doDispose() throws BaseException {
305
                this.metadata = null;
306
                this.store = null;
307
        }
308

    
309
        /**
310
         * unsupported geometry by default (return <code>null</code>), override this
311
         * otherwise
312
         *
313
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#getEnvelope()
314
         */
315
        public Envelope getEnvelope() throws DataException {
316
                return null;
317
        }
318

    
319
        /**
320
         * unsupported geometry write by default (return <code>false</code>),
321
         * override this otherwise
322
         *
323
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#canWriteGeometry(int,
324
         *      int)
325
         */
326
        public boolean canWriteGeometry(int geometryType, int geometrySubType)
327
                        throws DataException {
328
                return false;
329
        }
330

    
331
        // --- Metadata methods ---
332

    
333
        public void delegate(DynObject dynObject) {
334
                if (this.metadata == null) {
335
                        return;
336
                }
337
                this.metadata.delegate(dynObject);
338
        }
339

    
340
        public DynClass getDynClass() {
341
                if (this.metadata == null) {
342
                        return null;
343
                }
344
                return this.metadata.getDynClass();
345
        }
346

    
347
        public Object getDynValue(String name) throws DynFieldNotFoundException {
348
                if (this.metadata == null) {
349
                        return null;
350
                }
351
                // TODO this.open??
352
                return this.metadata.getDynValue(name);
353
        }
354

    
355
        public boolean hasDynValue(String name) {
356
                if (this.metadata == null) {
357
                        return false;
358
                }
359
                // TODO this.open??
360
                return this.metadata.hasDynValue(name);
361
        }
362

    
363
        public void implement(DynClass dynClass) {
364
                if (this.metadata == null) {
365
                        return;
366
                }
367
                this.metadata.implement(dynClass);
368

    
369
        }
370

    
371
        public Object invokeDynMethod(int code, DynObject context)
372
                        throws DynMethodException {
373
                if (this.metadata == null) {
374
                        return null;
375
                }
376
                // TODO this.open??
377
                return this.metadata.invokeDynMethod(this, code, context);
378
        }
379

    
380
        public Object invokeDynMethod(String name, DynObject context)
381
                        throws DynMethodException {
382
                if (this.metadata == null) {
383
                        return null;
384
                }
385
                // TODO this.open??
386
                return this.metadata.invokeDynMethod(this, name, context);
387
        }
388

    
389
        public void setDynValue(String name, Object value)
390
                        throws DynFieldNotFoundException {
391
                if (this.metadata == null) {
392
                        return;
393
                }
394
                // TODO this.open??
395
                this.metadata.setDynValue(name, value);
396
        }
397

    
398
        // --- end Metadata methods ---
399

    
400
        /**
401
         * unsupported by default, override this otherwise
402
         *
403
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#allowAutomaticValues()
404
         */
405
        public boolean allowAutomaticValues() {
406
                return false;
407

    
408
        }
409

    
410
        /**
411
         * unsupported by default, override this otherwise
412
         *
413
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#append(org.gvsig.
414
         *      fmap.dal.feature.spi.FeatureProvider)
415
         */
416
        public void append(FeatureProvider featureProvider) throws DataException {
417
                // FIXME exception
418
                throw new UnsupportedOperationException();
419
        }
420

    
421
        /**
422
         * unsupported by default, override this otherwise
423
         *
424
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#beginAppend()
425
         */
426
        public void beginAppend() throws DataException {
427
                // FIXME exception
428
                throw new UnsupportedOperationException();
429
        }
430

    
431
        /**
432
         * unsupported by default, override this otherwise
433
         *
434
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#endAppend()
435
         */
436
        public void endAppend() throws DataException {
437
                // FIXME exception
438
                throw new UnsupportedOperationException();
439
        }
440

    
441
        /**
442
         * unsupported by default, override this otherwise
443
         *
444
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#supportsAppendMode()
445
         */
446
        public boolean supportsAppendMode() {
447
                return false;
448
        }
449

    
450
        /**
451
         * unsupported by default (return null), override this otherwise
452
         *
453
         * @see org.gvsig.fmap.dal.spi.DataStoreProvider#getChilds()
454
         */
455
        public Iterator getChilds() {
456
                return null;
457
        }
458

    
459
        /**
460
         * unsupported by default (return null), override this otherwise
461
         *
462
         * @see org.gvsig.fmap.dal.spi.DataStoreProvider#getExplorer()
463
         */
464
        public DataServerExplorer getExplorer() throws ReadException,
465
                        ValidateDataParametersException {
466
                return null;
467
        }
468

    
469
        public void clear() {
470
                if (metadata != null) {
471
                        metadata.clear();
472
                }
473
        }
474

    
475
        /**
476
         * Returns a {@link FeatureProvider} by reference, using the provided
477
         * {@link FeatureType}. This is the child classes implementation of the
478
         * {@link #getFeatureProviderByReference(FeatureReferenceProviderServices)}
479
         * method.
480
         * 
481
         * @param reference
482
         *            the reference to the {@link FeatureProvider}
483
         * @param featureType
484
         *            the type of feature to load
485
         * @return the {@link FeatureProvider} being referenced
486
         * @throws DataException
487
         *             if there is an error loading the {@link FeatureProvider}
488
         */
489
        protected abstract FeatureProvider internalGetFeatureProviderByReference(
490
                        FeatureReferenceProviderServices reference, FeatureType featureType)
491
                        throws DataException;
492

    
493
        public static class FeatureProviderNotFoundException extends DataException {
494

    
495
                private static final long serialVersionUID = 5161749797695723151L;
496

    
497
                public FeatureProviderNotFoundException(FeatureReference reference) {
498
                        super("Cannot retreive FeatureProvider for reference %(reference)",
499
                                        "_FeatureProviderNotFoundException", serialVersionUID);
500
                        setValue("reference", reference.toString());
501
                }
502
        }
503
        
504
        public boolean isKnownEnvelope(){
505
            return true;
506
        }
507
    
508
    public boolean hasRetrievedFeaturesLimit(){
509
        return false;
510
    }
511
    
512
    public int getRetrievedFeaturesLimit(){
513
        return -1;
514
    }
515

    
516
    public Interval getInterval() {       
517
        return null;
518
    }    
519

    
520
    public Collection getTimes() {
521
        // TODO Auto-generated method stub
522
        return null;
523
    }
524

    
525
    public Collection getTimes(Interval interval) {
526
        // TODO Auto-generated method stub
527
        return null;
528
    }
529
}