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

History | View | Annotate | Download (16.1 KB)

1 40559 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40559 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5 40435 jjdelcerro
 *
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 40559 jjdelcerro
 * as published by the Free Software Foundation; either version 3
9 40435 jjdelcerro
 * 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 40559 jjdelcerro
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23 40435 jjdelcerro
 */
24
25
package org.gvsig.fmap.dal.feature.spi;
26
27 42712 fdiaz
import java.io.File;
28 40435 jjdelcerro
import java.util.Collection;
29
import java.util.Iterator;
30
31 42712 fdiaz
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 40435 jjdelcerro
import org.gvsig.fmap.dal.DALLocator;
39
import org.gvsig.fmap.dal.DataServerExplorer;
40
import org.gvsig.fmap.dal.DataStoreParameters;
41 43521 jjdelcerro
import org.gvsig.fmap.dal.ExpressionBuilder;
42 40435 jjdelcerro
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 43246 jjdelcerro
import org.gvsig.tools.dynobject.DynObject_v2;
65 40435 jjdelcerro
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
66
import org.gvsig.tools.dynobject.exception.DynMethodException;
67
import org.gvsig.tools.exception.BaseException;
68
69
/**
70
 * Abstract implementation of {@link FeatureStoreProvider}
71
 *
72
 */
