| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DateFormatter |
|
| 3.6;3.6 |
| 1 | /* | |
| 2 | * Copyright 2004 Jonathan M. Lehr | |
| 3 | * | |
| 4 | * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0 | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" | |
| 11 | * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language | |
| 12 | * governing permissions and limitations under the License. | |
| 13 | * | |
| 14 | * MODIFIED BY THE KUALI FOUNDATION | |
| 15 | */ | |
| 16 | // begin Kuali Foundation modification | |
| 17 | package org.kuali.rice.kns.web.format; | |
| 18 | // end Kuali Foundation modification | |
| 19 | ||
| 20 | // begin Kuali Foundation modification | |
| 21 | // import order changed, and java.util.Calendar, org.kuali.KeyConstants and org.kuali.rice.KNSServiceLocator added | |
| 22 | import java.sql.Date; | |
| 23 | import java.text.ParseException; | |
| 24 | import java.util.Calendar; | |
| 25 | ||
| 26 | import org.kuali.rice.kns.service.DateTimeService; | |
| 27 | import org.kuali.rice.kns.service.KNSServiceLocator; | |
| 28 | import org.kuali.rice.kns.util.RiceKeyConstants; | |
| 29 | ||
| 30 | /** | |
| 31 | * begin Kuali Foundation modification | |
| 32 | * This class is used to format Date objects. | |
| 33 | * end Kuali Foundation modification | |
| 34 | */ | |
| 35 | 0 | public class DateFormatter extends Formatter { |
| 36 | // begin Kuali Foundation modification | |
| 37 | // serialVersionUID changed from 1L | |
| 38 | private static final long serialVersionUID = 7612442662886603084L; | |
| 39 | ||
| 40 | ||
| 41 | private static DateTimeService dateTimeService; | |
| 42 | // end Kuali Foundation modification | |
| 43 | ||
| 44 | // begin Kuali Foundation modification | |
| 45 | // static variables DATE_ERROR_KEY and PARSE_MSG removed | |
| 46 | // method public String getErrorKey() removed | |
| 47 | // end Kuali Foundation modification | |
| 48 | ||
| 49 | // begin Kuali Foundation modification | |
| 50 | // added this method | |
| 51 | /** | |
| 52 | * | |
| 53 | * For a given user input date, this method returns the exact string the user entered after the last slash. This allows the | |
| 54 | * formatter to distinguish between ambiguous values such as "/06" "/6" and "/0006" | |
| 55 | * | |
| 56 | * @param date | |
| 57 | * @return | |
| 58 | */ | |
| 59 | private String verbatimYear(String date) { | |
| 60 | 0 | String result = ""; |
| 61 | ||
| 62 | 0 | int pos = date.lastIndexOf("/"); |
| 63 | 0 | if (pos >= 0) { |
| 64 | 0 | result = date.substring(pos); |
| 65 | } | |
| 66 | ||
| 67 | 0 | return result; |
| 68 | } | |
| 69 | // end Kuali Foundation modification | |
| 70 | ||
| 71 | ||
| 72 | /** | |
| 73 | * Unformats its argument and return a java.util.Date instance initialized with the resulting string. | |
| 74 | * | |
| 75 | * @return a java.util.Date intialized with the provided string | |
| 76 | */ | |
| 77 | protected Object convertToObject(String target) { | |
| 78 | // begin Kuali Foundation modification | |
| 79 | try { | |
| 80 | 0 | Date result = getDateTimeService().convertToSqlDate(target); |
| 81 | 0 | Calendar calendar = Calendar.getInstance(); |
| 82 | 0 | calendar.setTime(result); |
| 83 | 0 | if (calendar.get(Calendar.YEAR) < 1000 && verbatimYear(target).length() < 4) { |
| 84 | 0 | throw new FormatException("illegal year format", RiceKeyConstants.ERROR_DATE, target); |
| 85 | } | |
| 86 | 0 | return result; |
| 87 | } | |
| 88 | 0 | catch (ParseException e) { |
| 89 | 0 | throw new FormatException("parsing", RiceKeyConstants.ERROR_DATE, target, e); |
| 90 | } | |
| 91 | // end Kuali Foundation modification | |
| 92 | } | |
| 93 | ||
| 94 | /** | |
| 95 | * Returns a string representation of its argument, formatted as a date with the "MM/dd/yyyy" format. | |
| 96 | * | |
| 97 | * @return a formatted String | |
| 98 | */ | |
| 99 | public Object format(Object value) { | |
| 100 | 0 | if (value == null) { |
| 101 | 0 | return null; |
| 102 | } | |
| 103 | // begin Kuali Foundation modification | |
| 104 | 0 | if ("".equals(value)) { |
| 105 | 0 | return null; |
| 106 | } | |
| 107 | 0 | return getDateTimeService().toDateString((java.util.Date)value); |
| 108 | // end Kuali Foundation modification | |
| 109 | } | |
| 110 | ||
| 111 | ||
| 112 | public static DateTimeService getDateTimeService() { | |
| 113 | 0 | if ( dateTimeService == null ) { |
| 114 | 0 | dateTimeService = KNSServiceLocator.getDateTimeService(); |
| 115 | } | |
| 116 | 0 | return dateTimeService; |
| 117 | } | |
| 118 | ||
| 119 | /** | |
| 120 | * This method is invoked to validate a date string using the KNS Service | |
| 121 | * DateTimeService. | |
| 122 | * | |
| 123 | * @param dateString | |
| 124 | * @return | |
| 125 | */ | |
| 126 | public boolean validate(String dateString) { | |
| 127 | 0 | boolean isValid=false; |
| 128 | ||
| 129 | 0 | DateTimeService service=getDateTimeService(); |
| 130 | try { | |
| 131 | 0 | service.convertToSqlTimestamp(dateString); |
| 132 | 0 | isValid=true; |
| 133 | 0 | } catch (Exception e) { |
| 134 | ||
| 135 | 0 | } |
| 136 | ||
| 137 | 0 | return isValid; |
| 138 | ||
| 139 | } | |
| 140 | ||
| 141 | } |