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(
060                        "SELECT b from Batch2JobInstanceEntity b WHERE b.myDefinitionId = :defId AND (b.myParamsJson = :params OR b.myParamsJsonVc = :params) AND b.myStatus IN( :stats )")
061        List<Batch2JobInstanceEntity> findInstancesByJobIdParamsAndStatus(
062                        @Param("defId") String theDefinitionId,
063                        @Param("params") String theParams,
064                        @Param("stats") Set<StatusEnum> theStatus,
065                        Pageable thePageable);
066
067        @Query(
068                        "SELECT b from Batch2JobInstanceEntity b WHERE b.myDefinitionId = :defId AND (b.myParamsJson = :params OR b.myParamsJsonVc = :params)")
069        List<Batch2JobInstanceEntity> findInstancesByJobIdAndParams(
070                        @Param("defId") String theDefinitionId, @Param("params") String theParams, Pageable thePageable);
071
072        @Query("SELECT b from Batch2JobInstanceEntity b "
073                        + "WHERE (:definitionId IS NULL OR b.myDefinitionId = :definitionId) "
074                        + "AND (:status IS NULL OR b.myStatus = :status) "
075                        + "AND (:jobId IS NULL OR b.myId = :jobId) "
076                        + "AND (:from IS NULL OR b.myCreateTime >= :from) "
077                        + "AND (:to IS NULL OR b.myCreateTime <= :to)")
078        Page<Batch2JobInstanceEntity> findByJobDefinitionIdOrStatusOrIdOrCreateTime(
079                        @Param("definitionId") String theDefinitionId,
080                        @Param("status") StatusEnum theStatus,
081                        @Param("jobId") String theJobId,
082                        @Param("from") Date theFrom,
083                        @Param("to") Date theTo,
084                        Pageable thePageable);
085
086        @Query(
087                        "SELECT b from Batch2JobInstanceEntity b WHERE b.myDefinitionId = :defId  AND b.myStatus IN( :stats ) AND b.myEndTime < :cutoff")
088        List<Batch2JobInstanceEntity> findInstancesByJobIdAndStatusAndExpiry(
089                        @Param("defId") String theDefinitionId,
090                        @Param("stats") Set<StatusEnum> theStatus,
091                        @Param("cutoff") Date theCutoff,
092                        Pageable thePageable);
093
094        @Query(
095                        "SELECT e FROM Batch2JobInstanceEntity e WHERE e.myDefinitionId = :jobDefinitionId AND e.myStatus IN :statuses")
096        List<Batch2JobInstanceEntity> fetchInstancesByJobDefinitionIdAndStatus(
097                        @Param("jobDefinitionId") String theJobDefinitionId,
098                        @Param("statuses") Set<StatusEnum> theIncompleteStatuses,
099                        Pageable thePageRequest);
100
101        @Query("SELECT e FROM Batch2JobInstanceEntity e WHERE e.myDefinitionId = :jobDefinitionId")
102        List<Batch2JobInstanceEntity> findInstancesByJobDefinitionId(
103                        @Param("jobDefinitionId") String theJobDefinitionId, Pageable thePageRequest);
104
105        @Query(
106                        "SELECT new ca.uhn.fhir.batch2.model.BatchInstanceStatusDTO(e.myId, e.myStatus, e.myStartTime, e.myEndTime) FROM Batch2JobInstanceEntity e WHERE e.myId = :id")
107        BatchInstanceStatusDTO fetchBatchInstanceStatus(@Param("id") String theInstanceId);
108}