73
public abstract class AbstractFeatureStoreProvider extends AbstractDisposable
74 43020 jjdelcerro
                implements FeatureStoreProvider_v2 {
75 40435 jjdelcerro
76
        private FeatureStoreProviderServices store;
77
        private DelegatedDynObject metadata;
78
        private DataStoreParameters parameters;
79
80 42712 fdiaz
    private static final Logger logger = LoggerFactory.getLogger(AbstractFeatureStoreProvider.class);
81
82
83 40435 jjdelcerro
        /**
84
         * Default Constructor.
85
         *
86
         * @param params
87
         * @param storeServices
88
         * @param metadata
89
         */
90
        protected AbstractFeatureStoreProvider(DataStoreParameters params,
91
                        DataStoreProviderServices storeServices, DynObject metadata) {
92
                this.store = (FeatureStoreProviderServices) storeServices;
93
                this.metadata = (DelegatedDynObject) metadata;
94
                this.parameters = params;
95
        }
96
97
        /**
98
         * Constructor when cannot create metada in constrution time. <br>
99
         * <br>
100
         * <strong>Note: </strong> Don't use it if not is necesary. Set metada
101
         * <strong>as soon as posible</strong> by
102
         * {@link AbstractFeatureStoreProvider#setMetadata(DynObject)}
103 42712 fdiaz
         *
104 40435 jjdelcerro
         * @param params
105
         * @param storeServices
106
         */
107
        protected AbstractFeatureStoreProvider(DataStoreParameters params,
108
                        DataStoreProviderServices storeServices) {
109
                this.store = (FeatureStoreProviderServices) storeServices;
110
                this.metadata = null;
111
                this.parameters = params;
112
        }
113
114
        public final FeatureProvider getFeatureProviderByReference(
115
                        final FeatureReferenceProviderServices reference)
116
                        throws DataException {
117
                this.open();
118
                FeatureProvider featureProvider = (FeatureProvider) getResource()
119
                                .execute(new ResourceAction() {
120
                                        public Object run() throws Exception {
121
                                                return internalGetFeatureProviderByReference(reference);
122
                                        }
123 41437 jjdelcerro
                                        public String toString() {
124
                                            return "getFeatureByReference";
125
                                        }
126 42712 fdiaz
127 40435 jjdelcerro
                                });
128
129
                if (featureProvider == null) {
130
                        throw new FeatureProviderNotFoundException(reference);
131
                }
132
133
                return featureProvider;
134
        }
135
136
        public final FeatureProvider getFeatureProviderByReference(
137
                        final FeatureReferenceProviderServices reference,
138
                        final FeatureType featureType) throws DataException {
139
                this.open();
140
                FeatureProvider featureProvider = (FeatureProvider) getResource()
141
                                .execute(new ResourceAction() {
142
                        public Object run() throws Exception {
143
                                return internalGetFeatureProviderByReference(reference,
144
                                                featureType);
145
                        }
146
                });
147
148
                if (featureProvider == null) {
149
                        throw new FeatureProviderNotFoundException(reference);
150
                }
151
152
                return featureProvider;
153
        }
154
155
        /**
156
         * Returns a {@link FeatureProvider} by reference, using the default
157
         * {@link FeatureType}. This method may be rewritten by the child classes as
158
         * an implementation of the
159
         * {@link #getFeatureProviderByReference(FeatureReferenceProviderServices)}
160
         * method.
161 42712 fdiaz
         *
162 40435 jjdelcerro
         * @param reference
163
         *            the reference to the {@link FeatureProvider}
164
         * @return the {@link FeatureProvider} being referenced
165
         * @throws DataException
166
         *             if there is an error loading the {@link FeatureProvider}
167
         */
168
        protected FeatureProvider internalGetFeatureProviderByReference(
169
                        FeatureReferenceProviderServices reference) throws DataException {
170
                return internalGetFeatureProviderByReference(reference,
171
                                getStoreServices().getDefaultFeatureType());
172
        }
173
174
        /**
175
         * Set metada container if this not set at construction time and only in one
176
         * time. In other case an Exception will be throw
177
         *
178
         * @param metadata
179
         */
180
        protected void setMetadata(DynObject metadata) {
181
                if (this.metadata != null) {
182
                        // FIXME Exception
183
                        throw new IllegalStateException();
184
                }
185
                this.metadata = (DelegatedDynObject) metadata;
186
        }
187
188
        /**
189
         * @return the parameters
190
         */
191
        public DataStoreParameters getParameters() {
192
                return parameters;
193
        }
194
195
        /**
196
         * Create or get a resource of <code>type</code> for <code>params</code> in
197
         * {@link ResourceManager}
198
         *
199
         * @param type
200
         * @param params
201
         * @return
202
         * @throws InitializeException
203
         */
204
        protected ResourceProvider createResource(String type, Object[] params)
205
                        throws InitializeException {
206
                ResourceManagerProviderServices manager = (ResourceManagerProviderServices) DALLocator
207
                                .getResourceManager();
208
                ResourceProvider resource = manager.createAddResource(type, params);
209
                return resource;
210
        }
211
212
        /*
213
         * (non-Javadoc)
214
         *
215
         * @see
216
         * org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#getStoreServices()
217
         */
218
        public FeatureStoreProviderServices getStoreServices() {
219
                return this.store;
220
        }
221
222
        public FeatureStore getFeatureStore() {
223 42064 jbadia
                if(this.store == null){
224
                        return null;
225
                }
226 40435 jjdelcerro
                return this.store.getFeatureStore();
227
        }
228
229
        /**
230
         * unsupported by default, override this otherwise
231
         *
232
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#allowWrite()
233
         */
234
        public boolean allowWrite() {
235
                return false;
236
        }
237
238
        /**
239
         * unsupported by default, override this otherwise
240
         *
241
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#performChanges(Iterator,
242
         *      Iterator, Iterator, Iterator)
243
         */
244
245
        public void performChanges(Iterator deleteds, Iterator inserteds,
246
                        Iterator updateds, Iterator featureTypesChanged)
247
                        throws DataException {
248
                // FIXME exception
249
                throw new UnsupportedOperationException();
250
251
        }
252
253
        /**
254
         * unsupported by default, override this otherwise
255
         *
256
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#isLocksSupported()
257
         */
258
        public boolean isLocksSupported() {
259
                return false;
260
        }
261
262
        /**
263
         * Default Factory of {@link FeatureProvider}. Create a new default
264
         * {@link FeatureProvider} instance.<br>
265
         *
266
         * Override this if you need an special implemtation of
267
         * {@link FeatureProvider}.
268
         *
269
         * @return
270
         * @throws DataException
271
         *
272
         * @see {@link org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#createFeatureProvider(FeatureType)}
273
         */
274
275
        public FeatureProvider createFeatureProvider(FeatureType type)
276
                        throws DataException {
277
                return this.store.createDefaultFeatureProvider(type);
278
        }
279
280
        /**
281
         * unsupported by default (return <code>null</code>), override this
282
         * otherwise
283
         *
284
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#createFeatureLocks()
285
         */
286
        public FeatureLocks createFeatureLocks() throws DataException {
287
                return null;
288
        }
289
290
        /**
291
         * Default Factory of {@link FeatureSelection}. Create a new default
292
         * {@link FeatureSelection} instance.<br>
293
         *
294
         * Override this if you need an special implemtation of
295
         * {@link FeatureSelection}.
296
         *
297
         * @return
298
         * @throws DataException
299
         *
300
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#createFeatureSelection()
301
         */
302
        public FeatureSelection createFeatureSelection() throws DataException {
303
                return this.store.createDefaultFeatureSelection();
304
        }
305
306
        /**
307
         * do nothing by default, override this otherwise
308
         *
309
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#refresh()
310
         */
311
        public void refresh() throws OpenException {
312
                // Do nothing by default
313
        }
314
315
        /**
316
         * do nothing by default, override this otherwise
317
         *
318
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#close()
319
         */
320
        public void close() throws CloseException {
321
                // Do nothing by default
322
        }
323
324
        protected void doDispose() throws BaseException {
325
                this.metadata = null;
326
                this.store = null;
327
        }
328
329
        /**
330
         * unsupported geometry by default (return <code>null</code>), override this
331
         * otherwise
332
         *
333
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#getEnvelope()
334
         */
335
        public Envelope getEnvelope() throws DataException {
336
                return null;
337
        }
338
339
        /**
340
         * unsupported geometry write by default (return <code>false</code>),
341
         * override this otherwise
342
         *
343
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#canWriteGeometry(int,
344
         *      int)
345
         */
346
        public boolean canWriteGeometry(int geometryType, int geometrySubType)
347
                        throws DataException {
348
                return false;
349
        }
350
351
        // --- Metadata methods ---
352
353
        public void delegate(DynObject dynObject) {
354
                if (this.metadata == null) {
355
                        return;
356
                }
357
                this.metadata.delegate(dynObject);
358
        }
359
360
        public DynClass getDynClass() {
361
                if (this.metadata == null) {
362
                        return null;
363
                }
364
                return this.metadata.getDynClass();
365
        }
366
367
        public Object getDynValue(String name) throws DynFieldNotFoundException {
368
                if (this.metadata == null) {
369
                        return null;
370
                }
371
                // TODO this.open??
372
                return this.metadata.getDynValue(name);
373
        }
374
375
        public boolean hasDynValue(String name) {
376
                if (this.metadata == null) {
377
                        return false;
378
                }
379
                // TODO this.open??
380
                return this.metadata.hasDynValue(name);
381
        }
382
383 43246 jjdelcerro
    @Override
384
    public boolean hasDynMethod(String name) {
385
        if( metadata instanceof DynObject_v2 ) {
386
            return ((DynObject_v2)this.metadata).hasDynMethod(name);
387
        }
388
        return false;
389
    }
390
391 40435 jjdelcerro
        public void implement(DynClass dynClass) {
392
                if (this.metadata == null) {
393
                        return;
394
                }
395
                this.metadata.implement(dynClass);
396
397
        }
398
399 42775 jjdelcerro
        public Object invokeDynMethod(int code, Object[] args)
400 40435 jjdelcerro
                        throws DynMethodException {
401
                if (this.metadata == null) {
402
                        return null;
403
                }
404
                // TODO this.open??
405 42775 jjdelcerro
                return this.metadata.invokeDynMethod(this, code, args);
406 40435 jjdelcerro
        }
407
408 42775 jjdelcerro
        public Object invokeDynMethod(String name, Object[] args)
409 40435 jjdelcerro
                        throws DynMethodException {
410
                if (this.metadata == null) {
411
                        return null;
412
                }
413
                // TODO this.open??
414 42775 jjdelcerro
                return this.metadata.invokeDynMethod(this, name, args);
415 40435 jjdelcerro
        }
416
417
        public void setDynValue(String name, Object value)
418
                        throws DynFieldNotFoundException {
419
                if (this.metadata == null) {
420
                        return;
421
                }
422
                // TODO this.open??
423
                this.metadata.setDynValue(name, value);
424
        }
425
426
        // --- end Metadata methods ---
427
428
        /**
429
         * unsupported by default, override this otherwise
430
         *
431
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#allowAutomaticValues()
432
         */
433
        public boolean allowAutomaticValues() {
434
                return false;
435
436
        }
437
438
        /**
439
         * unsupported by default, override this otherwise
440
         *
441
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#append(org.gvsig.
442
         *      fmap.dal.feature.spi.FeatureProvider)
443
         */
444
        public void append(FeatureProvider featureProvider) 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#beginAppend()
453
         */
454
        public void beginAppend() throws DataException {
455
                // FIXME exception
456
                throw new UnsupportedOperationException();
457
        }
458
459
        /**
460
         * unsupported by default, override this otherwise
461
         *
462
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#endAppend()
463
         */
464
        public void endAppend() throws DataException {
465
                // FIXME exception
466
                throw new UnsupportedOperationException();
467
        }
468
469 41536 jjdelcerro
        public void abortAppend() throws DataException {
470
                // FIXME exception
471
                throw new UnsupportedOperationException();
472
        }
473
474 40435 jjdelcerro
        /**
475
         * unsupported by default, override this otherwise
476
         *
477
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#supportsAppendMode()
478
         */
479
        public boolean supportsAppendMode() {
480
                return false;
481
        }
482
483
        /**
484
         * unsupported by default (return null), override this otherwise
485
         *
486
         * @see org.gvsig.fmap.dal.spi.DataStoreProvider#getChilds()
487
         */
488
        public Iterator getChilds() {
489
                return null;
490
        }
491
492
        /**
493
         * unsupported by default (return null), override this otherwise
494
         *
495
         * @see org.gvsig.fmap.dal.spi.DataStoreProvider#getExplorer()
496
         */
497
        public DataServerExplorer getExplorer() throws ReadException,
498
                        ValidateDataParametersException {
499
                return null;
500
        }
501
502
        public void clear() {
503
                if (metadata != null) {
504
                        metadata.clear();
505
                }
506
        }
507
508
        /**
509
         * Returns a {@link FeatureProvider} by reference, using the provided
510
         * {@link FeatureType}. This is the child classes implementation of the
511
         * {@link #getFeatureProviderByReference(FeatureReferenceProviderServices)}
512
         * method.
513 42712 fdiaz
         *
514 40435 jjdelcerro
         * @param reference
515
         *            the reference to the {@link FeatureProvider}
516
         * @param featureType
517
         *            the type of feature to load
518
         * @return the {@link FeatureProvider} being referenced
519
         * @throws DataException
520
         *             if there is an error loading the {@link FeatureProvider}
521
         */
522
        protected abstract FeatureProvider internalGetFeatureProviderByReference(
523
                        FeatureReferenceProviderServices reference, FeatureType featureType)
524
                        throws DataException;
525
526
        public static class FeatureProviderNotFoundException extends DataException {
527
528
                private static final long serialVersionUID = 5161749797695723151L;
529
530
                public FeatureProviderNotFoundException(FeatureReference reference) {
531
                        super("Cannot retreive FeatureProvider for reference %(reference)",
532
                                        "_FeatureProviderNotFoundException", serialVersionUID);
533
                        setValue("reference", reference.toString());
534
                }
535
        }
536 42712 fdiaz
537 40435 jjdelcerro
        public boolean isKnownEnvelope(){
538
            return true;
539
        }
540 42712 fdiaz
541 40435 jjdelcerro
    public boolean hasRetrievedFeaturesLimit(){
542
        return false;
543
    }
544 42712 fdiaz
545 40435 jjdelcerro
    public int getRetrievedFeaturesLimit(){
546
        return -1;
547
    }
548
549 42712 fdiaz
    public Interval getInterval() {
550 40435 jjdelcerro
        return null;
551 42712 fdiaz
    }
552 40435 jjdelcerro
553
    public Collection getTimes() {
554
        // TODO Auto-generated method stub
555
        return null;
556
    }
557
558
    public Collection getTimes(Interval interval) {
559
        // TODO Auto-generated method stub
560
        return null;
561
    }
562 42712 fdiaz
563 43020 jjdelcerro
    @Override
564 43521 jjdelcerro
    public ExpressionBuilder createExpression() {
565 43020 jjdelcerro
        return new SQLBuilderBase();
566
    }
567
568 42712 fdiaz
    protected void savePrjFile(File dataFile, IProjection proj){
569
        File file = new File(FilenameUtils.removeExtension(dataFile.getAbsolutePath())+".prj");
570
        try {
571
            String export = proj.export(ICRSFactory.FORMAT_WKT_ESRI);
572
            if(export!=null){
573
                FileUtils.writeStringToFile(file, export);
574
            }
575
        } catch (Exception e) {
576
            logger.info("Can't write prj file '" + file.getAbsolutePath() + "'.");
577
        }
578
    }
579 40767 jjdelcerro
}