001 /**
002 * Copyright 2005-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.krad.web.bind;
017
018 import org.apache.log4j.Logger;
019 import org.kuali.rice.core.api.util.RiceKeyConstants;
020 import org.kuali.rice.core.api.util.type.KualiDecimal;
021 import org.kuali.rice.core.api.util.type.KualiInteger;
022 import org.kuali.rice.core.web.format.FormatException;
023
024 import java.beans.PropertyEditorSupport;
025 import java.io.Serializable;
026 import java.text.DecimalFormat;
027 import java.text.NumberFormat;
028 import java.text.ParseException;
029
030 /**
031 * Class is used to format
032 * <code>org.kuali.rice.core.api.util.type.KualiDecimal</code> in the local
033 * currency
034 *
035 * @author Kuali Rice Team (rice.collab@kuali.org)
036 */
037 public class UifCurrencyEditor extends PropertyEditorSupport implements Serializable {
038 private static final long serialVersionUID = 6692868638156609014L;
039 private static Logger LOG = Logger.getLogger(UifCurrencyEditor.class);
040
041 /**
042 * This overridden method ...
043 *
044 * @see java.beans.PropertyEditorSupport#getAsText()
045 */
046 @Override
047 public String getAsText() {
048 Object obj = this.getValue();
049
050 LOG.debug("format '" + obj + "'");
051 if (obj == null)
052 return null;
053
054 NumberFormat formatter = getCurrencyInstanceUsingParseBigDecimal();
055 String string = null;
056
057 try {
058 Number number = (Number) obj;
059 if (obj instanceof KualiInteger) {
060 formatter.setMaximumFractionDigits(0);
061 }
062 string = formatter.format(number.doubleValue());
063 } catch (IllegalArgumentException e) {
064 throw new FormatException("formatting", RiceKeyConstants.ERROR_CURRENCY, obj.toString(), e);
065 } catch (ClassCastException e) {
066 throw new FormatException("formatting", RiceKeyConstants.ERROR_CURRENCY, obj.toString(), e);
067 }
068
069 return string;
070 }
071
072 /**
073 * retrieves a currency formatter instance and sets ParseBigDecimal to true
074 * to fix [KULEDOCS-742]
075 *
076 * @return CurrencyInstance
077 */
078 private NumberFormat getCurrencyInstanceUsingParseBigDecimal() {
079 NumberFormat formatter = NumberFormat.getCurrencyInstance();
080 if (formatter instanceof DecimalFormat) {
081 ((DecimalFormat) formatter).setParseBigDecimal(true);
082 }
083 return formatter;
084 }
085
086 /**
087 * This overridden method sets the property value by parsing a given String.
088 * It uses the <code>convertToObject</code> method to make the code
089 * available to sub classes.
090 *
091 * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
092 */
093 @Override
094 public void setAsText(String text) {
095 this.setValue(convertToObject(text));
096 }
097
098 /**
099 * 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 }