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 @ 44779
History | View | Annotate | Download (7.84 KB)
1 | 44304 | jjdelcerro | 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 | 44377 | jjdelcerro | import org.gvsig.tools.observer.Notification; |
10 | import org.gvsig.tools.observer.ObservableHelper; |
||
11 | import org.gvsig.tools.observer.Observer; |
||
12 | 44304 | jjdelcerro | import org.gvsig.tools.util.UnmodifiableBasicSet; |
13 | import org.gvsig.tools.util.UnmodifiableBasicSetChained; |
||
14 | 44307 | jjdelcerro | import org.slf4j.Logger; |
15 | import org.slf4j.LoggerFactory; |
||
16 | 44304 | jjdelcerro | |
17 | /**
|
||
18 | *
|
||
19 | * @author jjdelcerro
|
||
20 | */
|
||
21 | @SuppressWarnings("UseSpecificCatch") |
||
22 | public abstract class AbstractStoresRepository implements StoresRepository { |
||
23 | |||
24 | 44307 | jjdelcerro | protected final Logger LOGGER = LoggerFactory.getLogger(AbstractStoresRepository.class); |
25 | |||
26 | 44304 | jjdelcerro | private final String name; |
27 | private final String label; |
||
28 | private final List<StoresRepository> subrepositories; |
||
29 | 44377 | jjdelcerro | private final ObservableHelper observableHelper; |
30 | 44304 | jjdelcerro | |
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 | 44377 | jjdelcerro | this.observableHelper = new ObservableHelper(); |
40 | 44304 | jjdelcerro | } |
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 | 44377 | jjdelcerro | 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 | 44304 | jjdelcerro | 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 | 44346 | jjdelcerro | |
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 | 44304 | jjdelcerro | |
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 | 44439 | jjdelcerro | public StoresRepository getSubrepository(String Id) { |
113 | for (StoresRepository repo : this.subrepositories) { |
||
114 | if( repo!=null && StringUtils.equalsIgnoreCase(Id, repo.getID()) ) { |
||
115 | return repo;
|
||
116 | } |
||
117 | } |
||
118 | return null; |
||
119 | } |
||
120 | |||
121 | @Override
|
||
122 | 44304 | jjdelcerro | public boolean addRepository(StoresRepository repository) { |
123 | 44377 | jjdelcerro | if( this.notifyObservers(NOTIFICATION_ADDREPOSITORY, repository).isCanceled() ) { |
124 | return false; |
||
125 | } |
||
126 | 44304 | jjdelcerro | this.removeRepository(repository.getID());
|
127 | this.subrepositories.add(repository);
|
||
128 | return true; |
||
129 | } |
||
130 | |||
131 | @Override
|
||
132 | public boolean removeRepository(String name) { |
||
133 | 44377 | jjdelcerro | if( this.notifyObservers(NOTIFICATION_REMOVEREPOSITORY, name).isCanceled() ) { |
134 | return false; |
||
135 | } |
||
136 | 44304 | jjdelcerro | for (int i = 0; i < subrepositories.size(); i++) { |
137 | StoresRepository repo = subrepositories.get(i); |
||
138 | if( StringUtils.equalsIgnoreCase(name, repo.getID()) ) {
|
||
139 | this.subrepositories.remove(i);
|
||
140 | return true; |
||
141 | } |
||
142 | } |
||
143 | return false; |
||
144 | } |
||
145 | |||
146 | @Override
|
||
147 | public DataStoreParameters get(String name) { |
||
148 | DataStoreParameters parameters = this.getMyParameters(name);
|
||
149 | if( parameters!=null ) { |
||
150 | return parameters;
|
||
151 | } |
||
152 | for ( StoresRepository theRepository : this.subrepositories) { |
||
153 | parameters = theRepository.get(name); |
||
154 | if( parameters!=null ) { |
||
155 | return parameters;
|
||
156 | } |
||
157 | } |
||
158 | return null; |
||
159 | } |
||
160 | |||
161 | @Override
|
||
162 | public DataStore getStore(String name) { |
||
163 | 44307 | jjdelcerro | DataStoreParameters parameters = this.getMyParameters(name);
|
164 | 44304 | jjdelcerro | if( parameters!=null ) { |
165 | try {
|
||
166 | DataStore store = DALLocator.getDataManager().openStore( |
||
167 | parameters.getDataStoreName(), |
||
168 | parameters |
||
169 | ); |
||
170 | return store;
|
||
171 | } catch (Exception ex) { |
||
172 | throw new RuntimeException("Can't open store '"+name+"'.", ex); |
||
173 | } |
||
174 | } |
||
175 | 44307 | jjdelcerro | for ( StoresRepository theRepository : this.subrepositories) { |
176 | DataStore store = theRepository.getStore(name); |
||
177 | if( store!=null ) { |
||
178 | return store;
|
||
179 | } |
||
180 | } |
||
181 | 44304 | jjdelcerro | return null; |
182 | } |
||
183 | |||
184 | @Override
|
||
185 | public boolean containsKey(String key) { |
||
186 | DataStoreParameters parameters = this.getMyParameters(name);
|
||
187 | if( parameters!=null ) { |
||
188 | return true; |
||
189 | } |
||
190 | for ( StoresRepository theRepository : this.subrepositories) { |
||
191 | if( theRepository.containsKey(key) ) {
|
||
192 | return true; |
||
193 | } |
||
194 | } |
||
195 | return false; |
||
196 | } |
||
197 | |||
198 | @Override
|
||
199 | public UnmodifiableBasicSet<String> keySet() { |
||
200 | List<UnmodifiableBasicSet<String>> sets = new ArrayList<>(); |
||
201 | 44408 | jjdelcerro | UnmodifiableBasicSet<String> keyset = this.getMyKeySet(); |
202 | if( keyset!=null && !keyset.isEmpty() ) { |
||
203 | sets.add(keyset); |
||
204 | } |
||
205 | 44304 | jjdelcerro | for (StoresRepository theRepository : this.subrepositories) { |
206 | 44408 | jjdelcerro | if( theRepository!=null ) { |
207 | keyset = theRepository.keySet(); |
||
208 | if( keyset!=null && !keyset.isEmpty() ) { |
||
209 | sets.add(keyset); |
||
210 | } |
||
211 | } |
||
212 | 44304 | jjdelcerro | } |
213 | if( sets.isEmpty() ) {
|
||
214 | return UnmodifiableBasicSet.EMPTY_UNMODIFIABLEBASICSET;
|
||
215 | } |
||
216 | if( sets.size()==1 ) { |
||
217 | return sets.get(0); |
||
218 | } |
||
219 | return new UnmodifiableBasicSetChained( |
||
220 | sets.toArray( |
||
221 | new UnmodifiableBasicSet[sets.size()]
|
||
222 | ) |
||
223 | ); |
||
224 | } |
||
225 | |||
226 | @Override
|
||
227 | public Map<String, DataStoreParameters> toMap() { |
||
228 | throw new UnsupportedOperationException(); |
||
229 | } |
||
230 | |||
231 | @Override
|
||
232 | public boolean isEmpty() { |
||
233 | if( !this.isEmptyMyRepository() ) { |
||
234 | return false; |
||
235 | } |
||
236 | for ( StoresRepository theRepository : this.subrepositories) { |
||
237 | if( !theRepository.isEmpty() ) {
|
||
238 | return false; |
||
239 | } |
||
240 | } |
||
241 | return true; |
||
242 | } |
||
243 | |||
244 | @Override
|
||
245 | public int size() { |
||
246 | int sz = 0; |
||
247 | if( !this.isEmptyMyRepository() ) { |
||
248 | sz = this.getMySize();
|
||
249 | } |
||
250 | for ( StoresRepository theRepository : this.subrepositories) { |
||
251 | sz += theRepository.size(); |
||
252 | } |
||
253 | return sz;
|
||
254 | } |
||
255 | |||
256 | @Override
|
||
257 | public Iterator<DataStoreParameters> iterator() { |
||
258 | final Iterator<String> it = this.keySet().iterator(); |
||
259 | return new Iterator<DataStoreParameters>() { |
||
260 | @Override
|
||
261 | public boolean hasNext() { |
||
262 | return it.hasNext();
|
||
263 | } |
||
264 | |||
265 | @Override
|
||
266 | public DataStoreParameters next() {
|
||
267 | String name = it.next();
|
||
268 | return get(name);
|
||
269 | } |
||
270 | }; |
||
271 | } |
||
272 | |||
273 | |||
274 | } |