001package ca.uhn.fhir.jpa.dao.r5;
002
003/*
004 * #%L
005 * HAPI FHIR JPA Server
006 * %%
007 * Copyright (C) 2014 - 2021 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.jpa.api.dao.IFhirResourceDaoSubscription;
024import ca.uhn.fhir.jpa.dao.BaseHapiFhirResourceDao;
025import ca.uhn.fhir.jpa.dao.data.ISubscriptionTableDao;
026import ca.uhn.fhir.jpa.entity.SubscriptionTable;
027import ca.uhn.fhir.jpa.model.cross.IBasePersistedResource;
028import ca.uhn.fhir.jpa.model.entity.ResourceTable;
029import ca.uhn.fhir.rest.api.server.RequestDetails;
030import ca.uhn.fhir.rest.api.server.storage.TransactionDetails;
031import org.hl7.fhir.instance.model.api.IBaseResource;
032import org.hl7.fhir.instance.model.api.IIdType;
033import org.hl7.fhir.r5.model.Subscription;
034import org.springframework.beans.factory.annotation.Autowired;
035
036import java.util.Date;
037
038public class FhirResourceDaoSubscriptionR5 extends BaseHapiFhirResourceDao<Subscription> implements IFhirResourceDaoSubscription<Subscription> {
039
040        @Autowired
041        private ISubscriptionTableDao mySubscriptionTableDao;
042
043        private void createSubscriptionTable(ResourceTable theEntity, Subscription theSubscription) {
044                SubscriptionTable subscriptionEntity = new SubscriptionTable();
045                subscriptionEntity.setCreated(new Date());
046                subscriptionEntity.setSubscriptionResource(theEntity);
047                myEntityManager.persist(subscriptionEntity);
048        }
049
050        @Override
051        public Long getSubscriptionTablePidForSubscriptionResource(IIdType theId, RequestDetails theRequest, TransactionDetails theTransactionDetails) {
052                ResourceTable entity = readEntityLatestVersion(theId, theRequest, theTransactionDetails);
053                SubscriptionTable table = mySubscriptionTableDao.findOneByResourcePid(entity.getId());
054                if (table == null) {
055                        return null;
056                }
057                return table.getId();
058        }
059
060
061        @Override
062        protected void postPersist(ResourceTable theEntity, Subscription theSubscription) {
063                super.postPersist(theEntity, theSubscription);
064
065                createSubscriptionTable(theEntity, theSubscription);
066        }
067
068
069        @Override
070        public ResourceTable updateEntity(RequestDetails theRequest, IBaseResource theResource, IBasePersistedResource theEntity, Date theDeletedTimestampOrNull, boolean thePerformIndexing, boolean theUpdateVersion,
071                                                                                                 TransactionDetails theTransactionDetails, boolean theForceUpdate, boolean theCreateNewHistoryEntry) {
072                ResourceTable retVal = super.updateEntity(theRequest, theResource, theEntity, theDeletedTimestampOrNull, thePerformIndexing, theUpdateVersion, theTransactionDetails, theForceUpdate, theCreateNewHistoryEntry);
073
074                if (theDeletedTimestampOrNull != null) {
075                        Long subscriptionId = getSubscriptionTablePidForSubscriptionResource(theEntity.getIdDt(), theRequest, theTransactionDetails);
076                        if (subscriptionId != null) {
077                                mySubscriptionTableDao.deleteAllForSubscription(retVal);
078                        }
079                }
080
081                return retVal;
082        }
083
084}