001package ca.uhn.fhir.rest.client.method; 002 003import static org.apache.commons.lang3.StringUtils.isNotBlank; 004 005/* 006 * #%L 007 * HAPI FHIR - Client Framework 008 * %% 009 * Copyright (C) 2014 - 2019 University Health Network 010 * %% 011 * Licensed under the Apache License, Version 2.0 (the "License"); 012 * you may not use this file except in compliance with the License. 013 * You may obtain a copy of the License at 014 * 015 * http://www.apache.org/licenses/LICENSE-2.0 016 * 017 * Unless required by applicable law or agreed to in writing, software 018 * distributed under the License is distributed on an "AS IS" BASIS, 019 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 020 * See the License for the specific language governing permissions and 021 * limitations under the License. 022 * #L% 023 */ 024 025import java.lang.reflect.Method; 026import java.util.ArrayList; 027import java.util.Collection; 028import java.util.List; 029import java.util.Map; 030 031import org.hl7.fhir.instance.model.api.IBaseResource; 032 033import ca.uhn.fhir.context.FhirContext; 034import ca.uhn.fhir.rest.api.QualifiedParamList; 035import ca.uhn.fhir.rest.api.RestSearchParameterTypeEnum; 036import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; 037import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; 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 /** 048 * Parameter should return true if {@link #parse(FhirContext, List)} should be called even if the query string 049 * contained no values for the given parameter 050 */ 051 public abstract boolean handlesMissing(); 052 053 @Override 054 public void initializeTypes(Method theMethod, Class<? extends Collection<?>> theOuterCollectionType, Class<? extends Collection<?>> theInnerCollectionType, Class<?> theParameterType) { 055 // ignore for now 056 } 057 058 public abstract boolean isRequired(); 059 060 public abstract Object parse(FhirContext theContext, List<QualifiedParamList> theString) throws InternalErrorException, InvalidRequestException; 061 062 @Override 063 public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map<String, List<String>> theTargetQueryArguments, IBaseResource theTargetResource) throws InternalErrorException { 064 if (theSourceClientArgument == null) { 065 if (isRequired()) { 066 throw new NullPointerException("SearchParameter '" + getName() + "' is required and may not be null"); 067 } 068 } else { 069 List<QualifiedParamList> value = encode(theContext, theSourceClientArgument); 070 071 for (QualifiedParamList nextParamEntry : value) { 072 StringBuilder b = new StringBuilder(); 073 for (String str : nextParamEntry) { 074 if (b.length() > 0) { 075 b.append(","); 076 } 077 b.append(str); 078 } 079 080 String qualifier = nextParamEntry.getQualifier(); 081 String paramName = isNotBlank(qualifier) ? getName() + qualifier : getName(); 082 List<String> paramValues = theTargetQueryArguments.get(paramName); 083 if (paramValues == null) { 084 paramValues = new ArrayList<>(value.size()); 085 theTargetQueryArguments.put(paramName, paramValues); 086 } 087 088 paramValues.add(b.toString()); 089 } 090 091 } 092 } 093 094}