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); } } } } }
product.setStock(resultSet.getInt("stock"));
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); }