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