public void updateStock(UUID productId, Integer stockOffset) throws EntityDoesNotExistException { Product product = this.findById(productId); product.setStock(product.getStock() + stockOffset); }
public class ProductBidRollBackListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent httpSessionEvent) { //To change body of implemented methods use File | Settings | File Templates. } @Override public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { HttpSession session=httpSessionEvent.getSession(); WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(session.getServletContext()); ProductService productService=(ProductService) context.getBean("productServiceImpl"); Cart cart=(Cart)session.getAttribute("cart"); if (cart!=null && cart.getCartItems()!=null && cart.getCartItems().size()>0){ for (int i=0; i<cart.getCartItems().size();i++){ CartItem cartItem=cart.getCartItems().get(i); if (cartItem.getProduct()!=null){ Product product = productService.getProductById(cartItem.getProduct().getId(),""); int stock=product.getStock(); product.setStock(stock+cartItem.getQuantity()); product = productService.updateProduct(product); } } } } }
@Override public void onEvent(Event event, Object source, Object data) { // Update stocks for bought products Order order = (Order) source; List<OrderItem> items = order.getOrderItems(); for (OrderItem item : items) { UUID itemId = item.getPurchasableId(); if (itemId == null) { return; } Long quantity = item.getQuantity(); try { Product product = productStore.get().findById(itemId); if (product.getStock() != null) { productStore.get().updateStock(itemId, -quantity.intValue()); } else if (product.getParentId() != null) { Product parent = productStore.get().findById(product.getParentId()); productStore.get().updateStock(product.getParentId(), -quantity.intValue()); } } catch (EntityDoesNotExistException e) { // Ignore, there is just no stock to update } } } }
public void updateStock(UUID productId, Integer stockOffset) throws EntityDoesNotExistException { this.dao.begin(); Product product = this.findById(productId); if (product == null) { this.dao.commit(); throw new EntityDoesNotExistException(); } product.setStock((product.getStock() == null ? 0 : product.getStock()) + stockOffset); Integer updatedRows = this.dao.updateProduct(product); this.dao.commit(); if (updatedRows <= 0) { throw new StoreException("No rows was updated when updating product"); } getObservationManager().notify(new EntityUpdatedEvent(), product); }