001/*
002 * #%L
003 * HAPI FHIR - Client Framework
004 * %%
005 * Copyright (C) 2014 - 2023 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.rest.client.method;
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}