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