@RequestMapping(value = "/robots/card", method = RequestMethod.GET) public String cardRobot( Model model, @RequestParam(value = "id") final int id, HttpServletRequest request) { HttpSession session = request.getSession(); User user = (User) session.getAttribute("user"); Robot robot = this.robotService.findById(id); String list_technologies = ""; // liste de dates au format francais Map<Integer, String> french_dates = new HashMap<Integer, String>(); for (int i = 0; i < robot.getTechnologies().size(); i++) { list_technologies += robot.getTechnologies().get(i).getName(); if (robot.getTechnologies().size() - 1 == i) { list_technologies += "."; } else { list_technologies += ", "; } } File image = new File( "D:/Workspace/insta/jee/WorldOfRobots/src/main/webapp/resources/images/robots/" + robot.getPath_picture()); String name_file = ""; if (image.exists() && !image.isDirectory()) { name_file = image.getName(); } else { name_file = "no-image.png"; } french_dates.put( robot.getId(), new SimpleDateFormat("dd/MM/yyyy HH:mm").format(robot.getCreation_date())); Long participate_competition = this.robotService.countCompetitionByRobot(robot.getId()); Long participate_battle = this.robotService.countBattleByRobot(robot.getId()); Long win_battle = this.robotService.countWinBalttleByRobot(robot.getId()); model.addAttribute("technologies", list_technologies); model.addAttribute("name_file", name_file); model.addAttribute("participate_competition", participate_competition); model.addAttribute("participate_battle", participate_battle); model.addAttribute("win", win_battle); model.addAttribute("lose", (participate_battle - win_battle)); model.addAttribute("robot", robot); model.addAttribute("french_dates", french_dates); session.setAttribute("user", user); return "robot"; }
@RequestMapping(value = "/robots", method = RequestMethod.GET) public String Robots(Model model) { List<Robot> robots = this.robotService.findAll(); // liste de dates au format francais Map<Integer, String> french_dates = new HashMap<Integer, String>(); // slider d'images ArrayList<String> list_images = new ArrayList<String>(); for (Robot robot : robots) { // images File image = new File( "D:/Workspace/insta/jee/WorldOfRobots/src/main/webapp/resources/images/robots/" + robot.getPath_picture()); if (image.exists() && !image.isDirectory()) { list_images.add(robot.getPath_picture()); } // date création french_dates.put( robot.getId(), new SimpleDateFormat("dd/MM/yyyy HH:mm").format(robot.getCreation_date())); } if (list_images.isEmpty()) { list_images.add("no-image.png"); } model.addAttribute("list_images", list_images); model.addAttribute("dates", french_dates); model.addAttribute("robots", robots); return "robots"; }
@RequestMapping(value = "/robots/add", method = RequestMethod.POST) public String toAddRobot( Model model, @ModelAttribute("AddRobot") AddRobot addRobot, @RequestParam("image") MultipartFile file, HttpServletRequest request) { HttpSession session = request.getSession(); User user = (User) session.getAttribute("user"); if (user != null) { if (!addRobot.getImage().isEmpty()) { byte[] bytes; try { bytes = addRobot.getImage().getBytes(); String name_file = addRobot.getName() + file.getOriginalFilename() .substring(file.getOriginalFilename().lastIndexOf(".")); name_file = name_file.replace(' ', '_'); File server_file = new File( "D:/Workspace/insta/jee/WorldOfRobots/src/main/webapp/resources/images/robots/" + name_file); BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(server_file)); stream.write(bytes); stream.close(); Robot robot = this.robotService.createRobot( addRobot.getTechnologies(), addRobot.getStrong_point(), addRobot.getName(), addRobot.getCreation_date(), name_file); this.userService.createRobot(user.getId(), robot.getId()); request.setAttribute("result", true); model.addAttribute("message", "L'ajout de votre robot a bien été enregistrée"); model.addAttribute("robot", this.robotService.findById(robot.getId())); } catch (IOException e) { e.printStackTrace(); request.setAttribute("result", false); model.addAttribute("message", "L'ajout de votre robot a échoué"); } } else { Robot robot = this.robotService.createRobot( addRobot.getTechnologies(), addRobot.getStrong_point(), addRobot.getName(), addRobot.getCreation_date(), null); if (robot != null) { session.setAttribute("user", user); this.userService.createRobot(user.getId(), robot.getId()); model.addAttribute("robot", this.robotService.findById(robot.getId())); request.setAttribute("result", true); model.addAttribute("message", "L'ajout de votre robot a bien été enregistrée"); } else { request.setAttribute("result", false); model.addAttribute("message", "L'ajout de votre robot a échoué"); } } user = this.userService.findById(user.getId()); session.setAttribute("user", user); return this.cardRobot(model, user.getId_robot(), request); } else { request.setAttribute("result", false); model.addAttribute("message", "Veuillez vous connecter avant d'ajouter un robot"); model.addAttribute("connexion", new Connexion()); model.addAttribute("register", new Register()); return "connexion"; } }