Coverage Report - org.kuali.rice.krad.web.bind.UifDateEditor
 
Classes in this File Line Coverage Branch Coverage Complexity
UifDateEditor
0%
0/24
0%
0/12
3.4
 
 1  
 /*
 2  
  * Copyright 2007 The Kuali Foundation Licensed under the Educational Community
 3  
  * License, Version 1.0 (the "License"); you may not use this file except in
 4  
  * compliance with the License. You may obtain a copy of the License at
 5  
  * http://www.opensource.org/licenses/ecl1.php Unless required by applicable law
 6  
  * or agreed to in writing, software distributed under the License is
 7  
  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 8  
  * KIND, either express or implied. See the License for the specific language
 9  
  * governing permissions and limitations under the License.
 10  
  */
 11  
 package org.kuali.rice.krad.web.bind;
 12  
 
 13  
 import org.kuali.rice.core.api.CoreConstants;
 14  
 import org.kuali.rice.core.api.datetime.DateTimeService;
 15  
 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
 16  
 import org.kuali.rice.core.util.RiceKeyConstants;
 17  
 import org.kuali.rice.core.web.format.FormatException;
 18  
 
 19  
 import java.beans.PropertyEditorSupport;
 20  
 import java.sql.Date;
 21  
 import java.text.ParseException;
 22  
 import java.util.Calendar;
 23  
 
 24  
 /**
 25  
  * This PropertyEditor converts between date display strings and
 26  
  * <code>java.sql.Date</code> objects.
 27  
  * 
 28  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 29  
  */
 30  0
 public class UifDateEditor extends PropertyEditorSupport {
 31  
 
 32  
     /** The date time service. */
 33  
     private transient DateTimeService dateTimeService;
 34  
 
 35  
     /**
 36  
      * This overridden method uses the
 37  
      * <code>org.kuali.rice.core.api.datetime.DateTimeService</code> to convert
 38  
      * the date object to the display string.
 39  
      * 
 40  
      * @see java.beans.PropertyEditorSupport#getAsText()
 41  
      */
 42  
     @Override
 43  
     public String getAsText() {
 44  0
         if (this.getValue() == null) {
 45  0
             return null;
 46  
         }
 47  0
         if ("".equals(this.getValue())) {
 48  0
             return null;
 49  
         }
 50  0
         return getDateTimeService().toDateString((java.util.Date) this.getValue());
 51  
     }
 52  
 
 53  
     /**
 54  
      * Gets the date time service.
 55  
      * 
 56  
      * @return the date time service
 57  
      */
 58  
     protected DateTimeService getDateTimeService() {
 59  0
         if (this.dateTimeService == null) {
 60  0
             this.dateTimeService = GlobalResourceLoader.getService(CoreConstants.Services.DATETIME_SERVICE);
 61  
         }
 62  0
         return this.dateTimeService;
 63  
     }
 64  
 
 65  
     /**
 66  
      * This overridden method converts the display string to a
 67  
      * <code>java.sql.Date</code> object using the
 68  
      * <code>org.kuali.rice.core.api.datetime.DateTimeService</code>.
 69  
      * 
 70  
      * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
 71  
      */
 72  
     @Override
 73  
     public void setAsText(String text) throws IllegalArgumentException {
 74  0
         this.setValue(convertToObject(text));
 75  0
     }
 76  
 
 77  
     /**
 78  
      * Convert display text to <code>java.sql.Date</code> object using the
 79  
      * <code>org.kuali.rice.core.api.datetime.DateTimeService</code>.
 80  
      * 
 81  
      * @param text
 82  
      *            the display text
 83  
      * @return the <code>java.sql.Date</code> object
 84  
      * @throws IllegalArgumentException
 85  
      *             the illegal argument exception
 86  
      */
 87  
     protected Object convertToObject(String text) throws IllegalArgumentException {
 88  
         try {
 89  0
             Date result = getDateTimeService().convertToSqlDate(text);
 90  0
             Calendar calendar = Calendar.getInstance();
 91  0
             calendar.setTime(result);
 92  0
             if (calendar.get(Calendar.YEAR) < 1000 && verbatimYear(text).length() < 4) {
 93  0
                 throw new FormatException("illegal year format", RiceKeyConstants.ERROR_DATE, text);
 94  
             }
 95  0
             return result;
 96  0
         } catch (ParseException e) {
 97  0
             throw new FormatException("parsing", RiceKeyConstants.ERROR_DATE, text, e);
 98  
         }
 99  
     }
 100  
 
 101  
     /**
 102  
      * For a given user input date, this method returns the exact string the
 103  
      * user entered after the last slash. This allows the formatter to
 104  
      * distinguish between ambiguous values such as "/06" "/6" and "/0006"
 105  
      * 
 106  
      * @param date
 107  
      * @return
 108  
      */
 109  
     private String verbatimYear(String date) {
 110  0
         String result = "";
 111  
 
 112  0
         int pos = date.lastIndexOf("/");
 113  0
         if (pos >= 0) {
 114  0
             result = date.substring(pos);
 115  
         }
 116  
 
 117  0
         return result;
 118  
     }
 119  
 
 120  
 }