1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.commons.ihe.xds.core.transform.hl7;
17
18 import ca.uhn.hl7v2.parser.PipeParser;
19 import org.apache.commons.lang3.StringUtils;
20 import org.openehealth.ipf.commons.ihe.xds.core.metadata.PatientInfo;
21
22 import java.util.*;
23
24
25
26
27
28
29
30
31 public class PatientInfoTransformer {
32
33
34
35
36
37
38
39
40 public PatientInfo fromHL7(List<String> hl7PID) {
41 if (hl7PID == null || hl7PID.isEmpty()) {
42 return null;
43 }
44
45 PatientInfo patientInfo = new PatientInfo();
46
47 for (String hl7PIDLine : hl7PID) {
48 String[] fields = PipeParser.split(hl7PIDLine.trim(), "|");
49 if (fields.length == 2) {
50 ListIterator<String> iterator = patientInfo.getHl7FieldIterator(fields[0]);
51 while (iterator.hasNext()) {
52 iterator.next();
53 }
54 String[] strings = PipeParser.split(fields[1], "~");
55 for (String string : strings) {
56 iterator.add(string);
57 }
58 }
59 }
60
61 return patientInfo;
62 }
63
64
65
66
67
68
69
70
71 public List<String> toHL7(PatientInfo patientInfo) {
72 if (patientInfo == null) {
73 return Collections.emptyList();
74 }
75
76 List<String> result = new ArrayList<>();
77
78 patientInfo.getAllFieldIds().stream().sorted(new PatientInfo.Hl7FieldIdComparator()).forEach(fieldId -> {
79 ListIterator<String> iterator = patientInfo.getHl7FieldIterator(fieldId);
80 String prefix = fieldId + '|';
81 StringBuilder sb = new StringBuilder(prefix);
82 while (iterator.hasNext()) {
83 String repetition = StringUtils.trimToEmpty(iterator.next());
84 if ((repetition.length() > 255 - sb.length()) && (sb.length() > prefix.length())) {
85 sb.setLength(sb.length() - 1);
86 result.add(sb.toString());
87 sb.setLength(prefix.length());
88 }
89 sb.append(repetition).append('~');
90 }
91 if (sb.length() > prefix.length() + 1) {
92 sb.setLength(sb.length() - 1);
93 result.add(sb.toString());
94 }
95 });
96
97 return result;
98 }
99
100 }