Coverage Report - org.apache.tapestry.form.validator.MinDate
 
Classes in this File Line Coverage Branch Coverage Complexity
MinDate
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  
  * Expects the value to be a {@link Date}, and constrains the date to follow a particular date.
 34  
  * 
 35  
  * @author Howard M. Lewis Ship
 36  
  * @since 4.0
 37  
  */
 38  
 
 39  
 public class MinDate extends BaseValidator
 40  
 {
 41  
     private Date _minDate;
 42  
 
 43  
     public MinDate()
 44  0
     {
 45  0
     }
 46  
 
 47  
     public MinDate(String initializer)
 48  
     {
 49  0
         super(initializer);
 50  0
     }
 51  
     
 52  
     public void setMinDate(Date minDate)
 53  
     {
 54  0
         _minDate = minDate;
 55  0
     }
 56  
     
 57  
     public Date getMinDate()
 58  
     {
 59  0
         return _minDate;
 60  
     }
 61  
     
 62  
     public void validate(IFormComponent field, ValidationMessages messages, Object object)
 63  
     throws ValidatorException
 64  
     {
 65  0
         Date date = (Date) object;
 66  0
         DateTranslator translator = (DateTranslator) getFieldTranslator(field, DateTranslator.class);
 67  
         
 68  0
         if (date.before(_minDate))
 69  0
             throw new ValidatorException(buildMessage(messages, field, translator), ValidationConstraint.TOO_SMALL);
 70  
 
 71  0
     }
 72  
     
 73  
     private String buildMessage(ValidationMessages messages, IFormComponent field, 
 74  
             DateTranslator translator)
 75  
     {
 76  0
         return messages.formatValidationMessage(
 77  
                 getMessage(),
 78  
                 ValidationStrings.DATE_TOO_EARLY,
 79  
                 new Object[] { field.getDisplayName(), 
 80  
                     (translator != null) ? 
 81  
                             translator.format(field, messages.getLocale(), _minDate) : _minDate.toString()});
 82  
     }
 83  
     
 84  
     public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
 85  
             FormComponentContributorContext context, IFormComponent field)
 86  
     {
 87  
         // TODO: This is a little hacky, but validators need to be able to cooperate
 88  
         // with translators during client side validation as well
 89  0
         DateTranslator translator = (DateTranslator) getFieldTranslator(field, DateTranslator.class);
 90  0
         if (translator == null)
 91  0
             return;
 92  
         
 93  0
         JSONObject profile = context.getProfile();
 94  
         
 95  0
         context.addInitializationScript(field, "dojo.require(\"tapestry.form.datetime\");");
 96  
         
 97  0
         if (!profile.has(ValidationConstants.CONSTRAINTS)) {
 98  0
             profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
 99  
         }
 100  0
         JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
 101  
         
 102  0
         accumulateProperty(cons, field.getClientId(), 
 103  
                 new JSONLiteral("[tapestry.form.datetime.isValidDate,{"
 104  
                         + "min:" 
 105  
                         + JSONObject.quote(translator.format(field, context.getLocale(), _minDate))
 106  
                         + ","
 107  
                         + "datePattern:" 
 108  
                         + JSONObject.quote(translator.getPattern())
 109  
                         + (translator.isLenient() ? "" : ",strict:true")
 110  
                         + "}]"));
 111  
         
 112  0
         accumulateProfileProperty(field, profile, 
 113  
                 ValidationConstants.CONSTRAINTS, buildMessage(context, field, translator));
 114  0
     }
 115  
 }