Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / spi / AbstractFeatureStoreProvider.java @ 32880

History | View | Annotate | Download (13.5 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.FeatureSelection;
43
import org.gvsig.fmap.dal.feature.FeatureStore;
44
import org.gvsig.fmap.dal.feature.FeatureType;
45
import org.gvsig.fmap.dal.resource.ResourceAction;
46
import org.gvsig.fmap.dal.resource.ResourceManager;
47
import org.gvsig.fmap.dal.resource.spi.ResourceManagerProviderServices;
48
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
49
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
50
import org.gvsig.fmap.geom.primitive.Envelope;
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
//        protected AbstractFeatureStoreProvider(DataStoreParameters params,
102
//                        DataStoreProviderServices storeServices, DynStruct metadata) {
103
//                this(
104
//                                params, 
105
//                                storeServices, 
106
//                                ToolsLocator.getDynObjectManager().createDynObject((DynClass) metadata)
107
//                );
108
//        }
109
        
110
        public final FeatureProvider getFeatureProviderByReference(
111
                        final FeatureReferenceProviderServices reference)
112
                        throws DataException {
113
                this.open();
114
                return (FeatureProvider) getResource().execute(new ResourceAction() {
115
                        public Object run() throws Exception {
116
                                return internalGetFeatureProviderByReference(reference);
117
                        }
118
                });
119
        }
120

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

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

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

    
167
        /**
168
         * @return the parameters
169
         */
170
        public DataStoreParameters getParameters() {
171
                return parameters;
172
        }
173

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

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

    
201
        public FeatureStore getFeatureStore() {
202
                return this.store.getFeatureStore();
203
        }
204

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

    
214

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

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

    
228
        }
229

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

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

    
252
        public FeatureProvider createFeatureProvider(FeatureType type)
253
                        throws DataException {
254
                return this.store.createDefaultFeatureProvider(type);
255
        }
256

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

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

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

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

    
301
        protected void doDispose() throws BaseException {
302
                this.metadata = null;
303
                this.store = null;
304
        }
305

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

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

    
328
        // --- Metadata methods ---
329

    
330

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

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

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

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

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

    
367
        }
368

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

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

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

    
396
        // --- end Metadata methods ---
397

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

    
406
        }
407

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

    
419
        /**
420
         * unsupported by default, override this otherwise
421
         *
422
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#beginAppend()
423
         */
424
        public void beginAppend() 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#endAppend()
433
         */
434
        public void endAppend() 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#supportsAppendMode()
443
         */
444
        public boolean supportsAppendMode() {
445
                return false;
446
        }
447

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

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

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

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

    
491
}