/**
 * 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.spi.datastore.predicates;

import org.jboss.pnc.model.TargetRepository;
import org.jboss.pnc.model.TargetRepository_;
import org.jboss.pnc.spi.datastore.repositories.api.Predicate;

import javax.persistence.criteria.Expression;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author <a href="mailto:matejonnet@gmail.com">Matej Lazar</a>
 */
public class TargetRepositoryPredicates {

    public static Predicate<TargetRepository> byIdentifierAndPath(String identifier, String repositoryPath) {
        return (root, query, cb) -> cb.and(
                cb.equal(root.get(TargetRepository_.identifier), identifier),
                cb.equal(root.get(TargetRepository_.repositoryPath), repositoryPath));
    }

    public static Predicate<TargetRepository> withIdentifierAndPathIn(
            Set<TargetRepository.IdentifierPath> identifierAndPaths) {
        Set<String> identifiers = identifierAndPaths.stream().map(ip -> ip.getIdentifier()).collect(Collectors.toSet());
        Set<String> identifierAndPathStrings = identifierAndPaths.stream()
                .map(ip -> ip.toString())
                .collect(Collectors.toSet());
        return (root, query, cb) -> {
            Expression<String> concatPart = cb.concat(
                    root.get(TargetRepository_.identifier),
                    TargetRepository.IdentifierPath.TO_STRING_DELIMITER);
            Expression<String> concat = cb.concat(concatPart, root.get(TargetRepository_.repositoryPath));
            return cb.and(
                    root.get(TargetRepository_.identifier).in(identifiers), // optimization: don't concatenate all the
                                                                            // entries
                    concat.in(identifierAndPathStrings));
        };
    }
}

@@ -751,16 +751,39 @@
 cates;%0A%0A
+import java.util.List;%0A
 import o
@@ -1032,16 +1032,56 @@
 lectors;
+%0Aimport javax.persistence.criteria.Path;
 %0A%0A/**%0A *
@@ -1670,396 +1670,75 @@
     
-Set%3CString%3E identifiers = identifierAndPaths.stream().map(ip -%3E ip.getIdentifier()).collect(Collectors.toSet());%0A        Set%3CString%3E identifierAndPathStrings = identifierAndPaths.stream()%0A                .map(ip -%3E ip.toString())%0A                .collect(Collectors.toSet());%0A        return (root, query, cb) -%3E %7B%0A            Expression%3CString%3E concatPart = cb.concat(%0A                   
+return (root, query, cb) -%3E %7B%0A            Path%3CString%3E identifier =
  roo
@@ -1764,33 +1764,33 @@
 ory_.identifier)
-,
+;
 %0A               
@@ -1790,92 +1790,12 @@
     
-        TargetRepository.IdentifierPath.TO_STRING_DELIMITER);%0A            Expression
+Path
 %3CStr
@@ -1803,38 +1803,14 @@
 ng%3E 
-conc
+p
 at
+h
  =
- cb.concat(concatPart,
  roo
@@ -1844,25 +1844,24 @@
 ositoryPath)
-)
 ;%0A          
@@ -1866,70 +1866,133 @@
     
-return cb.and(%0A                    root.get(TargetRepository_.
+List%3Cjavax.persistence.criteria.Predicate%3E ands = identifierAndPaths.stream()%0A                    .map(ip -%3E cb.and(cb.equal(
 iden
@@ -2001,14 +2001,17 @@
 fier
-).in(i
+, ip.getI
 dent
@@ -2019,196 +2019,187 @@
 fier
-s), // optimization: don't concatenate all the%0A                                                                            // entries%0A                    concat.in(identifierAndPathStrings
+()), cb.equal(path, ip.getRepositoryPath())))%0A                    .collect(Collectors.toList());%0A%0A            return cb.or(ands.toArray(javax.persistence.criteria.Predicate%5B%5D::new
 ));%0A

Optimize queriing of Target Repositories in withIdentifierAndPathIn