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.Severity;
 31  
 import ca.uhn.hl7v2.validation.Rule;
 32  
 import ca.uhn.hl7v2.validation.ValidationException;
 33  
 
 34  
 @SuppressWarnings("serial")
 35  65760
 public abstract class RuleSupport<T> implements Rule<T> {
 36  
 
 37  5
         private static final ValidationException[] PASSED = new ValidationException[0];
 38  65760
     private Severity severity = Severity.ERROR;
 39  
     private String description;
 40  
     private String sectionReference;
 41  65760
     private ErrorCode errorCode = ErrorCode.APPLICATION_INTERNAL_ERROR;
 42  
 
 43  
         protected ValidationException[] result(boolean result, Object value) {
 44  119608
                 return result(result, value, Location.UNKNOWN);
 45  
         }
 46  
         
 47  
         protected ValidationException[] result(boolean result, Object value, Location location) {
 48  119608
                 return result ? passed() : failedWithValue(value, location);
 49  
         }        
 50  
 
 51  
         protected ValidationException[] passed() {
 52  119028
                 return PASSED;
 53  
         }
 54  
 
 55  
         protected ValidationException[] failedWithValue(Object value, Location location) {
 56  610
                 String description = getDescription();
 57  610
                 String msg = String.format(description, String.valueOf(value));
 58  610
                 return failed("Validation failed: " + msg, location);
 59  
         }
 60  
 
 61  
         protected ValidationException[] failed(String msg) {
 62  15
                 return failed(msg, Location.UNKNOWN);
 63  
         }
 64  
 
 65  
         
 66  
         protected ValidationException[] failed(String msg, Location location) {
 67  625
                 ValidationException ve = new ValidationException(msg, severity);
 68  625
                 ve.setError(getErrorCode());
 69  625
                 ve.setLocation(location);
 70  625
                 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  590
         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  625
                 return errorCode;
 112  
         }
 113  
 
 114  
     public void setSeverity(Severity severity) {
 115  76445
         this.severity = severity;
 116  76445
     }
 117  
 
 118  
     public void setDescription(String description) {
 119  65615
         this.description = description;
 120  65615
     }
 121  
 
 122  
     public void setSectionReference(String sectionReference) {
 123  27095
         this.sectionReference = sectionReference;
 124  27095
     }
 125  
 
 126  
     public void setErrorCode(ErrorCode errorCode) {
 127  0
         this.errorCode = errorCode;
 128  0
     }
 129  
 }