001package ca.uhn.fhir.jpa.util;
002
003import ca.uhn.fhir.i18n.Msg;
004import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao;
005import ca.uhn.fhir.model.dstu2.resource.Subscription;
006import ca.uhn.fhir.model.dstu2.valueset.ResourceTypeEnum;
007import ca.uhn.fhir.model.dstu2.valueset.SubscriptionChannelTypeEnum;
008import ca.uhn.fhir.model.dstu2.valueset.SubscriptionStatusEnum;
009import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
010import ca.uhn.fhir.rest.api.server.RequestDetails;
011import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
012import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException;
013import ca.uhn.fhir.rest.server.interceptor.ServerOperationInterceptorAdapter;
014import org.hl7.fhir.instance.model.api.IBaseResource;
015import org.springframework.beans.factory.annotation.Autowired;
016import org.springframework.beans.factory.annotation.Qualifier;
017
018import static org.apache.commons.lang3.StringUtils.isNotBlank;
019
020/*
021 * #%L
022 * HAPI FHIR JPA Server
023 * %%
024 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
025 * %%
026 * Licensed under the Apache License, Version 2.0 (the "License");
027 * you may not use this file except in compliance with the License.
028 * You may obtain a copy of the License at
029 *
030 *      http://www.apache.org/licenses/LICENSE-2.0
031 *
032 * Unless required by applicable law or agreed to in writing, software
033 * distributed under the License is distributed on an "AS IS" BASIS,
034 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
035 * See the License for the specific language governing permissions and
036 * limitations under the License.
037 * #L%
038 */
039
040/**
041 * Interceptor which requires newly created {@link Subscription subscriptions} to be in
042 * {@link SubscriptionStatusEnum#REQUESTED} state and prevents clients from changing the status.
043 */
044public class SubscriptionsRequireManualActivationInterceptorDstu2 extends ServerOperationInterceptorAdapter {
045
046        @Autowired
047        @Qualifier("mySubscriptionDaoDstu2")
048        private IFhirResourceDao<Subscription> myDao;
049
050        @Override
051        public void resourceCreated(RequestDetails theRequest, IBaseResource theResource) {
052                if (myDao.getContext().getResourceType(theResource).equals(ResourceTypeEnum.SUBSCRIPTION.getCode())) {
053                        verifyStatusOk(RestOperationTypeEnum.CREATE, null, theResource);
054                }
055        }
056
057        @Override
058        public void resourceUpdated(RequestDetails theRequest, IBaseResource theOldResource, IBaseResource theNewResource) {
059                if (myDao.getContext().getResourceType(theNewResource).equals(ResourceTypeEnum.SUBSCRIPTION.getCode())) {
060                        verifyStatusOk(RestOperationTypeEnum.UPDATE, theOldResource, theNewResource);
061                }
062        }
063
064
065        public void setDao(IFhirResourceDao<Subscription> theDao) {
066                myDao = theDao;
067        }
068
069        private void verifyStatusOk(RestOperationTypeEnum theOperation, IBaseResource theOldResourceOrNull, IBaseResource theResource) {
070                Subscription subscription = (Subscription) theResource;
071                SubscriptionStatusEnum newStatus = subscription.getStatusElement().getValueAsEnum();
072
073                if (newStatus == SubscriptionStatusEnum.REQUESTED || newStatus == SubscriptionStatusEnum.OFF) {
074                        return;
075                }
076
077                if (newStatus == null) {
078                        String actualCode = subscription.getStatusElement().getValueAsString();
079                        throw new UnprocessableEntityException(Msg.code(800) + "Can not " + theOperation.getCode() + " resource: Subscription.status must be populated on this server" + ((isNotBlank(actualCode)) ? " (invalid value " + actualCode + ")" : ""));
080                }
081
082                if (theOldResourceOrNull != null) {
083                        try {
084                                Subscription existing = (Subscription) theOldResourceOrNull;
085                                SubscriptionStatusEnum existingStatus = existing.getStatusElement().getValueAsEnum();
086                                if (existingStatus != newStatus) {
087                                        verifyActiveStatus(theOperation, subscription, newStatus, existingStatus);
088                                }
089                        } catch (ResourceNotFoundException e) {
090                                verifyActiveStatus(theOperation, subscription, newStatus, null);
091                        }
092                } else {
093                        verifyActiveStatus(theOperation, subscription, newStatus, null);
094                }
095        }
096
097        private void verifyActiveStatus(RestOperationTypeEnum theOperation, Subscription theSubscription, SubscriptionStatusEnum newStatus, SubscriptionStatusEnum theExistingStatus) {
098                SubscriptionChannelTypeEnum channelType = theSubscription.getChannel().getTypeElement().getValueAsEnum();
099
100                if (channelType == null) {
101                        throw new UnprocessableEntityException(Msg.code(801) + "Subscription.channel.type must be populated");
102                }
103
104                if (channelType == SubscriptionChannelTypeEnum.WEBSOCKET) {
105                        return;
106                }
107
108                if (theExistingStatus != null) {
109                        throw new UnprocessableEntityException(Msg.code(802) + "Subscription.status can not be changed from " + describeStatus(theExistingStatus) + " to " + describeStatus(newStatus));
110                }
111
112                if (theSubscription.getStatus() == null) {
113                        throw new UnprocessableEntityException(Msg.code(803) + "Can not " + theOperation.getCode().toLowerCase() + " resource: Subscription.status must be populated on this server");
114                }
115
116                throw new UnprocessableEntityException(Msg.code(804) + "Subscription.status must be '" + SubscriptionStatusEnum.OFF.getCode() + "' or '" + SubscriptionStatusEnum.REQUESTED.getCode() + "' on a newly created subscription");
117        }
118
119        private String describeStatus(SubscriptionStatusEnum existingStatus) {
120                String existingStatusString;
121                if (existingStatus != null) {
122                        existingStatusString = '\'' + existingStatus.getCode() + '\'';
123                } else {
124                        existingStatusString = "null";
125                }
126                return existingStatusString;
127        }
128
129}