001package ca.uhn.fhir.rest.method;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2016 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.Collection;
025import java.util.Collections;
026import java.util.List;
027import java.util.Map;
028
029import org.apache.commons.lang3.StringUtils;
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.parser.DataFormatException;
036import ca.uhn.fhir.rest.annotation.Since;
037import ca.uhn.fhir.rest.param.ParameterUtil;
038import ca.uhn.fhir.rest.server.Constants;
039import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
040import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
041
042public class CountParameter implements IParameter {
043
044        private Class<?> myType;
045
046        @Override
047        public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map<String, List<String>> theTargetQueryArguments, IBaseResource theTargetResource) throws InternalErrorException {
048                if (theSourceClientArgument != null) {
049                        IntegerDt since = ParameterUtil.toInteger(theSourceClientArgument);
050                        if (since.isEmpty() == false) {
051                                theTargetQueryArguments.put(Constants.PARAM_COUNT, Collections.singletonList(since.getValueAsString()));
052                        }
053                }
054        }
055
056        @Override
057        public Object translateQueryParametersIntoServerArgument(RequestDetails theRequest, BaseMethodBinding<?> theMethodBinding) throws InternalErrorException, InvalidRequestException {
058                String[] sinceParams = theRequest.getParameters().remove(Constants.PARAM_COUNT);
059                if (sinceParams != null) {
060                        if (sinceParams.length > 0) {
061                                if (StringUtils.isNotBlank(sinceParams[0])) {
062                                        try {
063                                                IntegerDt since = new IntegerDt(sinceParams[0]);
064                                                return ParameterUtil.fromInteger(myType, since);
065                                        } catch (DataFormatException e) {
066                                                throw new InvalidRequestException("Invalid " + Constants.PARAM_COUNT + " value: " + sinceParams[0]);
067                                        }
068                                }
069                        }
070                }
071                return ParameterUtil.fromInteger(myType, null);
072        }
073
074        @Override
075        public void initializeTypes(Method theMethod, Class<? extends Collection<?>> theOuterCollectionType, Class<? extends Collection<?>> theInnerCollectionType, Class<?> theParameterType) {
076                if (theOuterCollectionType != null) {
077                        throw new ConfigurationException("Method '" + theMethod.getName() + "' in type '" +theMethod.getDeclaringClass().getCanonicalName()+ "' is annotated with @" + Since.class.getName() + " but can not be of collection type");
078                }
079                if (!ParameterUtil.getBindableIntegerTypes().contains(theParameterType)) {
080                        throw new ConfigurationException("Method '" + theMethod.getName() + "' in type '" +theMethod.getDeclaringClass().getCanonicalName()+ "' is annotated with @" + Since.class.getName() + " but type '" + theParameterType + "' is an invalid type, must be one of: " + ParameterUtil.getBindableIntegerTypes());
081                }
082                myType = theParameterType;
083        }
084
085}