Java tutorial
/* * The DecidR Development Team licenses this file to you 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 de.decidr.ui.main; import com.vaadin.Application; import com.vaadin.service.ApplicationContext.TransactionListener; import com.vaadin.ui.Window; import de.decidr.model.acl.roles.Role; import de.decidr.model.acl.roles.UserRole; import de.decidr.model.annotations.Reviewed; import de.decidr.model.annotations.Reviewed.State; import de.decidr.ui.controller.UIDirector; import de.decidr.ui.controller.parameterhandler.ConfirmationParameterHandler; import de.decidr.ui.controller.parameterhandler.InvitationParameterHandler; import de.decidr.ui.controller.parameterhandler.LoginParameterHandler; import de.decidr.ui.controller.parameterhandler.TenantParameterHandler; import de.decidr.ui.view.uibuilder.UIBuilder; import de.decidr.ui.view.uibuilder.UnregisteredUserViewBuilder; /** * This is the main class of the stateful web front-end. It initializes the main * window and is instantiated once per session/user. * * A somewhat fishy mechanism that provides the "current" application instance * has been implemented (see * http://vaadin.com/wiki/-/wiki/Main/ThreadLocal%20Pattern). * * @author AT * @author Daniel Huss */ @Reviewed(reviewers = { "unknown" }, lastRevision = "0", currentReviewState = State.NeedsReview) public class DecidrUI extends Application implements TransactionListener { /** * This servlet wraps the Vaadin application. No web.xml is required since * we're using the <code>@WebServlet</code> annotation * * @author Daniel Huss */ // @WebServlet(urlPatterns = "/*", initParams = { @WebInitParam(name = "widgetset", value = "de.decidr.ui.view.MainWidgetset") }) // public static class Servlet extends AbstractApplicationServlet { // // private static final long serialVersionUID = 5535271267077037472L; // // @Override // protected Class<? extends Application> getApplicationClass() { // return DecidrUI.class; // } // // @Override // protected Application getNewApplication(HttpServletRequest request) // throws ServletException { // return new DecidrUI(); // } // } private static final long serialVersionUID = 2668887930201158755L; /** * A set of application instances for the "current application" mechanism.s */ private static ThreadLocal<DecidrUI> currentApplication = new ThreadLocal<DecidrUI>(); /** * @return the current application instance */ public static DecidrUI getCurrent() { return currentApplication.get(); } /** * Remove the current application instance */ public static void removeCurrent() { currentApplication.remove(); } /** * Set the current application instance * * @param application * current application */ public static void setCurrent(DecidrUI application) { if (getCurrent() == null) { currentApplication.set(application); } } /** * The main window of this application. */ private Window main; /** * Builder pattern: director instance for the main window. */ private UIDirector director; /** * The current role / user. */ private Role user; // /////////////////////////////////////////////// // "Current application" mechanism methods // /////////////////////////////////////////////// /** * The ID of the currently active tenant. */ private Long currentTenantId; /** * @return the current tenant ID */ public Long getCurrentTenantId() { return currentTenantId; } /** * @return the current user / role */ public Role getCurrentUser() { return user; } /** * @return the director for building new UI elements. */ public UIDirector getUIDirector() { return director; } @Override public void init() { /* So that we immediately have access to the current application */ setCurrent(this); main = new Window(); setMainWindow(main); /* * Handle URLs such as * http://decidr.de/WebPortal/invitation?id=123&type=joinTenant */ main.addParameterHandler(new InvitationParameterHandler()); main.addParameterHandler(new ConfirmationParameterHandler()); main.addParameterHandler(new TenantParameterHandler()); main.addParameterHandler(new LoginParameterHandler()); director = new UIDirector(); director.createNewView(); /* * We're always starting as an unregistered user. */ UIBuilder ui = new UnregisteredUserViewBuilder(); director.setUiBuilder(ui); director.constructView(); user = new UserRole(UserRole.UNKNOWN_USER_ID); /* * FIXME The "decidr" theme is definitly broken since the upgrade to * Vaadin 6.2 ~dh */ setTheme(UIConventions.DEFAULT_THEME_NAME); /* * I guess this adds the navigation menu to the main window? TODO * document ~dh */ main.addComponent(director.getTemplateView()); /* * Register a transaction listener that updates our ThreadLocal with * each request */ if (getContext() != null) { getContext().addTransactionListener(this); } } /** * @param currentTenantId * the tenant ID to set */ public void setCurrentTenantId(Long currentTenantId) { this.currentTenantId = currentTenantId; } /** * Set the current user & role. * * @param user * the user to set */ public void setCurrentUser(Role user) { this.user = user; } @Override public void transactionEnd(Application application, Object transactionData) { if (application == this) { // Remove locale from the executing thread removeCurrent(); } } @Override public void transactionStart(Application application, Object transactionData) { if (application == this) { DecidrUI.setCurrent(this); } } }