Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package be.kdg.carpool.controller;
- import be.kdg.carpool.domain.Car;
- import be.kdg.carpool.domain.Path;
- import be.kdg.carpool.domain.Profile;
- import be.kdg.carpool.formview.PathForm;
- import be.kdg.carpool.services.CarService;
- import be.kdg.carpool.services.PathPassengerService;
- import be.kdg.carpool.services.PathService;
- import be.kdg.carpool.services.ProfileService;
- import be.kdg.carpool.utility.SecurityClass;
- import org.apache.log4j.Logger;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.security.access.annotation.Secured;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.validation.BindingResult;
- import org.springframework.web.bind.annotation.*;
- import javax.annotation.Resource;
- import javax.validation.Valid;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- /**
- * Created by IntelliJ IDEA.
- * User: jessica
- * Date: 13/02/12
- * Time: 13:33
- */
- @Controller(value = "pathController")
- @RequestMapping("/path")
- public class PathController {
- protected final static Logger logger = Logger.getLogger("controller");
- @Resource(name = "pathService")
- private PathService pathService;
- @Resource(name = "carService")
- private CarService carService;
- @Resource(name = "pathPassengerService")
- private PathPassengerService pathPassengerService;
- @Resource(name = "profileService")
- private ProfileService profileService;
- @Autowired
- private SecurityClass securityClass;
- /**
- * Handles and retrieves the paths JSP page
- *
- * @return the name of the JSP page
- */
- @Secured("ROLE_USER")
- @RequestMapping(method = RequestMethod.GET)
- public String getPathsPage(Model model) {
- logger.debug("Received request to show paths page");
- List<Path> pathsAsOwner = pathService.getAllPathsBelongTo(securityClass.getUsername());
- List<Path> pathsAsPassengerAccepted = pathService.getAllPathsAsPassengerAccepted(securityClass.getUsername());
- List<Path> pathsAsPassengerInvitation = pathService.getAllPathsAsPassengerInvitation(securityClass.getUsername());
- model.addAttribute("ownedPaths", pathsAsOwner);
- model.addAttribute("acceptedPassengerPaths", pathsAsPassengerAccepted);
- model.addAttribute("invitedPassengerPaths", pathsAsPassengerInvitation);
- return "path/paths";
- }
- @Secured("ROLE_USER")
- @RequestMapping(value = "/invite", method = RequestMethod. POST)
- public String inviteUser(@RequestParam(value = "pathId", required = true) int pathId,
- @RequestParam(value = "userToInvite", required = true)String userToInvite ,
- @RequestParam(value = "beginPointId", required = false) int beginPointId,
- @RequestParam(value = "endPointId", required = false) int endPointId,
- Model model) {
- logger.debug("Inviting user: " + userToInvite);
- boolean sent = pathService.invite(pathId, beginPointId, endPointId, userToInvite, securityClass.getUsername());
- model.addAttribute("sentMessage", sent);
- return getPathsPage(model);
- }
- @Secured("ROLE_USER")
- @RequestMapping(value = "/accept/{pathId}", method = RequestMethod.GET)
- public String acceptPath(@PathVariable(value = "pathId") int pathId, Model model) {
- logger.debug("Accepting path: " + pathId);
- boolean accepted = pathService.acceptPath(pathId, securityClass.getUsername(), securityClass.getUsername());
- model.addAttribute("acceptedPath", accepted);
- return getPathsPage(model);
- }
- @Secured("ROLE_USER")
- @RequestMapping(value = "/leave/{pathId}", method = RequestMethod.GET)
- public String leavePath(@PathVariable(value = "pathId") int pathId, Model model) {
- logger.debug("Leaving path: " + pathId);
- boolean left = pathService.leavePath(pathId, securityClass.getUsername(), securityClass.getUsername());
- model.addAttribute("leftPath", left);
- return getPathsPage(model);
- }
- @Secured("ROLE_USER")
- @RequestMapping(value = "/invite/delete/{passengerId}", method = RequestMethod.GET)
- public String deleteInvitation(@PathVariable(value = "passengerId") int passengerId, Model model) {
- logger.debug("Deleting invitation: " + passengerId);
- boolean deleted = pathService.deleteInvitation(passengerId, securityClass.getUsername());
- model.addAttribute("deletedInvitation", deleted);
- return getPathsPage(model);
- }
- @ModelAttribute(value = "allUsers")
- public String allUsers() {
- StringBuilder sb = new StringBuilder();
- for (Profile p : profileService.getAllProfiles()) {
- sb.append(p.getFirstName());
- sb.append(" ");
- sb.append(p.getLastName());
- sb.append(':');
- sb.append(p.getUserName());
- sb.append(',');
- }
- return sb.toString();
- }
- @Secured("ROLE_USER")
- @RequestMapping(value = "/addPath", method = RequestMethod.GET)
- public String getAddPathPage(Model model) {
- logger.debug("Received request to show paths page");
- PathForm pathForm = new PathForm();
- List<Car> cars = carService.searchCars(securityClass.getUsername());
- model.addAttribute("cars", cars);
- model.addAttribute("pathAttribute", pathForm);
- return "path/addPath";
- }
- @Secured("ROLE_USER")
- @RequestMapping(value = "/addPath", method = RequestMethod.POST)
- public String addPath(@Valid @ModelAttribute("pathAttribute") PathForm pathForm, BindingResult result, Model model) {
- logger.debug("Received request to add paths");
- Path path = new Path();
- pathForm.fillUpPath(path);
- List<String> pass = pathForm.getPassagepoints();
- System.out.println("list of the pass " + pass.get(0));
- System.out.println("list of the pass " + pass.get(1));
- System.out.println("list of the pass " + pass.size());
- pathService.createPath(path, pathForm.getStartPoint(), pathForm.getStartHour(), pathForm.getStartMin(), pathForm.getDate(), pathForm.getEndPoint(), pathForm.getEndHour(), pathForm.getEndMin(), pathForm.getCar(), pathForm.getPassagepoints());
- if (result.hasErrors()) {
- return "path/addPath";
- }
- /*
- pathService.createPath(path);
- List<Path> paths = pathService.getAllPaths(); */
- return "path/paths";
- }
- }
Add Comment
Please, Sign In to add comment