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