1
2
3
4
5
6
7
8
9
10
11 package org.kuali.rice.krad.web.bind;
12
13 import org.kuali.rice.core.api.CoreConstants;
14 import org.kuali.rice.core.api.datetime.DateTimeService;
15 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
16 import org.kuali.rice.core.util.RiceKeyConstants;
17 import org.kuali.rice.core.web.format.FormatException;
18
19 import java.beans.PropertyEditorSupport;
20 import java.sql.Date;
21 import java.text.ParseException;
22 import java.util.Calendar;
23
24
25
26
27
28
29
30 public class UifDateEditor extends PropertyEditorSupport {
31
32
33 private transient DateTimeService dateTimeService;
34
35
36
37
38
39
40
41
42 @Override
43 public String getAsText() {
44 if (this.getValue() == null) {
45 return null;
46 }
47 if ("".equals(this.getValue())) {
48 return null;
49 }
50 return getDateTimeService().toDateString((java.util.Date) this.getValue());
51 }
52
53
54
55
56
57
58 protected DateTimeService getDateTimeService() {
59 if (this.dateTimeService == null) {
60 this.dateTimeService = GlobalResourceLoader.getService(CoreConstants.Services.DATETIME_SERVICE);
61 }
62 return this.dateTimeService;
63 }
64
65
66
67
68
69
70
71
72 @Override
73 public void setAsText(String text) throws IllegalArgumentException {
74 this.setValue(convertToObject(text));
75 }
76
77
78
79
80
81
82
83
84
85
86
87 protected Object convertToObject(String text) throws IllegalArgumentException {
88 try {
89 Date result = getDateTimeService().convertToSqlDate(text);
90 Calendar calendar = Calendar.getInstance();
91 calendar.setTime(result);
92 if (calendar.get(Calendar.YEAR) < 1000 && verbatimYear(text).length() < 4) {
93 throw new FormatException("illegal year format", RiceKeyConstants.ERROR_DATE, text);
94 }
95 return result;
96 } catch (ParseException e) {
97 throw new FormatException("parsing", RiceKeyConstants.ERROR_DATE, text, e);
98 }
99 }
100
101
102
103
104
105
106
107
108
109 private String verbatimYear(String date) {
110 String result = "";
111
112 int pos = date.lastIndexOf("/");
113 if (pos >= 0) {
114 result = date.substring(pos);
115 }
116
117 return result;
118 }
119
120 }