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

History | View | Annotate | Download (16.8 KB)

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