-
Notifications
You must be signed in to change notification settings - Fork 5
/
ValueSetResourceProvider.java
201 lines (182 loc) · 8.88 KB
/
ValueSetResourceProvider.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
package org.openconceptlab.fhir.provider;
import ca.uhn.fhir.rest.annotation.*;
import ca.uhn.fhir.rest.api.server.RequestDetails;
import ca.uhn.fhir.rest.server.IResourceProvider;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.*;
import org.openconceptlab.fhir.converter.ValueSetConverter;
import org.openconceptlab.fhir.model.Collection;
import org.openconceptlab.fhir.repository.CollectionRepository;
import org.openconceptlab.fhir.util.OclFhirUtil;
import static org.openconceptlab.fhir.util.OclFhirConstants.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static org.openconceptlab.fhir.util.OclFhirUtil.*;
/**
* The ValueSetResourceProvider.
* @author harpatel1
*/
@Component
public class ValueSetResourceProvider implements IResourceProvider {
CollectionRepository collectionRepository;
ValueSetConverter valueSetConverter;
OclFhirUtil oclFhirUtil;
@Autowired
public ValueSetResourceProvider(CollectionRepository collectionRepository, ValueSetConverter valueSetConverter, OclFhirUtil oclFhirUtil) {
this.collectionRepository = collectionRepository;
this.valueSetConverter = valueSetConverter;
this.oclFhirUtil = oclFhirUtil;
}
@Override
public Class<? extends IBaseResource> getResourceType() {
return ValueSet.class;
}
/**
* Returns all public {@link ValueSet}.
*
* @return {@link Bundle}
*/
@Search
@Transactional
public Bundle searchValueSets(RequestDetails details) {
List<Collection> collections = filterHead(getCollections(publicAccess));
List<ValueSet> valueSets = valueSetConverter.convertToValueSet(collections);
return OclFhirUtil.getBundle(valueSets, details.getFhirServerBase(), details.getRequestPath());
}
/**
* Returns public {@link ValueSet} for a given Url.
* @param url
* @param version
* @return {@link Bundle}
*/
@Search
@Transactional
public Bundle searchValueSetByUrl(@RequiredParam(name = ValueSet.SP_URL) StringType url,
@OptionalParam(name = VERSION) StringType version,
RequestDetails details) {
List<Collection> collections = filterHead(getCollectionByUrl(url, version, publicAccess));
List<ValueSet> valueSets = valueSetConverter.convertToValueSet(collections);
return OclFhirUtil.getBundle(valueSets, details.getFhirServerBase(), details.getRequestPath());
}
/**
* Returns all public {@link ValueSet} for a given owner.
* @param owner
* @return {@link Bundle}
*/
@Search
@Transactional
public Bundle searchValueSetByOwner(@RequiredParam(name = OWNER) StringType owner,
RequestDetails details) {
List<Collection> collections = filterHead(getCollectionByOwner(owner, publicAccess));
List<ValueSet> valueSets = valueSetConverter.convertToValueSet(collections);
return OclFhirUtil.getBundle(valueSets, details.getFhirServerBase(), details.getRequestPath());
}
/**
* Returns public {@link ValueSet} for a given owner and Id. Returns given version if provided, otherwise
* most recent released version is returned.
* @param owner
* @param id
* @param version
* @return {@link Bundle}
*/
@Search
@Transactional
public Bundle searchValueSetByOwnerAndId(@RequiredParam(name = OWNER) StringType owner,
@RequiredParam(name = ID) StringType id,
@OptionalParam(name = VERSION) StringType version,
RequestDetails details) {
List<Collection> collections = filterHead(getCollectionByOwnerAndId(id, owner, version, publicAccess));
List<ValueSet> valueSets = valueSetConverter.convertToValueSet(collections);
return OclFhirUtil.getBundle(valueSets, details.getFhirServerBase(), details.getRequestPath());
}
private List<Collection> getCollections(List<String> access) {
return collectionRepository.findByPublicAccessIn(access).parallelStream().filter(Collection::getIsLatestVersion)
.collect(Collectors.toList());
}
private List<Collection> getCollectionByUrl(StringType url, StringType version, List<String> access) {
List<Collection> collections = new ArrayList<>();
if (isVersionAll(version)) {
// get all versions
collections.addAll(collectionRepository.findByCanonicalUrlAndPublicAccessIn(url.getValue(), access));
} else {
final Collection collection;
if (!isValid(version)) {
// get most recent released version
collection = getMostRecentReleasedCollectionByUrl(url, access);
} else {
// get a given version
collection = collectionRepository.findFirstByCanonicalUrlAndVersionAndPublicAccessIn(url.getValue(), version.getValue(), access);
}
if (collection != null) collections.add(collection);
}
if (collections.isEmpty())
throw new ResourceNotFoundException(notFound(ValueSet.class, url, version));
return collections;
}
private List<Collection> getCollectionByOwner(StringType owner, List<String> access) {
List<Collection> collections = new ArrayList<>();
String ownerType = getOwnerType(owner.getValue());
String value = getOwner(owner.getValue());
if (ORG.equals(ownerType)) {
collections.addAll(collectionRepository.findByOrganizationMnemonicAndPublicAccessIn(value, access));
} else {
collections.addAll(collectionRepository.findByUserIdUsernameAndPublicAccessIn(value, access));
}
return collections.parallelStream().filter(Collection::getIsLatestVersion).collect(Collectors.toList());
}
private List<Collection> getCollectionByOwnerAndId(StringType id, StringType owner, StringType version, List<String> access) {
List<Collection> collections = new ArrayList<>();
String ownerType = getOwnerType(owner.getValue());
String value = getOwner(owner.getValue());
if (isVersionAll(version)) {
// get all versions
if (ORG.equals(ownerType)) {
collections.addAll(collectionRepository.findByMnemonicAndOrganizationMnemonicAndPublicAccessIn(id.getValue(),
value, access));
} else {
collections.addAll(collectionRepository.findByMnemonicAndUserIdUsernameAndPublicAccessIn(id.getValue(),
value, access));
}
} else {
final Collection collection;
if (!isValid(version)) {
// get most recent released version
collection = getMostRecentReleasedCollectionByOwner(id.getValue(), value, ownerType, access);
} else {
// get a given version
if (ORG.equals(ownerType)) {
collection = collectionRepository.findFirstByMnemonicAndVersionAndOrganizationMnemonicAndPublicAccessIn(
id.getValue(), version.getValue(), value, access);
} else {
collection = collectionRepository.findFirstByMnemonicAndVersionAndUserIdUsernameAndPublicAccessIn(
id.getValue(), version.getValue(), value, access);
}
}
if (collection != null) collections.add(collection);
}
if (collections.isEmpty())
throw new ResourceNotFoundException(notFound(ValueSet.class, owner, id, version));
return collections;
}
private Collection getMostRecentReleasedCollectionByOwner(String id, String owner, String ownerType, List<String> access) {
if (ORG.equals(ownerType)) {
return collectionRepository.findFirstByMnemonicAndReleasedAndPublicAccessInAndOrganizationMnemonicOrderByCreatedAtDesc(
id, true, access, owner);
}
return collectionRepository.findFirstByMnemonicAndReleasedAndPublicAccessInAndUserIdUsernameOrderByCreatedAtDesc(
id, true, access, owner);
}
private Collection getMostRecentReleasedCollectionByUrl(StringType url, List<String> access) {
return collectionRepository.findFirstByCanonicalUrlAndReleasedAndPublicAccessInOrderByCreatedAtDesc(
url.getValue(), true, access
);
}
private List<Collection> filterHead(List<Collection> collections) {
return collections.stream().filter(s -> !HEAD.equals(s.getVersion())).collect(Collectors.toList());
}
}