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.dstu3;
021
022import ca.uhn.fhir.jpa.api.dao.IFhirResourceDaoSubscription;
023import ca.uhn.fhir.jpa.dao.BaseHapiFhirResourceDao;
024import ca.uhn.fhir.jpa.dao.data.ISubscriptionTableDao;
025import ca.uhn.fhir.jpa.entity.SubscriptionTable;
026import ca.uhn.fhir.jpa.model.cross.IBasePersistedResource;
027import ca.uhn.fhir.jpa.model.entity.ResourceTable;
028import ca.uhn.fhir.rest.api.server.RequestDetails;
029import ca.uhn.fhir.rest.api.server.storage.TransactionDetails;
030import org.hl7.fhir.dstu3.model.Subscription;
031import org.hl7.fhir.instance.model.api.IBaseResource;
032import org.hl7.fhir.instance.model.api.IIdType;
033import org.springframework.beans.factory.annotation.Autowired;
034
035import java.util.Date;
036
037public class FhirResourceDaoSubscriptionDstu3 extends BaseHapiFhirResourceDao<Subscription>
038                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(
052                        IIdType theId, RequestDetails theRequest, TransactionDetails theTransactionDetails) {
053                ResourceTable entity = readEntityLatestVersion(theId, theRequest, theTransactionDetails);
054                SubscriptionTable table = mySubscriptionTableDao.findOneByResourcePid(entity.getId());
055                if (table == null) {
056                        return null;
057                }
058                return table.getId();
059        }
060
061        @Override
062        protected void postPersist(
063                        ResourceTable theEntity, Subscription theSubscription, RequestDetails theRequestDetails) {
064                super.postPersist(theEntity, theSubscription, theRequestDetails);
065
066                createSubscriptionTable(theEntity, theSubscription);
067        }
068
069        @Override
070        public ResourceTable updateEntity(
071                        RequestDetails theRequest,
072                        IBaseResource theResource,
073                        IBasePersistedResource theEntity,
074                        Date theDeletedTimestampOrNull,
075                        boolean thePerformIndexing,
076                        boolean theUpdateVersion,
077                        TransactionDetails theTransactionDetails,
078                        boolean theForceUpdate,
079                        boolean theCreateNewHistoryEntry) {
080                ResourceTable retVal = super.updateEntity(
081                                theRequest,
082                                theResource,
083                                theEntity,
084                                theDeletedTimestampOrNull,
085                                thePerformIndexing,
086                                theUpdateVersion,
087                                theTransactionDetails,
088                                theForceUpdate,
089                                theCreateNewHistoryEntry);
090
091                if (theDeletedTimestampOrNull != null) {
092                        mySubscriptionTableDao.deleteAllForSubscription((ResourceTable) theEntity);
093                }
094                return retVal;
095        }
096}