001package ca.uhn.fhir.rest.server.provider;
002
003/*-
004 * #%L
005 * HAPI FHIR - Server Framework
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.i18n.Msg;
024import ca.uhn.fhir.context.FhirContext;
025import ca.uhn.fhir.rest.api.server.RequestDetails;
026import ca.uhn.fhir.rest.api.server.storage.IMultiUrlJobSubmitter;
027import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
028import ca.uhn.fhir.util.ParametersUtil;
029import org.hl7.fhir.instance.model.api.IBaseParameters;
030import org.hl7.fhir.instance.model.api.IPrimitiveType;
031import org.springframework.batch.core.JobExecution;
032import org.springframework.batch.core.JobParametersInvalidException;
033
034import javax.annotation.Nullable;
035import java.math.BigDecimal;
036import java.util.List;
037
038public class MultiUrlProcessor {
039        private final FhirContext myFhirContext;
040        private final IMultiUrlJobSubmitter myMultiUrlProcessorJobSubmitter;
041
042        public MultiUrlProcessor(FhirContext theFhirContext, IMultiUrlJobSubmitter theMultiUrlProcessorJobSubmitter) {
043                myMultiUrlProcessorJobSubmitter = theMultiUrlProcessorJobSubmitter;
044                myFhirContext = theFhirContext;
045        }
046
047        public IBaseParameters processUrls(List<String> theUrlsToProcess, Integer theBatchSize, RequestDetails theRequestDetails) {
048                try {
049                        JobExecution jobExecution = myMultiUrlProcessorJobSubmitter.submitJob(theBatchSize, theUrlsToProcess, theRequestDetails);
050                        IBaseParameters retval = ParametersUtil.newInstance(myFhirContext);
051                        ParametersUtil.addParameterToParametersLong(myFhirContext, retval, ProviderConstants.OPERATION_BATCH_RESPONSE_JOB_ID, jobExecution.getJobId());
052                        return retval;
053                } catch (JobParametersInvalidException e) {
054                        throw new InvalidRequestException(Msg.code(309) + "Invalid job parameters: " + e.getMessage(), e);
055                }
056        }
057
058        @Nullable
059        public Integer getBatchSize(IPrimitiveType<BigDecimal> theBatchSize) {
060                Integer batchSize = null;
061                if (theBatchSize != null && !theBatchSize.isEmpty()) {
062                        batchSize = theBatchSize.getValue().intValue();
063                }
064                return batchSize;
065        }
066}