com.konakart.bl
Class BasketMgr

java.lang.Object
  extended by com.konakart.bl.BaseMgr
      extended by com.konakart.bl.BasketMgr
All Implemented Interfaces:
BasketMgrIf

public class BasketMgr
extends BaseMgr
implements BasketMgrIf

Manages all business logic relevant to the management of the shopping cart within the system


Nested Class Summary
protected  class BasketMgr.OptionSortOrderComparator
          Used to sort the Option objects based on option id
 
Field Summary
protected static org.apache.commons.logging.Log log
          the log
 
Fields inherited from class com.konakart.bl.BaseMgr
templateBaseDir
 
Constructor Summary
BasketMgr(KKEngIf eng)
          Constructor
 
Method Summary
protected  int addToBasket(int customerId, Basket item, AddToBasketOptionsIf options)
          We must take the information passed into the method and create a customers_basket record as well as one or more customers_basket_attributes records.
 int addToBasket(java.lang.String sessionId, int customerId, BasketIf item)
          Determines the customerId to use and calls the protected method with the customerId.
 int addToBasketWithOptions(java.lang.String sessionId, int customerId, BasketIf item, AddToBasketOptionsIf options)
          If options is set to null, this method is identical to addToBasket().
protected  void checkBasketBelongsToCustomer(int customerId, int basketId)
          Throws an exception if the basket item doesn't belong to the customer.
protected  void checkBasketOptions(Basket b)
          We may have the case where the basket object has an array of options that only have the id and value id set.
 java.lang.String createEncodedProduct(int productId, OptionIf[] opts)
          Create the encoded product The customers_basket table contains a products_id field which is encoded like this : 1{3}6{4}3 which means product of id==1 with product_options of id==3 and product_options_values of id==6 followed by product_options of id==4 and product_options_values of id==3.
protected  FetchProductOptionsIf createFetchProductOptions(AddToBasketOptionsIf options)
          Creates a FetchProductOptions object from an AddToBasketOptions
 Option findOptionInProduct(ProductIf p, Option o)
          The basket object must have a populated product object with options.
protected  Basket getBasket(int basketId)
          Used to get a basket object using the primary key
 Basket[] getBasketItemsPerCustomer(java.lang.String sessionId, int customerId, int languageId)
          Get the basket items for a customer.
protected  Basket[] getBasketItemsPerCustomerWithOptions(int customerId, Address custAddr, int languageId, AddToBasketOptionsIf options)
          Get the basket items for a customer.
 Basket[] getBasketItemsPerCustomerWithOptions(java.lang.String sessionId, int customerId, int languageId, AddToBasketOptionsIf options)
          Get the basket items for a customer.
protected  Basket[] getBasketItemsPerEncodedProductAndCustomer(int customerId, java.lang.String encodedProduct)
          This is used to see if a product that we are adding to the basket already exists in it.
protected  int getCustomerId(java.lang.String sessionId, int customerId)
          If the sessionId is null, we check the customerId to ensure that it is negative.
protected  Option[] getOptsForBasketItem(Basket item, int priceId, int languageId, AddToBasketOptionsIf a2bOptions)
          The customers_basket table contains a products_id field which is encoded like this : 1{4}3{3}6 which means product of id==1 with product_options of id==4 and product_options_values of id==3 followed by product_options of id==3 and product_options_values of id==6.
 void mergeBaskets(java.lang.String sessionId, int customerFromId)
          Add all items from the basket of customerFrom to the basket of customerTo which is retrieved from the sessionId.
 void mergeBasketsWithOptions(java.lang.String sessionId, int customerFromId, AddToBasketOptionsIf options)
          Add all items from the basket of customerFrom to the basket of customerTo which is retrieved from the sessionId.
 void populateOptsForBasketItem(Basket item, int priceId, int languageId, AddToBasketOptionsIf a2bOptions)
          The customers_basket table contains a products_id field which is encoded like this : 1{4}3{3}6 which means product of id==1 with product_options of id==4 and product_options_values of id==3 followed by product_options of id==3 and product_options_values of id==6.
protected  void removeBasketItemsPerCustomer(int customerId)
          We delete all basket items that exist for the customer passed in as a parameter.
 void removeBasketItemsPerCustomer(java.lang.String sessionId, int customerId)
          We determine which customerId to use and then delete all basket items that exist for this customer by calling the protected method.
