View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
3    * Thomschke.
4    * 
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    * 
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.constraint;
14  
15  import java.lang.annotation.Documented;
16  import java.lang.annotation.ElementType;
17  import java.lang.annotation.Retention;
18  import java.lang.annotation.RetentionPolicy;
19  import java.lang.annotation.Target;
20  
21  import net.sf.oval.ConstraintTarget;
22  import net.sf.oval.ConstraintViolation;
23  import net.sf.oval.configuration.annotation.Constraint;
24  import net.sf.oval.configuration.annotation.Constraints;
25  
26  /**
27   * Check if the date is within the a date range.
28   * 
29   * <br><br>
30   * <b>Note:</b> This constraint is also satisfied when the value to validate is null, therefore you might also need to specified @NotNull
31   * 
32   * @author Sebastian Thomschke
33   */
34  @Documented
35  @Retention(RetentionPolicy.RUNTIME)
36  @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
37  @Constraint(checkWith = DateRangeCheck.class)
38  public @interface DateRange
39  {
40  	@Documented
41  	@Retention(RetentionPolicy.RUNTIME)
42  	@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
43  	@Constraints
44  	public @interface List
45  	{
46  		/**
47  		 * The DateRange constraints.
48  		 */
49  		DateRange[] value();
50  
51  		/**
52  		 * Formula returning <code>true</code> if this constraint shall be evaluated and
53  		 * <code>false</code> if it shall be ignored for the current validation.
54  		 * <p>
55  		 * <b>Important:</b> The formula must be prefixed with the name of the scripting language that is used.
56  		 * E.g. <code>groovy:_this.amount > 10</code>
57  		 * <p>
58  		 * Available context variables are:<br>
59  		 * <b>_this</b> -&gt; the validated bean<br>
60  		 * <b>_value</b> -&gt; the value to validate (e.g. the field value, parameter value, method return value,
61  		 *    or the validated bean for object level constraints)
62  		 */
63  		String when() default "";
64  	}
65  
66  	/**
67  	 * <p>In case the constraint is declared for an array, collection or map this controls how the constraint is applied to it and it's child objects.
68  	 * 
69  	 * <p><b>Default:</b> ConstraintTarget.VALUES
70  	 * 
71  	 * <p><b>Note:</b> This setting is ignored for object types other than array, map and collection.
72  	 */
73  	ConstraintTarget[] appliesTo() default ConstraintTarget.VALUES;
74  
75  	/**
76  	 * error code passed to the ConstraintViolation object
77  	 */
78  	String errorCode() default "net.sf.oval.constraint.DateRange";
79  
80  	/**
81  	 * The format of the specified dates in a form understandable by the SimpleDateFormat class.
82  	 * Defaults to the default format style of the default locale.
83  	 */
84  	String format() default "";
85  
86  	/**
87  	 * The upper date compared against in the format specified with the dateFormat parameter. 
88  	 * If not specified then no upper boundary check is performed.<br>
89  	 * Special values are:
90  	 * <ul>
91  	 * <li><code>now</code>
92  	 * <li><code>today</code>
93  	 * <li><code>yesterday</code>
94  	 * <li><code>tomorrow</code>
95  	 * </ul>
96  	 */
97  	String max() default "";
98  
99  	/**
100 	 * message to be used for the ContraintsViolatedException
101 	 * 
102 	 * @see ConstraintViolation
103 	 */
104 	String message() default "net.sf.oval.constraint.DateRange.violated";
105 
106 	/**
107 	 * The lower date compared against in the format specified with the dateFormat parameter. 
108 	 * If not specified then no upper boundary check is performed.<br>
109 	 * Special values are:
110 	 * <ul>
111 	 * <li><code>now</code>
112 	 * <li><code>today</code>
113 	 * <li><code>yesterday</code>
114 	 * <li><code>tomorrow</code>
115 	 * </ul>
116 	 */
117 	String min() default "";
118 
119 	/**
120 	 * The associated constraint profiles.
121 	 */
122 	String[] profiles() default {};
123 
124 	/**
125 	 * severity passed to the ConstraintViolation object
126 	 */
127 	int severity() default 0;
128 
129 	/**
130 	 * An expression to specify where in the object graph relative from this object the expression
131 	 * should be applied.
132 	 * <p>
133 	 * Examples:
134 	 * <li>"owner" would apply this constraint to the current object's property <code>owner</code>
135 	 * <li>"owner.id" would apply this constraint to the current object's <code>owner</code>'s property <code>id</code>
136 	 * <li>"jxpath:owner/id" would use the JXPath implementation to traverse the object graph to locate the object where this constraint should be applied.
137 	 */
138 	String target() default "";
139 
140 	/**
141 	 * Tolerance in milliseconds the validated value can be beyond the min/max limits. 
142 	 * This is useful to compensate time differences in distributed environments where the clocks are not 100% in sync.
143 	 */
144 	int tolerance() default 0;
145 }