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