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 */
022import static org.apache.commons.lang3.StringUtils.isNotBlank;
023
024import java.lang.reflect.Method;
025import java.util.ArrayList;
026import java.util.Collection;
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.annotation.Sort;
035import ca.uhn.fhir.rest.api.SortOrderEnum;
036import ca.uhn.fhir.rest.api.SortSpec;
037import ca.uhn.fhir.rest.server.Constants;
038import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
039import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
040
041public class SortParameter implements IParameter {
042
043        @Override
044        public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map<String, List<String>> theTargetQueryArguments, IBaseResource theTargetResource) throws InternalErrorException {
045                SortSpec ss = (SortSpec) theSourceClientArgument;
046                while (ss != null) {
047                        String name;
048                        if (ss.getOrder() == null) {
049                                name = Constants.PARAM_SORT;
050                        } else if (ss.getOrder() == SortOrderEnum.ASC) {
051                                name = Constants.PARAM_SORT_ASC;
052                        } else {
053                                name = Constants.PARAM_SORT_DESC;
054                        }
055
056                        if (ss.getParamName() != null) {
057                                if (!theTargetQueryArguments.containsKey(name)) {
058                                        theTargetQueryArguments.put(name, new ArrayList<String>());
059                                }
060                                theTargetQueryArguments.get(name).add(ss.getParamName());
061                        }
062                        ss = ss.getChain();
063                }
064        }
065
066        @Override
067        public Object translateQueryParametersIntoServerArgument(RequestDetails theRequest, BaseMethodBinding<?> theMethodBinding) throws InternalErrorException, InvalidRequestException {
068                if (!theRequest.getParameters().containsKey(Constants.PARAM_SORT)) {
069                        if (!theRequest.getParameters().containsKey(Constants.PARAM_SORT_ASC)) {
070                                if (!theRequest.getParameters().containsKey(Constants.PARAM_SORT_DESC)) {
071                                        return null;
072                                }
073                        }
074                }
075
076                SortSpec outerSpec = null;
077                SortSpec innerSpec = null;
078                for (String nextParamName : theRequest.getParameters().keySet()) {
079                        SortOrderEnum order;
080                        if (Constants.PARAM_SORT.equals(nextParamName)) {
081                                order = null;
082                        } else if (Constants.PARAM_SORT_ASC.equals(nextParamName)) {
083                                order = SortOrderEnum.ASC;
084                        } else if (Constants.PARAM_SORT_DESC.equals(nextParamName)) {
085                                order = SortOrderEnum.DESC;
086                        } else {
087                                continue;
088                        }
089
090                        String[] values = theRequest.getParameters().get(nextParamName);
091                        if (values != null) {
092                                for (String nextValue : values) {
093                                        if (isNotBlank(nextValue)) {
094                                                SortSpec spec = new SortSpec();
095                                                spec.setOrder(order);
096                                                spec.setParamName(nextValue);
097                                                if (innerSpec == null) {
098                                                        outerSpec = spec;
099                                                        innerSpec = spec;
100                                                } else {
101                                                        innerSpec.setChain(spec);
102                                                        innerSpec = spec;
103                                                }
104                                        }
105                                }
106                        }
107                }
108
109                return outerSpec;
110        }
111
112        @Override
113        public void initializeTypes(Method theMethod, Class<? extends Collection<?>> theOuterCollectionType, Class<? extends Collection<?>> theInnerCollectionType, Class<?> theParameterType) {
114                if (theOuterCollectionType != null || theInnerCollectionType != null) {
115                        throw new ConfigurationException("Method '" + theMethod.getName() + "' in type '" + theMethod.getDeclaringClass().getCanonicalName() + "' is annotated with @" + Sort.class.getName() + " but can not be of collection type");
116                }
117                if (!theParameterType.equals(SortSpec.class)) {
118                        throw new ConfigurationException("Method '" + theMethod.getName() + "' in type '" + theMethod.getDeclaringClass().getCanonicalName() + "' is annotated with @" + Sort.class.getName() + " but is an invalid type, must be: " + SortSpec.class.getCanonicalName());
119                }
120
121        }
122
123}