-
Notifications
You must be signed in to change notification settings - Fork 5
/
BaseConverter.java
662 lines (597 loc) · 30.5 KB
/
BaseConverter.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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
package org.openconceptlab.fhir.converter;
import ca.uhn.fhir.rest.server.exceptions.AuthenticationException;
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.apache.bcel.classfile.Code;
import org.hl7.fhir.r4.model.*;
import org.hl7.fhir.r4.model.codesystems.PublicationStatus;
import org.openconceptlab.fhir.model.Collection;
import org.openconceptlab.fhir.model.Organization;
import org.openconceptlab.fhir.model.*;
import org.openconceptlab.fhir.repository.*;
import org.openconceptlab.fhir.util.OclFhirUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.*;
import static org.openconceptlab.fhir.util.OclFhirConstants.*;
import static org.openconceptlab.fhir.util.OclFhirUtil.*;
@Component
public class BaseConverter {
protected SourceRepository sourceRepository;
protected ConceptRepository conceptRepository;
protected OclFhirUtil oclFhirUtil;
protected UserProfile oclUser;
protected ConceptsSourceRepository conceptsSourceRepository;
protected AuthtokenRepository authtokenRepository;
protected UserProfilesOrganizationRepository userProfilesOrganizationRepository;
protected OrganizationRepository organizationRepository;
protected UserRepository userRepository;
protected SimpleJdbcInsert insertLocalizedText;
protected SimpleJdbcInsert insertConcept;
protected SimpleJdbcInsert insertMapping;
protected DataSource dataSource;
protected JdbcTemplate jdbcTemplate;
protected CollectionRepository collectionRepository;
protected SimpleJdbcInsert insertCollectionReference;
protected NamedParameterJdbcTemplate namedParameterJdbcTemplate;
protected MappingRepository mappingRepository;
private static final Log log = LogFactory.getLog(BaseConverter.class);
protected static final String DEFAULT_RES_VERSION = "0.1";
@Autowired
public BaseConverter(SourceRepository sourceRepository, ConceptRepository conceptRepository, OclFhirUtil oclFhirUtil,
UserProfile oclUser, ConceptsSourceRepository conceptsSourceRepository, DataSource dataSource,
AuthtokenRepository authtokenRepository, UserProfilesOrganizationRepository userProfilesOrganizationRepository,
OrganizationRepository organizationRepository, UserRepository userRepository, CollectionRepository collectionRepository,
MappingRepository mappingRepository) {
this.sourceRepository = sourceRepository;
this.conceptRepository = conceptRepository;
this.oclFhirUtil = oclFhirUtil;
this.oclUser = oclUser;
this.conceptsSourceRepository = conceptsSourceRepository;
this.dataSource = dataSource;
this.authtokenRepository = authtokenRepository;
this.userProfilesOrganizationRepository = userProfilesOrganizationRepository;
this.organizationRepository = organizationRepository;
this.userRepository = userRepository;
this.collectionRepository = collectionRepository;
this.mappingRepository = mappingRepository;
}
@PostConstruct
public void init() {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
insertLocalizedText = new SimpleJdbcInsert(jdbcTemplate).withTableName("localized_texts");
insertConcept = new SimpleJdbcInsert(jdbcTemplate).withTableName("concepts");
insertMapping = new SimpleJdbcInsert(jdbcTemplate).withTableName("mappings");
}
protected BaseOclEntity validateOwner(String org, String username) {
if (isValid(org)) {
return validateOrg(org);
} else {
return validateUser(username);
}
}
protected BaseOclEntity validateOrg(String org) {
Organization organization = organizationRepository.findByMnemonic(org);
if (organization == null) {
throw new InvalidRequestException("The organization of id = " + org + " does not exist.");
} else {
return organization;
}
}
protected BaseOclEntity validateUser(String username) {
UserProfile userProfile = userRepository.findByUsername(username);
if (userProfile == null) {
throw new InvalidRequestException("The user of username = " + username + " does not exist.");
} else {
return userProfile;
}
}
protected AuthtokenToken validateToken(String authToken) {
if (isValid(authToken)) {
String tokenStr = authToken.replaceAll("Token\\s+", EMPTY);
return authtokenRepository.findByKey(tokenStr.trim());
} else {
throw new AuthenticationException("The authentication token is not provided.");
}
}
protected void authenticate(AuthtokenToken token, String username, String org) {
if (token == null) {
throw new AuthenticationException("Invalid authentication token.");
}
if (isOclAdmin(token)) return;
if (isValid(username)) {
if (!username.equals(token.getUserProfile().getUsername())) {
throw new AuthenticationException("The " + username + " is not authorized to use the token provided.");
}
} else if (isValid(org)) {
boolean isMember = userProfilesOrganizationRepository.findByOrganizationMnemonic(org)
.stream()
.map(UserProfilesOrganization::getUserProfile)
.anyMatch(f -> f.getUsername().equals(token.getUserProfile().getUsername())
&& f.getAuthtokenTokens().stream().anyMatch(t -> t.getKey().equals(token.getKey())));
if (!isMember) {
throw new AuthenticationException("The user " + token.getUserProfile().getUsername() + " is not authorized to access " +
org + " organization.");
}
} else {
throw new InvalidRequestException("Owner can not be empty.");
}
}
protected void validateId(String username, String org, String id, String version, String resourceType) {
if (CODESYSTEM.equals(resourceType) || CONCEPTMAP.equals(resourceType)) {
Source userSource = sourceRepository.findFirstByMnemonicAndVersionAndUserIdUsername(id, version, username);
Source orgSource = sourceRepository.findFirstByMnemonicAndVersionAndOrganizationMnemonic(id, version, org);
if (userSource != null || orgSource != null) {
throw new ResourceVersionConflictException(String.format("The %s %s of version %s already exists.", resourceType, id, version));
}
} else if (VALUESET.equals(resourceType)) {
Collection userCollection = collectionRepository.findFirstByMnemonicAndVersionAndUserIdUsername(id, version, username);
Collection orgCollection = collectionRepository.findFirstByMnemonicAndVersionAndOrganizationMnemonic(id, version, org);
if (userCollection != null || orgCollection != null) {
throw new ResourceVersionConflictException(String.format("The %s %s of version %s already exists.", resourceType, id, version));
}
} else {
throw new InternalErrorException("Invalid resource type.");
}
}
protected void validateCanonicalUrl(String username, String org, String url, String version, String resourceType) {
if (CODESYSTEM.equals(resourceType) || CONCEPTMAP.equals(resourceType)) {
Source userSource = sourceRepository.findFirstByCanonicalUrlAndVersionAndUserIdUsername(url, version, username);
Source orgSource = sourceRepository.findFirstByCanonicalUrlAndVersionAndOrganizationMnemonic(url, version, org);
if (userSource != null || orgSource != null) {
throw new ResourceVersionConflictException(String.format("The %s of canonical url %s and version %s already exists.", resourceType, url, version));
}
} else if (VALUESET.equals(resourceType)) {
Collection userCollection = collectionRepository.findFirstByCanonicalUrlAndVersionAndUserIdUsername(url, version, username);
Collection orgCollection = collectionRepository.findFirstByCanonicalUrlAndVersionAndOrganizationMnemonic(url, version, org);
if (userCollection != null || orgCollection != null) {
throw new ResourceVersionConflictException(String.format("The %s of canonical url %s and version %s already exists.", resourceType, url, version));
}
} else {
throw new InternalErrorException("Invalid resource type.");
}
}
protected boolean checkHeadVersionId(String username, String org, String id, String resourceType) {
if (CODESYSTEM.equals(resourceType) || CONCEPTMAP.equals(resourceType)) {
Source userSource = sourceRepository.findFirstByMnemonicAndVersionAndUserIdUsername(id, HEAD, username);
Source orgSource = sourceRepository.findFirstByMnemonicAndVersionAndOrganizationMnemonic(id, HEAD, org);
if (userSource != null || orgSource != null) return true;
} else if (VALUESET.equals(resourceType)) {
Collection userCollection = collectionRepository.findFirstByMnemonicAndVersionAndUserIdUsername(id, HEAD, username);
Collection orgCollection = collectionRepository.findFirstByMnemonicAndVersionAndOrganizationMnemonic(id, HEAD, org);
if (userCollection != null || orgCollection != null) return true;
}
return false;
}
protected boolean checkHeadVersionUrl(String username, String org, String url, String resourceType) {
if (CODESYSTEM.equals(resourceType) || CONCEPTMAP.equals(resourceType)) {
Source userSource = sourceRepository.findFirstByCanonicalUrlAndVersionAndUserIdUsername(url, HEAD, username);
Source orgSource = sourceRepository.findFirstByCanonicalUrlAndVersionAndOrganizationMnemonic(url, HEAD, org);
if (userSource != null || orgSource != null) return true;
} else if (VALUESET.equals(resourceType)) {
Collection userCollection = collectionRepository.findFirstByCanonicalUrlAndVersionAndUserIdUsername(url, HEAD, username);
Collection orgCollection = collectionRepository.findFirstByCanonicalUrlAndVersionAndOrganizationMnemonic(url, HEAD, org);
if (userCollection != null || orgCollection != null) return true;
}
return false;
}
protected void addJsonStrings(final CodeSystem codeSystem, final Source source) {
if (!codeSystem.getIdentifier().isEmpty())
source.setIdentifier(convertToJsonString(getResIdentifierString(codeSystem), IDENTIFIER));
if (!codeSystem.getContact().isEmpty())
source.setContact(convertToJsonString(getResContactString(codeSystem), CONTACT));
if (!codeSystem.getJurisdiction().isEmpty())
source.setJurisdiction(convertToJsonString(getResJurisdictionString(codeSystem), JURISDICTION));
if (codeSystem.getText().getStatus() != null || !codeSystem.getText().getDiv().isEmpty())
source.setText(convertToJsonString(getResTextString(codeSystem), TEXT));
Meta meta = codeSystem.getMeta();
if (meta.hasLastUpdated() || meta.hasSource() || meta.hasProfile() || meta.hasSecurity() || meta.hasTag()) {
source.setMeta(convertToJsonString(getResMetaString(codeSystem), META));
}
}
protected void addJsonStrings(final ValueSet valueSet, final Collection collection) {
if (!valueSet.getIdentifier().isEmpty())
collection.setIdentifier(convertToJsonString(getResIdentifierString(valueSet), IDENTIFIER));
if (!valueSet.getContact().isEmpty())
collection.setContact(convertToJsonString(getResContactString(valueSet), CONTACT));
if (!valueSet.getJurisdiction().isEmpty())
collection.setJurisdiction(convertToJsonString(getResJurisdictionString(valueSet), JURISDICTION));
if (valueSet.getText().getStatus() != null || !valueSet.getText().getDiv().isEmpty())
collection.setText(convertToJsonString(getResTextString(valueSet), TEXT));
Meta meta = valueSet.getMeta();
if (meta.hasLastUpdated() || meta.hasSource() || meta.hasProfile() || meta.hasSecurity() || meta.hasTag()) {
collection.setMeta(convertToJsonString(getResMetaString(valueSet), META));
}
}
protected void addJsonStrings(final ConceptMap conceptMap, final Source source) {
if (!conceptMap.getIdentifier().isEmpty())
source.setIdentifier(convertToJsonString(getResIdentifierString(conceptMap), IDENTIFIER));
if (!conceptMap.getContact().isEmpty())
source.setContact(convertToJsonString(getResContactString(conceptMap), CONTACT));
if (!conceptMap.getJurisdiction().isEmpty())
source.setJurisdiction(convertToJsonString(getResJurisdictionString(conceptMap), JURISDICTION));
if (conceptMap.getText().getStatus() != null || !conceptMap.getText().getDiv().isEmpty())
source.setText(convertToJsonString(getResTextString(conceptMap), TEXT));
Meta meta = conceptMap.getMeta();
if (meta.hasLastUpdated() || meta.hasSource() || meta.hasProfile() || meta.hasSecurity() || meta.hasTag()) {
source.setMeta(convertToJsonString(getResMetaString(conceptMap), META));
}
}
protected Long insert(SimpleJdbcInsert insert, Map<String, Object> parameters) {
if (!insert.isCompiled())
insert.usingGeneratedKeyColumns("id");
Number n = insert.executeAndReturnKeyHolder(parameters).getKey();
if (n instanceof Long)
return n.longValue();
if (n instanceof Integer)
return Long.valueOf(String.valueOf(n.intValue()));
return (Long) insert.executeAndReturnKeyHolder(parameters).getKey();
}
private String getResIdentifierString(final CodeSystem codeSystem) {
CodeSystem system = new CodeSystem();
system.setIdentifier(codeSystem.getIdentifier());
return getFhirContext().newJsonParser().encodeResourceToString(system);
}
private String getResContactString(final CodeSystem codeSystem) {
CodeSystem system = new CodeSystem();
system.setContact(codeSystem.getContact());
return getFhirContext().newJsonParser().encodeResourceToString(system);
}
private String getResJurisdictionString(final CodeSystem codeSystem) {
CodeSystem system = new CodeSystem();
system.setJurisdiction(codeSystem.getJurisdiction());
return getFhirContext().newJsonParser().encodeResourceToString(system);
}
private String getResTextString(final CodeSystem codeSystem) {
CodeSystem system = new CodeSystem();
system.setText(codeSystem.getText());
return getFhirContext().newJsonParser().encodeResourceToString(system);
}
private String getResMetaString(final CodeSystem codeSystem) {
CodeSystem system = new CodeSystem();
system.setMeta(codeSystem.getMeta());
return getFhirContext().newJsonParser().encodeResourceToString(system);
}
private String getResIdentifierString(final ValueSet valueSet) {
ValueSet set = new ValueSet();
set.setIdentifier(valueSet.getIdentifier());
return getFhirContext().newJsonParser().encodeResourceToString(set);
}
private String getResContactString(final ValueSet valueSet) {
ValueSet set = new ValueSet();
set.setContact(valueSet.getContact());
return getFhirContext().newJsonParser().encodeResourceToString(set);
}
private String getResJurisdictionString(final ValueSet valueSet) {
ValueSet set = new ValueSet();
set.setJurisdiction(valueSet.getJurisdiction());
return getFhirContext().newJsonParser().encodeResourceToString(set);
}
private String getResTextString(final ValueSet valueSet) {
ValueSet set = new ValueSet();
set.setText(valueSet.getText());
return getFhirContext().newJsonParser().encodeResourceToString(set);
}
private String getResMetaString(final ValueSet valueSet) {
ValueSet set = new ValueSet();
set.setMeta(valueSet.getMeta());
return getFhirContext().newJsonParser().encodeResourceToString(set);
}
private String getResIdentifierString(final ConceptMap conceptMap) {
ConceptMap map = new ConceptMap();
map.setIdentifier(conceptMap.getIdentifier());
return getFhirContext().newJsonParser().encodeResourceToString(map);
}
private String getResContactString(final ConceptMap conceptMap) {
ConceptMap map = new ConceptMap();
map.setContact(conceptMap.getContact());
return getFhirContext().newJsonParser().encodeResourceToString(map);
}
private String getResJurisdictionString(final ConceptMap conceptMap) {
ConceptMap map = new ConceptMap();
map.setJurisdiction(conceptMap.getJurisdiction());
return getFhirContext().newJsonParser().encodeResourceToString(map);
}
private String getResTextString(final ConceptMap conceptMap) {
ConceptMap map = new ConceptMap();
map.setText(conceptMap.getText());
return getFhirContext().newJsonParser().encodeResourceToString(map);
}
private String getResMetaString(final ConceptMap conceptMap) {
ConceptMap map = new ConceptMap();
map.setMeta(conceptMap.getMeta());
return getFhirContext().newJsonParser().encodeResourceToString(map);
}
private String convertToJsonString(String fhirResourceStr, String key) {
JsonObject object = jsonParser.parse(fhirResourceStr).getAsJsonObject();
if (object.has(RESOURCE_TYPE))
object.remove(RESOURCE_TYPE);
if (object.has(key)) {
if (object.get(key) instanceof JsonArray) {
return gson.toJson(object.getAsJsonArray(key));
} else {
return gson.toJson(object.getAsJsonObject(key));
}
}
return EMPTY_JSON;
}
protected String getStringProperty(List<CodeSystem.ConceptPropertyComponent> properties, String property) {
Optional<CodeSystem.ConceptPropertyComponent> component = properties.parallelStream().filter(p -> property.equals(p.getCode())).findAny();
if (component.isPresent() && isValid(component.get().getValueStringType().getValue()))
return component.get().getValueStringType().getValue();
return NA;
}
protected boolean getBooleanProperty(List<CodeSystem.ConceptPropertyComponent> properties, String property) {
Optional<CodeSystem.ConceptPropertyComponent> component = properties.parallelStream().filter(p -> property.equals(p.getCode())).findAny();
if (component.isPresent()) {
BooleanType value = component.get().getValueBooleanType();
if(value.getValue() != null) return value.getValue();
}
return false;
}
protected class OclEntity {
private BaseOclEntity owner;
private UserProfile userProfile;
private String accessionId;
private String org;
private String username;
private String resourceId;
private String resourceType;
private String url;
public OclEntity(MetadataResource resource, String accessionId, String authToken, boolean validateIfExists) {
// we'll support two type of accession id patterns as input
// 1. /users/testuser/<resource>/Test2/v20.0/
// 2. /users/testuser/<resource>/Test2/version/v20.0/
String org = EMPTY;
String username = EMPTY;
String resourceId = EMPTY;
String formattedId = formatExpression(accessionId);
String[] ar = formattedId.split(FS);
if (ar.length >= 5) {
if (ORGS.equals(ar[1]) && isValid(ar[2])) {
org = ar[2];
} else if (USERS.equals(ar[1]) && isValid(ar[2])) {
username = ar[2];
}
if (resource.getClass().getSimpleName().toLowerCase().equals(ar[3].toLowerCase()) && isValid(ar[4])) {
resourceId = ar[4];
resource.setId(resourceId);
}
if (ar.length >= 6 && isValid(ar[5])) {
if (VERSION.equals(ar[5])) {
if (ar.length >= 7 && isValid(ar[6]))
resource.setVersion(ar[6]);
} else {
resource.setVersion(ar[5]);
}
} else if (!isValid(resource.getVersion())) {
resource.setVersion(DEFAULT_RES_VERSION);
formattedId = formattedId + DEFAULT_RES_VERSION + FS;
} else {
formattedId = formattedId + resource.getVersion() + FS;
}
}
if (org.isEmpty() && username.isEmpty())
throw new InvalidRequestException("Owner type and id is required.");
if (resourceId.isEmpty())
throw new InvalidRequestException("Resource id is required.");
BaseOclEntity owner = validateOwner(org, username);
AuthtokenToken token = validateToken(authToken);
authenticate(token, username, org);
if (validateIfExists) {
validateId(username, org, resourceId, resource.getVersion(), resource.getClass().getSimpleName());
validateCanonicalUrl(username, org, resource.getUrl(), resource.getVersion(), resource.getClass().getSimpleName());
}
this.owner = owner;
this.userProfile = token.getUserProfile();
this.accessionId = formattedId;
this.org = org;
this.username = username;
this.resourceId = resourceId;
this.resourceType = resource.getClass().getSimpleName();
this.url = url;
}
public BaseOclEntity getOwner() {
return owner;
}
public UserProfile getUserProfile() {
return userProfile;
}
public String getAccessionId() {
return accessionId;
}
public String getOrg() {
return org;
}
public String getUsername() {
return username;
}
public String getResourceId() {
return resourceId;
}
public String getResourceType() {
return resourceType;
}
public String getUrl() {
return url;
}
}
protected Parameters.ParametersParameterComponent getParameter(String name, Type value) {
Parameters.ParametersParameterComponent component = new Parameters.ParametersParameterComponent();
component.setName(name).setValue(value);
return component;
}
protected Parameters.ParametersParameterComponent getParameter(String name, String value) {
Parameters.ParametersParameterComponent component = new Parameters.ParametersParameterComponent();
component.setName(name).setValue(new StringType(value));
return component;
}
protected <T> List<T> paginate(List<T> resources, Integer offset, Integer count) {
if (count == 0)
return resources;
if (offset < resources.size()) {
int start = offset;
int end = resources.size();
if (start + count < end)
end = start + count;
resources = resources.subList(start, end);
} else {
resources.clear();
}
return resources;
}
protected Optional<String> getToken() {
return oclUser.getAuthtokenTokens().stream()
.filter(f -> isValid(f.getKey()))
.map(m -> "Token " + m.getKey())
.findFirst();
}
protected boolean isOclAdmin(AuthtokenToken token) {
if (token == null) {
throw new AuthenticationException("Invalid authentication token.");
}
return oclUser.getAuthtokenTokens().stream()
.filter(f -> isValid(f.getKey()))
.anyMatch(f -> f.getKey().equals(token.getKey()));
}
protected void removeAccessionIdentifier(List<Identifier> identifiers) {
identifiers.removeIf(i -> i.getType().hasCoding(ACSN_SYSTEM, ACSN));
}
protected Source toBaseSource(final MetadataResource resource, final UserProfile user, final String uri) {
Source source = new Source();
// mnemonic
source.setMnemonic(resource.getId());
// canonical url
source.setCanonicalUrl(resource.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 (resource.getStatus() != null) {
// active
if (PublicationStatus.ACTIVE.toCode().equals(resource.getStatus().toCode())) {
source.setReleased(True);
// retired
} else if (PublicationStatus.RETIRED.toCode().equals(resource.getStatus().toCode())) {
source.setRetired(True);
source.setReleased(False);
source.setIsActive(False);
source.setIsLatestVersion(False);
}
}
// version
source.setVersion(resource.getVersion());
// default locale
source.setDefaultLocale(isValid(resource.getLanguage()) ? resource.getLanguage() : EN_LOCALE);
// uri
source.setUri(toOclUri(uri));
// name
String name = isValid(resource.getName()) ? resource.getName() : resource.getId();
source.setName(name);
// description
if (isValid(resource.getDescription()))
source.setDescription(resource.getDescription());
// title
if (isValid(resource.getTitle()))
source.setFullName(resource.getTitle());
// publisher
if (isValid(resource.getPublisher()))
source.setPublisher(resource.getPublisher());
// revision date
if (resource.getDate() != null)
source.setRevisionDate(resource.getDate());
// extras
source.setExtras(EMPTY_JSON);
// experimental
if (resource.getExperimentalElement().getValue() != null)
source.setExperimental(resource.getExperimentalElement().booleanValue());
// source_type
String sourceType = getSourceType(resource);
if (isValid(sourceType)) {
source.setSourceType(sourceType);
} else {
source.setSourceType(resource.getClass().getSimpleName());
}
if (resource instanceof CodeSystem) {
CodeSystem codeSystem = (CodeSystem) resource;
// content type
if (codeSystem.getContent() != null && !codeSystem.getContent().toCode().equals(CodeSystem.CodeSystemContentMode.NULL.toCode()))
source.setContentType(codeSystem.getContent().toCode());
// copyright
if (isValid(codeSystem.getCopyright()))
source.setCopyright(codeSystem.getCopyright());
// purpose
if (isValid(codeSystem.getPurpose()))
source.setPurpose(codeSystem.getPurpose());
// case_sensitive
if (codeSystem.getCaseSensitiveElement().getValue() != null)
source.setCaseSensitive(codeSystem.getCaseSensitiveElement().booleanValue());
source.setCaseSensitive(codeSystem.getCaseSensitive());
// collection_reference
if (isValid(codeSystem.getValueSet())) source.setCollectionReference(codeSystem.getValueSet());
// hierarchy_meaning
if (codeSystem.getHierarchyMeaning() != null) source.setHierarchyMeaning(codeSystem.getHierarchyMeaning().toCode());
// compositional
if (codeSystem.getCompositionalElement().getValue() != null)
source.setCompositional(codeSystem.getCompositionalElement().booleanValue());
// version_needed
if (codeSystem.getVersionNeededElement().getValue() != null)
source.setVersionNeeded(codeSystem.getVersionNeededElement().booleanValue());
}
if (resource instanceof ConceptMap) {
ConceptMap conceptMap = (ConceptMap) resource;
// copyright
if (isValid(conceptMap.getCopyright()))
source.setCopyright(conceptMap.getCopyright());
// purpose
if (isValid(conceptMap.getPurpose()))
source.setPurpose(conceptMap.getPurpose());
}
return source;
}
protected 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);
}
}
protected String getVersionLessSourceUri(Source source) {
String value = source.getUri().substring(0, source.getUri().lastIndexOf(FS));
return value.substring(0, value.lastIndexOf(FS)) + FS;
}
protected String removeVersion(String uri) {
if (!isValid(uri)) return EMPTY;
String[] arr = uri.split(FS);
if (arr.length >= 5) {
return FS + String.join(FS, arr[1], arr[2], arr[3], arr[4]) + FS;
}
return uri;
}
protected void removeVersionFromIdentifier(List<Identifier> identifiers) {
if (identifiers.isEmpty()) return;
identifiers.stream()
.filter(i -> ACSN_SYSTEM.equals(i.getType().getCodingFirstRep().getSystem())
&& ACSN.equals(i.getType().getCodingFirstRep().getCode()))
.findAny()
.ifPresent(i -> i.setValue(i.getValue().replaceAll("/version/.*", FS)));
}
}