protected  void removeFromBasket(int customerId, BasketIf item)
          We must remove the customers_basket record and any associated customers_basket_attributes records.
 void removeFromBasket(java.lang.String sessionId, int customerId, BasketIf item)
          We must remove the customers_basket record and any associated customers_basket_attributes records.
protected  void setCriteriaWithStandardAttributes(org.apache.torque.util.Criteria c)
          Sets the criteria with the standard attributes so as not to have to repeat this code many times.
protected  void updateBasket(int customerId, Basket item, AddToBasketOptionsIf options)
          We must update the customers_basket record.
 void updateBasket(java.lang.String sessionId, int customerId, BasketIf item)
          If the sessionId is null, we check the customerId to ensure that it is negative.
 void updateBasketWithOptions(java.lang.String sessionId, int customerId, BasketIf item, AddToBasketOptionsIf options)
          If the sessionId is null, we check the customerId to ensure that it is negative.
 Basket[] updateBasketWithStockInfo(BasketIf[] basketItems)
          Receives an array of basket items and updates each one with the current stock level.
 Basket[] updateBasketWithStockInfoWithOptions(BasketIf[] basketItems, AddToBasketOptionsIf options)
          Receives an array of basket items and updates each one with the current stock level.
 
Methods inherited from class com.konakart.bl.BaseMgr
addInsertAttr, addInsertAttr, checkRequired, getAdminEngMgr, getBasketMgr, getBillingMgr, getBookableProductMgr, getCatMgr, getConfigMgr, getCookieMgr, getCurrMgr, getCustMgr, getCustomerIdFromSession, getCustomerStatsMgr, getCustomerTagMgr, getEmailMgr, getEng, getLangMgr, getManuMgr, getMiscItemMgr, getMode, getModeString, getMqMgr, getMultiStoreMgr, getNewCriteria, getNewCriteria, getOrderMgr, getOrderTotalMgr, getPaymentMgr, getProdMgr, getPromMgr, getRewardPointMgr, getSecMgr, getShippingMgr, getSolrMgr, getStoreId, getTaxMgr, getTemplate, getTemplateRoot, getVelocityContextMgr, getWishListMgr, init, isEnterprise, isMultiStoreShareCustomers, isMultiStoreShareProducts
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static org.apache.commons.logging.Log log
the log

Constructor Detail

BasketMgr

public BasketMgr(KKEngIf eng)
          throws java.lang.Exception
Constructor

Parameters:
eng -
Throws:
java.lang.Exception
Method Detail

getBasketItemsPerCustomer

public Basket[] getBasketItemsPerCustomer(java.lang.String sessionId,
                                          int customerId,
                                          int languageId)
                                   throws java.lang.Exception
Get the basket items for a customer. We determine which customerId to use and then call the protected method. Although we read the final_price from the db, we set it through a calculation since OsCommerce doesn't store this in the DB. We do store it in the DB but never use it directly from the DB.

Specified by:
getBasketItemsPerCustomer in interface BasketMgrIf
Parameters:
sessionId -
customerId -
languageId -
Returns:
Returns an array of Basket Items
Throws:
java.lang.Exception

getBasketItemsPerCustomerWithOptions

public Basket[] getBasketItemsPerCustomerWithOptions(java.lang.String sessionId,
                                                     int customerId,
                                                     int languageId,
                                                     AddToBasketOptionsIf options)
                                              throws java.lang.Exception
Get the basket items for a customer. We determine which customerId to use and then call the protected method. Although we read the final_price from the db, we set it through a calculation since OsCommerce doesn't store this in the DB. We do store it in the DB but never use it directly from the DB. If options is set to null then this call is identical to getBasketItemsPerCustomer(). The options may determine from where we read the price and quantity of the basket items.

Specified by:
getBasketItemsPerCustomerWithOptions in interface BasketMgrIf
Parameters:
sessionId -
customerId -
languageId -
options -
Returns:
Returns an array of Basket Items
Throws:
java.lang.Exception

getBasketItemsPerCustomerWithOptions

