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.service;
 
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
 
Service capable of deleting old or defunct entities from the persistence layer (e.g. Carts and anonymous Customers). ResourcePurgeService for additional API documentation.

A basic Quartz scheduled job configuration for calling this service can be configured as follows:

<bean id="purgeCartConfig" class="org.springframework.beans.factory.config.MapFactoryBean"> <property name="sourceMap"> <map> <entry key="SECONDS_OLD" value="2592000"/> <entry key="STATUS" value="IN_PROCESS"/> </map> </property> </bean> <p/> <bean id="purgeCartJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="blResourcePurgeService" /> <property name="targetMethod" value="purgeCarts" /> <property name="arguments"> <list> <ref bean="purgeCartConfig"/> </list> </property> </bean> <p/> <bean id="purgeCartTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> <property name="jobDetail" ref="purgeCartJobDetail" /> <property name="startDelay" value="30000" /> <property name="repeatInterval" value="86400000" /> </bean>

Author(s):
Jeff Fischer
 
 @Service("blResourcePurgeService")
 public class ResourcePurgeServiceImpl implements ResourcePurgeService {
 
     private static final Log LOG = LogFactory.getLog(ResourcePurgeServiceImpl.class);
 
     @Resource(name="blStreamingTransactionCapableUtil")
 
     @Resource(name = "blResourcePurgeDao")
     protected ResourcePurgeDao resourcePurgeDao;
 
     @Resource(name = "blOrderService")
     protected OrderService orderService;
 
     @Resource(name = "blCustomerService")
     protected CustomerService customerService;
 
     protected Integer pageSize = 10;
 
     @PostConstruct
    public void init() {
        if ( != null) {
            .setPageSize();
        }
    }
    @Override
    public void purgeCarts(final Map<StringStringconfig) {
        if (.isDebugEnabled()) {
            .debug("Purging carts");
        }
        if (MapUtils.isEmpty(config)) {
            throw new IllegalArgumentException("Cannot purge carts since there was no configuration provided. " +
                    "In the absence of config params, all carts would be candidates for deletion.");
        }
        try {
            //The removal will be performed in chunks based on page size. This minimizes transaction times.
                @Override
                public void pagedExecute(Object[] paramthrows Throwable {
                    List<Orderorders = (List<Order>) param[0];
                    for (Order cart : orders) {
                        deleteCart(cart);
                    }
                }
                @Override
                public Object[] retrievePage(int startPosint pageSize) {
                    List<Orderresults = getCartsToPurge(configstartPospageSize);
                    return new Object[]{results};
                }
                @Override
                public Long retrieveTotalCount() {
                    return getCartsToPurgeLength(config);
                }
            }, RuntimeException.class);
        } catch (Exception e) {
            .error("Unable to purge carts"e);
        }
    }
    @Override
    public void purgeCustomers(final Map<StringStringconfig) {
        if (.isDebugEnabled()) {
            .debug("Purging customers");
        }
        if (MapUtils.isEmpty(config)) {
            throw new IllegalArgumentException("Cannot purge customers since there was no configuration provided. " +
                    "In the absence of config params, all customers would be candidates for deletion.");
        }
        try {
            //The removal will be performed in chunks based on page size. This minimizes transaction times.
                @Override
                public void pagedExecute(Object[] paramthrows Throwable {
                    List<Customercustomers = (List<Customer>) param[0];
                    for (Customer customer : customers) {
                        deleteCustomer(customer);
                    }
                }
                @Override
                public Object[] retrievePage(int startPosint pageSize) {
                    List<Customerresults = getCustomersToPurge(configstartPospageSize);
                    return new Object[]{results};
                }
                @Override
                public Long retrieveTotalCount() {
                    return getCustomersToPurgeLength(config);
                }
            }, RuntimeException.class);
        } catch (Exception e) {
            .error("Unable to purge customers"e);
        }
    }
    @Override
    public Integer getPageSize() {
        return ;
    }
    @Override
    public void setPageSize(Integer pageSize) {
        this. = pageSize;
    }

    
Get the list of carts to delete from the database. Subclasses may override for custom cart retrieval logic.

Parameters:
config params for the query
Returns:
list of carts to delete
    protected List<OrdergetCartsToPurge(Map<StringStringconfigint startPosint length) {
        CartPurgeParams purgeParams = new CartPurgeParams(config).invoke();
        String[] nameArray = purgeParams.getNameArray();
        OrderStatus[] statusArray = purgeParams.getStatusArray();
        Date dateCreatedMinThreshold = purgeParams.getDateCreatedMinThreshold();
        Boolean isPreview = purgeParams.getIsPreview();
        return .findCarts(nameArraystatusArraydateCreatedMinThresholdisPreviewstartPoslength);
    }

    
Get the count of carts to delete from the database. Subclasses may override for custom cart retrieval logic.

Parameters:
config params for the query
Returns:
count of carts to delete
    protected Long getCartsToPurgeLength(Map<StringStringconfig) {
        CartPurgeParams purgeParams = new CartPurgeParams(config).invoke();
        String[] nameArray = purgeParams.getNameArray();
        OrderStatus[] statusArray = purgeParams.getStatusArray();
        Date dateCreatedMinThreshold = purgeParams.getDateCreatedMinThreshold();
        Boolean isPreview = purgeParams.getIsPreview();
        return .findCartsCount(nameArraystatusArraydateCreatedMinThresholdisPreview);
    }

    
