-
Notifications
You must be signed in to change notification settings - Fork 5
/
CodeSystemConverter.java
534 lines (488 loc) · 20.9 KB
/
CodeSystemConverter.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
package org.openconceptlab.fhir.converter;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hl7.fhir.r4.model.*;
import org.hl7.fhir.r4.model.CodeSystem.ConceptPropertyComponent;
import org.hl7.fhir.r4.model.codesystems.PublicationStatus;
import org.openconceptlab.fhir.model.*;
import static org.openconceptlab.fhir.util.OclFhirConstants.*;
import static org.openconceptlab.fhir.util.OclFhirUtil.*;
import org.openconceptlab.fhir.model.Organization;
import org.openconceptlab.fhir.repository.*;
import org.openconceptlab.fhir.util.OclFhirConstants;
import org.openconceptlab.fhir.util.OclFhirUtil;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
/**
* The CodeSystemConverter.
* @author harpatel1
*/
@Component
public class CodeSystemConverter extends BaseConverter {
public static final String DEFAULT_RES_VERSION = "0.1";
private static final Log log = LogFactory.getLog(CodeSystemConverter.class);
public CodeSystemConverter(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<CodeSystem> convertToCodeSystem(List<Source> sources, boolean includeConcepts, int page) {
List<CodeSystem> codeSystems = new ArrayList<>();
sources.forEach(source -> {
// convert to base
CodeSystem codeSystem = toBaseCodeSystem(source);
if (includeConcepts) {
// add concepts
addConceptsToCodeSystem(codeSystem, source, page);
}
codeSystems.add(codeSystem);
});
return codeSystems;
}
private CodeSystem toBaseCodeSystem(final Source source){
CodeSystem codeSystem = new CodeSystem();
// Url
if(StringUtils.isNotBlank(source.getCanonicalUrl())) {
codeSystem.setUrl(source.getCanonicalUrl());
}
// id
codeSystem.setId(source.getMnemonic());
// identifier
getIdentifier(source.getUri())
.ifPresent(i -> codeSystem.getIdentifier().add(i));
// version
codeSystem.setVersion(source.getVersion());
// name
codeSystem.setName(source.getName());
// title
if(StringUtils.isNotBlank(source.getFullName())) {
codeSystem.setTitle(source.getFullName());
}
// status
addStatus(codeSystem, source.getRetired() != null ? source.getRetired() : False,
source.getReleased() != null ? source.getReleased() : False);
// language
codeSystem.setLanguage(source.getDefaultLocale());
// description
if(StringUtils.isNotBlank(source.getDescription()))
codeSystem.setDescription(source.getDescription());
// count
codeSystem.setCount(conceptRepository.findConceptCountInSource(source.getId()));
// property
addProperty(codeSystem);
// publisher
if (isValid(source.getPublisher()))
codeSystem.setPublisher(source.getPublisher());
// override default identifier with database value
// identifier, contact, jurisdiction
addJsonFields(codeSystem, isValid(source.getIdentifier()) && !EMPTY_JSON.equals(source.getIdentifier()) ? source.getIdentifier() : EMPTY,
source.getContact(), source.getJurisdiction());
// purpose
if (isValid(source.getPurpose()))
codeSystem.setPurpose(source.getPurpose());
// copyright
if (isValid(source.getCopyright()))
codeSystem.setCopyright(source.getCopyright());
// content_type
if (isValid(source.getContentType())) {
Optional<String> content = Stream.of(CodeSystem.CodeSystemContentMode.values()).map(Enum::toString).filter(m -> source.getContentType().equalsIgnoreCase(m))
.findAny();
if (content.isPresent()) {
codeSystem.setContent(CodeSystem.CodeSystemContentMode.fromCode(content.get().toLowerCase()));
} else {
codeSystem.setContent(CodeSystem.CodeSystemContentMode.NULL);
}
} else {
codeSystem.setContent(CodeSystem.CodeSystemContentMode.NULL);
}
// revision date
if (source.getRevisionDate() != null)
codeSystem.setDate(source.getRevisionDate());
return codeSystem;
}
private void addProperty(final CodeSystem codeSystem) {
// concept class
codeSystem.getProperty().add(getPropertyComponent(OclFhirConstants.CONCEPTCLASS,
SYSTEM_CC,
DESC_CC,
CodeSystem.PropertyType.STRING));
// data type
codeSystem.getProperty().add(getPropertyComponent(OclFhirConstants.DATATYPE,
SYSTEM_DT,
DESC_DT,
CodeSystem.PropertyType.STRING));
// inactive - http://hl7.org/fhir/concept-properties
codeSystem.getProperty().add(getPropertyComponent(INACTIVE, SYSTEM_HL7_CONCEPT_PROP,
DESC_HL7_CONCEPT_PROP, CodeSystem.PropertyType.CODING));
}
private CodeSystem.PropertyComponent getPropertyComponent(String code, String system, String description, CodeSystem.PropertyType type) {
CodeSystem.PropertyComponent component = new CodeSystem.PropertyComponent();
component.setCode(code);
component.setUri(system);
component.setDescription(description);
component.setType(type);
return component;
}
private void addConceptsToCodeSystem(final CodeSystem codeSystem, final Source source, int page) {
List<Concept> concepts = conceptRepository.findConcepts(source.getId(), PageRequest.of(page, 100));
for (Concept concept : concepts) {
CodeSystem.ConceptDefinitionComponent definitionComponent = new CodeSystem.ConceptDefinitionComponent();
// code
definitionComponent.setCode(concept.getMnemonic());
// display
List<LocalizedText> names = concept.getConceptsNames().stream()
.filter(c -> c.getLocalizedText() != null)
.map(ConceptsName::getLocalizedText)
.collect(Collectors.toList());
Optional<String> display = oclFhirUtil.getDisplayForLanguage(names, source.getDefaultLocale());
definitionComponent.setDisplay(display.orElse(EMPTY));
// definition
List<LocalizedText> definitions = concept.getConceptsDescriptions().stream()
.filter(c -> c.getLocalizedText() != null)
.filter(c -> isValid(c.getLocalizedText().getType()) && DEFINITION.equalsIgnoreCase(c.getLocalizedText().getType()))
.map(ConceptsDescription::getLocalizedText)
.collect(Collectors.toList());
Optional<String> definition = oclFhirUtil.getDisplayForLanguage(definitions, source.getDefaultLocale());
definitionComponent.setDefinition(definition.orElse(EMPTY));
// designation
addConceptDesignation(concept, definitionComponent);
// property - concept_class, data_type, ,inactive
definitionComponent.getProperty().add(new ConceptPropertyComponent(new CodeType(OclFhirConstants.CONCEPTCLASS),
new StringType(concept.getConceptClass())));
definitionComponent.getProperty().add(new ConceptPropertyComponent(new CodeType(OclFhirConstants.DATATYPE),
new StringType(concept.getDatatype())));
definitionComponent.getProperty().add(new ConceptPropertyComponent(new CodeType(INACTIVE),
new BooleanType(concept.getRetired())));
// add concept in CodeSystem
codeSystem.getConcept().add(definitionComponent);
}
}
public Parameters getLookupParameters(final Source source, final CodeType code, final CodeType displayLanguage) {
Optional<Concept> conceptOpt = oclFhirUtil.getSourceConcept(source, code.getCode(), EMPTY);
if (conceptOpt.isPresent()) {
Concept concept = conceptOpt.get();
Parameters parameters = new Parameters();
parameters.addParameter(getParameter(OclFhirConstants.NAME, source.getName()));
parameters.addParameter(getParameter(OclFhirConstants.VERSION, source.getVersion()));
List<LocalizedText> names = oclFhirUtil.getNames(concept);
getDisplayForLookUp(names, isValid(displayLanguage) ? displayLanguage.getCode() : EMPTY, source.getDefaultLocale())
.ifPresent(display -> parameters.addParameter(getParameter(DISPLAY, display)));
addDesignationParameters(parameters, names, getCode(displayLanguage));
return parameters;
} else {
throw new ResourceNotFoundException("The code " + code.getCode() + " is invalid.");
}
}
private Optional<String> anyDisplay(List<LocalizedText> names) {
return names.stream().map(LocalizedText::getName).findFirst();
}
private Optional<String> getDisplayForLookUp(List<LocalizedText> names, String displayLanguage, String defaultLocale) {
if (isValid(displayLanguage)) {
return oclFhirUtil.getDisplayForLanguage(names, displayLanguage);
}
Optional<String> defaultLocaleDisp = oclFhirUtil.getDisplayForLanguage(names, defaultLocale);
if (defaultLocaleDisp.isPresent()) {
return defaultLocaleDisp;
} else {
return anyDisplay(names);
}
}
private void addDesignationParameters(Parameters parameters, List<LocalizedText> names, String displayLanguage) {
names.stream()
.filter(name -> !isValid(displayLanguage) || name.getLocale().equals(displayLanguage))
.forEach(name -> {
parameters.addParameter().setName(DESIGNATION).setPart(getDesignationParameters(name));
});
}
public Parameters validateCode(final Source source, final String code, final StringType display, final CodeType displayLanguage) {
Parameters parameters = new Parameters();
BooleanType result = new BooleanType(False);
parameters.addParameter().setName(RESULT).setValue(result);
Optional<Concept> conceptOpt = oclFhirUtil.getSourceConcept(source, code, EMPTY);
if (conceptOpt.isPresent()) {
if (isValid(display)) {
StringType updated = newStringType(display.getValue().replaceAll("^\"", "")
.replaceAll("\"$", ""));
List<LocalizedText> names = oclFhirUtil.getNames(conceptOpt.get());
boolean match = oclFhirUtil.validateDisplay(names, updated, displayLanguage);
if (!match) {
parameters.addParameter().setName(MESSAGE).setValue(newStringType("Invalid display."));
} else {
result.setValue(True);
}
} else {
result.setValue(True);
}
}
return parameters;
}
private List<Parameters.ParametersParameterComponent> getDesignationParameters(final LocalizedText text) {
List<Parameters.ParametersParameterComponent> componentList = new ArrayList<>();
if (isValid(text.getLocale()))
componentList.add(getParameter(LANGUAGE, new CodeType(text.getLocale())));
if (isValid(text.getType()))
componentList.add(getParameter(USE, new Coding(EMPTY, EMPTY, text.getType())));
if (isValid(text.getName()))
componentList.add(getParameter(VALUE, new StringType(text.getName())));
return componentList;
}
private Parameters.ParametersParameterComponent getParameter(String name, Type value) {
Parameters.ParametersParameterComponent component = new Parameters.ParametersParameterComponent();
component.setName(name).setValue(value);
return component;
}
private Parameters.ParametersParameterComponent getParameter(String name, String value) {
Parameters.ParametersParameterComponent component = new Parameters.ParametersParameterComponent();
component.setName(name).setValue(new StringType(value));
return component;
}
public void createCodeSystem(CodeSystem codeSystem, String accessionId, String authToken) {
// validate and authenticate
OclEntity oclEntity = new OclEntity(codeSystem, accessionId, authToken);
UserProfile user = oclEntity.getUserProfile();
// base source
Source source = toBaseSource(codeSystem, user, oclEntity.getAccessionId());
// add parent and access
addParent(source, oclEntity.getOwner());
// add identifier, contact and jurisdiction
addJsonStrings(codeSystem, source);
// version-less source uri
String value = source.getUri().substring(0, source.getUri().lastIndexOf(FW_SLASH));
String versionLessSourceUri = value.substring(0, value.lastIndexOf(FW_SLASH)) + FW_SLASH;
// add concepts
List<Concept> concepts = toConcepts(codeSystem.getConcept(), codeSystem.getLanguage());
concepts.forEach(c -> {
c.setParent(source);
c.setPublicAccess(source.getPublicAccess());
c.setVersion("1");
c.setIsLatestVersion(True);
c.setReleased(c.getIsActive());
c.setRetired(c.getIsActive());
c.setDefaultLocale(source.getDefaultLocale());
c.setCreatedBy(user);
c.setUpdatedBy(user);
c.setVersionedObject(c);
c.setUri(versionLessSourceUri + "concepts/" + c.getMnemonic() + FW_SLASH + c.getVersion() + FW_SLASH);
c.setExtras(EMPTY_JSON);
});
// save source
sourceRepository.saveAndFlush(source);
log.info("saved source - " + source.getMnemonic());
// save concepts
List<Integer> conceptIds = new CopyOnWriteArrayList<>();
// concept, concept names, concept descriptions, localized texts
List<List<Concept>> conceptBatches = ListUtils.partition(concepts, 1000);
int i = 1;
for (List<Concept> cb: conceptBatches) {
log.info("Saving " + cb.size() + " concepts, batch " + i + " of " + conceptBatches.size());
batchConcepts(cb, conceptIds);
i++;
}
// update concept version = concept id
List<List<Integer>> conceptIdBatches = ListUtils.partition(conceptIds, 1000);
conceptIdBatches.forEach(this::batchUpdateConceptVersion);
// save concepts sources
conceptIdBatches.forEach(b -> batchUpdateConceptSources(b, source.getId().intValue()));
log.info("saved " + conceptIds.size() + " concepts");
}
private List<Concept> toConcepts(List<CodeSystem.ConceptDefinitionComponent> components, String defaultLocale) {
List<Concept> concepts = new ArrayList<>();
for (CodeSystem.ConceptDefinitionComponent component : components) {
Concept concept = toConcept(component, defaultLocale);
if (concept != null) concepts.add(concept);
}
return concepts;
}
private Concept toConcept(CodeSystem.ConceptDefinitionComponent component, String defaultLocale) {
Concept concept = new Concept();
String code = component.getCode();
if (isValid(code)) {
// code
concept.setMnemonic(code);
// name
concept.setName(code);
// definition
addDefinition(concept, component.getDefinition(), defaultLocale);
// designation
List<CodeSystem.ConceptDefinitionDesignationComponent> designationComponents = component.getDesignation();
List<LocalizedText> names = toLocalizedText(designationComponents, defaultLocale);
if (!names.isEmpty()) {
addDesignation(concept, names);
}
// property
List<ConceptPropertyComponent> properties = component.getProperty();
// -- concept class
concept.setConceptClass(getStringProperty(properties, OclFhirConstants.CONCEPT_CLASS));
// -- data type
concept.setDatatype(getStringProperty(properties, OclFhirConstants.DATATYPE));
// -- inactive
concept.setIsActive(!getBooleanProperty(properties, INACTIVE));
return concept;
}
return null;
}
private void addDefinition(Concept concept, String definition, String defaultLocale) {
if (isValid(definition)) {
LocalizedText text = new LocalizedText();
text.setName(definition);
text.setType(DEFINITION);
text.setLocale(defaultLocale);
text.setLocalePreferred(True);
ConceptsDescription description = new ConceptsDescription();
description.setConcept(concept);
description.setLocalizedText(text);
concept.getConceptsDescriptions().add(description);
}
}
private void addDesignation(Concept concept, List<LocalizedText> names) {
List<ConceptsName> conceptsNames = names.stream().map(name -> {
ConceptsName conceptsName = new ConceptsName();
conceptsName.setConcept(concept);
conceptsName.setLocalizedText(name);
return conceptsName;
}).collect(Collectors.toList());
concept.setConceptsNames(conceptsNames);
}
private List<LocalizedText> toLocalizedText(List<CodeSystem.ConceptDefinitionDesignationComponent> components, String defaultLocale) {
List<LocalizedText> texts = new ArrayList<>();
for (CodeSystem.ConceptDefinitionDesignationComponent component : components) {
String locale = component.getLanguage();
String type = component.getUse().getCode();
String name = component.getValue();
if (locale != null && name != null) {
LocalizedText text = new LocalizedText();
text.setLocale(locale);
if (type != null)
text.setType(type);
text.setName(name);
text.setLocalePreferred(locale.equals(defaultLocale));
texts.add(text);
}
}
return texts;
}
private Source toBaseSource(final CodeSystem codeSystem, final UserProfile user, final String uri) {
Source source = new Source();
// mnemonic
source.setMnemonic(codeSystem.getId());
// canonical url
source.setCanonicalUrl(codeSystem.getUrl());
// created by
source.setCreatedBy(user);
// updated by
source.setUpdatedBy(user);
// draft or unknown or empty
source.setIsActive(True);
source.setIsLatestVersion(True);
source.setRetired(False);
source.setReleased(False);
if (codeSystem.getStatus() != null) {
// active
if (PublicationStatus.ACTIVE.toCode().equals(codeSystem.getStatus().toCode())) {
source.setReleased(True);
// retired
} else if (PublicationStatus.RETIRED.toCode().equals(codeSystem.getStatus().toCode())) {
source.setRetired(True);
source.setReleased(False);
source.setIsActive(False);
source.setIsLatestVersion(False);
}
}
// version
source.setVersion(codeSystem.getVersion());
// default locale
source.setDefaultLocale(isValid(codeSystem.getLanguage()) ? codeSystem.getLanguage() : EN_LOCALE);
// uri
source.setUri(uri.replaceAll("(?i)"+ Pattern.quote(CODESYSTEM), "sources"));
// active concepts
source.setActiveConcepts(codeSystem.getConcept().size());
// active mappings
source.setActiveMappings(0);
// name
String name = isValid(codeSystem.getName()) ? codeSystem.getName() : codeSystem.getId();
source.setName(name);
// content type
if (codeSystem.getContent() != null)
source.setContentType(codeSystem.getContent().toCode());
// copyright
if (isValid(codeSystem.getCopyright()))
source.setCopyright(codeSystem.getCopyright());
// description
if (isValid(codeSystem.getDescription()))
source.setDescription(codeSystem.getDescription());
// title
if (isValid(codeSystem.getTitle()))
source.setFullName(codeSystem.getTitle());
// publisher
if (isValid(codeSystem.getPublisher()))
source.setPublisher(codeSystem.getPublisher());
// purpose
if (isValid(codeSystem.getPurpose()))
source.setPurpose(codeSystem.getPurpose());
// revision date
if (codeSystem.getDate() != null)
source.setRevisionDate(codeSystem.getDate());
// extras
source.setExtras(EMPTY_JSON);
return source;
}
private void addParent(final Source source, final BaseOclEntity owner) {
if (owner instanceof Organization) {
Organization organization = (Organization) owner;
source.setOrganization(organization);
source.setPublicAccess(organization.getPublicAccess());
} else if (owner instanceof UserProfile){
source.setUserId((UserProfile) owner);
}
}
public void retireCodeSystem(String id, String version, StringType owner, String authToken) {
// id, version and owner are all required
if (!isValid(id))
throw new InvalidRequestException("The id can not be empty. Please provide id.");
if (!isValid(version))
throw new InvalidRequestException("The version can not be empty. Please provide version.");
if (!isValid(owner))
throw new InvalidRequestException("The owner can not be empty.");
// validate ownerType and owner
if (!isValid(owner))
throw new InvalidRequestException("Invalid owner.");
String ownerType = getOwnerType(owner.getValue());
String value = getOwner(owner.getValue());
if (ORG.equals(ownerType)) {
validateOrg(value);
} else if (USER.equals(ownerType)){
validateUser(value);
} else {
throw new InvalidRequestException("Invalid owner type.");
}
// validate token and authenticate
AuthtokenToken token = validateToken(authToken);
authenticate(token, USER.equals(ownerType) ? value : null,
ORG.equals(ownerType) ? value : null);
// find source based on ownerType and owner
Source source;
if (ORG.equals(ownerType)) {
source = sourceRepository.findFirstByMnemonicAndVersionAndOrganizationMnemonic(id, version, value);
} else {
source = sourceRepository.findFirstByMnemonicAndVersionAndUserIdUsername(id, version, value);
}
if (source == null)
throw new ResourceNotFoundException(String.format("The CodeSystem of id %s and version %s does not exist.", id, version));
// retire source and update
source.setRetired(True);
sourceRepository.saveAndFlush(source);
}
}