/**
 * JBoss, Home of Professional Open Source.
 * Copyright 2014 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.spi.datastore.predicates;

import org.jboss.pnc.model.BuildConfigurationAudited;
import org.jboss.pnc.model.BuildRecord;
import org.jboss.pnc.model.BuildRecord_;
import org.jboss.pnc.model.Project;
import org.jboss.pnc.spi.datastore.repositories.api.Predicate;

import javax.persistence.criteria.Join;
import java.util.Collection;

/**
 * Predicates for {@link org.jboss.pnc.model.BuildRecord} entity.
 */
public class BuildRecordPredicates {

    public static Predicate<BuildRecord> withBuildRecordId(Integer buildRecordId) {
        return (root, query, cb) -> cb.equal(root.get(org.jboss.pnc.model.BuildRecord_.id), buildRecordId);
    }

    public static Predicate<BuildRecord> withBuildConfigurationId(Integer configurationId) {
        return (root, query, cb) -> {
            Join<BuildRecord, BuildConfigurationAudited> buildConfigurationAudited = root.join(BuildRecord_.buildConfigurationAudited);
            return cb.equal(buildConfigurationAudited.get(org.jboss.pnc.model.BuildConfigurationAudited_.id), configurationId);
        };
    }

    public static Predicate<BuildRecord> withBuildConfigurationIdInSet(Collection<Integer> buildConfigurationIds) {
        return (root, query, cb) -> {
            Join<BuildRecord, BuildConfigurationAudited> buildConfigurationAudited = root.join(BuildRecord_.buildConfigurationAudited);
            return buildConfigurationAudited.get(org.jboss.pnc.model.BuildConfigurationAudited_.id).in(buildConfigurationIds);
        };
    }

    public static Predicate<BuildRecord> withProjectId(Integer projectId) {
        return (root, query, cb) -> {
            Join<BuildRecord, BuildConfigurationAudited> buildConfigurationAudited = root.join(BuildRecord_.buildConfigurationAudited);
            Join<BuildConfigurationAudited, Project> project = buildConfigurationAudited.join(
                    org.jboss.pnc.model.BuildConfigurationAudited_.project);
            return cb.equal(project.get(org.jboss.pnc.model.Project_.id), projectId);
        };
    }

}

@@ -981,16 +981,59 @@
 icate;%0A%0A
+import com.google.common.base.Predicates;%0A%0A
 import j
@@ -1946,38 +1946,249 @@
     
-return (root, query, cb) -%3E %7B%0A
+if (buildConfigurationIds.isEmpty()) %7B%0A            // return an always false predicate if there are no build config ids%0A            return (root, query, cb) -%3E cb.disjunction();%0A        %7D else %7B%0A            return (root, query, cb) -%3E %7B%0A    
     
@@ -2264,32 +2264,57 @@
 onAudited = root
+%0A                        
 .join(BuildRecor
@@ -2336,32 +2336,36 @@
 rationAudited);%0A
+    
             retu
@@ -2447,16 +2447,41 @@
 ted_.id)
+%0A                        
 .in(buil
@@ -2504,32 +2504,47 @@
 s);%0A        
-%7D;%0A
     %7D
+;%0A        %7D%0A    %7D%0A
 %0A%0A    public

Handle loading build configuration set when there are no configurations