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

History | View | Annotate | Download (16.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
package org.gvsig.fmap.dal.feature.spi;
26

    
27
import java.io.File;
28
import java.util.Collection;
29
import java.util.Collections;
30
import java.util.Iterator;
31

    
32
import org.apache.commons.io.FileUtils;
33
import org.apache.commons.io.FilenameUtils;
34
import org.cresques.cts.ICRSFactory;
35
import org.cresques.cts.IProjection;
36
import org.gvsig.expressionevaluator.ExpressionBuilder;
37
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
38
import org.slf4j.Logger;
39
import org.slf4j.LoggerFactory;
40

    
41
import org.gvsig.fmap.dal.DALLocator;
42
import org.gvsig.fmap.dal.DataServerExplorer;
43
import org.gvsig.fmap.dal.DataStore;
44
import org.gvsig.fmap.dal.DataStoreParameters;
45
import org.gvsig.fmap.dal.StoresRepository;
46
import org.gvsig.fmap.dal.exception.CloseException;
47
import org.gvsig.fmap.dal.exception.DataException;
48
import org.gvsig.fmap.dal.exception.InitializeException;
49
import org.gvsig.fmap.dal.exception.OpenException;
50
import org.gvsig.fmap.dal.exception.ReadException;
51
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
52
import org.gvsig.fmap.dal.feature.FeatureLocks;
53
import org.gvsig.fmap.dal.feature.FeatureReference;
54
import org.gvsig.fmap.dal.feature.FeatureSelection;
55
import org.gvsig.fmap.dal.feature.FeatureStore;
56
import org.gvsig.fmap.dal.feature.FeatureType;
57
import org.gvsig.fmap.dal.resource.ResourceAction;
58
import org.gvsig.fmap.dal.resource.ResourceManager;
59
import org.gvsig.fmap.dal.resource.spi.ResourceManagerProviderServices;
60
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
61
import org.gvsig.fmap.dal.spi.DataStoreProvider;
62
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
63
import org.gvsig.fmap.geom.primitive.Envelope;
64
import org.gvsig.timesupport.Interval;
65
import org.gvsig.tools.dispose.impl.AbstractDisposable;
66
import org.gvsig.tools.dynobject.DelegatedDynObject;
67
import org.gvsig.tools.dynobject.DynClass;
68
import org.gvsig.tools.dynobject.DynObject;
69
import org.gvsig.tools.dynobject.DynObject_v2;
70
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
71
import org.gvsig.tools.dynobject.exception.DynMethodException;
72
import org.gvsig.tools.exception.BaseException;
73
import org.gvsig.tools.util.UnmodifiableBasicMap;
74

    
75
/**
76
 * Abstract implementation of {@link FeatureStoreProvider}
77
 *
78
 */
79
public abstract class AbstractFeatureStoreProvider extends AbstractDisposable
80
                implements FeatureStoreProvider_v2 {
81

    
82
        protected FeatureStoreProviderServices store;
83
        private DelegatedDynObject metadata;
84
        private DataStoreParameters parameters;
85

    
86
    private static final Logger logger = LoggerFactory.getLogger(AbstractFeatureStoreProvider.class);
87

    
88

    
89
        /**
90
         * Default Constructor.
91
         *
92
         * @param params
93
         * @param storeServices
94
         * @param metadata
95
         */
96
        protected AbstractFeatureStoreProvider(DataStoreParameters params,
97
                        DataStoreProviderServices storeServices, DynObject metadata) {
98
                this.store = (FeatureStoreProviderServices) storeServices;
99
                this.metadata = (DelegatedDynObject) metadata;
100
                this.parameters = params;
101
        }
102

    
103
        /**
104
         * Constructor when cannot create metada in constrution time. <br>
105
         * <br>
106
         * <strong>Note: </strong> Don't use it if not is necesary. Set metada
107
         * <strong>as soon as posible</strong> by
108
         * {@link AbstractFeatureStoreProvider#setMetadata(DynObject)}
109
         *
110
         * @param params
111
         * @param storeServices
112
         */
113
        protected AbstractFeatureStoreProvider(DataStoreParameters params,
114
                        DataStoreProviderServices storeServices) {
115
                this.store = (FeatureStoreProviderServices) storeServices;
116
                this.metadata = null;
117
                this.parameters = params;
118
        }
119

    
120
        public final FeatureProvider getFeatureProviderByReference(
121
                        final FeatureReferenceProviderServices reference)
122
                        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
                                        }
129
                                        public String toString() {
130
                                            return "getFeatureByReference";
131
                                        }
132

    
133
                                });
