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.*;
025
026import org.hl7.fhir.instance.model.api.IBaseResource;
027import org.hl7.fhir.instance.model.api.IIdType;
028
029import ca.uhn.fhir.context.FhirContext;
030import ca.uhn.fhir.rest.annotation.Delete;
031import ca.uhn.fhir.rest.api.RequestTypeEnum;
032import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
033import ca.uhn.fhir.rest.client.impl.BaseHttpClientInvocation;
034import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
035import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
036
037public class DeleteMethodBinding extends BaseOutcomeReturningMethodBindingWithResourceIdButNoResourceBody {
038
039        public DeleteMethodBinding(Method theMethod, FhirContext theContext, Object theProvider) {
040                super(theMethod, theContext, theProvider, Delete.class, theMethod.getAnnotation(Delete.class).type());
041        }
042
043        @Override
044        public RestOperationTypeEnum getRestOperationType() {
045                return RestOperationTypeEnum.DELETE;
046        }
047
048        @Override
049        protected Set<RequestTypeEnum> provideAllowableRequestTypes() {
050                return Collections.singleton(RequestTypeEnum.DELETE);
051        }
052
053        @Override
054        protected BaseHttpClientInvocation createClientInvocation(Object[] theArgs, IBaseResource theResource) {
055                StringBuilder urlExtension = new StringBuilder();
056                urlExtension.append(getContext().getResourceDefinition(theResource).getName());
057
058                return new HttpPostClientInvocation(getContext(), theResource, urlExtension.toString());
059        }
060
061        @Override
062        protected boolean allowVoidReturnType() {
063                return true;
064        }
065
066        @Override
067        public BaseHttpClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException {
068                IIdType idDt = (IIdType) theArgs[getIdParameterIndex()];
069                if (idDt == null) {
070                        throw new NullPointerException("ID can not be null");
071                }
072
073                if (idDt.hasResourceType() == false) {
074                        idDt = idDt.withResourceType(getResourceName());
075                } else if (getResourceName().equals(idDt.getResourceType()) == false) {
076                        throw new InvalidRequestException("ID parameter has the wrong resource type, expected '" + getResourceName() + "', found: " + idDt.getResourceType());
077                }
078
079                HttpDeleteClientInvocation retVal = createDeleteInvocation(getContext(), idDt);
080
081                for (int idx = 0; idx < theArgs.length; idx++) {
082                        IParameter nextParam = getParameters().get(idx);
083                        nextParam.translateClientArgumentIntoQueryArgument(getContext(), theArgs[idx], null, null);
084                }
085
086                return retVal;
087        }
088
089        public static HttpDeleteClientInvocation createDeleteInvocation(FhirContext theContext, IIdType theId) {
090                HttpDeleteClientInvocation retVal = new HttpDeleteClientInvocation(theContext, theId);
091                return retVal;
092        }
093
094
095        @Override
096        protected String getMatchingOperation() {
097                return null;
098        }
099
100        public static HttpDeleteClientInvocation createDeleteInvocation(FhirContext theContext, String theSearchUrl) {
101                HttpDeleteClientInvocation retVal = new HttpDeleteClientInvocation(theContext, theSearchUrl);
102                return retVal;
103        }
104
105        public static HttpDeleteClientInvocation createDeleteInvocation(FhirContext theContext, String theResourceType, Map<String, List<String>> theParams) {
106                return new HttpDeleteClientInvocation(theContext, theResourceType, theParams);
107        }
108
109}