Coverage Report - ca.uhn.hl7v2.AbstractHL7Exception
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractHL7Exception
90%
79/87
68%
26/38
1.864
 
 1  
 
 2  
 /*
 3  
  The contents of this file are subject to the Mozilla Public License Version 1.1
 4  
  (the "License"); you may not use this file except in compliance with the License.
 5  
  You may obtain a copy of the License at http://www.mozilla.org/MPL/
 6  
  Software distributed under the License is distributed on an "AS IS" basis,
 7  
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
 8  
  specific language governing rights and limitations under the License.
 9  
 
 10  
  The Original Code is "AbstractHL7Exception.java".  Description:
 11  
  "Exception containing a Location and Error Code"
 12  
 
 13  
  The Initial Developer of the Original Code is University Health Network. Copyright (C)
 14  
  2012.  All Rights Reserved.
 15  
 
 16  
  Contributor(s): ______________________________________.
 17  
 
 18  
  Alternatively, the contents of this file may be used under the terms of the
 19  
  GNU General Public License (the "GPL"), in which case the provisions of the GPL are
 20  
  applicable instead of those above.  If you wish to allow use of your version of this
 21  
  file only under the terms of the GPL and not to allow others to use your version
 22  
  of this file under the MPL, indicate your decision by deleting  the provisions above
 23  
  and replace  them with the notice and other provisions required by the GPL License.
 24  
  If you do not delete the provisions above, a recipient may use your version of
 25  
  this file under either the MPL or the GPL.
 26  
  */
 27  
 
 28  
 package ca.uhn.hl7v2;
 29  
 
 30  
 import static ca.uhn.hl7v2.Version.V25;
 31  
 import static ca.uhn.hl7v2.Version.versionOf;
 32  
 
 33  
 import ca.uhn.hl7v2.model.Message;
 34  
 import ca.uhn.hl7v2.model.Segment;
 35  
 import ca.uhn.hl7v2.util.Terser;
 36  
 
 37  
 /**
 38  
  * Abstract base class for Exceptions that are able to create acknowledgement
 39  
  * messages from their error code and location information
 40  
  *
 41  
  * @author Christian Ohr
 42  
  */
 43  
 @SuppressWarnings("serial")
 44  
 public abstract class AbstractHL7Exception extends Exception {
 45  
 
 46  
     private Location location;
 47  3730
     private ErrorCode errorCode = ErrorCode.APPLICATION_INTERNAL_ERROR;
 48  3730
     private Severity severity = Severity.ERROR;
 49  
     private Message responseMessage;
 50  
 
 51  
 
 52  
     public AbstractHL7Exception() {
 53  40
         super();
 54  40
     }
 55  
 
 56  
     public AbstractHL7Exception(String message, Throwable cause) {
 57  420
         super(message, cause);
 58  420
     }
 59  
 
 60  
     public AbstractHL7Exception(String message) {
 61  2490
         super(message);
 62  2490
     }
 63  
 
 64  
     public AbstractHL7Exception(Throwable cause) {
 65  780
         super(cause);
 66  780
     }
 67  
 
 68  
     public Location getLocation() {
 69  2270
         return location;
 70  
     }
 71  
 
 72  
     public void setLocation(Location location) {
 73  940
         this.location = location;
 74  940
     }
 75  
 
 76  
     public void setFieldPosition(int pos) {
 77  285
         if (location == null)
 78  280
             location = new Location();
 79  285
         location.withField(pos);
 80  285
     }
 81  
 
 82  
     public void setSegmentName(String segmentName) {
 83  290
         if (location == null)
 84  10
             location = new Location();
 85  290
         location.withSegmentName(segmentName);
 86  290
     }
 87  
 
 88  
     public void setSegmentRepetition(int segmentRepetition) {
 89  0
         if (location == null)
 90  0
             location = new Location();
 91  0
         location.withSegmentRepetition(segmentRepetition);
 92  0
     }
 93  
 
 94  
     public int getErrorCode() {
 95  235
         return errorCode.getCode();
 96  
     }
 97  
 
 98  
     public void setErrorCode(int errorCode) {
 99  235
         this.errorCode = ErrorCode.errorCodeFor(errorCode);
 100  235
     }
 101  
 
 102  
     public ErrorCode getError() {
 103  10
         return errorCode;
 104  
     }
 105  
 
 106  
     public void setError(ErrorCode errorCode) {
 107  770
         this.errorCode = errorCode;
 108  770
     }
 109  
 
 110  
     public Severity getSeverity() {
 111  415
         return severity;
 112  
     }
 113  
 
 114  
     public void setSeverity(Severity severity) {
 115  1225
         this.severity = severity;
 116  1225
     }
 117  
 
 118  
     public Message getResponseMessage() {
 119  360
         return responseMessage;
 120  
     }
 121  
 
 122  
     public void setResponseMessage(Message responseMessage) {
 123  10
         this.responseMessage = responseMessage;
 124  10
     }
 125  
 
 126  
     /**
 127  
      * Populates the generated response based on this exception.
 128  
      *
 129  
      * @param emptyResponse empty response message
 130  
      * @param acknowledgmentCode the acknowledgement code
 131  
      * @return response message
 132  
      * @throws HL7Exception if populating the response fails
 133  
      */
 134  
     public Message populateResponse(Message emptyResponse, AcknowledgmentCode acknowledgmentCode, int repetition)
 135  
             throws HL7Exception {
 136  370
         if (responseMessage == null) {
 137  370
             if (acknowledgmentCode == null) acknowledgmentCode = AcknowledgmentCode.AA;
 138  370
             if (V25.isGreaterThan(versionOf(emptyResponse.getVersion()))) {
 139  325
                 responseMessage = populateResponseBefore25(emptyResponse, acknowledgmentCode, repetition);
 140  
             } else {
 141  45
                 responseMessage = populateResponseAsOf25(emptyResponse, acknowledgmentCode, repetition);
 142  
             }
 143  
         }
 144  370
         return responseMessage;
 145  
     }
 146  
 
 147  
     /**
 148  
      * Fill segments for HL7 versions 2.5 or newer.
 149  
      * <p/>
 150  
      * HL7 versions before 2.5 require to set MSA-1. The ERR segment has various fields (ERR-2,
 151  
      * ERR-3) containing details about the exception. ERR-1 is marked as obsolete.
 152  
      *
 153  
      * @param response           the raw response message
 154  
      * @param acknowledgmentCode acknowledgmentCode
 155  
      * @param repetition         repetition of the ERR segment that shall be populated
 156  
      * @throws HL7Exception
 157  
      */
 158  
     private Message populateResponseAsOf25(Message response, AcknowledgmentCode acknowledgmentCode,
 159  
                                            int repetition) throws HL7Exception {
 160  
         // TODO define what should happen if there is no MSA or ERR
 161  45
         Segment msa = (Segment) response.get("MSA");
 162  45
         Terser.set(msa, 1, 0, 1, 1, acknowledgmentCode.name());
 163  45
         Segment err = (Segment) response.get("ERR", repetition);
 164  45
         if (location != null) {
 165  30
             if (location.getSegmentName() != null)
 166  30
                 Terser.set(err, 2, 0, 1, 1, location.getSegmentName());
 167  30
             if (location.getSegmentRepetition() > 0)
 168  0
                 Terser.set(err, 2, 0, 2, 1, Integer.toString(location.getSegmentRepetition()));
 169  30
             if (location.getField() > 0)
 170  30
                 Terser.set(err, 2, 0, 3, 1, Integer.toString(location.getField()));
 171  30
             if (location.getFieldRepetition() > 0)
 172  0
                 Terser.set(err, 2, 0, 4, 1, Integer.toString(location.getFieldRepetition()));
 173  30
             if (location.getComponent() > 0)
 174  0
                 Terser.set(err, 2, 0, 5, 1, Integer.toString(location.getComponent()));
 175  30
             if (location.getSubcomponent() > 0)
 176  0
                 Terser.set(err, 2, 0, 6, 1, Integer.toString(location.getSubcomponent()));
 177  
         }
 178  45
         Terser.set(err, 3, 0, 1, 1, Integer.toString(errorCode.getCode()));
 179  45
         Terser.set(err, 3, 0, 2, 1, errorCode.getMessage());
 180  45
         Terser.set(err, 3, 0, 3, 1, ErrorCode.codeTable());
 181  45
         Terser.set(err, 3, 0, 9, 1, getMessage());
 182  45
         Terser.set(err, 4, 0, 1, 1, "E");
 183  45
         return response;
 184  
     }
 185  
 
 186  
     /**
 187  
      * Fill segments for HL7 versions before 2.5.
 188  
      * <p/>
 189  
      * HL7 versions before 2.5 require to set MSA-1 and MSA-3. The ERR segment only has one
 190  
      * repeatable field (ERR-1) with components containing details about the exception.
 191  
      *
 192  
      * @param response           the raw response message
 193  
      * @param acknowledgmentCode acknowledgment code
 194  
      * @param repetition         repetition of the ERR segment that shall be popualted
 195  
      * @throws HL7Exception
 196  
      */
 197  
     private Message populateResponseBefore25(Message response, AcknowledgmentCode acknowledgmentCode,
 198  
                                              int repetition) throws HL7Exception {
 199  
         // TODO define what should happen if there is no MSA or ERR
 200  325
         Segment msa = (Segment) response.get("MSA");
 201  325
         Terser.set(msa, 1, 0, 1, 1, acknowledgmentCode.name());
 202  325
         Terser.set(msa, 3, 0, 1, 1, errorCode.getMessage());
 203  325
         Segment err = (Segment) response.get("ERR");
 204  325
         if (location != null) {
 205  15
             if (location.getSegmentName() != null)
 206  15
                 Terser.set(err, 1, repetition, 1, 1, location.getSegmentName());
 207  15
             if (location.getField() > 0)
 208  15
                 Terser.set(err, 1, repetition, 3, 1, Integer.toString(location.getField()));
 209  
         }
 210  325
         Terser.set(err, 1, repetition, 4, 1, Integer.toString(errorCode.getCode()));
 211  325
         Terser.set(err, 1, repetition, 4, 2, errorCode.getMessage());
 212  325
         Terser.set(err, 1, repetition, 4, 3, ErrorCode.codeTable());
 213  325
         Terser.set(err, 1, repetition, 4, 5, getMessage());
 214  325
         return response;
 215  
     }
 216  
 
 217  
     public String getMessage() {
 218  1285
         String message = getMessageWithoutLocation();
 219  1285
         StringBuilder msg = new StringBuilder(message);
 220  1285
         if (getLocation() != null && !getLocation().isUnknown()) {
 221  70
             msg.append(" at ").append(getLocation().toString());
 222  
         }
 223  1285
         return msg.toString();
 224  
     }
 225  
 
 226  
     public String getMessageWithoutLocation() {
 227  1520
         String message = super.getMessage();
 228  1520
         if (message == null) message = "Exception";
 229  1520
         return message;
 230  
     }
 231  
 
 232  
 }