@RequestMapping("/spec.html") public ModelAndView showProductSpec(@RequestParam("name") String name, ModelAndView mv) { Product product = new Product(); product.setName(name); BillOfMaterials bom = productsService.getProductBillOfMaterials(product); mv.addObject("bom", bom); mv.setViewName("productsSpec"); return mv; }
/** * Handle form with new product details TODO: form validation * * @param productName * @param productPrice * @param mv * @return */ @RequestMapping(value = "/add.html", method = RequestMethod.POST) public ModelAndView addProductsSave( @RequestParam("productName") String productName, @RequestParam("productPrice") Double productPrice, ModelAndView mv) { Product newProduct = new Product(); newProduct.setName(productName); newProduct.setPrice(productPrice); try { productsService.createProduct(newProduct); mv.addObject("product", newProduct); mv.setViewName("productsAddSuccessful"); } catch (Exception e) { mv.addObject("product", newProduct); mv.addObject("error", e.getLocalizedMessage()); mv.setViewName("productsAddFailed"); } return mv; }
/** * Show components selection page TODO: form validation * * @param name * @param mv * @return */ @RequestMapping(value = "/components.html", method = RequestMethod.GET) public ModelAndView componentsManageView(@RequestParam("name") String name, ModelAndView mv) { Product managedProduct = new Product(); managedProduct.setName(name); Collection<Component> managedProductComponents = productsService.getProductComponents(managedProduct); Map<String, Integer> componentsAmountMap = new HashMap<String, Integer>(); for (Component component : managedProductComponents) { componentsAmountMap.put(component.getComponent().getName(), component.getAmount()); } // According to code allProducts instance is used only here Collection<Product> allProducts = productsService.getAllProducts(); ProductComponents productComponents = new ProductComponents(); productComponents.setProductName(name); // Iterate through available products end initialise amount for those which are already // components of chosen product List<Component> componentsList = productComponents.getComponents(); for (Product product : allProducts) { Component component = new Component(); component.setComponent(product); Integer amount = componentsAmountMap.get(product.getName()); if (amount != null) { component.setAmount(amount); } else { component.setAmount(0); } componentsList.add(component); } // Product name mv.addObject("name", name); mv.addObject("componentsCommand", productComponents); mv.setViewName("componentsManage"); return mv; }