001package ca.uhn.fhir.jpa.delete;
002
003/*-
004 * #%L
005 * HAPI FHIR JPA Server
006 * %%
007 * Copyright (C) 2014 - 2022 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.i18n.Msg;
024import ca.uhn.fhir.context.FhirContext;
025import ca.uhn.fhir.interceptor.api.HookParams;
026import ca.uhn.fhir.interceptor.api.IInterceptorBroadcaster;
027import ca.uhn.fhir.interceptor.api.Pointcut;
028import ca.uhn.fhir.jpa.api.config.DaoConfig;
029import ca.uhn.fhir.jpa.batch.api.IBatchJobSubmitter;
030import ca.uhn.fhir.jpa.batch.config.BatchConstants;
031import ca.uhn.fhir.jpa.batch.job.PartitionedUrlValidator;
032import ca.uhn.fhir.jpa.batch.job.model.RequestListJson;
033import ca.uhn.fhir.jpa.batch.reader.ReverseCronologicalBatchResourcePidReader;
034import ca.uhn.fhir.jpa.partition.IRequestPartitionHelperSvc;
035import ca.uhn.fhir.jpa.searchparam.MatchUrlService;
036import ca.uhn.fhir.rest.api.server.RequestDetails;
037import ca.uhn.fhir.rest.api.server.storage.IDeleteExpungeJobSubmitter;
038import ca.uhn.fhir.rest.server.exceptions.ForbiddenOperationException;
039import ca.uhn.fhir.rest.server.provider.ProviderConstants;
040import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
041import ca.uhn.fhir.rest.server.util.CompositeInterceptorBroadcaster;
042import org.springframework.batch.core.Job;
043import org.springframework.batch.core.JobExecution;
044import org.springframework.batch.core.JobParameters;
045import org.springframework.batch.core.JobParametersInvalidException;
046import org.springframework.beans.factory.annotation.Autowired;
047import org.springframework.beans.factory.annotation.Qualifier;
048
049import javax.transaction.Transactional;
050import java.util.List;
051
052public class DeleteExpungeJobSubmitterImpl implements IDeleteExpungeJobSubmitter {
053        @Autowired
054        private IBatchJobSubmitter myBatchJobSubmitter;
055        @Autowired
056        @Qualifier(BatchConstants.DELETE_EXPUNGE_JOB_NAME)
057        private Job myDeleteExpungeJob;
058        @Autowired
059        FhirContext myFhirContext;
060        @Autowired
061        MatchUrlService myMatchUrlService;
062        @Autowired
063        IRequestPartitionHelperSvc myRequestPartitionHelperSvc;
064        @Autowired
065        DaoConfig myDaoConfig;
066        @Autowired
067        PartitionedUrlValidator myPartitionedUrlValidator;
068        @Autowired
069        IInterceptorBroadcaster myInterceptorBroadcaster;
070
071        @Override
072        @Transactional(Transactional.TxType.NEVER)
073        public JobExecution submitJob(Integer theBatchSize, List<String> theUrlsToDeleteExpunge, RequestDetails theRequest) throws JobParametersInvalidException {
074                if (theBatchSize == null) {
075                        theBatchSize = myDaoConfig.getExpungeBatchSize();
076                }
077                RequestListJson requestListJson = myPartitionedUrlValidator.buildRequestListJson(theRequest, theUrlsToDeleteExpunge);
078                if (!myDaoConfig.canDeleteExpunge()) {
079                        throw new ForbiddenOperationException(Msg.code(820) + "Delete Expunge not allowed:  " + myDaoConfig.cannotDeleteExpungeReason());
080                }
081
082                for (String url : theUrlsToDeleteExpunge) {
083                        HookParams params = new HookParams()
084                                .add(RequestDetails.class, theRequest)
085                                .addIfMatchesType(ServletRequestDetails.class, theRequest)
086                                .add(String.class, url);
087                        CompositeInterceptorBroadcaster.doCallHooks(myInterceptorBroadcaster, theRequest, Pointcut.STORAGE_PRE_DELETE_EXPUNGE, params);
088                }
089
090                JobParameters jobParameters = ReverseCronologicalBatchResourcePidReader.buildJobParameters(ProviderConstants.OPERATION_DELETE_EXPUNGE, theBatchSize, requestListJson);
091                return myBatchJobSubmitter.runJob(myDeleteExpungeJob, jobParameters);
092        }
093}