Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / spi / AbstractFeatureStoreProvider.java @ 35121

History | View | Annotate | Download (14.4 KB)

1
/* 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.Iterator;
31

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

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

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

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

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

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

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

    
117
                return featureProvider;
118
        }
119

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

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

    
136
                return featureProvider;
137
        }
138

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

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

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

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

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

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

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

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

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

    
232
        }
233

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

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

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

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

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

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

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

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

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

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

    
332
        // --- Metadata methods ---
333

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

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

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

    
356
        public boolean hasDynValue(String name) {
357
                if (this.metadata == null) {
358
                        return false;
359
                }
360
                // TODO this.open??
361
                return this.metadata.hasDynValue(name);
362
        }
363
        
364
        public boolean hasEmptyValues(){
365
            if (this.metadata == null) {
366
            return true;
367
        }
368
        return this.metadata.hasEmptyValues();
369
        }
370

    
371
        public void implement(DynClass dynClass) {
372
                if (this.metadata == null) {
373
                        return;
374
                }
375
                this.metadata.implement(dynClass);
376

    
377
        }
378

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

    
388
        public Object invokeDynMethod(String name, DynObject context)
389
                        throws DynMethodException {
390
                if (this.metadata == null) {
391
                        return null;
392
                }
393
                // TODO this.open??
394
                return this.metadata.invokeDynMethod(this, name, context);
395
        }
396

    
397
        public void setDynValue(String name, Object value)
398
                        throws DynFieldNotFoundException {
399
                if (this.metadata == null) {
400
                        return;
401
                }
402
                // TODO this.open??
403
                this.metadata.setDynValue(name, value);
404
        }
405

    
406
        // --- end Metadata methods ---
407

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

    
416
        }
417

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

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

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

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

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

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

    
477
        public void clear() {
478
                if (metadata != null) {
479
                        metadata.clear();
480
                }
481
        }
482

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

    
501
        public static class FeatureProviderNotFoundException extends DataException {
502

    
503
                private static final long serialVersionUID = 5161749797695723151L;
504

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

    
524
}