View Javadoc
1   /**
2   The contents of this file are subject to the Mozilla Public License Version 1.1 
3   (the "License"); you may not use this file except in compliance with the License. 
4   You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
5   Software distributed under the License is distributed on an "AS IS" basis, 
6   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
7   specific language governing rights and limitations under the License. 
8   
9   The Original Code is "RuleSupport.java".  Description: 
10  "Generic methods for validation rules" 
11  
12  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
13  2012.  All Rights Reserved. 
14  
15  Contributor(s): ______________________________________. 
16  
17  Alternatively, the contents of this file may be used under the terms of the 
18  GNU General Public License (the "GPL"), in which case the provisions of the GPL are 
19  applicable instead of those above.  If you wish to allow use of your version of this 
20  file only under the terms of the GPL and not to allow others to use your version 
21  of this file under the MPL, indicate your decision by deleting  the provisions above 
22  and replace  them with the notice and other provisions required by the GPL License.  
23  If you do not delete the provisions above, a recipient may use your version of 
24  this file under either the MPL or the GPL. 
25   */
26  package ca.uhn.hl7v2.validation.impl;
27  
28  import ca.uhn.hl7v2.ErrorCode;
29  import ca.uhn.hl7v2.Location;
30  import ca.uhn.hl7v2.Severity;
31  import ca.uhn.hl7v2.validation.Rule;
32  import ca.uhn.hl7v2.validation.ValidationException;
33  
34  @SuppressWarnings("serial")
35  public abstract class RuleSupport<T> implements Rule<T> {
36  
37  	private static final ValidationExceptionion.html#ValidationException">ValidationException[] PASSED = new ValidationException[0];
38      private Severity severity = Severity.ERROR;
39      private String description;
40      private String sectionReference;
41      private ErrorCode errorCode = ErrorCode.APPLICATION_INTERNAL_ERROR;
42  
43  	protected ValidationException[] result(boolean result, Object value) {
44  		return result(result, value, Location.UNKNOWN);
45  	}
46  	
47  	protected ValidationException[] result(boolean result, Object value, Location location) {
48  		return result ? passed() : failedWithValue(value, location);
49  	}	
50  
51  	protected ValidationException[] passed() {
52  		return PASSED;
53  	}
54  
55  	protected ValidationException[] failedWithValue(Object value, Location location) {
56  		String description = getDescription();
57  		String msg = String.format(description, value);
58  		return failed("Validation failed: " + msg, location);
59  	}
60  
61  	protected ValidationException[] failed(String msg) {
62  		return failed(msg, Location.UNKNOWN);
63  	}
64  
65  	
66  	protected ValidationException[] failed(String msg, Location location) {
67  		ValidationException ve = new ValidationException(msg, severity);
68  		ve.setError(getErrorCode());
69  		ve.setLocation(location);
70  		return new ValidationException[] { ve };
71  	}
72  
73  	protected ValidationException[] failed(Exception e) {
74  		return failed(e, Location.UNKNOWN);
75  	}
76  
77  	protected ValidationException[] failed(Exception e, Location location) {
78  		if (e instanceof ValidationException)
79  			return new ValidationException/../../ca/uhn/hl7v2/validation/ValidationException.html#ValidationException">ValidationException[] { (ValidationException) e };
80  		ValidationException ve = new ValidationException(e.getMessage(), e, severity);
81  		ve.setError(getErrorCode());
82  		ve.setLocation(location);
83  		return new ValidationException[] { ve };
84  	}
85  
86  	/**
87  	 * @see ca.uhn.hl7v2.validation.Rule#getSectionReference()
88  	 */
89  	public String getSectionReference() {
90  		return sectionReference;
91  	}
92  
93      /**
94       * @see ca.uhn.hl7v2.validation.Rule#getDescription()
95       */
96      public String getDescription() {
97          return description;
98      }
99  
100     /**
101      * @return the severity if this rule fails
102      */
103     public Severity getSeverity() {
104         return severity;
105     }
106 
107     /**
108 	 * @return the error code to be used in case the validation failed
109 	 */
110 	public ErrorCode getErrorCode() {
111 		return errorCode;
112 	}
113 
114     public void setSeverity(Severity severity) {
115         this.severity = severity;
116     }
117 
118     public void setDescription(String description) {
119         this.description = description;
120     }
121 
122     public void setSectionReference(String sectionReference) {
123         this.sectionReference = sectionReference;
124     }
125 
126     public void setErrorCode(ErrorCode errorCode) {
127         this.errorCode = errorCode;
128     }
129 }