Skip to content

Commit

Permalink
uwu
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosGuerraLora committed Mar 29, 2019
2 parents 5e1a1e7 + f8c2cb9 commit fbf8728
Show file tree
Hide file tree
Showing 36 changed files with 1,312 additions and 482 deletions.
60 changes: 47 additions & 13 deletions src/main/java/controllers/ReservationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,71 @@

package controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import security.UserAccount;
import services.ActorService;
import services.ReservationService;
import domain.Actor;
import domain.Driver;
import domain.Passenger;
import domain.Reservation;

@Controller
@RequestMapping("/reservation")
public class ReservationController extends AbstractController {

@Autowired
private ReservationService reservationService;

@Autowired
private ActorService actorService;


// Display ---------------------------------------------------------------

@RequestMapping("/display")
public ModelAndView action1() {
ModelAndView result;
@RequestMapping(value = "/display", method = RequestMethod.GET)
public ModelAndView display(@RequestParam final int reservationId) {
Assert.notNull(reservationId);
ModelAndView res;
Reservation reservation;
UserAccount ua;
Actor actor = this.actorService.findByPrincipal();
Driver driver = null;
Passenger passenger = null;

result = new ModelAndView("reservation/action-1");
if (actor instanceof Passenger) {

return result;
}
passenger = (Passenger) actor;

} else if (actor instanceof Driver) {

driver = (Driver) actor;

} else {

Assert.isTrue(1 == 0, "El usuario no es ni Drive ni Passenger");

}

reservation = this.reservationService.findOne(reservationId);
Assert.isTrue((reservation.getPassenger().getId() == passenger.getId()) || reservation.getRoute().getDriver().getId() == driver.getId());

// Action-2 ---------------------------------------------------------------
res = new ModelAndView("reservation/display");

@RequestMapping("/action-2")
public ModelAndView action2() {
ModelAndView result;
res.addObject("reservation", reservation);

result = new ModelAndView("reservation/action-2");
return res;

return result;
}

// Action-2 ---------------------------------------------------------------
// Listing ---------------------------------------------------------------

@RequestMapping("/action-3")
public ModelAndView action3() {
Expand Down
121 changes: 111 additions & 10 deletions src/main/java/controllers/RouteController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@

package controllers;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;

import javax.validation.Valid;

Expand All @@ -14,23 +18,32 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import security.LoginService;
import security.UserAccount;
import services.ActorService;
import services.ReservationService;
import services.RouteService;
import domain.Actor;
import domain.Administrator;
import domain.Driver;
import domain.Finder;
import domain.Passenger;
import domain.Reservation;
import domain.ReservationStatus;
import domain.Route;

@Controller
@RequestMapping("/route")
public class RouteController extends AbstractController {

// Services ---------------------------------------------------------------

@Autowired
private RouteService routeService;
private RouteService routeService;
@Autowired
private ReservationService reservationService;

@Autowired
private ActorService actorService;
private ActorService actorService;


// Constructors -----------------------------------------------------------
Expand All @@ -54,23 +67,111 @@ public ModelAndView list() {

}

// Creation ---------------------------------------------------------------
// Display ---------------------------------------------------------------

@RequestMapping(value = "/display", method = RequestMethod.GET)
public ModelAndView display(@RequestParam final int routeID) {

public ModelAndView display(@RequestParam final int routeId) {
ModelAndView result;
Route route;

route = this.routeService.findOne(routeID);
Collection<Reservation> reservations, displayableReservations;
Integer occupiedSeats;
UserAccount ua;
Actor actor;
Integer rol = 0; //1->conductor de la ruta | 2->pasajero con reserva | 3-> pasajero sin reserva | 4->admin
// Reservation reservation;
boolean startedRoute = false;
boolean hasPassed10Minutes = false;
boolean arrivalPlus10Min = false;

route = this.routeService.findOne(routeId);
Assert.notNull(route);
result = new ModelAndView("route/display");

// reservation = this.reservationService.create();
// reservation.setRoute(route);
// reservation.setPrice(route.getPricePerPassenger());
// reservation.setLuggageSize(LuggageSize.NOTHING);
// reservation.setStatus(ReservationStatus.PENDING);

reservations = route.getReservations();
displayableReservations = new ArrayList<Reservation>();
occupiedSeats = 0;
ua = LoginService.getPrincipal();
actor = this.actorService.findByUserAccount(ua);
Assert.notNull(actor);

if (actor instanceof Driver) {
final Driver driver = (Driver) actor;
if (route.getDriver().equals(driver))
rol = 1;
}
if (reservations != null && reservations.size() > 0)
for (final Reservation res : reservations) {
if (res.getStatus().equals(ReservationStatus.ACCEPTED)) {
occupiedSeats++; //Contamos asientos ocupados
displayableReservations.add(res); //añadimos las reservas aceptadas
}
if (actor instanceof Driver) {
final Driver driver = (Driver) actor;
if (route.getDriver().equals(driver)) { //Si el conductor logeado es el de la ruta...
rol = 1; //...se considerará como "conductor de la ruta"...
if (route.getDepartureDate().after(new Date())) // ...y además si la ruta no ha empezado...
if (res.getStatus().equals(ReservationStatus.PENDING))
displayableReservations.add(res); //...añadimos tambien reservas pendientes
}
}

if (actor instanceof Passenger) { //Si el actor logueado es pasajero...
final Passenger passenger = (Passenger) actor;
for (final Reservation r : reservations)
//...y ha hecho alguna reserva en la ruta
if (r.getPassenger().equals(passenger)) {
rol = 2; //...se considerara como "pasajero con reserva"
result.addObject("reservation", r);
if (route.getDepartureDate().before(new Date()))
startedRoute = true;
break;
} else
rol = 3;
//reservation.setPassenger(passenger);
}

if (actor instanceof Administrator)
rol = 4;
}
else if (actor instanceof Passenger)
//final Passenger passenger = (Passenger) actor;
rol = 3;

//----proceso para conseguir la fecha de llegada---
final Calendar date = Calendar.getInstance();
date.setTime(route.getDepartureDate());
final long departureDateMilis = date.getTimeInMillis();
final Date arrivalDate = new Date(departureDateMilis + route.getEstimatedDuration() * 60000);
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
sdf.format(arrivalDate);
//------------------------------------------------

//----proceso para conseguir la fecha de salida + 10 minutos---
final Date tenMinutesAfterDeparture = new Date(departureDateMilis + 600000);
if (new Date().after(tenMinutesAfterDeparture))
hasPassed10Minutes = true;
//----proceso para conseguir la fecha de llegada + 10 minutos---
final Date tenMinutesAfterArrival = new Date(departureDateMilis + (route.getEstimatedDuration() * 60000) + 600000);
if (new Date().after(tenMinutesAfterArrival))
arrivalPlus10Min = true;
//------------------------------------------------
result.addObject("route", route);
result.addObject("remainingSeats", route.getAvailableSeats() - occupiedSeats);
result.addObject("arrivalDate", sdf.format(arrivalDate));
result.addObject("reservations", displayableReservations);
result.addObject("rol", rol);
// result.addObject("newReservation", reservation);
result.addObject("startedRoute", startedRoute);
result.addObject("hasPassed10Minutes", hasPassed10Minutes);
result.addObject("hasPassed20Minutes", arrivalPlus10Min);

return result;
}

@RequestMapping(value = "/search", method = RequestMethod.GET)
public ModelAndView searchView() {
final ModelAndView result;
Expand Down
57 changes: 19 additions & 38 deletions src/main/java/controllers/driver/ReservationDriverController.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,62 +37,43 @@ public class ReservationDriverController extends AbstractController {
public ReservationDriverController() {
super();
}



// Accept Reservation ---------------------------------------------------------------

@RequestMapping(value="/acceptReservation", method=RequestMethod.POST, params = "acceptReservation")
public ModelAndView acceptReservation(@RequestParam(defaultValue = "0") int reservationId){
@RequestMapping(value = "/acceptReservation", method = RequestMethod.POST, params = "acceptReservation")
//, params = "acceptReservation")
public ModelAndView acceptReservation(@RequestParam(defaultValue = "0") final int reservationId) {
ModelAndView res;
Reservation reservation = reservationService.findOne(reservationId);
Route route = reservation.getRoute();
final Reservation reservation = this.reservationService.findOne(reservationId);
final Route route = reservation.getRoute();

try{
try {
this.reservationService.acceptReservation(reservationId);
res = new ModelAndView("redirect:/route/display.do?routeId=" + route.getId());

}catch (Exception e) {
} catch (final Exception e) {
res = new ModelAndView("redirect:/route/display.do?routeId=" + route.getId());
}

return res;
}

// Reject Reservation ---------------------------------------------------------------
@RequestMapping(value="/rejectReservation", method=RequestMethod.POST, params = "rejectReservation")
public ModelAndView rejectReservation(@RequestParam(defaultValue = "0") int reservationId){

@RequestMapping(value = "/rejectReservation", method = RequestMethod.POST, params = "rejectReservation")
public ModelAndView rejectReservation(@RequestParam(defaultValue = "0") final int reservationId) {
ModelAndView res;
Reservation reservation = reservationService.findOne(reservationId);
Route route = routeService.findOne(reservation.getRoute().getId());
final Reservation reservation = this.reservationService.findOne(reservationId);
final Route route = this.routeService.findOne(reservation.getRoute().getId());

try{
try {
this.reservationService.rejectReservation(reservationId);
res = new ModelAndView("redirect:/route/display.do" + route.getId());
res = new ModelAndView("redirect:/route/display.do?routeId=" + route.getId());

}catch (Exception e) {
res = new ModelAndView("redirect:/route/display.do" + route.getId());
} catch (final Exception e) {
res = new ModelAndView("redirect:/route/display.do?routeId=" + route.getId());
}

return res;
}

// Cancel Reservation ---------------------------------------------------------------

@RequestMapping(value="/cancelReservation", method=RequestMethod.POST, params = "cancelReservation")
public ModelAndView cancelReservation(@RequestParam(defaultValue = "0") int reservationId){
ModelAndView res;
Reservation reservation = reservationService.findOne(reservationId);
Route route = routeService.findOne(reservation.getRoute().getId());

try{
this.reservationService.cancelReservation(reservationId);
res = new ModelAndView("redirect:/route/display.do" + route.getId());

}catch (Exception e) {
res = new ModelAndView("redirect:/route/display.do" + route.getId());
}

return res;
}

Expand Down
26 changes: 20 additions & 6 deletions src/main/java/controllers/driver/RouteDriverController.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public ModelAndView edit(@RequestParam final int routeId) {
return result;
}

@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "save")
@RequestMapping(value = "/create", method = RequestMethod.POST, params = "save")
public ModelAndView save(@Valid final Route route, final BindingResult binding) {
Route saved;

Expand All @@ -115,14 +115,14 @@ public ModelAndView save(@Valid final Route route, final BindingResult binding)
}

@RequestMapping(value = "/cancel", method = RequestMethod.GET)
public ModelAndView cancel(@RequestParam final int routeID) {
public ModelAndView cancel(@RequestParam final int routeId) {
ModelAndView result;
Route route;

route = this.routeService.findOne(routeID);
route = this.routeService.findOne(routeId);
try {
this.routeService.cancel(route);
result = this.routeDisplayModelAndView(route, null);
result = new ModelAndView("redirect:/route/driver/listActive.do");
} catch (final Throwable oops) {
oops.printStackTrace();
result = this.routeDisplayModelAndView(route, "driver.cancel.error");
Expand All @@ -134,7 +134,7 @@ public ModelAndView cancel(@RequestParam final int routeID) {
// Delete/Confirm route ---------------------------------------------------------------

@RequestMapping(value = "/delete", method = RequestMethod.GET)
public ModelAndView delete(final Route route, final BindingResult binding) {
public ModelAndView delete(final Route route) {
ModelAndView result;

try {
Expand All @@ -146,6 +146,20 @@ public ModelAndView delete(final Route route, final BindingResult binding) {
return result;
}

@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "delete")
public ModelAndView delete(final Route route, final BindingResult binding) {
ModelAndView result;
for (final ObjectError oe : binding.getAllErrors())
System.out.println(oe);
try {
this.routeService.delete(route);
result = new ModelAndView("redirect:list.do");
} catch (final Throwable oops) {
result = this.createEditModelAndView(route, "route.commit.error");
}
return result;
}

@RequestMapping(value = "/confirmRoute", method = RequestMethod.GET)
public ModelAndView confirmRoute(@RequestParam final int routeId) {
ModelAndView result;
Expand Down Expand Up @@ -181,7 +195,7 @@ private ModelAndView createEditModelAndView(final Route route, final String mess

final Driver driver = (Driver) this.actorService.findByPrincipal();

requestURI = "route/driver/edit.do";
requestURI = "route/driver/create.do";
result = new ModelAndView("route/driver/create");
result.addObject("route", route);
result.addObject("vehicles", driver.getVehicles());
Expand Down
Loading

0 comments on commit fbf8728

Please sign in to comment.