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 package ca.uhn.hl7v2.parser;
28
29 import java.lang.reflect.Constructor;
30
31 import ca.uhn.hl7v2.ErrorCode;
32 import ca.uhn.hl7v2.HL7Exception;
33 import ca.uhn.hl7v2.Version;
34 import ca.uhn.hl7v2.model.*;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38
39
40
41
42 public final class FixFieldDataType {
43
44 private FixFieldDataType() {}
45
46
47
48
49
50
51 public static final String DEFAULT_OBX2_TYPE_PROP = "ca.uhn.hl7v2.model.varies.default_obx2_type";
52
53
54
55
56
57
58
59
60 public static final String INVALID_OBX2_TYPE_PROP = "ca.uhn.hl7v2.model.varies.invalid_obx2_type";
61
62
63
64
65
66
67 public static final String DEFAULT_MFE5_TYPE_PROP = "ca.uhn.hl7v2.model.varies.default_mfe5_type";
68
69
70
71
72
73
74
75
76 public static final String INVALID_MFE5_TYPE_PROP = "ca.uhn.hl7v2.model.varies.invalid_mfe5_type";
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 public static final String ESCAPE_SUBCOMPONENT_DELIM_IN_PRIMITIVE = "ca.uhn.hl7v2.model.varies.escape_subcomponent_delim_in_primitive";
104
105
106 private static final Logger LOG = LoggerFactory.getLogger(Varies.class);
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 public static void fixOBX5(Segment segment, ModelClassFactory factory, ParserConfiguration parserConfiguration)
127 throws HL7Exception {
128 if (!segment.getName().contains("OBX")) {
129 throw new IllegalArgumentException("Expected OBX segment, but was: " + segment.getName());
130 }
131 String defaultOBX2Type = parserConfiguration.getDefaultObx2Type();
132 if (defaultOBX2Type == null) {
133 defaultOBX2Type = System.getProperty(DEFAULT_OBX2_TYPE_PROP);
134 }
135 String invalidOBX2Type = parserConfiguration.getInvalidObx2Type();
136 if (invalidOBX2Type == null) {
137 invalidOBX2Type = System.getProperty(INVALID_OBX2_TYPE_PROP);
138 }
139
140 fix(segment, 2, 5, defaultOBX2Type, invalidOBX2Type, factory, parserConfiguration);
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160 public static void fixMFE4(Segment segment, ModelClassFactory factory, ParserConfiguration parserConfiguration)
161 throws HL7Exception {
162 if (!(segment.getName().contains("MFE")) &&
163 Version.versionOf(segment.getMessage().getVersion()).isGreaterThan(Version.V23)) {
164 throw new IllegalArgumentException("Expected MFE segment, but was: " + segment.getName());
165 }
166
167 String defaultMFE5Type = parserConfiguration.getDefaultMfe5Type();
168 if (defaultMFE5Type == null) {
169 defaultMFE5Type = System.getProperty(DEFAULT_MFE5_TYPE_PROP);
170 }
171
172 String invalidMFE5Type = parserConfiguration.getInvalidMfe5Type();
173 if (invalidMFE5Type == null) {
174 invalidMFE5Type = System.getProperty(INVALID_MFE5_TYPE_PROP);
175 }
176
177 fix(segment, 5, 4, defaultMFE5Type, invalidMFE5Type, factory, parserConfiguration);
178 }
179
180
181
182
183
184
185
186
187
188
189
190
191
192 public static void fix(Segment segment, int typeField, int dataField, String defaultType, String invalidType, ModelClassFactory factory, ParserConfiguration parserConfiguration)
193 throws HL7Exception {
194 try {
195
196 Primitive./../../../ca/uhn/hl7v2/model/Primitive.html#Primitive">Primitive type = (Primitive) segment.getField(typeField, 0);
197 Type[] reps = segment.getField(dataField);
198 for (Type rep : reps) {
199 Variesref="../../../../ca/uhn/hl7v2/model/Varies.html#Varies">Varies v = (Varies)rep;
200 if (type.getValue() == null) {
201 if (defaultType != null) {
202 LOG.debug("setting default {}-{} type to {}", segment.getName(), typeField, defaultType);
203 type.setValue(defaultType);
204 }
205 }
206
207 if (type.getValue() == null) {
208 if (v.getData() != null) {
209 if (!(v.getData() instanceof Primitive"../../../../ca/uhn/hl7v2/model/Primitive.html#Primitive">Primitive) || ((Primitive) v.getData()).getValue() != null) {
210 throw new HL7Exception(String.format(
211 "A datatype for %s-%d must be specified in %s-%d.", segment.getName(), dataField, segment.getName(), typeField),
212 ErrorCode.REQUIRED_FIELD_MISSING);
213 }
214 }
215 }
216 else {
217
218 String version = segment.getMessage().getVersion();
219 String typeValue = type.getValue();
220 Class<? extends Type> c = factory.getTypeClass(typeValue, version);
221 if (c == null) {
222 if (invalidType != null) {
223 c = factory.getTypeClass(invalidType, version);
224 }
225
226 if (c == null) {
227 Primitive./../../../ca/uhn/hl7v2/model/Primitive.html#Primitive">Primitive obx1 = (Primitive) segment.getField(1, 0);
228 HL7Exceptionl#HL7Exception">HL7Exception h = new HL7Exception("'" +
229 type.getValue() + "' in record " +
230 obx1.getValue() + " is invalid for version " + version,
231 ErrorCode.DATA_TYPE_ERROR);
232 h.setSegmentName(segment.getName());
233 h.setFieldPosition(typeField);
234 throw h;
235 }
236 }
237
238 Type newTypeInstance;
239 try {
240 Constructor<? extends Type> constr = c.getConstructor(Message.class);
241 newTypeInstance = constr.newInstance(v.getMessage());
242 } catch (NoSuchMethodException e) {
243 Constructor<? extends Type> constr = c.getConstructor(Message.class, Integer.class);
244 newTypeInstance = constr.newInstance(v.getMessage(), 0);
245 }
246
247 boolean escapeSubcomponentDelimInPrimitive =
248 parserConfiguration.isEscapeSubcomponentDelimiterInPrimitive() ||
249 escapeSubcomponentDelimInPrimitive();
250
251
252 if (newTypeInstance instanceof Primitive) {
253 Type[] subComponentsInFirstField = getFirstComponentSubcomponentsOnlyIfMoreThanOne(v);
254 if (subComponentsInFirstField != null) {
255
256 if (escapeSubcomponentDelimInPrimitive) {
257
258 StringBuilder firstComponentValue = new StringBuilder();
259 for (Type stype : subComponentsInFirstField) {
260 if (firstComponentValue.length() != 0) {
261 char subComponentSeparator = EncodingCharacters.getInstance(segment.getMessage()).getSubcomponentSeparator();
262 firstComponentValue.append(subComponentSeparator);
263 }
264 firstComponentValue.append(stype.encode());
265 }
266
267 setFirstComponentPrimitiveValue(v, firstComponentValue.toString());
268
269 }
270
271 }
272 }
273
274 v.setData(newTypeInstance);
275 }
276
277 }
278
279 }
280 catch (HL7Exception e) {
281 throw e;
282 }
283 catch (Exception e) {
284 throw new HL7Exception(
285 e.getClass().getName() + " trying to set data type of " + segment.getName() + "-" + dataField, e);
286 }
287 }
288
289 private static boolean escapeSubcomponentDelimInPrimitive() {
290 String property = System.getProperty(ESCAPE_SUBCOMPONENT_DELIM_IN_PRIMITIVE);
291 return property == null || "true".equalsIgnoreCase(property);
292 }
293
294 private static void setFirstComponentPrimitiveValue(Varies v, String theValue) throws DataTypeException {
295 Composite="../../../../ca/uhn/hl7v2/model/Composite.html#Composite">Composite c = (Composite) v.getData();
296 Type firstComponent = c.getComponent(0);
297 setFirstComponentPrimitiveValue(firstComponent, theValue);
298 }
299
300
301 private static void setFirstComponentPrimitiveValue(Type theFirstComponent, String theValue)
302 throws DataTypeException {
303
304 if (theFirstComponent instanceof Varies) {
305 Varies/uhn/hl7v2/model/Varies.html#Varies">Varies firstComponentVaries = (Varies)theFirstComponent;
306 if (((Varies) theFirstComponent).getData() instanceof Composite) {
307 Type[] subComponents = ((Composite)firstComponentVaries.getData()).getComponents();
308 setFirstComponentPrimitiveValue(subComponents[0], theValue);
309 for (int i = 1; i < subComponents.length; i++) {
310 setFirstComponentPrimitiveValue(subComponents[i], "");
311 }
312 } else {
313 Primitive="../../../../ca/uhn/hl7v2/model/Primitive.html#Primitive">Primitive p = (Primitive) firstComponentVaries.getData();
314 p.setValue(theValue);
315 }
316 } else if (theFirstComponent instanceof Composite) {
317 Type[] subComponents = ((Composite)theFirstComponent).getComponents();
318 setFirstComponentPrimitiveValue(subComponents[0], theValue);
319 for (int i = 1; i < subComponents.length; i++) {
320 setFirstComponentPrimitiveValue(subComponents[i], "");
321 }
322 } else {
323 ((Primitive)theFirstComponent).setValue(theValue);
324 }
325 }
326
327
328
329
330
331 private static Type[] getFirstComponentSubcomponentsOnlyIfMoreThanOne(Varies v) throws DataTypeException {
332 if (v.getData() instanceof Composite) {
333 Composite="../../../../ca/uhn/hl7v2/model/Composite.html#Composite">Composite c = (Composite) v.getData();
334 Type firstComponent = c.getComponent(0);
335 if (firstComponent instanceof Varies) {
336 Varies/uhn/hl7v2/model/Varies.html#Varies">Varies firstComponentVaries = (Varies) firstComponent;
337 if (firstComponentVaries.getData() instanceof Composite) {
338 return ((Composite)firstComponentVaries.getData()).getComponents();
339 }
340 }
341 }
342 return null;
343 }
344 }