View Javadoc

1   package com.atlassian.jira.util;
2   
3   import java.text.ParseException;
4   import java.util.Date;
5   import java.util.Locale;
6   import java.util.Map;
7   
8   import org.apache.log4j.Logger;
9   
10  import com.atlassian.jira.ManagerFactory;
11  import com.opensymphony.util.TextUtils;
12  
13  /**
14   * FQCN : com.atlassian.jira.util.ExtendedParameterUtils
15   * @author Kaamelot
16   * @since 3.10.1.30
17   * Description : Extend Parse mechanism in order to implement Date Time parameters
18   *
19   */
20  public class ExtendedParameterUtils extends ParameterUtils {
21  
22  	private static final Logger log = Logger.getLogger(ExtendedParameterUtils.class);
23  
24  	/**
25  	 * @param params Parameters
26  	 * @param s Parameter Key
27  	 * @param locale Locale to use
28  	 * @return Parameter parsed from DateTime or Date 
29  	 * @throws DateTooEarlyException
30  	 */
31  	public static Date getDateTimeParam(Map params, String s, Locale locale) throws DateTooEarlyException {
32  		String paramValue = getStringParam(params, s);
33  		return parseDatetime(paramValue, locale);
34  
35  	}
36  
37  	/**
38  	 * @param paramValue Parameter to parse as DateTime
39  	 * @param locale Locale to use
40  	 * @return Parameter parses as DateTime, or in Date in case of ParseException
41  	 */
42  	public static Date parseDatetime(String paramValue, Locale locale) {
43  		Date date = null;
44  		if (TextUtils.stringSet(paramValue)) {
45  			try {
46  				date = ManagerFactory.getOutlookDateManager().getOutlookDate(locale).parseDateTimePicker(paramValue);
47  				if (date.getTime() < 0) // this is an invalid date
48  				{
49  					throw new DateTooEarlyException();
50  				} else {
51  					return date;
52  				}
53  			} catch (ParseException e) {
54  				log.warn("Could not parse: " + paramValue + " into a DateTime. Try to parse as Date");
55  				date = parseDate(paramValue, locale); 
56  			}
57  		}
58  		return date;
59  	}
60  
61  }