001package ca.uhn.fhir.jpa.bulk.imprt.job;
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.i18n.Msg;
024import ca.uhn.fhir.jpa.batch.config.BatchConstants;
025import ca.uhn.fhir.jpa.dao.data.IBulkImportJobDao;
026import ca.uhn.fhir.jpa.entity.BulkImportJobEntity;
027import org.apache.commons.lang3.StringUtils;
028import org.springframework.batch.core.JobParameters;
029import org.springframework.batch.core.JobParametersInvalidException;
030import org.springframework.batch.core.JobParametersValidator;
031import org.springframework.beans.factory.annotation.Autowired;
032import org.springframework.transaction.PlatformTransactionManager;
033import org.springframework.transaction.support.TransactionTemplate;
034
035import java.util.Optional;
036
037/**
038 * This class will prevent a job from running if the UUID does not exist or is invalid.
039 */
040public class BulkImportJobParameterValidator implements JobParametersValidator {
041
042        @Autowired
043        private IBulkImportJobDao myBulkImportJobDao;
044        @Autowired
045        private PlatformTransactionManager myTransactionManager;
046
047        @Override
048        public void validate(JobParameters theJobParameters) throws JobParametersInvalidException {
049                if (theJobParameters == null) {
050                        throw new JobParametersInvalidException(Msg.code(784) + "This job needs Parameters: [jobUUID]");
051                }
052
053                TransactionTemplate txTemplate = new TransactionTemplate(myTransactionManager);
054                String errorMessage = txTemplate.execute(tx -> {
055                        StringBuilder errorBuilder = new StringBuilder();
056                        String jobUUID = theJobParameters.getString(BatchConstants.JOB_UUID_PARAMETER);
057                        Optional<BulkImportJobEntity> oJob = myBulkImportJobDao.findByJobId(jobUUID);
058                        if (!StringUtils.isBlank(jobUUID) && !oJob.isPresent()) {
059                                errorBuilder.append("There is no persisted job that exists with UUID: ");
060                                errorBuilder.append(jobUUID);
061                                errorBuilder.append(". ");
062                        }
063
064                        return errorBuilder.toString();
065                });
066
067                if (!StringUtils.isEmpty(errorMessage)) {
068                        throw new JobParametersInvalidException(Msg.code(785) + errorMessage);
069                }
070        }
071}