1 /*
2 * Copyright 2006-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
28 * <code>java.util.Calendar</code> objects using the
29 * <code>org.kuali.rice.core.api.datetime.DateTimeService</code>
30 *
31 * @author Kuali Rice Team (rice.collab@kuali.org)
32 */
33 public class UifCalendarEditor extends UifDateEditor implements Serializable {
34 private static final long serialVersionUID = 8123569337264797008L;
35
36 /**
37 * This overridden method uses the
38 * <code>org.kuali.rice.core.api.datetime.DateTimeService</code> to convert
39 * the calendar object to the display string.
40 *
41 * @see java.beans.PropertyEditorSupport#getAsText()
42 */
43 @Override
44 public String getAsText() {
45 if (this.getValue() == null) {
46 return null;
47 }
48 if ("".equals(this.getValue())) {
49 return null;
50 }
51
52 return getDateTimeService().toDateString(new Date(((java.util.Calendar) this.getValue()).getTimeInMillis()));
53 }
54
55 /**
56 * Convert display text to <code>java.util.Calendar</code> object using the
57 * <code>org.kuali.rice.core.api.datetime.DateTimeService</code>.
58 *
59 * @param text the display text
60 * @return the <code>java.util.Calendar</code> object
61 * @throws IllegalArgumentException the illegal argument exception
62 */
63 protected Object convertToObject(String text) throws IllegalArgumentException {
64 try {
65 // Allow user to clear dates
66 if (text == null || text.equals("")) {
67 return null;
68 }
69
70 Date result = getDateTimeService().convertToSqlDate(text);
71 Calendar calendar = getDateTimeService().getCalendar(result);
72 calendar.setTime(result);
73 if (calendar.get(Calendar.YEAR) < 1000 && verbatimYear(text).length() < 4) {
74 throw new FormatException("illegal year format", RiceKeyConstants.ERROR_DATE, text);
75 }
76 return calendar;
77 } catch (ParseException e) {
78 throw new FormatException("parsing", RiceKeyConstants.ERROR_DATE, text, e);
79 }
80 }
81
82 }