View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.web.bind;
17  
18  import org.kuali.rice.core.api.CoreConstants;
19  import org.kuali.rice.core.api.datetime.DateTimeService;
20  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
21  import org.kuali.rice.core.api.util.RiceKeyConstants;
22  import org.kuali.rice.core.web.format.FormatException;
23  
24  import java.beans.PropertyEditorSupport;
25  import java.io.Serializable;
26  import java.sql.Date;
27  import java.text.ParseException;
28  import java.util.Calendar;
29  
30  /**
31   * PropertyEditor converts between date display strings and <code>java.sql.Date</code> objects
32   * 
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   */
35  public class UifDateEditor extends PropertyEditorSupport implements Serializable {
36      private static final long serialVersionUID = 8122469337264797008L;
37  
38      /** The date time service. */
39      private transient DateTimeService dateTimeService;
40  
41      /**
42       * This overridden method uses the
43       * <code>org.kuali.rice.core.api.datetime.DateTimeService</code> to convert
44       * the date object to the display string.
45       * 
46       * @see java.beans.PropertyEditorSupport#getAsText()
47       */
48      @Override
49      public String getAsText() {
50          if (this.getValue() == null) {
51              return null;
52          }
53          if ("".equals(this.getValue())) {
54              return null;
55          }
56          return getDateTimeService().toDateString((java.util.Date) this.getValue());
57      }
58  
59      /**
60       * Gets the date time service.
61       * 
62       * @return the date time service
63       */
64      protected DateTimeService getDateTimeService() {
65          if (this.dateTimeService == null) {
66              this.dateTimeService = GlobalResourceLoader.getService(CoreConstants.Services.DATETIME_SERVICE);
67          }
68          return this.dateTimeService;
69      }
70  
71      /**
72       * This overridden method converts the display string to a
73       * <code>java.sql.Date</code> object using the
74       * <code>org.kuali.rice.core.api.datetime.DateTimeService</code>.
75       * 
76       * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
77       */
78      @Override
79      public void setAsText(String text) throws IllegalArgumentException {
80          this.setValue(convertToObject(text));
81      }
82  
83      /**
84       * Convert display text to <code>java.sql.Date</code> object using the
85       * <code>org.kuali.rice.core.api.datetime.DateTimeService</code>.
86       * 
87       * @param text
88       *            the display text
89       * @return the <code>java.sql.Date</code> object
90       * @throws IllegalArgumentException
91       *             the illegal argument exception
92       */
93      protected Object convertToObject(String text) throws IllegalArgumentException {
94          try {
95              Date result = getDateTimeService().convertToSqlDate(text);
96              Calendar calendar = Calendar.getInstance();
97              calendar.setTime(result);
98              if (calendar.get(Calendar.YEAR) < 1000 && verbatimYear(text).length() < 4) {
99                  throw new FormatException("illegal year format", RiceKeyConstants.ERROR_DATE, text);
100             }
101             return result;
102         } catch (ParseException e) {
103             throw new FormatException("parsing", RiceKeyConstants.ERROR_DATE, text, e);
104         }
105     }
106 
107     /**
108      * For a given user input date, this method returns the exact string the
109      * user entered after the last slash. This allows the formatter to
110      * distinguish between ambiguous values such as "/06" "/6" and "/0006"
111      * 
112      * @param date
113      * @return
114      */
115     private String verbatimYear(String date) {
116         String result = "";
117 
118         int pos = date.lastIndexOf("/");
119         if (pos >= 0) {
120             result = date.substring(pos);
121         }
122 
123         return result;
124     }
125 
126 }