-
Notifications
You must be signed in to change notification settings - Fork 5
/
ConceptMapConverter.java
344 lines (303 loc) · 14.5 KB
/
ConceptMapConverter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package org.openconceptlab.fhir.converter;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.r4.model.ConceptMap;
import org.hl7.fhir.r4.model.Enumerations;
import org.openconceptlab.fhir.model.Concept;
import org.openconceptlab.fhir.model.Mapping;
import org.openconceptlab.fhir.model.Source;
import org.openconceptlab.fhir.model.UserProfile;
import org.openconceptlab.fhir.repository.*;
import org.openconceptlab.fhir.util.OclFhirUtil;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
import java.util.*;
import java.util.stream.Collectors;
import static org.openconceptlab.fhir.util.OclFhirConstants.*;
import static org.openconceptlab.fhir.util.OclFhirUtil.*;
import static org.openconceptlab.fhir.util.OclFhirUtil.isValid;
@Component
public class ConceptMapConverter extends BaseConverter {
public ConceptMapConverter(SourceRepository sourceRepository, ConceptRepository conceptRepository, OclFhirUtil oclFhirUtil,
UserProfile oclUser, ConceptsSourceRepository conceptsSourceRepository, DataSource dataSource,
AuthtokenRepository authtokenRepository, UserProfilesOrganizationRepository userProfilesOrganizationRepository,
OrganizationRepository organizationRepository, UserRepository userRepository, CollectionRepository collectionRepository,
MappingRepository mappingRepository) {
super(sourceRepository, conceptRepository, oclFhirUtil, oclUser, conceptsSourceRepository, dataSource, authtokenRepository,
userProfilesOrganizationRepository, organizationRepository, userRepository, collectionRepository, mappingRepository);
}
public List<ConceptMap> convertToConceptMap(List<Source> sources, boolean includeMappings, int page) {
List<ConceptMap> conceptMaps = new ArrayList<>();
sources.forEach(source -> {
// convert to base concept map
ConceptMap conceptMap = toConceptMap(source);
if (includeMappings) {
// populate mappings
addMappingsToConceptMap(conceptMap, source.getId(), page);
}
conceptMaps.add(conceptMap);
});
return conceptMaps;
}
private ConceptMap toConceptMap(final Source source) {
ConceptMap conceptMap = new ConceptMap();
// Url
if(StringUtils.isNotBlank(source.getCanonicalUrl())) {
conceptMap.setUrl(source.getCanonicalUrl());
}
// id
conceptMap.setId(source.getMnemonic());
// identifier
getIdentifier(source.getUri().replace("sources", "ConceptMap"))
.ifPresent(conceptMap::setIdentifier);
// version
conceptMap.setVersion(source.getVersion());
// name
conceptMap.setName(source.getName());
// title
if(StringUtils.isNotBlank(source.getFullName())) {
conceptMap.setTitle(source.getFullName());
}
// status
addStatus(conceptMap, source.getRetired() != null ? source.getRetired() : False,
source.getReleased() != null ? source.getReleased() : False);
// description
if(StringUtils.isNotBlank(source.getDescription()))
conceptMap.setDescription(source.getDescription());
// publisher
if (isValid(source.getPublisher()))
conceptMap.setPublisher(source.getPublisher());
// override default identifier with database value
// identifier, contact, jurisdiction
addJsonFields(conceptMap, isValid(source.getIdentifier()) && !EMPTY_JSON.equals(source.getIdentifier()) ?
source.getIdentifier() : EMPTY, source.getContact(), source.getJurisdiction());
// purpose
if (isValid(source.getPurpose()))
conceptMap.setPurpose(source.getPurpose());
// copyright
if (isValid(source.getCopyright()))
conceptMap.setCopyright(source.getCopyright());
// revision date
if (source.getRevisionDate() != null)
conceptMap.setDate(source.getRevisionDate());
return conceptMap;
}
private void addMappingsToConceptMap(final ConceptMap conceptMap, final Long sourceId, int page) {
List<Mapping> mappings = mappingRepository.findMappings(sourceId, PageRequest.of(page, 100));
if (mappings != null && !mappings.isEmpty())
addMappings(conceptMap, mappings);
}
private String getFromConceptCode(final Mapping mapping) {
String fromConceptCode = mapping.getFromConceptCode();
if (!isValid(fromConceptCode)) {
Concept fromConcept = mapping.getFromConcept();
if (fromConcept != null)
return fromConcept.getMnemonic();
} else {
return fromConceptCode;
}
return EMPTY;
}
private String getToConceptCode(final Mapping mapping) {
String toConceptCode = mapping.getToConceptCode();
if (!isValid(toConceptCode)) {
Concept toConcept = mapping.getToConcept();
if (toConcept != null)
return toConcept.getMnemonic();
} else {
return toConceptCode;
}
return EMPTY;
}
private String getFromSourceUrl(final Mapping mapping) {
String fromSourceUrl = mapping.getFromSourceUrl();
if (!isValid(fromSourceUrl) || !fromSourceUrl.startsWith("http")) {
// only reason to resolve to canonical url from local url is to make sure compatibility with OCL imported
// data, since user can create mapping using local url in OCL. However, this should not be an issue when user
// creates mapping using OCL FHIR service because only canonical url is allowed as source/target system.
Source source = mapping.getFromSource();
if (source != null)
return source.getCanonicalUrl();
} else {
return fromSourceUrl;
}
return EMPTY;
}
private String getToSourceUrl(final Mapping mapping) {
String toSourceUrl = mapping.getToSourceUrl();
if (!isValid(toSourceUrl) || !toSourceUrl.startsWith("http")) {
Source source = mapping.getToSource();
if (source != null)
return source.getCanonicalUrl();
} else {
return toSourceUrl;
}
return EMPTY;
}
private void addMappings(final ConceptMap conceptMap, final List<Mapping> mappings) {
// creates ConceptMapGroups by grouping source/sourceversion/target/targetversion
Map<Object, List<ConceptMapGroup>> map = mappings.stream().map(ConceptMapGroup::new)
.filter(m -> isValid(m.getFromSystemUrl()) && isValid(m.getFromCode()) &&
isValid(m.getToSystemUrl()) && isValid(m.getToCode()) &&
isValid(m.getEquivalence()))
.collect(Collectors.groupingBy(m -> m.getFromSystemUrl() + m.getFromSystemVersion() + m.getToSystemUrl() + m.getToSystemVersion()));
map.forEach((k,groups) -> {
Optional<ConceptMap.ConceptMapGroupComponent> component = toConceptMapGroupComponent(groups);
component.ifPresent(c -> conceptMap.getGroup().add(c));
});
}
private Optional<ConceptMap.ConceptMapGroupComponent> toConceptMapGroupComponent(List<ConceptMapGroup> conceptMapGroups) {
if (conceptMapGroups != null && !conceptMapGroups.isEmpty()) {
// all conceptMapGroups are for a single parent group
ConceptMap.ConceptMapGroupComponent component = new ConceptMap.ConceptMapGroupComponent();
conceptMapGroups.stream().findAny().ifPresent(conceptMapGroup -> {
component.setSource(conceptMapGroup.getFromSystemUrl());
component.setTarget(conceptMapGroup.getToSystemUrl());
if (isValid(conceptMapGroup.getFromSystemVersion()))
component.setSourceVersion(conceptMapGroup.getFromSystemVersion());
if (isValid(conceptMapGroup.getToSystemVersion()))
component.setTargetVersion(conceptMapGroup.getToSystemVersion());
});
// 1 source code -> * target codes
// group by source code
Map<Object, List<ConceptMapGroup>> codeMap =
new TreeMap<>(conceptMapGroups.stream().collect(Collectors.groupingBy(ConceptMapGroup::getFromCode)));
codeMap.forEach((code, groups) -> {
// one source code
ConceptMap.SourceElementComponent sourceElementComponent = new ConceptMap.SourceElementComponent();
// add source code and display
groups.stream().findAny().ifPresent(group -> {
sourceElementComponent.setCode(group.getFromCode());
if (isValid(group.getFromDisplay()))
sourceElementComponent.setDisplay(group.getFromDisplay());
});
// add multiple target codes
groups.parallelStream().forEach(group -> {
ConceptMap.TargetElementComponent targetElementComponent = new ConceptMap.TargetElementComponent();
targetElementComponent.setCode(group.getToCode());
if (isValid(group.getToDisplay()))
targetElementComponent.setDisplay(group.getToDisplay());
Optional<Enumerations.ConceptMapEquivalence> conceptMapEquivalence = Arrays.stream(Enumerations.ConceptMapEquivalence.values())
.filter(e -> e.toCode().equals(group.getEquivalence())).findAny();
if (conceptMapEquivalence.isPresent()) {
targetElementComponent.setEquivalence(conceptMapEquivalence.get());
} else {
targetElementComponent.getExtensionFirstRep().setUrl("http://fhir.openconceptlab.org/ConceptMap/equivalence")
.setValue(newStringType(group.getEquivalence()));
}
sourceElementComponent.getTarget().add(targetElementComponent);
});
component.getElement().add(sourceElementComponent);
});
return Optional.of(component);
}
return Optional.empty();
}
static class ConceptMapGroup {
private String fromSystemUrl;
private String fromSystemVersion;
private String fromCode;
private String fromDisplay;
private String toSystemUrl;
private String toSystemVersion;
private String toCode;
private String toDisplay;
private String equivalence;
public ConceptMapGroup(final Mapping mapping) {
String fromSourceUrl = mapping.getFromSourceUrl();
if (!isValid(fromSourceUrl) || !fromSourceUrl.startsWith("http")) {
// only reason to resolve to canonical url from local url is to make sure compatibility with OCL imported
// data, since user can create mapping using local url in OCL. However, this should not be an issue when user
// creates mapping using OCL FHIR service because only canonical url is allowed as source/target system.
Source source = mapping.getFromSource();
if (source != null)
this.fromSystemUrl = source.getCanonicalUrl();
} else {
this.fromSystemUrl = fromSourceUrl;
}
String toSourceUrl = mapping.getToSourceUrl();
if (!isValid(toSourceUrl) || !toSourceUrl.startsWith("http")) {
Source source = mapping.getToSource();
if (source != null)
this.toSystemUrl = source.getCanonicalUrl();
} else {
this.toSystemUrl = toSourceUrl;
}
String fromConceptCode = mapping.getFromConceptCode();
if (!isValid(fromConceptCode)) {
Concept fromConcept = mapping.getFromConcept();
if (fromConcept != null)
this.fromCode = fromConcept.getMnemonic();
} else {
this.fromCode = fromConceptCode;
}
String toConceptCode = mapping.getToConceptCode();
if (!isValid(toConceptCode)) {
Concept toConcept = mapping.getToConcept();
if (toConcept != null)
this.toCode = toConcept.getMnemonic();
} else {
this.toCode = toConceptCode;
}
this.fromDisplay = mapping.getFromConceptName();
this.toDisplay = mapping.getToConceptName();
this.equivalence = mapping.getMapType();
this.fromSystemVersion = mapping.getFromSourceVersion();
this.toSystemVersion = mapping.getToSourceVersion();
}
public String getFromSystemUrl() {
return fromSystemUrl;
}
public void setFromSystemUrl(String fromSystemUrl) {
this.fromSystemUrl = fromSystemUrl;
}
public String getToSystemUrl() {
return toSystemUrl;
}
public void setToSystemUrl(String toSystemUrl) {
this.toSystemUrl = toSystemUrl;
}
public String getFromCode() {
return fromCode;
}
public void setFromCode(String fromCode) {
this.fromCode = fromCode;
}
public String getFromDisplay() {
return fromDisplay;
}
public void setFromDisplay(String fromDisplay) {
this.fromDisplay = fromDisplay;
}
public String getToCode() {
return toCode;
}
public void setToCode(String toCode) {
this.toCode = toCode;
}
public String getToDisplay() {
return toDisplay;
}
public void setToDisplay(String toDisplay) {
this.toDisplay = toDisplay;
}
public String getEquivalence() {
return equivalence;
}
public void setEquivalence(String equivalence) {
this.equivalence = equivalence;
}
public String getFromSystemVersion() {
return fromSystemVersion;
}
public void setFromSystemVersion(String fromSystemVersion) {
this.fromSystemVersion = fromSystemVersion;
}
public String getToSystemVersion() {
return toSystemVersion;
}
public void setToSystemVersion(String toSystemVersion) {
this.toSystemVersion = toSystemVersion;
}
}
}