001package ca.uhn.fhir.jpa.bulk.export.job;
002
003/*-
004 * #%L
005 * HAPI FHIR JPA Server
006 * %%
007 * Copyright (C) 2014 - 2021 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.svc.BulkExportDaoSvc;
025import org.slf4j.Logger;
026import org.springframework.batch.core.partition.support.Partitioner;
027import org.springframework.batch.item.ExecutionContext;
028import org.springframework.beans.factory.annotation.Autowired;
029import org.springframework.beans.factory.annotation.Value;
030
031import java.util.HashMap;
032import java.util.Map;
033
034import static org.slf4j.LoggerFactory.getLogger;
035
036public class ResourceTypePartitioner implements Partitioner {
037        private static final Logger ourLog = getLogger(ResourceTypePartitioner.class);
038
039
040        @Value("#{jobExecutionContext['jobUUID']}")
041        private String myJobUUID;
042
043        @Autowired
044        private BulkExportDaoSvc myBulkExportDaoSvc;
045
046        @Override
047        public Map<String, ExecutionContext> partition(int gridSize) {
048                Map<String, ExecutionContext> partitionContextMap = new HashMap<>();
049
050                Map<Long, String> idToResourceType = myBulkExportDaoSvc.getBulkJobCollectionIdToResourceTypeMap(myJobUUID);
051
052                idToResourceType.entrySet().stream()
053                        .forEach(entry -> {
054                                String resourceType = entry.getValue();
055                                Long collectionEntityId = entry.getKey();
056                                ourLog.debug("Creating a partition step for CollectionEntity: [{}] processing resource type [{}]", collectionEntityId, resourceType);
057
058                                ExecutionContext context = new ExecutionContext();
059                                //The worker step needs to know what resource type it is looking for.
060                                context.putString("resourceType", resourceType);
061
062                                // The worker step needs to know which parent job it is processing for, and which collection entity it will be
063                                // attaching its results to.
064                                context.putString(BatchConstants.JOB_UUID_PARAMETER, myJobUUID);
065                                context.putLong("bulkExportCollectionEntityId", collectionEntityId);
066
067                                // Name the partition based on the resource type
068                                partitionContextMap.put(resourceType, context);
069                                });
070
071
072                return partitionContextMap;
073        }
074
075
076}