1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
44
45
46
47
48
49
50
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
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
72
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
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
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
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
112 codeSlot.getValueList().getValue().clear();
113 codeSlot.getValueList().getValue().add(sb.toString());
114 }
115 }
116 }