-
Notifications
You must be signed in to change notification settings - Fork 5
/
BaseProvider.java
130 lines (115 loc) · 5.89 KB
/
BaseProvider.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
package org.openconceptlab.fhir.provider;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import org.hl7.fhir.r4.model.CodeSystem;
import org.hl7.fhir.r4.model.StringType;
import org.openconceptlab.fhir.converter.CodeSystemConverter;
import org.openconceptlab.fhir.converter.ConceptMapConverter;
import org.openconceptlab.fhir.converter.ValueSetConverter;
import org.openconceptlab.fhir.model.Collection;
import org.openconceptlab.fhir.model.Source;
import org.openconceptlab.fhir.repository.CollectionRepository;
import org.openconceptlab.fhir.repository.SourceRepository;
import org.openconceptlab.fhir.util.OclFhirUtil;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import static org.openconceptlab.fhir.util.OclFhirConstants.HEAD;
import static org.openconceptlab.fhir.util.OclFhirConstants.ORG;
import static org.openconceptlab.fhir.util.OclFhirUtil.*;
import static org.openconceptlab.fhir.util.OclFhirUtil.notFound;
public class BaseProvider {
SourceRepository sourceRepository;
CodeSystemConverter codeSystemConverter;
CollectionRepository collectionRepository;
ValueSetConverter valueSetConverter;
ConceptMapConverter conceptMapConverter;
OclFhirUtil oclFhirUtil;
@Autowired
public BaseProvider(SourceRepository sourceRepository, CodeSystemConverter codeSystemConverter,
CollectionRepository collectionRepository, ValueSetConverter valueSetConverter,
ConceptMapConverter conceptMapConverter, OclFhirUtil oclFhirUtil) {
this.sourceRepository = sourceRepository;
this.codeSystemConverter = codeSystemConverter;
this.collectionRepository = collectionRepository;
this.valueSetConverter = valueSetConverter;
this.conceptMapConverter = conceptMapConverter;
this.oclFhirUtil = oclFhirUtil;
}
protected List<Source> getSources(List<String> access) {
return sourceRepository.findAllMostRecentReleased(access);
}
protected List<Source> getSourceByUrl(StringType url, StringType version, List<String> access) {
List<Source> sources = new ArrayList<>();
if (isVersionAll(version)) {
// get all versions
sources.addAll(sourceRepository.findByCanonicalUrlAndPublicAccessIn(url.getValue(), access).stream()
.sorted(Comparator.comparing(Source::getVersion).reversed()).collect(Collectors.toList()));
} else {
final Source source;
if (!isValid(version)) {
// get most recent released version
source = oclFhirUtil.getMostRecentReleasedSourceByUrl(url, access);
} else {
// get a given version
source = sourceRepository.findFirstByCanonicalUrlAndVersionAndPublicAccessIn(url.getValue(), version.getValue(), access);
}
if (source != null) sources.add(source);
}
if (sources.isEmpty())
throw new ResourceNotFoundException(notFound(CodeSystem.class, url, version));
return sources;
}
protected List<Source> getSourceByOwner(StringType owner, List<String> access) {
if (!isValid(owner))
return new ArrayList<>();
List<Source> sources = new ArrayList<>();
String ownerType = getOwnerType(owner.getValue());
String value = getOwner(owner.getValue());
if (ORG.equals(ownerType)) {
sources.addAll(sourceRepository.findByOrganizationMnemonicAndPublicAccessIn(value, access));
} else {
sources.addAll(sourceRepository.findByUserIdUsernameAndPublicAccessIn(value, access));
}
Multimap<String, Source> map = ArrayListMultimap.create();
sources.forEach(s -> map.put(s.getMnemonic(), s));
List<Source> filtered = new ArrayList<>();
map.asMap().forEach((k,v) -> {
v.stream().filter(s -> (s.getReleased() != null && s.getReleased())).max(Comparator.comparing(Source::getCreatedAt)).stream().findFirst().ifPresent(filtered::add);
});
return filtered;
}
protected List<Source> getSourceByOwnerAndIdAndVersion(StringType id, StringType owner, StringType version, List<String> access) {
List<Source> sources = new ArrayList<>();
String ownerType = getOwnerType(owner.getValue());
String value = getOwner(owner.getValue());
if (isVersionAll(version)) {
// get all versions
if (ORG.equals(ownerType)) {
sources.addAll(sourceRepository.findByMnemonicAndOrganizationMnemonicAndPublicAccessIn(id.getValue(),
value, access));
} else {
sources.addAll(sourceRepository.findByMnemonicAndUserIdUsernameAndPublicAccessIn(id.getValue(),
value, access));
}
} else {
addSourceVersion(id, version, access, sources, ownerType, value);
}
if (sources.isEmpty())
throw new ResourceNotFoundException(notFound(CodeSystem.class, owner, id, version));
return sources;
}
protected void addSourceVersion(StringType id, StringType version, List<String> access, List<Source> sources, String ownerType, String ownerId) {
final Source source = oclFhirUtil.getSourceVersion(id, version, access, ownerType, ownerId);
if (source != null) sources.add(source);
}
protected List<Source> filterSourceHead(List<Source> sources) {
return sources.stream().filter(s -> !HEAD.equals(s.getVersion())).collect(Collectors.toList());
}
protected List<Collection> filterCollectionHead(List<Collection> collections) {
return collections.stream().filter(s -> !HEAD.equals(s.getVersion())).collect(Collectors.toList());
}
}