001package ca.uhn.fhir.rest.client.method;
002
003/*-
004 * #%L
005 * HAPI FHIR - Client Framework
006 * %%
007 * Copyright (C) 2014 - 2023 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 */
022
023import ca.uhn.fhir.i18n.Msg;
024import static org.apache.commons.lang3.StringUtils.isBlank;
025
026import java.lang.reflect.Method;
027import java.util.Collections;
028import java.util.Set;
029
030import org.hl7.fhir.instance.model.api.IBaseResource;
031import org.hl7.fhir.instance.model.api.IIdType;
032
033import ca.uhn.fhir.context.FhirContext;
034import ca.uhn.fhir.model.primitive.IdDt;
035import ca.uhn.fhir.rest.annotation.Update;
036import ca.uhn.fhir.rest.api.RequestTypeEnum;
037import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
038import ca.uhn.fhir.rest.client.impl.BaseHttpClientInvocation;
039import ca.uhn.fhir.rest.param.ParameterUtil;
040import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
041
042public class UpdateMethodBinding extends BaseOutcomeReturningMethodBindingWithResourceParam {
043
044        private Integer myIdParameterIndex;
045
046        public UpdateMethodBinding(Method theMethod, FhirContext theContext, Object theProvider) {
047                super(theMethod, theContext, Update.class, theProvider);
048
049                myIdParameterIndex = ParameterUtil.findIdParameterIndex(theMethod, getContext());
050        }
051
052
053        @Override
054        protected BaseHttpClientInvocation createClientInvocation(Object[] theArgs, IBaseResource theResource) {
055                IIdType idDt = (IIdType) theArgs[myIdParameterIndex];
056                if (idDt == null) {
057                        throw new NullPointerException(Msg.code(1447) + "ID can not be null");
058                }
059
060                FhirContext context = getContext();
061
062                HttpPutClientInvocation retVal = MethodUtil.createUpdateInvocation(theResource, null, idDt, context);
063
064                for (int idx = 0; idx < theArgs.length; idx++) {
065                        IParameter nextParam = getParameters().get(idx);
066                        nextParam.translateClientArgumentIntoQueryArgument(getContext(), theArgs[idx], null, null);
067                }
068
069                return retVal;
070        }
071
072        @Override
073        protected String getMatchingOperation() {
074                return null;
075        }
076
077        @Override
078        public RestOperationTypeEnum getRestOperationType() {
079                return RestOperationTypeEnum.UPDATE;
080        }
081
082        /*
083         * @Override public boolean incomingServerRequestMatchesMethod(RequestDetails theRequest) { if
084         * (super.incomingServerRequestMatchesMethod(theRequest)) { if (myVersionIdParameterIndex != null) { if
085         * (theRequest.getVersionId() == null) { return false; } } else { if (theRequest.getVersionId() != null) { return
086         * false; } } return true; } else { return false; } }
087         */
088
089        @Override
090        protected Set<RequestTypeEnum> provideAllowableRequestTypes() {
091                return Collections.singleton(RequestTypeEnum.PUT);
092        }
093
094        @Override
095        protected void validateResourceIdAndUrlIdForNonConditionalOperation(IBaseResource theResource, String theResourceId, String theUrlId, String theMatchUrl) {
096                if (isBlank(theMatchUrl)) {
097                        if (isBlank(theUrlId)) {
098                                String msg = getContext().getLocalizer().getMessage(BaseOutcomeReturningMethodBindingWithResourceParam.class, "noIdInUrlForUpdate");
099                                throw new InvalidRequestException(Msg.code(1448) + msg);
100                        }
101                        if (isBlank(theResourceId)) {
102                                String msg = getContext().getLocalizer().getMessage(BaseOutcomeReturningMethodBindingWithResourceParam.class, "noIdInBodyForUpdate");
103                                throw new InvalidRequestException(Msg.code(1449) + msg);
104                        }
105                        if (!theResourceId.equals(theUrlId)) {
106                                String msg = getContext().getLocalizer().getMessage(BaseOutcomeReturningMethodBindingWithResourceParam.class, "incorrectIdForUpdate", theResourceId, theUrlId);
107                                throw new InvalidRequestException(Msg.code(1450) + msg);
108                        }
109                } else {
110                        theResource.setId((IIdType)null);
111                }
112                
113        }
114}