1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.limmen.crs.model;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.limmen.crs.exception.ConfigurationException;
22 import org.limmen.crs.exception.SectionNotFoundException;
23 import org.limmen.crs.exception.SettingNotFoundException;
24
25 /***
26 * The Configuration class is a container for sections that contains all
27 * settings.
28 *
29 * @author Ivo Limmen
30 * @see Section
31 * @see Setting
32 */
33 public class Configuration implements java.io.Serializable {
34
35 private static final long serialVersionUID = -7002796947675718127L;
36
37 private Configuration baseConfiguration;
38
39 private List<Documentation> documentations = new ArrayList<Documentation>();
40
41 private final String id;
42
43 private List<Section> sections = new ArrayList<Section>();
44
45 /***
46 * Create a new configuration with an ID.
47 *
48 * @param id
49 * a unique identifier for this configuration
50 */
51 public Configuration(String id) {
52
53 this(id, null);
54 }
55
56 /***
57 * Full constructor for the Configuration object, creates a Configuration
58 * with a unique identifier, a base configuration (may be NULL) and a list
59 * of documentation items (may also be NULL).
60 *
61 * @param id unique identifier for this configuration object
62 * @param baseConfiguration configuration this configuration is based on
63 * @param documentation a list of {@link Documentation} objects that provide
64 * information on this configuration
65 */
66 public Configuration(String id, Configuration baseConfiguration) {
67
68 super();
69
70 this.id = id;
71 setBaseConfiguration(baseConfiguration);
72 }
73
74 public void addDocumentation(Documentation documentation) {
75
76 getDocumentation().add(documentation);
77 }
78
79 public void addDocumentation(String aSectionId, Documentation documentation) {
80
81 createSection(aSectionId);
82
83 Section section = getUncheckedSection(aSectionId);
84
85 section.getDocumentation().add(documentation);
86 }
87
88 public void addDocumentation(String aSectionId, String aSettingId, Documentation documentation) {
89
90 createSection(aSectionId);
91
92 Section section = getUncheckedSection(aSectionId);
93
94 section.getDocumentation().add(documentation);
95 }
96
97 public void addSetting(String aSectionId, Setting newSetting) {
98
99 createSection(aSectionId);
100
101 Section section = getUncheckedSection(aSectionId);
102
103 if (getBaseConfiguration() != null) {
104
105 if (!getBaseConfiguration().settingExists(aSectionId, newSetting.getId())) {
106
107 throw new IllegalArgumentException(
108 String.format(
109 "Setting '%0$s' does not exist in the base configuration",
110 newSetting.getId()));
111 }
112 }
113
114 section.addSetting(newSetting);
115 }
116
117 private void createSection(String newSectionId) {
118
119 if (!sectionExists(newSectionId)) {
120
121 if (getBaseConfiguration() != null) {
122
123 if (!getBaseConfiguration().sectionExists(newSectionId)) {
124
125 throw new IllegalArgumentException(
126 String.format(
127 "Section '%0$s' does not exist in the base configuration",
128 newSectionId));
129 }
130 }
131
132 getSections().add(new Section(newSectionId));
133 }
134 }
135
136 @Override
137 public boolean equals(Object obj) {
138
139 if (obj.getClass().equals(getClass())) {
140
141 Configuration other = (Configuration) obj;
142
143 if (getId().equals(other.getId())) {
144
145 if (getSections().equals(other.getSections())) {
146
147 return true;
148 }
149 }
150 }
151
152 return false;
153 }
154
155 public Configuration getBaseConfiguration() {
156
157 return baseConfiguration;
158 }
159
160 public List<Documentation> getDocumentation() {
161
162 return documentations;
163 }
164
165 public List<Documentation> getDocumentation(String aSectionId) throws SectionNotFoundException {
166
167 return getSection(aSectionId).getDocumentation();
168 }
169
170 public List<Documentation> getDocumentation(String aSectionId, String aSettingId) throws SectionNotFoundException, SettingNotFoundException {
171
172 return getSetting(aSectionId, aSettingId).getDocumentation();
173 }
174
175 public String getId() {
176
177 return this.id;
178 }
179
180 public Section getSection(String aSectionId) throws SectionNotFoundException {
181
182 for (Section section : getSections()) {
183
184 if (section.getId().equals(aSectionId)) {
185
186 return section;
187 }
188 }
189
190 throw new SectionNotFoundException(
191 String.format("Section '%0$s' not found", aSectionId));
192 }
193
194 public List<Section> getSections() {
195
196 return this.sections;
197 }
198
199 /***
200 * Retrieve a setting from this configuration object, when it is not found the configuration object will try to
201 * retrieve the setting from it's base configuration if it available.
202 *
203 * @param aSection
204 * the section to retrieve the setting from
205 * @param aSetting
206 * the setting to locate
207 * @return a {@link Setting} object
208 * @throws ConfigurationException
209 * when the section or setting is not found
210 */
211 public Setting getSetting(String aSection, String aSetting) throws SectionNotFoundException,
212 SettingNotFoundException {
213
214 try {
215
216 return getSection(aSection).getSetting(aSetting);
217 }
218 catch (ConfigurationException ce) {
219
220 if (getBaseConfiguration() != null) {
221
222 return getBaseConfiguration().getSection(aSection).getSetting(aSetting);
223 }
224
225
226 if (ce instanceof SettingNotFoundException) {
227
228 throw (SettingNotFoundException) ce;
229 }
230 else {
231
232 throw (SectionNotFoundException) ce;
233 }
234 }
235 }
236
237 private Section getUncheckedSection(String aSectionId) {
238
239 for (Section section : getSections()) {
240
241 if (section.getId().equals(aSectionId)) {
242
243 return section;
244 }
245 }
246
247 return null;
248 }
249
250 @Override
251 public int hashCode() {
252
253 final int prime = 31;
254 int result = 1;
255 result = prime * result + ((id == null) ? 0 : id.hashCode());
256 result = prime * result + ((sections == null) ? 0 : sections.hashCode());
257 return result;
258 }
259
260 /***
261 * Returns <code>true</code> when the specified section id exists.
262 */
263 private boolean sectionExists(String sectionId) {
264
265 for (Section section : getSections()) {
266
267 if (section.getId().equals(sectionId)) {
268
269 return true;
270 }
271 }
272
273 return false;
274 }
275
276 public void setBaseConfiguration(Configuration baseConfiguration) {
277
278 this.baseConfiguration = baseConfiguration;
279 }
280
281 public void setDocumentation(List<Documentation> documentations) {
282
283 this.documentations = documentations;
284 }
285
286 public void setSections(List<Section> sections) {
287
288 this.sections = sections;
289 }
290
291 /***
292 * Returns <code>true</code> when the specified setting id exists in a
293 * specified section id.
294 */
295 boolean settingExists(String sectionId, String settingId) {
296
297 Section selectedSection = null;
298
299 for (Section section : getSections()) {
300
301 if (section.getId().equals(sectionId)) {
302
303 selectedSection = section;
304 }
305 }
306
307 if (selectedSection == null) {
308
309 return false;
310 }
311
312 for (Setting setting : selectedSection.getSettings()) {
313
314 if (setting.getId().equals(settingId)) {
315
316 return true;
317 }
318 }
319
320 return false;
321 }
322 }