Guest User

Untitled

a guest
Jul 7th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. package be.kdg.carpool.controller;
  2.  
  3. import be.kdg.carpool.domain.Car;
  4. import be.kdg.carpool.domain.Path;
  5. import be.kdg.carpool.domain.Profile;
  6. import be.kdg.carpool.formview.PathForm;
  7. import be.kdg.carpool.services.CarService;
  8. import be.kdg.carpool.services.PathPassengerService;
  9. import be.kdg.carpool.services.PathService;
  10. import be.kdg.carpool.services.ProfileService;
  11. import be.kdg.carpool.utility.SecurityClass;
  12. import org.apache.log4j.Logger;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.security.access.annotation.Secured;
  15. import org.springframework.stereotype.Controller;
  16. import org.springframework.ui.Model;
  17. import org.springframework.validation.BindingResult;
  18. import org.springframework.web.bind.annotation.*;
  19.  
  20. import javax.annotation.Resource;
  21. import javax.validation.Valid;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.Map;
  25.  
  26. /**
  27. * Created by IntelliJ IDEA.
  28. * User: jessica
  29. * Date: 13/02/12
  30. * Time: 13:33
  31. */
  32.  
  33.  
  34. @Controller(value = "pathController")
  35. @RequestMapping("/path")
  36. public class PathController {
  37.  
  38. protected final static Logger logger = Logger.getLogger("controller");
  39.  
  40. @Resource(name = "pathService")
  41. private PathService pathService;
  42.  
  43. @Resource(name = "carService")
  44. private CarService carService;
  45.  
  46. @Resource(name = "pathPassengerService")
  47. private PathPassengerService pathPassengerService;
  48.  
  49. @Resource(name = "profileService")
  50. private ProfileService profileService;
  51.  
  52. @Autowired
  53. private SecurityClass securityClass;
  54.  
  55. /**
  56. * Handles and retrieves the paths JSP page
  57. *
  58. * @return the name of the JSP page
  59. */
  60. @Secured("ROLE_USER")
  61. @RequestMapping(method = RequestMethod.GET)
  62. public String getPathsPage(Model model) {
  63. logger.debug("Received request to show paths page");
  64. List<Path> pathsAsOwner = pathService.getAllPathsBelongTo(securityClass.getUsername());
  65. List<Path> pathsAsPassengerAccepted = pathService.getAllPathsAsPassengerAccepted(securityClass.getUsername());
  66. List<Path> pathsAsPassengerInvitation = pathService.getAllPathsAsPassengerInvitation(securityClass.getUsername());
  67. model.addAttribute("ownedPaths", pathsAsOwner);
  68. model.addAttribute("acceptedPassengerPaths", pathsAsPassengerAccepted);
  69. model.addAttribute("invitedPassengerPaths", pathsAsPassengerInvitation);
  70. return "path/paths";
  71. }
  72.  
  73. @Secured("ROLE_USER")
  74. @RequestMapping(value = "/invite", method = RequestMethod. POST)
  75. public String inviteUser(@RequestParam(value = "pathId", required = true) int pathId,
  76. @RequestParam(value = "userToInvite", required = true)String userToInvite ,
  77. @RequestParam(value = "beginPointId", required = false) int beginPointId,
  78. @RequestParam(value = "endPointId", required = false) int endPointId,
  79. Model model) {
  80. logger.debug("Inviting user: " + userToInvite);
  81.  
  82. boolean sent = pathService.invite(pathId, beginPointId, endPointId, userToInvite, securityClass.getUsername());
  83. model.addAttribute("sentMessage", sent);
  84.  
  85. return getPathsPage(model);
  86. }
  87.  
  88. @Secured("ROLE_USER")
  89. @RequestMapping(value = "/accept/{pathId}", method = RequestMethod.GET)
  90. public String acceptPath(@PathVariable(value = "pathId") int pathId, Model model) {
  91. logger.debug("Accepting path: " + pathId);
  92.  
  93. boolean accepted = pathService.acceptPath(pathId, securityClass.getUsername(), securityClass.getUsername());
  94. model.addAttribute("acceptedPath", accepted);
  95.  
  96. return getPathsPage(model);
  97. }
  98.  
  99. @Secured("ROLE_USER")
  100. @RequestMapping(value = "/leave/{pathId}", method = RequestMethod.GET)
  101. public String leavePath(@PathVariable(value = "pathId") int pathId, Model model) {
  102. logger.debug("Leaving path: " + pathId);
  103.  
  104. boolean left = pathService.leavePath(pathId, securityClass.getUsername(), securityClass.getUsername());
  105. model.addAttribute("leftPath", left);
  106.  
  107. return getPathsPage(model);
  108. }
  109. @Secured("ROLE_USER")
  110. @RequestMapping(value = "/invite/delete/{passengerId}", method = RequestMethod.GET)
  111. public String deleteInvitation(@PathVariable(value = "passengerId") int passengerId, Model model) {
  112. logger.debug("Deleting invitation: " + passengerId);
  113.  
  114. boolean deleted = pathService.deleteInvitation(passengerId, securityClass.getUsername());
  115. model.addAttribute("deletedInvitation", deleted);
  116.  
  117. return getPathsPage(model);
  118. }
  119.  
  120. @ModelAttribute(value = "allUsers")
  121. public String allUsers() {
  122. StringBuilder sb = new StringBuilder();
  123. for (Profile p : profileService.getAllProfiles()) {
  124. sb.append(p.getFirstName());
  125. sb.append(" ");
  126. sb.append(p.getLastName());
  127. sb.append(':');
  128. sb.append(p.getUserName());
  129. sb.append(',');
  130. }
  131.  
  132. return sb.toString();
  133. }
  134.  
  135. @Secured("ROLE_USER")
  136. @RequestMapping(value = "/addPath", method = RequestMethod.GET)
  137. public String getAddPathPage(Model model) {
  138. logger.debug("Received request to show paths page");
  139. PathForm pathForm = new PathForm();
  140. List<Car> cars = carService.searchCars(securityClass.getUsername());
  141. model.addAttribute("cars", cars);
  142. model.addAttribute("pathAttribute", pathForm);
  143. return "path/addPath";
  144. }
  145.  
  146. @Secured("ROLE_USER")
  147. @RequestMapping(value = "/addPath", method = RequestMethod.POST)
  148. public String addPath(@Valid @ModelAttribute("pathAttribute") PathForm pathForm, BindingResult result, Model model) {
  149. logger.debug("Received request to add paths");
  150. Path path = new Path();
  151.  
  152. pathForm.fillUpPath(path);
  153. List<String> pass = pathForm.getPassagepoints();
  154. System.out.println("list of the pass " + pass.get(0));
  155. System.out.println("list of the pass " + pass.get(1));
  156. System.out.println("list of the pass " + pass.size());
  157. pathService.createPath(path, pathForm.getStartPoint(), pathForm.getStartHour(), pathForm.getStartMin(), pathForm.getDate(), pathForm.getEndPoint(), pathForm.getEndHour(), pathForm.getEndMin(), pathForm.getCar(), pathForm.getPassagepoints());
  158.  
  159. if (result.hasErrors()) {
  160. return "path/addPath";
  161. }
  162. /*
  163. pathService.createPath(path);
  164. List<Path> paths = pathService.getAllPaths(); */
  165.  
  166. return "path/paths";
  167. }
  168. }
Add Comment
Please, Sign In to add comment