com.ibm.fhir.operation.term.cache.ValueSetClearCacheOperation Maven / Gradle / Ivy
/*
* (C) Copyright IBM Corp. 2021, 2022
*
* SPDX-License-Identifier: Apache-2.0
*/
package com.ibm.fhir.operation.term.cache;
import java.io.InputStream;
import com.ibm.fhir.config.FHIRRequestContext;
import com.ibm.fhir.core.HTTPReturnPreference;
import com.ibm.fhir.exception.FHIROperationException;
import com.ibm.fhir.model.format.Format;
import com.ibm.fhir.model.parser.FHIRParser;
import com.ibm.fhir.model.resource.OperationDefinition;
import com.ibm.fhir.model.resource.OperationOutcome;
import com.ibm.fhir.model.resource.Parameters;
import com.ibm.fhir.model.resource.Resource;
import com.ibm.fhir.model.resource.ValueSet;
import com.ibm.fhir.model.type.Code;
import com.ibm.fhir.model.type.CodeableConcept;
import com.ibm.fhir.model.type.Coding;
import com.ibm.fhir.model.type.code.IssueSeverity;
import com.ibm.fhir.model.type.code.IssueType;
import com.ibm.fhir.operation.term.AbstractTermOperation;
import com.ibm.fhir.search.util.SearchHelper;
import com.ibm.fhir.server.spi.operation.FHIROperationContext;
import com.ibm.fhir.server.spi.operation.FHIROperationUtil;
import com.ibm.fhir.server.spi.operation.FHIRResourceHelpers;
import com.ibm.fhir.term.util.ValueSetSupport;
public class ValueSetClearCacheOperation extends AbstractTermOperation {
@Override
protected OperationDefinition buildOperationDefinition() {
try (InputStream in = getClass().getClassLoader().getResourceAsStream("operation-valueset-clear-cache.json")) {
return FHIRParser.parser(Format.JSON).parse(in);
} catch (Exception e) {
throw new Error(e);
}
}
@Override
protected Parameters doInvoke(
FHIROperationContext operationContext,
Class extends Resource> resourceType,
String logicalId,
String versionId,
Parameters parameters,
FHIRResourceHelpers resourceHelper,
SearchHelper searchHelper) throws FHIROperationException {
try {
ValueSet valueSet = getResource(operationContext, logicalId, parameters, resourceHelper, ValueSet.class);
ValueSetSupport.clearCache(valueSet);
OperationOutcome operationOutcome = OperationOutcome.builder().issue(
OperationOutcome.Issue.builder()
.severity(IssueSeverity.INFORMATION)
.code(IssueType.INFORMATIONAL)
.details(CodeableConcept.builder().coding(
Coding.builder().code(Code.of("success")).build()
).build()).build()
).build();
if (FHIRRequestContext.get().getReturnPreference() == HTTPReturnPreference.OPERATION_OUTCOME) {
return FHIROperationUtil.getOutputParameters(operationOutcome);
} else {
return null;
}
} catch( Throwable t ) {
throw new FHIROperationException("Unexpected error occurred while processing request for operation '"
+ getName() + "': " + getCausedByMessage(t), t);
}
}
private String getCausedByMessage(Throwable throwable) {
return throwable.getClass().getName() + ": " + throwable.getMessage();
}
}