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

History | View | Annotate | Download (6.09 KB)

1
package org.gvsig.fmap.dal;
2

    
3
import java.util.ArrayList;
4
import java.util.Collection;
5
import java.util.Iterator;
6
import java.util.List;
7
import java.util.Map;
8
import org.apache.commons.lang3.StringUtils;
9
import org.gvsig.tools.util.UnmodifiableBasicSet;
10
import org.gvsig.tools.util.UnmodifiableBasicSetChained;
11
import org.slf4j.Logger;
12
import org.slf4j.LoggerFactory;
13

    
14
/**
15
 *
16
 * @author jjdelcerro
17
 */
18
@SuppressWarnings("UseSpecificCatch")
19
public abstract class AbstractStoresRepository  implements StoresRepository {
20
    
21
    protected final Logger LOGGER = LoggerFactory.getLogger(AbstractStoresRepository.class);
22
    
23
    private final String name;
24
    private final String label;
25
    private final List<StoresRepository> subrepositories;
26

    
27
    public AbstractStoresRepository(String name) {
28
        this(name, null);
29
    }
30
    
31
    public AbstractStoresRepository(String name, String label) {
32
        this.name = name;
33
        this.label = label;
34
        this.subrepositories = new ArrayList<>();
35
    }
36
    
37
    protected abstract DataStoreParameters getMyParameters(String name);
38

    
39
    protected abstract boolean isEmptyMyRepository();
40
    
41
    protected abstract int getMySize();
42

    
43
    protected abstract UnmodifiableBasicSet<String> getMyKeySet();
44

    
45
    @Override
46
    public void add(String name, DataStoreParameters parameters) {
47
        throw new UnsupportedOperationException();
48
    }
49

    
50
    @Override
51
    public void remove(String name) {
52
        throw new UnsupportedOperationException();
53
    }
54
    
55
    @Override
56
    public boolean contains(DataStoreParameters parameters) {
57
        for (DataStoreParameters currentParameters : this) {
58
            if( parameters.equals(currentParameters) ) {
59
                return true;
60
            }
61
        }
62
        return false;
63
    }
64

    
65
    @Override
66
    public String getID() {
67
        return this.name;
68
    }
69

    
70
    @Override
71
    public String getLabel() {
72
        if( StringUtils.isBlank(this.label) ) {
73
            return this.getID();
74
        }
75
        return this.label;
76
    }
77
    
78
    @Override
79
    public Collection<StoresRepository> getSubrepositories() {
80
        return this.subrepositories;
81
    }
82

    
83
    @Override
84
    public boolean addRepository(StoresRepository repository) {
85
        this.removeRepository(repository.getID());
86
        this.subrepositories.add(repository);
87
        return true;
88
    }
89

    
90
    @Override
91
    public boolean removeRepository(String name) {
92
        for (int i = 0; i < subrepositories.size(); i++) {
93
            StoresRepository repo = subrepositories.get(i);
94
            if( StringUtils.equalsIgnoreCase(name, repo.getID()) ) {
95
                this.subrepositories.remove(i);
96
                return true;
97
            }
98
        }
99
        return false;
100
    }
101

    
102
    @Override
103
    public DataStoreParameters get(String name) {
104
        DataStoreParameters parameters = this.getMyParameters(name);
105
        if( parameters!=null ) {
106
            return parameters;
107
        }
108
        for ( StoresRepository theRepository : this.subrepositories) {
109
            parameters = theRepository.get(name);
110
            if( parameters!=null ) {
111
                return parameters;
112
            }
113
        }
114
        return null;
115
    }
116

    
117
    @Override
118
    public DataStore getStore(String name) {
119
        DataStoreParameters parameters = this.getMyParameters(name);
120
        if( parameters!=null ) {
121
            try {
122
                DataStore store = DALLocator.getDataManager().openStore(
123
                        parameters.getDataStoreName(), 
124
                        parameters
125
                );
126
                return store;
127
            } catch (Exception ex) {
128
                throw new RuntimeException("Can't open store '"+name+"'.", ex);
129
            }
130
        }
131
        for ( StoresRepository theRepository : this.subrepositories) {
132
            DataStore store = theRepository.getStore(name);
133
            if( store!=null ) {
134
                return store;
135
            }
136
        }
137
        return null;
138
    }
139

    
140
    @Override
141
    public boolean containsKey(String key) {
142
        DataStoreParameters parameters = this.getMyParameters(name);
143
        if( parameters!=null ) {
144
            return true;
145
        }
146
        for ( StoresRepository theRepository : this.subrepositories) {
147
            if( theRepository.containsKey(key) ) {
148
                return true;
149
            }
150
        }
151
        return false;
152
    }
153

    
154
    @Override
155
    public UnmodifiableBasicSet<String> keySet() {
156
        List<UnmodifiableBasicSet<String>> sets = new ArrayList<>();
157
        sets.add(this.getMyKeySet());
158
        for (StoresRepository theRepository : this.subrepositories) {
159
            sets.add(theRepository.keySet());
160
        }
161
        if( sets.isEmpty() ) {
162
            return UnmodifiableBasicSet.EMPTY_UNMODIFIABLEBASICSET;
163
        }
164
        if( sets.size()==1 ) {
165
            return sets.get(0);
166
        }
167
        return new UnmodifiableBasicSetChained(
168
            sets.toArray(
169
                new UnmodifiableBasicSet[sets.size()]
170
            )
171
        );
172
    }
173

    
174
    @Override
175
    public Map<String, DataStoreParameters> toMap() {
176
        throw new UnsupportedOperationException();
177
    }
178

    
179
    @Override
180
    public boolean isEmpty() {
181
        if( !this.isEmptyMyRepository() ) {
182
            return false;
183
        }
184
        for ( StoresRepository theRepository : this.subrepositories) {
185
            if( !theRepository.isEmpty() ) {
186
                return false;
187
            }
188
        }
189
        return true;
190
    }
191

    
192
    @Override
193
    public int size() {
194
        int sz = 0;
195
        if( !this.isEmptyMyRepository() ) {
196
            sz = this.getMySize();
197
        }
198
        for ( StoresRepository theRepository : this.subrepositories) {
199
            sz += theRepository.size();
200
        }
201
        return sz;
202
    }
203

    
204
    @Override
205
    public Iterator<DataStoreParameters> iterator() {
206
        final Iterator<String> it = this.keySet().iterator();
207
        return new Iterator<DataStoreParameters>() {
208
            @Override
209
            public boolean hasNext() {
210
                return it.hasNext();
211
            }
212

    
213
            @Override
214
            public DataStoreParameters next() {
215
                String name = it.next();
216
                return get(name);
217
            }
218
        };
219
    }
220
    
221
    
222
}