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