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 @ 44408
History | View | Annotate | Download (7.55 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.observer.Notification; |
10 |
import org.gvsig.tools.observer.ObservableHelper; |
11 |
import org.gvsig.tools.observer.Observer; |
12 |
import org.gvsig.tools.util.UnmodifiableBasicSet; |
13 |
import org.gvsig.tools.util.UnmodifiableBasicSetChained; |
14 |
import org.slf4j.Logger; |
15 |
import org.slf4j.LoggerFactory; |
16 |
|
17 |
/**
|
18 |
*
|
19 |
* @author jjdelcerro
|
20 |
*/
|
21 |
@SuppressWarnings("UseSpecificCatch") |
22 |
public abstract class AbstractStoresRepository implements StoresRepository { |
23 |
|
24 |
protected final Logger LOGGER = LoggerFactory.getLogger(AbstractStoresRepository.class); |
25 |
|
26 |
private final String name; |
27 |
private final String label; |
28 |
private final List<StoresRepository> subrepositories; |
29 |
private final ObservableHelper observableHelper; |
30 |
|
31 |
public AbstractStoresRepository(String name) { |
32 |
this(name, null); |
33 |
} |
34 |
|
35 |
public AbstractStoresRepository(String name, String label) { |
36 |
this.name = name;
|
37 |
this.label = label;
|
38 |
this.subrepositories = new ArrayList<>(); |
39 |
this.observableHelper = new ObservableHelper(); |
40 |
} |
41 |
|
42 |
protected abstract DataStoreParameters getMyParameters(String name); |
43 |
|
44 |
protected abstract boolean isEmptyMyRepository(); |
45 |
|
46 |
protected abstract int getMySize(); |
47 |
|
48 |
protected abstract UnmodifiableBasicSet<String> getMyKeySet(); |
49 |
|
50 |
@Override
|
51 |
public void addObserver(Observer o) { |
52 |
this.observableHelper.addObserver(o);
|
53 |
} |
54 |
|
55 |
@Override
|
56 |
public void deleteObserver(Observer o) { |
57 |
this.observableHelper.deleteObserver(o);
|
58 |
} |
59 |
|
60 |
@Override
|
61 |
public void deleteObservers() { |
62 |
this.observableHelper.deleteObservers();
|
63 |
} |
64 |
|
65 |
protected Notification notifyObservers(String notificationType, Object value) { |
66 |
return this.observableHelper.notifyObservers(this, notificationType, value); |
67 |
} |
68 |
|
69 |
protected Notification notifyObservers(String notificationType, Object value1, Object value2) { |
70 |
return this.observableHelper.notifyObservers(this, notificationType, value1, value2); |
71 |
} |
72 |
|
73 |
@Override
|
74 |
public void add(String name, DataStoreParameters parameters) { |
75 |
throw new UnsupportedOperationException(); |
76 |
} |
77 |
|
78 |
@Override
|
79 |
public void remove(String name) { |
80 |
throw new UnsupportedOperationException(); |
81 |
} |
82 |
|
83 |
@Override
|
84 |
public boolean contains(DataStoreParameters parameters) { |
85 |
for (DataStoreParameters currentParameters : this) { |
86 |
if( parameters.equals(currentParameters) ) {
|
87 |
return true; |
88 |
} |
89 |
} |
90 |
return false; |
91 |
} |
92 |
|
93 |
@Override
|
94 |
public String getID() { |
95 |
return this.name; |
96 |
} |
97 |
|
98 |
@Override
|
99 |
public String getLabel() { |
100 |
if( StringUtils.isBlank(this.label) ) { |
101 |
return this.getID(); |
102 |
} |
103 |
return this.label; |
104 |
} |
105 |
|
106 |
@Override
|
107 |
public Collection<StoresRepository> getSubrepositories() { |
108 |
return this.subrepositories; |
109 |
} |
110 |
|
111 |
@Override
|
112 |
public boolean addRepository(StoresRepository repository) { |
113 |
if( this.notifyObservers(NOTIFICATION_ADDREPOSITORY, repository).isCanceled() ) { |
114 |
return false; |
115 |
} |
116 |
this.removeRepository(repository.getID());
|
117 |
this.subrepositories.add(repository);
|
118 |
return true; |
119 |
} |
120 |
|
121 |
@Override
|
122 |
public boolean removeRepository(String name) { |
123 |
if( this.notifyObservers(NOTIFICATION_REMOVEREPOSITORY, name).isCanceled() ) { |
124 |
return false; |
125 |
} |
126 |
for (int i = 0; i < subrepositories.size(); i++) { |
127 |
StoresRepository repo = subrepositories.get(i); |
128 |
if( StringUtils.equalsIgnoreCase(name, repo.getID()) ) {
|
129 |
this.subrepositories.remove(i);
|
130 |
return true; |
131 |
} |
132 |
} |
133 |
return false; |
134 |
} |
135 |
|
136 |
@Override
|
137 |
public DataStoreParameters get(String name) { |
138 |
DataStoreParameters parameters = this.getMyParameters(name);
|
139 |
if( parameters!=null ) { |
140 |
return parameters;
|
141 |
} |
142 |
for ( StoresRepository theRepository : this.subrepositories) { |
143 |
parameters = theRepository.get(name); |
144 |
if( parameters!=null ) { |
145 |
return parameters;
|
146 |
} |
147 |
} |
148 |
return null; |
149 |
} |
150 |
|
151 |
@Override
|
152 |
public DataStore getStore(String name) { |
153 |
DataStoreParameters parameters = this.getMyParameters(name);
|
154 |
if( parameters!=null ) { |
155 |
try {
|
156 |
DataStore store = DALLocator.getDataManager().openStore( |
157 |
parameters.getDataStoreName(), |
158 |
parameters |
159 |
); |
160 |
return store;
|
161 |
} catch (Exception ex) { |
162 |
throw new RuntimeException("Can't open store '"+name+"'.", ex); |
163 |
} |
164 |
} |
165 |
for ( StoresRepository theRepository : this.subrepositories) { |
166 |
DataStore store = theRepository.getStore(name); |
167 |
if( store!=null ) { |
168 |
return store;
|
169 |
} |
170 |
} |
171 |
return null; |
172 |
} |
173 |
|
174 |
@Override
|
175 |
public boolean containsKey(String key) { |
176 |
DataStoreParameters parameters = this.getMyParameters(name);
|
177 |
if( parameters!=null ) { |
178 |
return true; |
179 |
} |
180 |
for ( StoresRepository theRepository : this.subrepositories) { |
181 |
if( theRepository.containsKey(key) ) {
|
182 |
return true; |
183 |
} |
184 |
} |
185 |
return false; |
186 |
} |
187 |
|
188 |
@Override
|
189 |
public UnmodifiableBasicSet<String> keySet() { |
190 |
List<UnmodifiableBasicSet<String>> sets = new ArrayList<>(); |
191 |
UnmodifiableBasicSet<String> keyset = this.getMyKeySet(); |
192 |
if( keyset!=null && !keyset.isEmpty() ) { |
193 |
sets.add(keyset); |
194 |
} |
195 |
for (StoresRepository theRepository : this.subrepositories) { |
196 |
if( theRepository!=null ) { |
197 |
keyset = theRepository.keySet(); |
198 |
if( keyset!=null && !keyset.isEmpty() ) { |
199 |
sets.add(keyset); |
200 |
} |
201 |
} |
202 |
} |
203 |
if( sets.isEmpty() ) {
|
204 |
return UnmodifiableBasicSet.EMPTY_UNMODIFIABLEBASICSET;
|
205 |
} |
206 |
if( sets.size()==1 ) { |
207 |
return sets.get(0); |
208 |
} |
209 |
return new UnmodifiableBasicSetChained( |
210 |
sets.toArray( |
211 |
new UnmodifiableBasicSet[sets.size()]
|
212 |
) |
213 |
); |
214 |
} |
215 |
|
216 |
@Override
|
217 |
public Map<String, DataStoreParameters> toMap() { |
218 |
throw new UnsupportedOperationException(); |
219 |
} |
220 |
|
221 |
@Override
|
222 |
public boolean isEmpty() { |
223 |
if( !this.isEmptyMyRepository() ) { |
224 |
return false; |
225 |
} |
226 |
for ( StoresRepository theRepository : this.subrepositories) { |
227 |
if( !theRepository.isEmpty() ) {
|
228 |
return false; |
229 |
} |
230 |
} |
231 |
return true; |
232 |
} |
233 |
|
234 |
@Override
|
235 |
public int size() { |
236 |
int sz = 0; |
237 |
if( !this.isEmptyMyRepository() ) { |
238 |
sz = this.getMySize();
|
239 |
} |
240 |
for ( StoresRepository theRepository : this.subrepositories) { |
241 |
sz += theRepository.size(); |
242 |
} |
243 |
return sz;
|
244 |
} |
245 |
|
246 |
@Override
|
247 |
public Iterator<DataStoreParameters> iterator() { |
248 |
final Iterator<String> it = this.keySet().iterator(); |
249 |
return new Iterator<DataStoreParameters>() { |
250 |
@Override
|
251 |
public boolean hasNext() { |
252 |
return it.hasNext();
|
253 |
} |
254 |
|
255 |
@Override
|
256 |
public DataStoreParameters next() {
|
257 |
String name = it.next();
|
258 |
return get(name);
|
259 |
} |
260 |
}; |
261 |
} |
262 |
|
263 |
|
264 |
} |