1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.openehealth.ipf.platform.camel.hl7;
18
19 import ca.uhn.hl7v2.AcknowledgmentCode;
20 import ca.uhn.hl7v2.ErrorCode;
21 import ca.uhn.hl7v2.HL7Exception;
22 import ca.uhn.hl7v2.HapiContext;
23 import ca.uhn.hl7v2.model.Message;
24 import ca.uhn.hl7v2.parser.GenericParser;
25 import ca.uhn.hl7v2.parser.Parser;
26 import org.apache.camel.Exchange;
27 import org.apache.camel.Expression;
28 import org.apache.camel.Predicate;
29 import org.apache.camel.Processor;
30 import org.apache.camel.builder.ValueBuilder;
31 import org.apache.camel.component.hl7.HL7;
32 import org.openehealth.ipf.modules.hl7.validation.Validator;
33 import org.openehealth.ipf.platform.camel.core.adapter.ValidatorAdapter;
34
35 import static java.util.Objects.requireNonNull;
36
37
38
39
40
41 public final class HL7v2 {
42
43 private static final Parser FALLBACK = new GenericParser();
44
45 private HL7v2() {
46 }
47
48
49
50
51 public static ValueBuilder get(String terserSpec) {
52 return HL7.terser(terserSpec);
53 }
54
55
56
57
58 public static Expression set(String terserSpec, Expression value) {
59 return new TerserSetExpression(terserSpec, value);
60 }
61
62
63
64
65 public static ValueBuilder ack() {
66 return new ValueBuilder(HL7.ack());
67 }
68
69
70
71
72 public static ValueBuilder ack(AcknowledgmentCode acknowledgmentCode) {
73 return ack(acknowledgmentCode, null, ErrorCode.APPLICATION_INTERNAL_ERROR);
74 }
75
76
77
78
79 public static ValueBuilder ack(AcknowledgmentCode acknowledgmentCode, String message, ErrorCode errorCode) {
80 return new ValueBuilder(HL7.ack(acknowledgmentCode, message, errorCode));
81 }
82
83
84
85
86
87
88
89
90 public static ValueBuilder response(String eventType, String triggerEvent) {
91 return new ValueBuilder(new ResponseExpression(eventType, triggerEvent));
92 }
93
94
95
96
97
98
99
100
101 public static ValueBuilder staticResponse(String message) {
102 return new ValueBuilder(new StaticResponseExpression(message));
103 }
104
105
106
107
108
109
110
111
112 public static Processor validatingProcessor() {
113 return validatingProcessor(null);
114 }
115
116
117
118
119
120
121
122
123
124
125 public static Processor validatingProcessor(final HapiContext context) {
126 return exchange -> {
127 if (! ValidatorAdapter.validationEnabled(exchange)) {
128 return;
129 }
130
131 Message msg = bodyMessage(exchange);
132 Validator.validate(msg, context);
133 };
134 }
135
136
137
138
139 public static Predicate messageConforms() {
140 return HL7.messageConforms();
141 }
142
143
144
145
146 public static Predicate messageConformsTo(HapiContext hapiContext) {
147 return HL7.messageConformsTo(hapiContext);
148 }
149
150
151
152
153
154 public static Predicate messageConformsTo(Expression expression) {
155 return HL7.messageConformsTo(expression);
156 }
157
158
159
160
161
162
163
164
165 public static Message bodyMessage(Exchange exchange) throws HL7Exception {
166 Object body = exchange.getIn().getBody();
167 Message message;
168
169 if (body instanceof Message) {
170 message = (Message) body;
171 } else if (body instanceof String) {
172 HapiContext context = exchange.getIn().getHeader("CamelHL7Context", HapiContext.class);
173 Parser parser = context != null ? context.getGenericParser() : FALLBACK;
174 message = parser.parse((String) body);
175 } else {
176
177 message = exchange.getIn().getBody(Message.class);
178 }
179 requireNonNull(message, "Exchange does not contain or can be converted to the required 'ca.uhn.hl7v2.model.Message' type");
180 return message;
181 }
182
183
184 }