134

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

    
139
                return featureProvider;
140
        }
141

    
142
        public final FeatureProvider getFeatureProviderByReference(
143
                        final FeatureReferenceProviderServices reference,
144
                        final FeatureType featureType) throws DataException {
145
                this.open();
146
                FeatureProvider featureProvider = (FeatureProvider) getResource()
147
                                .execute(new ResourceAction() {
148
                        public Object run() throws Exception {
149
                                return internalGetFeatureProviderByReference(reference,
150
                                                featureType);
151
                        }
152
                });
153

    
154
                if (featureProvider == null) {
155
                        throw new FeatureProviderNotFoundException(reference);
156
                }
157

    
158
                return featureProvider;
159
        }
160

    
161
        /**
162
         * Returns a {@link FeatureProvider} by reference, using the default
163
         * {@link FeatureType}. This method may be rewritten by the child classes as
164
         * an implementation of the
165
         * {@link #getFeatureProviderByReference(FeatureReferenceProviderServices)}
166
         * method.
167
         *
168
         * @param reference
169
         *            the reference to the {@link FeatureProvider}
170
         * @return the {@link FeatureProvider} being referenced
171
         * @throws DataException
172
         *             if there is an error loading the {@link FeatureProvider}
173
         */
174
        protected FeatureProvider internalGetFeatureProviderByReference(
175
                        FeatureReferenceProviderServices reference) throws DataException {
176
                return internalGetFeatureProviderByReference(reference,
177
                                getStoreServices().getDefaultFeatureType());
178
        }
179

    
180
        /**
181
         * Set metada container if this not set at construction time and only in one
182
         * time. In other case an Exception will be throw
183
         *
184
         * @param metadata
185
         */
186
        protected void setMetadata(DynObject metadata) {
187
                if (this.metadata != null) {
188
                        // FIXME Exception
189
                        throw new IllegalStateException();
190
                }
191
                this.metadata = (DelegatedDynObject) metadata;
192
        }
193

    
194
        /**
195
         * @return the parameters
196
         */
197
        public DataStoreParameters getParameters() {
198
                return parameters;
199
        }
200

    
201
        /**
202
         * Create or get a resource of <code>type</code> for <code>params</code> in
203
         * {@link ResourceManager}
204
         *
205
         * @param type
206
         * @param params
207
         * @return
208
         * @throws InitializeException
209
         */
210
        protected ResourceProvider createResource(String type, Object[] params)
211
                        throws InitializeException {
212
                ResourceManagerProviderServices manager = (ResourceManagerProviderServices) DALLocator
213
                                .getResourceManager();
214
                ResourceProvider resource = manager.createAddResource(type, params);
215
                return resource;
216
        }
217

    
218
        @Override
219
        public FeatureStoreProviderServices getStoreServices() {
220
                return this.store;
221
        }
222

    
223
        @Override
224
        public FeatureStore getFeatureStore() {
225
            FeatureStoreProviderServices services = this.getStoreServices();
226
            if(services == null){
227
                    return null;
228
            }
229
            return services.getFeatureStore();
230
        }
231

    
232
        @Override
233
        public boolean allowWrite() {
234
                return false;
235
        }
236

    
237
        /**
238
         * unsupported by default, override this otherwise
239
         *
240
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#performChanges(Iterator,
241
         *      Iterator, Iterator, Iterator)
242
         */
243

    
244
        public void performChanges(Iterator deleteds, Iterator inserteds,
245
                        Iterator updateds, Iterator featureTypesChanged)
246
                        throws DataException {
247
                // FIXME exception
248
                throw new UnsupportedOperationException();
249

    
250
        }
251

    
252
        /**
253
         * unsupported by default, override this otherwise
254
         *
255
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#isLocksSupported()
256
         */
257
        public boolean isLocksSupported() {
258
                return false;
259
        }
260

    
261
        /**
262
         * Default Factory of {@link FeatureProvider}. Create a new default
263
         * {@link FeatureProvider} instance.<br>
264
         *
265
         * Override this if you need an special implemtation of
266
         * {@link FeatureProvider}.
267
         *
268
         * @return
269
         * @throws DataException
270
         *
271
         * @see {@link org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#createFeatureProvider(FeatureType)}
272
         */
273

    
274
        public FeatureProvider createFeatureProvider(FeatureType type)
275
                        throws DataException {
276
            FeatureStoreProviderServices services = this.getStoreServices();
277
            return services.createDefaultFeatureProvider(type);
278
        }
