001package ca.uhn.fhir.jpa.delete.job;
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.jpa.batch.job.MultiUrlJobParameterValidator;
024import ca.uhn.fhir.jpa.batch.listener.PidReaderCounterListener;
025import ca.uhn.fhir.jpa.batch.reader.ReverseCronologicalBatchResourcePidReader;
026import ca.uhn.fhir.jpa.batch.writer.SqlExecutorWriter;
027import org.springframework.batch.core.Job;
028import org.springframework.batch.core.Step;
029import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
030import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
031import org.springframework.batch.core.configuration.annotation.StepScope;
032import org.springframework.batch.core.listener.ExecutionContextPromotionListener;
033import org.springframework.beans.factory.annotation.Autowired;
034import org.springframework.context.annotation.Bean;
035import org.springframework.context.annotation.Configuration;
036import org.springframework.context.annotation.Lazy;
037
038import java.util.List;
039
040import static ca.uhn.fhir.jpa.batch.config.BatchConstants.DELETE_EXPUNGE_JOB_NAME;
041
042/**
043 * Spring batch Job configuration file. Contains all necessary plumbing to run a
044 * Delete Expunge job.
045 */
046@Configuration
047public class DeleteExpungeJobConfig {
048        public static final String DELETE_EXPUNGE_URL_LIST_STEP_NAME = "delete-expunge-url-list-step";
049
050        @Autowired
051        private StepBuilderFactory myStepBuilderFactory;
052        @Autowired
053        private JobBuilderFactory myJobBuilderFactory;
054
055        @Autowired
056        private MultiUrlJobParameterValidator myMultiUrlProcessorParameterValidator;
057
058        @Autowired
059        private PidReaderCounterListener myPidCountRecorderListener;
060
061        @Autowired
062        private ReverseCronologicalBatchResourcePidReader myReverseCronologicalBatchResourcePidReader;
063
064        @Autowired
065        private SqlExecutorWriter mySqlExecutorWriter;
066
067        @Bean(name = DELETE_EXPUNGE_JOB_NAME)
068        @Lazy
069        public Job deleteExpungeJob() {
070                return myJobBuilderFactory.get(DELETE_EXPUNGE_JOB_NAME)
071                        .validator(myMultiUrlProcessorParameterValidator)
072                        .start(deleteExpungeUrlListStep())
073                        .build();
074        }
075
076        @Bean
077        public Step deleteExpungeUrlListStep() {
078                return myStepBuilderFactory.get(DELETE_EXPUNGE_URL_LIST_STEP_NAME)
079                        .<List<Long>, List<String>>chunk(1)
080                        .reader(myReverseCronologicalBatchResourcePidReader)
081                        .processor(deleteExpungeProcessor())
082                        .writer(mySqlExecutorWriter)
083                        .listener(myPidCountRecorderListener)
084                        .listener(deleteExpungePromotionListener())
085                        .build();
086        }
087
088        @Bean
089        public ExecutionContextPromotionListener deleteExpungePromotionListener() {
090                ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener();
091
092                listener.setKeys(new String[]{SqlExecutorWriter.ENTITY_TOTAL_UPDATED_OR_DELETED, PidReaderCounterListener.RESOURCE_TOTAL_PROCESSED});
093
094                return listener;
095        }
096
097        @Bean
098        @StepScope
099        public DeleteExpungeProcessor deleteExpungeProcessor() {
100                return new DeleteExpungeProcessor();
101        }
102}