001package ca.uhn.fhir.jpa.dao.data;
002
003import ca.uhn.fhir.jpa.bulk.export.model.BulkExportJobStatusEnum;
004import ca.uhn.fhir.jpa.entity.BulkExportJobEntity;
005import org.springframework.data.domain.Pageable;
006import org.springframework.data.domain.Slice;
007import org.springframework.data.jpa.repository.JpaRepository;
008import org.springframework.data.jpa.repository.Modifying;
009import org.springframework.data.jpa.repository.Query;
010import org.springframework.data.repository.query.Param;
011
012import java.util.Date;
013import java.util.Optional;
014
015/*
016 * #%L
017 * HAPI FHIR JPA Server
018 * %%
019 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
020 * %%
021 * Licensed under the Apache License, Version 2.0 (the "License");
022 * you may not use this file except in compliance with the License.
023 * You may obtain a copy of the License at
024 *
025 * http://www.apache.org/licenses/LICENSE-2.0
026 *
027 * Unless required by applicable law or agreed to in writing, software
028 * distributed under the License is distributed on an "AS IS" BASIS,
029 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
030 * See the License for the specific language governing permissions and
031 * limitations under the License.
032 * #L%
033 */
034
035public interface IBulkExportJobDao extends JpaRepository<BulkExportJobEntity, Long>, IHapiFhirJpaRepository {
036
037        @Query("SELECT j FROM BulkExportJobEntity j WHERE j.myJobId = :jobid")
038        Optional<BulkExportJobEntity> findByJobId(@Param("jobid") String theUuid);
039
040        @Query("SELECT j FROM BulkExportJobEntity j WHERE j.myStatus = :status")
041        Slice<BulkExportJobEntity> findByStatus(Pageable thePage, @Param("status") BulkExportJobStatusEnum theSubmitted);
042
043        @Query("SELECT j FROM BulkExportJobEntity j WHERE j.myExpiry < :cutoff")
044        Slice<BulkExportJobEntity> findByExpiry(Pageable thePage, @Param("cutoff") Date theCutoff);
045
046        @Query("SELECT j FROM BulkExportJobEntity j WHERE j.myExpiry IS NOT NULL and j.myExpiry < :cutoff AND j.myStatus <> 'BUILDING'")
047        Slice<BulkExportJobEntity> findNotRunningByExpiry(Pageable thePage, @Param("cutoff") Date theCutoff);
048
049        @Query("SELECT j FROM BulkExportJobEntity j WHERE j.myRequest = :request AND j.myCreated > :createdAfter AND j.myStatus <> :status ORDER BY j.myCreated DESC")
050        Slice<BulkExportJobEntity> findExistingJob(Pageable thePage, @Param("request") String theRequest, @Param("createdAfter") Date theCreatedAfter, @Param("status") BulkExportJobStatusEnum theNotStatus);
051
052        @Modifying
053        @Query("DELETE FROM BulkExportJobEntity t")
054        void deleteAllFiles();
055
056        @Modifying
057        @Query("DELETE FROM BulkExportJobEntity t WHERE t.myId = :pid")
058        void deleteByPid(@Param("pid") Long theId);
059}