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

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

    
31
import org.apache.commons.io.FileUtils;
32
import org.apache.commons.io.FilenameUtils;
33
import org.cresques.cts.ICRSFactory;
34
import org.cresques.cts.IProjection;
35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37

    
38
import org.gvsig.fmap.dal.DALLocator;
39
import org.gvsig.fmap.dal.DataServerExplorer;
40
import org.gvsig.fmap.dal.DataStoreParameters;
41
import org.gvsig.fmap.dal.ExpressionBuilder;
42
import org.gvsig.fmap.dal.exception.CloseException;
43
import org.gvsig.fmap.dal.exception.DataException;
44
import org.gvsig.fmap.dal.exception.InitializeException;
45
import org.gvsig.fmap.dal.exception.OpenException;
46
import org.gvsig.fmap.dal.exception.ReadException;
47
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
48
import org.gvsig.fmap.dal.feature.FeatureLocks;
49
import org.gvsig.fmap.dal.feature.FeatureReference;
50
import org.gvsig.fmap.dal.feature.FeatureSelection;
51
import org.gvsig.fmap.dal.feature.FeatureStore;
52
import org.gvsig.fmap.dal.feature.FeatureType;
53
import org.gvsig.fmap.dal.resource.ResourceAction;
54
import org.gvsig.fmap.dal.resource.ResourceManager;
55
import org.gvsig.fmap.dal.resource.spi.ResourceManagerProviderServices;
56
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
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.dispose.impl.AbstractDisposable;
61
import org.gvsig.tools.dynobject.DelegatedDynObject;
62
import org.gvsig.tools.dynobject.DynClass;
63
import org.gvsig.tools.dynobject.DynObject;
64
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
65
import org.gvsig.tools.dynobject.exception.DynMethodException;
66
import org.gvsig.tools.exception.BaseException;
67

    
68
/**
69
 * Abstract implementation of {@link FeatureStoreProvider}
70
 *
71
 */
72
public abstract class AbstractFeatureStoreProvider extends AbstractDisposable
73
                implements FeatureStoreProvider_v2 {
74

    
75
        private FeatureStoreProviderServices store;
76
        private DelegatedDynObject metadata;
77
        private DataStoreParameters parameters;
78

    
79
    private static final Logger logger = LoggerFactory.getLogger(AbstractFeatureStoreProvider.class);
80

    
81

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

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

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

    
126
                                });
127

    
128
                if (featureProvider == null) {
129
                        throw new FeatureProviderNotFoundException(reference);
130
                }
131

    
132
                return featureProvider;
133
        }
134

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

    
147
                if (featureProvider == null) {
148
                        throw new FeatureProviderNotFoundException(reference);
149
                }
150

    
151
                return featureProvider;
152
        }
153

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

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

    
187
        /**
188
         * @return the parameters
189
         */
190
        public DataStoreParameters getParameters() {
191
                return parameters;
192
        }
193

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

    
211
        /*
212
         * (non-Javadoc)
213
         *
214
         * @see
215
         * org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#getStoreServices()
216
         */
217
        public FeatureStoreProviderServices getStoreServices() {
218
                return this.store;
219
        }
220

    
221
        public FeatureStore getFeatureStore() {
222
                if(this.store == null){
223
                        return null;
224
                }
225
                return this.store.getFeatureStore();
226
        }
227

    
228
        /**
229
         * unsupported by default, override this otherwise
230
         *
231
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#allowWrite()
232
         */
233
        public boolean allowWrite() {
234
                return false;
235
        }
236

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

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

    
250
        }
251

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

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

    
274
        public FeatureProvider createFeatureProvider(FeatureType type)
275
                        throws DataException {
276
                return this.store.createDefaultFeatureProvider(type);
277
        }
278

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

    
289
        /**
290
         * Default Factory of {@link FeatureSelection}. Create a new default
291
         * {@link FeatureSelection} instance.<br>
292
         *
293
         * Override this if you need an special implemtation of
294
         * {@link FeatureSelection}.
295
         *
296
         * @return
297
         * @throws DataException
298
         *
299
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#createFeatureSelection()
300
         */
