View Javadoc
1   /*
2    * Copyright 2009 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *     
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Transformation logic for a {@link PatientInfo}.
26   * <p>
27   * This class offers transformation between {@link PatientInfo} and an HL7v2.5
28   * PID string list.
29   * @author Jens Riemschneider
30   */
31  public class PatientInfoTransformer {
32  
33      /**
34       * Creates a {@link PatientInfo} instance via an HL7 XCN string.
35       * @param hl7PID
36       *          the HL7 PID strings. Can be <code>null</code>.
37       * @return the created {@link PatientInfo} instance. <code>null</code> if no relevant 
38       *          data was found in the HL7 string or the input was <code>null</code>.
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       * Transforms a {@link PatientInfo} instance into the HL7 representation. 
66       * @param patientInfo
67       *          the patient info to transform. Can be <code>null</code>.
68       * @return the HL7 representation. <code>null</code> if the input was <code>null</code> 
69       *          or the resulting HL7 string would be empty.
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 }