Coverage Report - org.kuali.rice.kns.uif.util.ClientValidationUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ClientValidationUtils
0%
0/264
0%
0/222
7.333
ClientValidationUtils$ValidationMessageKeys
0%
0/27
0%
0/4
7.333
 
 1  
 /*
 2  
  * Copyright 2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 1.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl1.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kns.uif.util;
 17  
 
 18  
 import java.text.MessageFormat;
 19  
 import java.util.ArrayList;
 20  
 import java.util.EnumSet;
 21  
 import java.util.List;
 22  
 
 23  
 import org.apache.commons.lang.StringUtils;
 24  
 import org.kuali.rice.core.api.config.property.ConfigurationService;
 25  
 import org.kuali.rice.kns.datadictionary.validation.constraint.BaseConstraint;
 26  
 import org.kuali.rice.kns.datadictionary.validation.constraint.CaseConstraint;
 27  
 import org.kuali.rice.kns.datadictionary.validation.constraint.Constraint;
 28  
 import org.kuali.rice.kns.datadictionary.validation.constraint.MustOccurConstraint;
 29  
 import org.kuali.rice.kns.datadictionary.validation.constraint.PrerequisiteConstraint;
 30  
 import org.kuali.rice.kns.datadictionary.validation.constraint.SimpleConstraint;
 31  
 import org.kuali.rice.kns.datadictionary.validation.constraint.ValidCharactersConstraint;
 32  
 import org.kuali.rice.kns.datadictionary.validation.constraint.WhenConstraint;
 33  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 34  
 import org.kuali.rice.kns.uif.container.View;
 35  
 import org.kuali.rice.kns.uif.control.TextControl;
 36  
 import org.kuali.rice.kns.uif.field.AttributeField;
 37  
 
 38  
 /**
 39  
  * This class contains all the methods necessary for generating the js required to perform validation client side.
 40  
  * The processAndApplyConstraints(AttributeField field, View view) is the key method of this class used by
 41  
  * AttributeField to setup its client side validation mechanisms.
 42  
  * 
 43  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 44  
  */
 45  0
 public class ClientValidationUtils {
 46  
         // used to give validation methods unique signatures
 47  0
         private static int methodKey = 0;
 48  
         
 49  
         // list used to temporarily store mustOccurs field names for the error
 50  
         // message
 51  
         private static List<List<String>> mustOccursPathNames;
 52  
         
 53  
         public static final String LABEL_KEY_SPLIT_PATTERN = ",";
 54  
         
 55  
         public static final String VALIDATION_MSG_KEY_PREFIX = "validation.";
 56  
         public static final String PREREQ_MSG_KEY = "prerequisite";
 57  
         public static final String POSTREQ_MSG_KEY = "postrequisite";
 58  
         public static final String MUSTOCCURS_MSG_KEY = "mustOccurs";
 59  
         public static final String GENERIC_FIELD_MSG_KEY = "general.genericFieldName";
 60  
         
 61  
         public static final String ALL_MSG_KEY = "general.all";
 62  
         public static final String ATMOST_MSG_KEY = "general.atMost";
 63  
         public static final String AND_MSG_KEY = "general.and";
 64  
         public static final String OR_MSG_KEY = "general.or";
 65  
         
 66  0
         private static ConfigurationService configService = KNSServiceLocator.getKualiConfigurationService();
 67  
         
 68  
         //Enum representing names of rules provided by the jQuery plugin
 69  0
         public static enum ValidationMessageKeys{
 70  0
                 REQUIRED("required"), 
 71  0
                 MIN_EXCLUSIVE("minExclusive"), 
 72  0
                 MAX_INCLUSIVE("maxInclusive"),
 73  0
                 MIN_LENGTH("minLengthConditional"),
 74  0
                 MAX_LENGTH("maxLengthConditional"),
 75  0
                 EMAIL("email"),
 76  0
                 URL("url"),
 77  0
                 DATE("date"),
 78  0
                 NUMBER("number"),
 79  0
                 DIGITS("digits"),
 80  0
                 CREDITCARD("creditcard"),
 81  0
                 LETTERS_WITH_BASIC_PUNC("letterswithbasicpunc"),
 82  0
                 ALPHANUMERIC("alphanumeric"),
 83  0
                 LETTERS_ONLY("lettersonly"),
 84  0
                 NO_WHITESPACE("nowhitespace"),
 85  0
                 INTEGER("integer"),
 86  0
                 PHONE_US("phoneUS"),
 87  0
                 TIME("time");
 88  
                 
 89  0
                 private ValidationMessageKeys(String name) {
 90  0
                         this.name = name;
 91  0
                 }
 92  
                 
 93  
                 private final String name;
 94  
                 
 95  
                 @Override
 96  
                 public String toString() {
 97  0
                         return name;
 98  
                 }
 99  
                 
 100  
                 public static boolean contains(String name){
 101  0
             for (ValidationMessageKeys element : EnumSet.allOf(ValidationMessageKeys.class)) {
 102  0
                 if (element.toString().equalsIgnoreCase(name)) {
 103  0
                     return true;
 104  
                 }
 105  
             }
 106  0
             return false;
 107  
                 }
 108  
         }
 109  
         
 110  
         public static String generateMessageFromLabelKey(String labelKey){
 111  0
                 String message = "NO MESSAGE";
 112  0
                 if(StringUtils.isNotEmpty(labelKey)){
 113  0
                         if(labelKey.contains(LABEL_KEY_SPLIT_PATTERN)){
 114  0
                                 message = "";
 115  0
                                 String[] tokens = labelKey.split(LABEL_KEY_SPLIT_PATTERN);
 116  0
                                 int i = 0;
 117  0
                                 for(String s: tokens){
 118  0
                                         String ps = configService.getPropertyString(VALIDATION_MSG_KEY_PREFIX + s);
 119  0
                                         i++;
 120  0
                                         if(i != tokens.length){
 121  0
                                                 message = message + ps + ", ";
 122  
                                         }
 123  
                                         else{
 124  0
                                                 message = message + ps;
 125  
                                         }
 126  
                                 }
 127  0
                         }
 128  
                         else{
 129  0
                                 message = configService.getPropertyString(VALIDATION_MSG_KEY_PREFIX + labelKey);
 130  
                         }
 131  
                 }
 132  0
                 return message;
 133  
         }
 134  
 
 135  
         /**
 136  
          * Generates the js object used to override all default messages for validator jquery plugin with custom
 137  
          * messages derived from the configService.
 138  
          * 
 139  
          * @return
 140  
          */
 141  
         public static String generateValidatorMessagesOption(){
 142  0
                 String mOption = "";
 143  0
                 String keyValuePairs = "";
 144  0
                 for(ValidationMessageKeys element : EnumSet.allOf(ValidationMessageKeys.class)){
 145  0
                         String key = element.toString();
 146  0
                         String message = configService.getPropertyString(VALIDATION_MSG_KEY_PREFIX + key);
 147  0
                         if(StringUtils.isNotEmpty(message)){
 148  0
                                 keyValuePairs = keyValuePairs + "\n" + key + ": \""+ message + "\",";
 149  
                                 
 150  
                         }
 151  
                         
 152  0
                 }
 153  0
                 keyValuePairs = StringUtils.removeEnd(keyValuePairs, ",");
 154  0
                 if(StringUtils.isNotEmpty(keyValuePairs)){
 155  0
                         mOption="{" + keyValuePairs + "}";
 156  
                 }
 157  
                 
 158  0
                 return mOption;
 159  
         }
 160  
         
 161  
         /**
 162  
          * Returns the add method jquery validator call for the regular expression
 163  
          * stored in validCharactersConstraint.
 164  
          * 
 165  
          * @param validCharactersConstraint
 166  
          * @return js validator.addMethod script
 167  
          */
 168  
         public static String getRegexMethod(ValidCharactersConstraint validCharactersConstraint) {
 169  0
                 String message = generateMessageFromLabelKey(validCharactersConstraint.getLabelKey());
 170  0
                 String key = "validChar" + methodKey;
 171  
                 
 172  0
                 return "\njQuery.validator.addMethod(\"" + key
 173  
                                 + "\", function(value, element) {\n" + " return this.optional(element) || "
 174  
                                 + validCharactersConstraint.getJsValue() + ".test(value); " + "}, \""
 175  
                                 + message + "\");";
 176  
         }
 177  
         
 178  
 
 179  
         /**
 180  
          * This method processes a single CaseConstraint. Internally it makes calls
 181  
          * to processWhenConstraint for each WhenConstraint that exists in this
 182  
          * constraint. It adds a "dependsOn" css class to this field for the field
 183  
          * which the CaseConstraint references.
 184  
          * 
 185  
          * @param view
 186  
          * @param andedCase
 187  
          *            the boolean logic to be anded when determining if this case is
 188  
          *            satisfied (used for nested CaseConstraints)
 189  
          */
 190  
         public static void processCaseConstraint(AttributeField field, View view, CaseConstraint constraint, String andedCase) {
 191  0
                 if (constraint.getOperator() == null) {
 192  0
                         constraint.setOperator("equals");
 193  
                 }
 194  
 
 195  0
                 String operator = "==";
 196  0
                 if (constraint.getOperator().equalsIgnoreCase("not_equals")) {
 197  0
                         operator = "!=";
 198  
                 }
 199  0
                 else if (constraint.getOperator().equalsIgnoreCase("greater_than_equal")) {
 200  0
                         operator = ">=";
 201  
                 }
 202  0
                 else if (constraint.getOperator().equalsIgnoreCase("less_than_equal")) {
 203  0
                         operator = "<=";
 204  
                 }
 205  0
                 else if (constraint.getOperator().equalsIgnoreCase("greater_than")) {
 206  0
                         operator = ">";
 207  
                 }
 208  0
                 else if (constraint.getOperator().equalsIgnoreCase("less_than")) {
 209  0
                         operator = "<";
 210  
                 }
 211  0
                 else if (constraint.getOperator().equalsIgnoreCase("has_value")) {
 212  0
                         operator = "";
 213  
                 }
 214  
                 // add more operator types here if more are supported later
 215  
 
 216  0
                 field.getControl().addStyleClass("dependsOn-" + constraint.getFieldPath());
 217  
 
 218  0
                 if (constraint.getWhenConstraint() != null && !constraint.getWhenConstraint().isEmpty()) {
 219  0
                         for (WhenConstraint wc : constraint.getWhenConstraint()) {
 220  0
                                 processWhenConstraint(field, view, constraint, wc, constraint.getFieldPath(), operator, andedCase);
 221  
                         }
 222  
                 }
 223  0
         }
 224  
         
 225  
 
 226  
 
 227  
         /**
 228  
          * This method processes the WhenConstraint passed in. The when constraint
 229  
          * is used to create a boolean statement to determine if the constraint will
 230  
          * be applied. The necessary rules/methods for applying this constraint are
 231  
          * created in the createRule call. Note the use of the use of coerceValue js
 232  
          * function call.
 233  
          * 
 234  
          * @param view
 235  
          * @param wc
 236  
          * @param fieldPath
 237  
          * @param operator
 238  
          * @param andedCase
 239  
          */
 240  
         private static void processWhenConstraint(AttributeField field, View view, CaseConstraint caseConstraint, WhenConstraint wc, String fieldPath,
 241  
                         String operator, String andedCase) {
 242  0
                 String ruleString = "";
 243  
                 // prerequisite constraint
 244  
 
 245  0
                 String booleanStatement = "";
 246  0
                 if (wc.getValues() != null) {
 247  
 
 248  0
                         String caseStr = "";
 249  0
                         if (!caseConstraint.isCaseSensitive()) {
 250  0
                                 caseStr = ".toUpperCase()";
 251  
                         }
 252  0
                         for (int i = 0; i < wc.getValues().size(); i++) {
 253  0
                                 if (operator.isEmpty()) {
 254  
                                         // has_value case
 255  0
                                         if (wc.getValues().get(i) instanceof String
 256  
                                                         && ((String) wc.getValues().get(i)).equalsIgnoreCase("false")) {
 257  0
                                                 booleanStatement = booleanStatement + "!(coerceValue('" + fieldPath + "'))";
 258  
                                         }
 259  
                                         else {
 260  0
                                                 booleanStatement = booleanStatement + "(coerceValue('" + fieldPath + "'))";
 261  
                                         }
 262  
                                 }
 263  
                                 else {
 264  
                                         // everything else
 265  0
                                         booleanStatement = booleanStatement + "(coerceValue('" + fieldPath + "')" + caseStr + " "
 266  
                                                         + operator + " \"" + wc.getValues().get(i) + "\"" + caseStr + ")";
 267  
                                 }
 268  0
                                 if ((i + 1) != wc.getValues().size()) {
 269  0
                                         booleanStatement = booleanStatement + " || ";
 270  
                                 }
 271  
                         }
 272  
 
 273  
                 }
 274  
 
 275  0
                 if (andedCase != null) {
 276  0
                         booleanStatement = "(" + booleanStatement + ") && (" + andedCase + ")";
 277  
                 }
 278  
 
 279  0
                 if (wc.getConstraint() != null && StringUtils.isNotEmpty(booleanStatement)) {
 280  0
                         ruleString = createRule(field, wc.getConstraint(), booleanStatement, view);
 281  
                 }
 282  
 
 283  0
                 if (StringUtils.isNotEmpty(ruleString)) {
 284  0
                         addScriptToView(view, ruleString);
 285  
                 }
 286  0
         }
 287  
         
 288  
 
 289  
 
 290  
         /**
 291  
          * Adds the script to the view to execute on a jQuery document ready event.
 292  
          * 
 293  
          * @param view
 294  
          * @param script
 295  
          */
 296  
         public static void addScriptToView(View view, String script) {
 297  0
                 String prefixScript = "";
 298  0
                 if (view.getOnDocumentReadyScript() != null) {
 299  0
                         prefixScript = view.getOnDocumentReadyScript();
 300  
                 }
 301  
 
 302  0
                 view.setOnDocumentReadyScript(prefixScript + "\n" + script);
 303  0
         }
 304  
 
 305  
         /**
 306  
          * This method takes in a constraint to apply only when the passed in
 307  
          * booleanStatement is valid. The method will create the necessary addMethod
 308  
          * and addRule jquery validator calls for the rule to be applied to the
 309  
          * field when the statement passed in evaluates to true during runtime and
 310  
          * this field is being validated. Note the use of custom methods for min/max
 311  
          * length/value.
 312  
          * 
 313  
          * @param applyToField
 314  
          *            the field to apply the generated methods and rules to
 315  
          * @param constraint
 316  
          *            the constraint to be applied when the booleanStatement
 317  
          *            evaluates to true during validation
 318  
          * @param booleanStatement
 319  
          *            the booleanstatement in js - should return true when the
 320  
          *            validation rule should be applied
 321  
          * @param view
 322  
          * @return
 323  
          */
 324  
         @SuppressWarnings("boxing")
 325  
         private static String createRule(AttributeField field, Constraint constraint, String booleanStatement, View view) {
 326  0
                 String rule = "";
 327  0
                 int constraintCount = 0;
 328  0
                 if (constraint instanceof BaseConstraint && ((BaseConstraint) constraint).getApplyClientSide()) {
 329  0
                         if (constraint instanceof SimpleConstraint) {
 330  0
                                 if (((SimpleConstraint) constraint).getRequired()) {
 331  0
                                         rule = rule + "required: function(element){\nreturn (" + booleanStatement + ");}";
 332  0
                                         constraintCount++;
 333  
                                 }
 334  0
                                 if (((SimpleConstraint) constraint).getMinLength() != null) {
 335  0
                                         if (constraintCount > 0) {
 336  0
                                                 rule = rule + ",\n";
 337  
                                         }
 338  0
                                         rule = rule + "minLengthConditional: [" + ((SimpleConstraint) constraint).getMinLength()
 339  
                                                         + ", function(){return " + booleanStatement + ";}]";
 340  
                                 }
 341  0
                                 if (((SimpleConstraint) constraint).getMaxLength() != null) {
 342  0
                                         if (constraintCount > 0) {
 343  0
                                                 rule = rule + ",\n";
 344  
                                         }
 345  0
                                         rule = rule + "maxLengthConditional: [" + ((SimpleConstraint) constraint).getMaxLength()
 346  
                                                         + ", function(){return " + booleanStatement + ";}]";
 347  
                                 }
 348  
 
 349  0
                                 if (((SimpleConstraint) constraint).getExclusiveMin() != null) {
 350  0
                                         if (constraintCount > 0) {
 351  0
                                                 rule = rule + ",\n";
 352  
                                         }
 353  0
                                         rule = rule + "minExclusive: [" + ((SimpleConstraint) constraint).getExclusiveMin()
 354  
                                                         + ", function(){return " + booleanStatement + ";}]";
 355  
                                 }
 356  
 
 357  0
                                 if (((SimpleConstraint) constraint).getInclusiveMax() != null) {
 358  0
                                         if (constraintCount > 0) {
 359  0
                                                 rule = rule + ",\n";
 360  
                                         }
 361  0
                                         rule = rule + "maxInclusive: [" + ((SimpleConstraint) constraint).getInclusiveMax()
 362  
                                                         + ", function(){return " + booleanStatement + ";}]";
 363  
                                 }
 364  
 
 365  0
                                 rule = "jq('[name=\"" + field.getBindingInfo().getBindingPath() + "\"]').rules(\"add\", {" + rule + "\n});";
 366  
                         }
 367  0
                         else if (constraint instanceof ValidCharactersConstraint) {
 368  0
                                 String regexMethod = "";
 369  0
                                 String methodName = "";
 370  0
                                 if(StringUtils.isNotEmpty(((ValidCharactersConstraint)constraint).getJsValue())) {
 371  0
                                         regexMethod = ClientValidationUtils.getRegexMethod((ValidCharactersConstraint) constraint) + "\n";
 372  0
                                         methodName = "validChar" + methodKey;
 373  0
                                         methodKey++;
 374  
                                 }
 375  
                                 else {
 376  0
                                         if(StringUtils.isNotEmpty(((ValidCharactersConstraint)constraint).getLabelKey())){
 377  0
                                                 methodName = ((ValidCharactersConstraint)constraint).getLabelKey();
 378  
                                         }
 379  
                                 }
 380  0
                                 if (StringUtils.isNotEmpty(methodName)) {
 381  0
                                         rule = regexMethod + "jq('[name=\"" + field.getBindingInfo().getBindingPath() + "\"]').rules(\"add\", {\n" + methodName
 382  
                                                         + ": function(element){return (" + booleanStatement + ");}\n});";
 383  
                                 }
 384  0
                         }
 385  0
                         else if (constraint instanceof PrerequisiteConstraint) {
 386  0
                                 processPrerequisiteConstraint(field, (PrerequisiteConstraint) constraint, view, booleanStatement);
 387  
                         }
 388  0
                         else if (constraint instanceof CaseConstraint) {
 389  0
                                 processCaseConstraint(field, view, (CaseConstraint) constraint, booleanStatement);
 390  
                         }
 391  0
                         else if (constraint instanceof MustOccurConstraint) {
 392  0
                                 processMustOccurConstraint(field, view, (MustOccurConstraint) constraint, booleanStatement);
 393  
                         }
 394  
                 }
 395  0
                 return rule;
 396  
         }
 397  
         
 398  
 
 399  
 
 400  
         /**
 401  
          * This method is a simpler version of processPrerequisiteConstraint
 402  
          * 
 403  
          * @see AttributeField#processPrerequisiteConstraint(PrerequisiteConstraint,
 404  
          *      View, String)
 405  
          * @param constraint
 406  
          * @param view
 407  
          */
 408  
         public static void processPrerequisiteConstraint(AttributeField field, PrerequisiteConstraint constraint, View view) {
 409  0
                 processPrerequisiteConstraint(field, constraint, view, "true");
 410  0
         }
 411  
 
 412  
         /**
 413  
          * This method processes a Prerequisite constraint that should be applied
 414  
          * when the booleanStatement passed in evaluates to true.
 415  
          * 
 416  
          * @param constraint
 417  
          *            prerequisiteConstraint
 418  
          * @param view
 419  
          * @param booleanStatement
 420  
          *            the booleanstatement in js - should return true when the
 421  
          *            validation rule should be applied
 422  
          */
 423  
         public static void processPrerequisiteConstraint(AttributeField field, PrerequisiteConstraint constraint, View view, String booleanStatement) {
 424  0
                 if (constraint != null && constraint.getApplyClientSide().booleanValue()) {
 425  0
                         addScriptToView(view, getPrerequisiteStatement(field, view, constraint, booleanStatement)
 426  
                                         + getPostrequisiteStatement(field, constraint, booleanStatement));
 427  
                 }
 428  0
         }
 429  
 
 430  
         /**
 431  
          * This method creates the script necessary for executing a prerequisite
 432  
          * rule in which this field occurs after the field specified in the
 433  
          * prerequisite rule - since it requires a specific set of UI logic. Builds
 434  
          * an if statement containing an addMethod jquery validator call. Adds a
 435  
          * "dependsOn" css class to this field for the field specified.
 436  
          * 
 437  
          * @param constraint
 438  
          *            prerequisiteConstraint
 439  
          * @param booleanStatement
 440  
          *            the booleanstatement in js - should return true when the
 441  
          *            validation rule should be applied
 442  
          * @return
 443  
          */
 444  
         private static String getPrerequisiteStatement(AttributeField field, View view, PrerequisiteConstraint constraint, String booleanStatement) {
 445  0
                 methodKey++;
 446  0
                 String message = "";
 447  0
                 if(StringUtils.isEmpty(constraint.getLabelKey())){
 448  0
                         message = configService.getPropertyString(VALIDATION_MSG_KEY_PREFIX + "prerequisite");
 449  
                 }
 450  
                 else{
 451  0
                         message = generateMessageFromLabelKey(constraint.getLabelKey());
 452  
                 }
 453  0
                 if(StringUtils.isEmpty(message)){
 454  0
                         message = "prerequisite - No message";
 455  
                 }
 456  
                 else{
 457  0
                         AttributeField requiredField = view.getViewIndex().getAttributeFieldByPath(constraint.getAttributePath());
 458  0
                         if(requiredField != null && StringUtils.isNotEmpty(requiredField.getLabel())){
 459  0
                                 message = MessageFormat.format(message, requiredField.getLabel());
 460  
                         }
 461  
                         else{
 462  0
                                 message = MessageFormat.format(message, configService.getPropertyString(GENERIC_FIELD_MSG_KEY));
 463  
                         }
 464  
                 }
 465  
                 // field occurs before case
 466  0
                 String dependsClass = "dependsOn-" + constraint.getAttributePath();
 467  0
                 String methodName = "prConstraint" + methodKey;
 468  0
                 String addClass = "jq('[name=\""+ field.getBindingInfo().getBindingPath() + "\"]').addClass('" + dependsClass + "');\n" +
 469  
                         "jq('[name=\""+ field.getBindingInfo().getBindingPath() + "\"]').addClass('" + methodName + "');\n";
 470  0
                 String method = "\njQuery.validator.addMethod(\""+ methodName +"\", function(value, element) {\n" +
 471  
                         " if(" + booleanStatement + "){ return (this.optional(element) || (coerceValue('" + constraint.getAttributePath() + "')));}else{return true;} " +
 472  
                         "}, \"" + message + "\");";
 473  
                 
 474  0
                 String ifStatement = "if(occursBefore('" + constraint.getAttributePath() + "','" + field.getBindingInfo().getBindingPath() + 
 475  
                 "')){" + addClass + method + "}";
 476  0
                 return ifStatement;
 477  
         }
 478  
 
 479  
         /**
 480  
          * This method creates the script necessary for executing a prerequisite
 481  
          * rule in which this field occurs before the field specified in the
 482  
          * prerequisite rule - since it requires a specific set of UI logic. Builds
 483  
          * an if statement containing an addMethod jquery validator call.
 484  
          * 
 485  
          * @param constraint
 486  
          *            prerequisiteConstraint
 487  
          * @param booleanStatement
 488  
          *            the booleanstatement in js - should return true when the
 489  
          *            validation rule should be applied
 490  
          * @return
 491  
          */
 492  
         private static String getPostrequisiteStatement(AttributeField field, PrerequisiteConstraint constraint, String booleanStatement) {
 493  
                 // field occurs after case
 494  0
                 String message = "";
 495  0
                 if(StringUtils.isEmpty(constraint.getLabelKey())){
 496  0
                         message = configService.getPropertyString(VALIDATION_MSG_KEY_PREFIX + "postrequisite");
 497  
                 }
 498  
                 else{
 499  0
                         message = generateMessageFromLabelKey(constraint.getLabelKey());
 500  
                 }
 501  
                 
 502  0
                 if(StringUtils.isEmpty(constraint.getLabelKey())){
 503  0
                         if(StringUtils.isNotEmpty(field.getLabel())){
 504  0
                                 message = MessageFormat.format(message, field.getLabel());
 505  
                         }
 506  
                         else{
 507  0
                                 message = MessageFormat.format(message, configService.getPropertyString(GENERIC_FIELD_MSG_KEY));
 508  
                         }
 509  
                         
 510  
                 }
 511  
                 
 512  0
                 String function = "function(element){\n" +
 513  
                         "return (coerceValue('"+ field.getBindingInfo().getBindingPath() + "') && " + booleanStatement + ");}";
 514  0
                 String postStatement = "\nelse if(occursBefore('" + field.getBindingInfo().getBindingPath() + "','" + constraint.getAttributePath() + 
 515  
                         "')){\njq('[name=\""+ constraint.getAttributePath() + 
 516  
                         "\"]').rules(\"add\", { required: \n" + function 
 517  
                         + ", \nmessages: {\nrequired: \""+ message +"\"}});}\n";
 518  
                 
 519  0
                 return postStatement;
 520  
 
 521  
         }
 522  
 
 523  
         /**
 524  
          * This method processes the MustOccurConstraint. The constraint is only
 525  
          * applied when the booleanStatement evaluates to true during validation.
 526  
          * This method creates the addMethod and add rule calls for the jquery
 527  
          * validation plugin necessary for applying this constraint to this field.
 528  
          * 
 529  
          * @param view
 530  
          * @param mc
 531  
          * @param booleanStatement
 532  
          *            the booleanstatement in js - should return true when the
 533  
          *            validation rule should be applied
 534  
          */
 535  
         public static void processMustOccurConstraint(AttributeField field, View view, MustOccurConstraint mc, String booleanStatement) {
 536  0
                 methodKey++;
 537  0
                 mustOccursPathNames = new ArrayList<List<String>>();
 538  
                 // TODO make this show the fields its requiring
 539  0
                 String methodName = "moConstraint" + methodKey;
 540  0
                 String method = "\njQuery.validator.addMethod(\""+ methodName +"\", function(value, element) {\n" +
 541  
                 " if(" + booleanStatement + "){return (this.optional(element) || ("+ getMustOccurStatement(field, mc) + "));}else{return true;}" +
 542  
                 "}, \"" + getMustOccursMessage(view, mc) +"\");";
 543  0
                 String rule = method + "jq('[name=\""+ field.getBindingInfo().getBindingPath() + "\"]').rules(\"add\", {\n" + methodName + ": function(element){return (" + booleanStatement + ");}\n});";
 544  0
                 addScriptToView(view, rule);
 545  0
         }
 546  
 
 547  
         /**
 548  
          * This method takes in a MustOccurConstraint and returns the statement used
 549  
          * in determining if the must occurs constraint has been satisfied when this
 550  
          * field is validated. Note the use of the mustOccurCheck method. Nested
 551  
          * mustOccurConstraints are ored against the result of the mustOccurCheck by
 552  
          * calling this method recursively.
 553  
          * 
 554  
          * @param constraint
 555  
          * @return
 556  
          */
 557  
         @SuppressWarnings("boxing")
 558  
         private static String getMustOccurStatement(AttributeField field, MustOccurConstraint constraint) {
 559  0
                 String statement = "";
 560  0
                 List<String> attributePaths = new ArrayList<String>();
 561  0
                 if (constraint != null && constraint.getApplyClientSide()) {
 562  0
                         String array = "[";
 563  0
                         if (constraint.getPrerequisiteConstraints() != null) {
 564  0
                                 for (int i = 0; i < constraint.getPrerequisiteConstraints().size(); i++) {
 565  0
                                         field.getControl().addStyleClass("dependsOn-"
 566  
                                                         + constraint.getPrerequisiteConstraints().get(i).getAttributePath());
 567  0
                                         array = array + "'" + constraint.getPrerequisiteConstraints().get(i).getAttributePath() + "'";
 568  0
                                         attributePaths.add(constraint.getPrerequisiteConstraints().get(i).getAttributePath());
 569  0
                                         if (i + 1 != constraint.getPrerequisiteConstraints().size()) {
 570  0
                                                 array = array + ",";
 571  
                                         }
 572  
 
 573  
                                 }
 574  
                         }
 575  0
                         array = array + "]";
 576  0
                         statement = "mustOccurTotal(" + array + ", " + constraint.getMin() + ", " + constraint.getMax() + ")";
 577  
                         //add min to string list
 578  0
                         if(constraint.getMin()!=null){
 579  0
                                 attributePaths.add(constraint.getMin().toString());
 580  
                         }
 581  
                         else{
 582  0
                                 attributePaths.add(null);
 583  
                         }
 584  
                         //add max to string list
 585  0
                         if(constraint.getMax()!=null){
 586  0
                                 attributePaths.add(constraint.getMax().toString());
 587  
                         }
 588  
                         else{
 589  0
                                 attributePaths.add(null);
 590  
                         }
 591  
                         
 592  0
                         mustOccursPathNames.add(attributePaths);
 593  0
                         if(StringUtils.isEmpty(statement)){
 594  0
                                 statement = "0";
 595  
                         }
 596  0
                         if (constraint.getMustOccurConstraints() != null) {
 597  0
                                 for (MustOccurConstraint mc : constraint.getMustOccurConstraints()) {
 598  0
                                         statement = "mustOccurCheck(" + statement + " + " + getMustOccurStatement(field, mc) +
 599  
                                                 ", " + constraint.getMin() + ", " + constraint.getMax() + ")";
 600  
                                 }
 601  
                         }
 602  
                         else{
 603  0
                                 statement = "mustOccurCheck(" + statement +
 604  
                                 ", " + constraint.getMin() + ", " + constraint.getMax() + ")";
 605  
                         }
 606  
                 }
 607  0
                 return statement;
 608  
         }
 609  
 
 610  
         
 611  
         /**
 612  
          * Generates a message for the must occur constraint (if no label key is specified).  
 613  
          * This message is most accurate when must occurs is a single
 614  
          * or double level constraint.  Beyond that, the message will still be accurate but may be confusing for
 615  
          * the user - this auto-generated message however will work in MOST use cases.
 616  
          * 
 617  
          * @param view
 618  
          * @return
 619  
          */
 620  
         private static String getMustOccursMessage(View view, MustOccurConstraint constraint){
 621  0
                 String message = "";
 622  0
                 if(StringUtils.isNotEmpty(constraint.getLabelKey())){
 623  0
                         message = generateMessageFromLabelKey(constraint.getLabelKey());
 624  
                 }
 625  
                 else{
 626  0
                         String and = configService.getPropertyString(AND_MSG_KEY);
 627  0
                         String all = configService.getPropertyString(ALL_MSG_KEY);
 628  0
                         String atMost = configService.getPropertyString(ATMOST_MSG_KEY);
 629  0
                         String genericLabel = configService.getPropertyString(GENERIC_FIELD_MSG_KEY);
 630  0
                         String mustOccursMsg = configService.getPropertyString(VALIDATION_MSG_KEY_PREFIX + MUSTOCCURS_MSG_KEY);
 631  
                         //String postfix = configService.getPropertyString(VALIDATION_MSG_KEY_PREFIX + MUSTOCCURS_POST_MSG_KEY);
 632  0
                         String statement="";
 633  0
                         for(int i=0; i< mustOccursPathNames.size(); i++){
 634  0
                                 String andedString = "";
 635  
                                 
 636  0
                                 List<String> paths = mustOccursPathNames.get(i);
 637  0
                                 if(!paths.isEmpty()){
 638  
                                         //note that the last 2 strings are min and max and rest are attribute paths
 639  0
                                         String min = paths.get(paths.size()-2);
 640  0
                                         String max = paths.get(paths.size()-1);
 641  0
                                         for(int j=0; j<paths.size()-2;j++){
 642  0
                                                 AttributeField field = view.getViewIndex().getAttributeFieldByPath(paths.get(j).trim());
 643  0
                                                 String label = genericLabel;
 644  0
                                                 if(field != null && StringUtils.isNotEmpty(field.getLabel())){
 645  0
                                                         label = field.getLabel();
 646  
                                                 }
 647  0
                                                 if(min.equals(max)){
 648  0
                                                         if(j==0){
 649  0
                                                                 andedString = label;
 650  
                                                         }
 651  0
                                                         else if(j==paths.size()-3){
 652  0
                                                                 andedString = andedString + " " + and + " " + label;
 653  
                                                         }
 654  
                                                         else{
 655  0
                                                                 andedString = andedString + ", " + label;
 656  
                                                         }
 657  
                                                 }
 658  
                                                 else{
 659  0
                                                         andedString = andedString + "<li>" + label + "</li>";
 660  
                                                 }
 661  
                                         }
 662  0
                                         if(min.equals(max)){
 663  0
                                                 andedString = "<li>" + andedString + "</li>";
 664  
                                         }
 665  0
                                         andedString="<ul>" + andedString + "</ul>";
 666  
                                 
 667  0
                                         if(StringUtils.isNotEmpty(min) && StringUtils.isNotEmpty(max) && !min.equals(max)){
 668  0
                                                 andedString = MessageFormat.format(mustOccursMsg, min + "-" + max) + "<br/>" +andedString;
 669  
                                         }
 670  0
                                         else if(StringUtils.isNotEmpty(min) && StringUtils.isNotEmpty(max) && min.equals(max) && i==0){
 671  0
                                                 andedString = MessageFormat.format(mustOccursMsg, all) + "<br/>" +andedString;
 672  
                                         }
 673  0
                                         else if(StringUtils.isNotEmpty(min) && StringUtils.isNotEmpty(max) && min.equals(max) && i!=0){
 674  
                                                 //leave andedString as is
 675  
                                         }
 676  0
                                         else if(StringUtils.isNotEmpty(min)){
 677  0
                                                 andedString = MessageFormat.format(mustOccursMsg, min) + "<br/>" +andedString;
 678  
                                         }
 679  0
                                         else if(StringUtils.isNotEmpty(max)){
 680  0
                                                 andedString = MessageFormat.format(mustOccursMsg, atMost + " " + max) + "<br/>" +andedString;
 681  
                                         }
 682  
                                 }
 683  0
                                 if(StringUtils.isNotEmpty(andedString)){
 684  0
                                         if(i==0){
 685  0
                                                 statement = andedString;
 686  
                                         }
 687  
                                         else{
 688  0
                                                 statement = statement + andedString;
 689  
                                         }
 690  
                                 }
 691  
                         }
 692  0
                         if(StringUtils.isNotEmpty(statement)){
 693  0
                                 message = statement;
 694  
                         }
 695  
                 }
 696  
                 
 697  0
                 return message;
 698  
         }
 699  
 
 700  
         /**
 701  
          * This method processes all the constraints on the AttributeField passed in and adds all the necessary
 702  
          * jQuery and js required (validator's rules, methods, and messages) to the View's onDocumentReady call.
 703  
          * The result is js that will validate all the constraints contained on an AttributeField during user interaction
 704  
          * with the field using the jQuery validation plugin and custom code.
 705  
          * 
 706  
          * @param attributeField
 707  
          */
 708  
         @SuppressWarnings("boxing")
 709  
         public static void processAndApplyConstraints(AttributeField field, View view) {
 710  0
                 if ((field.getRequired() != null) && (field.getRequired().booleanValue())) {
 711  0
                         field.getControl().addStyleClass("required");
 712  
                 }
 713  
 
 714  0
                 if (field.getExclusiveMin() != null) {
 715  0
                         if (field.getControl() instanceof TextControl && ((TextControl) field.getControl()).getDatePicker() != null) {
 716  0
                                 ((TextControl) field.getControl()).getDatePicker().getComponentOptions().put("minDate", field.getExclusiveMin());
 717  
                         }
 718  
                         else{
 719  0
                                 String rule = "jq('[name=\""+ field.getBindingInfo().getBindingPath() + "\"]').rules(\"add\", {\n minExclusive: ["+ field.getExclusiveMin() + "]});";
 720  0
                                 addScriptToView(view, rule);
 721  
                         }
 722  
                 }
 723  
 
 724  0
                 if (field.getInclusiveMax() != null) {
 725  0
                         if (field.getControl() instanceof TextControl && ((TextControl) field.getControl()).getDatePicker() != null) {
 726  0
                                 ((TextControl) field.getControl()).getDatePicker().getComponentOptions().put("maxDate", field.getInclusiveMax());
 727  
                         }
 728  
                         else{
 729  0
                                 String rule = "jq('[name=\""+ field.getBindingInfo().getBindingPath() + "\"]').rules(\"add\", {\n maxInclusive: ["+ field.getInclusiveMax() + "]});";
 730  0
                                 addScriptToView(view, rule);
 731  
                         }
 732  
                 }
 733  
 
 734  0
                 if (field.getValidCharactersConstraint() != null && field.getValidCharactersConstraint().getApplyClientSide()) {
 735  0
                         if(StringUtils.isNotEmpty(field.getValidCharactersConstraint().getJsValue())) {
 736  
                                 // set jsValue takes precedence
 737  0
                                 addScriptToView(view, ClientValidationUtils.getRegexMethod(field.getValidCharactersConstraint()));
 738  0
                                 field.getControl().addStyleClass("validChar" + methodKey);
 739  0
                                 methodKey++;
 740  
                         }
 741  
                         else {
 742  
                                 //blindly assume that if there is no js value defined that there must be a method by this name
 743  0
                                 if(StringUtils.isNotEmpty(field.getValidCharactersConstraint().getLabelKey())){
 744  0
                                         field.getControl().addStyleClass(field.getValidCharactersConstraint().getLabelKey());
 745  
                                 }
 746  
                         }
 747  
                 }
 748  
 
 749  0
                 if (field.getCaseConstraint() != null && field.getCaseConstraint().getApplyClientSide()) {
 750  0
                         processCaseConstraint(field, view, field.getCaseConstraint(), null);
 751  
                 }
 752  
 
 753  0
                 if (field.getDependencyConstraints() != null) {
 754  0
                         for (PrerequisiteConstraint prc : field.getDependencyConstraints()) {
 755  0
                                 processPrerequisiteConstraint(field, prc, view);
 756  
                         }
 757  
                 }
 758  
 
 759  0
                 if (field.getMustOccurConstraints() != null) {
 760  0
                         for (MustOccurConstraint mc : field.getMustOccurConstraints()) {
 761  0
                                 processMustOccurConstraint(field, view, mc, "true");
 762  
                         }
 763  
                 }
 764  
                 
 765  0
         }
 766  
 
 767  
 }