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.turbine.services.intake.IntakeException;
25  import org.apache.turbine.services.intake.validator.BigDecimalValidator;
26  import org.apache.turbine.services.intake.xmlmodel.XmlField;
27  
28  /***
29   * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
30   * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
31   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
32   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
33   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
34   * @version $Id: BigDecimalField.java 646288 2008-04-09 11:51:16Z tv $
35   */
36  public class BigDecimalField
37          extends Field
38  {
39      /***
40       * Constructor.
41       *
42       * @param field xml field definition object
43       * @param group xml group definition object
44       * @throws IntakeException thrown by superclass
45       */
46      public BigDecimalField(XmlField field, Group group)
47              throws IntakeException
48      {
49          super(field, group);
50      }
51  
52      /***
53       * Sets the default value for a BigDecimal field
54       *
55       * @param prop Parameter for the default values
56       */
57      public void setDefaultValue(String prop)
58      {
59          defaultValue = null;
60  
61          if (prop == null)
62          {
63              return;
64          }
65  
66          defaultValue = new BigDecimal(prop);
67      }
68  
69      /***
70       * Set the empty Value. This value is used if Intake
71       * maps a field to a parameter returned by the user and
72       * the corresponding field is either empty (empty string)
73       * or non-existant.
74       *
75       * @param prop The value to use if the field is empty.
76       */
77      public void setEmptyValue(String prop)
78      {
79          emptyValue = null;
80  
81          if (prop == null)
82          {
83              return;
84          }
85  
86          emptyValue = new BigDecimal(prop);
87      }
88  
89      /***
90       * A suitable validator.
91       *
92       * @return A suitable validator
93       */
94      protected String getDefaultValidator()
95      {
96          return BigDecimalValidator.class.getName();
97      }
98  
99      /***
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 }