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

History | View | Annotate | Download (15.7 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.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.DataStoreProviderServices;
57
import org.gvsig.fmap.geom.primitive.Envelope;
58
import org.gvsig.timesupport.Interval;
59
import org.gvsig.tools.dispose.impl.AbstractDisposable;
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.exception.DynFieldNotFoundException;
64
import org.gvsig.tools.dynobject.exception.DynMethodException;
65
import org.gvsig.tools.exception.BaseException;
66

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

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

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

    
80

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

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

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

    
125
                                });
126

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

    
131
                return featureProvider;
132
        }
133

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

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

    
150
                return featureProvider;
151
        }
152

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

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

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

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

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

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

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

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

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

    
249
        }
250

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

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

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

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

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

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

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

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

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

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

    
349
        // --- Metadata methods ---
350

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

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

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

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

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

    
387
        }
388

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

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

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

    
416
        // --- end Metadata methods ---
417

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

    
426
        }
427

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

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

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

    
459
        public void abortAppend() throws DataException {
460
                // FIXME exception
461
                throw new UnsupportedOperationException();
462
        }
463

    
464
        /**
465
         * unsupported by default, override this otherwise
466
         *
467
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#supportsAppendMode()
468
         */
469
        public boolean supportsAppendMode() {
470
                return false;
471
        }
472

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

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

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

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

    
516
        public static class FeatureProviderNotFoundException extends DataException {
517

    
518
                private static final long serialVersionUID = 5161749797695723151L;
519

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

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

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

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

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

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

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

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