001/*-
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2023 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 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("SELECT b from Batch2JobInstanceEntity b WHERE b.myDefinitionId = :defId AND b.myParamsJson = :params")
066        List<Batch2JobInstanceEntity> findInstancesByJobIdAndParams(
067                        @Param("defId") String theDefinitionId, @Param("params") String theParams, Pageable thePageable);
068
069        @Query("SELECT b from Batch2JobInstanceEntity b WHERE b.myStatus = :status")
070        List<Batch2JobInstanceEntity> findInstancesByJobStatus(@Param("status") StatusEnum theState, Pageable thePageable);
071
072        @Query("SELECT count(b) from Batch2JobInstanceEntity b WHERE b.myStatus = :status")
073        Integer findTotalJobsOfStatus(@Param("status") StatusEnum theState);
074
075        @Query(
076                        "SELECT b from Batch2JobInstanceEntity b WHERE b.myDefinitionId = :defId  AND b.myStatus IN( :stats ) AND b.myEndTime < :cutoff")
077        List<Batch2JobInstanceEntity> findInstancesByJobIdAndStatusAndExpiry(
078                        @Param("defId") String theDefinitionId,
079                        @Param("stats") Set<StatusEnum> theStatus,
080                        @Param("cutoff") Date theCutoff,
081                        Pageable thePageable);
082
083        @Query(
084                        "SELECT e FROM Batch2JobInstanceEntity e WHERE e.myDefinitionId = :jobDefinitionId AND e.myStatus IN :statuses")
085        List<Batch2JobInstanceEntity> fetchInstancesByJobDefinitionIdAndStatus(
086                        @Param("jobDefinitionId") String theJobDefinitionId,
087                        @Param("statuses") Set<StatusEnum> theIncompleteStatuses,
088                        Pageable thePageRequest);
089
090        @Query("SELECT e FROM Batch2JobInstanceEntity e WHERE e.myDefinitionId = :jobDefinitionId")
091        List<Batch2JobInstanceEntity> findInstancesByJobDefinitionId(
092                        @Param("jobDefinitionId") String theJobDefinitionId, Pageable thePageRequest);
093}