Guest User

Untitled

a guest
Mar 19th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. @Autowired
  2. private SessionFactory sessionFactory;
  3.  
  4. @Override
  5. public Boolean save(ProductInfo productInfo) {
  6. Boolean flag = null;
  7. if(productInfo!=null) {
  8. Long id = productInfo.getid();
  9. Product product = null;
  10. boolean isNew = false;
  11. if (id != null) {
  12. product = this.get(id);
  13. }
  14. if (product == null) {
  15. isNew = true;
  16. product = new Product();
  17. }
  18. product.setName(productInfo.getName());
  19. product.setPrice(productInfo.getPrice());
  20. product.setBrand(productInfo.getBrand());
  21. product.setCategory(productInfo.getCategory());
  22.  
  23. if (productInfo.getFileData() != null) {
  24. byte[] image = productInfo.getFileData().getBytes();
  25. if (image != null && image.length > 0) {
  26. product.setImage(image);
  27. }
  28. }
  29. if (isNew) {
  30. this.sessionFactory.getCurrentSession().persist(product);
  31. }
  32.  
  33. this.sessionFactory.getCurrentSession().flush();
  34. return Boolean.TRUE;
  35. }else {
  36. return Boolean.FALSE;
  37. }
  38. }
  39.  
  40. @Autowired
  41. private ProductDao productDao;
  42.  
  43.  
  44. public Boolean save(ProductInfo productInfo) {
  45. return productDao.save(productInfo);
  46. }
  47.  
  48. @Autowired
  49. private ProductDao productDAO;
  50.  
  51. @Autowired
  52. private ProductService productservice;
  53.  
  54. @Autowired
  55. private ProductInfoValidator productInfoValidator;
  56.  
  57. @InitBinder
  58. public void myInitBinder(WebDataBinder dataBinder) {
  59. Object target = dataBinder.getTarget();
  60. if (target == null) {
  61. return;
  62. }
  63. System.out.println("Target=" + target);
  64.  
  65. if (target.getClass() == ProductInfo.class) {
  66. dataBinder.setValidator(productInfoValidator);
  67.  
  68. dataBinder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
  69. }
  70. }
  71.  
  72.  
  73. @RequestMapping(value = { "/createproduct" }, method = RequestMethod.GET)
  74. public String product(Model model, @RequestParam(value = "id", defaultValue = "") Long id) {
  75.  
  76. ProductInfo productInfo = null;
  77.  
  78. if (id != null ) {
  79. productInfo = this.productservice.findProductInfo(id);
  80. }
  81. if (productInfo == null) {
  82. productInfo = new ProductInfo();
  83. productInfo.setNewProduct(true);
  84. }
  85. model.addAttribute("productForm", productInfo);
  86. return "createproduct";
  87. }
  88.  
  89.  
  90. @RequestMapping(value = { "/createproduct" }, method = RequestMethod.POST)
  91. @Transactional
  92. public String productSave(Model model,
  93. @ModelAttribute("productForm") @Validated ProductInfo productInfo,
  94. BindingResult result,
  95. final RedirectAttributes redirectAttributes) {
  96.  
  97. if (result.hasErrors()) {
  98. return "createproduct";
  99. }
  100. try {
  101. this.productservice.save(productInfo);
  102. } catch (Exception e) {
  103.  
  104. String message = e.getMessage();
  105. model.addAttribute("message", message);
  106.  
  107. return "createproduct";
  108.  
  109. }
  110. return "redirect:/productList";
  111. }
Add Comment
Please, Sign In to add comment