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