001package ca.uhn.fhir.jpa.batch.mdm;
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.interceptor.api.HookParams;
025import ca.uhn.fhir.interceptor.api.IInterceptorBroadcaster;
026import ca.uhn.fhir.interceptor.api.Pointcut;
027import ca.uhn.fhir.jpa.api.config.DaoConfig;
028import ca.uhn.fhir.jpa.batch.api.IBatchJobSubmitter;
029import ca.uhn.fhir.jpa.batch.config.BatchConstants;
030import ca.uhn.fhir.jpa.batch.job.PartitionedUrlValidator;
031import ca.uhn.fhir.jpa.batch.job.model.RequestListJson;
032import ca.uhn.fhir.jpa.batch.mdm.job.ReverseCronologicalBatchMdmLinkPidReader;
033import ca.uhn.fhir.mdm.api.IMdmClearJobSubmitter;
034import ca.uhn.fhir.rest.api.server.RequestDetails;
035import ca.uhn.fhir.rest.server.exceptions.ForbiddenOperationException;
036import ca.uhn.fhir.rest.server.provider.ProviderConstants;
037import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
038import ca.uhn.fhir.rest.server.util.CompositeInterceptorBroadcaster;
039import org.springframework.batch.core.Job;
040import org.springframework.batch.core.JobExecution;
041import org.springframework.batch.core.JobParameters;
042import org.springframework.batch.core.JobParametersInvalidException;
043import org.springframework.beans.factory.annotation.Autowired;
044import org.springframework.beans.factory.annotation.Qualifier;
045
046import javax.transaction.Transactional;
047import java.util.List;
048
049public class MdmClearJobSubmitterImpl implements IMdmClearJobSubmitter {
050        @Autowired
051        DaoConfig myDaoConfig;
052        @Autowired
053        PartitionedUrlValidator myPartitionedUrlValidator;
054        @Autowired
055        IInterceptorBroadcaster myInterceptorBroadcaster;
056        @Autowired
057        private IBatchJobSubmitter myBatchJobSubmitter;
058        @Autowired
059        @Qualifier(BatchConstants.MDM_CLEAR_JOB_NAME)
060        private Job myMdmClearJob;
061
062        @Override
063        @Transactional(Transactional.TxType.NEVER)
064        public JobExecution submitJob(Integer theBatchSize, List<String> theUrls, RequestDetails theRequest) throws JobParametersInvalidException {
065                if (theBatchSize == null) {
066                        theBatchSize = myDaoConfig.getExpungeBatchSize();
067                }
068                if (!myDaoConfig.canDeleteExpunge()) {
069                        throw new ForbiddenOperationException(Msg.code(1278) + "Delete Expunge not allowed:  " + myDaoConfig.cannotDeleteExpungeReason());
070                }
071
072                RequestListJson requestListJson = myPartitionedUrlValidator.buildRequestListJson(theRequest, theUrls);
073
074                for (String url : theUrls) {
075                        HookParams params = new HookParams()
076                                .add(RequestDetails.class, theRequest)
077                                .addIfMatchesType(ServletRequestDetails.class, theRequest)
078                                .add(String.class, url);
079                        CompositeInterceptorBroadcaster.doCallHooks(myInterceptorBroadcaster, theRequest, Pointcut.STORAGE_PRE_DELETE_EXPUNGE, params);
080                }
081
082                JobParameters jobParameters = ReverseCronologicalBatchMdmLinkPidReader.buildJobParameters(ProviderConstants.OPERATION_MDM_CLEAR, theBatchSize, requestListJson);
083                return myBatchJobSubmitter.runJob(myMdmClearJob, jobParameters);
084        }
085}