1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.sf.webmancer.widget;
23
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import net.sf.webmancer.base.ApplicationInformation;
31 import net.sf.webmancer.base.Event;
32 import net.sf.webmancer.base.FinalizationEvent;
33 import net.sf.webmancer.base.IApplicationInformation;
34 import net.sf.webmancer.base.IEventQueue;
35 import net.sf.webmancer.base.InitializationEvent;
36 import net.sf.webmancer.base.RequestEvent;
37 import net.sf.webmancer.model.IModelBuilder;
38 import net.sf.webmancer.model.IModeler;
39 import net.sf.webmancer.model.ModelingException;
40 import net.sf.webmancer.util.ContractChecker;
41
42 import org.springframework.web.servlet.ModelAndView;
43 import org.springframework.web.servlet.mvc.Controller;
44
45
46
47
48 public class Page extends AbstractContainer implements Controller {
49
50
51
52 public static final String PAGE_ELEMENT = "Page";
53
54
55
56
57 public static final String AUTHOR_ATTRIBUTE = "author";
58
59
60
61
62 public static final String APPLICATION_VERSION_ATTRIBUTE = "applicationVersion";
63
64
65
66
67 public static final String APPLICATION_TITLE_ATTRIBUTE = "applicationTitle";
68
69
70
71
72 public static final String PAGE_TITLE_ATTRIBUTE = "pageTitle";
73
74
75
76
77 private static final String DESCRIPTION_ATTRIBUTE = "description";
78
79
80
81
82 private static final String KEYWORDS_ATTRIBUTE = "keywords";
83
84
85
86
87 private String title;
88
89
90
91
92 private String keywords;
93
94
95
96
97 private String description;
98
99
100
101
102 private IApplicationInformation applicationInformation;
103
104
105
106
107 private IModeler modeler;
108
109
110
111
112 private IEventQueue eventQueue;
113
114
115
116
117
118 public Page() {
119 super();
120 this.title = "";
121 this.applicationInformation = new ApplicationInformation();
122 }
123
124
125
126
127 @Override
128 public void setParent(IWidget parent) {
129
130 throw new IllegalStateException("Cannot set parent widget of the " + this.getClass().getName() + " widget");
131 }
132
133
134
135
136
137
138 public String getDescription() {
139 return this.description;
140 }
141
142
143
144
145
146
147 public void setDescription(final String description) {
148 this.description = description;
149 }
150
151
152
153
154
155
156 public String getKeywords() {
157 return this.keywords;
158 }
159
160
161
162
163
164
165 public void setKeywords(final String keywords) {
166 this.keywords = keywords;
167 }
168
169
170
171
172
173 public String getTitle() {
174 return title;
175 }
176
177
178
179
180
181 public void setTitle(String title) {
182 ContractChecker.mustNotBeNull(title, "title");
183 this.title = title;
184 }
185
186
187
188
189
190
191 public IApplicationInformation getApplicationInformation() {
192 return this.applicationInformation;
193 }
194
195
196
197
198
199
200 public void setApplicationInformation(IApplicationInformation applicationInformation) {
201 ContractChecker.mustNotBeNull(applicationInformation, "applicationInformation");
202 this.applicationInformation = applicationInformation;
203 }
204
205
206
207
208
209
210 protected IModeler getModeler() {
211 return this.modeler;
212 }
213
214
215
216
217
218
219 public void setModeler(IModeler modeler) {
220 this.modeler = modeler;
221 }
222
223
224
225
226
227
228 protected IEventQueue getEventQueue() {
229 return this.eventQueue;
230 }
231
232
233
234
235
236
237 public void setEventQueue(IEventQueue eventQueue) {
238 this.eventQueue = eventQueue;
239 }
240
241
242
243
244 @Override
245 public void putEvent(Event event) {
246 ContractChecker.mustNotBeNull(event, "event");
247 this.eventQueue.putEvent(event);
248 }
249
250
251
252
253 @Override
254 public void build(final IModelBuilder builder) throws ModelingException {
255 Map<String, String> attributes = new HashMap<String, String>();
256 attributes.put(PAGE_TITLE_ATTRIBUTE, this.title);
257 attributes.put(APPLICATION_TITLE_ATTRIBUTE, this.applicationInformation.getTitle());
258 attributes.put(APPLICATION_VERSION_ATTRIBUTE, this.applicationInformation.getVersion());
259 attributes.put(AUTHOR_ATTRIBUTE, this.applicationInformation.getAuthor());
260 if (getKeywords() != null) {
261 attributes.put(KEYWORDS_ATTRIBUTE, getKeywords());
262 }
263 if (getDescription() != null) {
264 attributes.put(DESCRIPTION_ATTRIBUTE, getDescription());
265 }
266 builder.startElement(IModeler.WEBMANCER_NAMESPACE, PAGE_ELEMENT, attributes);
267 super.build(builder);
268 builder.endElement();
269 }
270
271
272
273
274 public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
275 processEventLoop(createInitializationEvent());
276 processEventLoop(createRequestEvent(request, response));
277 ModelAndView mv = processModeling();
278 processEventLoop(createFinalizationEvent());
279 return mv;
280 }
281
282
283
284
285 protected void processEventLoop(Event event) {
286 this.eventQueue.putEvent(event);
287 while (this.eventQueue.hasEvents()) {
288 Event next = this.eventQueue.nextEvent();
289 this.handleEvent(next);
290 }
291 }
292
293
294
295
296 protected ModelAndView processModeling() throws ModelingException {
297 return this.getModeler().createModel(this);
298 }
299
300
301
302
303 protected Event createInitializationEvent() {
304 return new InitializationEvent();
305 }
306
307
308
309
310
311
312 protected Event createRequestEvent(HttpServletRequest request, HttpServletResponse response) {
313 return new RequestEvent(null);
314 }
315
316
317
318
319 private Event createFinalizationEvent() {
320 return new FinalizationEvent();
321 }
322
323 }