1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
29
30
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 }