package com.thinkbiganalytics.metadata.jpa.jobrepo.job;

/*-
 * #%L
 * thinkbig-operational-metadata-jpa
 * %%
 * Copyright (C) 2017 ThinkBig Analytics
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */


import com.thinkbiganalytics.metadata.jpa.feed.RepositoryType;
import com.thinkbiganalytics.metadata.jpa.feed.security.FeedOpsAccessControlRepository;

import org.joda.time.DateTime;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Set;

/**
 * Spring data repository for accessing {@link JpaBatchJobExecution}
 */
@RepositoryType(BatchJobExecutionSecuringRepository.class)
public interface BatchJobExecutionRepository extends JpaRepository<JpaBatchJobExecution, Long>, QueryDslPredicateExecutor<JpaBatchJobExecution> {

    @Query(value = "select job from JpaBatchJobExecution as job "
                   + "join JpaNifiEventJobExecution as nifiEventJob on nifiEventJob.jobExecution.jobExecutionId = job.jobExecutionId  "
                   + "where nifiEventJob.flowFileId = :flowFileId")
    JpaBatchJobExecution findByFlowFile(@Param("flowFileId") String flowFileId);


    @Query(value = "select job from JpaBatchJobExecution as job "
                   + "join JpaNifiEventJobExecution as nifiEventJob on nifiEventJob.jobExecution.jobExecutionId = job.jobExecutionId "
                   + "join JpaNifiEvent nifiEvent on nifiEvent.eventId = nifiEventJob.eventId "
                   + "and nifiEvent.flowFileId = nifiEventJob.flowFileId "
                   + "left join fetch JpaBatchJobExecutionContextValue executionContext on executionContext.jobExecutionId = job.jobExecutionId "
                   + FeedOpsAccessControlRepository.JOIN_ACL_TO_JOB
                   + " where nifiEvent.feedName = :feedName "
                   + "and job.status = 'COMPLETED' "
                   + "and job.endTime > :sinceDate "
                   + "and "+FeedOpsAccessControlRepository.WHERE_PRINCIPALS_MATCH)
    Set<JpaBatchJobExecution> findJobsForFeedCompletedSince(@Param("feedName") String feedName, @Param("sinceDate") DateTime sinceDate);

    @Query("select job from JpaBatchJobExecution as job "
           + "join JpaBatchJobInstance  jobInstance on jobInstance.jobInstanceId = job.jobInstance.jobInstanceId "
           + "join JpaOpsManagerFeed  feed on feed.id = jobInstance.feed.id "
           + FeedOpsAccessControlRepository.JOIN_ACL_TO_FEED
           + "where feed.name = :feedName "
           + "and "+FeedOpsAccessControlRepository.WHERE_PRINCIPALS_MATCH
           + "and job.endTimeMillis = (SELECT max(job2.endTimeMillis)"
           + "     from JpaBatchJobExecution as job2 "
           + "join JpaBatchJobInstance  jobInstance2 on jobInstance2.jobInstanceId = job2.jobInstance.jobInstanceId "
           + "join JpaOpsManagerFeed  feed2 on feed2.id = jobInstance2.feed.id "
           + "where feed2.name = :feedName "
           + "and job2.status = 'COMPLETED')"
           + "order by job.jobExecutionId DESC ")
    List<JpaBatchJobExecution> findLatestCompletedJobForFeed(@Param("feedName") String feedName);

    @Query("select job from JpaBatchJobExecution as job "
           + "join JpaBatchJobInstance  jobInstance on jobInstance.jobInstanceId = job.jobInstance.jobInstanceId "
           + "join JpaOpsManagerFeed  feed on feed.id = jobInstance.feed.id "
           + "where feed.name = :feedName "
           + "and job.startTimeMillis = (SELECT max(job2.startTimeMillis)"
           + "     from JpaBatchJobExecution as job2 "
           + "join JpaBatchJobInstance  jobInstance2 on jobInstance2.jobInstanceId = job2.jobInstance.jobInstanceId "
           + "join JpaOpsManagerFeed  feed2 on feed2.id = jobInstance2.feed.id "
           + "where feed2.name = :feedName )"
           + "order by job.jobExecutionId DESC ")
    List<JpaBatchJobExecution> findLatestJobForFeed(@Param("feedName") String feedName);


    @Query("select job from JpaBatchJobExecution as job "
           + "join JpaBatchJobInstance  jobInstance on jobInstance.jobInstanceId = job.jobInstance.jobInstanceId "
           + "join JpaOpsManagerFeed  feed on feed.id = jobInstance.feed.id "
           + "where feed.name = :feedName "
           + "and job.startTimeMillis = (SELECT max(job2.startTimeMillis)"
           + "     from JpaBatchJobExecution as job2 "
           + "join JpaBatchJobInstance  jobInstance2 on jobInstance2.jobInstanceId = job2.jobInstance.jobInstanceId "
           + "join JpaOpsManagerFeed  feed2 on feed2.id = jobInstance2.feed.id "
           + "where job2.endTime is null "
           + "and feed2.name = :feedName )"
           + "order by job.jobExecutionId DESC ")
    List<JpaBatchJobExecution> findLatestRunningJobForFeed(@Param("feedName") String feedName);

    @Query(value = "select case when(count(job)) > 0 then true else false end "
                   + " from JpaBatchJobExecution as job "
                   + "join JpaBatchJobInstance  jobInstance on jobInstance.jobInstanceId = job.jobInstance.jobInstanceId "
                   + "join JpaOpsManagerFeed  feed on feed.id = jobInstance.feed.id "
                   + "where feed.name = :feedName "
                   + "and job.endTime is null")
    Boolean isFeedRunning(@Param("feedName") String feedName);
}

@@ -1822,25 +1822,24 @@
 owFileId);%0A%0A
-%0A
     @Query(v
@@ -1926,88 +1926,53 @@
  Jpa
-NifiEventJobExecution as nifiEventJob on nifiEventJob.jobExecution.jobExecutionI
+BatchJobInstance jobInstance on jobInstance.i
 d = 
@@ -1974,34 +1974,34 @@
 id = job.job
-ExecutionI
+Instance.i
 d %22%0A        
@@ -2026,145 +2026,59 @@
  Jpa
-NifiEvent nifiEvent on nifiEvent.eventId = nifiEventJob.eventId %22%0A                   + %22and nifiEvent.flowFileId = nifiEventJob.flowFileI
+OpsManagerFeed feed on feed.id = jobInstance.feed.i
 d %22%0A
@@ -2248,99 +2248,21 @@
   + 
-FeedOpsAccessControlRepository.JOIN_ACL_TO_JOB%0A                   + %22 where nifiEvent.
+%22where 
 feed
-N
+.n
 ame 
@@ -2384,90 +2384,8 @@
 te %22
-%0A                   + %22and %22+FeedOpsAccessControlRepository.WHERE_PRINCIPALS_MATCH
 )%0A  
@@ -2788,173 +2788,38 @@
   + 
-FeedOpsAccessControlRepository.JOIN_ACL_TO_FEED%0A           + %22where feed.name = :feedName %22%0A           + %22and %22+FeedOpsAccessControlRepository.WHERE_PRINCIPALS_MATCH
+%22where feed.name = :feedName %22
 %0A   

fix ops mgr queries to not use the deprecated NIFI_EVENT table