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