001package ca.uhn.fhir.jpa.term.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.dao.data.ITermCodeSystemVersionDao;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026import org.springframework.batch.item.ItemReader;
027import org.springframework.beans.factory.annotation.Autowired;
028import org.springframework.beans.factory.annotation.Value;
029
030import java.util.List;
031
032import static ca.uhn.fhir.jpa.batch.config.BatchConstants.JOB_PARAM_CODE_SYSTEM_ID;
033
034/**
035 *
036 */
037public class BatchTermCodeSystemVersionDeleteReader implements ItemReader<Long> {
038        private static final Logger ourLog = LoggerFactory.getLogger(BatchTermCodeSystemVersionDeleteReader.class);
039
040        @Autowired
041        private ITermCodeSystemVersionDao myTermCodeSystemVersionDao;
042
043        @Value("#{jobParameters['" + JOB_PARAM_CODE_SYSTEM_ID  + "']}")
044        private Long myTermCodeSystemPid;
045
046        private List<Long> myTermCodeSystemVersionPidList;
047        private int myCurrentIdx = 0;
048
049
050        @Override
051        public Long read() throws Exception {
052                if (myTermCodeSystemVersionPidList == null) {
053                        myTermCodeSystemVersionPidList = myTermCodeSystemVersionDao.findSortedPidsByCodeSystemPid(myTermCodeSystemPid);
054                }
055
056                if (myTermCodeSystemVersionPidList.isEmpty())  {
057                        // nothing to process
058                        ourLog.info("Nothing to process");
059                        return null;
060                }
061
062                if (myCurrentIdx >= myTermCodeSystemVersionPidList.size())  {
063                        // nothing else to process
064                        ourLog.info("No more versions to process");
065                        return null;
066                }
067
068                // still processing elements
069                long TermCodeSystemVersionPid = myTermCodeSystemVersionPidList.get(myCurrentIdx++);
070                ourLog.info("Passing termCodeSystemVersionPid: {} to writer", TermCodeSystemVersionPid);
071                return TermCodeSystemVersionPid;
072        }
073
074
075}