View Javadoc
1   /**
2    * Copyright 2005-2016 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.kuali.rice.core.api.CoreConstants;
20  import org.kuali.rice.core.api.datetime.DateTimeService;
21  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
22  import org.kuali.rice.core.api.util.RiceKeyConstants;
23  import org.kuali.rice.core.web.format.FormatException;
24  
25  import java.beans.PropertyEditorSupport;
26  import java.io.Serializable;
27  import java.sql.Time;
28  import java.text.ParseException;
29  
30  /**
31   * PropertyEditor converts between time display strings and {@code java.sql.Time} objects
32   *
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   */
35  public class UifTimeEditor extends PropertyEditorSupport implements Serializable {
36  
37      private static final long serialVersionUID = 3087028215717940843L;
38  
39      private transient DateTimeService dateTimeService;
40  
41      /**
42       * Converts the time object to the display string format.
43       *
44       * @return the time object in the display string format or null if it is empty
45       */
46      @Override
47      public String getAsText() {
48          if (getValue() == null) {
49              return null;
50          }
51  
52          if (getValue() instanceof String && StringUtils.isBlank((String) getValue())) {
53              return null;
54          }
55  
56          return getDateTimeService().toTimeString((Time) getValue());
57      }
58  
59      /**
60       * Converts the display string to a time object.
61       */
62      @Override
63      public void setAsText(String text) throws IllegalArgumentException {
64          setValue(convertToObject(text));
65      }
66  
67      /**
68       * Converts the display text to a time object.
69       *
70       * @param text the display text
71       *
72       * @return the time object
73       */
74      protected Object convertToObject(String text) {
75          if (StringUtils.isBlank(text)) {
76              return null;
77          }
78  
79          try {
80              return getDateTimeService().convertToSqlTime(text);
81          } catch (ParseException e) {
82              throw new FormatException("parsing", RiceKeyConstants.ERROR_TIME, text, e);
83          }
84      }
85  
86      /**
87       * Gets the date time service.
88       *
89       * @return the date time service
90       */
91      protected DateTimeService getDateTimeService() {
92          if (dateTimeService == null) {
93              dateTimeService = GlobalResourceLoader.getService(CoreConstants.Services.DATETIME_SERVICE);
94          }
95          return dateTimeService;
96      }
97  
98  }