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 */
022
023import ca.uhn.fhir.i18n.Msg;
024import java.lang.reflect.Method;
025import java.util.ArrayList;
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 SummaryEnumParameter 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                        List<String> values = new ArrayList<String>();
047                        for (SummaryEnum next : (Collection<SummaryEnum>) theSourceClientArgument) {
048                                if (next != null) {
049                                        values.add(next.getCode());
050                                }
051                        }
052                        theTargetQueryArguments.put(Constants.PARAM_SUMMARY, values);
053                } else {
054                        SummaryEnum ss = (SummaryEnum) theSourceClientArgument;
055                        if (ss != null) {
056                                theTargetQueryArguments.put(Constants.PARAM_SUMMARY, Collections.singletonList(ss.getCode()));
057                        }
058                }
059        }
060
061        @Override
062        public void initializeTypes(Method theMethod, Class<? extends Collection<?>> theOuterCollectionType, Class<? extends Collection<?>> theInnerCollectionType, Class<?> theParameterType) {
063                if (theOuterCollectionType != null) {
064                        throw new ConfigurationException(Msg.code(1422) + "Method '" + theMethod.getName() + "' in type '" + theMethod.getDeclaringClass().getCanonicalName() + "' is of type " + SummaryEnum.class
065                                        + " but can not be a collection of collections");
066                }
067        }
068
069}