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.util.ArrayList; 025import java.util.List; 026 027import org.hl7.fhir.instance.model.api.IBaseParameters; 028import org.hl7.fhir.instance.model.api.IBaseResource; 029 030import ca.uhn.fhir.context.FhirContext; 031import ca.uhn.fhir.model.valueset.BundleTypeEnum; 032import ca.uhn.fhir.rest.annotation.OperationParam; 033import ca.uhn.fhir.rest.annotation.Validate; 034import ca.uhn.fhir.rest.api.Constants; 035import ca.uhn.fhir.rest.api.EncodingEnum; 036import ca.uhn.fhir.rest.client.impl.BaseHttpClientInvocation; 037import ca.uhn.fhir.util.ParametersUtil; 038 039public class ValidateMethodBindingDstu2Plus extends OperationMethodBinding { 040 041 public ValidateMethodBindingDstu2Plus(Class<?> theReturnResourceType, Class<? extends IBaseResource> theReturnTypeFromRp, Method theMethod, FhirContext theContext, Object theProvider, 042 Validate theAnnotation) { 043 super(null, theReturnTypeFromRp, theMethod, theContext, theProvider, true, Constants.EXTOP_VALIDATE, theAnnotation.type(), new OperationParam[0], BundleTypeEnum.COLLECTION); 044 045 List<IParameter> newParams = new ArrayList<IParameter>(); 046 int idx = 0; 047 for (IParameter next : getParameters()) { 048 if (next instanceof ResourceParameter) { 049 if (IBaseResource.class.isAssignableFrom(((ResourceParameter) next).getResourceType())) { 050 Class<?> parameterType = theMethod.getParameterTypes()[idx]; 051 if (String.class.equals(parameterType) || EncodingEnum.class.equals(parameterType)) { 052 newParams.add(next); 053 } else { 054 OperationParameter parameter = new OperationParameter(theContext, Constants.EXTOP_VALIDATE, Constants.EXTOP_VALIDATE_RESOURCE, 0, 1); 055 parameter.initializeTypes(theMethod, null, null, parameterType); 056 newParams.add(parameter); 057 } 058 } else { 059 newParams.add(next); 060 } 061 } else { 062 newParams.add(next); 063 } 064 idx++; 065 } 066 setParameters(newParams); 067 068 } 069 070 071 public static BaseHttpClientInvocation createValidateInvocation(FhirContext theContext, IBaseResource theResource) { 072 IBaseParameters parameters = (IBaseParameters) theContext.getResourceDefinition("Parameters").newInstance(); 073 ParametersUtil.addParameterToParameters(theContext, parameters, "resource", theResource); 074 075 String resourceName = theContext.getResourceDefinition(theResource).getName(); 076 String resourceId = theResource.getIdElement().getIdPart(); 077 078 BaseHttpClientInvocation retVal = createOperationInvocation(theContext, resourceName, resourceId, null,Constants.EXTOP_VALIDATE, parameters, false); 079 return retVal; 080 } 081 082 083}