001package ca.uhn.fhir.rest.server.method;
002
003/*
004 * #%L
005 * HAPI FHIR - Server Framework
006 * %%
007 * Copyright (C) 2014 - 2021 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 */
022import static org.apache.commons.lang3.StringUtils.isNotBlank;
023
024import java.lang.reflect.Method;
025import java.util.Collection;
026import java.util.StringTokenizer;
027
028import ca.uhn.fhir.context.*;
029import ca.uhn.fhir.rest.annotation.Sort;
030import ca.uhn.fhir.rest.api.*;
031import ca.uhn.fhir.rest.api.server.RequestDetails;
032import ca.uhn.fhir.rest.param.ParameterUtil;
033import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
034import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
035
036public class SortParameter implements IParameter {
037
038        private FhirContext myContext;
039
040        public SortParameter(FhirContext theContext) {
041                myContext = theContext;
042        }
043
044        @Override
045        public void initializeTypes(Method theMethod, Class<? extends Collection<?>> theOuterCollectionType, Class<? extends Collection<?>> theInnerCollectionType, Class<?> theParameterType) {
046                if (theOuterCollectionType != null || theInnerCollectionType != null) {
047                        throw new ConfigurationException("Method '" + theMethod.getName() + "' in type '" + theMethod.getDeclaringClass().getCanonicalName() + "' is annotated with @" + Sort.class.getName()
048                                        + " but can not be of collection type");
049                }
050                if (!theParameterType.equals(SortSpec.class)) {
051                        throw new ConfigurationException("Method '" + theMethod.getName() + "' in type '" + theMethod.getDeclaringClass().getCanonicalName() + "' is annotated with @" + Sort.class.getName()
052                                        + " but is an invalid type, must be: " + SortSpec.class.getCanonicalName());
053                }
054
055        }
056
057        @Override
058        public Object translateQueryParametersIntoServerArgument(RequestDetails theRequest, BaseMethodBinding<?> theMethodBinding) throws InternalErrorException, InvalidRequestException {
059                if (!theRequest.getParameters().containsKey(Constants.PARAM_SORT)) {
060                        if (!theRequest.getParameters().containsKey(Constants.PARAM_SORT_ASC)) {
061                                if (!theRequest.getParameters().containsKey(Constants.PARAM_SORT_DESC)) {
062                                        return null;
063                                }
064                        }
065                }
066
067                SortSpec outerSpec = null;
068                SortSpec innerSpec = null;
069                for (String nextParamName : theRequest.getParameters().keySet()) {
070                        SortOrderEnum order;
071                        if (Constants.PARAM_SORT.equals(nextParamName)) {
072                                order = null;
073                        } else if (Constants.PARAM_SORT_ASC.equals(nextParamName)) {
074                                order = SortOrderEnum.ASC;
075                        } else if (Constants.PARAM_SORT_DESC.equals(nextParamName)) {
076                                order = SortOrderEnum.DESC;
077                        } else {
078                                continue;
079                        }
080
081                        String[] values = theRequest.getParameters().get(nextParamName);
082                        if (values != null) {
083
084                                for (String nextValue : values) {
085
086                                        if (myContext.getVersion().getVersion().isNewerThan(FhirVersionEnum.DSTU2) && order == null) {
087                                                StringTokenizer tok = new StringTokenizer(nextValue, ",");
088                                                while (tok.hasMoreTokens()) {
089                                                        String next = tok.nextToken();
090                                                        if (isNotBlank(next) && !next.equals("-")) {
091                                                                order = SortOrderEnum.ASC;
092                                                                if (next.startsWith("-")) {
093                                                                        order = SortOrderEnum.DESC;
094                                                                        next = next.substring(1);
095                                                                }
096
097                                                                SortSpec spec = new SortSpec();
098                                                                spec.setOrder(order);
099                                                                spec.setParamName(next);
100                                                                if (innerSpec == null) {
101                                                                        outerSpec = spec;
102                                                                        innerSpec = spec;
103                                                                } else {
104                                                                        innerSpec.setChain(spec);
105                                                                        innerSpec = spec;
106                                                                }
107
108                                                        }
109                                                }
110
111                                        } else {
112
113                                                if (isNotBlank(nextValue)) {
114                                                        SortSpec spec = new SortSpec();
115                                                        spec.setOrder(order);
116                                                        spec.setParamName(nextValue);
117                                                        if (innerSpec == null) {
118                                                                outerSpec = spec;
119                                                                innerSpec = spec;
120                                                        } else {
121                                                                innerSpec.setChain(spec);
122                                                                innerSpec = spec;
123                                                        }
124                                                }
125                                        }
126                                }
127                        }
128                }
129
130                return outerSpec;
131        }
132
133        public static String createSortStringDstu3(SortSpec ss) {
134                StringBuilder val = new StringBuilder();
135                while (ss != null) {
136
137                        if (isNotBlank(ss.getParamName())) {
138                                if (val.length() > 0) {
139                                        val.append(',');
140                                }
141                                if (ss.getOrder() == SortOrderEnum.DESC) {
142                                        val.append('-');
143                                }
144                                val.append(ParameterUtil.escape(ss.getParamName()));
145                        }
146
147                        ss = ss.getChain();
148                }
149
150                String string = val.toString();
151                return string;
152        }
153
154}