Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * #%L
   * BroadleafCommerce Framework
   * %%
   * Copyright (C) 2009 - 2014 Broadleaf Commerce
   * %%
   * 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%
  */
 package org.broadleafcommerce.core.util.dao;
 
 import java.util.Date;
 import java.util.List;
 
 

Author(s):
Jeff Fischer
 
 @Repository("blResourcePurgeDao")
 public class ResourcePurgeDaoImpl implements ResourcePurgeDao {
 
     @PersistenceContext(unitName = "blPU")
     protected EntityManager em;
 
     @Override
     public List<OrderfindCarts(String[] namesOrderStatus[] statusesDate dateCreatedMinThresholdBoolean isPreview) {
         TypedQuery<Orderquery = buildCartQuery(namesstatusesdateCreatedMinThresholdisPreviewOrder.class);
         return query.getResultList();
     }
 
     @Override
     public List<OrderfindCarts(String[] namesOrderStatus[] statusesDate dateCreatedMinThresholdBoolean isPreviewint startPosint length) {
         TypedQuery<Orderquery = buildCartQuery(namesstatusesdateCreatedMinThresholdisPreviewOrder.class);
         query.setFirstResult(startPos);
         query.setMaxResults(length);
         return query.getResultList();
     }
 
     @Override
     public Long findCartsCount(String[] namesOrderStatus[] statusesDate dateCreatedMinThresholdBoolean isPreview) {
         TypedQuery<Longquery = buildCartQuery(namesstatusesdateCreatedMinThresholdisPreviewLong.class);
         return query.getSingleResult();
     }
 
     @Override
     public List<CustomerfindCustomers(Date dateCreatedMinThresholdBoolean registeredBoolean deactivatedBoolean isPreview) {
         TypedQuery<Customerquery = buildCustomerQuery(dateCreatedMinThresholdregistereddeactivatedisPreviewCustomer.class);
         return query.getResultList();
     }
 
     @Override
     public List<CustomerfindCustomers(Date dateCreatedMinThresholdBoolean registeredBoolean deactivatedBoolean isPreviewint startPosint length) {
         TypedQuery<Customerquery = buildCustomerQuery(dateCreatedMinThresholdregistereddeactivatedisPreview,
                 Customer.class);
         query.setFirstResult(startPos);
         query.setMaxResults(length);
         return query.getResultList();
     }
 
     @Override
     public Long findCustomersCount(Date dateCreatedMinThresholdBoolean registeredBoolean deactivatedBoolean isPreview) {
         TypedQuery<Longquery = buildCustomerQuery(dateCreatedMinThresholdregistereddeactivatedisPreviewLong.class);
         return query.getSingleResult();
     }
 
     protected <T> TypedQuery<T> buildCustomerQuery(Date dateCreatedMinThresholdBoolean registeredBoolean deactivatedBoolean isPreviewClass<T> returnType) {
         CriteriaBuilder builder = .getCriteriaBuilder();
         CriteriaQuery<T> criteria = builder.createQuery(returnType);
         Root<CustomerImplroot = criteria.from(CustomerImpl.class);
         if (Long.class.equals(returnType)) {
             criteria.select((Selection<? extends T>) builder.count(root));
        } else {
            criteria.select((Selection<? extends T>) root);
        }
        //find only customers that do not have any orders, otherwise a purge would fail because of referential integrity
        Subquery<Longsubquery = criteria.subquery(Long.class);
        Root orderRoot = subquery.from(OrderImpl.class);
        subquery.select(builder.count(orderRoot));
        subquery.where(builder.equal(orderRoot.get("customer"),root));
        List<Predicaterestrictions = new ArrayList<Predicate>();
        restrictions.add(builder.equal(subquery, 0L));
        if (registered != null) {
            if (registered) {
                restrictions.add(builder.isTrue(root.get("registered").as(Boolean.class)));
            } else {
                restrictions.add(builder.or(builder.isNull(root.get("registered")),
                        builder.isFalse(root.get("registered").as(Boolean.class))));
            }
        }
        if (deactivated != null) {
            if (deactivated) {
                restrictions.add(builder.isTrue(root.get("deactivated").as(Boolean.class)));
            } else {
                restrictions.add(builder.or(builder.isNull(root.get("deactivated")),
                        builder.isFalse(root.get("deactivated").as(Boolean.class))));
            }
        }
        if (dateCreatedMinThreshold != null) {
            restrictions.add(builder.lessThan(root.get("auditable").get("dateCreated").as(Date.class), dateCreatedMinThreshold));
        }
        if (isPreview != null) {
            if (isPreview) {
                restrictions.add(builder.isTrue(root.get("previewable").get("isPreview").as(Boolean.class)));
            } else {
                restrictions.add(builder.or(builder.isNull(root.get("previewable").get("isPreview")),
                        builder.isFalse(root.get("previewable").get("isPreview").as(Boolean.class))));
            }
        }
        criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
        return .createQuery(criteria);
    }
    protected <T> TypedQuery<T> buildCartQuery(String[] namesOrderStatus[] statusesDate dateCreatedMinThresholdBoolean isPreviewClass<T> returnType) {
        CriteriaBuilder builder = .getCriteriaBuilder();
        CriteriaQuery<T> criteria = builder.createQuery(returnType);
        Root<OrderImplroot = criteria.from(OrderImpl.class);
        if (Long.class.equals(returnType)) {
            criteria.select((Selection<? extends T>) builder.count(root));
        } else {
            criteria.select((Selection<? extends T>) root);
        }
        List<Predicaterestrictions = new ArrayList<Predicate>();
        List<StringstatusList = new ArrayList<String>();
        if (statuses != null) {
            for (OrderStatus status : statuses) {
                statusList.add(status.getType());
            }
        } else {
            statusList.add("IN_PROCESS");
        }
        restrictions.add(root.get("status").in(statusList));
        if (names != null) {
            restrictions.add(root.get("name").in(Arrays.asList(names)));
        }
        if (dateCreatedMinThreshold != null) {
            restrictions.add(builder.lessThan(root.get("auditable").get("dateCreated").as(Date.class), dateCreatedMinThreshold));
        }
        if (isPreview != null) {
            if (isPreview) {
                restrictions.add(builder.isTrue(root.get("previewable").get("isPreview").as(Boolean.class)));
            } else {
                restrictions.add(builder.or(builder.isNull(root.get("previewable").get("isPreview")),
                        builder.isFalse(root.get("previewable").get("isPreview").as(Boolean.class))));
            }
        }
        criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
        return .createQuery(criteria);
    }
New to GrepCode? Check out our FAQ X