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