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