001/*-
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2024 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.StatusEnum;
023import ca.uhn.fhir.jpa.entity.Batch2JobInstanceEntity;
024import org.springframework.data.domain.Pageable;
025import org.springframework.data.jpa.repository.JpaRepository;
026import org.springframework.data.jpa.repository.Modifying;
027import org.springframework.data.jpa.repository.Query;
028import org.springframework.data.repository.query.Param;
029
030import java.util.Date;
031import java.util.List;
032import java.util.Set;
033
034public interface IBatch2JobInstanceRepository
035                extends JpaRepository<Batch2JobInstanceEntity, String>, IHapiFhirJpaRepository {
036
037        @Modifying
038        @Query(
039                        "UPDATE Batch2JobInstanceEntity e SET e.myStatus = :status WHERE e.myId = :id and e.myStatus IN ( :prior_states )")
040        int updateInstanceStatusIfIn(
041                        @Param("id") String theInstanceId,
042                        @Param("status") StatusEnum theNewState,
043                        @Param("prior_states") Set<StatusEnum> thePriorStates);
044
045        @Modifying
046        @Query("UPDATE Batch2JobInstanceEntity e SET e.myUpdateTime = :updated WHERE e.myId = :id")
047        int updateInstanceUpdateTime(@Param("id") String theInstanceId, @Param("updated") Date theUpdated);
048
049        @Modifying
050        @Query("UPDATE Batch2JobInstanceEntity e SET e.myCancelled = :cancelled WHERE e.myId = :id")
051        int updateInstanceCancelled(@Param("id") String theInstanceId, @Param("cancelled") boolean theCancelled);
052
053        @Modifying
054        @Query("UPDATE Batch2JobInstanceEntity e SET e.myWorkChunksPurged = true WHERE e.myId = :id")
055        int updateWorkChunksPurgedTrue(@Param("id") String theInstanceId);
056
057        @Query(
058                        "SELECT b from Batch2JobInstanceEntity b WHERE b.myDefinitionId = :defId AND (b.myParamsJson = :params OR b.myParamsJsonVc = :params) AND b.myStatus IN( :stats )")
059        List<Batch2JobInstanceEntity> findInstancesByJobIdParamsAndStatus(
060                        @Param("defId") String theDefinitionId,
061                        @Param("params") String theParams,
062                        @Param("stats") Set<StatusEnum> theStatus,
063                        Pageable thePageable);
064
065        @Query(
066                        "SELECT b from Batch2JobInstanceEntity b WHERE b.myDefinitionId = :defId AND (b.myParamsJson = :params OR b.myParamsJsonVc = :params)")
067        List<Batch2JobInstanceEntity> findInstancesByJobIdAndParams(
068                        @Param("defId") String theDefinitionId, @Param("params") String theParams, Pageable thePageable);
069
070        @Query("SELECT b from Batch2JobInstanceEntity b WHERE b.myStatus = :status")
071        List<Batch2JobInstanceEntity> findInstancesByJobStatus(@Param("status") StatusEnum theState, Pageable thePageable);
072
073        @Query("SELECT count(b) from Batch2JobInstanceEntity b WHERE b.myStatus = :status")
074        Integer findTotalJobsOfStatus(@Param("status") StatusEnum theState);
075
076        @Query(
077                        "SELECT b from Batch2JobInstanceEntity b WHERE b.myDefinitionId = :defId  AND b.myStatus IN( :stats ) AND b.myEndTime < :cutoff")
078        List<Batch2JobInstanceEntity> findInstancesByJobIdAndStatusAndExpiry(
079                        @Param("defId") String theDefinitionId,
080                        @Param("stats") Set<StatusEnum> theStatus,
081                        @Param("cutoff") Date theCutoff,
082                        Pageable thePageable);
083
084        @Query(
085                        "SELECT e FROM Batch2JobInstanceEntity e WHERE e.myDefinitionId = :jobDefinitionId AND e.myStatus IN :statuses")
086        List<Batch2JobInstanceEntity> fetchInstancesByJobDefinitionIdAndStatus(
087                        @Param("jobDefinitionId") String theJobDefinitionId,
088                        @Param("statuses") Set<StatusEnum> theIncompleteStatuses,
089                        Pageable thePageRequest);
090
091        @Query("SELECT e FROM Batch2JobInstanceEntity e WHERE e.myDefinitionId = :jobDefinitionId")
092        List<Batch2JobInstanceEntity> findInstancesByJobDefinitionId(
093                        @Param("jobDefinitionId") String theJobDefinitionId, Pageable thePageRequest);
094}