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 */ 022 023import java.lang.reflect.Method; 024import java.lang.reflect.Modifier; 025import java.util.*; 026 027import org.hl7.fhir.instance.model.api.IBaseResource; 028 029import ca.uhn.fhir.context.*; 030import ca.uhn.fhir.model.api.IResource; 031import ca.uhn.fhir.rest.annotation.TransactionParam; 032import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; 033 034public class TransactionParameter implements IParameter { 035 036 // private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(TransactionParameter.class); 037 private FhirContext myContext; 038 private ParamStyle myParamStyle; 039 040 public TransactionParameter(FhirContext theContext) { 041 myContext = theContext; 042 } 043 044 private String createParameterTypeError(Method theMethod) { 045 return "Method '" + theMethod.getName() + "' in type '" + theMethod.getDeclaringClass().getCanonicalName() + "' is annotated with @" + TransactionParam.class.getName() 046 + " but is not of type List<" + IResource.class.getCanonicalName() + "> or Bundle"; 047 } 048 049 @Override 050 public void initializeTypes(Method theMethod, Class<? extends Collection<?>> theOuterCollectionType, Class<? extends Collection<?>> theInnerCollectionType, Class<?> theParameterType) { 051 if (theOuterCollectionType != null) { 052 throw new ConfigurationException("Method '" + theMethod.getName() + "' in type '" + theMethod.getDeclaringClass().getCanonicalName() + "' is annotated with @" 053 + TransactionParam.class.getName() + " but can not be a collection of collections"); 054 } 055 if (Modifier.isInterface(theParameterType.getModifiers()) == false && IBaseResource.class.isAssignableFrom(theParameterType)) { 056 @SuppressWarnings("unchecked") 057 Class<? extends IBaseResource> parameterType = (Class<? extends IBaseResource>) theParameterType; 058 RuntimeResourceDefinition def = myContext.getResourceDefinition(parameterType); 059 if ("Bundle".equals(def.getName())) { 060 myParamStyle = ParamStyle.RESOURCE_BUNDLE; 061 } else { 062 throw new ConfigurationException(createParameterTypeError(theMethod)); 063 } 064 } else { 065 if (theInnerCollectionType.equals(List.class) == false) { 066 throw new ConfigurationException(createParameterTypeError(theMethod)); 067 } 068 if (theParameterType.equals(IResource.class) == false && theParameterType.equals(IBaseResource.class) == false) { 069 throw new ConfigurationException(createParameterTypeError(theMethod)); 070 } 071 myParamStyle = ParamStyle.RESOURCE_LIST; 072 } 073 } 074 075 @Override 076 public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map<String, List<String>> theTargetQueryArguments, IBaseResource theTargetResource) 077 throws InternalErrorException { 078 // nothing 079 080 } 081 082 public ParamStyle getParamStyle() { 083 return myParamStyle; 084 } 085 086 public enum ParamStyle { 087 /** New style bundle (defined in hapi-fhir-structures-* as a resource definition itself */ 088 RESOURCE_BUNDLE, 089 /** List of resources */ 090 RESOURCE_LIST 091 } 092 093}