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 */ 022import ca.uhn.fhir.i18n.Msg; 023import static org.apache.commons.lang3.StringUtils.isNotBlank; 024 025import java.lang.reflect.Method; 026import java.util.ArrayList; 027import java.util.Collection; 028import java.util.List; 029import java.util.Map; 030 031import org.hl7.fhir.instance.model.api.IBaseResource; 032 033import ca.uhn.fhir.context.ConfigurationException; 034import ca.uhn.fhir.context.FhirContext; 035import ca.uhn.fhir.context.FhirVersionEnum; 036import ca.uhn.fhir.rest.annotation.Sort; 037import ca.uhn.fhir.rest.api.Constants; 038import ca.uhn.fhir.rest.api.SortOrderEnum; 039import ca.uhn.fhir.rest.api.SortSpec; 040import ca.uhn.fhir.rest.param.ParameterUtil; 041import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; 042 043public class SortParameter implements IParameter { 044 045 private FhirContext myContext; 046 047 public SortParameter(FhirContext theContext) { 048 myContext = theContext; 049 } 050 051 @Override 052 public void initializeTypes(Method theMethod, Class<? extends Collection<?>> theOuterCollectionType, Class<? extends Collection<?>> theInnerCollectionType, Class<?> theParameterType) { 053 if (theOuterCollectionType != null || theInnerCollectionType != null) { 054 throw new ConfigurationException(Msg.code(1463) + "Method '" + theMethod.getName() + "' in type '" + theMethod.getDeclaringClass().getCanonicalName() + "' is annotated with @" + Sort.class.getName() + " but can not be of collection type"); 055 } 056 if (!theParameterType.equals(SortSpec.class)) { 057 throw new ConfigurationException(Msg.code(1464) + "Method '" + theMethod.getName() + "' in type '" + theMethod.getDeclaringClass().getCanonicalName() + "' is annotated with @" + Sort.class.getName() + " but is an invalid type, must be: " + SortSpec.class.getCanonicalName()); 058 } 059 060 } 061 062 @Override 063 public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map<String, List<String>> theTargetQueryArguments, IBaseResource theTargetResource) throws InternalErrorException { 064 SortSpec ss = (SortSpec) theSourceClientArgument; 065 066 if (myContext.getVersion().getVersion().isNewerThan(FhirVersionEnum.DSTU2)) { 067 String string = createSortStringDstu3(ss); 068 if (string.length() > 0) { 069 if (!theTargetQueryArguments.containsKey(Constants.PARAM_SORT)) { 070 theTargetQueryArguments.put(Constants.PARAM_SORT, new ArrayList<String>()); 071 } 072 theTargetQueryArguments.get(Constants.PARAM_SORT).add(string); 073 } 074 075 } else { 076 077 while (ss != null) { 078 String name; 079 if (ss.getOrder() == null) { 080 name = Constants.PARAM_SORT; 081 } else if (ss.getOrder() == SortOrderEnum.ASC) { 082 name = Constants.PARAM_SORT_ASC; 083 } else { 084 name = Constants.PARAM_SORT_DESC; 085 } 086 087 if (ss.getParamName() != null) { 088 if (!theTargetQueryArguments.containsKey(name)) { 089 theTargetQueryArguments.put(name, new ArrayList<String>()); 090 } 091 092 theTargetQueryArguments.get(name).add(ss.getParamName()); 093 } 094 ss = ss.getChain(); 095 } 096 } 097 } 098 099 100 public static String createSortStringDstu3(SortSpec ss) { 101 StringBuilder val = new StringBuilder(); 102 while (ss != null) { 103 104 if (isNotBlank(ss.getParamName())) { 105 if (val.length() > 0) { 106 val.append(','); 107 } 108 if (ss.getOrder() == SortOrderEnum.DESC) { 109 val.append('-'); 110 } 111 val.append(ParameterUtil.escape(ss.getParamName())); 112 } 113 114 ss = ss.getChain(); 115 } 116 117 String string = val.toString(); 118 return string; 119 } 120 121}