Skip to content

Commit

Permalink
OpenConceptLab/ocl_issues#495 added pagination for value set
Browse files Browse the repository at this point in the history
  • Loading branch information
Harsh Patel committed Dec 7, 2020
1 parent de831ae commit 9431934
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public ResponseEntity<String> expandValueSetByOrg(@PathVariable String org,
@RequestParam(name = URL) String url,
@RequestParam(name = VALUESET_VERSION, required = false) String valueSetVersion,
@RequestParam(name = OFFSET, required = false, defaultValue = "0") Integer offset,
@RequestParam(name = COUNT, required = false, defaultValue = "1000") Integer count,
@RequestParam(name = COUNT, required = false, defaultValue = "100") Integer count,
@RequestParam(name = INCLUDE_DESIGNATIONS, defaultValue = "true") Boolean includeDesignations,
@RequestParam(name = INCLUDE_DEFINITION, defaultValue = "false") Boolean includeDefinition,
@RequestParam(name = ACTIVE_ONLY, defaultValue = "true") Boolean activeOnly,
Expand Down Expand Up @@ -287,7 +287,7 @@ public ResponseEntity<String> expandValueSetByUser(@PathVariable String user,
@RequestParam(name = URL) String url,
@RequestParam(name = VALUESET_VERSION, required = false) String valueSetVersion,
@RequestParam(name = OFFSET, required = false, defaultValue = "0") Integer offset,
@RequestParam(name = COUNT, required = false, defaultValue = "1000") Integer count,
@RequestParam(name = COUNT, required = false, defaultValue = "100") Integer count,
@RequestParam(name = INCLUDE_DESIGNATIONS, defaultValue = "true") Boolean includeDesignations,
@RequestParam(name = INCLUDE_DEFINITION, defaultValue = "false") Boolean includeDefinition,
@RequestParam(name = ACTIVE_ONLY, defaultValue = "true") Boolean activeOnly,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ public ValueSetConverter(OclFhirUtil oclFhirUtil, ConceptsSourceRepository conce
@Value("${ocl.servlet.baseurl}")
private String baseUrl;

public List<ValueSet> convertToValueSet(List<Collection> collections) {
public List<ValueSet> convertToValueSet(List<Collection> collections, Integer page) {
List<ValueSet> valueSets = new ArrayList<>();
collections.forEach(collection -> {
ValueSet valueSet = toBaseValueSet(collection);
addCompose(valueSet, collection, false);
if (page != null)
addCompose(valueSet, collection, false, page);
valueSets.add(valueSet);
});
return valueSets;
Expand Down Expand Up @@ -107,10 +108,12 @@ private String formatExpression(String expression) {
return uri;
}

private void addCompose(ValueSet valueSet, Collection collection, boolean includeConceptDesignation) {
List<CollectionsConcept> collectionsConcepts = collection.getCollectionsConcepts();
private void addCompose(ValueSet valueSet, Collection collection, boolean includeConceptDesignation, Integer page) {
// We have to use expressions to determine actual Source version since its not possible through CollectionsConcepts
List<String> expressions = getExpressions(collection);
IntegerType offset = new IntegerType(page * 100);
IntegerType count = new IntegerType(100);

List<String> expressions = getExpressions(collection, offset, count);

// lets get all the source versions first to reduce the database calls
List<Source> sources = getSourcesFromExpressions(expressions);
Expand Down Expand Up @@ -167,7 +170,7 @@ private List<Source> getSourcesFromExpressions(List<String> expressions, List<St
if (map.containsKey(sourceId) && HEAD.equals(sourceVersion)) {
return sourcesProvided.parallelStream().filter(s -> s.getMnemonic().equals(sourceId)).findFirst().get();
} else {
// if source is not part of the system-version then don't override
// if source is not part of the system-version then don't override
return oclFhirUtil.getSourceVersion(m[2], m[3], publicAccess, m[0], m[1]);
}
})
Expand All @@ -191,11 +194,18 @@ private List<Source> getSourcesFromExpressions(List<String> expressions) {
}

private List<String> getExpressions(Collection collection) {
return collection.getCollectionsReferences().parallelStream()
Map<String, String> map = new TreeMap<>();
collection.getCollectionsReferences().stream()
.map(CollectionsReference::getCollectionReference)
.map(CollectionReference::getExpression)
.sorted()
.collect(Collectors.toList());
.forEach(e -> {
String [] ar = formatExpression(e).split("/");
String conceptId = getConceptId(ar);
if (isValid(conceptId))
map.put(conceptId + getConceptVersion(ar), e);
});

return new ArrayList<>(map.values());
}

private List<String> getExpressions(Collection collection, IntegerType offset, IntegerType count) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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.InvalidRequestException;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -61,7 +62,7 @@ public Class<? extends IBaseResource> getResourceType() {
@Transactional
public Bundle searchValueSets(RequestDetails details) {
List<Collection> collections = filterHead(getCollections(publicAccess));
List<ValueSet> valueSets = valueSetConverter.convertToValueSet(collections);
List<ValueSet> valueSets = valueSetConverter.convertToValueSet(collections, null);
return OclFhirUtil.getBundle(valueSets, details.getFhirServerBase(), details.getRequestPath());
}

Expand All @@ -75,9 +76,10 @@ public Bundle searchValueSets(RequestDetails details) {
@Transactional
public Bundle searchValueSetByUrl(@RequiredParam(name = ValueSet.SP_URL) StringType url,
@OptionalParam(name = VERSION) StringType version,
@OptionalParam(name = PAGE) StringType page,
RequestDetails details) {
List<Collection> collections = filterHead(getCollectionByUrl(url, version, publicAccess));
List<ValueSet> valueSets = valueSetConverter.convertToValueSet(collections);
List<ValueSet> valueSets = valueSetConverter.convertToValueSet(collections, isVersionAll(version) ? null : getPage(page));
return OclFhirUtil.getBundle(valueSets, details.getFhirServerBase(), details.getRequestPath());
}

Expand All @@ -91,7 +93,7 @@ public Bundle searchValueSetByUrl(@RequiredParam(name = ValueSet.SP_URL) StringT
public Bundle searchValueSetByOwner(@RequiredParam(name = OWNER) StringType owner,
RequestDetails details) {
List<Collection> collections = filterHead(getCollectionByOwner(owner, publicAccess));
List<ValueSet> valueSets = valueSetConverter.convertToValueSet(collections);
List<ValueSet> valueSets = valueSetConverter.convertToValueSet(collections, null);
return OclFhirUtil.getBundle(valueSets, details.getFhirServerBase(), details.getRequestPath());
}

Expand All @@ -106,20 +108,21 @@ public Bundle searchValueSetByOwner(@RequiredParam(name = OWNER) StringType owne
@Search
@Transactional
public Bundle searchValueSetByOwnerAndId(@RequiredParam(name = OWNER) StringType owner,
@RequiredParam(name = ID) StringType id,
@OptionalParam(name = VERSION) StringType version,
RequestDetails details) {
@RequiredParam(name = ID) StringType id,
@OptionalParam(name = VERSION) StringType version,
@OptionalParam(name = PAGE) StringType page,
RequestDetails details) {
List<Collection> collections = filterHead(getCollectionByOwnerAndId(id, owner, version, publicAccess));
List<ValueSet> valueSets = valueSetConverter.convertToValueSet(collections);
List<ValueSet> valueSets = valueSetConverter.convertToValueSet(collections, isVersionAll(version) ? null : getPage(page));
return OclFhirUtil.getBundle(valueSets, details.getFhirServerBase(), details.getRequestPath());
}

@Operation(name = VALIDATE_CODE, idempotent = true)
@Transactional
public Parameters valueSetValidateCode(@OperationParam(name = URL, type = UriType.class) UriType url,
public Parameters valueSetValidateCode(@OperationParam(name = URL, type = UriType.class, min = 1) UriType url,
@OperationParam(name = VALUESET_VERSION, type = StringType.class) StringType valueSetVersion,
@OperationParam(name = CODE, type = CodeType.class) CodeType code,
@OperationParam(name = SYSTEM, type = UriType.class) UriType system,
@OperationParam(name = CODE, type = CodeType.class, min = 1) CodeType code,
@OperationParam(name = SYSTEM, type = UriType.class, min = 1) UriType system,
@OperationParam(name = SYSTEM_VERSION, type = StringType.class) StringType systemVersion,
@OperationParam(name = DISPLAY, type = StringType.class) StringType display,
@OperationParam(name = DISP_LANG, type = CodeType.class) CodeType displayLanguage,
Expand All @@ -144,7 +147,7 @@ public Parameters valueSetValidateCode(@OperationParam(name = URL, type = UriTyp

@Operation(name = EXPAND, idempotent = true)
@Transactional
public ValueSet valueSetExpand(@OperationParam(name = URL, type = UriType.class) UriType url,
public ValueSet valueSetExpand(@OperationParam(name = URL, type = UriType.class, min = 1) UriType url,
@OperationParam(name = VALUESET_VERSION, type = StringType.class) StringType valueSetVersion,
@OperationParam(name = OFFSET, type = IntegerType.class) IntegerType offset,
@OperationParam(name = COUNT, type = IntegerType.class) IntegerType count,
Expand All @@ -160,7 +163,8 @@ public ValueSet valueSetExpand(@OperationParam(name = URL, type = UriType.class)
Collection collection = isValid(owner) ? getCollectionByOwnerAndUrl(owner, newStringType(url), valueSetVersion, publicAccess) :
getCollectionByUrl(newStringType(url), valueSetVersion, publicAccess).get(0);
if (!isValid(offset)) offset = new IntegerType(0);
if (!isValid(count)) count = new IntegerType(1000);
if (!isValid(count)) count = new IntegerType(100);
if (count.getValue() > 100) count.setValue(100);
// by default include all designations
if (!isValid(includeDesignations)) includeDesignations = new BooleanType(true);
// by default exclude ValueSet definition
Expand All @@ -171,10 +175,16 @@ public ValueSet valueSetExpand(@OperationParam(name = URL, type = UriType.class)
List<String> systemVersionsList = new ArrayList<>();
if (excludeSystems != null)
excludeSystemsList = excludeSystems.parallelStream()
.filter(OclFhirUtil::isValid).map(PrimitiveType::getValue).collect(Collectors.toList());
.filter(OclFhirUtil::isValid)
.map(PrimitiveType::getValue)
.map(String::trim)
.collect(Collectors.toList());
if (systemVersions != null)
systemVersionsList = systemVersions.parallelStream()
.filter(OclFhirUtil::isValid).map(PrimitiveType::getValue).collect(Collectors.toList());
.filter(OclFhirUtil::isValid)
.map(PrimitiveType::getValue)
.map(String::trim)
.collect(Collectors.toList());
validateSystemVersion(systemVersionsList);
return valueSetConverter.expand(collection, offset, count, includeDesignations, includeDefinition,
activeOnly, displayLanguage, excludeSystemsList, systemVersionsList, filter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public final class OclFhirConstants {
public static final String EXCLUDE_SYSTEM = "exclude-system";
public static final String SYSTEMVERSION = "system-version";
public static final String FILTER = "filter";
public static final String PAGE = "page";

public static final String PUBLISHER_REGEX = "^user:.*|^org:.*";
public static final String ORG_ = "org:";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,4 +437,10 @@ public static String getCode(CodeType type) {
return isValid(type) ? type.getCode() : EMPTY;
}

public static int getPage(StringType page) {
if (isValid(page) && !StringUtils.isNumeric(page.getValue())) {
throw new InvalidRequestException("Page value must be positive numeric value.");
}
return page == null || page.getValue().matches("0|1") ? 0 : Integer.parseInt(page.getValue()) - 1;
}
}

0 comments on commit 9431934

Please sign in to comment.