View Javadoc

1   package org.apache.turbine.services.intake.model;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.math.BigDecimal;
23  
24  import org.apache.commons.lang.StringUtils;
25  
26  import org.apache.turbine.services.intake.IntakeException;
27  import org.apache.turbine.services.intake.validator.BigDecimalValidator;
28  import org.apache.turbine.services.intake.xmlmodel.XmlField;
29  
30  /***
31   * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
32   * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
33   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
34   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
35   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
36   * @version $Id: BigDecimalField.java 534527 2007-05-02 16:10:59Z tv $
37   */
38  public class BigDecimalField
39          extends AbstractNumberField
40  {
41      /***
42       * Constructor.
43       *
44       * @param field xml field definition object
45       * @param group xml group definition object
46       * @throws IntakeException thrown by superclass
47       */
48      public BigDecimalField(XmlField field, Group group)
49              throws IntakeException
50      {
51          super(field, group);
52      }
53  
54      /***
55       * Sets the default value for a BigDecimal field
56       *
57       * @param prop Parameter for the default values
58       */
59      public void setDefaultValue(String prop)
60      {
61          defaultValue = null;
62  
63          if (prop == null)
64          {
65              return;
66          }
67  
68          defaultValue = new BigDecimal(prop);
69      }
70  
71      /***
72       * Set the empty Value. This value is used if Intake
73       * maps a field to a parameter returned by the user and
74       * the corresponding field is either empty (empty string)
75       * or non-existant.
76       *
77       * @param prop The value to use if the field is empty.
78       */
79      public void setEmptyValue(String prop)
80      {
81          emptyValue = null;
82  
83          if (prop == null)
84          {
85              return;
86          }
87  
88          emptyValue = new BigDecimal(prop);
89      }
90  
91      /***
92       * A suitable validator.
93       *
94       * @return A suitable validator
95       */
96      protected String getDefaultValidator()
97      {
98          return BigDecimalValidator.class.getName();
99      }
100 
101     /***
102      * Sets the value of the field from data in the parser.
103      */
104     protected void doSetValue()
105     {
106         if (isMultiValued)
107         {
108             String[] inputs = parser.getStrings(getKey());
109             BigDecimal[] values = new BigDecimal[inputs.length];
110             for (int i = 0; i < inputs.length; i++)
111             {
112                 values[i] = StringUtils.isNotEmpty(inputs[i])
113                         ? new BigDecimal(canonicalizeDecimalInput(inputs[i])) : (BigDecimal) getEmptyValue();
114             }
115             setTestValue(values);
116         }
117         else
118         {
119             String val = parser.getString(getKey());
120             setTestValue(StringUtils.isNotEmpty(val) ? new BigDecimal(canonicalizeDecimalInput(val)) : (BigDecimal) getEmptyValue());
121         }
122     }
123 }