|
4 | 4 | import org.drools.decisiontable.SpreadsheetCompiler; |
5 | 5 | import org.kie.api.runtime.KieContainer; |
6 | 6 | import org.kie.api.runtime.KieSession; |
| 7 | +import org.openmrs.Concept; |
| 8 | +import org.openmrs.ConceptMap; |
| 9 | +import org.openmrs.ConceptMapType; |
| 10 | +import org.openmrs.api.context.Context; |
7 | 11 | import org.openmrs.module.drools.session.ExternalEvaluatorManager; |
8 | 12 | import org.openmrs.module.drools.session.DroolsSessionConfig; |
9 | 13 | import org.slf4j.Logger; |
10 | 14 | import org.slf4j.LoggerFactory; |
11 | 15 |
|
12 | 16 | import java.io.*; |
| 17 | +import java.util.HashSet; |
| 18 | +import java.util.List; |
13 | 19 | import java.util.Map; |
14 | 20 | import java.util.Objects; |
| 21 | +import java.util.Set; |
15 | 22 |
|
16 | 23 | public class CommonUtils { |
17 | 24 |
|
@@ -85,4 +92,83 @@ public static void convertExcelRulesToDrl(String excelFilePath, String outputDrl |
85 | 92 | } |
86 | 93 |
|
87 | 94 | } |
| 95 | + |
| 96 | + /** |
| 97 | + * Gets a concept by its SAME-AS mapping to an external source. |
| 98 | + * Unlike ConceptService.getConceptByMapping(), this method only considers |
| 99 | + * SAME-AS mappings, avoiding the "Multiple non-retired concepts found" error |
| 100 | + * that occurs when a concept has both SAME-AS and NARROWER-THAN mappings. |
| 101 | + * |
| 102 | + * @param code The concept code in the external source |
| 103 | + * @param sourceName The name of the concept source (e.g., "CIEL") |
| 104 | + * @return The concept with a SAME-AS mapping to the given code, or null if not found |
| 105 | + */ |
| 106 | + public static Concept getConceptBySameAsMapping(String code, String sourceName) { |
| 107 | + if (code == null || sourceName == null) { |
| 108 | + return null; |
| 109 | + } |
| 110 | + |
| 111 | + List<Concept> concepts = Context.getConceptService().getConceptsByMapping(code, sourceName, false); |
| 112 | + if (concepts == null || concepts.isEmpty()) { |
| 113 | + return null; |
| 114 | + } |
| 115 | + |
| 116 | + // Filter for SAME-AS mapping type only |
| 117 | + for (Concept concept : concepts) { |
| 118 | + for (ConceptMap mapping : concept.getConceptMappings()) { |
| 119 | + if (mapping.getConceptReferenceTerm() != null |
| 120 | + && mapping.getConceptReferenceTerm().getConceptSource() != null |
| 121 | + && sourceName.equals(mapping.getConceptReferenceTerm().getConceptSource().getName()) |
| 122 | + && code.equals(mapping.getConceptReferenceTerm().getCode())) { |
| 123 | + ConceptMapType mapType = mapping.getConceptMapType(); |
| 124 | + if (mapType != null && "SAME-AS".equalsIgnoreCase(mapType.getName())) { |
| 125 | + return concept; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + // Fall back to first result if no SAME-AS found (backwards compatibility) |
| 132 | + log.warn("No SAME-AS mapping found for code {} from source {}, returning first match", code, sourceName); |
| 133 | + return concepts.get(0); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Gets all concepts that have a SAME-AS or NARROWER-THAN mapping to the given code. |
| 138 | + * This is useful for finding concepts that represent the same or more specific |
| 139 | + * versions of a given terminology code (e.g., SNOMED CT). |
| 140 | + * |
| 141 | + * @param code The concept code in the external source |
| 142 | + * @param sourceName The name of the concept source (e.g., "SNOMED CT") |
| 143 | + * @return A set of concepts with SAME-AS or NARROWER-THAN mappings, or empty set if none found |
| 144 | + */ |
| 145 | + public static Set<Concept> getConceptsBySameAsOrNarrowerThanMapping(String code, String sourceName) { |
| 146 | + Set<Concept> result = new HashSet<>(); |
| 147 | + if (code == null || sourceName == null) { |
| 148 | + return result; |
| 149 | + } |
| 150 | + |
| 151 | + List<Concept> concepts = Context.getConceptService().getConceptsByMapping(code, sourceName, false); |
| 152 | + if (concepts == null || concepts.isEmpty()) { |
| 153 | + return result; |
| 154 | + } |
| 155 | + |
| 156 | + for (Concept concept : concepts) { |
| 157 | + for (ConceptMap mapping : concept.getConceptMappings()) { |
| 158 | + if (mapping.getConceptReferenceTerm() != null |
| 159 | + && mapping.getConceptReferenceTerm().getConceptSource() != null |
| 160 | + && sourceName.equals(mapping.getConceptReferenceTerm().getConceptSource().getName()) |
| 161 | + && code.equals(mapping.getConceptReferenceTerm().getCode()) |
| 162 | + && mapping.getConceptMapType() != null) { |
| 163 | + String mapType = mapping.getConceptMapType().getName(); |
| 164 | + if ("SAME-AS".equalsIgnoreCase(mapType) || "NARROWER-THAN".equalsIgnoreCase(mapType)) { |
| 165 | + result.add(concept); |
| 166 | + break; |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return result; |
| 173 | + } |
88 | 174 | } |
0 commit comments