Coverage Report - org.apache.tapestry.form.validator.Identity
 
Classes in this File Line Coverage Branch Coverage Complexity
Identity
0%
0/39
0%
0/24
2.364
 
 1  
 // Copyright 2007 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 org.apache.tapestry.IMarkupWriter;
 18  
 import org.apache.tapestry.IRequestCycle;
 19  
 import org.apache.tapestry.form.FormComponentContributorContext;
 20  
 import org.apache.tapestry.form.IFormComponent;
 21  
 import org.apache.tapestry.form.ValidationMessages;
 22  
 import org.apache.tapestry.json.JSONLiteral;
 23  
 import org.apache.tapestry.json.JSONObject;
 24  
 import org.apache.tapestry.valid.ValidationConstants;
 25  
 import org.apache.tapestry.valid.ValidationConstraint;
 26  
 import org.apache.tapestry.valid.ValidationStrings;
 27  
 import org.apache.tapestry.valid.ValidatorException;
 28  
 
 29  
 /**
 30  
  * Validates that the input value is the same as the value of another field.
 31  
  * This validator can also work in 'differ' mode.
 32  
  * <p/>
 33  
  * Apply this validator to the second field in question and define the name
 34  
  * of the component against which to compare the current value.
 35  
  *
 36  
  * @since 4.1.2
 37  
  */
 38  
 public class Identity extends BaseValidator {
 39  
 
 40  
     private String _fieldName;
 41  
     private int _matchType;
 42  
 
 43  
     private static final int DIFFER = 0;
 44  
     private static final int MATCH = 1;
 45  
 
 46  
     public Identity()
 47  
     {
 48  0
         super();
 49  0
     }
 50  
 
 51  
     public Identity(String initializer)
 52  
     {
 53  0
         super(initializer);
 54  0
     }
 55  
 
 56  
     public String toString(IFormComponent field, Object value)
 57  
     {
 58  0
         if (value == null)
 59  0
             return null;
 60  
 
 61  0
         return value.toString();
 62  
     }
 63  
 
 64  
     public void validate(IFormComponent field, ValidationMessages messages, Object object)
 65  
             throws ValidatorException
 66  
     {
 67  0
         IFormComponent referent = (IFormComponent) field.getContainer().getComponent(_fieldName);
 68  0
         Object referentValue = referent.getBinding("value").getObject();
 69  
 
 70  
         //TODO: if component is null treat _fieldName as an ognl expression
 71  0
         boolean notEq = notEqual(referentValue, object);
 72  
 
 73  0
         if (_matchType == MATCH ? notEq : !notEq)
 74  0
             throw new ValidatorException(buildIdentityMessage(messages, field, referent),
 75  
                     ValidationConstraint.CONSISTENCY);
 76  0
     }
 77  
 
 78  
     public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
 79  
                                    FormComponentContributorContext context, IFormComponent field)
 80  
     {
 81  0
         if (field.isDisabled())
 82  0
             return;
 83  
 
 84  0
         IFormComponent referent = (IFormComponent) field.getContainer().getComponent(_fieldName);
 85  
 
 86  0
         JSONObject profile = context.getProfile();
 87  
 
 88  0
         if (!profile.has(ValidationConstants.CONSTRAINTS)) {
 89  0
             profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
 90  
         }
 91  0
         JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
 92  
 
 93  0
         String func = (_matchType == MATCH) ?
 94  
                 "tapestry.form.validation.isEqual" :
 95  
                 "tapestry.form.validation.isNotEqual";
 96  
 
 97  0
         accumulateProperty(cons, field.getClientId(),
 98  
                 new JSONLiteral("[" + func + ",\""
 99  
                                 + referent.getClientId() + "\"]"));
 100  
         // could define and use a new ValidationConstants.CONFIRM here to apply to
 101  
         // the profile, but it doesn't support differ.
 102  0
         accumulateProfileProperty(field, profile,
 103  
                 ValidationConstants.CONSTRAINTS, buildIdentityMessage(context, field, referent));
 104  0
     }
 105  
 
 106  
     public String getMatch()
 107  
     {
 108  0
         return _fieldName;
 109  
     }
 110  
 
 111  
     public void setMatch(String field)
 112  
     {
 113  0
         _fieldName = field;
 114  0
         _matchType = MATCH;
 115  
 
 116  0
     }
 117  
 
 118  
     public String getDiffer()
 119  
     {
 120  0
         return _fieldName;
 121  
     }
 122  
 
 123  
     public void setDiffer(String field)
 124  
     {
 125  0
         _fieldName = field;
 126  0
         _matchType = DIFFER;
 127  0
     }
 128  
     
 129  
     protected String buildIdentityMessage(ValidationMessages messages, IFormComponent field, IFormComponent referent)
 130  
     {
 131  0
         Object[] parameters = new Object[]{
 132  
                 field.getDisplayName(), new Integer(_matchType), referent.getDisplayName()
 133  
         };
 134  
         
 135  0
         return messages.formatValidationMessage(getMessage(),
 136  
                 ValidationStrings.INVALID_FIELD_EQUALITY, parameters);
 137  
     }
 138  
 
 139  
     private boolean notEqual(Object o1, Object o2)
 140  
     {
 141  0
         if (o1 == null && o2 == null)
 142  0
             return false;
 143  0
         if (o1 == null || o2 == null)
 144  0
             return true;
 145  
         
 146  0
         return !o1.equals(o2);
 147  
     }
 148  
 
 149  
 }