
001/*- 002 * #%L 003 * HAPI FHIR JPA Server 004 * %% 005 * Copyright (C) 2014 - 2025 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.jpa.dao.data; 021 022import ca.uhn.fhir.batch2.model.BatchInstanceStatusDTO; 023import ca.uhn.fhir.batch2.model.StatusEnum; 024import ca.uhn.fhir.jpa.entity.Batch2JobInstanceEntity; 025import org.springframework.data.domain.Page; 026import org.springframework.data.domain.Pageable; 027import org.springframework.data.jpa.repository.JpaRepository; 028import org.springframework.data.jpa.repository.Modifying; 029import org.springframework.data.jpa.repository.Query; 030import org.springframework.data.repository.query.Param; 031 032import java.util.Date; 033import java.util.List; 034import java.util.Set; 035 036public interface IBatch2JobInstanceRepository 037 extends JpaRepository<Batch2JobInstanceEntity, String>, IHapiFhirJpaRepository { 038 039 @Modifying 040 @Query( 041 "UPDATE Batch2JobInstanceEntity e SET e.myStatus = :status WHERE e.myId = :id and e.myStatus IN ( :prior_states )") 042 int updateInstanceStatusIfIn( 043 @Param("id") String theInstanceId, 044 @Param("status") StatusEnum theNewState, 045 @Param("prior_states") Set<StatusEnum> thePriorStates); 046 047 @Modifying 048 @Query("UPDATE Batch2JobInstanceEntity e SET e.myUpdateTime = :updated WHERE e.myId = :id") 049 int updateInstanceUpdateTime(@Param("id") String theInstanceId, @Param("updated") Date theUpdated); 050 051 @Modifying 052 @Query("UPDATE Batch2JobInstanceEntity e SET e.myCancelled = :cancelled WHERE e.myId = :id") 053 int updateInstanceCancelled(@Param("id") String theInstanceId, @Param("cancelled") boolean theCancelled); 054 055 @Modifying 056 @Query("UPDATE Batch2JobInstanceEntity e SET e.myWorkChunksPurged = true WHERE e.myId = :id") 057 int updateWorkChunksPurgedTrue(@Param("id") String theInstanceId); 058 059 @Query("SELECT b from Batch2JobInstanceEntity b " 060 + "WHERE b.myDefinitionId = :defId AND (b.myParamsJson = :params OR b.myParamsJsonVc = :params) " 061 + "AND b.myStatus IN (:stats) " 062 + "AND (:cancelled IS NULL OR b.myCancelled = :cancelled)") 063 List<Batch2JobInstanceEntity> findInstancesByJobIdParamsAndStatus( 064 @Param("defId") String theDefinitionId, 065 @Param("params") String theParams, 066 @Param("stats") Set<StatusEnum> theStatus, 067 @Param("cancelled") Boolean theIsCancelledBoolean, 068 Pageable thePageable); 069 070 @Query( 071 "SELECT b from Batch2JobInstanceEntity b WHERE b.myDefinitionId = :defId AND (b.myParamsJson = :params OR b.myParamsJsonVc = :params)") 072 List<Batch2JobInstanceEntity> findInstancesByJobIdAndParams( 073 @Param("defId") String theDefinitionId, @Param("params") String theParams, Pageable thePageable); 074 075 @Query("SELECT b from Batch2JobInstanceEntity b " 076 + "WHERE (:definitionId IS NULL OR b.myDefinitionId = :definitionId) " 077 + "AND (:status IS NULL OR b.myStatus = :status) " 078 + "AND (:jobId IS NULL OR b.myId = :jobId) " 079 + "AND (cast(:from as date) IS NULL OR b.myCreateTime >= :from) " 080 + "AND (cast(:to as date) IS NULL OR b.myCreateTime <= :to)") 081 Page<Batch2JobInstanceEntity> findByJobDefinitionIdOrStatusOrIdOrCreateTime( 082 @Param("definitionId") String theDefinitionId, 083 @Param("status") StatusEnum theStatus, 084 @Param("jobId") String theJobId, 085 @Param("from") Date theFrom, 086 @Param("to") Date theTo, 087 Pageable thePageable); 088 089 @Query( 090 "SELECT b from Batch2JobInstanceEntity b WHERE b.myDefinitionId = :defId AND b.myStatus IN( :stats ) AND b.myEndTime < :cutoff") 091 List<Batch2JobInstanceEntity> findInstancesByJobIdAndStatusAndExpiry( 092 @Param("defId") String theDefinitionId, 093 @Param("stats") Set<StatusEnum> theStatus, 094 @Param("cutoff") Date theCutoff, 095 Pageable thePageable); 096 097 @Query( 098 "SELECT e FROM Batch2JobInstanceEntity e WHERE e.myDefinitionId = :jobDefinitionId AND e.myStatus IN :statuses") 099 List<Batch2JobInstanceEntity> fetchInstancesByJobDefinitionIdAndStatus( 100 @Param("jobDefinitionId") String theJobDefinitionId, 101 @Param("statuses") Set<StatusEnum> theIncompleteStatuses, 102 Pageable thePageRequest); 103 104 @Query("SELECT e FROM Batch2JobInstanceEntity e WHERE e.myDefinitionId = :jobDefinitionId") 105 List<Batch2JobInstanceEntity> findInstancesByJobDefinitionId( 106 @Param("jobDefinitionId") String theJobDefinitionId, Pageable thePageRequest); 107 108 @Query( 109 "SELECT new ca.uhn.fhir.batch2.model.BatchInstanceStatusDTO(e.myId, e.myStatus, e.myStartTime, e.myEndTime) FROM Batch2JobInstanceEntity e WHERE e.myId = :id") 110 BatchInstanceStatusDTO fetchBatchInstanceStatus(@Param("id") String theInstanceId); 111}