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

History | View | Annotate | Download (13.2 KB)

1
package org.gvsig.fmap.dal;
2

    
3
import java.util.ArrayList;
4
import java.util.Collection;
5
import java.util.Collections;
6
import java.util.HashMap;
7
import java.util.Iterator;
8
import java.util.List;
9
import java.util.Map;
10
import java.util.Objects;
11
import java.util.Set;
12
import java.util.TreeSet;
13
import org.apache.commons.lang3.StringUtils;
14
import org.gvsig.fmap.dal.feature.FeatureStore;
15
import org.gvsig.fmap.dal.feature.FeatureType;
16
import org.gvsig.tools.dispose.DisposeUtils;
17
import org.gvsig.tools.observer.Notification;
18
import org.gvsig.tools.observer.ObservableHelper;
19
import org.gvsig.tools.observer.Observer;
20
import org.gvsig.tools.util.CachedValue;
21
import org.gvsig.tools.util.PropertiesSupport;
22
import org.gvsig.tools.util.PropertiesSupportHelper;
23
import org.gvsig.tools.util.UnmodifiableBasicSet;
24
import org.gvsig.tools.util.UnmodifiableBasicSetChained;
25
import org.slf4j.Logger;
26
import org.slf4j.LoggerFactory;
27

    
28
/**
29
 *
30
 * @author jjdelcerro
31
 */
