View Javadoc

1   package org.appfuse.webapp.client.application;
2   
3   import java.util.ArrayList;
4   import java.util.logging.Level;
5   import java.util.logging.Logger;
6   
7   import org.appfuse.webapp.client.application.base.request.RequestEvent;
8   import org.appfuse.webapp.client.application.base.security.AuthRequiredEvent;
9   import org.appfuse.webapp.client.application.base.security.LoginEvent;
10  import org.appfuse.webapp.client.application.base.security.LogoutEvent;
11  import org.appfuse.webapp.client.application.base.security.RequestForbidenEvent;
12  import org.appfuse.webapp.client.proxies.LookupConstantsProxy;
13  import org.appfuse.webapp.client.proxies.UserProxy;
14  import org.appfuse.webapp.client.requests.ApplicationRequestFactory;
15  import org.appfuse.webapp.client.ui.Shell;
16  import org.appfuse.webapp.client.ui.home.HomePlace;
17  import org.appfuse.webapp.client.ui.login.LoginPlace;
18  
19  import com.github.gwtbootstrap.client.ui.constants.AlertType;
20  import com.google.gwt.activity.shared.ActivityManager;
21  import com.google.gwt.core.client.GWT;
22  import com.google.gwt.dom.client.Document;
23  import com.google.gwt.dom.client.Element;
24  import com.google.gwt.logging.client.LogConfiguration;
25  import com.google.gwt.place.shared.Place;
26  import com.google.gwt.place.shared.PlaceChangeEvent;
27  import com.google.gwt.place.shared.PlaceController;
28  import com.google.gwt.place.shared.PlaceHistoryHandler;
29  import com.google.gwt.place.shared.PlaceHistoryMapper;
30  import com.google.gwt.user.client.DOM;
31  import com.google.gwt.user.client.History;
32  import com.google.gwt.user.client.Window;
33  import com.google.gwt.user.client.ui.RootLayoutPanel;
34  import com.google.inject.Inject;
35  import com.google.web.bindery.event.shared.EventBus;
36  import com.google.web.bindery.requestfactory.gwt.client.RequestFactoryLogHandler;
37  import com.google.web.bindery.requestfactory.shared.LoggingRequest;
38  import com.google.web.bindery.requestfactory.shared.Receiver;
39  
40  /**
41   * Application for browsing entities.
42   */
43  public class DesktopApplication extends Application implements LoginEvent.Handler, LogoutEvent.Handler {
44      private static final Logger LOGGER = Logger.getLogger(DesktopApplication.class.getName());
45  
46      @Inject
47      public DesktopApplication(
48              final Shell shell,
49              final ApplicationMenu menu,
50              final ApplicationRequestFactory requestFactory,
51              final EventBus eventBus,
52              final PlaceController placeController,
53              final PlaceHistoryMapper placeHistoryMapper,
54              final PlaceHistoryHandler placeHistoryHandler,
55              final ActivityManager activityManager,
56              final ApplicationProxyFactory proxyFactory,
57              final ApplicationValidatorFactory validatorFactory) {
58          super(shell, menu, requestFactory, eventBus, placeController, placeHistoryMapper, placeHistoryHandler, activityManager, proxyFactory,
59                  validatorFactory);
60      }
61  
62      @Override
63      public void run() {
64          setProgress(50);
65  
66          /* Add handlers */
67          initHandlers();
68  
69          setProgress(60);
70          /* load application constants */
71          requestFactory.lookupRequest().getApplicationConstants().fire(new Receiver<LookupConstantsProxy>() {
72              @Override
73              public void onSuccess(final LookupConstantsProxy lookupConstants) {
74                  setLookupConstants(lookupConstants);
75                  setProgress(70);
76  
77                  /* Authentication */
78                  requestFactory.userRequest().getCurrentUser().with("roles").fire(new Receiver<UserProxy>() {
79  
80                      @Override
81                      public void onSuccess(final UserProxy currentUser) {
82                          setProgress(85);
83  
84                          if (currentUser != null) {
85                              setCurrentUser(currentUser);
86  
87                              setProgress(100);
88                              showShell();
89  
90                              /*
91                               * Register home place and parse url for current
92                               * place token
93                               */
94                              final Place defaultPlace = new HomePlace();
95                              placeHistoryHandler.register(placeController, eventBus, defaultPlace);
96                              placeHistoryHandler.handleCurrentHistory();
97                              shell.onLoginEvent(new LoginEvent());
98                          } else {
99                              showShell();
100 
101                             /*
102                              * Register home place and parse url for current
103                              * place token
104                              */
105                             final Place defaultPlace = new LoginPlace(History.getToken());
106                             placeHistoryHandler.register(placeController, eventBus, defaultPlace);
107                             placeController.goTo(defaultPlace);
108                         }
109                     }
110                 });
111 
112             }
113         });
114 
115     }
116 
117     protected void showShell() {
118         final Element loading = Document.get().getElementById("loading");
119         loading.getParentElement().removeChild(loading);
120 
121         /* And show the user the shell */
122         RootLayoutPanel.get().add(shell);
123         RootLayoutPanel.get().getElement().setId("rootPanel");
124 
125         // remove gwt extra divs and place shell nodes directly into the
126         // document body,
127         // (and hope for the best about xbrowser compatibility..)
128         shell.getElement().setId("shell");
129         final Element shellElement = Document.get().getElementById("shell");
130         shellElement.removeFromParent();
131         Document.get().getElementById("rootPanel").removeFromParent();
132 
133         Element shellChildElement = shellElement.getFirstChildElement();
134         while (shellChildElement != null) {
135             shellChildElement.removeFromParent();
136             Document.get().getBody().appendChild(shellChildElement);
137             shellChildElement = shellElement.getFirstChildElement();
138         }
139 
140     }
141 
142     protected void initHandlers() {
143 
144         GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {
145             @Override
146             public void onUncaughtException(final Throwable e) {
147                 Window.alert("Error: " + e.getMessage());
148                 LOGGER.log(Level.SEVERE, e.getMessage(), e);
149             }
150         });
151 
152         if (LogConfiguration.loggingIsEnabled()) {
153             // Add remote logging handler
154             final RequestFactoryLogHandler.LoggingRequestProvider provider = new RequestFactoryLogHandler.LoggingRequestProvider() {
155                 @Override
156                 public LoggingRequest getLoggingRequest() {
157                     return requestFactory.loggingRequest();
158                 }
159             };
160             Logger.getLogger("").addHandler(new RequestFactoryLogHandler(provider, Level.WARNING, new ArrayList<String>()));
161         }
162 
163         RequestEvent.register(eventBus, new RequestEvent.Handler() {
164             // Only show loading status if a request isn't serviced in 250ms.
165             private static final int LOADING_TIMEOUT = 250;
166 
167             @Override
168             public void onRequestEvent(final RequestEvent requestEvent) {
169                 if (requestEvent.getState() == RequestEvent.State.SENT) {
170                     DOM.setStyleAttribute(shell.getElement(), "cursor", "wait");
171                     shell.getMole().showDelayed(LOADING_TIMEOUT);
172                 } else {
173                     DOM.setStyleAttribute(shell.getElement(), "cursor", "default");
174                     shell.getMole().hide();
175                 }
176             }
177         });
178 
179         if (shell instanceof PlaceChangeEvent.Handler) {
180             eventBus.addHandler(PlaceChangeEvent.TYPE, (PlaceChangeEvent.Handler) shell);
181         }
182 
183         LoginEvent.register(eventBus, this);
184         LogoutEvent.register(eventBus, this);
185 
186         AuthRequiredEvent.register(eventBus, new AuthRequiredEvent.Handler() {
187             @Override
188             public void onAuthRequiredEvent(final AuthRequiredEvent authRequiredEvent) {
189                 placeController.goTo(new LoginPlace(History.getToken()));
190             }
191         });
192 
193         RequestForbidenEvent.register(eventBus, new RequestForbidenEvent.Handler() {
194 
195             @Override
196             public void onRequestForbidenEvent(final RequestForbidenEvent requestForbidenEvent) {
197                 shell.addMessage(i18n._03_title(), AlertType.ERROR);
198             }
199         });
200     }
201 
202     @Override
203     public void onLoginEvent(final LoginEvent loginEvent) {
204 
205         requestFactory.userRequest().getCurrentUser().with("roles").fire(new Receiver<UserProxy>() {
206             @Override
207             public void onSuccess(final UserProxy currentUser) {
208                 if (currentUser != null) {
209                     setCurrentUser(currentUser);
210                     /* re-load application constants */
211                     requestFactory.lookupRequest().getApplicationConstants().fire(new Receiver<LookupConstantsProxy>() {
212                         @Override
213                         public void onSuccess(final LookupConstantsProxy lookupConstants) {
214                             setLookupConstants(lookupConstants);
215                             shell.onLoginEvent(loginEvent);
216 
217                             final Place currentPlace = placeController.getWhere();
218                             if (currentPlace instanceof LoginPlace) {// explicit
219                                                                      // login
220                                 final LoginPlace loginPlace = (LoginPlace) currentPlace;
221                                 if (loginPlace.getHistoryToken() != null && !"".equals(loginPlace.getHistoryToken())) {
222                                     History.newItem(loginPlace.getHistoryToken());
223                                 } else {
224                                     placeController.goTo(new HomePlace());
225                                 }
226                             } else {
227                                 // this was an intercepted login so we leave
228                                 // user on current page
229                             }
230                         }
231                     });
232                 }
233             }
234         });
235     }
236 
237     @Override
238     public void onLogoutEvent(final LogoutEvent logoutEvent) {
239         setCurrentUser(null);
240         shell.onLogoutEvent(logoutEvent);
241         placeController.goTo(new LoginPlace());
242     }
243 
244     /* The progressbar */
245     private Element progressbar;
246 
247     private void setProgress(final int progress) {
248         if (progressbar == null) {
249             progressbar = Document.get().getElementById("progressbar");
250         }
251         progressbar.setAttribute("style", "width: " + progress + "%;");
252     }
253 
254 }