Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * #%L
   * BroadleafCommerce Framework
   * %%
   * Copyright (C) 2009 - 2013 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.offer.service;
 
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
The Class OfferServiceImpl.
 
 @Service("blOfferService")
 public class OfferServiceImpl implements OfferService {
     
     private static final Log LOG = LogFactory.getLog(OfferServiceImpl.class);
 
     // should be called outside of Offer service after Offer service is executed
     @Resource(name="blCustomerOfferDao")
     protected CustomerOfferDao customerOfferDao;
 
     @Resource(name="blOfferCodeDao")
     protected OfferCodeDao offerCodeDao;
     
     @Resource(name="blOfferAuditService")
     protected OfferAuditService offerAuditService;
 
     @Resource(name="blOfferDao")
     protected OfferDao offerDao;
     
     @Resource(name="blOrderOfferProcessor")
     
     @Resource(name="blItemOfferProcessor")
     
     @Resource(name="blFulfillmentGroupOfferProcessor")
     
     @Resource(name="blPromotableItemFactory")
 
     @Resource(name = "blOfferServiceExtensionManager")
 
     @Resource(name = "blOrderService")
     protected OrderService orderService;
    @Override
    public List<OfferfindAllOffers() {
        return .readAllOffers();
    }
    @Override
    @Transactional("blTransactionManager")
    public Offer save(Offer offer) {
        return .save(offer);
    }
    @Override
    @Transactional("blTransactionManager")
    public OfferCode saveOfferCode(OfferCode offerCode) {
        offerCode.setOffer(.save(offerCode.getOffer()));
        return .save(offerCode);
    }

    
Creates a list of offers that applies to this order. All offers that are assigned to the customer, entered during checkout, or has a delivery type of automatic are added to the list. The same offer cannot appear more than once in the list.

Parameters:
code
Returns:
a List of offers that may apply to this order
    @Override
    public Offer lookupOfferByCode(String code) {
        Offer offer = null;
        OfferCode offerCode = .readOfferCodeByCode(code);
        if (offerCode != null) {
            offer = offerCode.getOffer();
        }
        return offer;
    }
    
    @Override
    public OfferCode lookupOfferCodeByCode(String code){
        return .readOfferCodeByCode(code);
    }

    
Creates a list of offers that applies to this order. All offers that are assigned to the customer, entered during checkout, or has a delivery type of automatic are added to the list. The same offer cannot appear more than once in the list.

Parameters:
order
Returns:
a List of offers that may apply to this order
    @Override
    public List<OfferbuildOfferListForOrder(Order order) {
        List<Offeroffers = new ArrayList<Offer>();
        List<CustomerOffercustomerOffers = lookupOfferCustomerByCustomer(order.getCustomer());
        for (CustomerOffer customerOffer : customerOffers) {
            if (!offers.contains(customerOffer.getOffer())) {
                offers.add(customerOffer.getOffer());
            }
        }
        List<OfferCodeorderOfferCodes = order.getAddedOfferCodes();
        orderOfferCodes = removeOutOfDateOfferCodes(orderOfferCodes);
        for (OfferCode orderOfferCode : orderOfferCodes) {
            if (!offers.contains(orderOfferCode.getOffer())) {
                offers.add(orderOfferCode.getOffer());
            }
        }
        List<OfferglobalOffers = lookupAutomaticDeliveryOffers();
        for (Offer globalOffer : globalOffers) {
            if (!offers.contains(globalOffer) && verifyMaxCustomerUsageThreshold(order.getCustomer(), globalOffer)) {
                offers.add(globalOffer);
            }
        }
        
        if ( != null) {
            .getProxy().applyAdditionalFilters(offers);
        }
        
        return offers;
    }

    
Private method used to retrieve all offers assigned to this customer. These offers have a DeliveryType of MANUAL and are programmatically assigned to the customer.

Parameters:
customer
Returns:
a List of offers assigned to the customer
    protected List<CustomerOfferlookupOfferCustomerByCustomer(Customer customer) {
        List<CustomerOfferofferCustomers = .readCustomerOffersByCustomer(customer);
        return offerCustomers;
    }

    
Private method used to retrieve all offers with DeliveryType of AUTOMATIC

Returns:
a List of automatic delivery offers
    protected List<OfferlookupAutomaticDeliveryOffers() {
        List<OfferglobalOffers = .readOffersByAutomaticDeliveryType();
        return globalOffers;
    }

    
Removes all out of date offerCodes based on the offerCode and its offer's start and end date. If an offerCode has a later start date, that offerCode will be removed. OfferCodes without a start date will still be processed. If the offerCode has a end date that has already passed, that offerCode will be removed. OfferCodes without a end date will be processed. The start and end dates on the offer will still need to be evaluated.

Parameters:
offerCodes
Returns:
a List of non-expired offers
    protected List<OfferCoderemoveOutOfDateOfferCodes(List<OfferCodeofferCodes){
        List<OfferCodeofferCodesToRemove = new ArrayList<OfferCode>();
        for (OfferCode offerCode : offerCodes) {
            if (!offerCode.isActive()){
                offerCodesToRemove.add(offerCode);
            }
        }
        // remove all offers in the offersToRemove list from original offers list
        for (OfferCode offerCode : offerCodesToRemove) {
            offerCodes.remove(offerCode);
        }
        return offerCodes;
    }
    /*
     *
     * Offers Logic:
     * 1) Remove all existing offers in the Order (order, item, and fulfillment)
     * 2) Check and remove offers
     *    a) Remove out of date offers
     *    b) Remove offers that do not apply to this customer
     * 3) Loop through offers
     *    a) Verifies type of offer (order, order item, fulfillment)
     *    b) Verifies if offer can be applies
     *    c) Assign offer to type (order, order item, or fulfillment)
     * 4) Sorts the order and item offers list by priority and then discount
     * 5) Identify the best offers to apply to order item and create adjustments for each item offer
     * 6) Compare order item adjustment price to sales price, and remove adjustments if sale price is better
     * 7) Identify the best offers to apply to the order and create adjustments for each order offer
     * 8) If item contains non-combinable offers remove either the item or order adjustments based on discount value
     * 9) Set final order item prices and reapply order offers
     *
     * Assumptions:
     * 1) % off all items will be created as an item offer with no expression
     * 2) $ off order will be created as an order offer
     * 3) Order offers applies to the best price for each item (not just retail price)
     * 4) Fulfillment offers apply to best price for each item (not just retail price)
     * 5) Stackable only applies to the same offer type (i.e. a not stackable order offer can be used with item offers)
     * 6) Fulfillment offers cannot be not combinable
     * 7) Order offers cannot be FIXED_PRICE
     * 8) FIXED_PRICE offers cannot be stackable
     * 9) Non-combinable offers only apply to the order and order items, fulfillment group offers will always apply
     *
     */
    @Override
    @Transactional("blTransactionManager")
    public void applyOffersToOrder(List<OfferoffersOrder orderthrows PricingException {
        /*
        TODO rather than a threadlocal, we should update the "shouldPrice" boolean on the service API to
        use a richer object to describe the parameters of the pricing call. This object would include
        the pricing boolean, but would also include a list of activities to include or exclude in the
        call - see http://jira.broadleafcommerce.org/browse/BLC-664
         */
        OfferContext offerContext = OfferContext.getOfferContext();
        if (offerContext == null || offerContext.executePromotionCalculation) {
            order.updatePrices();
            PromotableOrder promotableOrder = .createPromotableOrder(orderfalse);
            List<OfferfilteredOffers = .filterOffers(offersorder.getCustomer());
            if ((filteredOffers == null) || (filteredOffers.isEmpty())) {
                if (.isTraceEnabled()) {
                    .trace("No offers applicable to this order.");
                }
            } else {
                List<PromotableCandidateOrderOfferqualifiedOrderOffers = new ArrayList<PromotableCandidateOrderOffer>();
                List<PromotableCandidateItemOfferqualifiedItemOffers = new ArrayList<PromotableCandidateItemOffer>();
                .filterOffers(promotableOrderfilteredOffersqualifiedOrderOffersqualifiedItemOffers);
                if (! (qualifiedItemOffers.isEmpty() && qualifiedOrderOffers.isEmpty())) {                
                    // At this point, we should have a PromotableOrder that contains PromotableItems each of which
                    // has a list of candidatePromotions that might be applied.
                    // We also have a list of orderOffers that might apply and a list of itemOffers that might apply.
                    .applyAndCompareOrderAndItemOffers(promotableOrderqualifiedOrderOffersqualifiedItemOffers);
                }
            }
            .synchronizeAdjustmentsAndPrices(promotableOrder);
            order.setSubTotal(order.calculateSubTotal());
            order.finalizeItemPrices();
            .save(orderfalse);
        }
    }
    @Override
    @Transactional("blTransactionManager")
    public void applyFulfillmentGroupOffersToOrder(List<OfferoffersOrder orderthrows PricingException {
        OfferContext offerContext = OfferContext.getOfferContext();
        if (offerContext == null || offerContext.executePromotionCalculation) {
            PromotableOrder promotableOrder =
                    .createPromotableOrder(ordertrue);
            List<OfferpossibleFGOffers = new ArrayList<Offer>();
            for (Offer offer : offers) {
                if (offer.getType().getType().equals(..getType())) {
                    possibleFGOffers.add(offer);
                }
            }
            List<OfferfilteredOffers = .filterOffers(possibleFGOffersorder.getCustomer());
            for (Offer offer : filteredOffers) {
                .filterFulfillmentGroupLevelOffer(promotableOrderqualifiedFGOffersoffer);
            }
            if (!qualifiedFGOffers.isEmpty()) {
                .applyAllFulfillmentGroupOffers(qualifiedFGOfferspromotableOrder);
                .calculateFulfillmentGroupTotal(promotableOrder);
                .synchronizeAdjustmentsAndPrices(promotableOrder);
            }
            .save(orderfalse);
        }
    }
    
    @Override
    public boolean verifyMaxCustomerUsageThreshold(Customer customerOffer offer) {
        if (offer.isLimitedUsePerCustomer()) {                
            Long currentUses = .countUsesByCustomer(customer.getId(), offer.getId());
            if (currentUses >= offer.getMaxUsesPerCustomer()) {
                return false;
            }
        }
        return true;
    }
    
    @Override
    public boolean verifyMaxCustomerUsageThreshold(@NotNull Customer customerOfferCode code) {
        boolean underCodeMaxUses = true;
        if (code.isLimitedUse()) {
            Long currentCodeUses = .countOfferCodeUses(code.getId());
            underCodeMaxUses = currentCodeUses < code.getMaxUses();
        }
        return underCodeMaxUses && verifyMaxCustomerUsageThreshold(customercode.getOffer());
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public Set<OffergetUniqueOffersFromOrder(Order order) {
        HashSet<Offerresult = new HashSet<Offer>();
        
        Transformer adjustmentToOfferTransformer = new Transformer() {
            
            @Override
            public Object transform(Object input) {
                return ((Adjustment)input).getOffer();
            }
        };
        
        result.addAll(CollectionUtils.collect(order.getOrderAdjustments(), adjustmentToOfferTransformer));
        if (order.getOrderItems() != null) {
            for (OrderItem item : order.getOrderItems()) {
                result.addAll(CollectionUtils.collect(item.getOrderItemAdjustments(), adjustmentToOfferTransformer));
                
                //record usage for price details on the item as well
                if (item.getOrderItemPriceDetails() != null) {
                    for (OrderItemPriceDetail detail : item.getOrderItemPriceDetails()) {
                        result.addAll(CollectionUtils.collect(detail.getOrderItemPriceDetailAdjustments(), adjustmentToOfferTransformer));
                    }
                }
            }
        }
        if (order.getFulfillmentGroups() != null) {
            for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
                result.addAll(CollectionUtils.collect(fg.getFulfillmentGroupAdjustments(), adjustmentToOfferTransformer));
            }
        }
        return result;
    }
    
    @Override
    public Map<OfferOfferCodegetOffersRetrievedFromCodes(Order order)  {
    }
    
    @Override
    public Map<OfferOfferCodegetOffersRetrievedFromCodes(List<OfferCodecodesSet<OfferappliedOffers) {
        HashMap<OfferOfferCodeofferToCodeMapping = new HashMap<OfferOfferCode>();
        for (OfferCode code : codes) {
            if (appliedOffers.contains(code.getOffer())) {
                offerToCodeMapping.put(code.getOffer(), code);
            }
        }
        return offerToCodeMapping;
    }
    
    @Override
        return ;
    }
    @Override
    public void setCustomerOfferDao(CustomerOfferDao customerOfferDao) {
        this. = customerOfferDao;
    }
    @Override
    public OfferCodeDao getOfferCodeDao() {
        return ;
    }
    @Override
    public void setOfferCodeDao(OfferCodeDao offerCodeDao) {
        this. = offerCodeDao;
    }
    @Override
    public OfferDao getOfferDao() {
        return ;
    }
    @Override
    public void setOfferDao(OfferDao offerDao) {
        this. = offerDao;
    }
    @Override
        return ;
    }
    @Override
    public void setOrderOfferProcessor(OrderOfferProcessor orderOfferProcessor) {
        this. = orderOfferProcessor;
    }
    @Override
        return ;
    }
    @Override
    public void setItemOfferProcessor(ItemOfferProcessor itemOfferProcessor) {
        this. = itemOfferProcessor;
    }
    @Override
        return ;
    }
    @Override
    public void setFulfillmentGroupOfferProcessor(FulfillmentGroupOfferProcessor fulfillmentGroupOfferProcessor) {
        this. = fulfillmentGroupOfferProcessor;
    }
    @Override
        return ;
    }
    @Override
    public void setPromotableItemFactory(PromotableItemFactory promotableItemFactory) {
        this. = promotableItemFactory;
    }
    @Override
    public OfferCode findOfferCodeById(Long id) {
        return .readOfferCodeById(id);
    }
    @Override
    public OrderService getOrderService() {
        return ;
    }
    @Override
    public void setOrderService(OrderService orderService) {
        this. = orderService;
    }
New to GrepCode? Check out our FAQ X