32
@SuppressWarnings("UseSpecificCatch")
33
public abstract class AbstractStoresRepository  implements StoresRepository {
34
    
35
    protected final Logger LOGGER = LoggerFactory.getLogger(AbstractStoresRepository.class);
36
    
37
    private final String name;
38
    private final String label;
39
    private final List<StoresRepository> subrepositories;
40
    private final ObservableHelper observableHelper;
41
    private final PropertiesSupportHelper propertiesHelper;
42
    protected Map<String, CachedValue<FeatureType>> featureTypes;
43
    protected long featureTypeExpireTimeInMillis;
44

    
45
    public AbstractStoresRepository(String name) {
46
        this(name, null);
47
    }
48
    
49
    public AbstractStoresRepository(String name, String label) {
50
        this.name = name;
51
        this.label = label;
52
        this.subrepositories = new ArrayList<>();
53
        this.observableHelper = new ObservableHelper();
54
        this.propertiesHelper = new PropertiesSupportHelper();
55
        this.featureTypeExpireTimeInMillis = 5*60*1000;
56
    }
57
    
58
    protected abstract DataStoreParameters getMyParameters(String name);
59

    
60
    protected abstract boolean isEmptyMyRepository();
61
    
62
    protected abstract int getMySize();
63

    
64
    protected abstract UnmodifiableBasicSet<String> getMyKeySet();
65

    
66
    protected FeatureType getMyFeatureType(String name) {
67
        if(this.featureTypes == null){
68
            this.featureTypes = new HashMap<>();
69
        }
70
        CachedValue<FeatureType> featureType = this.featureTypes.get(name.toLowerCase());
71
        if (featureType == null) {
72
            DataStoreParameters parameters = this.getMyParameters(name);
73
            if (parameters == null) {
74
                return null;
75
            }
76
            featureType = new CachedValue<FeatureType>(this.featureTypeExpireTimeInMillis){
77
                @Override
78
                protected void reload() {
79
                    DataStore store = null;
80
                    try {
81
                        store = DALLocator.getDataManager().openStore(
82
                                parameters.getDataStoreName(),
83
                                parameters,
84
                                false
85
                        );
86
                        if (!(store instanceof FeatureStore)) {
87
                            return;
88
                        }
89
                        this.setValue(((FeatureStore) store).getDefaultFeatureType());
90
                    } catch (Exception ex) {
91
                        throw new RuntimeException("Can't get feature type of '" + name + "'.", ex);
92
                    } finally {
93
                        DisposeUtils.disposeQuietly(store);
94
                    }
95
                }
96
            };
97
            
98
            this.featureTypes.put(name.toLowerCase(), featureType);
99

    
100
        }
101
        return featureType.get();
102
    }
103

    
104
    @Override
105
    public void addObserver(Observer o) {
106
        this.observableHelper.addObserver(o);
107
    }
108

    
109
    @Override
110
    public void deleteObserver(Observer o) {
111
        this.observableHelper.deleteObserver(o);
112
    }
113

    
114
    @Override
115
    public void deleteObservers() {
116
        this.observableHelper.deleteObservers();
117
    }
118
    
119
    protected Notification notifyObservers(String notificationType, Object value) {
120
        return this.observableHelper.notifyObservers(this, notificationType, value);
121
    }
122

    
123
    protected Notification notifyObservers(String notificationType, Object value1, Object value2) {
124
        return this.observableHelper.notifyObservers(this, notificationType, value1, value2);
125
    }
126

    
127
    @Override
128
    public void add(String name, DataStoreParameters parameters) {
129
        throw new UnsupportedOperationException();
130
    }
131

    
132
    @Override
133
    public void add(String name, FeatureStore store) {
134
        add(name, store.getParameters());
135
    }
136

    
137
    @Override
138
    public void remove(String name) {
139
        throw new UnsupportedOperationException();
140
    }
141
    
142
    @Override
143
    public boolean contains(DataStoreParameters parameters) {
144
        for (DataStoreParameters currentParameters : this) {
145
            if( parameters.isTheSameStore(currentParameters) ) {
146
                return true;
147
            }
148
        }
149
        return false;
150
    }
151

    
152
    @Override
153
    public String getID() {
154
        return this.name;
155
    }
156

    
157
    @Override
158
    public String getLabel() {
159
        if( StringUtils.isBlank(this.label) ) {
160
            return this.getID();
161
        }
162
        return this.label;
163
    }
164
    
165
    @Override
166
    public Collection<StoresRepository> getSubrepositories() {
167
        return this.subrepositories;
168
    }
169

    
170
    @Override
171
    public StoresRepository getSubrepository(String Id) {
172
        for (StoresRepository repo : this.subrepositories) {
173
            if( repo!=null && StringUtils.equalsIgnoreCase(Id, repo.getID()) ) {
174
                return repo;
175
            }
176
        }
177
        return null;
178
    }
179
    
180
    @Override
181
    public boolean addRepository(StoresRepository repository) {
182
        if( this.notifyObservers(NOTIFICATION_ADDREPOSITORY, repository).isCanceled() ) {
183
            return false;
184
        }
185
        this.removeRepository(repository.getID());
186
        this.subrepositories.add(repository);
187
        return true;
188
    }
189

    
190
    @Override
191
    public boolean removeRepository(String name) {
192
        if( this.notifyObservers(NOTIFICATION_REMOVEREPOSITORY, name).isCanceled() ) {
193
            return false;
194
        }
195
        for (int i = 0; i < subrepositories.size(); i++) {
196
            StoresRepository repo = subrepositories.get(i);
197
            if( StringUtils.equalsIgnoreCase(name, repo.getID()) ) {
198
                this.subrepositories.remove(i);
199
                return true;
200
            }
201
        }
202
        return false;
203
    }
204

    
205
    @Override
206
    public DataStoreParameters get(String name) {
207
        DataStoreParameters parameters = this.getMyParameters(name);
208
        if( parameters!=null ) {
209
            return parameters;
210
        }
211
        for ( StoresRepository theRepository : this.subrepositories) {
212
            parameters = theRepository.get(name);
213
            if( parameters!=null ) {
214
                return parameters;
215
            }
216
        }
217
        return null;
218
    }
219
    
220
     public PropertiesSupport getProperties(String name) {
221
         return new PropertiesSupport() {
222
             @Override
223
             public Object getProperty(String name) {
224
                 return null;
225
             }
226

    
227
             @Override
228
             public void setProperty(String name, Object value) {
229
                 
230
             }
231

    
232
             @Override
233
             public Map<String, Object> getProperties() {
234
                 return Collections.EMPTY_MAP;
235
             }
236
         };
237
     }
238
    
239
    @Override
240
    public DataStore getStore(String name) {
241
        PropertiesSupport properties = this.getProperties(name);
242
        if(properties!=null && 
243
                StringUtils.equalsIgnoreCase(Objects.toString(properties.getProperty("ignoreDALResource")),"true")) {
244
            return this.getStore(name, true);
245
        }
246
        return this.getStore(name, false);
247
    }
248

    
249
    public DataStore getStore(String name, boolean ignoreDALResource) {
250
        DataStoreParameters parameters = this.getMyParameters(name);
251
        if( parameters!=null ) {
252
            try {
253
                DataStore store = DALLocator.getDataManager().openStore(
254
                        parameters.getDataStoreName(), 
255
                        parameters,
256
                        ignoreDALResource
257
                );
258
                return store;
259
            } catch (Exception ex) {
260
                throw new RuntimeException("Can't open store '"+name+"'.", ex);
261
            }
262
        }
263
        for ( StoresRepository theRepository : this.subrepositories) {
264
            DataStore store = theRepository.getStore(name);
265
            if( store!=null ) {
266
                return store;
267
            }
268
        }
269
        return null;
270
    }
271

    
272
    @Override
273
    public FeatureType getFeatureType(String name) {
274
        FeatureType featureType = this.getMyFeatureType(name);
275
        if( featureType != null ) {
276
            return featureType;
277
        }
278
        for ( StoresRepository theRepository : this.subrepositories) {
279
            featureType = theRepository.getFeatureType(name);
280
            if( featureType != null ) {
281
                return featureType;
282
            }
283
        }
284
        return null;
285
    }
286

    
287
    @Override
288
    public boolean containsKey(String key) {
289
        DataStoreParameters parameters = this.getMyParameters(name);
290
        if( parameters!=null ) {
291
            return true;
292
        }
293
        for ( StoresRepository theRepository : this.subrepositories) {
294
            if( theRepository.containsKey(key) ) {
295
                return true;
296
            }
297
        }
298
        return false;
299
    }
300

    
301
    @Override
302
    public UnmodifiableBasicSet<String> keySet() {
303
        List<UnmodifiableBasicSet<String>> sets = new ArrayList<>();
304
        UnmodifiableBasicSet<String> keyset = this.getMyKeySet();
305
        if( keyset!=null && !keyset.isEmpty() ) {
306
            sets.add(keyset);
307
        }
308
        for (StoresRepository theRepository : this.subrepositories) {
309
            if( theRepository!=null ) {
310
                keyset = theRepository.keySet();
311
                if( keyset!=null && !keyset.isEmpty() ) {
312
                    sets.add(keyset);
313
                }
314
            }
315
        }
316
        if( sets.isEmpty() ) {
317
            return UnmodifiableBasicSet.EMPTY_UNMODIFIABLEBASICSET;
318
        }
319
        if( sets.size()==1 ) {
320
            return sets.get(0);
321
        }
322
        return new UnmodifiableBasicSetChained(
323
            sets.toArray(
324
                new UnmodifiableBasicSet[sets.size()]
325
            )
326
        );
327
    }
328

    
329
    @Override
330
    public Map<String, DataStoreParameters> toMap() {
331
        throw new UnsupportedOperationException();
332
    }
333

    
334
    @Override
335
    public boolean isEmpty() {
336
        if( !this.isEmptyMyRepository() ) {
337
            return false;
338
        }
339
        for ( StoresRepository theRepository : this.subrepositories) {
340
            if( !theRepository.isEmpty() ) {
341
                return false;
342
            }
343
        }
344
        return true;
345
    }
346

    
347
    @Override
348
    public int size() {
349
        int sz = 0;
350
        if( !this.isEmptyMyRepository() ) {
351
            sz = this.getMySize();
352
        }
353
        for ( StoresRepository theRepository : this.subrepositories) {
354
            sz += theRepository.size();
355
        }
356
        return sz;
357
    }
358

    
359
    @Override
360
    public Iterator<DataStoreParameters> iterator() {
361
        final Iterator<String> it = this.keySet().iterator();
362
        return new Iterator<DataStoreParameters>() {
363
            @Override
364
            public boolean hasNext() {
365
                return it.hasNext();
366
            }
367

    
368
            @Override
369
            public DataStoreParameters next() {
370
                String name = it.next();
371
                return get(name);
372
            }
373
        };
374
    }
375

    
376
    @Override
377
    public Map<String, Object> getProperties() {
378
        return this.propertiesHelper.getProperties();
379
    }
380

    
381
    @Override
382
    public Object getProperty(String name) {
383
        return this.propertiesHelper.getProperty(name);
384
    }
385

    
386
    @Override
387
    public void setProperty(String name, Object value) {
388
        this.propertiesHelper.setProperty(name, value);
389
    }
390

    
391
    public Set<String> keySetDeep() {
392
        Set<String>keys = new TreeSet<>();
393
        UnmodifiableBasicSet<String> localkeys = this.getMyKeySet();
394
        if( localkeys!=null && !localkeys.isEmpty() ) {
395
            for (String key : localkeys) {
396
                keys.add(key);
397
            }
398
        }
399
        for (StoresRepository theRepository : this.subrepositories) {
400
            if( theRepository!=null ) {
401
                UnmodifiableBasicSet<String> subrepokeys = theRepository.keySet();
402
                if( subrepokeys!=null && !subrepokeys.isEmpty() ) {
403
                    for (String key : subrepokeys) {
404
                        keys.add(key);
405
                    }
406
                }
407
                keys.add(theRepository.getID());
408
            }
409
        }
410
        return keys;
411
    }
412
    
413
    public Set<String> keySetShallow() {
414
        Set<String>keys = new TreeSet<>();
415
        UnmodifiableBasicSet<String> localkeys = this.getMyKeySet();
416
        if( localkeys!=null && !localkeys.isEmpty() ) {
417
            for (String key : localkeys) {
418
                keys.add(key);
419
            }
420
        }
421
        for (StoresRepository theRepository : this.subrepositories) {
422
            if( theRepository!=null ) {
423
                keys.add(theRepository.getID());
424
            }
425
        }
426
        return keys;
427
    }
428
}