301
        public FeatureSelection createFeatureSelection() throws DataException {
302
                return this.store.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
        public void close() throws CloseException {
320
                // Do nothing by default
321
        }
322

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

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

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

    
350
        // --- Metadata methods ---
351

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

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

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

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

    
382
        public void implement(DynClass dynClass) {
383
                if (this.metadata == null) {
384
                        return;
385
                }
386
                this.metadata.implement(dynClass);
387

    
388
        }
389

    
390
        public Object invokeDynMethod(int code, Object[] args)
391
                        throws DynMethodException {
392
                if (this.metadata == null) {
393
                        return null;
394
                }
395
                // TODO this.open??
396
                return this.metadata.invokeDynMethod(this, code, args);
397
        }
398

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

    
408
        public void setDynValue(String name, Object value)
409
                        throws DynFieldNotFoundException {
410
                if (this.metadata == null) {
411
                        return;
412
                }
413
                // TODO this.open??
414
                this.metadata.setDynValue(name, value);
415
        }
416

    
417
        // --- end Metadata methods ---
418

    
419
        /**
420
         * unsupported by default, override this otherwise
421
         *
422
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#allowAutomaticValues()
423
         */
424
        public boolean allowAutomaticValues() {
425
                return false;
426

    
427
        }
428

    
429
        /**
430
         * unsupported by default, override this otherwise
431
         *
432
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#append(org.gvsig.
433
         *      fmap.dal.feature.spi.FeatureProvider)
434
         */
435
        public void append(FeatureProvider featureProvider) throws DataException {
436
                // FIXME exception
437
                throw new UnsupportedOperationException();
438
        }
439

    
440
        /**
441
         * unsupported by default, override this otherwise
442
         *
443
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#beginAppend()
444
         */
445
        public void beginAppend() 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#endAppend()
454
         */
455
        public void endAppend() throws DataException {
456
                // FIXME exception
457
                throw new UnsupportedOperationException();
458
        }
459

    
460
        public void abortAppend() 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#supportsAppendMode()
469
         */
470
        public boolean supportsAppendMode() {
471
                return false;
472
        }
473

    
474
        /**
475
         * unsupported by default (return null), override this otherwise
476
         *
477
         * @see org.gvsig.fmap.dal.spi.DataStoreProvider#getChilds()
478
         */
479
        public Iterator getChilds() {
480
                return null;
481
        }
482

    
483
        /**
484
         * unsupported by default (return null), override this otherwise
485
         *
486
         * @see org.gvsig.fmap.dal.spi.DataStoreProvider#getExplorer()
487
         */
488
        public DataServerExplorer getExplorer() throws ReadException,
489
                        ValidateDataParametersException {
490
                return null;
491
        }
492

    
493
        public void clear() {
494
                if (metadata != null) {
495
                        metadata.clear();
496
                }
497
        }
498

    
499
        /**
500
         * Returns a {@link FeatureProvider} by reference, using the provided
501
         * {@link FeatureType}. This is the child classes implementation of the
502
         * {@link #getFeatureProviderByReference(FeatureReferenceProviderServices)}
503
         * method.
504
         *
505
         * @param reference
506
         *            the reference to the {@link FeatureProvider}
507
         * @param featureType
508
         *            the type of feature to load
509
         * @return the {@link FeatureProvider} being referenced
510
         * @throws DataException
511
         *             if there is an error loading the {@link FeatureProvider}
512
         */
513
        protected abstract FeatureProvider internalGetFeatureProviderByReference(
514
                        FeatureReferenceProviderServices reference, FeatureType featureType)
515
                        throws DataException;
516

    
517
        public static class FeatureProviderNotFoundException extends DataException {
518

    
519
                private static final long serialVersionUID = 5161749797695723151L;
520

    
521
                public FeatureProviderNotFoundException(FeatureReference reference) {
522
                        super("Cannot retreive FeatureProvider for reference %(reference)",
523
                                        "_FeatureProviderNotFoundException", serialVersionUID);
524
                        setValue("reference", reference.toString());
525
                }
526
        }
527

    
528
        public boolean isKnownEnvelope(){
529
            return true;
530
        }
531

    
532
    public boolean hasRetrievedFeaturesLimit(){
533
        return false;
534
    }
535

    
536
    public int getRetrievedFeaturesLimit(){
537
        return -1;
538
    }
539

    
540
    public Interval getInterval() {
541
        return null;
542
    }
543

    
544
    public Collection getTimes() {
545
        // TODO Auto-generated method stub
546
        return null;
547
    }
548

    
549
    public Collection getTimes(Interval interval) {
550
        // TODO Auto-generated method stub
551
        return null;
552
    }
553

    
554
    @Override
555
    public ExpressionBuilder createExpressionBuilder() {
556
        return new SQLBuilderBase();
557
    }
558

    
559
    protected void savePrjFile(File dataFile, IProjection proj){
560
        File file = new File(FilenameUtils.removeExtension(dataFile.getAbsolutePath())+".prj");
561
        try {
562
            String export = proj.export(ICRSFactory.FORMAT_WKT_ESRI);
563
            if(export!=null){
564
                FileUtils.writeStringToFile(file, export);
565
            }
566
        } catch (Exception e) {
567
            logger.info("Can't write prj file '" + file.getAbsolutePath() + "'.");
568
        }
569
    }
570
}