001package ca.uhn.fhir.rest.client.method; 002 003/* 004 * #%L 005 * HAPI FHIR - Client Framework 006 * %% 007 * Copyright (C) 2014 - 2019 University Health Network 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 */ 022import static org.apache.commons.lang3.StringUtils.isNotBlank; 023 024import java.lang.reflect.Method; 025import java.util.Collection; 026import java.util.Collections; 027import java.util.List; 028import java.util.Map; 029 030import org.hl7.fhir.instance.model.api.IBaseResource; 031 032import ca.uhn.fhir.context.ConfigurationException; 033import ca.uhn.fhir.context.FhirContext; 034import ca.uhn.fhir.rest.api.Constants; 035import ca.uhn.fhir.rest.api.SummaryEnum; 036import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; 037 038public class ElementsParameter implements IParameter { 039 040 @SuppressWarnings("unchecked") 041 @Override 042 public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map<String, List<String>> theTargetQueryArguments, IBaseResource theTargetResource) 043 throws InternalErrorException { 044 if (theSourceClientArgument instanceof Collection) { 045 StringBuilder values = new StringBuilder(); 046 for (String next : (Collection<String>) theSourceClientArgument) { 047 if (isNotBlank(next)) { 048 if (values.length() > 0) { 049 values.append(','); 050 } 051 values.append(next); 052 } 053 } 054 theTargetQueryArguments.put(Constants.PARAM_ELEMENTS, Collections.singletonList(values.toString())); 055 } else { 056 String elements = (String) theSourceClientArgument; 057 if (elements != null) { 058 theTargetQueryArguments.put(Constants.PARAM_ELEMENTS, Collections.singletonList(elements)); 059 } 060 } 061 } 062 063 @Override 064 public void initializeTypes(Method theMethod, Class<? extends Collection<?>> theOuterCollectionType, Class<? extends Collection<?>> theInnerCollectionType, Class<?> theParameterType) { 065 if (theOuterCollectionType != null) { 066 throw new ConfigurationException("Method '" + theMethod.getName() + "' in type '" + theMethod.getDeclaringClass().getCanonicalName() + "' is of type " + SummaryEnum.class 067 + " but can not be a collection of collections"); 068 } 069 } 070 071}