View Javadoc

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  public class UifCurrencyEditor extends PropertyEditorSupport {
37  
38      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          Object obj = this.getValue();
48  
49          LOG.debug("format '" + obj + "'");
50          if (obj == null)
51              return null;
52  
53          NumberFormat formatter = getCurrencyInstanceUsingParseBigDecimal();
54          String string = null;
55  
56          try {
57              Number number = (Number) obj;
58              if (obj instanceof KualiInteger) {
59                  formatter.setMaximumFractionDigits(0);
60              }
61              string = formatter.format(number.doubleValue());
62          } catch (IllegalArgumentException e) {
63              throw new FormatException("formatting", RiceKeyConstants.ERROR_CURRENCY, obj.toString(), e);
64          } catch (ClassCastException e) {
65              throw new FormatException("formatting", RiceKeyConstants.ERROR_CURRENCY, obj.toString(), e);
66          }
67  
68          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          NumberFormat formatter = NumberFormat.getCurrencyInstance();
79          if (formatter instanceof DecimalFormat) {
80              ((DecimalFormat) formatter).setParseBigDecimal(true);
81          }
82          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          this.setValue(convertToObject(text));
95      }
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         KualiDecimal value = null;
109 
110         LOG.debug("convertToObject '" + text + "'");
111 
112         if (text != null) {
113             text = text.trim();
114             NumberFormat formatter = getCurrencyInstanceUsingParseBigDecimal();
115             // Add the currency symbol suffix/prefix to the text to change to
116             // correct format
117             if (formatter instanceof DecimalFormat) {
118                 String prefix = ((DecimalFormat) formatter).getPositivePrefix();
119                 String suffix = ((DecimalFormat) formatter).getPositiveSuffix();
120                 if (!prefix.equals("") && !text.startsWith(prefix)) {
121                     text = prefix.concat(text);
122                 }
123                 if (!suffix.equals("") && !text.endsWith(suffix)) {
124                     text = text.concat(suffix);
125                 }
126             }
127             try {
128                 Number parsedNumber = formatter.parse(text);
129                 value = new KualiDecimal(parsedNumber.toString());
130             } catch (NumberFormatException e) {
131                 throw new FormatException("parsing", RiceKeyConstants.ERROR_CURRENCY, text, e);
132             } catch (ParseException e) {
133                 throw new FormatException("parsing", RiceKeyConstants.ERROR_CURRENCY, text, e);
134             }
135         }
136         return value;
137     }
138 
139 }