/*
 * ProActive Parallel Suite(TM):
 * The Open Source library for parallel and distributed
 * Workflows & Scheduling, Orchestration, Cloud Automation
 * and Big Data Analysis on Enterprise Grids & Clouds.
 *
 * Copyright (c) 2007 - 2017 ActiveEon
 * Contact: contact@activeeon.com
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation: version 3 of
 * the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * If needed, contact us to obtain a release under GPL Version 2 or 3
 * or a different license than the AGPL.
 */
package org.ow2.proactive.catalog.repository;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.*;

import org.ow2.proactive.catalog.repository.entity.CatalogObjectRevisionEntity;
import org.springframework.stereotype.Repository;


@Repository
public class CatalogObjectRevisionRepositoryImpl implements CatalogObjectRevisionCustom {

    @PersistenceContext
    EntityManager em;

    @Override
    public List<CatalogObjectRevisionEntity> findDefaultCatalogObjectsOfKindListAndContentTypeAndObjectNameInBucket(
            List<String> bucketNames, List<String> kindList, String contentType, String objectName, int pageNo,
            int pageSize) {
        return em.createQuery(buildCriteriaQuery(bucketNames, kindList, contentType, objectName))
                 .setMaxResults(pageSize)
                 .setFirstResult(pageNo * pageSize)
                 .getResultList();
    }

    private CriteriaQuery<CatalogObjectRevisionEntity> buildCriteriaQuery(List<String> bucketNames,
            List<String> kindList, String contentType, String objectName) {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<CatalogObjectRevisionEntity> cq = cb.createQuery(CatalogObjectRevisionEntity.class);
        Root<CatalogObjectRevisionEntity> catalogObjectRevisionEntityRoot = cq.from(CatalogObjectRevisionEntity.class);
        cq.orderBy(cb.asc(catalogObjectRevisionEntityRoot.get("projectName")));

        List<Predicate> allPredicates = new ArrayList<>();
        if (!kindList.isEmpty()) {
            Predicate kindPredicate = cb.or();
            for (String kind : kindList) {
                kindPredicate = cb.or(kindPredicate,
                                      cb.like(catalogObjectRevisionEntityRoot.get("catalogObject").get("kindLower"),
                                              kind.toLowerCase() + "%"));
            }
            allPredicates.add(kindPredicate);
        }

        if (contentType != null) {
            Predicate contentTypePredicate = cb.like(catalogObjectRevisionEntityRoot.get("catalogObject")
                                                                                    .get("contentTypeLower"),
                                                     contentType.toLowerCase() + "%");
            allPredicates.add(contentTypePredicate);
        }
        if (objectName != null) {
            Predicate objectNamePredicate = cb.like(catalogObjectRevisionEntityRoot.get("catalogObject")
                                                                                   .get("nameLower"),
                                                    "%" + objectName.toLowerCase() + "%");
            allPredicates.add(objectNamePredicate);
        }

        if (bucketNames != null) {
            Predicate bucketNamesPredicate = cb.in(catalogObjectRevisionEntityRoot.get("catalogObject")
                                                                                  .get("bucket")
                                                                                  .get("bucketName"))
                                               .value(bucketNames);
            allPredicates.add(bucketNamesPredicate);

        }

        Predicate lastCommitTimePredicate = cb.equal(catalogObjectRevisionEntityRoot.get("catalogObject")
                                                                                    .get("lastCommitTime"),
                                                     catalogObjectRevisionEntityRoot.get("commitTime"));
        allPredicates.add(lastCommitTimePredicate);

        Predicate filtersPredicate;
        if (allPredicates.size() == 1) {
            filtersPredicate = allPredicates.get(0);
        } else {
            filtersPredicate = cb.and(allPredicates.toArray(new Predicate[0]));
        }
        cq.where(filtersPredicate);

        cq.select(catalogObjectRevisionEntityRoot);
        return cq;
    }

}

@@ -2408,36 +2408,9 @@
 ty%3E 
-catalogObjectRevisionEntityR
+r
 oot 
@@ -2458,88 +2458,8 @@
 ss);
-%0A        cq.orderBy(cb.asc(catalogObjectRevisionEntityRoot.get(%22projectName%22)));
 %0A%0A  
@@ -2554,32 +2554,37 @@
 ) %7B%0A            
+List%3C
 Predicate kindPr
@@ -2576,16 +2576,17 @@
 redicate
+%3E
  kindPre
@@ -2591,24 +2591,35 @@
 redicate
+s
  = 
-cb.or
+new ArrayList%3C%3E
 ();%0A    
@@ -2690,106 +2690,23 @@
 cate
- = cb.or(kindPredicate,%0A                                      cb.like(catalogObjectRevisionEntityR
+s.add(cb.like(r
 oot.
@@ -2735,36 +2735,61 @@
 et(%22kindLower%22),
-%0A   
+ kind.toLowerCase() + %22%25%22));%0A
                 
@@ -2776,32 +2776,34 @@
 ));%0A            
+%7D%0A
                 
@@ -2802,68 +2802,84 @@
     
-                   kind.toLowerCase() + %22%25%22));%0A            %7D
+Predicate orKindPredicate = cb.or(kindPredicates.toArray(new Predicate%5B0%5D));
 %0A   
@@ -2905,17 +2905,19 @@
 tes.add(
-k
+orK
 indPredi
@@ -3026,145 +3026,33 @@
 ike(
-catalogObjectRevisionEntityRoot.get(%22catalogObject%22)%0A                                                                                    
+root.get(%22catalogObject%22)
 .get
@@ -3313,144 +3313,33 @@
 ike(
-catalogObjectRevisionEntityRoot.get(%22catalogObject%22)%0A                                                                                   
+root.get(%22catalogObject%22)
 .get
@@ -3597,240 +3597,47 @@
 .in(
-catalogObjectRevisionEntityRoot.get(%22catalogObject%22)%0A                                                                                  .get(%22bucket%22)%0A                                                                                  
+root.get(%22catalogObject%22).get(%22bucket%22)
 .get
@@ -3842,145 +3842,33 @@
 ual(
-catalogObjectRevisionEntityRoot.get(%22catalogObject%22)%0A                                                                                    
+root.get(%22catalogObject%22)
 .get
@@ -3944,36 +3944,9 @@
     
-catalogObjectRevisionEntityR
+r
 oot.
@@ -4030,184 +4030,16 @@
     
-Predicate filtersPredicate;%0A        if (allPredicates.size() == 1) %7B%0A            filtersPredicate = allPredicates.get(0);%0A        %7D else %7B%0A            filtersPredicate = cb.and
+cq.where
 (all
@@ -4077,25 +4077,16 @@
 e%5B0%5D));%0A
-        %7D
 %0A       
@@ -4093,80 +4093,70 @@
  cq.
-where(filtersPredicate);%0A%0A        cq.select(catalogObjectRevisionEntityR
+orderBy(cb.asc(root.get(%22projectName%22)));%0A%0A        cq.select(r
 oot)

Improve buildCriteriaQuery