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