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 private ErrorCode errorCode = ErrorCode.APPLICATION_INTERNAL_ERROR;
48 private Severity severity = Severity.ERROR;
49 private Message responseMessage;
50
51
52 public AbstractHL7Exception() {
53 super();
54 }
55
56 public AbstractHL7Exception(String message, Throwable cause) {
57 super(message, cause);
58 }
59
60 public AbstractHL7Exception(String message) {
61 super(message);
62 }
63
64 public AbstractHL7Exception(Throwable cause) {
65 super(cause);
66 }
67
68 public Location getLocation() {
69 return location;
70 }
71
72 public void setLocation(Location location) {
73 this.location = location;
74 }
75
76 public void setFieldPosition(int pos) {
77 if (location == null)
78 location = new Location();
79 location.withField(pos);
80 }
81
82 public void setSegmentName(String segmentName) {
83 if (location == null)
84 location = new Location();
85 location.withSegmentName(segmentName);
86 }
87
88 public void setSegmentRepetition(int segmentRepetition) {
89 if (location == null)
90 location = new Location();
91 location.withSegmentRepetition(segmentRepetition);
92 }
93
94 public int getErrorCode() {
95 return errorCode.getCode();
96 }
97
98 public void setErrorCode(int errorCode) {
99 this.errorCode = ErrorCode.errorCodeFor(errorCode);
100 }
101
102 public ErrorCode getError() {
103 return errorCode;
104 }
105
106 public void setError(ErrorCode errorCode) {
107 this.errorCode = errorCode;
108 }
109
110 public Severity getSeverity() {
111 return severity;
112 }
113
114 public void setSeverity(Severity severity) {
115 this.severity = severity;
116 }
117
118 public Message getResponseMessage() {
119 return responseMessage;
120 }
121
122 public void setResponseMessage(Message responseMessage) {
123 this.responseMessage = responseMessage;
124 }
125
126
127
128
129
130
131
132
133
134 public Message/ca/uhn/hl7v2/model/Message.html#Message">Message populateResponse(Message emptyResponse, AcknowledgmentCode acknowledgmentCode, int repetition)
135 throws HL7Exception {
136 if (responseMessage == null) {
137 if (acknowledgmentCode == null) acknowledgmentCode = AcknowledgmentCode.AA;
138 if (V25.isGreaterThan(versionOf(emptyResponse.getVersion()))) {
139 responseMessage = populateResponseBefore25(emptyResponse, acknowledgmentCode, repetition);
140 } else {
141 responseMessage = populateResponseAsOf25(emptyResponse, acknowledgmentCode, repetition);
142 }
143 }
144 return responseMessage;
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158 private Messagen/hl7v2/model/Message.html#Message">Message populateResponseAsOf25(Message response, AcknowledgmentCode acknowledgmentCode,
159 int repetition) throws HL7Exception {
160
161 Segment="../../../ca/uhn/hl7v2/model/Segment.html#Segment">Segment msa = (Segment) response.get("MSA");
162 Terser.set(msa, 1, 0, 1, 1, acknowledgmentCode.name());
163 Segment="../../../ca/uhn/hl7v2/model/Segment.html#Segment">Segment err = (Segment) response.get("ERR", repetition);
164 if (location != null) {
165 if (location.getSegmentName() != null)
166 Terser.set(err, 2, 0, 1, 1, location.getSegmentName());
167 if (location.getSegmentRepetition() > 0)
168 Terser.set(err, 2, 0, 2, 1, Integer.toString(location.getSegmentRepetition()));
169 if (location.getField() > 0)
170 Terser.set(err, 2, 0, 3, 1, Integer.toString(location.getField()));
171 if (location.getFieldRepetition() > 0)
172 Terser.set(err, 2, 0, 4, 1, Integer.toString(location.getFieldRepetition()));
173 if (location.getComponent() > 0)
174 Terser.set(err, 2, 0, 5, 1, Integer.toString(location.getComponent()));
175 if (location.getSubcomponent() > 0)
176 Terser.set(err, 2, 0, 6, 1, Integer.toString(location.getSubcomponent()));
177 }
178 Terser.set(err, 3, 0, 1, 1, Integer.toString(errorCode.getCode()));
179 Terser.set(err, 3, 0, 2, 1, errorCode.getMessage());
180 Terser.set(err, 3, 0, 3, 1, ErrorCode.codeTable());
181 Terser.set(err, 3, 0, 9, 1, getMessage());
182 Terser.set(err, 4, 0, 1, 1, "E");
183 return response;
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197 private Messagehl7v2/model/Message.html#Message">Message populateResponseBefore25(Message response, AcknowledgmentCode acknowledgmentCode,
198 int repetition) throws HL7Exception {
199
200 Segment="../../../ca/uhn/hl7v2/model/Segment.html#Segment">Segment msa = (Segment) response.get("MSA");
201 Terser.set(msa, 1, 0, 1, 1, acknowledgmentCode.name());
202 Terser.set(msa, 3, 0, 1, 1, errorCode.getMessage());
203 Segment="../../../ca/uhn/hl7v2/model/Segment.html#Segment">Segment err = (Segment) response.get("ERR");
204 if (location != null) {
205 if (location.getSegmentName() != null)
206 Terser.set(err, 1, repetition, 1, 1, location.getSegmentName());
207 if (location.getField() > 0)
208 Terser.set(err, 1, repetition, 3, 1, Integer.toString(location.getField()));
209 }
210 Terser.set(err, 1, repetition, 4, 1, Integer.toString(errorCode.getCode()));
211 Terser.set(err, 1, repetition, 4, 2, errorCode.getMessage());
212 Terser.set(err, 1, repetition, 4, 3, ErrorCode.codeTable());
213 Terser.set(err, 1, repetition, 4, 5, getMessage());
214 return response;
215 }
216
217 public String getMessage() {
218 String message = getMessageWithoutLocation();
219 StringBuilder msg = new StringBuilder(message);
220 if (getLocation() != null && !getLocation().isUnknown()) {
221 msg.append(" at ").append(getLocation().toString());
222 }
223 return msg.toString();
224 }
225
226 public String getMessageWithoutLocation() {
227 String message = super.getMessage();
228 if (message == null) message = "Exception";
229 return message;
230 }
231
232 }