001package ca.uhn.fhir.rest.param;
002
003import ca.uhn.fhir.context.FhirContext;
004import ca.uhn.fhir.i18n.Msg;
005import ca.uhn.fhir.model.api.IQueryParameterType;
006import ca.uhn.fhir.rest.api.Constants;
007import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
008
009import static org.apache.commons.lang3.StringUtils.defaultString;
010
011/*
012 * #%L
013 * HAPI FHIR - Core Library
014 * %%
015 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
016 * %%
017 * Licensed under the Apache License, Version 2.0 (the "License");
018 * you may not use this file except in compliance with the License.
019 * You may obtain a copy of the License at
020 *
021 *      http://www.apache.org/licenses/LICENSE-2.0
022 *
023 * Unless required by applicable law or agreed to in writing, software
024 * distributed under the License is distributed on an "AS IS" BASIS,
025 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
026 * See the License for the specific language governing permissions and
027 * limitations under the License.
028 * #L%
029 */
030
031/**
032 * Implementation of the _has method parameter
033 */
034public class HasParam extends BaseParam implements IQueryParameterType {
035
036        private static final long serialVersionUID = 1L;
037
038        private String myReferenceFieldName;
039        private String myParameterName;
040        private String myParameterValue;
041        private String myTargetResourceType;
042
043        public HasParam() {
044                super();
045        }
046
047
048        public HasParam(String theTargetResourceType, String theReferenceFieldName, String theParameterName, String theParameterValue) {
049                this();
050                myTargetResourceType = theTargetResourceType;
051                myReferenceFieldName = theReferenceFieldName;
052                myParameterName = theParameterName;
053                myParameterValue = theParameterValue;
054        }
055
056
057        @Override
058        String doGetQueryParameterQualifier() {
059                return ':' + myTargetResourceType + ':' + myReferenceFieldName + ':' + myParameterName;
060        }
061        
062        @Override
063        String doGetValueAsQueryToken(FhirContext theContext) {
064                return myParameterValue;
065        }
066
067        @Override
068        void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) {
069                String qualifier = defaultString(theQualifier);
070                if (!qualifier.startsWith(":")) {
071                        throwInvalidSyntaxException(Constants.PARAM_HAS + qualifier);
072                }
073                int colonIndex0 = qualifier.indexOf(':', 1);
074                validateColon(qualifier, colonIndex0);
075                int colonIndex1 = qualifier.indexOf(':', colonIndex0 + 1);
076                validateColon(qualifier, colonIndex1);
077                
078                myTargetResourceType = qualifier.substring(1, colonIndex0);
079                myReferenceFieldName = qualifier.substring(colonIndex0 + 1, colonIndex1);
080                myParameterName = qualifier.substring(colonIndex1 + 1);
081                myParameterValue = theValue;
082        }
083
084        public String getReferenceFieldName() {
085                return myReferenceFieldName;
086        }
087
088        public String getParameterName() {
089                return myParameterName;
090        }
091
092        public String getParameterValue() {
093                return myParameterValue;
094        }
095
096        public String getTargetResourceType() {
097                return myTargetResourceType;
098        }
099
100        private static void validateColon(String theParameterName, int colonIndex) {
101                if (colonIndex == -1) {
102                        throwInvalidSyntaxException(theParameterName);
103                }
104        }
105
106
107        private static void throwInvalidSyntaxException(String theParameterName) {
108                throw new InvalidRequestException(Msg.code(1942) + "Invalid _has parameter syntax: " + theParameterName);
109        }
110
111}