/*
* Created on Mar 2, 2004
*/
package org.openedit.store.modules;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.entermediadb.asset.MediaArchive;
import org.entermediadb.links.Link;
import org.openedit.Data;
import org.openedit.OpenEditException;
import org.openedit.WebPageRequest;
import org.openedit.data.Searcher;
import org.openedit.data.SearcherManager;
import org.openedit.event.WebEvent;
import org.openedit.hittracker.HitTracker;
import org.openedit.hittracker.SearchQuery;
import org.openedit.money.Money;
import org.openedit.page.Page;
import org.openedit.profile.UserProfile;
import org.openedit.store.Cart;
import org.openedit.store.CartItem;
import org.openedit.store.Category;
import org.openedit.store.CreditPaymentMethod;
import org.openedit.store.InventoryItem;
import org.openedit.store.Price;
import org.openedit.store.PriceSupport;
import org.openedit.store.Product;
import org.openedit.store.ProductAdder;
import org.openedit.store.PurchaseOrderMethod;
import org.openedit.store.ShippingMethod;
import org.openedit.store.Store;
import org.openedit.store.convert.ConvertStatus;
import org.openedit.store.customer.Address;
import org.openedit.store.customer.Customer;
import org.openedit.store.orders.Order;
import org.openedit.store.orders.OrderArchive;
import org.openedit.store.orders.OrderState;
import org.openedit.store.orders.SubmittedOrder;
import org.openedit.users.User;
import org.openedit.users.UserSearcher;
import org.openedit.util.DateStorageUtil;
import org.openedit.util.PathUtilities;
/**
* @author cburkey
*
*/
public class CartModule extends BaseStoreModule
{
protected List fieldListConverters;
protected UserSearcher fieldUserSearcher;
protected SearcherManager fieldSearcherManager;
public SearcherManager getSearcherManager()
{
return fieldSearcherManager;
}
public void setSearcherManager(SearcherManager inSearcherManager)
{
fieldSearcherManager = inSearcherManager;
}
public UserSearcher getUserSearcher(String inCatalogId)
{
if (fieldUserSearcher == null)
{
fieldUserSearcher = (UserSearcher) getSearcherManager().getSearcher(inCatalogId, "user");
}
return fieldUserSearcher;
}
public void setUserSearcher(UserSearcher inUserSearcher)
{
fieldUserSearcher = inUserSearcher;
}
private static final Log log = LogFactory.getLog(CartModule.class);
public CartModule()
{
}
public void loadCatalog(WebPageRequest inPageRequest) throws OpenEditException
{
String id = inPageRequest.getRequestParameter(CATEGORYID);
if (id == null)
{
id = inPageRequest.getCurrentAction().getChildValue("catalogid");
}
if (id == null)
{
Page page = inPageRequest.getPage();
id = page.get(CATEGORYID);
}
if (id == null)
{
Page page = inPageRequest.getContentPage();
id = page.get(CATEGORYID);
}
if (id == null)
{
// get it from the path name
String path = inPageRequest.getPath();
id = PathUtilities.extractPageName(path);
}
Store store = getStore(inPageRequest);
Category cat = store.getCategory(id);
if (cat == null)
{
log.error("No Such catalog: " + id);
inPageRequest.putPageValue("catalog", "No such catalog " + id);
}
else
{
// Crumb crumb = store.buildCrumb(cat);
Link link = store.buildLink(cat, inPageRequest.findValue("url-prefix"));
inPageRequest.putPageValue("crumb", link);
inPageRequest.putPageValue("catalog", cat); // @deprecated
inPageRequest.putPageValue("category", cat);
getCart(inPageRequest).setLastVisitedCatalog(cat);
}
}
public void loadProductDetails(WebPageRequest inPageRequest) throws OpenEditException
{
/*
* Page page = inPageRequest.getContentPage(); if (page.isImage()) //
* this action is called for every image. Yuck. { return; } Store store
* = getStore(inPageRequest); String sourcePath =
* inPageRequest.getRequestParameter("sourcepath"); String id =
* inPageRequest.getRequestParameter("productid");
*
* Product item = null;
*
* if (sourcePath != null) { item =
* store.getProductBySourcePath(sourcePath); }
*
* if( id != null) { if( id.startsWith("multiedit:") ) { Data data =
* (Data)inPageRequest.getSessionValue(id);
* inPageRequest.putPageValue("product", item);
* inPageRequest.putPageValue("data", item); return; } } else if (item
* == null ) { item = store.getProductBySourcePath(page); }
*
* if ( item == null ) { if ( id == null ) { id =
* page.getPageSettings().getUserDefined("product", "id"); } item =
* store.getProduct(id); // get it from the path? // String path =
* page.getPath(); // id = PathUtilities.extractPageName(path); }
*/
Product item = getProduct(inPageRequest);
if (item == null)
{
log.debug("Product: is null, cannot put page value");
return;
}
Store store = getStore(inPageRequest);
inPageRequest.putPageValue("product", item);
inPageRequest.putPageValue("data", item);
Category cat = item.getDefaultCategory();
if (cat != null)
{
Link crumb = store.buildLink(cat, inPageRequest.findValue("url-prefix"));
inPageRequest.putPageValue("crumb", crumb);
}
}
public void clearCart(WebPageRequest inPageRequest) throws Exception
{
Cart cart = getCart(inPageRequest);
if (cart != null)
{
cart.removeAllItems();
}
}
/**
* @deprecated used on aniara TODO: Replace with cart properties
* @param inReq
* @throws Exception
*/
public void setRegion(WebPageRequest inReq) throws Exception
{
Cart cart = getCart(inReq);
String region = inReq.getRequestParameter("region");
cart.setRegion(region);
}
public void updateCart(WebPageRequest inPageRequest) throws Exception
{
Cart cart = getCart(inPageRequest);
getProductAdder().updateCart(inPageRequest, cart);
}
/**
* @param inPageRequest
*/
// Is this used anywhere?
public void removeProduct(WebPageRequest inPageRequest) throws Exception
{
String productId = inPageRequest.getRequestParameter("productid");
if (productId != null)
{
Store store = getStore(inPageRequest);
Product product = store.getProduct(productId);
Cart cart = getCart(inPageRequest);
cart.removeProduct(product);
}
}
public void removeItemById(WebPageRequest inPageRequest) throws Exception
{
String id = inPageRequest.getRequestParameter("itemid");
if (id != null)
{
Cart cart = getCart(inPageRequest);
CartItem cartitem = null;
// find cartitem first
for (Iterator> iterator = cart.getItemIterator(); iterator.hasNext();)
{
CartItem item = (CartItem) iterator.next();
if (id.equals(item.getId()))
{
cartitem = item;
break;
}
}
cart.removeById(id);
cart.recalculateAdjustments();
}
}
public void toggleProduct(WebPageRequest inPageRequest) throws Exception
{
String productId = inPageRequest.getRequestParameter("productid");
if (productId != null)
{
Store store = getStore(inPageRequest);
Product product = store.getProduct(productId);
Cart cart = getCart(inPageRequest);
if (cart.containsProduct(productId))
{
cart.removeProduct(product);
}
else
{
updateCart(inPageRequest);
}
}
}
protected ProductAdder getProductAdder()
{
ProductAdder adder = (ProductAdder) getModuleManager().getBean("ProductAdder");
return adder;
}
/*
* public void addItem(WebPageRequest inPageRequest) throws Exception { Cart
* cart = getCart(inPageRequest); getProductAdder().addItem(inPageRequest,
* cart); }
*/
public void addCoupon(WebPageRequest inReq) throws Exception
{
Cart cart = getCart(inReq);
getProductAdder().addCoupon(inReq, cart);
}
public Cart getCart(WebPageRequest inPageRequest) throws OpenEditException
{
Store store = getStore(inPageRequest);
Cart cart = (Cart) inPageRequest.getSessionValue(store.getCatalogId() + "cart");
if (cart == null)
{
cart = new Cart();
inPageRequest.putSessionValue(store.getCatalogId() + "cart", cart);
}
cart.setStore(store);
inPageRequest.putPageValue("cart", cart);
return cart;
}
public void createCustomer(WebPageRequest inReq) throws Exception
{
Store store = getStore(inReq);
Cart cart = getCart(inReq);
if (cart.getCustomer() != null && cart.getCustomer().getUser() != null)
{
log.info("customer \"" + cart.getCustomer().getUser() + "\" already present on cart, aborting");
return;
}
// page could have username, email, password + first/last names
String username = inReq.getRequestParameter("userName");
String email = inReq.getRequestParameter("email");
String password = inReq.getRequestParameter("password");
String firstname = inReq.getRequestParameter("firstName");
String lastname = inReq.getRequestParameter("lastName");
if (username != null && username.isEmpty())
{
username = null;
}
if (email != null && email.isEmpty())
{
email = null;
}
else if (email != null)
{
email = email.toLowerCase().trim();
}
if (password != null && password.isEmpty())
{
password = null;
}
if (firstname != null && firstname.isEmpty())
{
firstname = null;
}
if (lastname != null && lastname.isEmpty())
{
lastname = null;
}
if (cart.getCustomer() != null)
{
if (username == null && email == null)
{
return;
}
if ((username != null || email != null) && password != null)
{
User user = null;
if (username != null)
{
user = store.getUserManager().getUser(username);
}
if (user == null && email != null)
{
user = store.getUserManager().getUserByEmail(email);
}
if (user != null && cart.getCustomer().getUserName().equals(user.getUserName()))
{
log.info("Already created " + username);
return;
}
}
}
Customer customer = null;
if ((username != null || email != null) && password != null)
{
User user = null;
if (username != null)
{
user = getUserSearcher(store.getCatalogId()).getUser(username);
}
if (user == null && email != null)
{
user = getUserSearcher(store.getCatalogId()).getUserByEmail(email);
}
if (user != null)
{
if (getUserManager(inReq).authenticate(user, password))
{
customer = store.getCustomerArchive().getCustomer(username);
inReq.putSessionValue("user", user);
}
else
{
inReq.putPageValue("authenticationstatus", "failed");
String redirecturl = inReq.getPageProperty("registerpage");
cart.setCustomer(null);
inReq.setCancelActions(true);
if (redirecturl != null)
{
inReq.forward(redirecturl);
}
return;
}
}
}
if (customer == null)
{
// Brand new user
if (password == null)
{
customer = store.getCustomerArchive().createNewCustomer(null, null);
}
else
{
customer = store.getCustomerArchive().createNewCustomer(null, password);
}
log.info("Created new Customer");
customer.getUser().setEnabled(true);
if (email != null)
{
customer.setEmail(email);
}
inReq.putSessionValue("user", customer.getUser());
}
// final sanity check
if (firstname != null)
{
customer.setFirstName(firstname);
}
if (lastname != null)
{
customer.setLastName(lastname);
}
customer.setPaymentMethod(null);// make sure payment method has been reset
cart.setCustomer(customer);
inReq.putPageValue("user", customer.getUser());
inReq.putPageValue("customer", customer);
}
public void updateCustomer(WebPageRequest inReq) throws Exception
{
Cart cart = getCart(inReq);
Customer customer = cart.getCustomer();
Store store = getStore(inReq);
// Page one stuff
String email = inReq.getRequestParameter("email");
if (email != null)
{
customer.setEmail(email);
String firstName = inReq.getRequestParameter("firstName");
customer.setFirstName(firstName);
String lastName = inReq.getRequestParameter("lastName");
customer.setLastName(lastName);
String company = inReq.getRequestParameter("company");
if (company != null)
{
customer.setCompany(company);
}
else
{
customer.setCompany(customer.getFirstName() + " " + customer.getLastName());
}
customer.setAllowEmail(Boolean.valueOf(inReq.getRequestParameter("allowEmail")).booleanValue());
customer.setPhone1(inReq.getRequestParameter("phone1"));
customer.setFax(inReq.getRequestParameter("fax"));
// TODO: Remove these, replace with list that the usermanager uses
// to display data
customer.setUserField1(inReq.getRequestParameter("userfield1"));
customer.setUserField2(inReq.getRequestParameter("userfield2"));
if (customer.getReferenceNumber() == null)
{
customer.setReferenceNumber(inReq.getRequestParameter("referenceNumber"));
}
}
if (inReq.getRequestParameter("billing.address1.value") != null && !inReq.getRequestParameter("billing.address1.value").isEmpty())
{
populateCustomerAddress(inReq, customer.getBillingAddress());
log.info("billing address " + customer.getBillingAddress());
cart.setBillingAddress(customer.getBillingAddress());
}
if (inReq.getRequestParameter("shipping.address1.value") != null && !inReq.getRequestParameter("shipping.address1.value").isEmpty())
{
log.info("Shipping address found, saving it");
populateCustomerAddress(inReq, customer.getShippingAddress());
log.info("Shipping address " + customer.getShippingAddress());
cart.setShippingAddress(customer.getShippingAddress());
}
Address taxRateAddress = null;
String taxrate = null;
if (customer.getShippingAddress() != null && customer.getShippingAddress().getState() != null && !customer.getShippingAddress().getState().isEmpty())
{
taxRateAddress = customer.getShippingAddress();
taxrate = taxRateAddress.getState();
}
else if (customer.getBillingAddress() != null && customer.getBillingAddress().getState() != null && !customer.getBillingAddress().getState().isEmpty())
{
taxRateAddress = customer.getBillingAddress();
taxrate = taxRateAddress.getState();
}
UserProfile profile = (UserProfile) cart.getStore().getSearcherManager().getSearcher(store.getCatalogId(), "userprofile").searchById(customer.getUser().getId());
if (profile != null)
{
if (profile.get("state_province") != null)
{
taxrate = profile.get("state_province");
}
}
if (taxrate != null)
{
List> taxrates = cart.getStore().getTaxRatesFor(taxrate);
customer.setTaxRates(taxrates);
}
if (inReq.getRequestParameter("taxExemptId") != null && !inReq.getRequestParameter("taxExemptId").isEmpty())
{
customer.setTaxExemptId(inReq.getRequestParameter("taxExemptId"));
}
UserSearcher searcher = (UserSearcher) store.getSearcherManager().getSearcher(store.getCatalogId(), "user");
String[] fields = inReq.getRequestParameters("field");
searcher.updateData(inReq, fields, customer);
log.debug("Setting cart customer to " + customer);
cart.setCustomer(customer);
cart.getStore().getCustomerArchive().saveCustomer(customer);
// cart.setShippingAddress(customer.getShippingAddress());
// cart.setBillingAddress(customer.getBillingAddress());
customer.getUser().setEnabled(true);
inReq.putPageValue("customer", customer);
}
protected void populateCustomerAddress(WebPageRequest inPageRequest, Address inAddress)
{
String inPrefix = inAddress.getPrefix();
String addressid = getAddressValue(inPageRequest, inPrefix, "id");
if (addressid != null)
{
inAddress.setId(addressid);
}
String storeName = getAddressValue(inPageRequest, inPrefix, "name");
if (storeName != null)
{
inAddress.setName(storeName);
}
inAddress.setAddress1(getAddressValue(inPageRequest, inPrefix, "address1"));
inAddress.setAddress2(getAddressValue(inPageRequest, inPrefix, "address2"));
inAddress.setCity(getAddressValue(inPageRequest, inPrefix, "city"));
String state = getAddressValue(inPageRequest, inPrefix, "state");
inAddress.setState(state);
inAddress.setCountry(getAddressValue(inPageRequest, inPrefix, "country"));
inAddress.setZipCode(getAddressValue(inPageRequest, inPrefix, "zipCode"));
String desc = getAddressValue(inPageRequest, inPrefix, "description");
if (desc != null)
{
inAddress.setDescription(desc);
}
}
private String getAddressValue(WebPageRequest inPageRequest, String inPrefix, String key)
{
String value = inPageRequest.getRequestParameter(inPrefix + "." + key + ".value");
if (value == null)
{
value = inPageRequest.getRequestParameter(key + ".value");
}
return value;
}
public Customer loadCustomer(WebPageRequest inPageRequest) throws Exception
{
Store store = getStore(inPageRequest);
Cart cart = getCart(inPageRequest);
if (cart.getCustomer() != null && cart.getCustomer().getUser() != null)
{
log.info("customer \"" + cart.getCustomer().getUser() + "\" already present on cart, aborting");
populateAddressList(cart.getCustomer());
inPageRequest.putPageValue("customer", cart.getCustomer());
return cart.getCustomer();
}
Customer customer = null;
String customerId = inPageRequest.getRequestParameter("customerId");
if (customerId == null)
{
User user = inPageRequest.getUser();
if (user != null)
{
customerId = user.getUserName();
}
}
if (customerId != null)
{
customer = store.getCustomerArchive().getCustomer(customerId);
if (customer == null)
{
inPageRequest.putPageValue("errorMessage", "No such customer");
}
else
{
cart.setCustomer(customer);
}
}
if (cart.getCustomer() != null)
{
populateAddressList(cart.getCustomer());
inPageRequest.putPageValue("customer", cart.getCustomer());
}
return customer;
}
public void initSelectCustomer(WebPageRequest inReq) throws OpenEditException
{
User user = inReq.getUser();
if (user == null)
{
log.info("user not signed in, cannot prepare select customer, aborting");
return;
}
Cart cart = getCart(inReq);
if (cart == null)
{
log.info("cart is null, aborting");
return;
}
Customer customer = cart.getCustomer();
if (customer == null)
{
log.info("customer has not been set on the cart, ready to select customer");
return;
}
if (customer.getId().equals(user.getId()))
{
log.info("customer is the same as the user logged in, resetting customer");
cart.setCustomer(null);
inReq.putPageValue("customer", null);
}
}
public void loadActiveUsers(WebPageRequest inReq) throws OpenEditException
{
Store store = getStore(inReq);
Searcher searcher = store.getSearcherManager().getSearcher(store.getCatalogId(), "user");
SearchQuery query = searcher.createSearchQuery();
query.addExact("enabled", "true");
query.addSortBy("id");
HitTracker activeaccounts = searcher.search(query);
inReq.putPageValue("accounts", activeaccounts);
}
public void selectCustomer(WebPageRequest inReq) throws OpenEditException
{
Cart cart = getCart(inReq);
String customerId = inReq.getRequestParameter("customerId");
if (customerId != null)
{
Store store = getStore(inReq);
Customer customer = store.getCustomerArchive().getCustomer(customerId);
if (customer == null)
{
inReq.putPageValue("errorMessage", "No such customer");
}
else
{
cart.setCustomer(customer);
}
if (cart.getCustomer() != null)
{
populateAddressList(cart.getCustomer());
inReq.putPageValue("customer", cart.getCustomer());
}
}
}
private void populateAddressList(Customer customer)
{
User user = customer.getUser();
String alist = (String) user.get("addresslist");
customer.getAddressList().clear();
if (alist != null && !alist.equals(""))
{
String[] current = alist.split(",");
for (int i = 0; i < current.length; i++)
{
String prefix = current[i];
Address address = new Address();
address.setPrefix(prefix);
address.setAddress1((String) user.get(prefix + "Address1"));
address.setAddress2((String) user.get(prefix + "Address2"));
address.setCity((String) user.get(prefix + "City"));
address.setCountry((String) user.get(prefix + "Country"));
address.setState((String) user.get(prefix + "State"));
address.setZipCode((String) user.get(prefix + "ZipCode"));
customer.addAddress(address);
}
}
}
public void saveAddress(WebPageRequest inPageRequest) throws OpenEditException
{
String prefix = inPageRequest.getRequestParameter("prefix");
if (prefix == null)
{
return;
}
Cart cart = getCart(inPageRequest);
Customer customer = cart.getCustomer();
Address address = new Address();
address.setPrefix(prefix);
populateCustomerAddress(inPageRequest, address);
customer.addAddress(address);
updateAddresses(customer);
getStore(inPageRequest).saveCustomer(customer);
}
public void removeAddress(WebPageRequest inPageRequest) throws OpenEditException
{
String prefix = getAddressPrefix(inPageRequest);
Cart cart = getCart(inPageRequest);
Customer customer = cart.getCustomer();
Address toRemove = customer.getAddress(prefix);
if (toRemove != null)
{
log.debug("Removing address with prefix: " + toRemove.getPrefix());
customer.removeAddress(toRemove);
String[] fields = new String[] { "Address1", "Address2", "City", "State", "ZipCode", "Country" };
for (int i = 0; i < fields.length; i++)
{
customer.getUser().setValue(prefix + fields[i], null);
}
updateAddresses(customer);
getStore(inPageRequest).saveCustomer(customer);
}
}
protected String getAddressPrefix(WebPageRequest inPageRequest)
{
String prefix = inPageRequest.getRequestParameter("prefix");
if (prefix == null)
{
prefix = "";
}
return prefix;
}
private void updateAddresses(Customer customer)
{
List addresslist = customer.getAddressList();
StringBuffer buff = new StringBuffer();
for (Iterator iterator = addresslist.iterator(); iterator.hasNext();)
{
Address add = (Address) iterator.next();
buff.append(add.getPrefix());
if (iterator.hasNext())
{
buff.append(",");
}
}
String list = buff.toString();
if (list.length() == 0)
{
customer.getUser().setValue("addresslist", null);
}
else
{
customer.getUser().setValue("addresslist", buff.toString());
}
}
public void updatePaymentAdjustments(WebPageRequest inReq) throws OpenEditException
{
Cart cart = getCart(inReq);
String[] products = inReq.getRequestParameters("products");
if (products != null && products.length != 0)
{
for (String product : products)
{
String frequency = inReq.getRequestParameter(product + ".frequency");
String occurrences = inReq.getRequestParameter(product + ".occurrences");
if (frequency != null && occurrences != null)
{
cart.updatePartialPayment(product, frequency, occurrences);
}
}
}
}
public void saveCreditPaymentMethodData(WebPageRequest inReq) throws OpenEditException
{
Cart cart = getCart(inReq);
// check for billing info
if (inReq.getRequestParameter("billing.address1.value") != null)
{
populateCustomerAddress(inReq, cart.getCustomer().getBillingAddress());
}
CreditPaymentMethod method = null;
// PO
String purchaseorder = inReq.getRequestParameter("purchaseorder");
if (purchaseorder != null && purchaseorder.trim().length() > 0)
{
PurchaseOrderMethod po = new PurchaseOrderMethod();
po.setPoNumber(purchaseorder);
method = po;
}
else
{
method = new CreditPaymentMethod();
}
// Card type
String cardType = inReq.getRequestParameter("cardType");
if (cardType != null && !cardType.isEmpty())
{
method.setCreditCardType(getStore(inReq).getCreditCardType(cardType));
}
// Card number
String cardNumber = inReq.getRequestParameter("cardNumber");
if (!CreditPaymentMethod.isMasked(cardNumber))
{
method.setCardNumber(cardNumber);
}
String cardVerificationCode = inReq.getRequestParameter("cardVerificationCode");
if (!CreditPaymentMethod.isMasked(cardVerificationCode))
{
method.setCardVerificationCode(cardVerificationCode);
}
// Expiration month
String expirationMonth = inReq.getRequestParameter("expirationMonth");
if (expirationMonth != null && !expirationMonth.trim().equals(""))
{
method.setExpirationMonth(Integer.valueOf(expirationMonth).intValue());
}
// Expiration year
String expirationYear = inReq.getRequestParameter("expirationYear");
if (expirationYear != null && !expirationYear.trim().equals(""))
{
method.setExpirationYear(Integer.valueOf(expirationYear).intValue());
}
// cardholder name
String cardholdername = inReq.getRequestParameter("cardholdername");
method.setCardHolderName(cardholdername == null || cardholdername.isEmpty() ? "" : cardholdername);
// note
String note = inReq.getRequestParameter("ordernote");
if (note != null)
{
method.setNote(note);
}
String bill = inReq.getRequestParameter("billmelater");
boolean billMeLater = (bill != null && bill.equalsIgnoreCase("true"));
method.setBillMeLater(billMeLater);
// Save payment method
cart.getCustomer().setPaymentMethod(method);
}
public void autoSelectShipping(WebPageRequest inPageRequest) throws OpenEditException
{
Cart cart = getCart(inPageRequest);
if (!cart.hasZeroSubTotal())
{
List availableMethods = cart.getAvailableShippingMethods();
Store store = getStore(inPageRequest);
if (availableMethods.size() == 1)
{
cart.setShippingMethod((ShippingMethod) availableMethods.get(0));
inPageRequest.redirect(cart.getStore().getStoreHome() + "/checkout2.html");
}
else if (availableMethods.size() > 1 && store.isAssignShippingMethod())
{
cart.setShippingMethod((ShippingMethod) availableMethods.get(0));
inPageRequest.redirect(cart.getStore().getStoreHome() + "/checkout2.html");
}
}
}
public void saveShippingMethod(WebPageRequest inPageRequest) throws OpenEditException
{
String method = inPageRequest.getRequestParameter("shippingmethod");
Cart cart = getCart(inPageRequest);
Store store = getStore(inPageRequest);
if (method != null)
{
ShippingMethod smethod = store.findShippingMethod(method);
cart.setShippingMethod(smethod);
}
else if (cart.getShippingMethod() == null && store.isAssignShippingMethod())
{
List availableMethods = cart.getAvailableShippingMethods();
if (availableMethods.size() > 0)
{
cart.setShippingMethod((ShippingMethod) availableMethods.get(0));
}
}
}
public void forceStripeOrCode(WebPageRequest inReq)
{
Data code = (Data) inReq.getSessionValue("coupon");
if (code == null)
{
String stripetoken = inReq.getRequestParameter("stripeToken");
if (stripetoken == null)
{
inReq.setCancelActions(true);
inReq.putPageValue("error", "notokenorcode");
}
}
}
public synchronized Order processOrder(WebPageRequest inReq) throws Exception
{
Store store = getStore(inReq);
Cart cart = getCart(inReq);
if (cart.isEmpty())
{
inReq.putPageValue("orderState", "Your cart is empty");
return null;
}
Order order = store.getOrderGenerator().createNewOrder(store, cart);
OrderState orderState = order.getOrderStatus();
cart.setCurrentOrder(order);
if (cart.isEmpty())
{
orderState.setOk(false);
orderState.setDescription("Error: Cart is empty.
");
return order;
}
// update gateway if needed
if (cart.get("gateway") != null)
{
order.setProperty("gateway", cart.get("gateway"));
cart.setProperty("gateway", null);
}
// check forcetestmode
if (cart.get("forcetestmode") != null)
{
order.setProperty("forcetestmode", cart.get("forcetestmode"));
}
String applicationid = inReq.findValue("applicationid");
order.setProperty("applicationid", applicationid);
// Assign default shipping method if one has not been assigned
if (cart.getShippingMethod() == null)
{
List shippingMethods = cart.getAvailableShippingMethods();
if (shippingMethods.size() > 0)
{
cart.setShippingMethod((ShippingMethod) shippingMethods.get(0));
}
}
order.copyAdjustments(cart);
order.setShippingAddress(cart.getShippingAddress());
order.setBillingAddress(cart.getBillingAddress());
// log.info("#### ADJUSTMENTS 1: "+order.getAdjustments());
// Export order to XML
store.saveOrder(order);
// append this to order history, opened
inReq.putPageValue("orderhistorystate", "opened");
appendToOrderHistory(inReq);
if (cart.getShippingAddress() != null && cart.getShippingAddress().getId() == null)
{
Searcher addressSearcher = getSearcherManager().getSearcher(store.getCatalogId(), "address");
Address target = cart.getShippingAddress();
target.setId(addressSearcher.nextId());
target.setProperty("userprofile", cart.getCustomer().getId());
if (inReq.getUserProfile().get("dealer") != null)
{
target.setProperty("dealer", inReq.getUserProfileValue("dealer"));
}
addressSearcher.saveData(target, inReq.getUser());
}
else
{
Searcher addressSearcher = getSearcherManager().getSearcher(store.getCatalogId(), "address");
Address target = cart.getShippingAddress();
if (target != null)
{
if (target.getName() == null)
{
String inPrefix = target.getPrefix();
String storeName = getAddressValue(inReq, inPrefix, "name");
if (storeName != null)
{
target.setName(storeName);
}
}
target.setProperty("userprofile", cart.getCustomer().getId());
addressSearcher.saveData(target, inReq.getUser());
}
}
// process the order with the varous processors
inReq.putPageValue("order", order);
store.processOrder(inReq, order);
store.saveOrder(order);
if (order.getOrderStatus().isOk())
{
// Remove items from stock
for (Iterator iter = cart.getItemIterator(); iter.hasNext();)
{
CartItem cartItem = (CartItem) iter.next();
InventoryItem inventoryItem = cartItem.getInventoryItem();
if (inventoryItem != null)
{
inventoryItem.decreaseQuantityInStock(cartItem.getQuantity());
}
}
// Save updated product inventory values to disk
for (Iterator iter = cart.getItemIterator(); iter.hasNext();)
{
CartItem cartItem = (CartItem) iter.next();
Product product = cartItem.getProduct();
store.getProductArchive().saveProduct(product);
}
// Order succeeded - remove cart
cart.removeAllItems();
cart.getAdjustments().clear();
cart.setCoupons(null);
store.saveOrder(order);
}
// export order again after state change
// log.info("#### ADJUSTMENTS 2: "+order.getAdjustments());
// append this to order history, opened
inReq.putPageValue("orderhistorystate", "orderplaced");
appendToOrderHistory(inReq);
// trigger webevent after the order has been saved
if (order.getOrderStatus().isOk())
{
WebEvent event = new WebEvent();
event.setSearchType("order");
event.setCatalogId(getMediaArchive(inReq).getCatalogId());
event.setOperation("orderprocessed");
event.setUser(inReq.getUser());
event.setProperty("orderid", order.getId());
getMediaArchive(inReq).getEventManager().fireEvent(event);
}
return order;
}
public void archiveOrders(WebPageRequest inPageRequest) throws Exception
{
Store store = getStore(inPageRequest);
OrderArchive orderArchive = store.getOrderArchive();
orderArchive.archiveOrderData(store);
}
public void convertData(WebPageRequest inPageRequest) throws Exception
{
// loop over all the converters and let them convert what they find
// clear out the reader so its all reloaded
Store store = getStore(inPageRequest);
boolean forced = Boolean.parseBoolean(inPageRequest.getRequestParameter("forced"));
ConvertStatus errorlog = store.convertCatalog(inPageRequest.getUser(), forced);
if (inPageRequest != null)
{
inPageRequest.removeSessionValue("store");
inPageRequest.putPageValue("exception-report", errorlog.getLog());
inPageRequest.putPageValue("status", errorlog);
}
}
/**
*
*/
public void reIndexStore(WebPageRequest inPageRequest) throws Exception
{
Store store = getStore(inPageRequest);
store.reindexAll();
store.getCategoryArchive().reloadCategories();
}
public void loadCustomerAddressList(WebPageRequest inReq)
{
// if (inReq.getUser() == null) {
// return;
// }
// Store store = getStore(inReq);
// if (inReq.getUser() == null || inReq.getUserProfile().getId() == null) {
// return;
// }
// Searcher addressSearcher = store.getSearcherManager().getSearcher(
// store.getCatalogId(), "address");
//
// HitTracker addresslist;
// String inDealer = inReq.getUserProfile().get("dealer");
// if (inDealer != null) {
// addresslist = addressSearcher.fieldSearch("dealer", inDealer);
// } else {
// addresslist = addressSearcher.fieldSearch("userprofile", inReq.getUserProfile().getId());
// }
// if (addresslist != null && addresslist.size() > 0) {
// inReq.putPageValue("addresslist", addresslist);
// } else {
// Data userAddress = (Data) addressSearcher.searchById(inReq.getUser().getId());
// if (userAddress != null) {
// List addr = new ArrayList();
// addr.add(userAddress);
// inReq.putPageValue("addresslist", addr);
// } else {
// inReq.putPageValue("addresslist", null);
// }
// }
if (inReq.getUser() == null)
{
return;
}
Store store = getStore(inReq);
if (inReq.getUser() == null || inReq.getUserProfile().getId() == null)
{
return;
}
Searcher addressSearcher = store.getSearcherManager().getSearcher(store.getCatalogId(), "address");
HitTracker addresslist;
String inDealer = inReq.getUserProfile().get("dealer");
if (inDealer != null)
{
SearchQuery query = addressSearcher.createSearchQuery();
query.addMatches("dealer", inDealer);
addresslist = addressSearcher.cachedSearch(inReq, query);
}
else
{
String userid = inReq.getUserProfile().getId();
Cart cart = getCart(inReq);
if (cart != null)
{
Customer customer = cart.getCustomer();
if (customer != null)
{
userid = customer.getId();
}
}
addresslist = addressSearcher.fieldSearch("userprofile", userid);
}
inReq.putPageValue("addresslist", addresslist);
}
public void selectShippingAddress(WebPageRequest inReq)
{
Store store = getStore(inReq);
Searcher addressSearcher = store.getSearcherManager().getSearcher(store.getCatalogId(), "address");
String addressid = inReq.getRequestParameter("address.value");
Address address = (Address) addressSearcher.searchById(addressid);
getCart(inReq).setShippingAddress(address);
}
public void createCartFromOrder(WebPageRequest inReq) throws Exception
{
clearCart(inReq);
String orderid = inReq.getRequestParameter("orderid");
Store store = getStore(inReq);
Order order = (SubmittedOrder) store.getOrderSearcher().searchById(orderid);
Cart cart = order.getCart();
inReq.putSessionValue(store.getCatalogId() + "cart", cart);
cart.setStore(store);
inReq.putPageValue("cart", cart);
}
public void saveWishList(WebPageRequest inReq) throws Exception
{
Cart cart = getCart(inReq);
Store store = getStore(inReq);
Searcher wishlistsearcher = store.getSearcherManager().getSearcher(store.getCatalogId(), "wishlist");
Searcher wishitems = store.getSearcherManager().getSearcher(store.getCatalogId(), "wishlistitems");
UserProfile profile = inReq.getUserProfile();
String parentid = profile.get("parentprofile");
User user = inReq.getUser();
Data wishlist = wishlistsearcher.createNewData();
wishlist.setProperty("userid", user.getId());
wishlist.setProperty("creationdate", DateStorageUtil.getStorageUtil().formatForStorage(new Date()));
wishlist.setProperty("profileid", profile.getId());
wishlist.setProperty("dealer", profile.get("dealer"));
wishlist.setProperty("address", profile.get("address"));
wishlist.setProperty("store", profile.get("store"));
wishlist.setId(wishlistsearcher.nextId());
wishlist.setProperty("name", "WISHLIST" + wishlist.getId());
String[] fields = inReq.getRequestParameters("field");
wishlistsearcher.updateData(inReq, fields, wishlist);
wishlist.setProperty("wishstatus", "pending");
wishlistsearcher.saveData(wishlist, user);
for (Iterator iterator = cart.getItems().iterator(); iterator.hasNext();)
{
CartItem item = (CartItem) iterator.next();
Data listitem = wishitems.createNewData();
listitem.setId(wishitems.nextId());
listitem.setProperty("product", item.getProduct().getId());
listitem.setProperty("quantity", String.valueOf(item.getQuantity()));
listitem.setProperty("wishlist", wishlist.getId());
wishitems.saveData(listitem, inReq.getUser());
}
}
public Cart loadWishList(WebPageRequest inReq) throws Exception
{
Store store = getStore(inReq);
clearCart(inReq);
Cart cart = getCart(inReq);
String[] listids = inReq.getRequestParameters("listid");
Searcher wishlistsearcher = store.getSearcherManager().getSearcher(store.getCatalogId(), "wishlist");
Searcher wishitems = store.getSearcherManager().getSearcher(store.getCatalogId(), "wishlistitems");
for (int i = 0; i < listids.length; i++)
{
String listid = listids[i];
cart.setProperty("wishlist", listid);
HitTracker items = wishitems.fieldSearch("wishlist", listid);
for (Iterator iterator = items.iterator(); iterator.hasNext();)
{
Data item = (Data) iterator.next();
Product product = store.getProduct(item.get("product"));
if (product != null)
{
CartItem cartitem = new CartItem();
cartitem.setProduct(product);
cartitem.setQuantity(Double.parseDouble(item.get("quantity")));
cart.addItem(cartitem);
}
}
}
return cart;
}
public void addItemBySku(WebPageRequest inReq) throws Exception
{
Store store = getStore(inReq);
clearCart(inReq);
Cart cart = getCart(inReq);
String sku = inReq.getRequestParameter("sku");
Product p = getProduct(inReq);
if (sku != null)
{
InventoryItem i = p.getInventoryItemBySku(sku);
CartItem item = new CartItem();
item.setInventoryItem(i);
item.setQuantity(1);
cart.addItem(item);
}
}
protected void appendToOrderHistory(WebPageRequest inReq)
{
Cart cart = getCart(inReq);
Order order = cart == null ? null : cart.getCurrentOrder();
if (order == null)
{
log.info("Error! Unable to load order, cannot append order history, skipping");
return;
}
if (inReq.getPageValue("orderhistorystate") == null)
{
log.info("Error! Unable to load orderhistorystate from webpagecontext, cannot append order history, skipping");
return;
}
String state = inReq.getPageValue("orderhistorystate").toString();
MediaArchive archive = (MediaArchive) inReq.getPageValue("mediaarchive");
if (archive == null)
{
log.info("Error! Unable to load mediaarchive from webpagecontext, cannot append order history, skipping");
return;
}
WebEvent evt = new WebEvent();
evt.setSearchType("detailedorderhistory");// productupdates
evt.setCatalogId(archive.getCatalogId());
evt.setProperty("applicationid", inReq.findValue("applicationid"));
evt.setOperation("appendorderhistory");// products/
evt.setProperty("orderid", order.getId());
evt.setProperty("type", "automatic");
evt.setProperty("state", state);
archive.getEventManager().fireEvent(evt);
}
public void changeMode(WebPageRequest inReq) throws Exception
{
Cart cart = getCart(inReq);
if (!"administrator".equals(inReq.getUserProfileValue("settingsgroup")))
{
// if not administrator, ignore command but also make sure forcetestmode is
// false
if (cart.getBoolean("forcetestmode"))
{
cart.setProperty("forcetestmode", "false");
}
log.info("ignoring request to change mode of order, user is not an administrator");
return;
}
String mode = inReq.getRequestParameter("mode");
if (mode != null)
{
cart.setProperty("forcetestmode", Boolean.parseBoolean(mode) ? "true" : "false");
log.info("changed forcetestmode to " + Boolean.parseBoolean(mode));
}
else
{
log.info("did not modify forcetestmode, mode is null");
}
}
public void changeGateway(WebPageRequest inReq) throws Exception
{
Cart cart = getCart(inReq);
if (!"administrator".equals(inReq.getUserProfileValue("settingsgroup")))
{
// if not administrator, ignore command but also make sure forcetestmode is
// false
log.info("ignoring request to change gateway of order, user is not an administrator");
return;
}
String gateway = inReq.getRequestParameter("gateway");
if (gateway != null)
{
cart.setProperty("gateway", gateway);
log.info("changed gateway of cart to " + gateway);
}
else
{
log.info("did not modify gateway, request parameter is null");
}
}
public synchronized void processDonation(WebPageRequest inReq) throws Exception
{
Cart cart = getCart(inReq);
cart.removeAllItems();
Store store = getStore(inReq);
String donationamount = inReq.findValue("amount");
// CartItem item = new CartItem();
Money money = new Money(donationamount);
// item.setYourPrice(money);
// cart.addItem(item);
Order order = store.getOrderGenerator().createNewOrder(store, cart);
order.setTotalPrice(money);
OrderState orderState = order.getOrderStatus();
cart.setCurrentOrder(order);
if (cart.get("gateway") != null)
{
order.setProperty("gateway", cart.get("gateway"));
cart.setProperty("gateway", null);
}
// check forcetestmode
if (cart.get("forcetestmode") != null)
{
order.setProperty("forcetestmode", cart.get("forcetestmode"));
}
String applicationid = inReq.findValue("applicationid");
order.setProperty("applicationid", applicationid);
store.saveOrder(order);
// append this to order history, opened
inReq.putPageValue("order", order);
store.processOrder(inReq, order);
store.saveOrder(order);
// handle saving recurring payments
clearCart(inReq);
}
public void loadAuctionStatus(WebPageRequest inReq)
{
Store store = getStore(inReq);
Product p = (Product) inReq.getPageValue("product");
if (p == null)
{
String productid = inReq.findValue("productid");
if (productid == null)
{
return;
}
p = store.getProduct(productid);
}
if(p == null) {
return;
}
inReq.putPageValue("product", p);
boolean ended = false;
boolean started = false;
Date end = p.getDate("silentauctionend");
if (end != null && end.before(new Date()))
{
inReq.putPageValue("auctionended", true);
ended = true;
}
Date start = p.getDate("silentauctionstart");
if (start != null && start.after(new Date()))
{
inReq.putPageValue("auctionnotstarted", true);
} else {
started = true;
}
String currenthigh = p.get("currenthighbidder");
if(currenthigh != null && currenthigh.equals(inReq.getUserName())) {
inReq.putPageValue("winningbid", true);
if(ended) {
inReq.putPageValue("canpurchaseauction",true);
}
}
}
public synchronized void handleAuctionBid(WebPageRequest inReq)
{
Store store = getStore(inReq);
String productid = inReq.findValue("productid");
if (productid == null)
{
return;
}
Product p = store.getProduct(productid);
inReq.putPageValue("product", p);
if (!"silentauction".equals(p.getValue("productpagetype")))
{
throw new OpenEditException("Cannot bid on non silent auction items!!");
}
String newbid = inReq.findValue("newbid");
if (newbid == null)
{
return;//nothing to do.
}
Date end = p.getDate("silentauctionend");
if (end != null && end.before(new Date()))
{
inReq.putPageValue("auctionended", end);
return;
}
try
{
Money bid = new Money(newbid);
Money currentprice = p.getYourPrice();
Money increment = null;
if (p.getDouble("bidincrement") != null)
{
increment = new Money(p.getDouble("bidincrement"));
}
if (increment != null)
{
double increase = bid.doubleValue() - currentprice.doubleValue();
if (increment.doubleValue() >= increase+.01)
{
inReq.putPageValue("notenough", true);
return;
}
}
if (bid.doubleValue() >= currentprice.doubleValue() )
{
log.info("New high bid placed by user" + inReq.getUserName());
Searcher bidsearcher = store.getSearcherManager().getSearcher(store.getCatalogId(), "productbid");
Data winning = bidsearcher.createNewData();
winning.setValue("userid", inReq.getUser());
winning.setValue("productid", p.getId());
winning.setValue("date", new Date());
winning.setValue("bidamount", bid.doubleValue());
bidsearcher.saveData(winning);
inReq.putPageValue("winningbid", winning);
PriceSupport ps = new PriceSupport();
Price point = new Price();
point.setRetailPrice(bid);
ps.addTierPrice(1, point);
p.setPriceSupport(ps);
p.setValue("currenthighbidder", inReq.getUserName());
store.saveProduct(p);
}
else
{
inReq.putPageValue("outbid", true);
}
}
catch (Exception e)
{
inReq.putPageValue("invalidinput", newbid);
return;
}
}
}