| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| UifDateEditor |
|
| 3.4;3.4 |
| 1 | /** | |
| 2 | * Copyright 2005-2011 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.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 | * This PropertyEditor converts between date display strings and | |
| 31 | * <code>java.sql.Date</code> objects. | |
| 32 | * | |
| 33 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
| 34 | */ | |
| 35 | 0 | public class UifDateEditor extends PropertyEditorSupport { |
| 36 | ||
| 37 | /** The date time service. */ | |
| 38 | private transient DateTimeService dateTimeService; | |
| 39 | ||
| 40 | /** | |
| 41 | * This overridden method uses the | |
| 42 | * <code>org.kuali.rice.core.api.datetime.DateTimeService</code> to convert | |
| 43 | * the date object to the display string. | |
| 44 | * | |
| 45 | * @see java.beans.PropertyEditorSupport#getAsText() | |
| 46 | */ | |
| 47 | @Override | |
| 48 | public String getAsText() { | |
| 49 | 0 | if (this.getValue() == null) { |
| 50 | 0 | return null; |
| 51 | } | |
| 52 | 0 | if ("".equals(this.getValue())) { |
| 53 | 0 | return null; |
| 54 | } | |
| 55 | 0 | return getDateTimeService().toDateString((java.util.Date) this.getValue()); |
| 56 | } | |
| 57 | ||
| 58 | /** | |
| 59 | * Gets the date time service. | |
| 60 | * | |
| 61 | * @return the date time service | |
| 62 | */ | |
| 63 | protected DateTimeService getDateTimeService() { | |
| 64 | 0 | if (this.dateTimeService == null) { |
| 65 | 0 | this.dateTimeService = GlobalResourceLoader.getService(CoreConstants.Services.DATETIME_SERVICE); |
| 66 | } | |
| 67 | 0 | return this.dateTimeService; |
| 68 | } | |
| 69 | ||
| 70 | /** | |
| 71 | * This overridden method converts the display string to a | |
| 72 | * <code>java.sql.Date</code> object using the | |
| 73 | * <code>org.kuali.rice.core.api.datetime.DateTimeService</code>. | |
| 74 | * | |
| 75 | * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String) | |
| 76 | */ | |
| 77 | @Override | |
| 78 | public void setAsText(String text) throws IllegalArgumentException { | |
| 79 | 0 | this.setValue(convertToObject(text)); |
| 80 | 0 | } |
| 81 | ||
| 82 | /** | |
| 83 | * Convert display text to <code>java.sql.Date</code> object using the | |
| 84 | * <code>org.kuali.rice.core.api.datetime.DateTimeService</code>. | |
| 85 | * | |
| 86 | * @param text | |
| 87 | * the display text | |
| 88 | * @return the <code>java.sql.Date</code> object | |
| 89 | * @throws IllegalArgumentException | |
| 90 | * the illegal argument exception | |
| 91 | */ | |
| 92 | protected Object convertToObject(String text) throws IllegalArgumentException { | |
| 93 | try { | |
| 94 | 0 | Date result = getDateTimeService().convertToSqlDate(text); |
| 95 | 0 | Calendar calendar = Calendar.getInstance(); |
| 96 | 0 | calendar.setTime(result); |
| 97 | 0 | if (calendar.get(Calendar.YEAR) < 1000 && verbatimYear(text).length() < 4) { |
| 98 | 0 | throw new FormatException("illegal year format", RiceKeyConstants.ERROR_DATE, text); |
| 99 | } | |
| 100 | 0 | return result; |
| 101 | 0 | } catch (ParseException e) { |
| 102 | 0 | throw new FormatException("parsing", RiceKeyConstants.ERROR_DATE, text, e); |
| 103 | } | |
| 104 | } | |
| 105 | ||
| 106 | /** | |
| 107 | * For a given user input date, this method returns the exact string the | |
| 108 | * user entered after the last slash. This allows the formatter to | |
| 109 | * distinguish between ambiguous values such as "/06" "/6" and "/0006" | |
| 110 | * | |
| 111 | * @param date | |
| 112 | * @return | |
| 113 | */ | |
| 114 | private String verbatimYear(String date) { | |
| 115 | 0 | String result = ""; |
| 116 | ||
| 117 | 0 | int pos = date.lastIndexOf("/"); |
| 118 | 0 | if (pos >= 0) { |
| 119 | 0 | result = date.substring(pos); |
| 120 | } | |
| 121 | ||
| 122 | 0 | return result; |
| 123 | } | |
| 124 | ||
| 125 | } |