1 /**
2 * Copyright 2005-2014 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/ecl2.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.kuali.rice.krad.web.bind;
17
18 import org.kuali.rice.core.api.util.RiceKeyConstants;
19 import org.kuali.rice.core.web.format.FormatException;
20
21 import java.io.Serializable;
22 import java.sql.Date;
23 import java.text.ParseException;
24 import java.util.Calendar;
25
26 /**
27 * PropertyEditor converts between date display strings and <code>java.util.Calendar</code> objects using the
28 * <code>org.kuali.rice.core.api.datetime.DateTimeService</code>.
29 *
30 * @author Kuali Rice Team (rice.collab@kuali.org)
31 */
32 public class UifCalendarEditor extends UifDateEditor implements Serializable {
33 private static final long serialVersionUID = 8123569337264797008L;
34
35 /**
36 * This overridden method uses the
37 * <code>org.kuali.rice.core.api.datetime.DateTimeService</code> to convert
38 * the calendar object to the display string.
39 */
40 @Override
41 public String getAsText() {
42 if (this.getValue() == null) {
43 return null;
44 }
45
46 if ("".equals(this.getValue())) {
47 return null;
48 }
49
50 return getDateTimeService().toDateString(new Date(((java.util.Calendar) this.getValue()).getTimeInMillis()));
51 }
52
53 /**
54 * Convert display text to <code>java.util.Calendar</code> object using the
55 * <code>org.kuali.rice.core.api.datetime.DateTimeService</code>.
56 *
57 * @param text the display text
58 * @return the <code>java.util.Calendar</code> object
59 * @throws IllegalArgumentException the illegal argument exception
60 */
61 protected Object convertToObject(String text) throws IllegalArgumentException {
62 try {
63 // Allow user to clear dates
64 if (text == null || text.equals("")) {
65 return null;
66 }
67
68 Date result = getDateTimeService().convertToSqlDate(text);
69 Calendar calendar = getDateTimeService().getCalendar(result);
70 calendar.setTime(result);
71
72 if (calendar.get(Calendar.YEAR) < 1000 && verbatimYear(text).length() < 4) {
73 throw new FormatException("illegal year format", RiceKeyConstants.ERROR_DATE, text);
74 }
75
76 return calendar;
77 } catch (ParseException e) {
78 throw new FormatException("parsing", RiceKeyConstants.ERROR_DATE, text, e);
79 }
80 }
81
82 }