protected Basket[] getBasketItemsPerCustomerWithOptions(int customerId,
                                                        Address custAddr,
                                                        int languageId,
                                                        AddToBasketOptionsIf options)
                                                 throws java.lang.Exception
Get the basket items for a customer. Although we read the final_price from the db, we set it through a calculation since OsCommerce doesn't store this in the DB. We do store it in the DB but never use it directly from the DB. This makes sense since the basket is persisted and the price of a product may change during the life of the basket. If we are passed an address, then we use that address for calculating tax. Otherwise we get the address from the customerId. If the customer id < 0, then we use the store location for calculating tax because this means that the customer hasn't logged in yet.

Parameters:
customerId -
custAddr -
languageId -
Returns:
Returns an array of Basket Items
Throws:
java.lang.Exception

addToBasketWithOptions

public int addToBasketWithOptions(java.lang.String sessionId,
                                  int customerId,
                                  BasketIf item,
                                  AddToBasketOptionsIf options)
                           throws java.lang.Exception
If options is set to null, this method is identical to addToBasket(). Otherwise the functionality differs depending on the combination of options set.

Specified by:
addToBasketWithOptions in interface BasketMgrIf
Parameters:
sessionId -
customerId -
item -
options -
Returns:
Returns the id of the newly created Basket object
Throws:
java.lang.Exception

addToBasket

public int addToBasket(java.lang.String sessionId,
                       int customerId,
                       BasketIf item)
                throws java.lang.Exception
Determines the customerId to use and calls the protected method with the customerId.

Specified by:
addToBasket in interface BasketMgrIf
Parameters:
sessionId -
customerId -
item -
Returns:
Returns the id of the newly created Basket object
Throws:
java.lang.Exception

addToBasket

protected int addToBasket(int customerId,
                          Basket item,
                          AddToBasketOptionsIf options)
                   throws java.lang.Exception
We must take the information passed into the method and create a customers_basket record as well as one or more customers_basket_attributes records. The inserts are done in a transaction. Depending on how the options are set, if the item already exists with the same options etc, then we just increase the quantity of the existing item.

Parameters:
customerId -
item -
options -
Returns:
Returns the id of the newly created Basket object
Throws:
java.lang.Exception

getBasketItemsPerEncodedProductAndCustomer

protected Basket[] getBasketItemsPerEncodedProductAndCustomer(int customerId,
                                                              java.lang.String encodedProduct)
                                                       throws org.apache.torque.TorqueException,
                                                              com.workingdogs.village.DataSetException
This is used to see if a product that we are adding to the basket already exists in it.

Parameters:
customerId -
encodedProduct -
Returns:
Basket Object
Throws:
org.apache.torque.TorqueException
com.workingdogs.village.DataSetException

checkBasketOptions

protected void checkBasketOptions(Basket b)
                           throws KKException
We may have the case where the basket object has an array of options that only have the id and value id set. If this is the case we need to populate them with the price so that the total can be calculated. Any option that is attached to the basket, should also be present in the product, so here we get the fully populated option from the product and attach it to the basket. If it isn't present in the product we throw an exception.

Parameters:
b -
Throws:
KKException

findOptionInProduct

public Option findOptionInProduct(ProductIf p,
                                  Option o)
                           throws KKException
The basket object must have a populated product object with options. The option passed in as a parameter is only partially populated so we try to find it in the array of options of the product and return the fully populated one.

Specified by:
findOptionInProduct in interface BasketMgrIf
Parameters:
p -
o -
Returns:
Option
Throws:
KKException

updateBasket

public void updateBasket(java.lang.String sessionId,
                         int customerId,
                         BasketIf item)
                  throws KKException,
                         com.workingdogs.village.DataSetException,
                         java.lang.Exception
If the sessionId is null, we check the customerId to ensure that it is negative. If it is negative, this means that it is a non registered customer or a customer that hasn't logged in yet and so there is no customer mapped to the session id. Otherwise we ensure that the sessionId is valid and that it points to the customerId passed in as a parameter. Then we call the protected method.

Specified by:
updateBasket in interface BasketMgrIf
Parameters:
sessionId -
customerId -
item -
Throws:
java.lang.Exception
com.workingdogs.village.DataSetException
KKException

updateBasketWithOptions

