use of com.salesmanager.core.model.shoppingcart.ShoppingCart in project shopizer by shopizer-ecommerce.
the class CustomerLoginController method logon.
private AjaxResponse logon(String userName, String password, String storeCode, HttpServletRequest request, HttpServletResponse response) throws Exception {
AjaxResponse jsonObject = new AjaxResponse();
try {
LOG.debug("Authenticating user " + userName);
// user goes to shop filter first so store and language are set
MerchantStore store = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
Language language = (Language) request.getAttribute("LANGUAGE");
// check if username is from the appropriate store
Customer customerModel = customerFacade.getCustomerByUserName(userName, store);
if (customerModel == null) {
jsonObject.setStatus(AjaxResponse.RESPONSE_STATUS_FAIURE);
return jsonObject;
}
if (!customerModel.getMerchantStore().getCode().equals(storeCode)) {
jsonObject.setStatus(AjaxResponse.RESPONSE_STATUS_FAIURE);
return jsonObject;
}
customerFacade.authenticate(customerModel, userName, password);
// set customer in the http session
super.setSessionAttribute(Constants.CUSTOMER, customerModel, request);
jsonObject.setStatus(AjaxResponse.RESPONSE_STATUS_SUCCESS);
jsonObject.addEntry(Constants.RESPONSE_KEY_USERNAME, customerModel.getNick());
LOG.info("Fetching and merging Shopping Cart data");
String sessionShoppingCartCode = (String) request.getSession().getAttribute(Constants.SHOPPING_CART);
if (!StringUtils.isBlank(sessionShoppingCartCode)) {
ShoppingCart shoppingCart = customerFacade.mergeCart(customerModel, sessionShoppingCartCode, store, language);
if (shoppingCart != null) {
ShoppingCartData shoppingCartData = this.populateShoppingCartData(shoppingCart, store, language);
if (shoppingCartData != null) {
jsonObject.addEntry(Constants.SHOPPING_CART, shoppingCartData.getCode());
request.getSession().setAttribute(Constants.SHOPPING_CART, shoppingCartData.getCode());
// set cart in the cookie
Cookie c = new Cookie(Constants.COOKIE_NAME_CART, shoppingCartData.getCode());
c.setMaxAge(60 * 24 * 3600);
c.setPath(Constants.SLASH);
response.addCookie(c);
} else {
// DELETE COOKIE
Cookie c = new Cookie(Constants.COOKIE_NAME_CART, "");
c.setMaxAge(0);
c.setPath(Constants.SLASH);
response.addCookie(c);
}
}
} else {
ShoppingCart cartModel = shoppingCartService.getShoppingCart(customerModel);
if (cartModel != null) {
jsonObject.addEntry(Constants.SHOPPING_CART, cartModel.getShoppingCartCode());
request.getSession().setAttribute(Constants.SHOPPING_CART, cartModel.getShoppingCartCode());
Cookie c = new Cookie(Constants.COOKIE_NAME_CART, cartModel.getShoppingCartCode());
c.setMaxAge(60 * 24 * 3600);
c.setPath(Constants.SLASH);
response.addCookie(c);
}
}
StringBuilder cookieValue = new StringBuilder();
cookieValue.append(store.getCode()).append("_").append(customerModel.getNick());
// set username in the cookie
Cookie c = new Cookie(Constants.COOKIE_NAME_USER, cookieValue.toString());
c.setMaxAge(60 * 24 * 3600);
c.setPath(Constants.SLASH);
response.addCookie(c);
} catch (AuthenticationException ex) {
jsonObject.setStatus(AjaxResponse.RESPONSE_STATUS_FAIURE);
} catch (Exception e) {
jsonObject.setStatus(AjaxResponse.RESPONSE_STATUS_FAIURE);
}
return jsonObject;
}
use of com.salesmanager.core.model.shoppingcart.ShoppingCart in project shopizer by shopizer-ecommerce.
the class ShoppingCartServiceImpl method getByCode.
/**
* Get a {@link ShoppingCart} for a given code. Will update the shopping
* cart prices and items based on the actual inventory. This method will
* remove the shopping cart if no items are attached.
*/
@Override
@Transactional
public ShoppingCart getByCode(final String code, final MerchantStore store) throws ServiceException {
try {
ShoppingCart shoppingCart = shoppingCartRepository.findByCode(store.getId(), code);
if (shoppingCart == null) {
return null;
}
getPopulatedShoppingCart(shoppingCart);
if (shoppingCart.isObsolete()) {
delete(shoppingCart);
return null;
} else {
return shoppingCart;
}
} catch (javax.persistence.NoResultException nre) {
return null;
} catch (Throwable e) {
throw new ServiceException(e);
}
}
use of com.salesmanager.core.model.shoppingcart.ShoppingCart in project shopizer by shopizer-ecommerce.
the class ShoppingCartFacadeImpl method addToCart.
@Override
public ReadableShoppingCart addToCart(Customer customer, PersistableShoppingCartItem item, MerchantStore store, Language language) throws Exception {
Validate.notNull(customer, "Customer cannot be null");
Validate.notNull(customer.getId(), "Customer.id cannot be null or empty");
ShoppingCart cartModel = shoppingCartService.getShoppingCart(customer);
// if cart does not exist create a new one
if (cartModel == null) {
cartModel = new ShoppingCart();
cartModel.setCustomerId(customer.getId());
cartModel.setMerchantStore(store);
cartModel.setShoppingCartCode(uniqueShoppingCartCode());
}
return readableShoppingCart(cartModel, item, store, language);
}
use of com.salesmanager.core.model.shoppingcart.ShoppingCart in project shopizer by shopizer-ecommerce.
the class ShoppingCartFacadeImpl method createCartModel.
@Override
public ShoppingCart createCartModel(final String shoppingCartCode, final MerchantStore store, final Customer customer) throws Exception {
final Long CustomerId = customer != null ? customer.getId() : null;
ShoppingCart cartModel = new ShoppingCart();
if (StringUtils.isNotBlank(shoppingCartCode)) {
cartModel.setShoppingCartCode(shoppingCartCode);
} else {
cartModel.setShoppingCartCode(uniqueShoppingCartCode());
}
cartModel.setMerchantStore(store);
if (CustomerId != null) {
cartModel.setCustomerId(CustomerId);
}
shoppingCartService.create(cartModel);
return cartModel;
}
use of com.salesmanager.core.model.shoppingcart.ShoppingCart in project shopizer by shopizer-ecommerce.
the class ShoppingCartFacadeImpl method setOrderId.
@Override
public void setOrderId(String code, Long orderId, MerchantStore store) throws Exception {
ShoppingCart cart = this.getShoppingCartModel(code, store);
if (cart == null) {
LOG.warn("Shopping cart with code [" + code + "] not found, expected to find a cart to set order id [" + orderId + "]");
} else {
cart.setOrderId(orderId);
}
saveOrUpdateShoppingCart(cart);
}
Aggregations