001: package org.apache.turbine.services.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.commons.lang.StringUtils;
025:
026: import org.apache.turbine.services.intake.IntakeException;
027: import org.apache.turbine.services.intake.validator.BigDecimalValidator;
028: import org.apache.turbine.services.intake.xmlmodel.XmlField;
029:
030: /**
031: * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
032: * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
033: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
034: * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
035: * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
036: * @version $Id: BigDecimalField.java 534527 2007-05-02 16:10:59Z tv $
037: */
038: public class BigDecimalField extends AbstractNumberField {
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: super (field, group);
049: }
050:
051: /**
052: * Sets the default value for a BigDecimal field
053: *
054: * @param prop Parameter for the default values
055: */
056: public void setDefaultValue(String prop) {
057: defaultValue = null;
058:
059: if (prop == null) {
060: return;
061: }
062:
063: defaultValue = new BigDecimal(prop);
064: }
065:
066: /**
067: * Set the empty Value. This value is used if Intake
068: * maps a field to a parameter returned by the user and
069: * the corresponding field is either empty (empty string)
070: * or non-existant.
071: *
072: * @param prop The value to use if the field is empty.
073: */
074: public void setEmptyValue(String prop) {
075: emptyValue = null;
076:
077: if (prop == null) {
078: return;
079: }
080:
081: emptyValue = new BigDecimal(prop);
082: }
083:
084: /**
085: * A suitable validator.
086: *
087: * @return A suitable validator
088: */
089: protected String getDefaultValidator() {
090: return BigDecimalValidator.class.getName();
091: }
092:
093: /**
094: * Sets the value of the field from data in the parser.
095: */
096: protected void doSetValue() {
097: if (isMultiValued) {
098: String[] inputs = parser.getStrings(getKey());
099: BigDecimal[] values = new BigDecimal[inputs.length];
100: for (int i = 0; i < inputs.length; i++) {
101: values[i] = StringUtils.isNotEmpty(inputs[i]) ? new BigDecimal(
102: canonicalizeDecimalInput(inputs[i]))
103: : (BigDecimal) getEmptyValue();
104: }
105: setTestValue(values);
106: } else {
107: String val = parser.getString(getKey());
108: setTestValue(StringUtils.isNotEmpty(val) ? new BigDecimal(
109: canonicalizeDecimalInput(val))
110: : (BigDecimal) getEmptyValue());
111: }
112: }
113: }
|