public void updateBasketWithOptions(java.lang.String sessionId,
                                    int customerId,
                                    BasketIf item,
                                    AddToBasketOptionsIf options)
                             throws KKException,
                                    com.workingdogs.village.DataSetException,
                                    java.lang.Exception
If the sessionId is null, we check the customerId to ensure that it is negative. If it is negative, this means that it is a non registered customer or a customer that hasn't logged in yet and so there is no customer mapped to the session id. Otherwise we ensure that the sessionId is valid and that it points to the customerId passed in as a parameter. Then we call the protected method.

Specified by:
updateBasketWithOptions in interface BasketMgrIf
Parameters:
sessionId -
customerId -
item -
options -
Throws:
java.lang.Exception
com.workingdogs.village.DataSetException
KKException

updateBasket

protected void updateBasket(int customerId,
                            Basket item,
                            AddToBasketOptionsIf options)
                     throws KKException,
                            com.workingdogs.village.DataSetException,
                            java.lang.Exception
We must update the customers_basket record. Normally used to change the quantity. The final price is recalculated and saved. We never use the price from the db (we always recalculate it) since OsCommerce doesn't set it so we have to be compatible.

Parameters:
customerId -
item -
Throws:
java.lang.Exception
com.workingdogs.village.DataSetException
KKException

removeBasketItemsPerCustomer

public void removeBasketItemsPerCustomer(java.lang.String sessionId,
                                         int customerId)
                                  throws java.lang.Exception
We determine which customerId to use and then delete all basket items that exist for this customer by calling the protected method.

Specified by:
removeBasketItemsPerCustomer in interface BasketMgrIf
Parameters:
sessionId -
customerId -
Throws:
java.lang.Exception

removeBasketItemsPerCustomer

protected void removeBasketItemsPerCustomer(int customerId)
                                     throws java.lang.Exception
We delete all basket items that exist for the customer passed in as a parameter.

Parameters:
customerId -
Throws:
java.lang.Exception

removeFromBasket

public void removeFromBasket(java.lang.String sessionId,
                             int customerId,
                             BasketIf item)
                      throws java.lang.Exception
We must remove the customers_basket record and any associated customers_basket_attributes records.

Specified by:
removeFromBasket in interface BasketMgrIf
Parameters:
sessionId -
customerId -
item -
Throws:
java.lang.Exception

removeFromBasket

protected void removeFromBasket(int customerId,
                                BasketIf item)
                         throws java.lang.Exception
We must remove the customers_basket record and any associated customers_basket_attributes records.

Parameters:
customerId -
item -
Throws:
java.lang.Exception

populateOptsForBasketItem

public void populateOptsForBasketItem(Basket item,
                                      int priceId,
                                      int languageId,
                                      AddToBasketOptionsIf a2bOptions)
                               throws java.lang.Exception
The customers_basket table contains a products_id field which is encoded like this : 1{4}3{3}6 which means product of id==1 with product_options of id==4 and product_options_values of id==3 followed by product_options of id==3 and product_options_values of id==6. We have to read this encoded field called encodedProduct and create an array of Options and then set the opts attribute and the productId attribute of the Basket object passed in.

Specified by:
populateOptsForBasketItem in interface BasketMgrIf
Parameters:
item -
priceId -
languageId -
a2bOptions -
Throws:
java.lang.Exception

getOptsForBasketItem

protected Option[] getOptsForBasketItem(Basket item,
                                        int priceId,
                                        int languageId,
                                        AddToBasketOptionsIf a2bOptions)
                                 throws java.lang.Exception
The customers_basket table contains a products_id field which is encoded like this : 1{4}3{3}6 which means product of id==1 with product_options of id==4 and product_options_values of id==3 followed by product_options of id==3 and product_options_values of id==6. We have to read this encoded field called encodedProduct and create an array of Options and then set the productId attribute of the Basket object passed in.

Parameters:
item -
priceId -
languageId -
a2bOptions -
Returns:
Returns an array of options
Throws:
java.lang.Exception

createEncodedProduct

public java.lang.String createEncodedProduct(int productId,
                                             OptionIf[] opts)
Create the encoded product The customers_basket table contains a products_id field which is encoded like this : 1{3}6{4}3 which means product of id==1 with product_options of id==3 and product_options_values of id==6 followed by product_options of id==4 and product_options_values of id==3. The order is very important (product option ids increasing) since this the encoded product has to match regardless of the ordering of the options in the ops array.

