001/**
002The contents of this file are subject to the Mozilla Public License Version 1.1 
003(the "License"); you may not use this file except in compliance with the License. 
004You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
005Software distributed under the License is distributed on an "AS IS" basis, 
006WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
007specific language governing rights and limitations under the License. 
008
009The Original Code is "RuleSupport.java".  Description: 
010"Generic methods for validation rules" 
011
012The Initial Developer of the Original Code is University Health Network. Copyright (C) 
0132012.  All Rights Reserved. 
014
015Contributor(s): ______________________________________. 
016
017Alternatively, the contents of this file may be used under the terms of the 
018GNU General Public License (the "GPL"), in which case the provisions of the GPL are 
019applicable instead of those above.  If you wish to allow use of your version of this 
020file only under the terms of the GPL and not to allow others to use your version 
021of this file under the MPL, indicate your decision by deleting  the provisions above 
022and replace  them with the notice and other provisions required by the GPL License.  
023If you do not delete the provisions above, a recipient may use your version of 
024this file under either the MPL or the GPL. 
025 */
026package ca.uhn.hl7v2.validation.impl;
027
028import ca.uhn.hl7v2.ErrorCode;
029import ca.uhn.hl7v2.Location;
030import ca.uhn.hl7v2.Severity;
031import ca.uhn.hl7v2.validation.Rule;
032import ca.uhn.hl7v2.validation.ValidationException;
033
034@SuppressWarnings("serial")
035public abstract class RuleSupport<T> implements Rule<T> {
036
037        private static final ValidationException[] PASSED = new ValidationException[0];
038    private Severity severity = Severity.ERROR;
039    private String description;
040    private String sectionReference;
041    private ErrorCode errorCode = ErrorCode.APPLICATION_INTERNAL_ERROR;
042
043        protected ValidationException[] result(boolean result, Object value) {
044                return result(result, value, Location.UNKNOWN);
045        }
046        
047        protected ValidationException[] result(boolean result, Object value, Location location) {
048                return result ? passed() : failedWithValue(value, location);
049        }       
050
051        protected ValidationException[] passed() {
052                return PASSED;
053        }
054
055        protected ValidationException[] failedWithValue(Object value, Location location) {
056                String description = getDescription();
057                String msg = String.format(description, String.valueOf(value));
058                return failed("Validation failed: " + msg, location);
059        }
060
061        protected ValidationException[] failed(String msg) {
062                return failed(msg, Location.UNKNOWN);
063        }
064
065        
066        protected ValidationException[] failed(String msg, Location location) {
067                ValidationException ve = new ValidationException(msg, severity);
068                ve.setError(getErrorCode());
069                ve.setLocation(location);
070                return new ValidationException[] { ve };
071        }
072
073        protected ValidationException[] failed(Exception e) {
074                return failed(e, Location.UNKNOWN);
075        }
076
077        protected ValidationException[] failed(Exception e, Location location) {
078                if (e instanceof ValidationException)
079                        return new ValidationException[] { (ValidationException) e };
080                ValidationException ve = new ValidationException(e.getMessage(), e, severity);
081                ve.setError(getErrorCode());
082                ve.setLocation(location);
083                return new ValidationException[] { ve };
084        }
085
086        /**
087         * @see ca.uhn.hl7v2.validation.Rule#getSectionReference()
088         */
089        public String getSectionReference() {
090                return sectionReference;
091        }
092
093    /**
094     * @see ca.uhn.hl7v2.validation.Rule#getDescription()
095     */
096    public String getDescription() {
097        return description;
098    }
099
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}