View Javadoc

1   /*
2    * Copyright 2007 The International Moth Class Association (IMCA)
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 net.sf.imca.web.backingbeans;
17  
18  import java.util.Locale;
19  
20  import javax.faces.FactoryFinder;
21  import javax.faces.application.Application;
22  import javax.faces.application.ApplicationFactory;
23  import javax.faces.context.FacesContext;
24  import javax.faces.el.ValueBinding;
25  import net.sf.imca.model.ImcaUtils;
26  
27  /**
28   * Utilities for Java Server Faces.
29   * 
30   * @author dougculnane
31   */
32  public class Utils {
33  
34      protected static final String ACTION_FAIL = "fail";
35  
36      protected static final String ACTION_SUCCESS = "success";
37      
38      protected static final String ACTION_LOGOUT = "logout";
39  
40      protected static final String WEB_USER_BEAN_NAME = "webUser";
41      
42      protected static final String ACTION_CONFIRM = "confirm";
43      
44      protected static final String ACTION_BACK = "back";
45  
46  
47      public static Object getManagedBean(String beanName) {
48  
49          ApplicationFactory appFactory = (ApplicationFactory) FactoryFinder
50                  .getFactory(FactoryFinder.APPLICATION_FACTORY);
51          Application application = appFactory.getApplication();
52          ValueBinding valueBinding = application.createValueBinding("#{"
53                  + beanName + "}");
54  
55          return valueBinding.getValue(FacesContext.getCurrentInstance());
56      }
57      
58      public static void setManagedBean(String beanName, Object bean) {
59  
60          ApplicationFactory appFactory = (ApplicationFactory) FactoryFinder
61                  .getFactory(FactoryFinder.APPLICATION_FACTORY);
62          Application application = appFactory.getApplication();
63          ValueBinding valueBinding = application.createValueBinding("#{"
64                  + beanName + "}");
65          valueBinding.setValue(FacesContext.getCurrentInstance(), bean);
66      }
67      
68      public static Locale getWebAppLocale() {
69          WebUser webUser = (WebUser) Utils.getManagedBean(WEB_USER_BEAN_NAME);
70          Locale locale = Locale.ENGLISH;
71          if (webUser != null && webUser.getPerson() != null) {
72              locale = webUser.getPerson().getLocale();    
73          }
74          return locale;
75      }
76  
77      public static WebUser getWebUser() {
78          return (WebUser)getManagedBean(WEB_USER_BEAN_NAME);
79      }
80  
81      public static void setWebUser(WebUser webUser) {
82          Utils.setManagedBean(WEB_USER_BEAN_NAME, webUser);
83      }
84      
85      public static String getMessage(String messageKey) {
86          return ImcaUtils.getMessage(messageKey, getWebAppLocale());
87      }
88  }