View Javadoc
1   /**
2    * Copyright 2005-2015 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.apache.commons.lang.StringUtils;
19  import org.joda.time.DateTime;
20  import org.kuali.rice.core.api.CoreConstants;
21  import org.kuali.rice.core.api.datetime.DateTimeService;
22  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
23  import org.kuali.rice.core.api.util.RiceKeyConstants;
24  import org.kuali.rice.core.web.format.FormatException;
25  
26  import java.beans.PropertyEditorSupport;
27  import java.io.Serializable;
28  import java.sql.Date;
29  import java.sql.Timestamp;
30  import java.text.ParseException;
31  import java.util.Calendar;
32  
33  /**
34   * PropertyEditor converts between timestamp display strings and @{DateTime} objects.
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  public class UifDateTimeEditor extends PropertyEditorSupport implements Serializable {
39  
40      private static final long serialVersionUID = -1315597474978280713L;
41  
42      private transient DateTimeService dateTimeService;
43  
44      /**
45       * {@inheritDoc}
46       *
47       * <p>
48       * This implementation uses the {@link DateTimeService} to convert the {@link DateTime} object to a
49       * {@link Timestamp} display string.
50       * </p>
51       */
52      @Override
53      public String getAsText() {
54          if (getValue() == null) {
55              return null;
56          }
57  
58          if (getValue().equals(StringUtils.EMPTY)) {
59              return null;
60          }
61  
62          DateTime value = (DateTime) getValue();
63  
64          return getDateTimeService().toDateTimeString(new Timestamp(value.getMillis()));
65      }
66  
67      /**
68       * {@inheritDoc}
69       *
70       * This implementation converts the {@link Date} display string to a {@link DateTime} object using the
71       * {@link DateTimeService}.
72       */
73      @Override
74      public void setAsText(String text) throws IllegalArgumentException {
75          if (StringUtils.isBlank(text)) {
76              setValue(null);
77              return;
78          }
79  
80          try {
81              Date value = getDateTimeService().convertToSqlDate(text);
82  
83              Calendar calendar = Calendar.getInstance();
84              calendar.setTime(value);
85              if (calendar.get(Calendar.YEAR) < 1000 && verbatimYear(text).length() < 4) {
86                  throw new FormatException("illegal year format", RiceKeyConstants.ERROR_DATE, text);
87              }
88  
89              setValue(new DateTime(value.getTime()));
90          } catch (ParseException e) {
91              throw new FormatException("parsing", RiceKeyConstants.ERROR_DATE, text, e);
92          }
93      }
94  
95      /**
96       * For a given user input date, this method returns the exact string the
97       * user entered after the last slash. This allows the formatter to
98       * distinguish between ambiguous values such as "/06" "/6" and "/0006"
99       *
100      * @param date the date to process
101      *
102      * @return the year
103      */
104     protected String verbatimYear(String date) {
105         String result = "";
106 
107         int pos = date.lastIndexOf("/");
108         if (pos >= 0) {
109             result = date.substring(pos);
110         }
111 
112         return result;
113     }
114 
115     /**
116      * Returns the {@link DateTimeService}.
117      *
118      * @return the {@link DateTimeService}
119      */
120     protected DateTimeService getDateTimeService() {
121         if (dateTimeService == null) {
122             dateTimeService = GlobalResourceLoader.getService(CoreConstants.Services.DATETIME_SERVICE);
123         }
124 
125         return dateTimeService;
126     }
127 
128 }