001/*
002 * #%L
003 * HAPI FHIR - Client Framework
004 * %%
005 * Copyright (C) 2014 - 2023 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.i18n.Msg;
023import static org.apache.commons.lang3.StringUtils.isBlank;
024import static org.apache.commons.lang3.StringUtils.isNotBlank;
025
026import java.lang.reflect.Method;
027import java.lang.reflect.Modifier;
028import java.util.Date;
029
030import ca.uhn.fhir.rest.param.DateParam;
031import ca.uhn.fhir.rest.param.DateRangeParam;
032import org.hl7.fhir.instance.model.api.*;
033
034import ca.uhn.fhir.context.FhirContext;
035import ca.uhn.fhir.model.api.IResource;
036import ca.uhn.fhir.model.valueset.BundleTypeEnum;
037import ca.uhn.fhir.rest.annotation.History;
038import ca.uhn.fhir.rest.api.Constants;
039import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
040import ca.uhn.fhir.rest.client.impl.BaseHttpClientInvocation;
041import ca.uhn.fhir.rest.param.ParameterUtil;
042import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
043
044public class HistoryMethodBinding extends BaseResourceReturningMethodBinding {
045
046        private final Integer myIdParamIndex;
047        private String myResourceName;
048        private final RestOperationTypeEnum myResourceOperationType;
049
050        public HistoryMethodBinding(Method theMethod, FhirContext theContext, Object theProvider) {
051                super(toReturnType(theMethod, theProvider), theMethod, theContext, theProvider);
052
053                myIdParamIndex = ParameterUtil.findIdParameterIndex(theMethod, getContext());
054
055                History historyAnnotation = theMethod.getAnnotation(History.class);
056                Class<? extends IBaseResource> type = historyAnnotation.type();
057                if (Modifier.isInterface(type.getModifiers())) {
058                        myResourceOperationType = RestOperationTypeEnum.HISTORY_SYSTEM;
059                } else {
060                        if (myIdParamIndex != null) {
061                                myResourceOperationType = RestOperationTypeEnum.HISTORY_INSTANCE;
062                        } else {
063                                myResourceOperationType = RestOperationTypeEnum.HISTORY_TYPE;
064                        }
065                }
066
067                if (type != IBaseResource.class && type != IResource.class) {
068                        myResourceName = theContext.getResourceType(type);
069                } else {
070                        myResourceName = null;
071                }
072
073        }
074
075        @Override
076        public RestOperationTypeEnum getRestOperationType() {
077                return myResourceOperationType;
078        }
079
080        @Override
081        protected BundleTypeEnum getResponseBundleType() {
082                return BundleTypeEnum.HISTORY;
083        }
084
085        @Override
086        public ReturnTypeEnum getReturnType() {
087                return ReturnTypeEnum.BUNDLE;
088        }
089
090        @Override
091        public BaseHttpClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException {
092                IIdType id = null;
093                String resourceName = myResourceName;
094                if (myIdParamIndex != null) {
095                        id = (IIdType) theArgs[myIdParamIndex];
096                        if (id == null || isBlank(id.getValue())) {
097                                throw new NullPointerException(Msg.code(1441) + "ID can not be null");
098                        }
099                }
100
101                String historyId = id != null ? id.getIdPart() : null;
102                HttpGetClientInvocation retVal = createHistoryInvocation(getContext(), resourceName, historyId, null, null, null);
103
104                if (theArgs != null) {
105                        for (int idx = 0; idx < theArgs.length; idx++) {
106                                IParameter nextParam = getParameters().get(idx);
107                                nextParam.translateClientArgumentIntoQueryArgument(getContext(), theArgs[idx], retVal.getParameters(), null);
108                        }
109                }
110
111                return retVal;
112        }
113
114        public static HttpGetClientInvocation createHistoryInvocation(FhirContext theContext, String theResourceName, String theId, IPrimitiveType<Date> theSince, Integer theLimit, DateRangeParam theAt) {
115                StringBuilder b = new StringBuilder();
116                if (theResourceName != null) {
117                        b.append(theResourceName);
118                        if (isNotBlank(theId)) {
119                                b.append('/');
120                                b.append(theId);
121                        }
122                }
123                if (b.length() > 0) {
124                        b.append('/');
125                }
126                b.append(Constants.PARAM_HISTORY);
127
128                boolean haveParam = false;
129                if (theSince != null && !theSince.isEmpty()) {
130                        haveParam = true;
131                        b.append('?').append(Constants.PARAM_SINCE).append('=').append(theSince.getValueAsString());
132                }
133                if (theLimit != null) {
134                        b.append(haveParam ? '&' : '?');
135                        haveParam = true;
136                        b.append(Constants.PARAM_COUNT).append('=').append(theLimit);
137                }
138                if (theAt != null) {
139                        for (DateParam next : theAt.getValuesAsQueryTokens()) {
140                                b.append(haveParam ? '&' : '?');
141                                haveParam = true;
142                                b.append(Constants.PARAM_AT);
143                                b.append("=");
144                                b.append(next.getValueAsQueryToken(theContext));
145                        }
146                }
147
148                HttpGetClientInvocation retVal = new HttpGetClientInvocation(theContext, b.toString());
149                return retVal;
150        }
151
152        private static Class<? extends IBaseResource> toReturnType(Method theMethod, Object theProvider) {
153                History historyAnnotation = theMethod.getAnnotation(History.class);
154                Class<? extends IBaseResource> type = historyAnnotation.type();
155                if (type != IBaseResource.class && type != IResource.class) {
156                        return type;
157                }
158                return null;
159        }
160
161}