| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 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 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 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 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 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 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
private Message populateResponseAsOf25(Message response, AcknowledgmentCode acknowledgmentCode, |
| 159 | |
int repetition) throws HL7Exception { |
| 160 | |
|
| 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 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
private Message populateResponseBefore25(Message response, AcknowledgmentCode acknowledgmentCode, |
| 198 | |
int repetition) throws HL7Exception { |
| 199 | |
|
| 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 | |
} |