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

History | View | Annotate | Download (14.7 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
 * AUTHORS (In addition to CIT):
26
 * 2008 {{Company}}   {{Task}}
27
 */
28

    
29
package org.gvsig.fmap.dal.feature.spi;
30

    
31
import java.util.Collection;
32
import java.util.Iterator;
33

    
34
import org.gvsig.fmap.dal.DALLocator;
35
import org.gvsig.fmap.dal.DataServerExplorer;
36
import org.gvsig.fmap.dal.DataStoreParameters;
37
import org.gvsig.fmap.dal.exception.CloseException;
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.ReadException;
42
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
43
import org.gvsig.fmap.dal.feature.FeatureLocks;
44
import org.gvsig.fmap.dal.feature.FeatureReference;
45
import org.gvsig.fmap.dal.feature.FeatureSelection;
46
import org.gvsig.fmap.dal.feature.FeatureStore;
47
import org.gvsig.fmap.dal.feature.FeatureType;
48
import org.gvsig.fmap.dal.resource.ResourceAction;
49
import org.gvsig.fmap.dal.resource.ResourceManager;
50
import org.gvsig.fmap.dal.resource.spi.ResourceManagerProviderServices;
51
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
52
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
53
import org.gvsig.fmap.geom.primitive.Envelope;
54
import org.gvsig.timesupport.Interval;
55
import org.gvsig.tools.dispose.impl.AbstractDisposable;
56
import org.gvsig.tools.dynobject.DelegatedDynObject;
57
import org.gvsig.tools.dynobject.DynClass;
58
import org.gvsig.tools.dynobject.DynObject;
59
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
60
import org.gvsig.tools.dynobject.exception.DynMethodException;
61
import org.gvsig.tools.exception.BaseException;
62

    
63
/**
64
 * Abstract implementation of {@link FeatureStoreProvider}
65
 *
66
 */
67
public abstract class AbstractFeatureStoreProvider extends AbstractDisposable
68
                implements FeatureStoreProvider {
69

    
70
        private FeatureStoreProviderServices store;
71
        private DelegatedDynObject metadata;
72
        private DataStoreParameters parameters;
73

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

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

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

    
116
                if (featureProvider == null) {
117
                        throw new FeatureProviderNotFoundException(reference);
118
                }
119

    
120
                return featureProvider;
121
        }
122

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

    
135
                if (featureProvider == null) {
136
                        throw new FeatureProviderNotFoundException(reference);
137
                }
138

    
139
                return featureProvider;
140
        }
141

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

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

    
175
        /**
176
         * @return the parameters
177
         */
178
        public DataStoreParameters getParameters() {
179
                return parameters;
180
        }
181

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

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

    
209
        public FeatureStore getFeatureStore() {
210
                return this.store.getFeatureStore();
211
        }
212

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

    
222
        /**
223
         * unsupported by default, override this otherwise
224
         *
225
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#performChanges(Iterator,
226
         *      Iterator, Iterator, Iterator)
227
         */
228

    
229
        public void performChanges(Iterator deleteds, Iterator inserteds,
230
                        Iterator updateds, Iterator featureTypesChanged)
231
                        throws DataException {
232
                // FIXME exception
233
                throw new UnsupportedOperationException();
234

    
235
        }
236

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

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

    
259
        public FeatureProvider createFeatureProvider(FeatureType type)
260
                        throws DataException {
261
                return this.store.createDefaultFeatureProvider(type);
262
        }
263

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

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

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

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

    
308
        protected void doDispose() throws BaseException {
309
                this.metadata = null;
310
                this.store = null;
311
        }
312

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

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

    
335
        // --- Metadata methods ---
336

    
337
        public void delegate(DynObject dynObject) {
338
                if (this.metadata == null) {
339
                        return;
340
                }
341
                this.metadata.delegate(dynObject);
342
        }
343

    
344
        public DynClass getDynClass() {
345
                if (this.metadata == null) {
346
                        return null;
347
                }
348
                return this.metadata.getDynClass();
349
        }
350

    
351
        public Object getDynValue(String name) throws DynFieldNotFoundException {
352
                if (this.metadata == null) {
353
                        return null;
354
                }
355
                // TODO this.open??
356
                return this.metadata.getDynValue(name);
357
        }
358

    
359
        public boolean hasDynValue(String name) {
360
                if (this.metadata == null) {
361
                        return false;
362
                }
363
                // TODO this.open??
364
                return this.metadata.hasDynValue(name);
365
        }
366

    
367
        public void implement(DynClass dynClass) {
368
                if (this.metadata == null) {
369
                        return;
370
                }
371
                this.metadata.implement(dynClass);
372

    
373
        }
374

    
375
        public Object invokeDynMethod(int code, DynObject context)
376
                        throws DynMethodException {
377
                if (this.metadata == null) {
378
                        return null;
379
                }
380
                // TODO this.open??
381
                return this.metadata.invokeDynMethod(this, code, context);
382
        }
383

    
384
        public Object invokeDynMethod(String name, DynObject context)
385
                        throws DynMethodException {
386
                if (this.metadata == null) {
387
                        return null;
388
                }
389
                // TODO this.open??
390
                return this.metadata.invokeDynMethod(this, name, context);
391
        }
392

    
393
        public void setDynValue(String name, Object value)
394
                        throws DynFieldNotFoundException {
395
                if (this.metadata == null) {
396
                        return;
397
                }
398
                // TODO this.open??
399
                this.metadata.setDynValue(name, value);
400
        }
401

    
402
        // --- end Metadata methods ---
403

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

    
412
        }
413

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

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

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

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

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

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

    
473
        public void clear() {
474
                if (metadata != null) {
475
                        metadata.clear();
476
                }
477
        }
478

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

    
497
        public static class FeatureProviderNotFoundException extends DataException {
498

    
499
                private static final long serialVersionUID = 5161749797695723151L;
500

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

    
520
    public Interval getInterval() {       
521
        return null;
522
    }    
523

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

    
529
    public Collection getTimes(Interval interval) {
530
        // TODO Auto-generated method stub
531
        return null;
532
    }
533
}