279

    
280
        /**
281
         * unsupported by default (return <code>null</code>), override this
282
         * otherwise
283
         *
284
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#createFeatureLocks()
285
         */
286
        public FeatureLocks createFeatureLocks() throws DataException {
287
                return null;
288
        }
289

    
290
        /**
291
         * Default Factory of {@link FeatureSelection}. Create a new default
292
         * {@link FeatureSelection} instance.<br>
293
         *
294
         * Override this if you need an special implemtation of
295
         * {@link FeatureSelection}.
296
         *
297
         * @return
298
         * @throws DataException
299
         *
300
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#createFeatureSelection()
301
         */
302
        @Override
303
        public FeatureSelection createFeatureSelection() throws DataException {
304
            FeatureStoreProviderServices services = this.getStoreServices();
305
            return services.createDefaultFeatureSelection();
306
        }
307

    
308
        /**
309
         * do nothing by default, override this otherwise
310
         *
311
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#refresh()
312
         */
313
        public void refresh() throws OpenException {
314
                // Do nothing by default
315
        }
316

    
317
        /**
318
         * do nothing by default, override this otherwise
319
         *
320
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#close()
321
         */
322
        @Override
323
        public void close() throws CloseException {
324
                // Do nothing by default
325
        }
326

    
327
        @Override
328
        protected void doDispose() throws BaseException {
329
                this.metadata = null;
330
                this.store = null;
331
        }
332

    
333
        /**
334
         * unsupported geometry by default (return <code>null</code>), override this
335
         * otherwise
336
         *
337
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#getEnvelope()
338
         */
339
        public Envelope getEnvelope() throws DataException {
340
                return null;
341
        }
342

    
343
        /**
344
         * unsupported geometry write by default (return <code>false</code>),
345
         * override this otherwise
346
         *
347
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#canWriteGeometry(int,
348
         *      int)
349
         */
350
        public boolean canWriteGeometry(int geometryType, int geometrySubType)
351
                        throws DataException {
352
                return false;
353
        }
354

    
355
        // --- Metadata methods ---
356

    
357
        public void delegate(DynObject dynObject) {
358
                if (this.metadata == null) {
359
                        return;
360
                }
361
                this.metadata.delegate(dynObject);
362
        }
363

    
364
        public DynClass getDynClass() {
365
                if (this.metadata == null) {
366
                        return null;
367
                }
368
                return this.metadata.getDynClass();
369
        }
370

    
371
        public Object getDynValue(String name) throws DynFieldNotFoundException {
372
                if (this.metadata == null) {
373
                        return null;
374
                }
375
                // TODO this.open??
376
                return this.metadata.getDynValue(name);
377
        }
378

    
379
        public boolean hasDynValue(String name) {
380
                if (this.metadata == null) {
381
                        return false;
382
                }
383
                // TODO this.open??
384
                return this.metadata.hasDynValue(name);
385
        }
386

    
387
    @Override
388
    public boolean hasDynMethod(String name) {
389
        if( metadata instanceof DynObject_v2 ) {
390
            return ((DynObject_v2)this.metadata).hasDynMethod(name);
391
        }
392
        return false;
393
    }
394

    
395
        public void implement(DynClass dynClass) {
396
                if (this.metadata == null) {
397
                        return;
398
                }
399
                this.metadata.implement(dynClass);
400

    
401
        }
402

    
403
        public Object invokeDynMethod(int code, Object[] args)
404
                        throws DynMethodException {
405
                if (this.metadata == null) {
406
                        return null;
407
                }
408
                // TODO this.open??
409
                return this.metadata.invokeDynMethod(this, code, args);
410
        }
411

    
412
        public Object invokeDynMethod(String name, Object[] args)
413
                        throws DynMethodException {
414
                if (this.metadata == null) {
415
                        return null;
416
                }
417
                // TODO this.open??
418
                return this.metadata.invokeDynMethod(this, name, args);
419
        }
420

    
421
        public void setDynValue(String name, Object value)
422
                        throws DynFieldNotFoundException {
423
                if (this.metadata == null) {
424
                        return;
425
                }
426
                // TODO this.open??
427
                this.metadata.setDynValue(name, value);
428
        }
429

    
430
        // --- end Metadata methods ---
431

    
432
        /**
433
         * unsupported by default, override this otherwise
434
         *
435
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#allowAutomaticValues()
436
         */
437
        public boolean allowAutomaticValues() {
438
                return false;
439

    
440
        }
441

    
442
        /**
443
         * unsupported by default, override this otherwise
444
         *
445
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#append(org.gvsig.
446
         *      fmap.dal.feature.spi.FeatureProvider)
447
         */
