public void setCompanyToDto(String companyIdString, ComputerDTO computerDTO) { long companyId = UsefulFunctions.stringToLong(companyIdString, 0); Company company = companyServices.getCompany(companyId); String companyName = null; if (company != null) { companyName = company.getName(); } computerDTO.setCompanyId(companyId); computerDTO.setCompanyName(companyName); }
@RequestMapping(value = "AddComputer", method = RequestMethod.POST) public ModelAndView postAddComputer( @ModelAttribute("dto") @Valid ComputerDTO computerDTO, BindingResult result, @RequestParam("company") String companyIdString) throws ServletException, IOException { setCompanyToDto(companyIdString, computerDTO); ModelAndView mav = new ModelAndView(); long companyId = UsefulFunctions.stringToLong(companyIdString, 0); Company company = null; if (companyId != 0) { company = companyServices.getCompany(companyId); } /* Validation case */ if (!result.hasErrors()) { Computer neoComputer = computerMapper.dtoToObject(computerDTO, company); long id = computerServices.insert(neoComputer); if (id >= 0) { mav.getModelMap().addAttribute("message", "add"); mav.getModelMap().addAttribute("computerIdMessage", id); mav.setViewName("redirect:DashBoard"); } /* Error case */ } else { allCompany = companyServices.getAllCompanies(); mav.getModelMap().addAttribute("allCompany", allCompany); mav.getModelMap().addAttribute("error", result.hasErrors()); mav.setViewName("addComputer"); } return mav; }