001package ca.uhn.fhir.jpa.provider.r5;
002
003/*
004 * #%L
005 * HAPI FHIR JPA Server
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.context.support.IValidationSupport;
024import ca.uhn.fhir.jpa.api.dao.IFhirResourceDaoCodeSystem;
025import ca.uhn.fhir.jpa.model.util.JpaConstants;
026import ca.uhn.fhir.jpa.provider.BaseJpaResourceProviderValueSetDstu2;
027import ca.uhn.fhir.rest.annotation.IdParam;
028import ca.uhn.fhir.rest.annotation.Operation;
029import ca.uhn.fhir.rest.annotation.OperationParam;
030import ca.uhn.fhir.rest.api.server.RequestDetails;
031
032import org.hl7.fhir.r5.model.IdType;
033import org.hl7.fhir.r5.model.BooleanType;
034import org.hl7.fhir.r5.model.CodeSystem;
035import org.hl7.fhir.r5.model.CodeType;
036import org.hl7.fhir.r5.model.CodeableConcept;
037import org.hl7.fhir.r5.model.Coding;
038import org.hl7.fhir.r5.model.Parameters;
039import org.hl7.fhir.r5.model.StringType;
040import org.hl7.fhir.r5.model.UriType;
041
042import javax.servlet.http.HttpServletRequest;
043import java.util.List;
044
045public class BaseJpaResourceProviderCodeSystemR5 extends JpaResourceProviderR5<CodeSystem> {
046
047        /**
048         * $lookup operation
049         */
050        @SuppressWarnings("unchecked")
051        @Operation(name = JpaConstants.OPERATION_LOOKUP, idempotent = true, returnParameters= {
052                @OperationParam(name="name", type=StringType.class, min=1),
053                @OperationParam(name="version", type=StringType.class, min=0),
054                @OperationParam(name="display", type=StringType.class, min=1),
055                @OperationParam(name="abstract", type=BooleanType.class, min=1),
056        })
057        public Parameters lookup(
058                        HttpServletRequest theServletRequest,
059                        @OperationParam(name="code", min=0, max=1) CodeType theCode, 
060                        @OperationParam(name="system", min=0, max=1) UriType theSystem,
061                        @OperationParam(name="coding", min=0, max=1) Coding theCoding,
062                        @OperationParam(name="version", min=0, max=1) StringType theVersion,
063                        @OperationParam(name="displayLanguage", min=0, max=1) CodeType theDisplayLanguage,
064                        @OperationParam(name = "property", min = 0, max = OperationParam.MAX_UNLIMITED) List<CodeType> theProperties,
065                        RequestDetails theRequestDetails
066                        ) {
067
068                startRequest(theServletRequest);
069                try {
070                        IFhirResourceDaoCodeSystem<CodeSystem, Coding, CodeableConcept> dao = (IFhirResourceDaoCodeSystem<CodeSystem, Coding, CodeableConcept>) getDao();
071                        IValidationSupport.LookupCodeResult result;
072                        if (theVersion != null) {
073                                result = dao.lookupCode(theCode, new UriType(theSystem.getValue() + "|" + theVersion), theCoding, theDisplayLanguage, theRequestDetails);
074                        } else {
075                                result = dao.lookupCode(theCode, theSystem, theCoding, theDisplayLanguage, theRequestDetails);
076                        }
077                        result.throwNotFoundIfAppropriate();
078                        return (Parameters) result.toParameters(theRequestDetails.getFhirContext(), theProperties);
079                } finally {
080                        endRequest(theServletRequest);
081                }
082        }
083
084
085        /**
086         * $subsumes operation
087         */
088        @Operation(name = JpaConstants.OPERATION_SUBSUMES, idempotent = true, returnParameters= {
089                @OperationParam(name="outcome", type=CodeType.class, min=1),
090        })
091        public Parameters subsumes(
092                HttpServletRequest theServletRequest,
093                @OperationParam(name="codeA", min=0, max=1) CodeType theCodeA,
094                @OperationParam(name="codeB", min=0, max=1) CodeType theCodeB,
095                @OperationParam(name="system", min=0, max=1) UriType theSystem,
096                @OperationParam(name="codingA", min=0, max=1) Coding theCodingA,
097                @OperationParam(name="codingB", min=0, max=1) Coding theCodingB,
098                @OperationParam(name="version", min=0, max=1) StringType theVersion,
099                RequestDetails theRequestDetails
100        ) {
101
102                startRequest(theServletRequest);
103                try {
104                        IFhirResourceDaoCodeSystem<CodeSystem, Coding, CodeableConcept> dao = (IFhirResourceDaoCodeSystem<CodeSystem, Coding, CodeableConcept>) getDao();
105                        IFhirResourceDaoCodeSystem.SubsumesResult result;
106                        if (theVersion != null) {
107                                theSystem = new UriType(theSystem.asStringValue() + "|" + theVersion.toString());
108                        }
109                        result = dao.subsumes(theCodeA, theCodeB, theSystem, theCodingA, theCodingB, theRequestDetails);
110                        return (Parameters) result.toParameters(theRequestDetails.getFhirContext());
111                } finally {
112                        endRequest(theServletRequest);
113                }
114        }
115        
116        /**
117         * $validate-code operation
118         */
119        @SuppressWarnings("unchecked")
120        @Operation(name = JpaConstants.OPERATION_VALIDATE_CODE, idempotent = true, returnParameters = {
121                @OperationParam(name = "result", type = BooleanType.class, min = 1),
122                @OperationParam(name = "message", type = StringType.class),
123                @OperationParam(name = "display", type = StringType.class)
124        })
125        public Parameters validateCode(
126                HttpServletRequest theServletRequest,
127                @IdParam(optional = true) IdType theId,
128                @OperationParam(name = "url", min = 0, max = 1) UriType theCodeSystemUrl,
129                @OperationParam(name = "version", min = 0, max = 1) StringType theVersion,
130                @OperationParam(name = "code", min = 0, max = 1) CodeType theCode,
131                @OperationParam(name = "display", min = 0, max = 1) StringType theDisplay,
132                @OperationParam(name = "coding", min = 0, max = 1) Coding theCoding,
133                @OperationParam(name = "codeableConcept", min = 0, max = 1) CodeableConcept theCodeableConcept,
134                RequestDetails theRequestDetails
135        ) {
136
137                startRequest(theServletRequest);
138                try {
139                        IFhirResourceDaoCodeSystem<CodeSystem, Coding, CodeableConcept> dao = (IFhirResourceDaoCodeSystem<CodeSystem, Coding, CodeableConcept>) getDao();
140                                
141                        IValidationSupport.CodeValidationResult result = dao.validateCode(theId, theCodeSystemUrl, theVersion, theCode, theDisplay, theCoding, theCodeableConcept, theRequestDetails);
142                        return (Parameters) BaseJpaResourceProviderValueSetDstu2.toValidateCodeResult(getContext(), result);
143                } finally {
144                        endRequest(theServletRequest);
145                }
146        }
147}