001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.form.validator;
016    
017    import org.apache.tapestry.IMarkupWriter;
018    import org.apache.tapestry.IRequestCycle;
019    import org.apache.tapestry.form.FormComponentContributorContext;
020    import org.apache.tapestry.form.IFormComponent;
021    import org.apache.tapestry.form.ValidationMessages;
022    import org.apache.tapestry.form.translator.NumberTranslator;
023    import org.apache.tapestry.json.JSONLiteral;
024    import org.apache.tapestry.json.JSONObject;
025    import org.apache.tapestry.valid.ValidationConstants;
026    import org.apache.tapestry.valid.ValidationConstraint;
027    import org.apache.tapestry.valid.ValidationStrings;
028    import org.apache.tapestry.valid.ValidatorException;
029    
030    import java.text.DecimalFormat;
031    import java.text.DecimalFormatSymbols;
032    import java.util.Locale;
033    
034    /**
035     * Expects the object to be a number, and checks that the value not smaller than a specified value.
036     *
037     * @author Howard Lewis Ship
038     * @since 4.0
039     */
040    public class Min extends BaseValidator
041    {
042        private double _min;
043    
044        public Min()
045        {
046        }
047    
048        public Min(String initializer)
049        {
050            super(initializer);
051        }
052    
053        /**
054         * Does comparison based on the {@link Number#doubleValue()}.
055         */
056    
057        public void validate(IFormComponent field, ValidationMessages messages, Object object)
058                throws ValidatorException
059        {
060            Number value = (Number) object;
061    
062            if (_min > value.doubleValue())
063                throw new ValidatorException(buildMessage(messages, field), ValidationConstraint.TOO_SMALL);
064        }
065    
066        private String getStringValue(Locale locale, IFormComponent field)
067        {
068            String ret = null;
069            NumberTranslator translator = (NumberTranslator)super.getFieldTranslator(field, NumberTranslator.class);
070    
071            if (translator != null)
072                ret = translator.format(field, locale, new Double(_min));
073            else
074                ret = String.valueOf(_min);
075    
076            return ret;
077        }
078    
079        private String buildMessage(ValidationMessages messages, IFormComponent field)
080        {
081            return messages.formatValidationMessage(
082                    getMessage(),
083                    ValidationStrings.VALUE_TOO_SMALL,
084                    new Object[] {
085                            field.getDisplayName(), getStringValue(messages.getLocale(), field)
086                    });
087        }
088    
089        public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
090                                       FormComponentContributorContext context, IFormComponent field)
091        {
092            JSONObject profile = context.getProfile();
093    
094            if (!profile.has(ValidationConstants.CONSTRAINTS)) {
095                profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
096            }
097            JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
098    
099            context.addInitializationScript(field, "dojo.require(\"dojo.i18n.number\");");
100    
101            String minString = getStringValue(context.getLocale(), field);
102            String grouping = "";
103    
104            DecimalFormatSymbols symbols = null;
105            NumberTranslator translator = (NumberTranslator)super.getFieldTranslator(field, NumberTranslator.class);
106    
107            if (translator != null) {
108                DecimalFormat format = translator.getDecimalFormat(context.getLocale());
109    
110                if (format.isGroupingUsed()) {
111    
112                    grouping += ",separator:" + JSONObject.quote(format.getDecimalFormatSymbols().getGroupingSeparator());
113                    grouping += ",groupSize:" + format.getGroupingSize();
114                } else {
115    
116                    grouping += ",separator:\"\"";
117                }
118                
119                symbols = format.getDecimalFormatSymbols();
120            } else {
121    
122                symbols = new DecimalFormatSymbols(context.getLocale());
123            }
124    
125            accumulateProperty(cons, field.getClientId(),
126                               new JSONLiteral("[tapestry.form.validation.greaterThanOrEqual,"
127                                               + JSONObject.quote(minString)
128                                               + ",{"
129                                               + "decimal:" + JSONObject.quote(symbols.getDecimalSeparator())
130                                               + grouping
131                                               + "}]"));
132    
133            accumulateProfileProperty(field, profile, ValidationConstants.CONSTRAINTS, buildMessage(context, field));
134        }
135    
136        public void setMin(double min)
137        {
138            _min = min;
139        }
140    
141        public double getMin()
142        {
143            return _min;
144        }
145    }