387f193114da0f58c0d1181fb8d3c6ce3b75a7fd
[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 import javax.transaction.UserTransaction;
20
21 import org.apache.wicket.IPageMap;
22 import org.apache.wicket.PageParameters;
23 import org.apache.wicket.markup.html.WebPage;
24 import org.apache.wicket.model.IModel;
25 import org.apache.wicket.protocol.http.WebResponse;
26
27 /**
28  * Base page for wicket applications that allows customization of various bahaviors of the 
29  * page. This page has no associated markup. 
30  * 
31  * The utility methods {@link #disableCaching()}, {@link #expireImmediately()}, 
32  * and {@link #flushEntityManagerAfterRender(EntityManager)} are provide for quicly adding 
33  * behaviors. Alternatively, the method {@link #addBehavior(PageBehavior)} can be used. 
34  * 
35  * @author Erik Brakkee
36  *
37  */
38 public class WebApplicationBasePage extends WebPage {
39     
40     private CompositePageBehavior behaviors;
41     
42     protected WebApplicationBasePage() { 
43         super();
44         init();
45     }
46     
47     protected WebApplicationBasePage(final IModel<?> model)
48     {
49         super(model);
50         init();
51     }
52
53     protected WebApplicationBasePage(final IPageMap pageMap)
54     {
55         super(pageMap);
56         init();
57     }
58     
59     protected WebApplicationBasePage(final IPageMap pageMap, final IModel<?> model)
60     {
61         super(pageMap, model);
62         init();
63     }
64
65     protected WebApplicationBasePage(final PageParameters parameters)
66     {
67         super(parameters);
68         init();
69     }
70
71     protected WebApplicationBasePage(final IPageMap pageMap, final PageParameters parameters)
72     {
73         super(pageMap, parameters);
74         init();
75     }
76     
77     private void init() { 
78         behaviors = new CompositePageBehavior(); 
79     }
80     
81     /**
82      * Disables caching. This implies expiry of the page from the page map. 
83      */
84     protected void disableCaching() { 
85         addBehavior(new DisableCachingBehavior());
86     }
87     
88     /**
89      * Expires the page immediately. Refresh in the browser will lead to an expired page. 
90      */
91     protected void expireImmediately() { 
92         addBehavior(new ExpirePageImmediatelyBehavior());
93     }
94     
95     /**
96      * Flushes the entitymanager immedately after rendering to make sure that errors are
97      * caught early. 
98      * @param aEntityManager Contextual reference to an entity manager. 
99      */
100     protected void flushEntityManagerAfterRender(EntityManager aEntityManager) { 
101         addBehavior(new FlushEntityManagerBehavior(aEntityManager));
102     }
103     
104     /**
105      * Adds a specific behavior to the page. 
106      * @param aBehavior Behavior to add. 
107      */
108     public void addBehavior(PageBehavior aBehavior) { 
109         behaviors.add(aBehavior);
110     }
111     
112     @Override
113     protected void onBeforeRender() {
114         behaviors.onBeforeRender(this);
115         super.onBeforeRender();
116     }
117     
118     @Override
119     protected void setHeaders(WebResponse aResponse) {
120         super.setHeaders(aResponse);
121         behaviors.setHeaders(this, aResponse);
122     }
123     
124     @Override
125     protected void onAfterRender() {
126         super.onAfterRender();
127         behaviors.onAfterRender(this);
128     }
129 }