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.app;
29
30 import java.io.IOException;
31 import java.util.Date;
32 import java.util.GregorianCalendar;
33
34 import ca.uhn.hl7v2.HL7Exception;
35 import ca.uhn.hl7v2.model.DataTypeException;
36 import ca.uhn.hl7v2.model.Message;
37 import ca.uhn.hl7v2.model.Segment;
38 import ca.uhn.hl7v2.model.Structure;
39 import ca.uhn.hl7v2.model.primitive.CommonTS;
40 import ca.uhn.hl7v2.parser.DefaultModelClassFactory;
41 import ca.uhn.hl7v2.parser.ModelClassFactory;
42 import ca.uhn.hl7v2.parser.Parser;
43 import ca.uhn.hl7v2.util.MessageIDGenerator;
44 import ca.uhn.hl7v2.util.Terser;
45
46
47
48
49
50
51
52
53 public class DefaultApplication implements Application {
54
55
56 public DefaultApplication() {
57 }
58
59
60
61
62 public boolean canProcess(Message in) {
63 return true;
64 }
65
66
67
68
69
70 public Message processMessage(Message in) throws ApplicationException {
71 try {
72
73 Message out = makeACK(in);
74 fillDetails(out);
75 return out;
76 } catch (Exception e) {
77 throw new ApplicationException("Couldn't create response message: "
78 + e.getMessage());
79 }
80
81 }
82
83
84
85
86
87
88 public void fillDetails(Message ack) throws ApplicationException {
89 try {
90
91 Segment msa = (Segment) ack.get("MSA");
92 Terser.set(msa, 1, 0, 1, 1, "AR");
93 Terser.set(
94 msa,
95 3,
96 0,
97 1,
98 1,
99 "No appropriate destination could be found to which this message could be routed.");
100
101
102
103 Structure s = ack.get("ERR");
104 if (s != null) {
105 Segment err = (Segment) s;
106 Terser.set(err, 1, 0, 4, 1, "207");
107 Terser.set(err, 1, 0, 4, 2, "Application Internal Error");
108 Terser.set(err, 1, 0, 4, 3, "HL70357");
109 }
110
111 } catch (Exception e) {
112 throw new ApplicationException(
113 "Error trying to create Application Reject message: "
114 + e.getMessage());
115 }
116 }
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 public static Message makeACK(Message message) throws HL7Exception, IOException {
136 return makeACK((Segment)message.get("MSH"));
137 }
138
139 public static Message makeACK(Segment inboundHeader) throws HL7Exception,
140 IOException {
141 if (!inboundHeader.getName().equals("MSH"))
142 throw new HL7Exception(
143 "Need an MSH segment to create a response ACK (got "
144 + inboundHeader.getName() + ")");
145
146
147 Class<? extends Message> clazz = null;
148 try {
149 Message inbound = inboundHeader.getMessage();
150 Parser p = inbound.getParser();
151 ModelClassFactory mcf = p != null ? p.getFactory() : new DefaultModelClassFactory();
152 String version = inbound.getVersion();
153 if (version == null)
154 version = "2.4";
155 clazz = mcf.getMessageClass("ACK", version, false);
156 Message out = clazz.newInstance();
157 Terser terser = new Terser(out);
158
159
160 Segment outHeader = (Segment) out.get("MSH");
161 fillResponseHeader(inboundHeader, outHeader);
162
163 terser.set("/MSH-9-1", "ACK");
164 terser.set("/MSH-9-2", Terser.get(inboundHeader, 9, 0, 2, 1));
165 terser.set("/MSH-12", Terser.get(inboundHeader, 12, 0, 1, 1));
166 terser.set("/MSA-1", "AA");
167 terser.set("/MSA-2", Terser.get(inboundHeader, 10, 0, 1, 1));
168 return out;
169
170 } catch (Exception e) {
171 throw new HL7Exception("Can't instantiate ACK of class "
172 + clazz.getName(), e);
173 }
174
175 }
176
177
178
179
180
181
182
183
184 public static void fillResponseHeader(Segment inbound, Segment outbound)
185 throws HL7Exception, IOException {
186 if (!inbound.getName().equals("MSH")
187 || !outbound.getName().equals("MSH"))
188 throw new HL7Exception("Need MSH segments. Got "
189 + inbound.getName() + " and " + outbound.getName());
190
191
192 String encChars = Terser.get(inbound, 2, 0, 1, 1);
193 String fieldSep = Terser.get(inbound, 1, 0, 1, 1);
194 String procID = Terser.get(inbound, 11, 0, 1, 1);
195
196
197 Terser.set(outbound, 2, 0, 1, 1, encChars);
198 Terser.set(outbound, 1, 0, 1, 1, fieldSep);
199 GregorianCalendar now = new GregorianCalendar();
200 now.setTime(new Date());
201 Terser.set(outbound, 7, 0, 1, 1, CommonTS.toHl7TSFormat(now));
202 Terser.set(outbound, 10, 0, 1, 1, MessageIDGenerator.getInstance()
203 .getNewID());
204 Terser.set(outbound, 11, 0, 1, 1, procID);
205
206
207 Terser.set(outbound, 3, 0, 1, 1, Terser.get(inbound, 5, 0, 1, 1));
208 Terser.set(outbound, 4, 0, 1, 1, Terser.get(inbound, 6, 0, 1, 1));
209 Terser.set(outbound, 5, 0, 1, 1, Terser.get(inbound, 3, 0, 1, 1));
210 Terser.set(outbound, 6, 0, 1, 1, Terser.get(inbound, 4, 0, 1, 1));
211 }
212
213 }