View Javadoc

1   /*
2    * Copyright 2004 Jonathan M. Lehr
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
12   * governing permissions and limitations under the License.
13   *
14   * MODIFIED BY THE KUALI FOUNDATION
15   */
16  // begin Kuali Foundation modification
17  package org.kuali.rice.kns.web.format;
18  // end Kuali Foundation modification
19  
20  // begin Kuali Foundation modification
21  // import order changed, and java.util.Calendar, org.kuali.KeyConstants and org.kuali.rice.KNSServiceLocator added
22  import java.sql.Date;
23  import java.text.ParseException;
24  import java.util.Calendar;
25  
26  import org.kuali.rice.kns.service.DateTimeService;
27  import org.kuali.rice.kns.service.KNSServiceLocator;
28  import org.kuali.rice.kns.util.RiceKeyConstants;
29  
30  /**
31   * begin Kuali Foundation modification
32   * This class is used to format Date objects.
33   * end Kuali Foundation modification
34   */
35  public class DateFormatter extends Formatter {
36      // begin Kuali Foundation modification
37      // serialVersionUID changed from 1L
38      private static final long serialVersionUID = 7612442662886603084L;
39  
40  
41      private static DateTimeService dateTimeService;
42      // end Kuali Foundation modification
43  
44  	// begin Kuali Foundation modification
45  	// static variables DATE_ERROR_KEY and PARSE_MSG removed
46  	// method public String getErrorKey() removed
47  	// end Kuali Foundation modification
48  
49  	// begin Kuali Foundation modification
50  	// added this method
51      /**
52       *
53       * For a given user input date, this method returns the exact string the user entered after the last slash. This allows the
54       * formatter to distinguish between ambiguous values such as "/06" "/6" and "/0006"
55       *
56       * @param date
57       * @return
58       */
59      private String verbatimYear(String date) {
60          String result = "";
61  
62          int pos = date.lastIndexOf("/");
63          if (pos >= 0) {
64              result = date.substring(pos);
65          }
66  
67          return result;
68      }
69      // end Kuali Foundation modification
70  
71  
72      /**
73       * Unformats its argument and return a java.util.Date instance initialized with the resulting string.
74       *
75       * @return a java.util.Date intialized with the provided string
76       */
77      protected Object convertToObject(String target) {
78      	// begin Kuali Foundation modification
79          try {
80              Date result = getDateTimeService().convertToSqlDate(target);
81              Calendar calendar = Calendar.getInstance();
82              calendar.setTime(result);
83              if (calendar.get(Calendar.YEAR) < 1000 && verbatimYear(target).length() < 4) {
84                  throw new FormatException("illegal year format", RiceKeyConstants.ERROR_DATE, target);
85              }
86              return result;
87          }
88          catch (ParseException e) {
89              throw new FormatException("parsing", RiceKeyConstants.ERROR_DATE, target, e);
90          }
91          // end Kuali Foundation modification
92      }
93  
94      /**
95       * Returns a string representation of its argument, formatted as a date with the "MM/dd/yyyy" format.
96       *
97       * @return a formatted String
98       */
99      public Object format(Object value) {
100         if (value == null) {
101             return null;
102         }
103         // begin Kuali Foundation modification
104         if ("".equals(value)) {
105             return null;
106         }
107         return getDateTimeService().toDateString((java.util.Date)value);
108         // end Kuali Foundation modification
109     }
110 
111 
112     public static DateTimeService getDateTimeService() {
113         if ( dateTimeService == null ) {
114             dateTimeService = KNSServiceLocator.getDateTimeService();
115         }
116         return dateTimeService;
117     }
118 
119     /**
120      * This method is invoked to validate a date string using the KNS Service
121      * DateTimeService.
122      *
123      * @param dateString
124      * @return
125      */
126     public boolean validate(String dateString) {
127         boolean isValid=false;
128 
129         DateTimeService service=getDateTimeService();
130         try {
131             service.convertToSqlTimestamp(dateString);
132             isValid=true;
133         } catch (Exception e) {
134 
135         }
136 
137         return isValid;
138 
139     }
140 
141 }