comparison src/main/java/de/mpiwg/itgroup/ismi/util/guiComponents/FacesUtils.java @ 1:2e911857a759

(none)
author jurzua
date Wed, 29 Oct 2014 14:00:28 +0000
parents
children
comparison
equal deleted inserted replaced
0:74df02964906 1:2e911857a759
1 package de.mpiwg.itgroup.ismi.util.guiComponents;
2 import javax.faces.FactoryFinder;
3 import javax.faces.application.Application;
4 import javax.faces.application.ApplicationFactory;
5 import javax.faces.application.FacesMessage;
6 import javax.faces.context.ExternalContext;
7 import javax.faces.context.FacesContext;
8 import javax.faces.el.ValueBinding;
9 import javax.servlet.ServletContext;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpSession;
12
13 /**
14 * JSF utilities.
15 */
16 public class FacesUtils {
17 /**
18 * Get servlet context.
19 *
20 * @return the servlet context
21 */
22 public static ServletContext getServletContext() {
23 return (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
24 }
25
26 public static ExternalContext getExternalContext() {
27 FacesContext fc = FacesContext.getCurrentInstance();
28 return fc.getExternalContext();
29 }
30
31 public static HttpSession getHttpSession(boolean create) {
32 return (HttpSession) FacesContext.getCurrentInstance().
33 getExternalContext().getSession(create);
34 }
35
36 /**
37 * Get managed bean based on the bean name.
38 *
39 * @param beanName the bean name
40 * @return the managed bean associated with the bean name
41 */
42 public static Object getManagedBean(String beanName) {
43
44 return getValueBinding(getJsfEl(beanName)).getValue(FacesContext.getCurrentInstance());
45 }
46
47 /**
48 * Remove the managed bean based on the bean name.
49 *
50 * @param beanName the bean name of the managed bean to be removed
51 */
52 public static void resetManagedBean(String beanName) {
53 getValueBinding(getJsfEl(beanName)).setValue(FacesContext.getCurrentInstance(), null);
54 }
55
56 /**
57 * Store the managed bean inside the session scope.
58 *
59 * @param beanName the name of the managed bean to be stored
60 * @param managedBean the managed bean to be stored
61 */
62 public static void setManagedBeanInSession(String beanName, Object managedBean) {
63 FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(beanName, managedBean);
64 }
65
66 /**
67 * Get parameter value from request scope.
68 *
69 * @param name the name of the parameter
70 * @return the parameter value
71 */
72 public static String getRequestParameter(String name) {
73 return (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(name);
74 }
75
76 /**
77 * Add information message.
78 *
79 * @param msg the information message
80 */
81 public static void addInfoMessage(String msg) {
82 addInfoMessage(null, msg);
83 }
84
85 /**
86 * Add information message to a specific client.
87 *
88 * @param clientId the client id
89 * @param msg the information message
90 */
91 public static void addInfoMessage(String clientId, String msg) {
92 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg));
93 }
94
95 /**
96 * Add error message.
97 *
98 * @param msg the error message
99 */
100 public static void addErrorMessage(String msg) {
101 addErrorMessage(null, msg);
102 }
103
104 /**
105 * Add error message to a specific client.
106 *
107 * @param clientId the client id
108 * @param msg the error message
109 */
110 public static void addErrorMessage(String clientId, String msg) {
111 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg));
112 }
113
114 private static Application getApplication() {
115 ApplicationFactory appFactory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
116 return appFactory.getApplication();
117 }
118
119 private static ValueBinding getValueBinding(String el) {
120 return getApplication().createValueBinding(el);
121 }
122
123 private static HttpServletRequest getServletRequest() {
124 return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
125 }
126
127 private static Object getElValue(String el) {
128 return getValueBinding(el).getValue(FacesContext.getCurrentInstance());
129 }
130
131 private static String getJsfEl(String value) {
132 return "#{" + value + "}";
133 }
134 }