Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.api / src / main / java / org / gvsig / fmap / dal / AbstractStoresRepository.java @ 47778

History | View | Annotate | Download (19.6 KB)

1 44304 jjdelcerro
package org.gvsig.fmap.dal;
2
3
import java.util.ArrayList;
4
import java.util.Collection;
5 46050 omartinez
import java.util.Collections;
6 46517 fdiaz
import java.util.HashMap;
7 44304 jjdelcerro
import java.util.Iterator;
8
import java.util.List;
9
import java.util.Map;
10 46050 omartinez
import java.util.Objects;
11 46399 jjdelcerro
import java.util.Set;
12
import java.util.TreeSet;
13 44304 jjdelcerro
import org.apache.commons.lang3.StringUtils;
14 46301 fdiaz
import org.gvsig.fmap.dal.feature.FeatureStore;
15 46517 fdiaz
import org.gvsig.fmap.dal.feature.FeatureType;
16
import org.gvsig.tools.dispose.DisposeUtils;
17 44377 jjdelcerro
import org.gvsig.tools.observer.Notification;
18
import org.gvsig.tools.observer.ObservableHelper;
19
import org.gvsig.tools.observer.Observer;
20 46517 fdiaz
import org.gvsig.tools.util.CachedValue;
21 46050 omartinez
import org.gvsig.tools.util.PropertiesSupport;
22 46295 jjdelcerro
import org.gvsig.tools.util.PropertiesSupportHelper;
23 44304 jjdelcerro
import org.gvsig.tools.util.UnmodifiableBasicSet;
24
import org.gvsig.tools.util.UnmodifiableBasicSetChained;
25 44307 jjdelcerro
import org.slf4j.Logger;
26
import org.slf4j.LoggerFactory;
27 44304 jjdelcerro
28
/**
29
 *
30
 * @author jjdelcerro
31
 */
