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 @ 44307
History | View | Annotate | Download (5.81 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 String getID() { |
57 |
return this.name; |
58 |
} |
59 |
|
60 |
@Override
|
61 |
public String getLabel() { |
62 |
if( StringUtils.isBlank(this.label) ) { |
63 |
return this.getID(); |
64 |
} |
65 |
return this.label; |
66 |
} |
67 |
|
68 |
@Override
|
69 |
public Collection<StoresRepository> getSubrepositories() { |
70 |
return this.subrepositories; |
71 |
} |
72 |
|
73 |
@Override
|
74 |
public boolean addRepository(StoresRepository repository) { |
75 |
this.removeRepository(repository.getID());
|
76 |
this.subrepositories.add(repository);
|
77 |
return true; |
78 |
} |
79 |
|
80 |
@Override
|
81 |
public boolean removeRepository(String name) { |
82 |
for (int i = 0; i < subrepositories.size(); i++) { |
83 |
StoresRepository repo = subrepositories.get(i); |
84 |
if( StringUtils.equalsIgnoreCase(name, repo.getID()) ) {
|
85 |
this.subrepositories.remove(i);
|
86 |
return true; |
87 |
} |
88 |
} |
89 |
return false; |
90 |
} |
91 |
|
92 |
@Override
|
93 |
public DataStoreParameters get(String name) { |
94 |
DataStoreParameters parameters = this.getMyParameters(name);
|
95 |
if( parameters!=null ) { |
96 |
return parameters;
|
97 |
} |
98 |
for ( StoresRepository theRepository : this.subrepositories) { |
99 |
parameters = theRepository.get(name); |
100 |
if( parameters!=null ) { |
101 |
return parameters;
|
102 |
} |
103 |
} |
104 |
return null; |
105 |
} |
106 |
|
107 |
@Override
|
108 |
public DataStore getStore(String name) { |
109 |
DataStoreParameters parameters = this.getMyParameters(name);
|
110 |
if( parameters!=null ) { |
111 |
try {
|
112 |
DataStore store = DALLocator.getDataManager().openStore( |
113 |
parameters.getDataStoreName(), |
114 |
parameters |
115 |
); |
116 |
return store;
|
117 |
} catch (Exception ex) { |
118 |
throw new RuntimeException("Can't open store '"+name+"'.", ex); |
119 |
} |
120 |
} |
121 |
for ( StoresRepository theRepository : this.subrepositories) { |
122 |
DataStore store = theRepository.getStore(name); |
123 |
if( store!=null ) { |
124 |
return store;
|
125 |
} |
126 |
} |
127 |
return null; |
128 |
} |
129 |
|
130 |
@Override
|
131 |
public boolean containsKey(String key) { |
132 |
DataStoreParameters parameters = this.getMyParameters(name);
|
133 |
if( parameters!=null ) { |
134 |
return true; |
135 |
} |
136 |
for ( StoresRepository theRepository : this.subrepositories) { |
137 |
if( theRepository.containsKey(key) ) {
|
138 |
return true; |
139 |
} |
140 |
} |
141 |
return false; |
142 |
} |
143 |
|
144 |
@Override
|
145 |
public UnmodifiableBasicSet<String> keySet() { |
146 |
List<UnmodifiableBasicSet<String>> sets = new ArrayList<>(); |
147 |
sets.add(this.getMyKeySet());
|
148 |
for (StoresRepository theRepository : this.subrepositories) { |
149 |
sets.add(theRepository.keySet()); |
150 |
} |
151 |
if( sets.isEmpty() ) {
|
152 |
return UnmodifiableBasicSet.EMPTY_UNMODIFIABLEBASICSET;
|
153 |
} |
154 |
if( sets.size()==1 ) { |
155 |
return sets.get(0); |
156 |
} |
157 |
return new UnmodifiableBasicSetChained( |
158 |
sets.toArray( |
159 |
new UnmodifiableBasicSet[sets.size()]
|
160 |
) |
161 |
); |
162 |
} |
163 |
|
164 |
@Override
|
165 |
public Map<String, DataStoreParameters> toMap() { |
166 |
throw new UnsupportedOperationException(); |
167 |
} |
168 |
|
169 |
@Override
|
170 |
public boolean isEmpty() { |
171 |
if( !this.isEmptyMyRepository() ) { |
172 |
return false; |
173 |
} |
174 |
for ( StoresRepository theRepository : this.subrepositories) { |
175 |
if( !theRepository.isEmpty() ) {
|
176 |
return false; |
177 |
} |
178 |
} |
179 |
return true; |
180 |
} |
181 |
|
182 |
@Override
|
183 |
public int size() { |
184 |
int sz = 0; |
185 |
if( !this.isEmptyMyRepository() ) { |
186 |
sz = this.getMySize();
|
187 |
} |
188 |
for ( StoresRepository theRepository : this.subrepositories) { |
189 |
sz += theRepository.size(); |
190 |
} |
191 |
return sz;
|
192 |
} |
193 |
|
194 |
@Override
|
195 |
public Iterator<DataStoreParameters> iterator() { |
196 |
final Iterator<String> it = this.keySet().iterator(); |
197 |
return new Iterator<DataStoreParameters>() { |
198 |
@Override
|
199 |
public boolean hasNext() { |
200 |
return it.hasNext();
|
201 |
} |
202 |
|
203 |
@Override
|
204 |
public DataStoreParameters next() {
|
205 |
String name = it.next();
|
206 |
return get(name);
|
207 |
} |
208 |
}; |
209 |
} |
210 |
|
211 |
|
212 |
} |