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