32
@SuppressWarnings("UseSpecificCatch")
33
public abstract class AbstractStoresRepository  implements StoresRepository {
34 46915 jjdelcerro
35 44307 jjdelcerro
    protected final Logger LOGGER = LoggerFactory.getLogger(AbstractStoresRepository.class);
36 47084 jjdelcerro
37
    private class FeatureTypeCache extends CachedValue<FeatureType> {
38
39
        private final DataStoreParameters parameters;
40
41
        public FeatureTypeCache(DataStoreParameters parameters) {
42
            super(featureTypeExpireTimeInMillis);
43
            this.parameters = parameters;
44
        }
45
46
        @Override
47
        protected void reload() {
48
            DataStore store = null;
49
            try {
50
                store = DALLocator.getDataManager().openStore(
51
                        parameters.getProviderName(),
52
                        parameters,
53
                        false
54
                );
55
                if (!(store instanceof FeatureStore)) {
56
                    return;
57
                }
58
                FeatureType ft = ((FeatureStore) store).getDefaultFeatureType();
59
                this.setValue(ft);
60
                setLabel(name, ft.getLabel());
61
            } catch (Exception ex) {
62
                throw new RuntimeException("Can't get feature type of '" + name + "'.", ex);
63
            } finally {
64
                DisposeUtils.disposeQuietly(store);
65
            }
66
        }
67
    }
68 44307 jjdelcerro
69 44304 jjdelcerro
    private final String name;
70
    private final String label;
71 47043 jjdelcerro
    protected List<StoresRepository> subrepositories;
72 47084 jjdelcerro
    protected Map<String,String> labels;
73 44377 jjdelcerro
    private final ObservableHelper observableHelper;
74 46295 jjdelcerro
    private final PropertiesSupportHelper propertiesHelper;
75 47084 jjdelcerro
    protected Map<String, FeatureTypeCache> featureTypes;
76 46517 fdiaz
    protected long featureTypeExpireTimeInMillis;
77 46915 jjdelcerro
    private DataServerExplorerParameters serverParameters;
78 44304 jjdelcerro
79
    public AbstractStoresRepository(String name) {
80
        this(name, null);
81
    }
82
83
    public AbstractStoresRepository(String name, String label) {
84
        this.name = name;
85
        this.label = label;
86 47043 jjdelcerro
        this.subrepositories = null;
87 44377 jjdelcerro
        this.observableHelper = new ObservableHelper();
88 46295 jjdelcerro
        this.propertiesHelper = new PropertiesSupportHelper();
89 46517 fdiaz
        this.featureTypeExpireTimeInMillis = 5*60*1000;
90 44304 jjdelcerro
    }
91
92
    protected abstract DataStoreParameters getMyParameters(String name);
93
94
    protected abstract boolean isEmptyMyRepository();
95
96
    protected abstract int getMySize();
97
98
    protected abstract UnmodifiableBasicSet<String> getMyKeySet();
99
100 46517 fdiaz
    protected FeatureType getMyFeatureType(String name) {
101 46844 jjdelcerro
        if( StringUtils.isBlank(name) ) {
102
            return null;
103
        }
104 46517 fdiaz
        if(this.featureTypes == null){
105
            this.featureTypes = new HashMap<>();
106
        }
107 47084 jjdelcerro
        FeatureTypeCache featureTypeCache = this.featureTypes.get(name.toLowerCase());
108
        if (featureTypeCache == null) {
109 46517 fdiaz
            DataStoreParameters parameters = this.getMyParameters(name);
110
            if (parameters == null) {
111
                return null;
112
            }
113 47084 jjdelcerro
            featureTypeCache = new FeatureTypeCache(parameters);
114
            this.featureTypes.put(name.toLowerCase(), featureTypeCache);
115 46517 fdiaz
        }
116 47084 jjdelcerro
        return featureTypeCache.get();
117 46517 fdiaz
    }
118 47084 jjdelcerro
119
    protected void setFeatureType(String name, DataStoreParameters parameters, FeatureType ftype) {
120
        if(this.featureTypes == null){
121
            this.featureTypes = new HashMap<>();
122
        }
123
        FeatureTypeCache featureTypeCache = new FeatureTypeCache(parameters);
124
        featureTypeCache.set(ftype);
125
        this.featureTypes.put(name.toLowerCase(), featureTypeCache);
126
    }
127 46517 fdiaz
128 44304 jjdelcerro
    @Override
129 44377 jjdelcerro
    public void addObserver(Observer o) {
130
        this.observableHelper.addObserver(o);
131
    }
132
133
    @Override
134
    public void deleteObserver(Observer o) {
135
        this.observableHelper.deleteObserver(o);
136
    }
137
138
    @Override
139
    public void deleteObservers() {
140
        this.observableHelper.deleteObservers();
141
    }
142
143
    protected Notification notifyObservers(String notificationType, Object value) {
144
        return this.observableHelper.notifyObservers(this, notificationType, value);
145
    }
146
147
    protected Notification notifyObservers(String notificationType, Object value1, Object value2) {
148
        return this.observableHelper.notifyObservers(this, notificationType, value1, value2);
149
    }
150
151
    @Override
152 44304 jjdelcerro
    public void add(String name, DataStoreParameters parameters) {
153
        throw new UnsupportedOperationException();
154
    }
155
156
    @Override
157 47084 jjdelcerro
    public void add(String name, DataStoreParameters parameters, String label) {
158
        throw new UnsupportedOperationException();
159
    }
160
161
    @Override
162 46301 fdiaz
    public void add(String name, FeatureStore store) {
163 47084 jjdelcerro
        add(name, store, null);
164 46301 fdiaz
    }
165
166
    @Override
167 47084 jjdelcerro
    public void add(String name, FeatureStore store, String label) {
168
        FeatureType ft = store.getDefaultFeatureTypeQuietly();
169
        DataStoreParameters params = store.getParameters();
170
        this.add(name, params);
171
        this.setFeatureType(name, params, ft);
172
        this.setLabel(name, label);
173
    }
174
175
    @Override
176 44304 jjdelcerro
    public void remove(String name) {
177
        throw new UnsupportedOperationException();
178
    }
179 44346 jjdelcerro
180
    @Override
181
    public boolean contains(DataStoreParameters parameters) {
182
        for (DataStoreParameters currentParameters : this) {
183 46338 fdiaz
            if( parameters.isTheSameStore(currentParameters) ) {
184 44346 jjdelcerro
                return true;
185
            }
186
        }
187
        return false;
188
    }
189 44304 jjdelcerro
190
    @Override
191 46915 jjdelcerro
    public boolean contains(String name) {
192
        DataStoreParameters parameters = this.getMyParameters(name);
193
        if( parameters!=null ) {
194
            return true;
195
        }
196 47043 jjdelcerro
        for ( StoresRepository theRepository : this.getSubrepositories()) {
197 46915 jjdelcerro
            if( theRepository.contains(name) ) {
198
                return true;
199
            }
200
        }
201
        return false;
202
    }
203
204
    @Override
205 44304 jjdelcerro
    public String getID() {
206
        return this.name;
207
    }
208
209
    @Override
210
    public String getLabel() {
211
        if( StringUtils.isBlank(this.label) ) {
212
            return this.getID();
213
        }
214
        return this.label;
215
    }
216
217
    @Override
218 47084 jjdelcerro
    public String getLabel(String name) {
219
        if( this.labels!=null && this.labels.containsKey(name) ) {
220
            return this.labels.get(name);
221
        }
222
        if( this.featureTypes!=null && this.featureTypes.containsKey(name) ) {
223
            FeatureType ft = this.getFeatureType(name);
224
            String theLabel = ft.getLabel();
225
            if( StringUtils.isBlank(theLabel) ) {
226
                return null;
227
            }
228
            this.labels.put(name, theLabel);
229
            return theLabel;
230
        }
231
        for ( StoresRepository theRepository : this.getSubrepositories()) {
232
            String theLabel = theRepository.getLabel(name);
233
            if( theLabel!=null ) {
234
                return theLabel;
235
            }
236
        }
237
        return null;
238
    }
239
240
    @Override
241
    public String getLabelOrName(String name) {
242
        String theLabel = this.getLabel(name);
243
        if( StringUtils.isBlank(theLabel) ) {
244
            return name;
245
        }
246
        return theLabel;
247
    }
248
249
    protected void setLabel(String name, String label) {
250
        if( StringUtils.isNotBlank(label) ) {
251
            if( this.labels==null ) {
252
                this.labels = new HashMap<>();
253
            }
254
            labels.put(name, label);
255
        }
256
    }
257
258
    @Override
259 44304 jjdelcerro
    public Collection<StoresRepository> getSubrepositories() {
260 47043 jjdelcerro
        if( this.subrepositories == null ) {
261
            this.subrepositories = new ArrayList<>();
262
        }
263 44304 jjdelcerro
        return this.subrepositories;
264
    }
265
266
    @Override
267 44439 jjdelcerro
    public StoresRepository getSubrepository(String Id) {
268 47043 jjdelcerro
        for (StoresRepository repo : this.getSubrepositories()) {
269 44439 jjdelcerro
            if( repo!=null && StringUtils.equalsIgnoreCase(Id, repo.getID()) ) {
270
                return repo;
271
            }
272
        }
273
        return null;
274
    }
275
276
    @Override
277 44304 jjdelcerro
    public boolean addRepository(StoresRepository repository) {
278 44377 jjdelcerro
        if( this.notifyObservers(NOTIFICATION_ADDREPOSITORY, repository).isCanceled() ) {
279
            return false;
280
        }
281 44304 jjdelcerro
        this.removeRepository(repository.getID());
282 47043 jjdelcerro
        this.getSubrepositories().add(repository);
283 44304 jjdelcerro
        return true;
284
    }
285
286
    @Override
287
    public boolean removeRepository(String name) {
288 44377 jjdelcerro
        if( this.notifyObservers(NOTIFICATION_REMOVEREPOSITORY, name).isCanceled() ) {
289
            return false;
290
        }
291 47043 jjdelcerro
        Iterator<StoresRepository> it = this.getSubrepositories().iterator();
292
        while( it.hasNext() ) {
293
            StoresRepository repo = it.next();
294 44304 jjdelcerro
            if( StringUtils.equalsIgnoreCase(name, repo.getID()) ) {
295 47043 jjdelcerro
                it.remove();
296 44304 jjdelcerro
                return true;
297
            }
298
        }
299
        return false;
300
    }
301
302
    @Override
303
    public DataStoreParameters get(String name) {
304
        DataStoreParameters parameters = this.getMyParameters(name);
305
        if( parameters!=null ) {
306
            return parameters;
307
        }
308 47043 jjdelcerro
        for ( StoresRepository theRepository : this.getSubrepositories()) {
309 44304 jjdelcerro
            parameters = theRepository.get(name);
310
            if( parameters!=null ) {
311
                return parameters;
312
            }
313
        }
314
        return null;
315
    }
316 46050 omartinez
317
     public PropertiesSupport getProperties(String name) {
318
         return new PropertiesSupport() {
319
             @Override
320
             public Object getProperty(String name) {
321
                 return null;
322
             }
323 44304 jjdelcerro
324 46050 omartinez
             @Override
325
             public void setProperty(String name, Object value) {
326
327
             }
328
329
             @Override
330
             public Map<String, Object> getProperties() {
331
                 return Collections.EMPTY_MAP;
332
             }
333
         };
334
     }
335
336 44304 jjdelcerro
    @Override
337
    public DataStore getStore(String name) {
338 46050 omartinez
        PropertiesSupport properties = this.getProperties(name);
339
        if(properties!=null &&
340
                StringUtils.equalsIgnoreCase(Objects.toString(properties.getProperty("ignoreDALResource")),"true")) {
341
            return this.getStore(name, true);
342
        }
343
        return this.getStore(name, false);
344
    }
345 47672 jjdelcerro
346
    @Override
347
    public String getStoreId(String label) {
348
        DataStoreParameters parameters = this.getMyParameters(label);
349
        if( parameters!=null ) {
350
            DataStore store = null;
351
            try {
352
                store = DALLocator.getDataManager().openStore(
353
                        parameters.getProviderName(),
354
                        parameters,
355
                        true
356
                );
357
                return store.getName();
358
            } catch (Exception ex) {
359
                throw new RuntimeException("Can't open store '"+label+"'.", ex);
360
            } finally {
361
                DisposeUtils.disposeQuietly(store);
362
            }
363
        }
364
        for ( StoresRepository theRepository : this.getSubrepositories()) {
365
            String s = theRepository.getStoreId(label);
366
            if( s !=null ) {
367
                return s;
368
            }
369
        }
370
        return null;
371
    }
372 46050 omartinez
373
    public DataStore getStore(String name, boolean ignoreDALResource) {
374 44307 jjdelcerro
        DataStoreParameters parameters = this.getMyParameters(name);
375 44304 jjdelcerro
        if( parameters!=null ) {
376
            try {
377
                DataStore store = DALLocator.getDataManager().openStore(
378 46737 jjdelcerro
                        parameters.getProviderName(),
379 46050 omartinez
                        parameters,
380
                        ignoreDALResource
381 44304 jjdelcerro
                );
382
                return store;
383
            } catch (Exception ex) {
384
                throw new RuntimeException("Can't open store '"+name+"'.", ex);
385
            }
386
        }
387 47043 jjdelcerro
        for ( StoresRepository theRepository : this.getSubrepositories()) {
388 44307 jjdelcerro
            DataStore store = theRepository.getStore(name);
389
            if( store!=null ) {
390
                return store;
391
            }
392
        }
393 44304 jjdelcerro
        return null;
394
    }
395
396
    @Override
397 46737 jjdelcerro
    public String getRepositoryIDOfStore(String name) {
398
        DataStoreParameters parameters = this.getMyParameters(name);
399
        if( parameters!=null ) {
400
            return this.getID();
401
        }
402 47043 jjdelcerro
        for ( StoresRepository theRepository : this.getSubrepositories()) {
403 46737 jjdelcerro
            String id = theRepository.getRepositoryIDOfStore(name);
404
            if( id!=null ) {
405
                return id;
406
            }
407
        }
408
        return null;
409
    }
410
411
    public StoresRepository getRepositoryOfStore(String name) {
412
        DataStoreParameters parameters = this.getMyParameters(name);
413
        if( parameters!=null ) {
414
            return this;
415
        }
416 47043 jjdelcerro
        for ( StoresRepository theRepository : this.getSubrepositories()) {
417 46737 jjdelcerro
            StoresRepository repo = theRepository.getRepositoryOfStore(name);
418
            if( repo!=null ) {
419
                return repo;
420
            }
421
        }
422
        return null;
423
    }
424
425
    @Override
426 46517 fdiaz
    public FeatureType getFeatureType(String name) {
427 46844 jjdelcerro
        if( StringUtils.isBlank(name) ) {
428
            return null;
429
        }
430 46517 fdiaz
        FeatureType featureType = this.getMyFeatureType(name);
431
        if( featureType != null ) {
432
            return featureType;
433
        }
434 47043 jjdelcerro
        for ( StoresRepository theRepository : this.getSubrepositories()) {
435 46517 fdiaz
            featureType = theRepository.getFeatureType(name);
436
            if( featureType != null ) {
437
                return featureType;
438
            }
439
        }
440
        return null;
441
    }
442
443
    @Override
444 44304 jjdelcerro
    public boolean containsKey(String key) {
445 46844 jjdelcerro
        DataStoreParameters parameters = this.getMyParameters(key);
446 44304 jjdelcerro
        if( parameters!=null ) {
447
            return true;
448
        }
449 47043 jjdelcerro
        for ( StoresRepository theRepository : this.getSubrepositories()) {
450 44304 jjdelcerro
            if( theRepository.containsKey(key) ) {
451
                return true;
452
            }
453
        }
454
        return false;
455
    }
456
457
    @Override
458
    public UnmodifiableBasicSet<String> keySet() {
459
        List<UnmodifiableBasicSet<String>> sets = new ArrayList<>();
460 44408 jjdelcerro
        UnmodifiableBasicSet<String> keyset = this.getMyKeySet();
461
        if( keyset!=null && !keyset.isEmpty() ) {
462
            sets.add(keyset);
463
        }
464 47043 jjdelcerro
        for (StoresRepository theRepository : this.getSubrepositories()) {
465 44408 jjdelcerro
            if( theRepository!=null ) {
466
                keyset = theRepository.keySet();
467
                if( keyset!=null && !keyset.isEmpty() ) {
468
                    sets.add(keyset);
469
                }
470
            }
471 44304 jjdelcerro
        }
472
        if( sets.isEmpty() ) {
473
            return UnmodifiableBasicSet.EMPTY_UNMODIFIABLEBASICSET;
474
        }
475
        if( sets.size()==1 ) {
476
            return sets.get(0);
477
        }
478
        return new UnmodifiableBasicSetChained(
479
            sets.toArray(
480
                new UnmodifiableBasicSet[sets.size()]
481
            )
482
        );
483
    }
484
485
    @Override
486
    public Map<String, DataStoreParameters> toMap() {
487
        throw new UnsupportedOperationException();
488
    }
489
490
    @Override
491
    public boolean isEmpty() {
492
        if( !this.isEmptyMyRepository() ) {
493
            return false;
494
        }
495 47043 jjdelcerro
        for ( StoresRepository theRepository : this.getSubrepositories()) {
496 44304 jjdelcerro
            if( !theRepository.isEmpty() ) {
497
                return false;
498
            }
499
        }
500
        return true;
501
    }
502
503
    @Override
504
    public int size() {
505
        int sz = 0;
506
        if( !this.isEmptyMyRepository() ) {
507
            sz = this.getMySize();
508
        }
509 47043 jjdelcerro
        for ( StoresRepository theRepository : this.getSubrepositories()) {
510 44304 jjdelcerro
            sz += theRepository.size();
511
        }
512
        return sz;
513
    }
514
515
    @Override
516
    public Iterator<DataStoreParameters> iterator() {
517
        final Iterator<String> it = this.keySet().iterator();
518
        return new Iterator<DataStoreParameters>() {
519
            @Override
520
            public boolean hasNext() {
521
                return it.hasNext();
522
            }
523
524
            @Override
525
            public DataStoreParameters next() {
526
                String name = it.next();
527
                return get(name);
528
            }
529
        };
530
    }
531 46295 jjdelcerro
532
    @Override
533
    public Map<String, Object> getProperties() {
534
        return this.propertiesHelper.getProperties();
535
    }
536
537
    @Override
538
    public Object getProperty(String name) {
539
        return this.propertiesHelper.getProperty(name);
540
    }
541
542
    @Override
543
    public void setProperty(String name, Object value) {
544
        this.propertiesHelper.setProperty(name, value);
545
    }
546 46399 jjdelcerro
547
    public Set<String> keySetDeep() {
548
        Set<String>keys = new TreeSet<>();
549
        UnmodifiableBasicSet<String> localkeys = this.getMyKeySet();
550
        if( localkeys!=null && !localkeys.isEmpty() ) {
551
            for (String key : localkeys) {
552
                keys.add(key);
553
            }
554
        }
555 47043 jjdelcerro
        for (StoresRepository theRepository : this.getSubrepositories()) {
556 46399 jjdelcerro
            if( theRepository!=null ) {
557
                UnmodifiableBasicSet<String> subrepokeys = theRepository.keySet();
558
                if( subrepokeys!=null && !subrepokeys.isEmpty() ) {
559
                    for (String key : subrepokeys) {
560
                        keys.add(key);
561
                    }
562
                }
563
                keys.add(theRepository.getID());
564
            }
565
        }
566
        return keys;
567
    }
568 44304 jjdelcerro
569 46399 jjdelcerro
    public Set<String> keySetShallow() {
570
        Set<String>keys = new TreeSet<>();
571
        UnmodifiableBasicSet<String> localkeys = this.getMyKeySet();
572
        if( localkeys!=null && !localkeys.isEmpty() ) {
573
            for (String key : localkeys) {
574
                keys.add(key);
575
            }
576
        }
577 47043 jjdelcerro
        for (StoresRepository theRepository : this.getSubrepositories()) {
578 46399 jjdelcerro
            if( theRepository!=null ) {
579
                keys.add(theRepository.getID());
580
            }
581
        }
582
        return keys;
583
    }
584 46915 jjdelcerro
585
    /**
586
     * @return the serverParameters
587
     */
588
    @Override
589
    public DataServerExplorerParameters getServerParameters() {
590
        return serverParameters;
591
    }
592
593
    /**
594
     * @param serverParameters the serverParameters to set
595
     */
596
    protected void setServerParameters(DataServerExplorerParameters serverParameters) {
597
        this.serverParameters = serverParameters;
598
    }
599
600
601
    protected boolean isMyServer(DataServerExplorerParameters serverParameters) {
602
        if( this.serverParameters==null || serverParameters==null ) {
603
            return false;
604
        }
605
        return this.serverParameters.isTheSameServerExplorer(serverParameters);
606
    }
607
608
    @Override
609
    public StoresRepository getSubrepository(DataServerExplorer server) {
610
        if( server == null ) {
611
            return null;
612
        }
613
        return this.getSubrepository(server.getParameters());
614
    }
615
616
    @Override
617
    public StoresRepository getSubrepository(DataServerExplorerParameters serverParameters) {
618
        if( this.isMyServer(serverParameters) ) {
619
            return this;
620
        }
621 47043 jjdelcerro
        for (StoresRepository repo : this.getSubrepositories()) {
622 46915 jjdelcerro
            if( repo!=null  ) {
623 46933 jjdelcerro
                StoresRepository r = repo.getSubrepository(serverParameters);
624 46915 jjdelcerro
                if( r!=null ) {
625
                    return repo;
626
                }
627
            }
628
        }
629
        return null;
630
    }
631 46982 jjdelcerro
632
    @Override
633
    public String toString() {
634
        return this.getLabel();
635
    }
636 46915 jjdelcerro
637 44304 jjdelcerro
}