/**
 * JBoss, Home of Professional Open Source.
 * Copyright 2014-2020 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * 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.
 */
package org.jboss.pnc.datastore.repositories.internal;

import org.jboss.pnc.model.BuildRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import javax.enterprise.context.Dependent;
import java.util.List;
import java.util.Set;

@Dependent
public interface BuildRecordSpringRepository
        extends JpaRepository<BuildRecord, Long>, JpaSpecificationExecutor<BuildRecord> {

    @Query("SELECT br FROM BuildRecord br WHERE br.submitTime = (SELECT max(brr.submitTime) FROM BuildRecord brr"
            + " WHERE br.buildConfigurationId = brr.buildConfigurationId) AND ((COALESCE(?1) IS NULL) OR (br.buildConfigurationId IN ?1))")
    List<BuildRecord> getLatestBuildsByBuildConfigIds(List<Integer> configIds);

    @Query("select br from BuildRecord br fetch all properties where br.id = ?1")
    BuildRecord findByIdFetchAllProperties(Long id);

    @Query("select br from BuildRecord br " + "left join fetch br.productMilestone "
            + "left join fetch br.buildConfigSetRecord " + "left join fetch br.user " + "where br.id = ?1")
    BuildRecord findByIdFetchProperties(Long id);

    @Query("SELECT DISTINCT br FROM BuildRecord br " + "JOIN br.builtArtifacts builtArtifacts "
            + "WHERE ((COALESCE(?1) IS NULL) OR (builtArtifacts.id IN ?1))")
    Set<BuildRecord> findByBuiltArtifacts(Set<Integer> dependenciesIds);
}

@@ -986,16 +986,72 @@
 y.Query;
+%0Aimport org.springframework.data.repository.query.Param;
 %0A%0Aimport
@@ -1328,16 +1328,43 @@
 r WHERE 
+(size(:configIds) %3E 0) AND 
 br.submi
@@ -1503,36 +1503,8 @@
 AND 
-((COALESCE(?1) IS NULL) OR (
 br.b
@@ -1522,27 +1522,35 @@
 rationId IN 
-?1)
+(:configIds
 )%22)%0A    List
@@ -1595,16 +1595,36 @@
 nfigIds(
+@Param(%22configIds%22) 
 List%3CInt
@@ -2144,35 +2144,40 @@
 RE (
-(COALESCE(?1) IS NULL) OR (
+size(:dependenciesIds) %3E 0) AND 
 buil
@@ -2197,11 +2197,25 @@
  IN 
-?1)
+(:dependenciesIds
 )%22)%0A
@@ -2256,16 +2256,42 @@
 tifacts(
+@Param(%22dependenciesIds%22) 
 Set%3CInte
@@ -2313,10 +2313,11 @@
 esIds);%0A
+%0A
 %7D%0A

Use Named Parameters and use size() method