Commit 22e7f5b9 by Antonio Rueda

Añadida servicio programado para eliminación de reservas antiguas.

parent 61614bfc
......@@ -3,12 +3,14 @@ package es.ujaen.dae.reservahoteles.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
*
* @author ajrueda
*/
@SpringBootApplication(scanBasePackages="es.ujaen.dae.reservahoteles.servicios")
@EnableScheduling
public class ReservaHoteles {
public static void main(String[] args) {
SpringApplication.run(ReservaHoteles.class);
......
......@@ -171,4 +171,17 @@ public class Hotel {
private static boolean fechaEnIntervalo(LocalDate fecha, LocalDate fechaInicio, LocalDate fechaFin) {
return fecha.isAfter(fechaInicio.minusDays(1)) && fecha.isBefore(fechaFin.plusDays(1));
}
/**
* Elimina las reservas anteriores a una fecha limite
* @param fechaLimite la fecha límite para preservar una reserva
*/
public void eliminarReservasAnteriores(LocalDate fechaLimite) {
var i = reservas.iterator();
while (i.hasNext()) {
Reserva reserva = i.next();
if (reserva.fechaFin().isBefore(fechaLimite))
i.remove();
}
}
}
......@@ -18,6 +18,8 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
......@@ -31,6 +33,9 @@ public class ServicioReservas {
Map<Integer, Hotel> hoteles;
Map<String, Usuario> clientes;
@Value("${meses-historico}")
int mesesHistorico;
// Cliente especial de dirección
private static final Usuario direccion = new Usuario("direccion", "-", "670343332", "direccion@hotelxyz.es", "SeCrEtO");
......@@ -145,4 +150,13 @@ public class ServicioReservas {
return reserva;
}
@Scheduled(cron="0 0 0 1 * ?")
public void eliminarReservasAntiguas() {
var fechaLimite = LocalDate.now().minusMonths(mesesHistorico);
for (var hotel: hoteles.values()) {
hotel.eliminarReservasAnteriores(fechaLimite);
}
}
}
meses-historico: 3
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment