View Javadoc

1   /*
2    * $Id: ContextTools.java,v 1.18 2011/06/16 09:29:41 jbmeslin Exp $
3    * 
4    */
5   package org.rcfaces.core.internal.tools;
6   
7   import java.util.Locale;
8   import java.util.Map;
9   import java.util.TimeZone;
10  
11  import javax.faces.component.UIViewRoot;
12  import javax.faces.context.ExternalContext;
13  import javax.faces.context.FacesContext;
14  
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  
18  /**
19   * 
20   * @author Olivier Oeuillot (latest modification by $Author: jbmeslin $)
21   * @version $Revision: 1.18 $ $Date: 2011/06/16 09:29:41 $
22   */
23  public final class ContextTools {
24      private static final String REVISION = "$Revision: 1.18 $";
25  
26      private static final Log LOG = LogFactory.getLog(ContextTools.class);
27  
28      public static final Object resolveAttribute(FacesContext facesContext,
29              String attributeName) {
30  
31          ExternalContext externalContext = facesContext.getExternalContext();
32  
33          Object value = externalContext.getRequestMap().get(attributeName);
34          if (value != null) {
35              return value;
36          }
37  
38          Map session = externalContext.getSessionMap();
39          if (session != null) {
40              value = session.get(attributeName);
41              if (value != null) {
42                  return value;
43              }
44          }
45  
46          value = externalContext.getApplicationMap().get(attributeName);
47          if (value != null) {
48              return value;
49          }
50  
51          return null;
52      }
53  
54      public static final String resolveText(FacesContext facesContext,
55              String bundleVar, String attributeName) {
56  
57          Object bundle = ContextTools.resolveAttribute(facesContext, bundleVar);
58          if (bundle instanceof Map) {
59              String rtext = (String) ((Map) bundle).get(attributeName);
60              if (rtext != null) {
61                  return rtext;
62              }
63  
64              return "???" + attributeName + " (key not found)???";
65          }
66  
67          return "???" + attributeName + " (bundle not found)???";
68      }
69  
70      public static Locale getUserLocale(FacesContext facesContext) {
71          if (facesContext == null) {
72              facesContext = FacesContext.getCurrentInstance();
73          }
74  
75          UIViewRoot viewRoot = facesContext.getViewRoot();
76          if (viewRoot != null) {
77              Locale locale = viewRoot.getLocale();
78              if (locale != null) {
79                  if (LOG.isDebugEnabled()) {
80                      LOG.debug("Get locale from viewRoot: " + locale);
81                  }
82                  return locale;
83              }
84          }
85  
86          Locale locale = facesContext.getApplication().getViewHandler()
87                  .calculateLocale(facesContext);
88          if (LOG.isDebugEnabled()) {
89              LOG.debug("Calculate locale from view handler: " + locale);
90          }
91  
92          return locale;
93      }
94  
95      public static TimeZone getUserTimeZone(FacesContext facesContext) {
96          // TODO Auto-generated method stub
97          return null;
98      }
99  }