001    package org.apache.fulcrum.intake.model;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.math.BigDecimal;
023    
024    import org.apache.fulcrum.intake.IntakeException;
025    import org.apache.fulcrum.intake.validator.BigDecimalValidator;
026    import org.apache.fulcrum.intake.xmlmodel.XmlField;
027    
028    /**
029     * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
030     * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
031     * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
032     * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
033     * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
034     * @version $Id: BigDecimalField.java 663702 2008-06-05 19:01:36Z tv $
035     */
036    public class BigDecimalField
037            extends Field
038    {
039        /**
040         * Constructor.
041         *
042         * @param field xml field definition object
043         * @param group xml group definition object
044         * @throws IntakeException thrown by superclass
045         */
046        public BigDecimalField(XmlField field, Group group)
047                throws IntakeException
048        {
049            super(field, group);
050        }
051    
052        /**
053         * Sets the default value for a BigDecimal field
054         *
055         * @param prop Parameter for the default values
056         */
057        public void setDefaultValue(String prop)
058        {
059            defaultValue = null;
060    
061            if (prop == null)
062            {
063                return;
064            }
065    
066            defaultValue = new BigDecimal(prop);
067        }
068    
069        /**
070         * Set the empty Value. This value is used if Intake
071         * maps a field to a parameter returned by the user and
072         * the corresponding field is either empty (empty string)
073         * or non-existant.
074         *
075         * @param prop The value to use if the field is empty.
076         */
077        public void setEmptyValue(String prop)
078        {
079            emptyValue = null;
080    
081            if (prop == null)
082            {
083                return;
084            }
085    
086            emptyValue = new BigDecimal(prop);
087        }
088    
089        /**
090         * A suitable validator.
091         *
092         * @return A suitable validator
093         */
094        protected String getDefaultValidator()
095        {
096            return BigDecimalValidator.class.getName();
097        }
098    
099        /**
100         * Sets the value of the field from data in the parser.
101         */
102        protected void doSetValue()
103        {
104            if (isMultiValued)
105            {
106                BigDecimal[] values = parser.getBigDecimals(getKey());
107    
108                for (int i = 0; i < values.length; i++)
109                {
110                    if (values[i] == null)
111                    {
112                        values[i] = (BigDecimal) getEmptyValue();
113                    }
114                }
115    
116                setTestValue(values);
117            }
118            else
119            {
120                setTestValue(parser.getBigDecimal(getKey(), (BigDecimal)getEmptyValue()));
121            }
122        }
123    }