Coverage Report - org.kuali.rice.krad.web.bind.UifCurrencyEditor
 
Classes in this File Line Coverage Branch Coverage Complexity
UifCurrencyEditor
0%
0/44
0%
0/18
5.5
 
 1  
 /*
 2  
  * Copyright 2011 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.apache.log4j.Logger;
 14  
 import org.kuali.rice.core.util.RiceKeyConstants;
 15  
 import org.kuali.rice.core.util.type.KualiDecimal;
 16  
 import org.kuali.rice.core.util.type.KualiInteger;
 17  
 import org.kuali.rice.core.web.format.FormatException;
 18  
 
 19  
 import java.beans.PropertyEditorSupport;
 20  
 import java.text.DecimalFormat;
 21  
 import java.text.NumberFormat;
 22  
 import java.text.ParseException;
 23  
 
 24  
 /**
 25  
  * This class is used to format
 26  
  * <code>org.kuali.rice.core.util.type.KualiDecimal</code> in the local
 27  
  * currency.
 28  
  * 
 29  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 30  
  */
 31  0
 public class UifCurrencyEditor extends PropertyEditorSupport {
 32  
 
 33  0
     private static Logger LOG = Logger.getLogger(UifCurrencyEditor.class);
 34  
 
 35  
     /**
 36  
      * This overridden method ...
 37  
      * 
 38  
      * @see java.beans.PropertyEditorSupport#getAsText()
 39  
      */
 40  
     @Override
 41  
     public String getAsText() {
 42  0
         Object obj = this.getValue();
 43  
 
 44  0
         LOG.debug("format '" + obj + "'");
 45  0
         if (obj == null)
 46  0
             return null;
 47  
 
 48  0
         NumberFormat formatter = getCurrencyInstanceUsingParseBigDecimal();
 49  0
         String string = null;
 50  
 
 51  
         try {
 52  0
             Number number = (Number) obj;
 53  0
             if (obj instanceof KualiInteger) {
 54  0
                 formatter.setMaximumFractionDigits(0);
 55  
             }
 56  0
             string = formatter.format(number.doubleValue());
 57  0
         } catch (IllegalArgumentException e) {
 58  0
             throw new FormatException("formatting", RiceKeyConstants.ERROR_CURRENCY, obj.toString(), e);
 59  0
         } catch (ClassCastException e) {
 60  0
             throw new FormatException("formatting", RiceKeyConstants.ERROR_CURRENCY, obj.toString(), e);
 61  0
         }
 62  
 
 63  0
         return string;
 64  
     }
 65  
 
 66  
     /**
 67  
      * retrieves a currency formatter instance and sets ParseBigDecimal to true
 68  
      * to fix [KULEDOCS-742]
 69  
      * 
 70  
      * @return CurrencyInstance
 71  
      */
 72  
     private NumberFormat getCurrencyInstanceUsingParseBigDecimal() {
 73  0
         NumberFormat formatter = NumberFormat.getCurrencyInstance();
 74  0
         if (formatter instanceof DecimalFormat) {
 75  0
             ((DecimalFormat) formatter).setParseBigDecimal(true);
 76  
         }
 77  0
         return formatter;
 78  
     }
 79  
 
 80  
     /**
 81  
      * This overridden method sets the property value by parsing a given String.
 82  
      * It uses the <code>convertToObject</code> method to make the code
 83  
      * available to sub classes.
 84  
      * 
 85  
      * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
 86  
      */
 87  
     @Override
 88  
     public void setAsText(String text) {
 89  0
         this.setValue(convertToObject(text));
 90  0
     }
 91  
 
 92  
     /**
 93  
      * Converts the string to a
 94  
      * <code>org.kuali.rice.core.util.type.KualiDecimal</code> object using the
 95  
      * local currency format.
 96  
      * 
 97  
      * @param text
 98  
      *            the text from the UI to convert
 99  
      * @return the <code>org.kuali.rice.core.util.type.KualiDecimal</code>
 100  
      *         object to be set on the bean
 101  
      */
 102  
     protected Object convertToObject(String text) {
 103  0
         KualiDecimal value = null;
 104  
 
 105  0
         LOG.debug("convertToObject '" + text + "'");
 106  
 
 107  0
         if (text != null) {
 108  0
             text = text.trim();
 109  0
             NumberFormat formatter = getCurrencyInstanceUsingParseBigDecimal();
 110  
             // Add the currency symbol suffix/prefix to the text to change to
 111  
             // correct format
 112  0
             if (formatter instanceof DecimalFormat) {
 113  0
                 String prefix = ((DecimalFormat) formatter).getPositivePrefix();
 114  0
                 String suffix = ((DecimalFormat) formatter).getPositiveSuffix();
 115  0
                 if (!prefix.equals("") && !text.startsWith(prefix)) {
 116  0
                     text = prefix.concat(text);
 117  
                 }
 118  0
                 if (!suffix.equals("") && !text.endsWith(suffix)) {
 119  0
                     text = text.concat(suffix);
 120  
                 }
 121  
             }
 122  
             try {
 123  0
                 Number parsedNumber = formatter.parse(text);
 124  0
                 value = new KualiDecimal(parsedNumber.toString());
 125  0
             } catch (NumberFormatException e) {
 126  0
                 throw new FormatException("parsing", RiceKeyConstants.ERROR_CURRENCY, text, e);
 127  0
             } catch (ParseException e) {
 128  0
                 throw new FormatException("parsing", RiceKeyConstants.ERROR_CURRENCY, text, e);
 129  0
             }
 130  
         }
 131  0
         return value;
 132  
     }
 133  
 
 134  
 }