1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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 | |
|
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 = KNSServiceLocator.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(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 | |
|
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 | addScriptToView(view, ruleString); |
285 | |
} |
286 | 0 | } |
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
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 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
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 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
public static void processPrerequisiteConstraint(AttributeField field, PrerequisiteConstraint constraint, View view) { |
409 | 0 | processPrerequisiteConstraint(field, constraint, view, "true"); |
410 | 0 | } |
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | |
|
420 | |
|
421 | |
|
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 | |
|
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | |
|
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
|
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 | |
|
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 | |
|
481 | |
|
482 | |
|
483 | |
|
484 | |
|
485 | |
|
486 | |
|
487 | |
|
488 | |
|
489 | |
|
490 | |
|
491 | |
|
492 | |
private static String getPostrequisiteStatement(AttributeField field, PrerequisiteConstraint constraint, String booleanStatement) { |
493 | |
|
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 | |
|
525 | |
|
526 | |
|
527 | |
|
528 | |
|
529 | |
|
530 | |
|
531 | |
|
532 | |
|
533 | |
|
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 | |
|
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 | |
|
549 | |
|
550 | |
|
551 | |
|
552 | |
|
553 | |
|
554 | |
|
555 | |
|
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 | |
|
578 | 0 | if(constraint.getMin()!=null){ |
579 | 0 | attributePaths.add(constraint.getMin().toString()); |
580 | |
} |
581 | |
else{ |
582 | 0 | attributePaths.add(null); |
583 | |
} |
584 | |
|
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 | |
|
613 | |
|
614 | |
|
615 | |
|
616 | |
|
617 | |
|
618 | |
|
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 | |
|
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 | |
|
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 | |
|
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 | |
|
702 | |
|
703 | |
|
704 | |
|
705 | |
|
706 | |
|
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 | |
|
737 | 0 | addScriptToView(view, ClientValidationUtils.getRegexMethod(field.getValidCharactersConstraint())); |
738 | 0 | field.getControl().addStyleClass("validChar" + methodKey); |
739 | 0 | methodKey++; |
740 | |
} |
741 | |
else { |
742 | |
|
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 | |
} |