001/*
002 * #%L
003 * HAPI FHIR - Client Framework
004 * %%
005 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.rest.client.method;
021
022import ca.uhn.fhir.context.FhirContext;
023import ca.uhn.fhir.model.valueset.BundleTypeEnum;
024import ca.uhn.fhir.rest.annotation.Validate;
025import ca.uhn.fhir.rest.api.Constants;
026import ca.uhn.fhir.rest.api.EncodingEnum;
027import ca.uhn.fhir.rest.client.impl.BaseHttpClientInvocation;
028import ca.uhn.fhir.util.ParametersUtil;
029import org.hl7.fhir.instance.model.api.IBaseParameters;
030import org.hl7.fhir.instance.model.api.IBaseResource;
031
032import java.lang.reflect.Method;
033import java.util.ArrayList;
034import java.util.List;
035
036public class ValidateMethodBindingDstu2Plus extends OperationMethodBinding {
037
038        public ValidateMethodBindingDstu2Plus(
039                        Class<?> theReturnResourceType,
040                        Class<? extends IBaseResource> theReturnTypeFromRp,
041                        Method theMethod,
042                        FhirContext theContext,
043                        Object theProvider,
044                        Validate theAnnotation) {
045                super(
046                                null,
047                                theReturnTypeFromRp,
048                                theMethod,
049                                theContext,
050                                theProvider,
051                                true,
052                                Constants.EXTOP_VALIDATE,
053                                theAnnotation.type(),
054                                BundleTypeEnum.COLLECTION);
055
056                List<IParameter> newParams = new ArrayList<IParameter>();
057                int idx = 0;
058                for (IParameter next : getParameters()) {
059                        if (next instanceof ResourceParameter) {
060                                if (IBaseResource.class.isAssignableFrom(((ResourceParameter) next).getResourceType())) {
061                                        Class<?> parameterType = theMethod.getParameterTypes()[idx];
062                                        if (String.class.equals(parameterType) || EncodingEnum.class.equals(parameterType)) {
063                                                newParams.add(next);
064                                        } else {
065                                                OperationParameter parameter = new OperationParameter(
066                                                                theContext, Constants.EXTOP_VALIDATE, Constants.EXTOP_VALIDATE_RESOURCE, 0, 1);
067                                                parameter.initializeTypes(theMethod, null, null, parameterType);
068                                                newParams.add(parameter);
069                                        }
070                                } else {
071                                        newParams.add(next);
072                                }
073                        } else {
074                                newParams.add(next);
075                        }
076                        idx++;
077                }
078                setParameters(newParams);
079        }
080
081        public static BaseHttpClientInvocation createValidateInvocation(FhirContext theContext, IBaseResource theResource) {
082                IBaseParameters parameters =
083                                (IBaseParameters) theContext.getResourceDefinition("Parameters").newInstance();
084                ParametersUtil.addParameterToParameters(theContext, parameters, "resource", theResource);
085
086                String resourceName = theContext.getResourceType(theResource);
087                String resourceId = theResource.getIdElement().getIdPart();
088
089                BaseHttpClientInvocation retVal = createOperationInvocation(
090                                theContext, resourceName, resourceId, null, Constants.EXTOP_VALIDATE, parameters, false);
091                return retVal;
092        }
093}