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

History | View | Annotate | Download (16.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.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

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

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

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

    
86

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

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

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

    
131
                                });
132

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

    
137
                return featureProvider;
138
        }
139

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

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

    
156
                return featureProvider;
157
        }
158

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

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

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

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

    
216
        /*
217
         * (non-Javadoc)
218
         *
219
         * @see
220
         * org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#getStoreServices()
221
         */
222
        public FeatureStoreProviderServices getStoreServices() {
223
                return this.store;
224
        }
225

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

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

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

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

    
255
        }
256

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

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

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

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

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

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

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

    
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
        /**
479
         * unsupported by default, override this otherwise
480
         *
481
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#supportsAppendMode()
482
         */
483
        public boolean supportsAppendMode() {
484
                return false;
485
        }
486

    
487
        @Override
488
        public Iterator<DataStore> getChildren() {
489
                return Collections.EMPTY_LIST.iterator();
490
        }
491

    
492
        @Override
493
        public DataStore getChild(String name) {
494
            return null;
495
        }
496

    
497
        @Override
498
        public boolean hasChildren() {
499
            return false;
500
        }
501

    
502
        /**
503
         * unsupported by default (return null), override this otherwise
504
         *
505
         * @see org.gvsig.fmap.dal.spi.DataStoreProvider#getExplorer()
506
         */
507
        public DataServerExplorer getExplorer() throws ReadException,
508
                        ValidateDataParametersException {
509
                return null;
510
        }
511

    
512
        public void clear() {
513
                if (metadata != null) {
514
                        metadata.clear();
515
                }
516
        }
517

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

    
536
        public static class FeatureProviderNotFoundException extends DataException {
537

    
538
                private static final long serialVersionUID = 5161749797695723151L;
539

    
540
                public FeatureProviderNotFoundException(FeatureReference reference) {
541
                        super("Cannot retreive FeatureProvider for reference %(reference)",
542
                                        "_FeatureProviderNotFoundException", serialVersionUID);
543
                        setValue("reference", reference.toString());
544
                }
545
        }
546

    
547
        public boolean isKnownEnvelope(){
548
            return true;
549
        }
550

    
551
    public boolean hasRetrievedFeaturesLimit(){
552
        return false;
553
    }
554

    
555
    public int getRetrievedFeaturesLimit(){
556
        return -1;
557
    }
558

    
559
    public Interval getInterval() {
560
        return null;
561
    }
562

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

    
568
    public Collection getTimes(Interval interval) {
569
        // TODO Auto-generated method stub
570
        return null;
571
    }
572

    
573
    @Override
574
    public ExpressionBuilder createExpression() {
575
        ExpressionBuilder builder = ExpressionEvaluatorLocator.getManager().createExpressionBuilder();
576
        return builder;
577
    }
578

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