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.jpa.batch.config.BatchConstants;
024import ca.uhn.fhir.jpa.bulk.export.api.IBulkDataExportSvc;
025import ca.uhn.fhir.model.primitive.IdDt;
026import ca.uhn.fhir.rest.api.Constants;
027import ca.uhn.fhir.rest.api.server.bulk.BulkDataExportOptions;
028import org.apache.commons.lang3.StringUtils;
029import org.springframework.batch.core.StepContribution;
030import org.springframework.batch.core.scope.context.ChunkContext;
031import org.springframework.batch.core.step.tasklet.Tasklet;
032import org.springframework.batch.repeat.RepeatStatus;
033import org.springframework.beans.factory.annotation.Autowired;
034
035import java.util.Arrays;
036import java.util.Date;
037import java.util.Map;
038import java.util.Set;
039import java.util.stream.Collectors;
040
041public class CreateBulkExportEntityTasklet implements Tasklet {
042
043        @Autowired
044        private IBulkDataExportSvc myBulkDataExportSvc;
045
046        public static void addUUIDToJobContext(ChunkContext theChunkContext, String theJobUUID) {
047                theChunkContext
048                        .getStepContext()
049                        .getStepExecution()
050                        .getJobExecution()
051                        .getExecutionContext()
052                        .putString(BatchConstants.JOB_UUID_PARAMETER, theJobUUID);
053        }
054
055        @Override
056        public RepeatStatus execute(StepContribution theStepContribution, ChunkContext theChunkContext) throws Exception {
057                Map<String, Object> jobParameters = theChunkContext.getStepContext().getJobParameters();
058
059                //We can leave early if they provided us with an existing job.
060                if (jobParameters.containsKey(BatchConstants.JOB_UUID_PARAMETER)) {
061                        addUUIDToJobContext(theChunkContext, (String) jobParameters.get(BatchConstants.JOB_UUID_PARAMETER));
062                        return RepeatStatus.FINISHED;
063                } else {
064                        String resourceTypes = (String) jobParameters.get("resourceTypes");
065                        Date since = (Date) jobParameters.get("since");
066                        String filters = (String) jobParameters.get("filters");
067                        Set<String> filterSet;
068                        if (StringUtils.isBlank(filters)) {
069                                filterSet = null;
070                        } else {
071                                filterSet = Arrays.stream(filters.split(",")).collect(Collectors.toSet());
072                        }
073                        Set<String> resourceTypeSet = Arrays.stream(resourceTypes.split(",")).collect(Collectors.toSet());
074
075                        String outputFormat = (String) jobParameters.get("outputFormat");
076                        if (StringUtils.isBlank(outputFormat)) {
077                                outputFormat = Constants.CT_FHIR_NDJSON;
078                        }
079
080                        BulkDataExportOptions bulkDataExportOptions = new BulkDataExportOptions();
081                        bulkDataExportOptions.setOutputFormat(outputFormat);
082                        bulkDataExportOptions.setResourceTypes(resourceTypeSet);
083                        bulkDataExportOptions.setSince(since);
084                        bulkDataExportOptions.setFilters(filterSet);
085                        //Set export style
086                        String exportStyle = (String)jobParameters.get("exportStyle");
087                        bulkDataExportOptions.setExportStyle(BulkDataExportOptions.ExportStyle.valueOf(exportStyle));
088
089                        //Set group id if present
090                        String groupId = (String)jobParameters.get("groupId");
091                        bulkDataExportOptions.setGroupId(new IdDt(groupId));
092
093                        IBulkDataExportSvc.JobInfo jobInfo = myBulkDataExportSvc.submitJob(bulkDataExportOptions);
094
095                        addUUIDToJobContext(theChunkContext, jobInfo.getJobId());
096                        return RepeatStatus.FINISHED;
097                }
098        }
099}