View Javadoc

1   /*
2    * File:    Page.java
3    * Created: 5.11.2006 16:10:24
4    *
5    * Copyright 2006 Michal Burda.
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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   * @author Michal Burda
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      * @param id
116      * @param title
117      */
118     public Page() {
119         super();
120         this.title = "";
121         this.applicationInformation = new ApplicationInformation();
122     }
123 
124     /**
125      * @see net.sf.webmancer.widget.AbstractWidget#setParent(net.sf.webmancer.widget.IWidget)
126      */
127     @Override
128     public void setParent(IWidget parent) {
129         // Page should always be a root widget (without any parent)
130         throw new IllegalStateException("Cannot set parent widget of the " + this.getClass().getName() + " widget");
131     }
132 
133     /**
134      * Returns the description.
135      *
136      * @return the description
137      */
138     public String getDescription() {
139         return this.description;
140     }
141 
142     /**
143      * Sets the description.
144      *
145      * @param description the description to set
146      */
147     public void setDescription(final String description) {
148         this.description = description;
149     }
150     
151     /**
152      * Returns the keywords.
153      *
154      * @return the keywords
155      */
156     public String getKeywords() {
157         return this.keywords;
158     }
159     
160     /**
161      * Sets the keywords.
162      *
163      * @param keywords the keywords to set
164      */
165     public void setKeywords(final String keywords) {
166         this.keywords = keywords;
167     }
168     
169     /**
170      * Gets the title.
171      * @return the title
172      */
173     public String getTitle() {
174         return title;
175     }
176 
177     /**
178      * Sets the title.
179      * @param title the title to set
180      */
181     public void setTitle(String title) {
182         ContractChecker.mustNotBeNull(title, "title");
183         this.title = title;
184     }
185     
186     /**
187      * Returns the applicationInformation.
188      *
189      * @return the applicationInformation
190      */
191     public IApplicationInformation getApplicationInformation() {
192         return this.applicationInformation;
193     }
194 
195     /**
196      * Sets the applicationInformation.
197      *
198      * @param applicationInformation the applicationInformation to set
199      */
200     public void setApplicationInformation(IApplicationInformation applicationInformation) {
201         ContractChecker.mustNotBeNull(applicationInformation, "applicationInformation");
202         this.applicationInformation = applicationInformation;
203     }
204     
205     /**
206      * Returns the modeler.
207      *
208      * @return the modeler
209      */
210     protected IModeler getModeler() {
211         return this.modeler;
212     }
213 
214     /**
215      * Sets the modeler.
216      *
217      * @param modeler the modeler to set
218      */
219     public void setModeler(IModeler modeler) {
220         this.modeler = modeler;
221     }
222 
223     /**
224      * Returns the eventQueue.
225      *
226      * @return the eventQueue
227      */
228     protected IEventQueue getEventQueue() {
229         return this.eventQueue;
230     }
231 
232     /**
233      * Sets the eventQueue.
234      *
235      * @param eventQueue the eventQueue to set
236      */
237     public void setEventQueue(IEventQueue eventQueue) {
238         this.eventQueue = eventQueue;
239     }
240 
241     /**
242      * @see net.sf.webmancer.widget.AbstractWidget#putEvent(net.sf.webmancer.base.Event)
243      */
244     @Override
245     public void putEvent(Event event) {
246         ContractChecker.mustNotBeNull(event, "event");
247         this.eventQueue.putEvent(event);
248     }
249 
250     /**
251      * @see net.sf.webmancer.widget.AbstractContainer#build(net.sf.webmancer.model.IModelBuilder)
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      * @see org.springframework.web.servlet.mvc.Controller#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
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      * @param event
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      * @return
295      */
296     protected ModelAndView processModeling() throws ModelingException {
297         return this.getModeler().createModel(this);
298     }
299 
300     /**
301      * @return
302      */
303     protected Event createInitializationEvent() {
304         return new InitializationEvent();
305     }
306 
307     /**
308      * @param request
309      * @param response
310      * @return
311      */
312     protected Event createRequestEvent(HttpServletRequest request, HttpServletResponse response) {
313         return new RequestEvent(null);
314     }
315 
316     /**
317      * @return
318      */
319     private Event createFinalizationEvent() {
320         return new FinalizationEvent();
321     }
322 
323 }