001package ca.uhn.fhir.jpa.bulk.export.svc;
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.bulk.export.model.BulkExportJobStatusEnum;
024import ca.uhn.fhir.jpa.dao.data.IBulkExportCollectionDao;
025import ca.uhn.fhir.jpa.dao.data.IBulkExportCollectionFileDao;
026import ca.uhn.fhir.jpa.dao.data.IBulkExportJobDao;
027import ca.uhn.fhir.jpa.entity.BulkExportCollectionEntity;
028import ca.uhn.fhir.jpa.entity.BulkExportCollectionFileEntity;
029import ca.uhn.fhir.jpa.entity.BulkExportJobEntity;
030import org.slf4j.Logger;
031import org.springframework.beans.factory.annotation.Autowired;
032import org.springframework.stereotype.Service;
033
034import javax.transaction.Transactional;
035import java.util.Collection;
036import java.util.Map;
037import java.util.Optional;
038import java.util.stream.Collectors;
039
040import static org.slf4j.LoggerFactory.getLogger;
041
042@Service
043public class BulkExportDaoSvc {
044        private static final Logger ourLog = getLogger(BulkExportDaoSvc.class);
045        @Autowired
046        IBulkExportJobDao myBulkExportJobDao;
047        @Autowired
048        IBulkExportCollectionDao myBulkExportCollectionDao;
049        @Autowired
050        IBulkExportCollectionFileDao myBulkExportCollectionFileDao;
051
052        @Transactional
053        public void addFileToCollectionWithId(Long theCollectionEntityId, BulkExportCollectionFileEntity theFile) {
054                Optional<BulkExportCollectionEntity> byId = myBulkExportCollectionDao.findById(theCollectionEntityId);
055                if (byId.isPresent()) {
056                        BulkExportCollectionEntity exportCollectionEntity = byId.get();
057                        theFile.setCollection(exportCollectionEntity);
058                        exportCollectionEntity.getFiles().add(theFile);
059                        myBulkExportCollectionFileDao.saveAndFlush(theFile);
060                        myBulkExportCollectionDao.saveAndFlush(exportCollectionEntity);
061                }
062        }
063
064        @Transactional
065        public Map<Long, String> getBulkJobCollectionIdToResourceTypeMap(String theJobUUID) {
066                BulkExportJobEntity bulkExportJobEntity = loadJob(theJobUUID);
067                Collection<BulkExportCollectionEntity> collections = bulkExportJobEntity.getCollections();
068                return collections.stream()
069                        .collect(Collectors.toMap(
070                                BulkExportCollectionEntity::getId,
071                                BulkExportCollectionEntity::getResourceType
072                        ));
073        }
074
075        private BulkExportJobEntity loadJob(String theJobUUID) {
076                Optional<BulkExportJobEntity> jobOpt = myBulkExportJobDao.findByJobId(theJobUUID);
077                if (!jobOpt.isPresent()) {
078                        ourLog.warn("Job with UUID {} appears to be deleted", theJobUUID);
079                        return null;
080                }
081                return jobOpt.get();
082        }
083
084        @Transactional
085        public void setJobToStatus(String theJobUUID, BulkExportJobStatusEnum theStatus) {
086                setJobToStatus(theJobUUID, theStatus, null);
087        }
088
089        @Transactional
090        public void setJobToStatus(String theJobUUID, BulkExportJobStatusEnum theStatus, String theStatusMessage) {
091                Optional<BulkExportJobEntity> oJob = myBulkExportJobDao.findByJobId(theJobUUID);
092                if (!oJob.isPresent()) {
093                        ourLog.error("Job with UUID {} doesn't exist!", theJobUUID);
094                        return;
095                }
096
097                ourLog.info("Setting job with UUID {} to {}", theJobUUID, theStatus);
098                BulkExportJobEntity bulkExportJobEntity = oJob.get();
099                bulkExportJobEntity.setStatus(theStatus);
100                bulkExportJobEntity.setStatusMessage(theStatusMessage);
101                myBulkExportJobDao.save(bulkExportJobEntity);
102
103        }
104
105}