448
        public void append(FeatureProvider featureProvider) throws DataException {
449
                // FIXME exception
450
                throw new UnsupportedOperationException();
451
        }
452

    
453
        /**
454
         * unsupported by default, override this otherwise
455
         *
456
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#beginAppend()
457
         */
458
        public void beginAppend() throws DataException {
459
                // FIXME exception
460
                throw new UnsupportedOperationException();
461
        }
462

    
463
        /**
464
         * unsupported by default, override this otherwise
465
         *
466
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#endAppend()
467
         */
468
        public void endAppend() throws DataException {
469
                // FIXME exception
470
                throw new UnsupportedOperationException();
471
        }
472

    
473
        public void abortAppend() throws DataException {
474
                // FIXME exception
475
                throw new UnsupportedOperationException();
476
        }
477

    
478
        @Override
479
        public boolean supportsAppendMode() {
480
                return false;
481
        }
482

    
483
        @Override
484
        public UnmodifiableBasicMap<String,DataStore> getChildren() {
485
                return UnmodifiableBasicMap.EMPTY_UNMODIFIABLEBASICMAP;
486
        }
487

    
488
        @Override
489
        public StoresRepository getStoresRepository() {
490
            return null;
491
        }
492

    
493
        /**
494
         * unsupported by default (return null), override this otherwise
495
         *
496
         * @throws org.gvsig.fmap.dal.exception.ReadException
497
         * @throws org.gvsig.fmap.dal.exception.ValidateDataParametersException
498
         * @see org.gvsig.fmap.dal.spi.DataStoreProvider#getExplorer()
499
         */
500
        @Override
501
        public DataServerExplorer getExplorer() throws ReadException,
502
                        ValidateDataParametersException {
503
                return null;
504
        }
505

    
506
        public void clear() {
507
                if (metadata != null) {
508
                        metadata.clear();
509
                }
510
        }
511

    
512
        /**
513
         * Returns a {@link FeatureProvider} by reference, using the provided
514
         * {@link FeatureType}. This is the child classes implementation of the
515
         * {@link #getFeatureProviderByReference(FeatureReferenceProviderServices)}
516
         * method.
517
         *
518
         * @param reference
519
         *            the reference to the {@link FeatureProvider}
520
         * @param featureType
521
         *            the type of feature to load
522
         * @return the {@link FeatureProvider} being referenced
523
         * @throws DataException
524
         *             if there is an error loading the {@link FeatureProvider}
525
         */
526
        protected abstract FeatureProvider internalGetFeatureProviderByReference(
527
                        FeatureReferenceProviderServices reference, FeatureType featureType)
528
                        throws DataException;
529

    
530
        public static class FeatureProviderNotFoundException extends DataException {
531

    
532
                private static final long serialVersionUID = 5161749797695723151L;
533

    
534
                public FeatureProviderNotFoundException(FeatureReference reference) {
535
                        super("Cannot retreive FeatureProvider for reference %(reference)",
536
                                        "_FeatureProviderNotFoundException", serialVersionUID);
537
                        setValue("reference", reference.toString());
538
                }
539
        }
540

    
541
        public boolean isKnownEnvelope(){
542
            return true;
543
        }
544

    
545
    public boolean hasRetrievedFeaturesLimit(){
546
        return false;
547
    }
548

    
549
    public int getRetrievedFeaturesLimit(){
550
        return -1;
551
    }
552

    
553
    public Interval getInterval() {
554
        return null;
555
    }
556

    
557
    public Collection getTimes() {
558
        // TODO Auto-generated method stub
559
        return null;
560
    }
561

    
562
    public Collection getTimes(Interval interval) {
563
        // TODO Auto-generated method stub
564
        return null;
565
    }
566

    
567
    @Override
568
    public ExpressionBuilder createExpression() {
569
        ExpressionBuilder builder = ExpressionEvaluatorLocator.getManager().createExpressionBuilder();
570
        return builder;
571
    }
572

    
573
    protected void savePrjFile(File dataFile, IProjection proj){
574
        File file = new File(FilenameUtils.removeExtension(dataFile.getAbsolutePath())+".prj");
575
        try {
576
            String export = proj.export(ICRSFactory.FORMAT_WKT_ESRI);
577
            if(export!=null){
578
                FileUtils.writeStringToFile(file, export);
579
            }
580
        } catch (Exception e) {
581
            logger.info("Can't write prj file '" + file.getAbsolutePath() + "'.");
582
        }
583
    }
584
}