1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.melati.poem;
47
48 import org.melati.poem.generated.SettingBase;
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 public class Setting extends SettingBase {
83
84
85
86
87
88
89
90
91
92
93 public Setting() { }
94
95
96
97 private FieldAttributes<?> valueAttributes = null;
98 private Object raw = null;
99 private Object cooked = null;
100
101
102
103
104
105
106
107
108
109
110 public Setting(Integer typefactory, String name, String value,
111 String displayname, String description) {
112 setTypefactory_unsafe(typefactory);
113 setName_unsafe(name);
114 setValue_unsafe(value);
115 setDisplayname_unsafe(displayname);
116 setDescription_unsafe(description);
117 setUsereditable_unsafe(Boolean.TRUE);
118 setNullable_unsafe(Boolean.TRUE);
119 setSize_unsafe(new Integer(-1));
120 setWidth_unsafe(new Integer(20));
121 setHeight_unsafe(new Integer(1));
122 setPrecision_unsafe(new Integer(22));
123 setScale_unsafe(new Integer(2));
124 }
125
126
127
128
129 public static class SettingValidationException extends PoemException {
130 private static final long serialVersionUID = 1L;
131
132
133 public String name;
134
135
136 public SettingValidationException(String name, Exception problem) {
137 super(problem);
138 this.name = name;
139 }
140
141
142 public String getMessage() {
143 return "A problem arose updating the value of the `" + name +
144 "' setting:\n" + subException.getMessage();
145 }
146 }
147
148
149
150
151
152
153 public void setValue(String value) {
154 Object rawLocal;
155 try {
156 rawLocal = getType().rawOfString(value);
157 } catch (Exception e) {
158 throw new SettingValidationException(getName_unsafe(), e);
159 }
160
161 super.setValue(value);
162 this.raw = rawLocal;
163 cooked = null;
164 }
165
166
167
168
169
170 public void setRaw(Object raw) {
171 String string;
172 Object newRaw;
173 try {
174 string = getType().stringOfRaw(raw);
175 } catch (Exception e) {
176 throw new SettingValidationException(getName_unsafe(), e);
177 }
178 try {
179 newRaw = getType().rawOfString(string);
180 } catch (Exception e) {
181 throw new SettingValidationException(getName_unsafe(), e);
182 }
183 super.setValue(newRaw == null ? null : string);
184 this.raw = newRaw;
185 cooked = null;
186 }
187
188
189
190
191 public Object getRaw() {
192 if (raw == null)
193 try {
194 raw = getType().rawOfString(getValue());
195 } catch (Exception e) {
196 throw new SettingValidationException(getName_unsafe(), e);
197 }
198
199 return raw;
200 }
201
202
203
204
205 public Object getCooked() {
206 if (cooked == null)
207 cooked = getType().cookedOfRaw(getRaw());
208 return cooked;
209 }
210
211
212
213
214 public static class
215 SettingTypeMismatchException extends AppBugPoemException {
216 private static final long serialVersionUID = 1L;
217
218
219 public String name;
220
221 public PoemTypeFactory type;
222
223 public String reqType;
224
225
226 public SettingTypeMismatchException(String name, PoemTypeFactory type,
227 String reqType) {
228 this.name = name;
229 this.type = type;
230 this.reqType = reqType;
231 }
232
233
234 public String getMessage() {
235 return "The setting `" + name + "' has type `" + type + "' but " +
236 "the application asked for a value of type " + reqType;
237 }
238 }
239
240
241
242
243 public Integer getIntegerCooked() {
244 Object cookedLocal = getCooked();
245 if (cookedLocal == null && getNullable().booleanValue())
246 return null;
247 else if (cookedLocal instanceof Integer)
248 return (Integer)cookedLocal;
249 else
250 throw new SettingTypeMismatchException(getName_unsafe(),
251 getTypefactory(), "Integer");
252 }
253
254
255
256
257 public String getStringCooked() {
258 Object cookedLocal = getCooked();
259 if (cookedLocal == null && getNullable().booleanValue())
260 return null;
261 else if (cookedLocal instanceof String)
262 return (String)cookedLocal;
263 else
264 throw new SettingTypeMismatchException(getName_unsafe(),
265 getTypefactory(), "String");
266 }
267
268
269
270
271 public Boolean getBooleanCooked() {
272 Object cookedLocal = getCooked();
273 if (cookedLocal == null && getNullable().booleanValue())
274 return null;
275 else if (cookedLocal instanceof Boolean)
276 return (Boolean)cookedLocal;
277 else
278 throw new SettingTypeMismatchException(getName_unsafe(),
279 getTypefactory(), "Boolean");
280 }
281
282
283
284
285
286 @SuppressWarnings("rawtypes")
287 private FieldAttributes valueFieldAttributes() {
288 if (valueAttributes == null)
289 valueAttributes =
290 fieldAttributesRenamedAs(getSettingTable().getValueColumn());
291
292 return valueAttributes;
293 }
294
295
296
297
298
299
300 @SuppressWarnings({ "rawtypes", "unchecked" })
301 public Field getValueField() {
302 try {
303 return new Field<String>(getRaw(), valueFieldAttributes());
304 } catch (AccessPoemException accessException) {
305 return new Field<Exception>(accessException, valueFieldAttributes());
306 }
307 }
308
309
310
311
312
313
314
315
316 public void postEdit(boolean creating) {
317 super.postEdit(creating);
318 valueAttributes = null;
319 }
320
321 }