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

History | View | Annotate | Download (17.5 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.Iterator;
30
import org.apache.commons.io.FileUtils;
31
import org.apache.commons.io.FilenameUtils;
32
import org.cresques.cts.ICRSFactory;
33
import org.cresques.cts.IProjection;
34
import org.gvsig.expressionevaluator.ExpressionBuilder;
35
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
36
import org.gvsig.fmap.dal.DALLocator;
37
import org.gvsig.fmap.dal.DataServerExplorer;
38
import org.gvsig.fmap.dal.DataStore;
39
import org.gvsig.fmap.dal.DataStoreParameters;
40
import org.gvsig.fmap.dal.StoresRepository;
41
import org.gvsig.fmap.dal.exception.CloseException;
42
import org.gvsig.fmap.dal.exception.DataException;
43
import org.gvsig.fmap.dal.exception.InitializeException;
44
import org.gvsig.fmap.dal.exception.OpenException;
45
import org.gvsig.fmap.dal.exception.ReadException;
46
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
47
import org.gvsig.fmap.dal.feature.FeatureLocks;
48
import org.gvsig.fmap.dal.feature.FeatureReference;
49
import org.gvsig.fmap.dal.feature.FeatureSelection;
50
import org.gvsig.fmap.dal.feature.FeatureStore;
51
import org.gvsig.fmap.dal.feature.FeatureType;
52
import org.gvsig.fmap.dal.resource.ResourceAction;
53
import org.gvsig.fmap.dal.resource.ResourceManager;
54
import org.gvsig.fmap.dal.resource.spi.ResourceManagerProviderServices;
55
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
56
import org.gvsig.fmap.dal.spi.AbstractDataStoreProvider;
57
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
58
import org.gvsig.fmap.geom.primitive.Envelope;
59
import org.gvsig.timesupport.Interval;
60
import org.gvsig.tools.dynobject.DelegatedDynObject;
61
import org.gvsig.tools.dynobject.DynClass;
62
import org.gvsig.tools.dynobject.DynObject;
63
import org.gvsig.tools.dynobject.DynObject_v2;
64
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
65
import org.gvsig.tools.dynobject.exception.DynMethodException;
66
import org.gvsig.tools.exception.BaseException;
67
import org.gvsig.tools.resourcesstorage.ResourcesStorage;
68
import org.gvsig.tools.util.UnmodifiableBasicMap;
69
import org.slf4j.Logger;
70
import org.slf4j.LoggerFactory;
71

    
72
/**
73
 * Abstract implementation of {@link FeatureStoreProvider}
74
 *
75
 */
76
public abstract class AbstractFeatureStoreProvider extends AbstractDataStoreProvider
77
                implements FeatureStoreProvider_v2 {
78

    
79
        protected FeatureStoreProviderServices store;
80
        private DelegatedDynObject metadata;
81
        private DataStoreParameters parameters;
82

    
83
    private static final Logger logger = LoggerFactory.getLogger(AbstractFeatureStoreProvider.class);
84

    
85

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

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

    
117
        public final FeatureProvider getFeatureProviderByReference(
118
                        final FeatureReferenceProviderServices reference)
119
                        throws DataException {
120
                this.open();
121
                FeatureProvider featureProvider = (FeatureProvider) getResource()
122
                                .execute(new ResourceAction() {
123
                                        public Object run() throws Exception {
124
                                                return internalGetFeatureProviderByReference(reference);
125
                                        }
126
                                        public String toString() {
127
                                            return "getFeatureByReference";
128
                                        }
129

    
130
                                });
131

    
132
                if (featureProvider == null) {
133
                        throw new FeatureProviderNotFoundException(reference);
134
                }
135

    
136
                return featureProvider;
137
        }
138

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

    
151
                if (featureProvider == null) {
152
                        throw new FeatureProviderNotFoundException(reference);
153
                }
154

    
155
                return featureProvider;
156
        }
157

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

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

    
191
        /**
192
         * @return the parameters
193
         */
194
        public DataStoreParameters getParameters() {
195
                return parameters;
196
        }
197

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

    
215
        @Override
216
        public FeatureStoreProviderServices getStoreServices() {
217
                return this.store;
218
        }
219

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

    
229
        @Override
230
        public boolean allowWrite() {
231
                return false;
232
        }
233

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

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

    
247
        }
248

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

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

    
271
        public FeatureProvider createFeatureProvider(FeatureType type)
272
                        throws DataException {
273
            FeatureStoreProviderServices services = this.getStoreServices();
274
            return services.createDefaultFeatureProvider(type);
275
        }
276

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

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

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

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

    
324
        @Override
325
        protected void doDispose() throws BaseException {
326
                this.metadata = null;
327
                this.store = null;
328
        }
329

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

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

    
352
        // --- Metadata methods ---
353

    
354
        public void delegate(DynObject dynObject) {
355
                if (this.metadata == null) {
356
                        return;
357
                }
358
                this.metadata.delegate(dynObject);
359
        }
360

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

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

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

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

    
392
        public void implement(DynClass dynClass) {
393
                if (this.metadata == null) {
394
                        return;
395
                }
396
                this.metadata.implement(dynClass);
397

    
398
        }
399

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

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

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

    
427
        // --- end Metadata methods ---
428

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

    
437
        }
438

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

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

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

    
470
        public void abortAppend() throws DataException {
471
                // FIXME exception
472
                throw new UnsupportedOperationException();
473
        }
474

    
475
        @Override
476
        public boolean supportsAppendMode() {
477
                return false;
478
        }
479

    
480
        @Override
481
        public UnmodifiableBasicMap<String,DataStore> getChildren() {
482
                return UnmodifiableBasicMap.EMPTY_UNMODIFIABLEBASICMAP;
483
        }
484

    
485
        @Override
486
        public StoresRepository getStoresRepository() {
487
            return null;
488
        }
489
        
490
        @Override
491
        public ResourcesStorage getResourcesStorage() {
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

    
587
    @Override
588
    public boolean isTemporary() {
589
        return false;
590
    }
591

    
592
    @Override
593
    public void fixFeatureTypeFromParameters() {
594
        
595
    }
596

    
597
    @Override
598
    public boolean supportsPassThroughMode() {
599
        return false;
600
    }
601

    
602
    @Override
603
    public void passThroughInsert(FeatureProvider featureProvider) throws DataException {
604
        throw new UnsupportedOperationException();
605
    }
606

    
607
    @Override
608
    public void passThroughUpdate(FeatureProvider featureProvider) throws DataException {
609
        throw new UnsupportedOperationException();
610
    }
611
    
612
    @Override
613
    public void passThroughDelete(FeatureReferenceProviderServices featureReference) throws DataException {
614
        throw new UnsupportedOperationException();
615
    }
616

    
617
}