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