PageRenderTime 79ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/cargodenuit/web/controller/IndexController.java

https://gitlab.com/rizomm/Technical-Project
Java | 181 lines | 128 code | 41 blank | 12 comment | 13 complexity | 9f68631ccd2deb3ab127e35020bc6af6 MD5 | raw file
  1. package com.cargodenuit.web.controller;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.Iterator;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import javax.annotation.Resource;
  8. import org.springframework.security.core.Authentication;
  9. import org.springframework.security.core.context.SecurityContextHolder;
  10. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  11. import org.springframework.stereotype.Controller;
  12. import org.springframework.ui.Model;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RequestMethod;
  15. import org.springframework.web.bind.annotation.RequestParam;
  16. import org.springframework.web.servlet.ModelAndView;
  17. import com.cargodenuit.business.service.BookingService;
  18. import com.cargodenuit.business.service.BrandService;
  19. import com.cargodenuit.business.service.CarService;
  20. import com.cargodenuit.business.service.CarmodelService;
  21. import com.cargodenuit.business.service.FuelService;
  22. import com.cargodenuit.business.service.UserRolesService;
  23. import com.cargodenuit.business.service.UserService;
  24. import com.cargodenuit.entity.Booking;
  25. import com.cargodenuit.entity.Brand;
  26. import com.cargodenuit.entity.Car;
  27. import com.cargodenuit.entity.Fuel;
  28. import com.cargodenuit.entity.Trip;
  29. import com.cargodenuit.entity.User;
  30. import com.cargodenuit.entity.UserRoles;
  31. import com.cargodenuit.web.common.FormMode;
  32. import com.cargodenuit.web.listitem.FuelListItem;
  33. @Controller
  34. public class IndexController {
  35. @Resource
  36. private UserRolesService userRolesService;
  37. @Resource
  38. private UserService userService;
  39. @Resource
  40. private BookingService bookingService;
  41. @Resource
  42. private CarService carService;
  43. @Resource
  44. private CarmodelService carmodelService;
  45. @Resource
  46. private FuelService fuelService;
  47. @Resource
  48. private BrandService brandService;
  49. public User getLastUser(final Collection<User> c) {
  50. final Iterator<User> itr = c.iterator();
  51. Object lastElement = itr.next();
  52. while(itr.hasNext()) {
  53. lastElement=itr.next();
  54. }
  55. return (User) lastElement;
  56. }
  57. public Car getLastCar(final Collection<Car> c) {
  58. final Iterator<Car> itr = c.iterator();
  59. Object lastElement = itr.next();
  60. while(itr.hasNext()) {
  61. lastElement=itr.next();
  62. }
  63. return (Car) lastElement;
  64. }
  65. @RequestMapping(value = "")
  66. public ModelAndView index() {
  67. ModelAndView model = new ModelAndView("index");
  68. /**
  69. * On construit le form pour créer un user
  70. */
  71. User user = new User();
  72. List<User> allUsers = userService.findAll();
  73. User lastuser = getLastUser(allUsers);
  74. if(allUsers != null && !allUsers.isEmpty()){
  75. user.setId(lastuser.getId()+1);
  76. }
  77. else{
  78. user.setId(0);
  79. }
  80. /**
  81. * On construit le form pour créer une voiture
  82. */
  83. Car car = new Car();
  84. Integer idcar = 0 ;
  85. List<Car> allCars = carService.findAll();
  86. if(allCars != null && !allCars.isEmpty()){
  87. Car lastCar = getLastCar(allCars);
  88. idcar = lastCar.getId() +1 ;
  89. car.setId(idcar);
  90. }
  91. else{
  92. car.setId(idcar);
  93. }
  94. List<Brand> listofbrand = brandService.findAll();
  95. List<Fuel> list = fuelService.findAll();
  96. List<FuelListItem> listOfFuelItems = new LinkedList<FuelListItem>();
  97. for ( Fuel fuel : list ) {
  98. listOfFuelItems.add(new FuelListItem( fuel ) );
  99. }
  100. /**
  101. * On construit la home page
  102. */
  103. Boolean isLogged = false ;
  104. Boolean isDriver = false ;
  105. Boolean isPassenger = false ;
  106. Boolean isAdmin = false ;
  107. List<Booking> bookingList = new ArrayList<Booking>() ;
  108. // On test si il y a un user connecté
  109. Authentication auth = SecurityContextHolder.getContext().getAuthentication();
  110. if(auth.getName() != "anonymousUser"){
  111. isLogged = true;
  112. //On recupere la liste des roles du ser
  113. List<UserRoles> userRoles = userRolesService.findByUsername(auth.getName());
  114. User connectedUser = userService.findByUsername(auth.getName());
  115. //On boucle sur la liste des roles du user et si on trouve le ROLE_USER ou le ROLE_ADMIN, on met les flags a true
  116. for( UserRoles role : userRoles ){
  117. if(role.getRole().contentEquals("ROLE_DRIVER")){
  118. isDriver = true;
  119. }
  120. if(role.getRole().contentEquals("ROLE_ADMIN")){
  121. isAdmin = true;
  122. }
  123. if(role.getRole().contentEquals("ROLE_PASSENGER")){
  124. isPassenger = true;
  125. }
  126. }
  127. bookingList = bookingService.findByUserId(connectedUser.getId());
  128. }
  129. model.addObject("isLogged", isLogged);
  130. model.addObject("isDriver", isDriver);
  131. model.addObject("isPassenger", isPassenger);
  132. model.addObject("isAdmin", isAdmin);
  133. model.addObject("bookingList", bookingList);
  134. model.addObject("user", user);
  135. model.addObject("car", car);
  136. model.addObject("listofbrand", listofbrand);
  137. model.addObject("listOfFuelItems", listOfFuelItems);
  138. return model;
  139. }
  140. }