001package ca.uhn.fhir.jpa.batch.processor;
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.api.dao.DaoRegistry;
025import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao;
026import ca.uhn.fhir.jpa.batch.log.Logs;
027import ca.uhn.fhir.jpa.dao.ISearchBuilder;
028import ca.uhn.fhir.jpa.dao.SearchBuilderFactory;
029import ca.uhn.fhir.rest.api.server.storage.ResourcePersistentId;
030import org.hl7.fhir.instance.model.api.IBaseResource;
031import org.slf4j.Logger;
032import org.springframework.batch.item.ItemProcessor;
033import org.springframework.beans.factory.annotation.Autowired;
034import org.springframework.beans.factory.annotation.Value;
035
036import java.util.ArrayList;
037import java.util.Collections;
038import java.util.List;
039import java.util.stream.Collectors;
040
041/**
042 * Reusable Item Processor which converts ResourcePersistentIds to their IBaseResources
043 */
044public class PidToIBaseResourceProcessor implements ItemProcessor<List<ResourcePersistentId>, List<IBaseResource>> {
045         private static final Logger ourLog = Logs.getBatchTroubleshootingLog();
046
047        @Autowired
048        private SearchBuilderFactory mySearchBuilderFactory;
049
050        @Autowired
051        private DaoRegistry myDaoRegistry;
052
053
054        @Value("#{stepExecutionContext['resourceType']}")
055        private String myResourceType;
056
057        @Autowired
058        private FhirContext myContext;
059
060        @Override
061        public List<IBaseResource> process(List<ResourcePersistentId> theResourcePersistentId) {
062                String collect = theResourcePersistentId.stream().map(pid -> pid.getId().toString()).collect(Collectors.joining(","));
063                ourLog.trace("Processing PIDs: {}" + collect);
064
065                IFhirResourceDao<?> dao = myDaoRegistry.getResourceDao(myResourceType);
066                Class<? extends IBaseResource> resourceTypeClass = myContext.getResourceDefinition(myResourceType).getImplementingClass();
067
068                ISearchBuilder sb = mySearchBuilderFactory.newSearchBuilder(dao, myResourceType, resourceTypeClass);
069                List<IBaseResource> outgoing = new ArrayList<>();
070                sb.loadResourcesByPid(theResourcePersistentId, Collections.emptyList(), outgoing, false, null);
071
072                ourLog.trace("Loaded resources: {}", outgoing.stream().filter(t -> t != null).map(t -> t.getIdElement().getValue()).collect(Collectors.joining(", ")));
073
074                return outgoing;
075
076        }
077
078}