Coverage Report - org.apache.tapestry.form.validator.MaxDate
 
Classes in this File Line Coverage Branch Coverage Complexity
MaxDate
0%
0/24
0%
0/14
2
 
 1  
 // Copyright 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.form.validator;
 16  
 
 17  
 import java.util.Date;
 18  
 
 19  
 import org.apache.tapestry.IMarkupWriter;
 20  
 import org.apache.tapestry.IRequestCycle;
 21  
 import org.apache.tapestry.form.FormComponentContributorContext;
 22  
 import org.apache.tapestry.form.IFormComponent;
 23  
 import org.apache.tapestry.form.ValidationMessages;
 24  
 import org.apache.tapestry.form.translator.DateTranslator;
 25  
 import org.apache.tapestry.json.JSONLiteral;
 26  
 import org.apache.tapestry.json.JSONObject;
 27  
 import org.apache.tapestry.valid.ValidationConstants;
 28  
 import org.apache.tapestry.valid.ValidationConstraint;
 29  
 import org.apache.tapestry.valid.ValidationStrings;
 30  
 import org.apache.tapestry.valid.ValidatorException;
 31  
 
 32  
 /**
 33  
  * Validates that the object, a Date, is not after a set maximum.
 34  
  * 
 35  
  * @author Howard Lewis Ship
 36  
  * @since 4.0
 37  
  */
 38  
 public class MaxDate extends BaseValidator
 39  
 {
 40  
     private Date _maxDate;
 41  
 
 42  
     public MaxDate()
 43  0
     {
 44  0
     }
 45  
 
 46  
     public MaxDate(String initializer)
 47  
     {
 48  0
         super(initializer);
 49  0
     }
 50  
 
 51  
     public void validate(IFormComponent field, ValidationMessages messages, Object object)
 52  
             throws ValidatorException
 53  
     {
 54  0
         Date date = (Date) object;
 55  0
         DateTranslator translator = (DateTranslator) getFieldTranslator(field, DateTranslator.class);
 56  
         
 57  0
         if (date.after(_maxDate))
 58  0
             throw new ValidatorException(buildMessage(messages, field, translator),
 59  
                     ValidationConstraint.TOO_LARGE);
 60  
         
 61  0
     }
 62  
     
 63  
     private String buildMessage(ValidationMessages messages, IFormComponent field, 
 64  
             DateTranslator translator)
 65  
     {
 66  0
         return messages.formatValidationMessage(
 67  
                 getMessage(),
 68  
                 ValidationStrings.DATE_TOO_LATE,
 69  
                 new Object[]
 70  
                 { field.getDisplayName(), 
 71  
                     (translator != null) ? 
 72  
                             translator.format(field, messages.getLocale(), _maxDate)
 73  
                             : _maxDate.toString()});
 74  
     }
 75  
     
 76  
     public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
 77  
             FormComponentContributorContext context, IFormComponent field)
 78  
     {
 79  
         // TODO: This is a little hacky, but validators need to be able to cooperate
 80  
         // with translators during client side validation as well
 81  0
         DateTranslator translator = (DateTranslator) getFieldTranslator(field, DateTranslator.class);
 82  0
         if (translator == null)
 83  0
             return;
 84  
         
 85  0
         JSONObject profile = context.getProfile();
 86  
         
 87  0
         context.addInitializationScript(field, "dojo.require(\"tapestry.form.datetime\");");
 88  
         
 89  0
         if (!profile.has(ValidationConstants.CONSTRAINTS)) {
 90  0
             profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
 91  
         }
 92  0
         JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
 93  
         
 94  0
         accumulateProperty(cons, field.getClientId(), 
 95  
                 new JSONLiteral("[tapestry.form.datetime.isValidDate,{"
 96  
                         + "max:" 
 97  
                         + JSONObject.quote(translator.format(field, context.getLocale(), _maxDate))
 98  
                         + ","
 99  
                         + "datePattern:" 
 100  
                         + JSONObject.quote(translator.getPattern())
 101  
                         + (translator.isLenient() ? "" : ",strict:true")
 102  
                         + "}]"));
 103  
         
 104  0
         accumulateProfileProperty(field, profile, 
 105  
                 ValidationConstants.CONSTRAINTS, buildMessage(context, field, translator));
 106  0
     }
 107  
     
 108  
     public void setMaxDate(Date minDate)
 109  
     {
 110  0
         _maxDate = minDate;
 111  0
     }
 112  
 
 113  
     public Date getMaxDate()
 114  
     {
 115  0
         return _maxDate;
 116  
     }
 117  
 }