001package ca.uhn.fhir.jpa.bulk.export.job; 002 003/*- 004 * #%L 005 * HAPI FHIR Storage api 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.interceptor.model.RequestPartitionId; 025import ca.uhn.fhir.jpa.api.dao.DaoRegistry; 026import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao; 027import ca.uhn.fhir.jpa.api.model.DaoMethodOutcome; 028import ca.uhn.fhir.jpa.batch.config.BatchConstants; 029import ca.uhn.fhir.jpa.batch.log.Logs; 030import ca.uhn.fhir.jpa.partition.SystemRequestDetails; 031import ca.uhn.fhir.parser.IParser; 032import ca.uhn.fhir.rest.api.Constants; 033import ca.uhn.fhir.util.BinaryUtil; 034import org.hl7.fhir.instance.model.api.IBaseBinary; 035import org.hl7.fhir.instance.model.api.IBaseResource; 036import org.hl7.fhir.instance.model.api.IIdType; 037import org.slf4j.Logger; 038import org.springframework.batch.item.ItemWriter; 039import org.springframework.beans.factory.annotation.Value; 040 041import java.io.ByteArrayOutputStream; 042import java.io.OutputStreamWriter; 043import java.util.List; 044import java.util.Optional; 045 046public abstract class BaseResourceToFileWriter implements ItemWriter<List<IBaseResource>> { 047 protected static final Logger ourLog = Logs.getBatchTroubleshootingLog(); 048 049 protected FhirContext myFhirContext; 050 051 protected DaoRegistry myDaoRegistry; 052 053 protected ByteArrayOutputStream myOutputStream; 054 055 @Value("#{stepExecutionContext['" + BatchConstants.JOB_COLLECTION_ENTITY_ID + "']}") 056 protected Long myBulkExportCollectionEntityId; 057 058 @Value("#{stepExecutionContext['" + BatchConstants.JOB_EXECUTION_RESOURCE_TYPE + "']}") 059 protected String myResourceType; 060 061 protected IFhirResourceDao myBinaryDao; 062 private final OutputStreamWriter myWriter; 063 private final IParser myParser; 064 065 protected BaseResourceToFileWriter(FhirContext theFhirContext, DaoRegistry theDaoRegistry) { 066 myFhirContext = theFhirContext; 067 myDaoRegistry = theDaoRegistry; 068 myParser = myFhirContext.newJsonParser().setPrettyPrint(false); 069 myOutputStream = new ByteArrayOutputStream(); 070 myWriter = new OutputStreamWriter(myOutputStream, Constants.CHARSET_UTF8); 071 } 072 073 074 protected IIdType createBinaryFromOutputStream() { 075 IBaseBinary binary = BinaryUtil.newBinary(myFhirContext); 076 binary.setContentType(Constants.CT_FHIR_NDJSON); 077 binary.setContent(myOutputStream.toByteArray()); 078 DaoMethodOutcome outcome = myBinaryDao.create(binary, new SystemRequestDetails().setRequestPartitionId(RequestPartitionId.defaultPartition())); 079 return outcome.getResource().getIdElement(); 080 } 081 082 @Override 083 public void write(List<? extends List<IBaseResource>> theList) throws Exception { 084 085 int count = 0; 086 for (List<IBaseResource> resourceList : theList) { 087 for (IBaseResource nextFileResource : resourceList) { 088 myParser.encodeResourceToWriter(nextFileResource, myWriter); 089 myWriter.append("\n"); 090 count++; 091 } 092 } 093 094 Optional<IIdType> createdId = flushToFiles(); 095 if (createdId.isPresent()) { 096 ourLog.info("Created {} resources for bulk export file containing {} resources of type {} ", count, createdId.get().toUnqualifiedVersionless().getValue(), myResourceType); 097 } 098 } 099 100 protected abstract Optional<IIdType> flushToFiles(); 101 102}