97e41a04c066cc6bdf59d6cbd134781e3aa95690
[utils] / wicket / components / src / main / java / org / wamblee / wicket / page / 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.page;
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
29  * bahaviors of the page. This page has no associated markup.
30  * 
31  * The utility methods {@link #disableCaching()}, {@link #expireImmediately()},
32  * and {@link #flushEntityManagerAfterRender(EntityManager)} are provide for
33  * quicly adding behaviors. Alternatively, the method
34  * {@link #addBehavior(PageBehavior)} can be used.
35  * 
36  * @author Erik Brakkee
37  * 
38  */
39 public class WebApplicationBasePage extends WebPage {
40
41     private CompositePageBehavior behaviors;
42
43     protected WebApplicationBasePage() {
44         super();
45         init();
46     }
47
48     protected WebApplicationBasePage(final IModel<?> model) {
49         super(model);
50         init();
51     }
52
53     protected WebApplicationBasePage(final IPageMap pageMap) {
54         super(pageMap);
55         init();
56     }
57
58     protected WebApplicationBasePage(final IPageMap pageMap,
59         final IModel<?> model) {
60         super(pageMap, model);
61         init();
62     }
63
64     protected WebApplicationBasePage(final PageParameters parameters) {
65         super(parameters);
66         init();
67     }
68
69     protected WebApplicationBasePage(final IPageMap pageMap,
70         final PageParameters parameters) {
71         super(pageMap, parameters);
72         init();
73     }
74
75     private void init() {
76         behaviors = new CompositePageBehavior();
77     }
78
79     /**
80      * Disables caching. This implies expiry of the page from the page map.
81      */
82     protected void disableCaching() {
83         addBehavior(new DisableCachingBehavior());
84     }
85
86     /**
87      * Expires the page immediately. Refresh in the browser will lead to an
88      * 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
96      * errors are caught early.
97      * 
98      * @param aEntityManager
99      *            Contextual reference to an entity manager.
100      */
101     protected void flushEntityManagerAfterRender(EntityManager aEntityManager) {
102         addBehavior(new FlushEntityManagerBehavior(aEntityManager));
103     }
104
105     /**
106      * Adds a specific behavior to the page.
107      * 
108      * @param aBehavior
109      *            Behavior to add.
110      */
111     public void addBehavior(PageBehavior aBehavior) {
112         behaviors.add(aBehavior);
113     }
114
115     @Override
116     protected void onBeforeRender() {
117         behaviors.onBeforeRender(this);
118         super.onBeforeRender();
119     }
120
121     @Override
122     protected void setHeaders(WebResponse aResponse) {
123         super.setHeaders(aResponse);
124         behaviors.setHeaders(this, aResponse);
125     }
126
127     @Override
128     protected void onAfterRender() {
129         super.onAfterRender();
130         behaviors.onAfterRender(this);
131     }
132     
133     @Override
134     protected void onDetach() {
135         super.onDetach();
136         behaviors.onDetach(this);
137     }
138 }