(no commit message)
[utils] / wicket / components / src / main / java / org / wamblee / wicket / behavior / WebApplicationBasePage.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.wicket.behavior;
17
18 import javax.persistence.EntityManager;
19
20 import org.apache.wicket.IPageMap;
21 import org.apache.wicket.PageParameters;
22 import org.apache.wicket.markup.html.WebPage;
23 import org.apache.wicket.model.IModel;
24 import org.apache.wicket.protocol.http.WebResponse;
25
26 /**
27  * Base page for wicket applications that allows customization of various bahaviors of the 
28  * page. This page has no associated markup. 
29  * 
30  * The utility methods {@link #disableCaching()}, {@link #expireImmediately()}, 
31  * and {@link #flushEntityManagerAfterRender(EntityManager)} are provide for quicly adding 
32  * behaviors. Alternatively, the method {@link #addBehavior(PageBehavior)} can be used. 
33  * 
34  * @author Erik Brakkee
35  *
36  */
37 public class WebApplicationBasePage extends WebPage {
38     
39     private CompositePageBehavior behaviors;
40     
41     protected WebApplicationBasePage() { 
42         super();
43         init();
44     }
45     
46     protected WebApplicationBasePage(final IModel<?> model)
47     {
48         super(model);
49         init();
50     }
51
52     protected WebApplicationBasePage(final IPageMap pageMap)
53     {
54         super(pageMap);
55         init();
56     }
57     
58     protected WebApplicationBasePage(final IPageMap pageMap, final IModel<?> model)
59     {
60         super(pageMap, model);
61         init();
62     }
63
64     protected WebApplicationBasePage(final PageParameters parameters)
65     {
66         super(parameters);
67         init();
68     }
69
70     protected WebApplicationBasePage(final IPageMap pageMap, final PageParameters parameters)
71     {
72         super(pageMap, parameters);
73         init();
74     }
75     
76     private void init() { 
77         behaviors = new CompositePageBehavior(); 
78     }
79     
80     /**
81      * Disables caching. This implies expiry of the page from the page map. 
82      */
83     protected void disableCaching() { 
84         addBehavior(new DisableCachingBehavior());
85     }
86     
87     /**
88      * Expires the page immediately. Refresh in the browser will lead to an expired page. 
89      */
90     protected void expireImmediately() { 
91         addBehavior(new ExpirePageImmediatelyBehavior());
92     }
93     
94     /**
95      * Flushes the entitymanager immedately after rendering to make sure that errors are
96      * caught early. 
97      * @param aEntityManager Contextual reference to an entity manager. 
98      */
99     protected void flushEntityManagerAfterRender(EntityManager aEntityManager) { 
100         addBehavior(new FlushEntityManagerBehavior(aEntityManager));
101     }
102     
103     
104     public void addBehavior(PageBehavior aBehavior) { 
105         behaviors.add(aBehavior);
106     }
107     
108     @Override
109     protected void onBeforeRender() {
110         behaviors.onBeforeRender(this);
111         super.onBeforeRender();
112     }
113     
114     @Override
115     protected void setHeaders(WebResponse aResponse) {
116         super.setHeaders(aResponse);
117         behaviors.setHeaders(this, aResponse);
118     }
119     
120     @Override
121     protected void onAfterRender() {
122         super.onAfterRender();
123         behaviors.onAfterRender(this);
124     }
125 }