001package ca.uhn.fhir.jpa.searchparam.util;
002
003/*-
004 * #%L
005 * HAPI FHIR Search Parameters
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.i18n.Msg;
024import ca.uhn.fhir.context.FhirContext;
025import ca.uhn.fhir.context.FhirVersionEnum;
026import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
027
028public class LastNParameterHelper {
029
030        public static boolean isLastNParameter(String theParamName, FhirContext theContext) {
031                if (theParamName == null) {
032                        return false;
033                }
034
035                FhirVersionEnum version = theContext.getVersion().getVersion();
036
037                if (isR5(version) && isLastNParameterR5(theParamName)) {
038                        return true;
039                } else if (isR4(version) && isLastNParameterR4(theParamName)) {
040                        return true;
041                } else if (isDstu3(version) && isLastNParameterDstu3(theParamName)) {
042                        return true;
043                } else {
044                        return false;
045                }
046        }
047
048        private static boolean isDstu3(FhirVersionEnum theVersion) {
049                return (theVersion == FhirVersionEnum.DSTU3);
050        }
051
052        private static boolean isR4(FhirVersionEnum theVersion) {
053                return (theVersion == FhirVersionEnum.R4);
054        }
055
056        private static boolean isR5(FhirVersionEnum theVersion) {
057                return (theVersion == FhirVersionEnum.R5);
058        }
059
060        private static boolean isLastNParameterDstu3(String theParamName) {
061                return (theParamName.equals(org.hl7.fhir.dstu3.model.Observation.SP_SUBJECT) || theParamName.equals(org.hl7.fhir.dstu3.model.Observation.SP_PATIENT)
062                        || theParamName.equals(org.hl7.fhir.dstu3.model.Observation.SP_CATEGORY) || theParamName.equals(org.hl7.fhir.r4.model.Observation.SP_CODE))
063                        || theParamName.equals(org.hl7.fhir.dstu3.model.Observation.SP_DATE);
064        }
065
066        private static boolean isLastNParameterR4(String theParamName) {
067                return (theParamName.equals(org.hl7.fhir.r4.model.Observation.SP_SUBJECT) || theParamName.equals(org.hl7.fhir.r4.model.Observation.SP_PATIENT)
068                        || theParamName.equals(org.hl7.fhir.r4.model.Observation.SP_CATEGORY) || theParamName.equals(org.hl7.fhir.r4.model.Observation.SP_CODE))
069                        || theParamName.equals(org.hl7.fhir.r4.model.Observation.SP_DATE);
070        }
071
072        private static boolean isLastNParameterR5(String theParamName) {
073                return (theParamName.equals(org.hl7.fhir.r4.model.Observation.SP_SUBJECT) || theParamName.equals(org.hl7.fhir.r4.model.Observation.SP_PATIENT)
074                        || theParamName.equals(org.hl7.fhir.r4.model.Observation.SP_CATEGORY) || theParamName.equals(org.hl7.fhir.r4.model.Observation.SP_CODE))
075                        || theParamName.equals(org.hl7.fhir.r4.model.Observation.SP_DATE);
076        }
077
078        public static String getSubjectParamName(FhirContext theContext) {
079                if (theContext.getVersion().getVersion() == FhirVersionEnum.R5) {
080                        return org.hl7.fhir.r4.model.Observation.SP_SUBJECT;
081                } else if (theContext.getVersion().getVersion() == FhirVersionEnum.R4) {
082                        return org.hl7.fhir.r4.model.Observation.SP_SUBJECT;
083                } else if (theContext.getVersion().getVersion() == FhirVersionEnum.DSTU3) {
084                        return org.hl7.fhir.dstu3.model.Observation.SP_SUBJECT;
085                } else {
086                        throw new InvalidRequestException(Msg.code(489) + "$lastn operation is not implemented for FHIR Version " + theContext.getVersion().getVersion().getFhirVersionString());
087                }
088        }
089
090        public static String getPatientParamName(FhirContext theContext) {
091                if (theContext.getVersion().getVersion() == FhirVersionEnum.R5) {
092                        return org.hl7.fhir.r4.model.Observation.SP_PATIENT;
093                } else if (theContext.getVersion().getVersion() == FhirVersionEnum.R4) {
094                        return org.hl7.fhir.r4.model.Observation.SP_PATIENT;
095                } else if (theContext.getVersion().getVersion() == FhirVersionEnum.DSTU3) {
096                        return org.hl7.fhir.dstu3.model.Observation.SP_PATIENT;
097                } else {
098                        throw new InvalidRequestException(Msg.code(490) + "$lastn operation is not implemented for FHIR Version " + theContext.getVersion().getVersion().getFhirVersionString());
099                }
100        }
101
102        public static String getEffectiveParamName(FhirContext theContext) {
103                if (theContext.getVersion().getVersion() == FhirVersionEnum.R5) {
104                        return org.hl7.fhir.r4.model.Observation.SP_DATE;
105                } else if (theContext.getVersion().getVersion() == FhirVersionEnum.R4) {
106                        return org.hl7.fhir.r4.model.Observation.SP_DATE;
107                } else if (theContext.getVersion().getVersion() == FhirVersionEnum.DSTU3) {
108                        return org.hl7.fhir.dstu3.model.Observation.SP_DATE;
109                } else {
110                        throw new InvalidRequestException(Msg.code(491) + "$lastn operation is not implemented for FHIR Version " + theContext.getVersion().getVersion().getFhirVersionString());
111                }
112        }
113
114        public static String getCategoryParamName(FhirContext theContext) {
115                if (theContext.getVersion().getVersion() == FhirVersionEnum.R5) {
116                        return org.hl7.fhir.r4.model.Observation.SP_CATEGORY;
117                } else if (theContext.getVersion().getVersion() == FhirVersionEnum.R4) {
118                        return org.hl7.fhir.r4.model.Observation.SP_CATEGORY;
119                } else if (theContext.getVersion().getVersion() == FhirVersionEnum.DSTU3) {
120                        return org.hl7.fhir.dstu3.model.Observation.SP_CATEGORY;
121                } else {
122                        throw new InvalidRequestException(Msg.code(492) + "$lastn operation is not implemented for FHIR Version " + theContext.getVersion().getVersion().getFhirVersionString());
123                }
124        }
125
126        public static String getCodeParamName(FhirContext theContext) {
127                if (theContext.getVersion().getVersion() == FhirVersionEnum.R5) {
128                        return org.hl7.fhir.r4.model.Observation.SP_CODE;
129                } else if (theContext.getVersion().getVersion() == FhirVersionEnum.R4) {
130                        return org.hl7.fhir.r4.model.Observation.SP_CODE;
131                } else if (theContext.getVersion().getVersion() == FhirVersionEnum.DSTU3) {
132                        return org.hl7.fhir.dstu3.model.Observation.SP_CODE;
133                } else {
134                        throw new InvalidRequestException(Msg.code(493) + "$lastn operation is not implemented for FHIR Version " + theContext.getVersion().getVersion().getFhirVersionString());
135                }
136        }
137
138
139}