| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |  package org.kuali.rice.krad.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.krad.datadictionary.validation.constraint.BaseConstraint; | 
  | 26 |  |  import org.kuali.rice.krad.datadictionary.validation.constraint.CaseConstraint; | 
  | 27 |  |  import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint; | 
  | 28 |  |  import org.kuali.rice.krad.datadictionary.validation.constraint.MustOccurConstraint; | 
  | 29 |  |  import org.kuali.rice.krad.datadictionary.validation.constraint.PrerequisiteConstraint; | 
  | 30 |  |  import org.kuali.rice.krad.datadictionary.validation.constraint.SimpleConstraint; | 
  | 31 |  |  import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint; | 
  | 32 |  |  import org.kuali.rice.krad.datadictionary.validation.constraint.WhenConstraint; | 
  | 33 |  |  import org.kuali.rice.krad.service.KRADServiceLocator; | 
  | 34 |  |  import org.kuali.rice.krad.uif.container.View; | 
  | 35 |  |  import org.kuali.rice.krad.uif.control.TextControl; | 
  | 36 |  |  import org.kuali.rice.krad.uif.field.AttributeField; | 
  | 37 |  |   | 
  | 38 |  |   | 
  | 39 |  |   | 
  | 40 |  |   | 
  | 41 |  |   | 
  | 42 |  |   | 
  | 43 |  |   | 
  | 44 |  |   | 
  | 45 | 0 |  public class ClientValidationUtils { | 
  | 46 |  |           | 
  | 47 | 0 |          private static int methodKey = 0; | 
  | 48 |  |           | 
  | 49 |  |           | 
  | 50 |  |           | 
  | 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 = KRADServiceLocator.getKualiConfigurationService(); | 
  | 67 |  |           | 
  | 68 |  |           | 
  | 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 |  |   | 
  | 137 |  |   | 
  | 138 |  |   | 
  | 139 |  |   | 
  | 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 |  |   | 
  | 163 |  |   | 
  | 164 |  |   | 
  | 165 |  |   | 
  | 166 |  |   | 
  | 167 |  |   | 
  | 168 |  |          public static String getRegexMethod(AttributeField field, ValidCharactersConstraint validCharactersConstraint) { | 
  | 169 | 0 |                  String message = generateMessageFromLabelKey(validCharactersConstraint.getLabelKey()); | 
  | 170 | 0 |                  String key = "validChar-" + field.getBindingInfo().getBindingPath() + methodKey; | 
  | 171 |  |                   | 
  | 172 | 0 |                  return "\njQuery.validator.addMethod(\"" + key | 
  | 173 |  |                                  + "\", function(value, element) {\n" + " return this.optional(element) || " | 
  | 174 |  |                                  + "/" + validCharactersConstraint.getValue() + "/.test(value); " + "}, \"" | 
  | 175 |  |                                  + message + "\");"; | 
  | 176 |  |          } | 
  | 177 |  |           | 
  | 178 |  |   | 
  | 179 |  |           | 
  | 180 |  |   | 
  | 181 |  |   | 
  | 182 |  |   | 
  | 183 |  |   | 
  | 184 |  |   | 
  | 185 |  |   | 
  | 186 |  |   | 
  | 187 |  |   | 
  | 188 |  |   | 
  | 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 |  |                   | 
  | 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 |  |   | 
  | 229 |  |   | 
  | 230 |  |   | 
  | 231 |  |   | 
  | 232 |  |   | 
  | 233 |  |   | 
  | 234 |  |   | 
  | 235 |  |   | 
  | 236 |  |   | 
  | 237 |  |   | 
  | 238 |  |   | 
  | 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 |  |                   | 
  | 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 |  |                                           | 
  | 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 |  |                                           | 
  | 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 |                          addScriptToPage(view, field, ruleString); | 
  | 285 |  |                  } | 
  | 286 | 0 |          } | 
  | 287 |  |           | 
  | 288 |  |   | 
  | 289 |  |   | 
  | 290 |  |           | 
  | 291 |  |   | 
  | 292 |  |   | 
  | 293 |  |   | 
  | 294 |  |   | 
  | 295 |  |   | 
  | 296 |  |          public static void addScriptToPage(View view, AttributeField field, String script) { | 
  | 297 | 0 |          String prefixScript = ""; | 
  | 298 |  |           | 
  | 299 | 0 |          if (field.getOnDocumentReadyScript() != null) { | 
  | 300 | 0 |              prefixScript = field.getOnDocumentReadyScript(); | 
  | 301 |  |          } | 
  | 302 | 0 |          field.setOnDocumentReadyScript(prefixScript + "\n" + "runValidationScript(function(){" + script + "});"); | 
  | 303 | 0 |          } | 
  | 304 |  |           | 
  | 305 |  |           | 
  | 306 |  |   | 
  | 307 |  |   | 
  | 308 |  |   | 
  | 309 |  |   | 
  | 310 |  |   | 
  | 311 |  |   | 
  | 312 |  |          private static List<String> parseOutFields(String statement){ | 
  | 313 | 0 |              List<String> fieldNames = new ArrayList<String>(); | 
  | 314 | 0 |              String[] splits = StringUtils.splitByWholeSeparator(statement, "coerceValue('"); | 
  | 315 | 0 |              for(String s: splits){ | 
  | 316 | 0 |                  String fieldName = StringUtils.substringBefore(s, "'"); | 
  | 317 | 0 |                  fieldNames.add(fieldName); | 
  | 318 |  |              } | 
  | 319 | 0 |              return fieldNames; | 
  | 320 |  |          } | 
  | 321 |  |   | 
  | 322 |  |           | 
  | 323 |  |   | 
  | 324 |  |   | 
  | 325 |  |   | 
  | 326 |  |   | 
  | 327 |  |   | 
  | 328 |  |   | 
  | 329 |  |   | 
  | 330 |  |   | 
  | 331 |  |   | 
  | 332 |  |   | 
  | 333 |  |   | 
  | 334 |  |   | 
  | 335 |  |   | 
  | 336 |  |   | 
  | 337 |  |   | 
  | 338 |  |   | 
  | 339 |  |   | 
  | 340 |  |   | 
  | 341 |  |          @SuppressWarnings("boxing") | 
  | 342 |  |          private static String createRule(AttributeField field, Constraint constraint, String booleanStatement, View view) { | 
  | 343 | 0 |                  String rule = ""; | 
  | 344 | 0 |                  int constraintCount = 0; | 
  | 345 | 0 |                  if (constraint instanceof BaseConstraint && ((BaseConstraint) constraint).getApplyClientSide()) { | 
  | 346 | 0 |                          if (constraint instanceof SimpleConstraint) { | 
  | 347 | 0 |                                  if (((SimpleConstraint) constraint).getRequired()) { | 
  | 348 | 0 |                                          rule = rule + "required: function(element){\nreturn (" + booleanStatement + ");}"; | 
  | 349 |  |                                           | 
  | 350 | 0 |                                          String showIndicatorScript = ""; | 
  | 351 | 0 |                                          for(String checkedField: parseOutFields(booleanStatement)){ | 
  | 352 | 0 |                                              showIndicatorScript = showIndicatorScript +  | 
  | 353 |  |                                                  "setupShowReqIndicatorCheck('"+ checkedField +"', '" + field.getBindingInfo().getBindingPath() + "', " + "function(){\nreturn (" + booleanStatement + ");});\n"; | 
  | 354 |  |                                          } | 
  | 355 | 0 |                                          addScriptToPage(view, field, showIndicatorScript); | 
  | 356 |  |                                           | 
  | 357 | 0 |                                          constraintCount++; | 
  | 358 |  |                                  } | 
  | 359 | 0 |                                  if (((SimpleConstraint) constraint).getMinLength() != null) { | 
  | 360 | 0 |                                          if (constraintCount > 0) { | 
  | 361 | 0 |                                                  rule = rule + ",\n"; | 
  | 362 |  |                                          } | 
  | 363 | 0 |                                          rule = rule + "minLengthConditional: [" + ((SimpleConstraint) constraint).getMinLength() | 
  | 364 |  |                                                          + ", function(){return " + booleanStatement + ";}]"; | 
  | 365 |  |                                  } | 
  | 366 | 0 |                                  if (((SimpleConstraint) constraint).getMaxLength() != null) { | 
  | 367 | 0 |                                          if (constraintCount > 0) { | 
  | 368 | 0 |                                                  rule = rule + ",\n"; | 
  | 369 |  |                                          } | 
  | 370 | 0 |                                          rule = rule + "maxLengthConditional: [" + ((SimpleConstraint) constraint).getMaxLength() | 
  | 371 |  |                                                          + ", function(){return " + booleanStatement + ";}]"; | 
  | 372 |  |                                  } | 
  | 373 |  |   | 
  | 374 | 0 |                                  if (((SimpleConstraint) constraint).getExclusiveMin() != null) { | 
  | 375 | 0 |                                          if (constraintCount > 0) { | 
  | 376 | 0 |                                                  rule = rule + ",\n"; | 
  | 377 |  |                                          } | 
  | 378 | 0 |                                          rule = rule + "minExclusive: [" + ((SimpleConstraint) constraint).getExclusiveMin() | 
  | 379 |  |                                                          + ", function(){return " + booleanStatement + ";}]"; | 
  | 380 |  |                                  } | 
  | 381 |  |   | 
  | 382 | 0 |                                  if (((SimpleConstraint) constraint).getInclusiveMax() != null) { | 
  | 383 | 0 |                                          if (constraintCount > 0) { | 
  | 384 | 0 |                                                  rule = rule + ",\n"; | 
  | 385 |  |                                          } | 
  | 386 | 0 |                                          rule = rule + "maxInclusive: [" + ((SimpleConstraint) constraint).getInclusiveMax() | 
  | 387 |  |                                                          + ", function(){return " + booleanStatement + ";}]"; | 
  | 388 |  |                                  } | 
  | 389 |  |   | 
  | 390 | 0 |                                  rule = "jq('[name=\"" + field.getBindingInfo().getBindingPath() + "\"]').rules(\"add\", {" + rule + "\n});"; | 
  | 391 |  |                          } | 
  | 392 | 0 |                          else if (constraint instanceof ValidCharactersConstraint) { | 
  | 393 | 0 |                                  String regexMethod = ""; | 
  | 394 | 0 |                                  String methodName = ""; | 
  | 395 | 0 |                                  if(StringUtils.isNotEmpty(((ValidCharactersConstraint)constraint).getValue())) { | 
  | 396 | 0 |                                          regexMethod = ClientValidationUtils.getRegexMethod(field, (ValidCharactersConstraint) constraint) + "\n"; | 
  | 397 | 0 |                                          methodName = "validChar-" + field.getBindingInfo().getBindingPath() + methodKey; | 
  | 398 | 0 |                                          methodKey++; | 
  | 399 |  |                                  } | 
  | 400 |  |                                  else { | 
  | 401 | 0 |                                          if(StringUtils.isNotEmpty(((ValidCharactersConstraint)constraint).getLabelKey())){ | 
  | 402 | 0 |                                                  methodName = ((ValidCharactersConstraint)constraint).getLabelKey(); | 
  | 403 |  |                                          } | 
  | 404 |  |                                  } | 
  | 405 | 0 |                                  if (StringUtils.isNotEmpty(methodName)) { | 
  | 406 | 0 |                                          rule = regexMethod + "jq('[name=\"" + field.getBindingInfo().getBindingPath() + "\"]').rules(\"add\", {\n\"" + methodName | 
  | 407 |  |                                                          + "\" : function(element){return (" + booleanStatement + ");}\n});"; | 
  | 408 |  |                                  } | 
  | 409 | 0 |                          } | 
  | 410 | 0 |                          else if (constraint instanceof PrerequisiteConstraint) { | 
  | 411 | 0 |                                  processPrerequisiteConstraint(field, (PrerequisiteConstraint) constraint, view, booleanStatement); | 
  | 412 |  |                          } | 
  | 413 | 0 |                          else if (constraint instanceof CaseConstraint) { | 
  | 414 | 0 |                                  processCaseConstraint(field, view, (CaseConstraint) constraint, booleanStatement); | 
  | 415 |  |                          } | 
  | 416 | 0 |                          else if (constraint instanceof MustOccurConstraint) { | 
  | 417 | 0 |                                  processMustOccurConstraint(field, view, (MustOccurConstraint) constraint, booleanStatement); | 
  | 418 |  |                          } | 
  | 419 |  |                  } | 
  | 420 | 0 |                  return rule; | 
  | 421 |  |          } | 
  | 422 |  |           | 
  | 423 |  |   | 
  | 424 |  |   | 
  | 425 |  |           | 
  | 426 |  |   | 
  | 427 |  |   | 
  | 428 |  |   | 
  | 429 |  |   | 
  | 430 |  |   | 
  | 431 |  |   | 
  | 432 |  |   | 
  | 433 |  |          public static void processPrerequisiteConstraint(AttributeField field, PrerequisiteConstraint constraint, View view) { | 
  | 434 | 0 |                  processPrerequisiteConstraint(field, constraint, view, "true"); | 
  | 435 | 0 |          } | 
  | 436 |  |   | 
  | 437 |  |           | 
  | 438 |  |   | 
  | 439 |  |   | 
  | 440 |  |   | 
  | 441 |  |   | 
  | 442 |  |   | 
  | 443 |  |   | 
  | 444 |  |   | 
  | 445 |  |   | 
  | 446 |  |   | 
  | 447 |  |   | 
  | 448 |  |          public static void processPrerequisiteConstraint(AttributeField field, PrerequisiteConstraint constraint, View view, String booleanStatement) { | 
  | 449 | 0 |                  if (constraint != null && constraint.getApplyClientSide().booleanValue()) { | 
  | 450 | 0 |                          addScriptToPage(view, field, getPrerequisiteStatement(field, view, constraint, booleanStatement) | 
  | 451 |  |                                          + getPostrequisiteStatement(field, constraint, booleanStatement)); | 
  | 452 |  |                   | 
  | 453 | 0 |                  String showIndicatorScript = "setupShowReqIndicatorCheck('"+ field.getBindingInfo().getBindingPath() +"', '" + constraint.getAttributePath() + "', " + "function(){\nreturn (coerceValue('" + field.getBindingInfo().getBindingPath() + "') && " + booleanStatement + ");});\n"; | 
  | 454 | 0 |                  addScriptToPage(view, field, showIndicatorScript); | 
  | 455 |  |                  } | 
  | 456 | 0 |          } | 
  | 457 |  |   | 
  | 458 |  |           | 
  | 459 |  |   | 
  | 460 |  |   | 
  | 461 |  |   | 
  | 462 |  |   | 
  | 463 |  |   | 
  | 464 |  |   | 
  | 465 |  |   | 
  | 466 |  |   | 
  | 467 |  |   | 
  | 468 |  |   | 
  | 469 |  |   | 
  | 470 |  |   | 
  | 471 |  |   | 
  | 472 |  |          private static String getPrerequisiteStatement(AttributeField field, View view, PrerequisiteConstraint constraint, String booleanStatement) { | 
  | 473 | 0 |                  methodKey++; | 
  | 474 | 0 |                  String message = ""; | 
  | 475 | 0 |                  if(StringUtils.isEmpty(constraint.getLabelKey())){ | 
  | 476 | 0 |                          message = configService.getPropertyString(VALIDATION_MSG_KEY_PREFIX + "prerequisite"); | 
  | 477 |  |                  } | 
  | 478 |  |                  else{ | 
  | 479 | 0 |                          message = generateMessageFromLabelKey(constraint.getLabelKey()); | 
  | 480 |  |                  } | 
  | 481 | 0 |                  if(StringUtils.isEmpty(message)){ | 
  | 482 | 0 |                          message = "prerequisite - No message"; | 
  | 483 |  |                  } | 
  | 484 |  |                  else{ | 
  | 485 | 0 |                          AttributeField requiredField = view.getViewIndex().getAttributeFieldByPath(constraint.getAttributePath()); | 
  | 486 | 0 |                          if(requiredField != null && StringUtils.isNotEmpty(requiredField.getLabel())){ | 
  | 487 | 0 |                                  message = MessageFormat.format(message, requiredField.getLabel()); | 
  | 488 |  |                          } | 
  | 489 |  |                          else{ | 
  | 490 | 0 |                                  message = MessageFormat.format(message, configService.getPropertyString(GENERIC_FIELD_MSG_KEY)); | 
  | 491 |  |                          } | 
  | 492 |  |                  } | 
  | 493 |  |                   | 
  | 494 |  |                   | 
  | 495 | 0 |                  String dependsClass = "dependsOn-" + constraint.getAttributePath(); | 
  | 496 | 0 |                  String methodName = "prConstraint-" + field.getBindingInfo().getBindingPath()+ methodKey; | 
  | 497 | 0 |                  String addClass = "jq('[name=\""+ field.getBindingInfo().getBindingPath() + "\"]').addClass('" + dependsClass + "');\n" + | 
  | 498 |  |                          "jq('[name=\""+ field.getBindingInfo().getBindingPath() + "\"]').addClass('" + methodName + "');\n"; | 
  | 499 | 0 |                  String method = "\njQuery.validator.addMethod(\""+ methodName +"\", function(value, element) {\n" + | 
  | 500 |  |                          " if(" + booleanStatement + "){ return (this.optional(element) || (coerceValue('" + constraint.getAttributePath() + "')));}else{return true;} " + | 
  | 501 |  |                          "}, \"" + message + "\");"; | 
  | 502 |  |                   | 
  | 503 | 0 |                  String ifStatement = "if(occursBefore('" + constraint.getAttributePath() + "','" + field.getBindingInfo().getBindingPath() +  | 
  | 504 |  |                  "')){" + addClass + method + "}"; | 
  | 505 | 0 |                  return ifStatement; | 
  | 506 |  |          } | 
  | 507 |  |   | 
  | 508 |  |           | 
  | 509 |  |   | 
  | 510 |  |   | 
  | 511 |  |   | 
  | 512 |  |   | 
  | 513 |  |   | 
  | 514 |  |   | 
  | 515 |  |   | 
  | 516 |  |   | 
  | 517 |  |   | 
  | 518 |  |   | 
  | 519 |  |   | 
  | 520 |  |   | 
  | 521 |  |          private static String getPostrequisiteStatement(AttributeField field, PrerequisiteConstraint constraint, String booleanStatement) { | 
  | 522 |  |                   | 
  | 523 | 0 |                  String message = ""; | 
  | 524 | 0 |                  if(StringUtils.isEmpty(constraint.getLabelKey())){ | 
  | 525 | 0 |                          message = configService.getPropertyString(VALIDATION_MSG_KEY_PREFIX + "postrequisite"); | 
  | 526 |  |                  } | 
  | 527 |  |                  else{ | 
  | 528 | 0 |                          message = generateMessageFromLabelKey(constraint.getLabelKey()); | 
  | 529 |  |                  } | 
  | 530 |  |                   | 
  | 531 | 0 |                  if(StringUtils.isEmpty(constraint.getLabelKey())){ | 
  | 532 | 0 |                          if(StringUtils.isNotEmpty(field.getLabel())){ | 
  | 533 | 0 |                                  message = MessageFormat.format(message, field.getLabel()); | 
  | 534 |  |                          } | 
  | 535 |  |                          else{ | 
  | 536 | 0 |                                  message = MessageFormat.format(message, configService.getPropertyString(GENERIC_FIELD_MSG_KEY)); | 
  | 537 |  |                          } | 
  | 538 |  |                           | 
  | 539 |  |                  } | 
  | 540 |  |                   | 
  | 541 | 0 |                  String function = "function(element){\n" + | 
  | 542 |  |                          "return (coerceValue('"+ field.getBindingInfo().getBindingPath() + "') && " + booleanStatement + ");}"; | 
  | 543 | 0 |                  String postStatement = "\nelse if(occursBefore('" + field.getBindingInfo().getBindingPath() + "','" + constraint.getAttributePath() +  | 
  | 544 |  |                          "')){\njq('[name=\""+ constraint.getAttributePath() +  | 
  | 545 |  |                          "\"]').rules(\"add\", { required: \n" + function  | 
  | 546 |  |                          + ", \nmessages: {\nrequired: \""+ message +"\"}});}\n"; | 
  | 547 |  |                   | 
  | 548 | 0 |                  return postStatement; | 
  | 549 |  |   | 
  | 550 |  |          } | 
  | 551 |  |   | 
  | 552 |  |           | 
  | 553 |  |   | 
  | 554 |  |   | 
  | 555 |  |   | 
  | 556 |  |   | 
  | 557 |  |   | 
  | 558 |  |   | 
  | 559 |  |   | 
  | 560 |  |   | 
  | 561 |  |   | 
  | 562 |  |   | 
  | 563 |  |   | 
  | 564 |  |          public static void processMustOccurConstraint(AttributeField field, View view, MustOccurConstraint mc, String booleanStatement) { | 
  | 565 | 0 |                  methodKey++; | 
  | 566 | 0 |                  mustOccursPathNames = new ArrayList<List<String>>(); | 
  | 567 |  |                   | 
  | 568 | 0 |                  String methodName = "moConstraint-" + field.getBindingInfo().getBindingPath() + methodKey; | 
  | 569 | 0 |                  String method = "\njQuery.validator.addMethod(\""+ methodName +"\", function(value, element) {\n" + | 
  | 570 |  |                  " if(" + booleanStatement + "){return (this.optional(element) || ("+ getMustOccurStatement(field, mc) + "));}else{return true;}" + | 
  | 571 |  |                  "}, \"" + getMustOccursMessage(view, mc) +"\");"; | 
  | 572 | 0 |                  String rule = method + "jq('[name=\""+ field.getBindingInfo().getBindingPath() + "\"]').rules(\"add\", {\n\"" + methodName + "\": function(element){return (" + booleanStatement + ");}\n});"; | 
  | 573 | 0 |                  addScriptToPage(view, field, rule); | 
  | 574 | 0 |          } | 
  | 575 |  |   | 
  | 576 |  |           | 
  | 577 |  |   | 
  | 578 |  |   | 
  | 579 |  |   | 
  | 580 |  |   | 
  | 581 |  |   | 
  | 582 |  |   | 
  | 583 |  |   | 
  | 584 |  |   | 
  | 585 |  |   | 
  | 586 |  |          @SuppressWarnings("boxing") | 
  | 587 |  |          private static String getMustOccurStatement(AttributeField field, MustOccurConstraint constraint) { | 
  | 588 | 0 |                  String statement = ""; | 
  | 589 | 0 |                  List<String> attributePaths = new ArrayList<String>(); | 
  | 590 | 0 |                  if (constraint != null && constraint.getApplyClientSide()) { | 
  | 591 | 0 |                          String array = "["; | 
  | 592 | 0 |                          if (constraint.getPrerequisiteConstraints() != null) { | 
  | 593 | 0 |                                  for (int i = 0; i < constraint.getPrerequisiteConstraints().size(); i++) { | 
  | 594 | 0 |                                          field.getControl().addStyleClass("dependsOn-" | 
  | 595 |  |                                                          + constraint.getPrerequisiteConstraints().get(i).getAttributePath()); | 
  | 596 | 0 |                                          array = array + "'" + constraint.getPrerequisiteConstraints().get(i).getAttributePath() + "'"; | 
  | 597 | 0 |                                          attributePaths.add(constraint.getPrerequisiteConstraints().get(i).getAttributePath()); | 
  | 598 | 0 |                                          if (i + 1 != constraint.getPrerequisiteConstraints().size()) { | 
  | 599 | 0 |                                                  array = array + ","; | 
  | 600 |  |                                          } | 
  | 601 |  |   | 
  | 602 |  |                                  } | 
  | 603 |  |                          } | 
  | 604 | 0 |                          array = array + "]"; | 
  | 605 | 0 |                          statement = "mustOccurTotal(" + array + ", " + constraint.getMin() + ", " + constraint.getMax() + ")"; | 
  | 606 |  |                           | 
  | 607 | 0 |                          if(constraint.getMin()!=null){ | 
  | 608 | 0 |                                  attributePaths.add(constraint.getMin().toString()); | 
  | 609 |  |                          } | 
  | 610 |  |                          else{ | 
  | 611 | 0 |                                  attributePaths.add(null); | 
  | 612 |  |                          } | 
  | 613 |  |                           | 
  | 614 | 0 |                          if(constraint.getMax()!=null){ | 
  | 615 | 0 |                                  attributePaths.add(constraint.getMax().toString()); | 
  | 616 |  |                          } | 
  | 617 |  |                          else{ | 
  | 618 | 0 |                                  attributePaths.add(null); | 
  | 619 |  |                          } | 
  | 620 |  |                           | 
  | 621 | 0 |                          mustOccursPathNames.add(attributePaths); | 
  | 622 | 0 |                          if(StringUtils.isEmpty(statement)){ | 
  | 623 | 0 |                                  statement = "0"; | 
  | 624 |  |                          } | 
  | 625 | 0 |                          if (constraint.getMustOccurConstraints() != null) { | 
  | 626 | 0 |                                  for (MustOccurConstraint mc : constraint.getMustOccurConstraints()) { | 
  | 627 | 0 |                                          statement = "mustOccurCheck(" + statement + " + " + getMustOccurStatement(field, mc) + | 
  | 628 |  |                                                  ", " + constraint.getMin() + ", " + constraint.getMax() + ")"; | 
  | 629 |  |                                  } | 
  | 630 |  |                          } | 
  | 631 |  |                          else{ | 
  | 632 | 0 |                                  statement = "mustOccurCheck(" + statement + | 
  | 633 |  |                                  ", " + constraint.getMin() + ", " + constraint.getMax() + ")"; | 
  | 634 |  |                          } | 
  | 635 |  |                  } | 
  | 636 | 0 |                  return statement; | 
  | 637 |  |          } | 
  | 638 |  |   | 
  | 639 |  |           | 
  | 640 |  |           | 
  | 641 |  |   | 
  | 642 |  |   | 
  | 643 |  |   | 
  | 644 |  |   | 
  | 645 |  |   | 
  | 646 |  |   | 
  | 647 |  |   | 
  | 648 |  |   | 
  | 649 |  |          private static String getMustOccursMessage(View view, MustOccurConstraint constraint){ | 
  | 650 | 0 |                  String message = ""; | 
  | 651 | 0 |                  if(StringUtils.isNotEmpty(constraint.getLabelKey())){ | 
  | 652 | 0 |                          message = generateMessageFromLabelKey(constraint.getLabelKey()); | 
  | 653 |  |                  } | 
  | 654 |  |                  else{ | 
  | 655 | 0 |                          String and = configService.getPropertyString(AND_MSG_KEY); | 
  | 656 | 0 |                          String all = configService.getPropertyString(ALL_MSG_KEY); | 
  | 657 | 0 |                          String atMost = configService.getPropertyString(ATMOST_MSG_KEY); | 
  | 658 | 0 |                          String genericLabel = configService.getPropertyString(GENERIC_FIELD_MSG_KEY); | 
  | 659 | 0 |                          String mustOccursMsg = configService.getPropertyString(VALIDATION_MSG_KEY_PREFIX + MUSTOCCURS_MSG_KEY); | 
  | 660 |  |                           | 
  | 661 | 0 |                          String statement=""; | 
  | 662 | 0 |                          for(int i=0; i< mustOccursPathNames.size(); i++){ | 
  | 663 | 0 |                                  String andedString = ""; | 
  | 664 |  |                                   | 
  | 665 | 0 |                                  List<String> paths = mustOccursPathNames.get(i); | 
  | 666 | 0 |                                  if(!paths.isEmpty()){ | 
  | 667 |  |                                           | 
  | 668 | 0 |                                          String min = paths.get(paths.size()-2); | 
  | 669 | 0 |                                          String max = paths.get(paths.size()-1); | 
  | 670 | 0 |                                          for(int j=0; j<paths.size()-2;j++){ | 
  | 671 | 0 |                                                  AttributeField field = view.getViewIndex().getAttributeFieldByPath(paths.get(j).trim()); | 
  | 672 | 0 |                                                  String label = genericLabel; | 
  | 673 | 0 |                                                  if(field != null && StringUtils.isNotEmpty(field.getLabel())){ | 
  | 674 | 0 |                                                          label = field.getLabel(); | 
  | 675 |  |                                                  } | 
  | 676 | 0 |                                                  if(min.equals(max)){ | 
  | 677 | 0 |                                                          if(j==0){ | 
  | 678 | 0 |                                                                  andedString = label; | 
  | 679 |  |                                                          } | 
  | 680 | 0 |                                                          else if(j==paths.size()-3){ | 
  | 681 | 0 |                                                                  andedString = andedString + " " + and + " " + label; | 
  | 682 |  |                                                          } | 
  | 683 |  |                                                          else{ | 
  | 684 | 0 |                                                                  andedString = andedString + ", " + label; | 
  | 685 |  |                                                          } | 
  | 686 |  |                                                  } | 
  | 687 |  |                                                  else{ | 
  | 688 | 0 |                                                          andedString = andedString + "<li>" + label + "</li>"; | 
  | 689 |  |                                                  } | 
  | 690 |  |                                          } | 
  | 691 | 0 |                                          if(min.equals(max)){ | 
  | 692 | 0 |                                                  andedString = "<li>" + andedString + "</li>"; | 
  | 693 |  |                                          } | 
  | 694 | 0 |                                          andedString="<ul>" + andedString + "</ul>"; | 
  | 695 |  |                                   | 
  | 696 | 0 |                                          if(StringUtils.isNotEmpty(min) && StringUtils.isNotEmpty(max) && !min.equals(max)){ | 
  | 697 | 0 |                                                  andedString = MessageFormat.format(mustOccursMsg, min + "-" + max) + "<br/>" +andedString; | 
  | 698 |  |                                          } | 
  | 699 | 0 |                                          else if(StringUtils.isNotEmpty(min) && StringUtils.isNotEmpty(max) && min.equals(max) && i==0){ | 
  | 700 | 0 |                                                  andedString = MessageFormat.format(mustOccursMsg, all) + "<br/>" +andedString; | 
  | 701 |  |                                          } | 
  | 702 | 0 |                                          else if(StringUtils.isNotEmpty(min) && StringUtils.isNotEmpty(max) && min.equals(max) && i!=0){ | 
  | 703 |  |                                                   | 
  | 704 |  |                                          } | 
  | 705 | 0 |                                          else if(StringUtils.isNotEmpty(min)){ | 
  | 706 | 0 |                                                  andedString = MessageFormat.format(mustOccursMsg, min) + "<br/>" +andedString; | 
  | 707 |  |                                          } | 
  | 708 | 0 |                                          else if(StringUtils.isNotEmpty(max)){ | 
  | 709 | 0 |                                                  andedString = MessageFormat.format(mustOccursMsg, atMost + " " + max) + "<br/>" +andedString; | 
  | 710 |  |                                          } | 
  | 711 |  |                                  } | 
  | 712 | 0 |                                  if(StringUtils.isNotEmpty(andedString)){ | 
  | 713 | 0 |                                          if(i==0){ | 
  | 714 | 0 |                                                  statement = andedString; | 
  | 715 |  |                                          } | 
  | 716 |  |                                          else{ | 
  | 717 | 0 |                                                  statement = statement + andedString; | 
  | 718 |  |                                          } | 
  | 719 |  |                                  } | 
  | 720 |  |                          } | 
  | 721 | 0 |                          if(StringUtils.isNotEmpty(statement)){ | 
  | 722 | 0 |                                  message = statement; | 
  | 723 |  |                          } | 
  | 724 |  |                  } | 
  | 725 |  |                   | 
  | 726 | 0 |                  return message; | 
  | 727 |  |          } | 
  | 728 |  |   | 
  | 729 |  |           | 
  | 730 |  |   | 
  | 731 |  |   | 
  | 732 |  |   | 
  | 733 |  |   | 
  | 734 |  |   | 
  | 735 |  |   | 
  | 736 |  |   | 
  | 737 |  |          @SuppressWarnings("boxing") | 
  | 738 |  |          public static void processAndApplyConstraints(AttributeField field, View view) { | 
  | 739 | 0 |                  methodKey = 0; | 
  | 740 | 0 |                  if ((field.getRequired() != null) && (field.getRequired().booleanValue())) { | 
  | 741 | 0 |                          field.getControl().addStyleClass("required"); | 
  | 742 |  |                  } | 
  | 743 |  |   | 
  | 744 | 0 |                  if (field.getExclusiveMin() != null) { | 
  | 745 | 0 |                          if (field.getControl() instanceof TextControl && ((TextControl) field.getControl()).getDatePicker() != null) { | 
  | 746 | 0 |                                  ((TextControl) field.getControl()).getDatePicker().getComponentOptions().put("minDate", field.getExclusiveMin()); | 
  | 747 |  |                          } | 
  | 748 |  |                          else{ | 
  | 749 | 0 |                                  String rule = "jq('[name=\""+ field.getBindingInfo().getBindingPath() + "\"]').rules(\"add\", {\n minExclusive: ["+ field.getExclusiveMin() + "]});"; | 
  | 750 | 0 |                                  addScriptToPage(view, field, rule); | 
  | 751 |  |                          } | 
  | 752 |  |                  } | 
  | 753 |  |   | 
  | 754 | 0 |                  if (field.getInclusiveMax() != null) { | 
  | 755 | 0 |                          if (field.getControl() instanceof TextControl && ((TextControl) field.getControl()).getDatePicker() != null) { | 
  | 756 | 0 |                                  ((TextControl) field.getControl()).getDatePicker().getComponentOptions().put("maxDate", field.getInclusiveMax()); | 
  | 757 |  |                          } | 
  | 758 |  |                          else{ | 
  | 759 | 0 |                                  String rule = "jq('[name=\""+ field.getBindingInfo().getBindingPath() + "\"]').rules(\"add\", {\n maxInclusive: ["+ field.getInclusiveMax() + "]});"; | 
  | 760 | 0 |                                  addScriptToPage(view, field, rule); | 
  | 761 |  |                          } | 
  | 762 |  |                  } | 
  | 763 |  |   | 
  | 764 | 0 |                  if (field.getValidCharactersConstraint() != null && field.getValidCharactersConstraint().getApplyClientSide()) { | 
  | 765 | 0 |                          if(StringUtils.isNotEmpty(field.getValidCharactersConstraint().getValue())) { | 
  | 766 |  |                                   | 
  | 767 | 0 |                                  addScriptToPage(view, field, ClientValidationUtils.getRegexMethod(field, field.getValidCharactersConstraint())); | 
  | 768 | 0 |                                  field.getControl().addStyleClass("validChar-" + field.getBindingInfo().getBindingPath()+ methodKey); | 
  | 769 | 0 |                                  methodKey++; | 
  | 770 |  |                          } | 
  | 771 |  |                          else { | 
  | 772 |  |                                   | 
  | 773 | 0 |                                  if(StringUtils.isNotEmpty(field.getValidCharactersConstraint().getLabelKey())){ | 
  | 774 | 0 |                                          field.getControl().addStyleClass(field.getValidCharactersConstraint().getLabelKey()); | 
  | 775 |  |                                  } | 
  | 776 |  |                          } | 
  | 777 |  |                  } | 
  | 778 |  |   | 
  | 779 | 0 |                  if (field.getCaseConstraint() != null && field.getCaseConstraint().getApplyClientSide()) { | 
  | 780 | 0 |                          processCaseConstraint(field, view, field.getCaseConstraint(), null); | 
  | 781 |  |                  } | 
  | 782 |  |   | 
  | 783 | 0 |                  if (field.getDependencyConstraints() != null) { | 
  | 784 | 0 |                          for (PrerequisiteConstraint prc : field.getDependencyConstraints()) { | 
  | 785 | 0 |                                  processPrerequisiteConstraint(field, prc, view); | 
  | 786 |  |                          } | 
  | 787 |  |                  } | 
  | 788 |  |   | 
  | 789 | 0 |                  if (field.getMustOccurConstraints() != null) { | 
  | 790 | 0 |                          for (MustOccurConstraint mc : field.getMustOccurConstraints()) { | 
  | 791 | 0 |                                  processMustOccurConstraint(field, view, mc, "true"); | 
  | 792 |  |                          } | 
  | 793 |  |                  } | 
  | 794 |  |                   | 
  | 795 | 0 |          } | 
  | 796 |  |   | 
  | 797 |  |  } |