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.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.model.primitive.IntegerDt;
035import ca.uhn.fhir.rest.annotation.Offset;
036import ca.uhn.fhir.rest.annotation.Since;
037import ca.uhn.fhir.rest.api.Constants;
038import ca.uhn.fhir.rest.param.ParameterUtil;
039import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
040
041public class OffsetParameter implements IParameter {
042
043        @Override
044        public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map<String, List<String>> theTargetQueryArguments, IBaseResource theTargetResource)
045                        throws InternalErrorException {
046                if (theSourceClientArgument != null) {
047                        IntegerDt since = ParameterUtil.toInteger(theSourceClientArgument);
048                        if (since.isEmpty() == false) {
049                                theTargetQueryArguments.put(Constants.PARAM_OFFSET, Collections.singletonList(since.getValueAsString()));
050                        }
051                }
052        }
053
054        @Override
055        public void initializeTypes(Method theMethod, Class<? extends Collection<?>> theOuterCollectionType, Class<? extends Collection<?>> theInnerCollectionType, Class<?> theParameterType) {
056                if (theOuterCollectionType != null) {
057                        throw new ConfigurationException(Msg.code(1476) + "Method '" + theMethod.getName() + "' in type '" + theMethod.getDeclaringClass().getCanonicalName() + "' is annotated with @" + Offset.class.getName()
058                                        + " but can not be of collection type");
059                }
060                if (!ParameterUtil.isBindableIntegerType(theParameterType)) {
061                        throw new ConfigurationException(Msg.code(1477) + "Method '" + theMethod.getName() + "' in type '" + theMethod.getDeclaringClass().getCanonicalName() + "' is annotated with @" + Offset.class.getName()
062                                        + " but type '" + theParameterType + "' is an invalid type, must be Integer or IntegerType");
063                }
064        }
065
066}