001package ca.uhn.fhir.rest.client.method;
002
003import ca.uhn.fhir.i18n.Msg;
004import ca.uhn.fhir.context.FhirContext;
005import ca.uhn.fhir.rest.api.QualifiedParamList;
006import ca.uhn.fhir.rest.api.RestSearchParameterTypeEnum;
007import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
008import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
009import org.hl7.fhir.instance.model.api.IBaseResource;
010
011import java.lang.reflect.Method;
012import java.util.ArrayList;
013import java.util.Collection;
014import java.util.List;
015import java.util.Map;
016
017import static org.apache.commons.lang3.StringUtils.isNotBlank;
018
019/*
020 * #%L
021 * HAPI FHIR - Client Framework
022 * %%
023 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
024 * %%
025 * Licensed under the Apache License, Version 2.0 (the "License");
026 * you may not use this file except in compliance with the License.
027 * You may obtain a copy of the License at
028 *
029 *      http://www.apache.org/licenses/LICENSE-2.0
030 *
031 * Unless required by applicable law or agreed to in writing, software
032 * distributed under the License is distributed on an "AS IS" BASIS,
033 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
034 * See the License for the specific language governing permissions and
035 * limitations under the License.
036 * #L%
037 */
038
039public abstract class BaseQueryParameter implements IParameter {
040
041        public abstract List<QualifiedParamList> encode(FhirContext theContext, Object theObject) throws InternalErrorException;
042
043        public abstract String getName();
044
045        public abstract RestSearchParameterTypeEnum getParamType();
046
047        @Override
048        public void initializeTypes(Method theMethod, Class<? extends Collection<?>> theOuterCollectionType, Class<? extends Collection<?>> theInnerCollectionType, Class<?> theParameterType) {
049                // ignore for now
050        }
051
052        public abstract boolean isRequired();
053
054        @Override
055        public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map<String, List<String>> theTargetQueryArguments, IBaseResource theTargetResource) throws InternalErrorException {
056                if (theSourceClientArgument == null) {
057                        if (isRequired()) {
058                                throw new NullPointerException(Msg.code(1451) + "SearchParameter '" + getName() + "' is required and may not be null");
059                        }
060                } else {
061                        List<QualifiedParamList> value = encode(theContext, theSourceClientArgument);
062
063                        for (QualifiedParamList nextParamEntry : value) {
064                                StringBuilder b = new StringBuilder();
065                                for (String str : nextParamEntry) {
066                                        if (b.length() > 0) {
067                                                b.append(",");
068                                        }
069                                        b.append(str);
070                                }
071
072                                String qualifier = nextParamEntry.getQualifier();
073                                String paramName = isNotBlank(qualifier) ? getName() + qualifier : getName();
074                                List<String> paramValues = theTargetQueryArguments.computeIfAbsent(paramName, k -> new ArrayList<>(value.size()));
075
076                                paramValues.add(b.toString());
077                        }
078
079                }
080        }
081
082}