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