View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2013 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;
14  
15  import java.io.Serializable;
16  import java.util.Map;
17  
18  import net.sf.oval.context.OValContext;
19  import net.sf.oval.exception.OValException;
20  
21  /**
22   * interface for classes that can check/validate if a constraint is satisfied
23   *
24   * @author Sebastian Thomschke
25   */
26  public interface Check extends Serializable
27  {
28  	/**
29  	 * <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.
30  	 *
31  	 * <p><b>Default:</b> ConstraintTarget.CONTAINER
32  	 *
33  	 * <p><b>Note:</b> This setting is ignored for object types other than array, map and collection.
34  	 */
35  	ConstraintTarget[] getAppliesTo();
36  
37  	/**
38  	 * @return Returns the context where the constraint was declared.
39  	 *
40  	 * @see net.sf.oval.context.ClassContext
41  	 * @see net.sf.oval.context.FieldContext
42  	 * @see net.sf.oval.context.MethodEntryContext
43  	 * @see net.sf.oval.context.MethodExitContext
44  	 * @see net.sf.oval.context.MethodParameterContext
45  	 * @see net.sf.oval.context.MethodReturnValueContext
46  	 */
47  	OValContext getContext();
48  
49  	/**
50  	 * @return the error code that will be used in a corresponding ConstraintViolation object
51  	 */
52  	String getErrorCode();
53  
54  	/**
55  	 * gets the default message that is displayed if a corresponding message key
56  	 * is not found in the messages properties file
57  	 * <br>
58  	 * default processed place holders are:
59  	 * <ul>
60  	 * <li>{context} => specifies which getter, method parameter or field was validated
61  	 * <li>{invalidValue} => string representation of the validated value
62  	 * </ul>
63  	 */
64  	String getMessage();
65  
66  	/**
67  	 * values that are used to fill place holders when rendering the error message.
68  	 * A key "min" with a value "4" will replace the place holder {min} in an error message
69  	 * like "Value cannot be smaller than {min}" with the string "4".
70  	 */
71  	Map<String, ? extends Serializable> getMessageVariables();
72  
73  	/**
74  	 * @return the profiles, may return null
75  	 */
76  	String[] getProfiles();
77  
78  	/**
79  	 * @return the severity
80  	 */
81  	int getSeverity();
82  
83  	/**
84  	 * An expression to specify where in the object graph relative from this object the expression
85  	 * should be applied.
86  	 * <p>
87  	 * Examples:
88  	 * <li>"owner" would apply this constraint to the current object's property <code>owner</code>
89  	 * <li>"owner.id" would apply this constraint to the current object's <code>owner</code>'s property <code>id</code>
90  	 * <li>"jxpath:owner/id" would use the JXPath implementation to traverse the object graph to locate the object where this constraint should be applied.
91  	 */
92  	String getTarget();
93  
94  	/**
95  	 * Formula returning <code>true</code> if this constraint shall be evaluated and
96  	 * <code>false</code> if it shall be ignored for the current validation.
97  	 * <p>
98  	 * <b>Important:</b> The formula must be prefixed with the name of the scripting language that is used.
99  	 * E.g. <code>groovy:_this.amount > 10</code>
100 	 * <p>
101 	 * Available context variables are:<br>
102 	 * <b>_this</b> -&gt; the validated bean<br>
103 	 * <b>_value</b> -&gt; the value to validate (e.g. the field value, parameter value, method return value,
104 	 *    or the validated bean for object level constraints)
105 	 *
106 	 * @return the formula
107 	 */
108 	String getWhen();
109 
110 	/**
111 	 *
112 	 * @param validatedObject the object/bean to validate the value against, for static fields or methods this is the class
113 	 * @param valueToValidate the value to validate, may be null when validating pre conditions for static methods
114 	 * @param validator the calling validator
115 	 * @return <code>true</code> if this check is active and must be satisfied
116 	 */
117 	boolean isActive(Object validatedObject, Object valueToValidate, Validator validator);
118 
119 	/**
120 	 * This method implements the validation logic
121 	 *
122 	 * @param validatedObject the object/bean to validate the value against, for static fields or methods this is the class
123 	 * @param valueToValidate the value to validate, may be null when validating pre conditions for static methods
124 	 * @param context the validation context (e.g. a field, a constructor parameter or a method parameter)
125 	 * @param validator the calling validator
126 	 * @return true if the value satisfies the checked constraint
127 	 */
128 	boolean isSatisfied(Object validatedObject, Object valueToValidate, OValContext context, Validator validator)
129 			throws OValException;
130 
131 	/**
132 	 * @param target the constraint target to set
133 	 */
134 	void setAppliesTo(ConstraintTarget... target);
135 
136 	/**
137 	 * @param context the context to set
138 	 */
139 	void setContext(OValContext context);
140 
141 	/**
142 	 * @param errorCode the error code to set
143 	 */
144 	void setErrorCode(String errorCode);
145 
146 	/**
147 	 * sets the default message that is displayed if a corresponding message key
148 	 * is not found in the messages properties file
149 	 *
150 	 * <br>
151 	 * default processed place holders are:
152 	 * <ul>
153 	 * <li>{context} => specifies which getter, method parameter or field was validated
154 	 * <li>{invalidValue} => string representation of the validated value
155 	 * </ul>
156 	 */
157 	void setMessage(String message);
158 
159 	/**
160 	 * @param profiles the profiles to set
161 	 */
162 	void setProfiles(String... profiles);
163 
164 	/**
165 	 * @param severity the severity to set
166 	 */
167 	void setSeverity(int severity);
168 
169 	/**
170 	 * Sets an expression to specify where in the object graph relative from this object the expression
171 	 * should be applied.
172 	 * <p>
173 	 * Examples:
174 	 * <li>"owner" would apply this constraint to the current object's property <code>owner</code>
175 	 * <li>"owner.id" would apply this constraint to the current object's <code>owner</code>'s property <code>id</code>
176 	 * <li>"jxpath:owner/id" would use the JXPath implementation to traverse the object graph to locate the object where this constraint should be applied.
177 	 */
178 	void setTarget(String target);
179 
180 	/**
181 	 * Sets the formula returning <code>true</code> if this constraint shall be evaluated and
182 	 * <code>false</code> if it shall be ignored for the current validation.
183 	 * <p>
184 	 * <b>Important:</b> The formula must be prefixed with the name of the scripting language that is used.
185 	 * E.g. <code>groovy:_this.amount > 10</code>
186 	 * <p>
187 	 * Available context variables are:<br>
188 	 * <b>_this</b> -&gt; the validated bean<br>
189 	 * <b>_value</b> -&gt; the value to validate (e.g. the field value, parameter value, method return value,
190 	 *    or the validated bean for object level constraints)
191 	 *
192 	 * @param when formula calculating if this check is active
193 	 */
194 	void setWhen(String when);
195 }