View Javadoc

1   /**
2    * 
3    */
4   package org.appfuse.webapp.client.application;
5   
6   import javax.validation.Validation;
7   import javax.validation.ValidatorFactory;
8   
9   import org.appfuse.webapp.client.proxies.LookupConstantsProxy;
10  import org.appfuse.webapp.client.proxies.RoleProxy;
11  import org.appfuse.webapp.client.proxies.UserProxy;
12  import org.appfuse.webapp.client.requests.ApplicationRequestFactory;
13  import org.appfuse.webapp.client.ui.Shell;
14  
15  import com.google.gwt.activity.shared.ActivityManager;
16  import com.google.gwt.core.client.GWT;
17  import com.google.gwt.dom.client.Document;
18  import com.google.gwt.dom.client.Element;
19  import com.google.gwt.dom.client.MetaElement;
20  import com.google.gwt.dom.client.NodeList;
21  import com.google.gwt.place.shared.PlaceController;
22  import com.google.gwt.place.shared.PlaceHistoryHandler;
23  import com.google.gwt.place.shared.PlaceHistoryMapper;
24  import com.google.inject.Inject;
25  import com.google.web.bindery.event.shared.EventBus;
26  
27  /**
28   * @author ivangsa
29   *
30   */
31  public abstract class Application {
32  
33      protected final Shell shell;
34      protected final EventBus eventBus;
35      protected final ApplicationMenu menu;
36      protected final PlaceController placeController;
37      protected final PlaceHistoryMapper placeHistoryMapper;
38      protected final PlaceHistoryHandler placeHistoryHandler;
39      protected final ApplicationRequestFactory requestFactory;
40      protected final ValidatorFactory validatorFactory;
41      protected final ApplicationProxyFactory proxyFactory;
42      protected final ApplicationResources i18n = GWT.create(ApplicationResources.class);
43  
44      private String contextPath = "/";
45      private boolean rememberMeEnabled = false;
46      private UserProxy currentUser;
47      private LookupConstantsProxy lookupConstants;
48  
49      @Inject
50      public Application(
51              final Shell shell,
52              final ApplicationMenu menu,
53              final ApplicationRequestFactory requestFactory,
54              final EventBus eventBus,
55              final PlaceController placeController,
56              final PlaceHistoryMapper placeHistoryMapper,
57              final PlaceHistoryHandler placeHistoryHandler,
58              final ActivityManager activityManager,
59              final ApplicationProxyFactory proxyFactory,
60              final ApplicationValidatorFactory validatorFactory) {
61          super();
62          this.shell = shell;
63          this.menu = menu;
64          this.requestFactory = requestFactory;
65          this.eventBus = eventBus;
66          this.placeController = placeController;
67          this.placeHistoryMapper = placeHistoryMapper;
68          this.placeHistoryHandler = placeHistoryHandler;
69          this.proxyFactory = proxyFactory;
70          this.validatorFactory = Validation.buildDefaultValidatorFactory();
71          shell.setApplication(this);
72          activityManager.setDisplay(shell.getContentsPanel());
73  
74          final NodeList<Element> metas = Document.get().getElementsByTagName("meta");
75          for (int i = 0; i < metas.getLength(); i++) {
76              final MetaElement meta = (MetaElement) metas.getItem(i);
77              if ("rememberMeEnabled".equals(meta.getName())) {
78                  rememberMeEnabled = "true".equals(meta.getContent());
79              }
80          }
81  
82          contextPath = GWT.getModuleBaseURL()
83                  .replace(GWT.getModuleName() + "/", "");
84  
85      }
86  
87      public abstract void run();
88  
89      public Shell getShell() {
90          return shell;
91      }
92  
93      public ApplicationMenu getMenu() {
94          return menu;
95      }
96  
97      public EventBus getEventBus() {
98          return eventBus;
99      }
100 
101     public PlaceController getPlaceController() {
102         return placeController;
103     }
104 
105     public ApplicationRequestFactory getRequestFactory() {
106         return requestFactory;
107     }
108 
109     public PlaceHistoryMapper getPlaceHistoryMapper() {
110         return placeHistoryMapper;
111     }
112 
113     public PlaceHistoryHandler getPlaceHistoryHandler() {
114         return placeHistoryHandler;
115     }
116 
117     public ApplicationProxyFactory getProxyFactory() {
118         return proxyFactory;
119     }
120 
121     public ValidatorFactory getValidatorFactory() {
122         return validatorFactory;
123     }
124 
125     public ApplicationResources getI18n() {
126         return i18n;
127     }
128 
129     public String getContextPath() {
130         return contextPath;
131     }
132 
133     public boolean isRememberMeEnabled() {
134         return rememberMeEnabled;
135     }
136 
137     public UserProxy getCurrentUser() {
138         return currentUser;
139     }
140 
141     public void setCurrentUser(final UserProxy currentUser) {
142         this.currentUser = currentUser;
143     }
144 
145     public LookupConstantsProxy getLookupConstants() {
146         return lookupConstants;
147     }
148 
149     public void setLookupConstants(final LookupConstantsProxy lookupConstants) {
150         this.lookupConstants = lookupConstants;
151     }
152 
153     public String getCurrentUsername() {
154         if (getCurrentUser() == null) {
155             return null;
156         }
157         return getCurrentUser().getUsername();
158     }
159 
160     public boolean isUserInRole(final String role) {
161         if (RoleProxy.ANONYMOUS.equals(role)) {
162             return currentUser == null;
163         } else if (RoleProxy.AUTHENTICATED.equals(role)) {
164             return currentUser != null;
165         }
166         else if (currentUser != null && currentUser.getRoles() != null && role != null) {
167             for (final RoleProxy roleProxy : currentUser.getRoles()) {
168                 if (role.equalsIgnoreCase(roleProxy.getName())) {
169                     return true;
170                 }
171             }
172         }
173         return false;
174     }
175 
176     public boolean hasAnyRole(final String[] roles) {
177         if (roles != null) {
178             for (final String role : roles) {
179                 if (isUserInRole(role)) {
180                     return true;
181                 }
182             }
183         }
184         return false;
185     }
186 
187 }