001package ca.uhn.fhir.rest.client.method; 002 003/* 004 * #%L 005 * HAPI FHIR - Client Framework 006 * %% 007 * Copyright (C) 2014 - 2019 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.context.FhirVersionEnum; 035import ca.uhn.fhir.rest.annotation.Sort; 036import ca.uhn.fhir.rest.api.Constants; 037import ca.uhn.fhir.rest.api.SortOrderEnum; 038import ca.uhn.fhir.rest.api.SortSpec; 039import ca.uhn.fhir.rest.param.ParameterUtil; 040import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; 041 042public class SortParameter implements IParameter { 043 044 private FhirContext myContext; 045 046 public SortParameter(FhirContext theContext) { 047 myContext = theContext; 048 } 049 050 @Override 051 public void initializeTypes(Method theMethod, Class<? extends Collection<?>> theOuterCollectionType, Class<? extends Collection<?>> theInnerCollectionType, Class<?> theParameterType) { 052 if (theOuterCollectionType != null || theInnerCollectionType != null) { 053 throw new ConfigurationException("Method '" + theMethod.getName() + "' in type '" + theMethod.getDeclaringClass().getCanonicalName() + "' is annotated with @" + Sort.class.getName() + " but can not be of collection type"); 054 } 055 if (!theParameterType.equals(SortSpec.class)) { 056 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()); 057 } 058 059 } 060 061 @Override 062 public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map<String, List<String>> theTargetQueryArguments, IBaseResource theTargetResource) throws InternalErrorException { 063 SortSpec ss = (SortSpec) theSourceClientArgument; 064 065 if (myContext.getVersion().getVersion().isNewerThan(FhirVersionEnum.DSTU2)) { 066 String string = createSortStringDstu3(ss); 067 if (string.length() > 0) { 068 if (!theTargetQueryArguments.containsKey(Constants.PARAM_SORT)) { 069 theTargetQueryArguments.put(Constants.PARAM_SORT, new ArrayList<String>()); 070 } 071 theTargetQueryArguments.get(Constants.PARAM_SORT).add(string); 072 } 073 074 } else { 075 076 while (ss != null) { 077 String name; 078 if (ss.getOrder() == null) { 079 name = Constants.PARAM_SORT; 080 } else if (ss.getOrder() == SortOrderEnum.ASC) { 081 name = Constants.PARAM_SORT_ASC; 082 } else { 083 name = Constants.PARAM_SORT_DESC; 084 } 085 086 if (ss.getParamName() != null) { 087 if (!theTargetQueryArguments.containsKey(name)) { 088 theTargetQueryArguments.put(name, new ArrayList<String>()); 089 } 090 091 theTargetQueryArguments.get(name).add(ss.getParamName()); 092 } 093 ss = ss.getChain(); 094 } 095 } 096 } 097 098 099 public static String createSortStringDstu3(SortSpec ss) { 100 StringBuilder val = new StringBuilder(); 101 while (ss != null) { 102 103 if (isNotBlank(ss.getParamName())) { 104 if (val.length() > 0) { 105 val.append(','); 106 } 107 if (ss.getOrder() == SortOrderEnum.DESC) { 108 val.append('-'); 109 } 110 val.append(ParameterUtil.escape(ss.getParamName())); 111 } 112 113 ss = ss.getChain(); 114 } 115 116 String string = val.toString(); 117 return string; 118 } 119 120}