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

History | View | Annotate | Download (16.3 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.gvsig.expressionevaluator.ExpressionBuilder;
36
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
37
import org.slf4j.Logger;
38
import org.slf4j.LoggerFactory;
39

    
40
import org.gvsig.fmap.dal.DALLocator;
41
import org.gvsig.fmap.dal.DataServerExplorer;
42
import org.gvsig.fmap.dal.DataStoreParameters;
43
import org.gvsig.fmap.dal.exception.CloseException;
44
import org.gvsig.fmap.dal.exception.DataException;
45
import org.gvsig.fmap.dal.exception.InitializeException;
46
import org.gvsig.fmap.dal.exception.OpenException;
47
import org.gvsig.fmap.dal.exception.ReadException;
48
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
49
import org.gvsig.fmap.dal.feature.FeatureLocks;
50
import org.gvsig.fmap.dal.feature.FeatureReference;
51
import org.gvsig.fmap.dal.feature.FeatureSelection;
52
import org.gvsig.fmap.dal.feature.FeatureStore;
53
import org.gvsig.fmap.dal.feature.FeatureType;
54
import org.gvsig.fmap.dal.resource.ResourceAction;
55
import org.gvsig.fmap.dal.resource.ResourceManager;
56
import org.gvsig.fmap.dal.resource.spi.ResourceManagerProviderServices;
57
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
58
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
59
import org.gvsig.fmap.geom.primitive.Envelope;
60
import org.gvsig.timesupport.Interval;
61
import org.gvsig.tools.dispose.impl.AbstractDisposable;
62
import org.gvsig.tools.dynobject.DelegatedDynObject;
63
import org.gvsig.tools.dynobject.DynClass;
64
import org.gvsig.tools.dynobject.DynObject;
65
import org.gvsig.tools.dynobject.DynObject_v2;
66
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
67
import org.gvsig.tools.dynobject.exception.DynMethodException;
68
import org.gvsig.tools.exception.BaseException;
69

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

    
77
        private FeatureStoreProviderServices store;
78
        private DelegatedDynObject metadata;
79
        private DataStoreParameters parameters;
80

    
81
    private static final Logger logger = LoggerFactory.getLogger(AbstractFeatureStoreProvider.class);
82

    
83

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

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

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

    
128
                                });
129

    
130
                if (featureProvider == null) {
131
                        throw new FeatureProviderNotFoundException(reference);
132
                }
133

    
134
                return featureProvider;
135
        }
136

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

    
149
                if (featureProvider == null) {
150
                        throw new FeatureProviderNotFoundException(reference);
151
                }
152

    
153
                return featureProvider;
154
        }
155

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

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

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

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

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

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

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

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

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

    
252
        }
253

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
398
        }
399

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

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

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

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

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

    
437
        }
438

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

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

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

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

    
475
        /**
476
         * unsupported by default, override this otherwise
477
         *
478
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#supportsAppendMode()
479
         */
480
        public boolean supportsAppendMode() {
481
                return false;
482
        }
483

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

    
493
        /**
494
         * unsupported by default (return null), override this otherwise
495
         *
496
         * @see org.gvsig.fmap.dal.spi.DataStoreProvider#getExplorer()
497
         */
498
        public DataServerExplorer getExplorer() throws ReadException,
499
                        ValidateDataParametersException {
500
                return null;
501
        }
502

    
503
        public void clear() {
504
                if (metadata != null) {
505
                        metadata.clear();
506
                }
507
        }
508

    
509
        /**
510
         * Returns a {@link FeatureProvider} by reference, using the provided
511
         * {@link FeatureType}. This is the child classes implementation of the
512
         * {@link #getFeatureProviderByReference(FeatureReferenceProviderServices)}
513
         * method.
514
         *
515
         * @param reference
516
         *            the reference to the {@link FeatureProvider}
517
         * @param featureType
518
         *            the type of feature to load
519
         * @return the {@link FeatureProvider} being referenced
520
         * @throws DataException
521
         *             if there is an error loading the {@link FeatureProvider}
522
         */
523
        protected abstract FeatureProvider internalGetFeatureProviderByReference(
524
                        FeatureReferenceProviderServices reference, FeatureType featureType)
525
                        throws DataException;
526

    
527
        public static class FeatureProviderNotFoundException extends DataException {
528

    
529
                private static final long serialVersionUID = 5161749797695723151L;
530

    
531
                public FeatureProviderNotFoundException(FeatureReference reference) {
532
                        super("Cannot retreive FeatureProvider for reference %(reference)",
533
                                        "_FeatureProviderNotFoundException", serialVersionUID);
534
                        setValue("reference", reference.toString());
535
                }
536
        }
537

    
538
        public boolean isKnownEnvelope(){
539
            return true;
540
        }
541

    
542
    public boolean hasRetrievedFeaturesLimit(){
543
        return false;
544
    }
545

    
546
    public int getRetrievedFeaturesLimit(){
547
        return -1;
548
    }
549

    
550
    public Interval getInterval() {
551
        return null;
552
    }
553

    
554
    public Collection getTimes() {
555
        // TODO Auto-generated method stub
556
        return null;
557
    }
558

    
559
    public Collection getTimes(Interval interval) {
560
        // TODO Auto-generated method stub
561
        return null;
562
    }
563

    
564
    @Override
565
    public ExpressionBuilder createExpression() {
566
        ExpressionBuilder builder = ExpressionEvaluatorLocator.getManager().createExpressionBuilder();
567
        return builder;
568
    }
569

    
570
    protected void savePrjFile(File dataFile, IProjection proj){
571
        File file = new File(FilenameUtils.removeExtension(dataFile.getAbsolutePath())+".prj");
572
        try {
573
            String export = proj.export(ICRSFactory.FORMAT_WKT_ESRI);
574
            if(export!=null){
575
                FileUtils.writeStringToFile(file, export);
576
            }
577
        } catch (Exception e) {
578
            logger.info("Can't write prj file '" + file.getAbsolutePath() + "'.");
579
        }
580
    }
581
}