1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.trinidad.validator;
20
21 import javax.el.ValueExpression;
22
23 import javax.faces.application.FacesMessage;
24 import javax.faces.component.UIComponent;
25 import javax.faces.context.FacesContext;
26 import javax.faces.el.ValueBinding;
27
28
29 import javax.faces.validator.Validator;
30 import javax.faces.validator.ValidatorException;
31
32 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
33 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFValidator;
34 import org.apache.myfaces.trinidad.bean.FacesBean;
35 import org.apache.myfaces.trinidad.bean.PropertyKey;
36 import org.apache.myfaces.trinidad.util.ComponentUtils;
37 import org.apache.myfaces.trinidad.util.IntegerUtils;
38 import org.apache.myfaces.trinidad.util.MessageFactory;
39
40
41
42
43
44 @JSFValidator(configExcluded=true)
45 public class LongRangeValidator extends javax.faces.validator.LongRangeValidator
46 {
47
48 public static final String VALIDATOR_ID = "org.apache.myfaces.trinidad.LongRange";
49
50
51
52
53
54
55
56
57
58 public static final String MAXIMUM_MESSAGE_ID =
59 "org.apache.myfaces.trinidad.validator.LongRangeValidator.MAXIMUM";
60
61
62
63
64
65
66
67
68 public static final String MINIMUM_MESSAGE_ID =
69 "org.apache.myfaces.trinidad.validator.LongRangeValidator.MINIMUM";
70
71
72
73
74
75
76
77
78
79
80
81 public static final String NOT_IN_RANGE_MESSAGE_ID =
82 "org.apache.myfaces.trinidad.validator.LongRangeValidator.NOT_IN_RANGE";
83
84
85
86
87
88 public static final String CONVERT_MESSAGE_ID =
89 "org.apache.myfaces.trinidad.convert.LongConverter.CONVERT";
90
91
92
93
94
95 public LongRangeValidator()
96 {
97 super();
98 }
99
100
101
102
103
104
105
106 public LongRangeValidator(long maximum)
107 {
108 super(maximum);
109 }
110
111
112
113
114
115
116
117
118
119 public LongRangeValidator(long maximum, long minimum)
120 {
121 super(maximum, minimum);
122 }
123
124
125
126
127
128
129 @JSFProperty
130 @Override
131 public long getMaximum()
132 {
133 Object maxLong = _facesBean.getProperty(_MAXIMUM_KEY);
134 if(maxLong == null)
135 maxLong = _MAXIMUM_KEY.getDefault();
136 return ComponentUtils.resolveLong(maxLong);
137 }
138
139
140
141
142
143
144
145 @Override
146 public void setMaximum(long maximum)
147 {
148 _facesBean.setProperty(_MAXIMUM_KEY, Long.valueOf(maximum));
149 }
150
151
152
153
154
155
156
157 @JSFProperty
158 @Override
159 public long getMinimum()
160 {
161 Object minLong = _facesBean.getProperty(_MINIMUM_KEY);
162 if(minLong == null)
163 minLong = _MINIMUM_KEY.getDefault();
164 return ComponentUtils.resolveLong(minLong);
165 }
166
167
168
169
170
171
172
173 @Override
174 public void setMinimum(long minimum)
175 {
176 _facesBean.setProperty(_MINIMUM_KEY, Long.valueOf(minimum));
177 }
178
179
180
181
182
183
184
185 public void setMessageDetailMaximum(String maximumMessageDetail)
186 {
187 _facesBean.setProperty(_MAXIMUM_MESSAGE_DETAIL_KEY, maximumMessageDetail);
188 }
189
190
191
192
193
194
195
196 @JSFProperty
197 public String getMessageDetailMaximum()
198 {
199 Object maxMsgDet = _facesBean.getProperty(_MAXIMUM_MESSAGE_DETAIL_KEY);
200 return ComponentUtils.resolveString(maxMsgDet);
201 }
202
203
204
205
206
207
208
209
210 public void setMessageDetailMinimum(String minimumMessageDetail)
211 {
212 _facesBean.setProperty(_MINIMUM_MESSAGE_DETAIL_KEY, minimumMessageDetail);
213 }
214
215
216
217
218
219
220
221 @JSFProperty
222 public String getMessageDetailMinimum()
223 {
224 Object minMsgDet = _facesBean.getProperty(_MINIMUM_MESSAGE_DETAIL_KEY);
225 return ComponentUtils.resolveString(minMsgDet);
226 }
227
228
229
230
231
232
233
234
235 public void setMessageDetailNotInRange(String notInRangeMessageDetail)
236 {
237 _facesBean.setProperty(_NOT_IN_RANGE_MESSAGE_DETAIL_KEY, notInRangeMessageDetail);
238 }
239
240
241
242
243
244
245
246
247 @JSFProperty
248 public String getMessageDetailNotInRange()
249 {
250 Object notInRngMsg = _facesBean.getProperty(_NOT_IN_RANGE_MESSAGE_DETAIL_KEY);
251 return ComponentUtils.resolveString(notInRngMsg);
252 }
253
254
255
256
257
258
259 public void setHintMaximum(String hintMaximum)
260 {
261 _facesBean.setProperty(_HINT_MAXIMUM_KEY, hintMaximum);
262 }
263
264
265
266
267
268
269 @JSFProperty(tagExcluded=true)
270 public String getHintMaximum()
271 {
272 Object obj = _facesBean.getProperty(_HINT_MAXIMUM_KEY);
273 return ComponentUtils.resolveString(obj);
274 }
275
276
277
278
279
280
281 public void setHintMinimum(String hintMinimum)
282 {
283 _facesBean.setProperty(_HINT_MINIMUM_KEY, hintMinimum);
284 }
285
286
287
288
289
290
291 @JSFProperty(tagExcluded=true)
292 public String getHintMinimum()
293 {
294 Object obj = _facesBean.getProperty(_HINT_MINIMUM_KEY);
295 return ComponentUtils.resolveString(obj);
296 }
297
298
299
300
301
302
303 public void setHintNotInRange(String hintNotInRange)
304 {
305 _facesBean.setProperty(_HINT_NOT_IN_RANGE, hintNotInRange);
306 }
307
308
309
310
311
312
313 public String getHintNotInRange()
314 {
315 Object obj = _facesBean.getProperty(_HINT_NOT_IN_RANGE);
316 return ComponentUtils.resolveString(obj);
317 }
318
319 @Override
320 public void validate(
321 FacesContext context,
322 UIComponent component,
323 Object value
324 ) throws ValidatorException
325 {
326 if (isDisabled())
327 return;
328
329 if ((context == null) || (component == null))
330 {
331 throw new NullPointerException();
332 }
333
334 if (value == null)
335 {
336 return;
337 }
338
339 try
340 {
341 long longValue = _convertValueToLong(value);
342 long min = getMinimum();
343 long max = getMaximum();
344
345 if(isMaximumSet() && isMinimumSet())
346 {
347 if(longValue<min || longValue>max)
348 {
349 throw new ValidatorException(
350 _getNotInRangeMessage(context, component, value, min, max));
351 }
352 }
353
354 if(isMaximumSet())
355 {
356 if (longValue > max)
357 {
358 throw new ValidatorException(
359 _getMaximumMessage(context, component, value, IntegerUtils.getString(max)));
360 }
361 }
362
363 if(isMinimumSet())
364 {
365 if (longValue < min)
366 {
367 throw new ValidatorException(
368 _getMinimumMessage(context, component, value, IntegerUtils.getString(min)));
369 }
370 }
371 }
372 catch (NumberFormatException nfe)
373 {
374 FacesMessage msg = _getNotCorrectType(context);
375 throw new ValidatorException(msg);
376 }
377 }
378
379
380 @Override
381 public Object saveState(FacesContext context)
382 {
383 return _facesBean.saveState(context);
384 }
385
386
387 @Override
388 public void restoreState(FacesContext context, Object state)
389 {
390 _facesBean.restoreState(context, state);
391 }
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406 public void setValueExpression(String name, ValueExpression expression)
407 {
408 ValidatorUtils.setValueExpression(_facesBean, name, expression) ;
409 }
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424 public ValueExpression getValueExpression(String name)
425 {
426 return ValidatorUtils.getValueExpression(_facesBean, name);
427 }
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444 public void setValueBinding(String name, ValueBinding binding)
445 {
446 ValidatorUtils.setValueBinding(_facesBean, name, binding) ;
447 }
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462 public ValueBinding getValueBinding(String name)
463 {
464 return ValidatorUtils.getValueBinding(_facesBean, name);
465 }
466
467 @JSFProperty(istransient=true, tagExcluded=true)
468 @Override
469 public boolean isTransient()
470 {
471 return (_transientValue);
472 }
473
474 @Override
475 public void setTransient(boolean transientValue)
476 {
477 _transientValue = transientValue;
478 }
479
480 @Override
481 public boolean equals(Object otherObj)
482 {
483 if (!(otherObj instanceof LongRangeValidator))
484 {
485 return false;
486 }
487
488 LongRangeValidator other = (LongRangeValidator) otherObj;
489
490 return ((this.getMaximum() == other.getMaximum())
491 && (this.getMinimum() == other.getMinimum())
492 && (this.isMaximumSet() == other.isMaximumSet())
493 && (this.isMinimumSet() == other.isMinimumSet())
494 && (this.isDisabled() == other.isDisabled())
495 && (this.isTransient() == other.isTransient()));
496 }
497
498 @Override
499 public int hashCode()
500 {
501 int result = 17;
502 Object maxMsgDet = getMessageDetailMaximum();
503 Object minMsgDet = getMessageDetailMinimum();
504 Object notInRangeMsgDet = getMessageDetailNotInRange();
505
506 result = 37 * result + (isDisabled() ? 1 : 0);
507 result = 37 * result + (isTransient() ? 0 : 1);
508 result = 37 * result + (isMaximumSet() ? 0 : 1);
509 result = 37 * result + (isMinimumSet() ? 0 : 1);
510 result = 37 * result + Long.valueOf(getMinimum()).hashCode();
511 result = 37 * result + Long.valueOf(getMaximum()).hashCode();
512 result = 37 * result + ( maxMsgDet == null ? 0 : maxMsgDet.hashCode());
513 result = 37 * result + ( minMsgDet == null ? 0 : minMsgDet.hashCode());
514 result = 37 * result + ( notInRangeMsgDet == null ? 0 : notInRangeMsgDet.hashCode());
515
516 return result;
517 }
518
519
520
521
522
523 public void setDisabled(boolean isDisabled)
524 {
525 _facesBean.setProperty(_DISABLED_KEY, Boolean.valueOf(isDisabled));
526 }
527
528
529
530
531
532 public boolean isDisabled()
533 {
534 Boolean disabled = (Boolean) _facesBean.getProperty(_DISABLED_KEY);
535
536 return (disabled != null) ? disabled.booleanValue() : false;
537 }
538
539 protected boolean isMaximumSet()
540 {
541 return _facesBean.getProperty(_MAXIMUM_KEY) != null;
542 }
543
544 protected boolean isMinimumSet()
545 {
546 return _facesBean.getProperty(_MINIMUM_KEY) != null;
547 }
548
549 private FacesMessage _getNotCorrectType(
550 FacesContext context)
551 {
552 return MessageFactory.getMessage(context, CONVERT_MESSAGE_ID);
553 }
554
555
556
557
558
559
560
561
562
563
564
565 private long _convertValueToLong(Object value) throws NumberFormatException
566 {
567 if(value instanceof Number)
568 {
569 return ((Number)value).longValue();
570 }
571 else
572 {
573 return Long.parseLong(value.toString());
574 }
575 }
576
577 private FacesMessage _getNotInRangeMessage(
578 FacesContext context,
579 UIComponent component,
580 Object value,
581 Object min,
582 Object max)
583 {
584 Object msg = _getRawNotInRangeMessageDetail();
585 Object label = ValidatorUtils.getComponentLabel(component);
586
587 Object[] params = {label, value, min, max};
588
589 return MessageFactory.getMessage(context, NOT_IN_RANGE_MESSAGE_ID,
590 msg, params, component);
591 }
592
593
594
595 private Object _getRawNotInRangeMessageDetail()
596 {
597 return _facesBean.getRawProperty(_NOT_IN_RANGE_MESSAGE_DETAIL_KEY);
598 }
599
600
601 private FacesMessage _getMaximumMessage(
602 FacesContext context,
603 UIComponent component,
604 Object value,
605 Object max)
606 {
607
608 Object msg = _getRawMaximumMessageDetail();
609 Object label = ValidatorUtils.getComponentLabel(component);
610
611 Object[] params = {label, value, max};
612
613 return MessageFactory.getMessage(context,
614 MAXIMUM_MESSAGE_ID,
615 msg,
616 params,
617 component);
618 }
619
620 private Object _getRawMaximumMessageDetail()
621 {
622 return _facesBean.getRawProperty(_MAXIMUM_MESSAGE_DETAIL_KEY);
623 }
624
625 private FacesMessage _getMinimumMessage(
626 FacesContext context,
627 UIComponent component,
628 Object value,
629 Object min)
630 {
631 Object msg = _getRawMinimumMessageDetail();
632 Object label = ValidatorUtils.getComponentLabel(component);
633
634 Object[] params = {label, value, min};
635
636 return MessageFactory.getMessage(context, MINIMUM_MESSAGE_ID,
637 msg, params, component);
638 }
639
640 private Object _getRawMinimumMessageDetail()
641 {
642 return _facesBean.getRawProperty(_MINIMUM_MESSAGE_DETAIL_KEY);
643 }
644
645 private static final FacesBean.Type _TYPE = new FacesBean.Type();
646
647 private static final PropertyKey _MINIMUM_KEY =
648 _TYPE.registerKey("minimum", Long.class,
649
650 Long.valueOf(Long.MIN_VALUE));
651
652 private static final PropertyKey _MAXIMUM_KEY =
653 _TYPE.registerKey("maximum", Long.class,
654
655 Long.valueOf(Long.MAX_VALUE));
656
657 private static final PropertyKey _MAXIMUM_MESSAGE_DETAIL_KEY =
658 _TYPE.registerKey("messageDetailMaximum", String.class);
659
660 private static final PropertyKey _MINIMUM_MESSAGE_DETAIL_KEY =
661 _TYPE.registerKey("messageDetailMinimum", String.class);
662
663 private static final PropertyKey _NOT_IN_RANGE_MESSAGE_DETAIL_KEY =
664 _TYPE.registerKey("messageDetailNotInRange", String.class);
665
666 private static final PropertyKey _HINT_MAXIMUM_KEY =
667 _TYPE.registerKey("hintMaximum", String.class);
668
669 private static final PropertyKey _HINT_MINIMUM_KEY =
670 _TYPE.registerKey("hintMinimum", String.class);
671
672 private static final PropertyKey _HINT_NOT_IN_RANGE =
673 _TYPE.registerKey("hintNotInRange", String.class);
674
675
676 private static final PropertyKey _DISABLED_KEY =
677 _TYPE.registerKey("disabled", Boolean.class, Boolean.FALSE);
678
679 private FacesBean _facesBean = ValidatorUtils.getFacesBean(_TYPE);
680
681 private boolean _transientValue = false;
682 }