1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
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
43
44
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
61
62 @Override
63 public void setAsText(String text) throws IllegalArgumentException {
64 setValue(convertToObject(text));
65 }
66
67
68
69
70
71
72
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
88
89
90
91 protected DateTimeService getDateTimeService() {
92 if (dateTimeService == null) {
93 dateTimeService = GlobalResourceLoader.getService(CoreConstants.Services.DATETIME_SERVICE);
94 }
95 return dateTimeService;
96 }
97
98 }