Remove the cart from the persistence layer. Subclasses may override for custom cart retrieval logic.

Parameters:
cart the cart to remove
    protected void deleteCart(Order cart) {
        //We delete the order this way (rather than with a delete query) in order to ensure the cascades take place
        try {
            .deleteOrder(cart);
        } catch (Exception e) {
            .error("Unable to purge a cart"e);
        }
    }

    
Get the list of carts to delete from the database. Subclasses may override for custom cart retrieval logic.

Parameters:
config params for the query
Returns:
list of carts to delete
    protected List<CustomergetCustomersToPurge(Map<StringStringconfigint startPosint length) {
        CustomerPurgeParams purgeParams = new CustomerPurgeParams(config).invoke();
        Boolean isRegistered = purgeParams.getIsRegistered();
        Boolean isDeactivated = purgeParams.getIsDeactivated();
        Date dateCreatedMinThreshold = purgeParams.getDateCreatedMinThreshold();
        Boolean isPreview = purgeParams.getIsPreview();
        return .findCustomers(dateCreatedMinThresholdisRegisteredisDeactivatedisPreviewstartPoslength);
    }

    
Get the count of carts to delete from the database. Subclasses may override for custom cart retrieval logic.

Parameters:
config params for the query
Returns:
count of carts to delete
    protected Long getCustomersToPurgeLength(Map<StringStringconfig) {
        CustomerPurgeParams purgeParams = new CustomerPurgeParams(config).invoke();
        Boolean isRegistered = purgeParams.getIsRegistered();
        Boolean isDeactivated = purgeParams.getIsDeactivated();
        Date dateCreatedMinThreshold = purgeParams.getDateCreatedMinThreshold();
        Boolean isPreview = purgeParams.getIsPreview();
        return .findCustomersCount(dateCreatedMinThresholdisRegisteredisDeactivatedisPreview);
    }

    
Remove the cart from the persistence layer. Subclasses may override for custom cart retrieval logic.

Parameters:
customer the customer to remove
    protected void deleteCustomer(Customer customer) {
        //We delete the customer this way (rather than with a delete query) in order to ensure the cascades take place
        try {
            .deleteCustomer(customer);
        } catch (Exception e) {
            .error("Unable to purge a customer"e);
        }
    }
    private class CartPurgeParams {
        private Map<StringStringconfig;
        private String[] nameArray;
        private OrderStatus[] statusArray;
        private Date dateCreatedMinThreshold;
        private Boolean isPreview;
        public CartPurgeParams(Map<StringStringconfig) {
            this. = config;
        }
        public String[] getNameArray() {
            return ;
        }
        public OrderStatus[] getStatusArray() {
            return ;
        }
        public Date getDateCreatedMinThreshold() {
            return ;
        }
        public Boolean getIsPreview() {
            return ;
        }
        public CartPurgeParams invoke() {
             = null;
             = null;
             = null;
             = null;
            for (Map.Entry<StringStringentry : .entrySet()) {
                if (..toString().equals(entry.getKey())) {
                    String[] temp = entry.getValue().split(",");
                     = new OrderStatus[temp.length];
                    int index = 0;
                    for (String name : temp) {
                        OrderStatus orderStatus = OrderStatus.getInstance(name);
                        [index] = orderStatus;
                        index++;
                    }
                }
                if (..toString().equals(entry.getKey())) {
                     = entry.getValue().split(",");
                }
                if (..toString().equals(entry.getKey())) {
                    Long secondsOld = Long.parseLong(entry.getValue());
                     = new Date(SystemTime.asMillis() - (secondsOld * 1000));
                }
                if (..toString().equals(entry.getKey())) {
                     = Boolean.parseBoolean(entry.getValue());
                }
            }
            return this;
        }
    }
    private class CustomerPurgeParams {
        private Map<StringStringconfig;
        private Date dateCreatedMinThreshold;
        private Boolean isPreview;
        private Boolean isRegistered;
        private Boolean isDeactivated;
        public CustomerPurgeParams(Map<StringStringconfig) {
            this. = config;
        }
        public Date getDateCreatedMinThreshold() {
            return ;
        }
        public Boolean getIsPreview() {
            return ;
        }
        public Boolean getIsRegistered() {
            return ;
        }
        public Boolean getIsDeactivated() {
            return ;
        }
        public CustomerPurgeParams invoke() {
             = null;
             = null;
             = null;
             = null;
            for (Map.Entry<StringStringentry : .entrySet()) {
                if (..toString().equals(entry.getKey())) {
                    Long secondsOld = Long.parseLong(entry.getValue());
                     = new Date(SystemTime.asMillis() - (secondsOld * 1000));
                }
                if (..toString().equals(entry.getKey())) {
                     = Boolean.parseBoolean(entry.getValue());
                }
                if (..toString().equals(entry.getKey())) {
                     = Boolean.parseBoolean(entry.getValue());
                }
                if (..toString().equals(entry.getKey())) {
                     = Boolean.parseBoolean(entry.getValue());
                }
            }
            return this;
        }
    }
New to GrepCode? Check out our FAQ X