View Javadoc
1   /*
2    * Copyright 2012 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.platform.camel.ihe.xds.iti18;
17  
18  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.query.AdhocQueryRequest;
19  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.SlotType1;
20  import org.openehealth.ipf.commons.ihe.xds.core.transform.requests.query.QuerySlotHelper;
21  
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  /**
27   * @author Dmytro Rud
28   */
29  public class CodeSlotsNormalizer {
30  
31      private static List<String> getSlotValues(SlotType1 slot) {
32          List<String> result = new ArrayList<>();
33          if ((slot != null) && (slot.getValueList() != null)) {
34              for (String values : slot.getValueList().getValue()) {
35                  result.addAll(QuerySlotHelper.decodeStringList(values));
36              }
37          }
38          return result;
39      }
40  
41  
42      /**
43       * Converts pairs of code and code scheme values (IHE ITI TF v.5)
44       * to HL7v2-based code values (IHE ITI TF v.8).
45       *
46       * @param adhocQueryRequest
47       *      original request, will be modified in-place.
48       * @param codeSlotName
49       *      name of the slot containing codes.  The scheme slot name will be
50       *      created by adding a suffix "Scheme" to this value.
51       */
52      public static void normalizeCodeSlots(AdhocQueryRequest adhocQueryRequest, String codeSlotName) {
53          final String schemeSlotName = codeSlotName + "Scheme";
54  
55          List<SlotType1> codeSlots = new ArrayList<>();
56          List<SlotType1> schemeSlots = new ArrayList<>();
57  
58          // collect code and code scheme slots, remove the latter from the query
59          Iterator<SlotType1> iterator = adhocQueryRequest.getAdhocQuery().getSlot().iterator();
60          while (iterator.hasNext()) {
61              SlotType1 slot = iterator.next();
62              if (codeSlotName.equals(slot.getName())) {
63                  codeSlots.add(slot);
64              }
65              else if (schemeSlotName.equals(slot.getName())) {
66                  schemeSlots.add(slot);
67                  iterator.remove();
68              }
69          }
70  
71          // it's OK to have only code slots, but when scheme slots are present,
72          // their count must match with the count of code slots
73          if (schemeSlots.isEmpty()) {
74              return;
75          }
76          if (codeSlots.size() != schemeSlots.size()) {
77              throw new RuntimeException("Counts of " + codeSlotName +
78                      " slots and corresponding code scheme slots are not equal");
79          }
80  
81          // combine slots pairwise
82          for (int i = 0; i < codeSlots.size(); ++i) {
83              SlotType1 codeSlot = codeSlots.get(i);
84              SlotType1 schemeSlot = schemeSlots.get(i);
85  
86              List<String> codeValues = getSlotValues(codeSlot);
87              List<String> schemeValues = getSlotValues(schemeSlot);
88  
89              // check equality of value counts
90              if (codeValues.size() != schemeValues.size()) {
91                  throw new RuntimeException(
92                          "Counts of code and code scheme values are not equal in the " +
93                          codeSlotName + " slot pair No. " + i);
94              }
95  
96              if (codeValues.isEmpty()) {
97                  continue;
98              }
99  
100             // create lists of HL7 v2 code representations
101             StringBuilder sb = new StringBuilder("(");
102             for (int j = 0; j < codeValues.size(); ++j) {
103                 sb.append('\'')
104                   .append(codeValues.get(j).replace("'", "''"))
105                   .append("^^")
106                   .append(schemeValues.get(j).replace("'", "''"))
107                   .append("',");
108             }
109             sb.deleteCharAt(sb.length() - 1).append(')');
110 
111             // replace code slot contents with the new value
112             codeSlot.getValueList().getValue().clear();
113             codeSlot.getValueList().getValue().add(sb.toString());
114         }
115     }
116 }