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.torque.om.NumberKey;
027:
028: import org.apache.turbine.services.intake.IntakeException;
029: import org.apache.turbine.services.intake.validator.NumberKeyValidator;
030: import org.apache.turbine.services.intake.xmlmodel.XmlField;
031:
032: /**
033: * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
034: * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
035: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
036: * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
037: * @version $Id: NumberKeyField.java 534527 2007-05-02 16:10:59Z tv $
038: */
039: public class NumberKeyField extends BigDecimalField {
040:
041: /**
042: * Constructor.
043: *
044: * @param field xml field definition object
045: * @param group xml group definition object
046: * @throws IntakeException thrown by superclass
047: */
048: public NumberKeyField(XmlField field, Group group)
049: throws IntakeException {
050: super (field, group);
051: }
052:
053: /**
054: * Sets the default value for a NumberKey field
055: *
056: * @param prop Parameter for the default values
057: */
058: public void setDefaultValue(String prop) {
059: if (prop == null) {
060: return;
061: }
062:
063: defaultValue = new NumberKey(prop);
064: }
065:
066: /**
067: * A suitable validator.
068: *
069: * @return A suitable validator
070: */
071: protected String getDefaultValidator() {
072: return NumberKeyValidator.class.getName();
073: }
074:
075: /**
076: * Sets the value of the field from data in the parser.
077: */
078: protected void doSetValue() {
079: if (isMultiValued) {
080: String[] inputs = parser.getStrings(getKey());
081: NumberKey[] values = new NumberKey[inputs.length];
082: for (int i = 0; i < inputs.length; i++) {
083: if (StringUtils.isNotEmpty(inputs[i])) {
084: values[i] = new NumberKey(
085: canonicalizeDecimalInput(inputs[i]));
086: } else {
087: values[i] = null;
088: }
089: }
090: setTestValue(values);
091: } else {
092: String val = parser.getString(getKey());
093: if (StringUtils.isNotEmpty(val)) {
094: BigDecimal bd = new BigDecimal(
095: canonicalizeDecimalInput(val));
096: setTestValue(new NumberKey(bd));
097: } else {
098: setTestValue(null);
099: }
100: }
101: }
102: }
|