| 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.core.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.KNSServiceLocatorInternal added | |
| 22 | ||
| 23 |  import java.sql.Date; | |
| 24 |  import java.text.ParseException; | |
| 25 |  import java.util.Calendar; | |
| 26 | ||
| 27 |  import org.kuali.rice.core.api.datetime.DateTimeService; | |
| 28 |  import org.kuali.rice.core.api.CoreConstants; | |
| 29 |  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; | |
| 30 |  import org.kuali.rice.core.util.RiceKeyConstants; | |
| 31 | ||
| 32 |  /** | |
| 33 |   * begin Kuali Foundation modification | |
| 34 |   * This class is used to format Date objects. | |
| 35 |   * end Kuali Foundation modification | |
| 36 |   */ | |
| 37 | 0 |  public class DateFormatter extends Formatter { | 
| 38 | private static final long serialVersionUID = 7612442662886603084L;  | |
| 39 | ||
| 40 | private transient DateTimeService dateTimeService;  | |
| 41 | ||
| 42 |      /** | |
| 43 |       * For a given user input date, this method returns the exact string the user entered after the last slash. This allows the | |
| 44 |       * formatter to distinguish between ambiguous values such as "/06" "/6" and "/0006" | |
| 45 |       * | |
| 46 |       * @param date | |
| 47 |       * @return | |
| 48 |       */ | |
| 49 |      private String verbatimYear(String date) { | |
| 50 | 0 |          String result = ""; | 
| 51 | ||
| 52 | 0 |          int pos = date.lastIndexOf("/"); | 
| 53 | 0 |          if (pos >= 0) { | 
| 54 | 0 |              result = date.substring(pos); | 
| 55 | }  | |
| 56 | ||
| 57 | 0 |          return result; | 
| 58 | }  | |
| 59 |      // end Kuali Foundation modification | |
| 60 | ||
| 61 | ||
| 62 |      /** | |
| 63 |       * Unformats its argument and return a java.util.Date instance initialized with the resulting string. | |
| 64 |       * | |
| 65 |       * @return a java.util.Date intialized with the provided string | |
| 66 |       */ | |
| 67 |      protected Object convertToObject(String target) { | |
| 68 |          // begin Kuali Foundation modification | |
| 69 |          try { | |
| 70 | 0 |              Date result = getDateTimeService().convertToSqlDate(target); | 
| 71 | 0 |              Calendar calendar = Calendar.getInstance(); | 
| 72 | 0 |              calendar.setTime(result); | 
| 73 | 0 |              if (calendar.get(Calendar.YEAR) < 1000 && verbatimYear(target).length() < 4) { | 
| 74 | 0 |                  throw new FormatException("illegal year format", RiceKeyConstants.ERROR_DATE, target); | 
| 75 | }  | |
| 76 | 0 |              return result; | 
| 77 | 0 |          } catch (ParseException e) { | 
| 78 | 0 |              throw new FormatException("parsing", RiceKeyConstants.ERROR_DATE, target, e); | 
| 79 | }  | |
| 80 |          // end Kuali Foundation modification | |
| 81 | }  | |
| 82 | ||
| 83 |      /** | |
| 84 |       * Returns a string representation of its argument, formatted as a date with the "MM/dd/yyyy" format. | |
| 85 |       * | |
| 86 |       * @return a formatted String | |
| 87 |       */ | |
| 88 |      public Object format(Object value) { | |
| 89 | 0 |          if (value == null) { | 
| 90 | 0 |              return null; | 
| 91 | }  | |
| 92 |          // begin Kuali Foundation modification | |
| 93 | 0 |          if ("".equals(value)) { | 
| 94 | 0 |              return null; | 
| 95 | }  | |
| 96 | 0 |          return getDateTimeService().toDateString((java.util.Date) value); | 
| 97 |          // end Kuali Foundation modification | |
| 98 | }  | |
| 99 | ||
| 100 | ||
| 101 |      /** | |
| 102 |       * This method is invoked to validate a date string using the KNS Service | |
| 103 |       * DateTimeService. | |
| 104 |       * | |
| 105 |       * @param dateString | |
| 106 |       * @return | |
| 107 |       */ | |
| 108 | public boolean validate(String dateString) {  | |
| 109 | 0 |          boolean isValid = false; | 
| 110 | ||
| 111 |          try { | |
| 112 | 0 |              getDateTimeService().convertToSqlTimestamp(dateString); | 
| 113 | 0 |              isValid = true; | 
| 114 | 0 |          } catch (Exception e) { | 
| 115 | ||
| 116 | 0 |          } | 
| 117 | ||
| 118 | 0 |          return isValid; | 
| 119 | ||
| 120 | }  | |
| 121 | ||
| 122 |      protected DateTimeService getDateTimeService() { | |
| 123 | 0 |              if (this.dateTimeService == null) { | 
| 124 | 0 |                      this.dateTimeService = GlobalResourceLoader.getService(CoreConstants.Services.DATETIME_SERVICE); | 
| 125 | }  | |
| 126 | 0 |              return this.dateTimeService; | 
| 127 | }  | |
| 128 | ||
| 129 | }  |