Coverage Report - ca.uhn.hl7v2.validation.impl.RuleSupport
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleSupport
67%
23/34
50%
2/4
1.188
 
 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.validation.Rule;
 31  
 import ca.uhn.hl7v2.Severity;
 32  
 import ca.uhn.hl7v2.validation.ValidationException;
 33  
 
 34  
 @SuppressWarnings("serial")
 35  5621
 public abstract class RuleSupport<T> implements Rule<T> {
 36  
 
 37  1
         private static final ValidationException[] PASSED = new ValidationException[0];
 38  5621
     private Severity severity = Severity.ERROR;
 39  
     private String description;
 40  
     private String sectionReference;
 41  5621
     private ErrorCode errorCode = ErrorCode.APPLICATION_INTERNAL_ERROR;
 42  
 
 43  
         protected ValidationException[] result(boolean result, Object value) {
 44  15527
                 return result(result, value, Location.UNKNOWN);
 45  
         }
 46  
         
 47  
         protected ValidationException[] result(boolean result, Object value, Location location) {
 48  15527
                 return result ? passed() : failedWithValue(value, location);
 49  
         }        
 50  
 
 51  
         protected ValidationException[] passed() {
 52  15411
                 return PASSED;
 53  
         }
 54  
 
 55  
         protected ValidationException[] failedWithValue(Object value, Location location) {
 56  121
                 String description = getDescription();
 57  121
                 String msg = String.format(description, String.valueOf(value));
 58  121
                 return failed("Validation failed: " + msg, location);
 59  
         }
 60  
 
 61  
         protected ValidationException[] failed(String msg) {
 62  1
                 return failed(msg, Location.UNKNOWN);
 63  
         }
 64  
 
 65  
         
 66  
         protected ValidationException[] failed(String msg, Location location) {
 67  122
                 ValidationException ve = new ValidationException(msg, severity);
 68  122
                 ve.setError(getErrorCode());
 69  122
                 ve.setLocation(location);
 70  122
                 return new ValidationException[] { ve };
 71  
         }
 72  
 
 73  
         protected ValidationException[] failed(Exception e) {
 74  0
                 return failed(e, Location.UNKNOWN);
 75  
         }
 76  
 
 77  
         protected ValidationException[] failed(Exception e, Location location) {
 78  0
                 if (e instanceof ValidationException)
 79  0
                         return new ValidationException[] { (ValidationException) e };
 80  0
                 ValidationException ve = new ValidationException(e.getMessage(), e, severity);
 81  0
                 ve.setError(getErrorCode());
 82  0
                 ve.setLocation(location);
 83  0
                 return new ValidationException[] { ve };
 84  
         }
 85  
 
 86  
         /**
 87  
          * @see ca.uhn.hl7v2.validation.Rule#getSectionReference()
 88  
          */
 89  
         public String getSectionReference() {
 90  0
                 return sectionReference;
 91  
         }
 92  
 
 93  
     /**
 94  
      * @see ca.uhn.hl7v2.validation.Rule#getDescription()
 95  
      */
 96  
     public String getDescription() {
 97  117
         return description;
 98  
     }
 99  
 
 100  
     /**
 101  
      * @return the severity if this rule fails
 102  
      */
 103  
     public Severity getSeverity() {
 104  0
         return severity;
 105  
     }
 106  
 
 107  
     /**
 108  
          * @return the error code to be used in case the validation failed
 109  
          */
 110  
         public ErrorCode getErrorCode() {
 111  122
                 return ErrorCode.APPLICATION_INTERNAL_ERROR;
 112  
         }
 113  
 
 114  
     public void setSeverity(Severity severity) {
 115  6511
         this.severity = severity;
 116  6511
     }
 117  
 
 118  
     public void setDescription(String description) {
 119  5595
         this.description = description;
 120  5595
     }
 121  
 
 122  
     public void setSectionReference(String sectionReference) {
 123  2294
         this.sectionReference = sectionReference;
 124  2294
     }
 125  
 
 126  
     public void setErrorCode(ErrorCode errorCode) {
 127  0
         this.errorCode = errorCode;
 128  0
     }
 129  
 }