001package ca.uhn.fhir.jpa.bulk.imprt.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.context.FhirContext;
024import ca.uhn.fhir.jpa.batch.config.BatchConstants;
025import ca.uhn.fhir.jpa.batch.log.Logs;
026import ca.uhn.fhir.jpa.bulk.imprt.api.IBulkDataImportSvc;
027import ca.uhn.fhir.jpa.bulk.imprt.model.BulkImportJobFileJson;
028import ca.uhn.fhir.jpa.bulk.imprt.model.ParsedBulkImportRecord;
029import ca.uhn.fhir.util.IoUtil;
030import com.google.common.io.LineReader;
031import org.hl7.fhir.instance.model.api.IBaseResource;
032import org.springframework.batch.item.ItemReader;
033import org.springframework.beans.factory.annotation.Autowired;
034import org.springframework.beans.factory.annotation.Value;
035
036import java.io.StringReader;
037
038@SuppressWarnings("UnstableApiUsage")
039public class BulkImportFileReader implements ItemReader<ParsedBulkImportRecord> {
040
041        @Autowired
042        private IBulkDataImportSvc myBulkDataImportSvc;
043        @Autowired
044        private FhirContext myFhirContext;
045        @Value("#{stepExecutionContext['" + BatchConstants.JOB_UUID_PARAMETER + "']}")
046        private String myJobUuid;
047        @Value("#{stepExecutionContext['" + BulkImportPartitioner.FILE_INDEX + "']}")
048        private int myFileIndex;
049
050        private StringReader myReader;
051        private LineReader myLineReader;
052        private int myLineIndex;
053        private String myTenantName;
054
055        @Override
056        public ParsedBulkImportRecord read() throws Exception {
057
058                if (myReader == null) {
059                        BulkImportJobFileJson file = myBulkDataImportSvc.fetchFile(myJobUuid, myFileIndex);
060                        myTenantName = file.getTenantName();
061                        myReader = new StringReader(file.getContents());
062                        myLineReader = new LineReader(myReader);
063                }
064
065                String nextLine = myLineReader.readLine();
066                if (nextLine == null) {
067                        IoUtil.closeQuietly(myReader);
068                        return null;
069                }
070
071                Logs.getBatchTroubleshootingLog().debug("Reading line {} file index {} for job: {}", myLineIndex++, myFileIndex, myJobUuid);
072
073                IBaseResource parsed = myFhirContext.newJsonParser().parseResource(nextLine);
074                return new ParsedBulkImportRecord(myTenantName, parsed, myLineIndex);
075        }
076}