001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sections/tags/sakai_2-4-1/sections-app-util/src/java/org/sakaiproject/tool/section/jsf/JsfUtil.java $
003: * $Id: JsfUtil.java 21060 2007-02-06 20:48:59Z ray@media.berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Regents of the University of California and The Regents of the University of Michigan
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.section.jsf;
021:
022: import java.sql.Time;
023: import java.text.MessageFormat;
024: import java.text.ParseException;
025: import java.text.SimpleDateFormat;
026: import java.util.Comparator;
027: import java.util.Date;
028: import java.util.Locale;
029: import java.util.ResourceBundle;
030:
031: import javax.faces.application.FacesMessage;
032: import javax.faces.context.FacesContext;
033: import javax.faces.model.SelectItem;
034:
035: import org.apache.commons.lang.StringUtils;
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038: import org.sakaiproject.jsf.util.ConversionUtil;
039: import org.sakaiproject.jsf.util.LocaleUtil;
040: import org.sakaiproject.tool.section.jsf.MessagingBean;
041:
042: /**
043: * A utility to help deal with common tasks in JSF.
044: *
045: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
046: *
047: */
048: public class JsfUtil {
049: private static final Log log = LogFactory.getLog(JsfUtil.class);
050:
051: /**
052: * As part of the crutch for JSF's inability to do validation on relative
053: * values in different components. This pattern defines how times should
054: * be displayed in the Section Info UI.
055: */
056: public static final String TIME_PATTERN_DISPLAY = "h:mm";
057:
058: /**
059: * As part of the crutch for JSF's inability to do validation on relative
060: * values in different components. This pattern defines how to parse and
061: * format complete times (with hours, minutes, and am/pm marker).
062: */
063: public static final String TIME_PATTERN_LONG = "h:mm a";
064:
065: /**
066: * As part of the crutch for JSF's inability to do validation on relative
067: * values in different components. This pattern defines how to parse and
068: * format abberviated times (with only hours and am/pm marker).
069: */
070: public static final String TIME_PATTERN_SHORT = "h a";
071:
072: /**
073: * To cut down on configuration noise, allow access to request-scoped beans from
074: * session-scoped beans, and so on, this method lets the caller try to find
075: * anything anywhere that Faces can look for it.
076: *
077: * WARNING: If what you're looking for is a managed bean and it isn't found,
078: * it will be created as a result of this call.
079: */
080: public static final Object resolveVariable(String name) {
081: FacesContext context = FacesContext.getCurrentInstance();
082: Object result = context.getApplication().getVariableResolver()
083: .resolveVariable(context, name);
084: if (log.isDebugEnabled())
085: log
086: .debug("JSF variable " + name + " resolved to "
087: + result);
088: return result;
089: }
090:
091: /**
092: * Adds an error message for display on a page when the page is guaranteed
093: * not to be displayed via a redirect.
094: *
095: * @param message
096: */
097: public static void addErrorMessage(String message) {
098: FacesContext.getCurrentInstance().addMessage(
099: null,
100: new FacesMessage(FacesMessage.SEVERITY_ERROR, message,
101: null));
102: }
103:
104: /**
105: * Adds an error message for display on a component when the page is guaranteed
106: * not to be displayed via a redirect.
107: *
108: * @param message
109: * @param componentId
110: */
111: public static void addErrorMessage(String message,
112: String componentId) {
113: FacesContext.getCurrentInstance().addMessage(
114: componentId,
115: new FacesMessage(FacesMessage.SEVERITY_ERROR, message,
116: null));
117: }
118:
119: /**
120: * Adds an info message for display on a page when the page is guaranteed
121: * not to be displayed via a redirect.
122: *
123: * @param message
124: */
125: public static void addInfoMessage(String message) {
126: FacesContext.getCurrentInstance().addMessage(
127: null,
128: new FacesMessage(FacesMessage.SEVERITY_INFO, message,
129: null));
130: }
131:
132: /**
133: * Adds an info message for display on a page even if faces sends the user
134: * to the page via a redirect.
135: *
136: * @param message
137: */
138: public static void addRedirectSafeInfoMessage(String message) {
139: MessagingBean mb = (MessagingBean) resolveVariable("messagingBean");
140: mb.addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO,
141: message, null));
142: }
143:
144: /**
145: * Adds a warning message for display on a page even if faces sends the user
146: * to the page via a redirect.
147: *
148: * @param message
149: */
150: public static void addRedirectSafeWarnMessage(String message) {
151: MessagingBean mb = (MessagingBean) resolveVariable("messagingBean");
152: mb.addMessage(new FacesMessage(FacesMessage.SEVERITY_WARN,
153: message, null));
154: }
155:
156: /**
157: * Gets a localized message from the message bundle.
158: */
159: public static String getLocalizedMessage(String key) {
160: return LocaleUtil.getLocalizedString(FacesContext
161: .getCurrentInstance(), "sections", key);
162: }
163:
164: /**
165: * Gets a localized message from the message bundle and formats it using the
166: * parameter array.
167: *
168: * @param key
169: * @param params
170: * @return
171: */
172: public static String getLocalizedMessage(String key, String[] params) {
173: String rawString = getLocalizedMessage(key);
174: MessageFormat format = new MessageFormat(rawString);
175: return format.format(params);
176: }
177:
178: /**
179: * Gets a value from the request parameter map, as provided by the faces
180: * context.
181: *
182: * @param string
183: * @return
184: */
185: public static String getStringFromParam(String string) {
186: return (String) FacesContext.getCurrentInstance()
187: .getExternalContext().getRequestParameterMap().get(
188: string);
189: }
190:
191: /**
192: * Converts a string and a boolean (am) into a java.sql.Time object.
193: *
194: * @param str
195: * @param am
196: * @return
197: */
198: public static Time convertStringToTime(String str, boolean am) {
199: if (StringUtils.trimToNull(str) == null) {
200: return null;
201: }
202:
203: // Set the am/pm flag to ensure that the time is parsed properly
204: if (am) {
205: str = str + " AM";
206: } else {
207: str = str + " PM";
208: }
209:
210: String pattern = (str.indexOf(':') != -1) ? JsfUtil.TIME_PATTERN_LONG
211: : JsfUtil.TIME_PATTERN_SHORT;
212: SimpleDateFormat sdf = new SimpleDateFormat(pattern);
213: Date date;
214: try {
215: date = sdf.parse(str);
216: } catch (ParseException pe) {
217: throw new RuntimeException(
218: "A bad date made it through validation! This should never happen!");
219: }
220: return ConversionUtil.convertDateToTime(date, am);
221: }
222:
223: public static String convertTimeToString(Time time) {
224: if (time == null) {
225: return null;
226: }
227: SimpleDateFormat sdf = new SimpleDateFormat(
228: JsfUtil.TIME_PATTERN_DISPLAY);
229: return sdf.format(time);
230: }
231:
232: public static final Comparator getSelectItemComparator() {
233: return new Comparator() {
234: public int compare(Object o1, Object o2) {
235: SelectItem item1 = (SelectItem) o1;
236: SelectItem item2 = (SelectItem) o2;
237: return item1.getLabel().toString().compareTo(
238: item2.getLabel().toString());
239: }
240: };
241: }
242: }
|