+/*
+ * Copyright 2005-2010 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.wicket.page;
+
+import javax.persistence.EntityManager;
+import javax.transaction.UserTransaction;
+
+import org.apache.wicket.IPageMap;
+import org.apache.wicket.PageParameters;
+import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.model.IModel;
+import org.apache.wicket.protocol.http.WebResponse;
+
+/**
+ * Base page for wicket applications that allows customization of various
+ * bahaviors of the page. This page has no associated markup.
+ *
+ * The utility methods {@link #disableCaching()}, {@link #expireImmediately()},
+ * and {@link #flushEntityManagerAfterRender(EntityManager)} are provide for
+ * quicly adding behaviors. Alternatively, the method
+ * {@link #addBehavior(PageBehavior)} can be used.
+ *
+ * @author Erik Brakkee
+ *
+ */
+public class WebApplicationBasePage extends WebPage {
+
+ private CompositePageBehavior behaviors;
+
+ protected WebApplicationBasePage() {
+ super();
+ init();
+ }
+
+ protected WebApplicationBasePage(final IModel<?> model) {
+ super(model);
+ init();
+ }
+
+ protected WebApplicationBasePage(final IPageMap pageMap) {
+ super(pageMap);
+ init();
+ }
+
+ protected WebApplicationBasePage(final IPageMap pageMap,
+ final IModel<?> model) {
+ super(pageMap, model);
+ init();
+ }
+
+ protected WebApplicationBasePage(final PageParameters parameters) {
+ super(parameters);
+ init();
+ }
+
+ protected WebApplicationBasePage(final IPageMap pageMap,
+ final PageParameters parameters) {
+ super(pageMap, parameters);
+ init();
+ }
+
+ private void init() {
+ behaviors = new CompositePageBehavior();
+ }
+
+ /**
+ * Disables caching. This implies expiry of the page from the page map.
+ */
+ protected void disableCaching() {
+ addBehavior(new DisableCachingBehavior());
+ }
+
+ /**
+ * Expires the page immediately. Refresh in the browser will lead to an
+ * expired page.
+ */
+ protected void expireImmediately() {
+ addBehavior(new ExpirePageImmediatelyBehavior());
+ }
+
+ /**
+ * Flushes the entitymanager immedately after rendering to make sure that
+ * errors are caught early.
+ *
+ * @param aEntityManager
+ * Contextual reference to an entity manager.
+ */
+ protected void flushEntityManagerAfterRender(EntityManager aEntityManager) {
+ addBehavior(new FlushEntityManagerBehavior(aEntityManager));
+ }
+
+ /**
+ * Adds a specific behavior to the page.
+ *
+ * @param aBehavior
+ * Behavior to add.
+ */
+ public void addBehavior(PageBehavior aBehavior) {
+ behaviors.add(aBehavior);
+ }
+
+ @Override
+ protected void onBeforeRender() {
+ behaviors.onBeforeRender(this);
+ super.onBeforeRender();
+ }
+
+ @Override
+ protected void setHeaders(WebResponse aResponse) {
+ super.setHeaders(aResponse);
+ behaviors.setHeaders(this, aResponse);
+ }
+
+ @Override
+ protected void onAfterRender() {
+ super.onAfterRender();
+ behaviors.onAfterRender(this);
+ }
+}