Specified by:
createEncodedProduct in interface BasketMgrIf
Parameters:
productId -
opts -
Returns:
The encoded string

mergeBaskets

public void mergeBaskets(java.lang.String sessionId,
                         int customerFromId)
                  throws java.lang.Exception
Add all items from the basket of customerFrom to the basket of customerTo which is retrieved from the sessionId. This is done when a customer logs in, to move his temporary basket to his permanent basket.

Specified by:
mergeBaskets in interface BasketMgrIf
Parameters:
sessionId -
customerFromId -
Throws:
java.lang.Exception

mergeBasketsWithOptions

public void mergeBasketsWithOptions(java.lang.String sessionId,
                                    int customerFromId,
                                    AddToBasketOptionsIf options)
                             throws java.lang.Exception
Add all items from the basket of customerFrom to the basket of customerTo which is retrieved from the sessionId. This is done when a customer logs in, to move his temporary basket to his permanent basket.

Specified by:
mergeBasketsWithOptions in interface BasketMgrIf
Parameters:
sessionId -
customerFromId -
options -
Throws:
java.lang.Exception

getCustomerId

protected int getCustomerId(java.lang.String sessionId,
                            int customerId)
                     throws java.lang.Exception
If the sessionId is null, we check the customerId to ensure that it is negative. If it is negative, this means that it is a non registered customer or a customer that hasn't logged in yet and so there is no customer mapped to the session id. Otherwise we ensure that the sessionId is valid and we don't use the customerId because we get the id from the session.

Parameters:
sessionId -
customerId -
Returns:
Returns the customerId that should be used by the caller
Throws:
java.lang.Exception

checkBasketBelongsToCustomer

protected void checkBasketBelongsToCustomer(int customerId,
                                            int basketId)
                                     throws org.apache.torque.TorqueException,
                                            KKException
Throws an exception if the basket item doesn't belong to the customer.

Parameters:
customerId -
basketId -
Throws:
org.apache.torque.TorqueException
KKException

updateBasketWithStockInfo

public Basket[] updateBasketWithStockInfo(BasketIf[] basketItems)
                                   throws java.lang.Exception
Receives an array of basket items and updates each one with the current stock level. This is a useful check to make before checking out, to see whether the stock level is sufficient. If the product or any of the product attributes of a basket item no longer exist, then that basket item is not returned. This means that the size of the returned array may be smaller than the size of the array passed in as a parameter.

Specified by:
updateBasketWithStockInfo in interface BasketMgrIf
Parameters:
basketItems -
Returns:
Returns an array of basket items with updated stock level
Throws:
java.lang.Exception

updateBasketWithStockInfoWithOptions

public Basket[] updateBasketWithStockInfoWithOptions(BasketIf[] basketItems,
                                                     AddToBasketOptionsIf options)
                                              throws java.lang.Exception
Receives an array of basket items and updates each one with the current stock level. This is a useful check to make before checking out, to see whether the stock level is sufficient. If the product or any of the product attributes of a basket item no longer exist, then that basket item is not returned. This means that the size of the returned array may be smaller than the size of the array passed in as a parameter.

Specified by:
updateBasketWithStockInfoWithOptions in interface BasketMgrIf
Parameters:
basketItems -
options -
Returns:
Returns an array of basket items with updated stock level
Throws:
java.lang.Exception

createFetchProductOptions

protected FetchProductOptionsIf createFetchProductOptions(AddToBasketOptionsIf options)
Creates a FetchProductOptions object from an AddToBasketOptions

Parameters:
options -
Returns:
Return a FetchProductOptions object

getBasket

protected Basket getBasket(int basketId)
                    throws java.lang.Exception
Used to get a basket object using the primary key

Parameters:
basketId -
Returns:
Returns a basket object
Throws:
java.lang.Exception

setCriteriaWithStandardAttributes

protected void setCriteriaWithStandardAttributes(org.apache.torque.util.Criteria c)
Sets the criteria with the standard attributes so as not to have to repeat this code many times.

Parameters:
c -


Copyright © 2011 DS Data Systems UK Ltd.