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 org.apache.fulcrum.intake.IntakeException;
023    import org.apache.fulcrum.intake.validator.IntegerValidator;
024    import org.apache.fulcrum.intake.xmlmodel.XmlField;
025    
026    /**
027     * Processor for int fields.
028     *
029     * @version $Id: IntegerField.java 663702 2008-06-05 19:01:36Z tv $
030     */
031    public class IntegerField
032            extends Field
033    {
034        /**
035         * Constructor.
036         *
037         * @param field xml field definition object
038         * @param group xml group definition object
039         * @throws IntakeException thrown by superclass
040         */
041        public IntegerField(XmlField field, Group group)
042                throws IntakeException
043        {
044            super(field, group);
045        }
046    
047        /**
048         * Sets the default value for an Integer Field
049         *
050         * @param prop Parameter for the default values
051         */
052        public void setDefaultValue(String prop)
053        {
054            defaultValue = null;
055    
056            if (prop == null)
057            {
058                return;
059            }
060    
061            defaultValue = new Integer(prop);
062        }
063    
064        /**
065         * Set the empty Value. This value is used if Intake
066         * maps a field to a parameter returned by the user and
067         * the corresponding field is either empty (empty string)
068         * or non-existent.
069         *
070         * @param prop The value to use if the field is empty.
071         */
072        public void setEmptyValue(String prop)
073        {
074            emptyValue = null;
075    
076            if (prop == null)
077            {
078                return;
079            }
080    
081            emptyValue = new Integer(prop);
082        }
083    
084        /**
085         * Provides access to emptyValue such that the value returned will be
086         * acceptable as an argument parameter to Method.invoke.  Subclasses
087         * that deal with primitive types should ensure that they return an
088         * appropriate value wrapped in the object wrapper class for the
089         * primitive type.
090         *
091         * @return the value to use when the field is empty or an Object that
092         * wraps the empty value for primitive types.
093         */
094        protected Object getSafeEmptyValue()
095        {
096            if (isMultiValued)
097            {
098                return new int[0];
099            }
100            else
101            {
102                return (null == getEmptyValue())
103                        ? new Integer(0) : getEmptyValue();
104            }
105        }
106    
107        /**
108         * A suitable validator.
109         *
110         * @return A suitable validator
111         */
112        protected String getDefaultValidator()
113        {
114            return IntegerValidator.class.getName();
115        }
116    
117        /**
118         * Sets the value of the field from data in the parser.
119         */
120        protected void doSetValue()
121        {
122            if (isMultiValued)
123            {
124                Integer[] inputs = parser.getIntObjects(getKey());
125                int[] values = new int[inputs.length];
126    
127                for (int i = 0; i < inputs.length; i++)
128                {
129                    values[i] = inputs[i] == null 
130                            ? ((Integer) getEmptyValue()).intValue() 
131                            : inputs[i].intValue();
132                }
133    
134                setTestValue(values);
135            }
136            else
137            {
138                setTestValue(parser.getIntObject(getKey(), (Integer)getEmptyValue()));
139            }
140        }
141    
142    }