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.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
35
36
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
46
47
48
49
50
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
69
70
71
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
97
98
99
100
101
102
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
117
118
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 }