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

History | View | Annotate | Download (16.6 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.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
import org.gvsig.fmap.dal.spi.DataStoreProvider;
61
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.dispose.impl.AbstractDisposable;
65
import org.gvsig.tools.dynobject.DelegatedDynObject;
66
import org.gvsig.tools.dynobject.DynClass;
67
import org.gvsig.tools.dynobject.DynObject;
68
import org.gvsig.tools.dynobject.DynObject_v2;
69
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
70
import org.gvsig.tools.dynobject.exception.DynMethodException;
71
import org.gvsig.tools.exception.BaseException;
72
import org.gvsig.tools.util.UnmodifiableBasicMap;
73

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

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

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

    
87

    
88
        /**
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
         *
109
         * @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
                                        public String toString() {
129
                                            return "getFeatureByReference";
130
                                        }
131

    
132
                                });
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
         *
167
         * @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
        /*
218
         * (non-Javadoc)
219
         *
220
         * @see
221
         * org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#getStoreServices()
222
         */
223
        public FeatureStoreProviderServices getStoreServices() {
224
                return this.store;
225
        }
226

    
227
        public FeatureStore getFeatureStore() {
228
                if(this.store == null){
229
                        return null;
230
                }
231
                return this.store.getFeatureStore();
232
        }
233

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

    
243
        /**
244
         * unsupported by default, override this otherwise
245
         *
246
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#performChanges(Iterator,
247
         *      Iterator, Iterator, Iterator)
248
         */
249

    
250
        public void performChanges(Iterator deleteds, Iterator inserteds,
251
                        Iterator updateds, Iterator featureTypesChanged)
252
                        throws DataException {
253
                // FIXME exception
254
                throw new UnsupportedOperationException();
255

    
256
        }
257

    
258
        /**
259
         * unsupported by default, override this otherwise
260
         *
261
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#isLocksSupported()
262
         */
263
        public boolean isLocksSupported() {
264
                return false;
265
        }
266

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

    
280
        public FeatureProvider createFeatureProvider(FeatureType type)
281
                        throws DataException {
282
                return this.store.createDefaultFeatureProvider(type);
283
        }
284

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

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

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

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

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

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

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

    
356
        // --- Metadata methods ---
357

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

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

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

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

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

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

    
402
        }
403

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

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

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

    
431
        // --- end Metadata methods ---
432

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

    
441
        }
442

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

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

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

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

    
479
        /**
480
         * unsupported by default, override this otherwise
481
         *
482
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#supportsAppendMode()
483
         */
484
        @Override
485
        public boolean supportsAppendMode() {
486
                return false;
487
        }
488

    
489
        @Override
490
        public UnmodifiableBasicMap<String,DataStore> getChildren() {
491
                return UnmodifiableBasicMap.EMPTY_UNMODIFIABLEBASICMAP;
492
        }
493

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

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

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

    
531
        public static class FeatureProviderNotFoundException extends DataException {
532

    
533
                private static final long serialVersionUID = 5161749797695723151L;
534

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

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

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

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

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

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

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

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

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