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

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

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

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

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

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

    
257
        }
258

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

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

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

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

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

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

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

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

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

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

    
357
        // --- Metadata methods ---
358

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

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

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

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

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

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

    
403
        }
404

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

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

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

    
432
        // --- end Metadata methods ---
433

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

    
442
        }
443

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

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

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

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

    
480
        @Override
481
        public boolean supportsAppendMode() {
482
                return false;
483
        }
484

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

    
490
        @Override
491
        public StoresRepository getStoresRepository() {
492
            return null;
493
        }
494

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

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

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

    
532
        public static class FeatureProviderNotFoundException extends DataException {
533

    
534
                private static final long serialVersionUID = 5161749797695723151L;
535

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

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

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

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

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

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

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

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

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