From 70eee311c501813595bf5f7047fad0b8743e21c5 Mon Sep 17 00:00:00 2001 From: Melina Gutierrez Date: Tue, 12 Nov 2024 07:00:06 -0400 Subject: [PATCH] #45-Registro Semanal de Horas Trabajadas corregir duplicados --- .../primefactorsolutions/model/Actividad.java | 66 +++++++++++++++- .../model/HoursWorked.java | 19 ++++- .../repositories/HoursWorkedRepository.java | 2 - .../views/HoursWorkedView.java | 78 ++++++++++++++++--- 4 files changed, 152 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/model/Actividad.java b/src/main/java/com/primefactorsolutions/model/Actividad.java index 6b0a4b1..7c7df34 100644 --- a/src/main/java/com/primefactorsolutions/model/Actividad.java +++ b/src/main/java/com/primefactorsolutions/model/Actividad.java @@ -1,6 +1,19 @@ package com.primefactorsolutions.model; -public final class Actividad { +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; + +import java.util.UUID; + + +@Entity +public class Actividad extends BaseEntity { + @Id + @GeneratedValue(strategy = GenerationType.UUID) + private UUID id; + private String nombre; private double lunes; private double martes; @@ -12,6 +25,8 @@ public final class Actividad { private String tarea; private double horas; + public Actividad() {} + public Actividad(final Builder builder) { this.nombre = builder.nombre; this.lunes = builder.lunes; @@ -25,10 +40,59 @@ public final class Actividad { this.horas = builder.horas; } + public String setNombre(final String nombre) { + this.nombre = nombre; + return nombre; + } + public String getNombre() { return nombre; } + public void setLunes(double lunes) { + this.lunes = lunes; + return; + } + + public void setMartes(double martes) { + this.martes = martes; + return; + } + + public void setMiercoles(double miercoles) { + this.miercoles = miercoles; + return; + } + + public void setJueves(double jueves) { + this.jueves = jueves; + return; + } + + public void setViernes(double viernes) { + this.viernes = viernes; + return; + } + + public void setSabado(double sabado) { + this.sabado = sabado; + return; + } + + public void setDomingo(double domingo) { + this.domingo = domingo; + return; + } + public void setTarea(String tarea) { + this.tarea = tarea; + return; + } + + public void setHoras(double horas) { + this.horas = horas; + return; + } + public double getLunes() { return lunes; } diff --git a/src/main/java/com/primefactorsolutions/model/HoursWorked.java b/src/main/java/com/primefactorsolutions/model/HoursWorked.java index d02310f..e23e9b0 100644 --- a/src/main/java/com/primefactorsolutions/model/HoursWorked.java +++ b/src/main/java/com/primefactorsolutions/model/HoursWorked.java @@ -6,6 +6,7 @@ import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; +import java.time.LocalDate; import java.util.UUID; @Entity @@ -17,10 +18,24 @@ public class HoursWorked extends BaseEntity { @ManyToOne private Employee employee; + @ManyToOne + private Actividad actividad; + private int weekNumber; private double totalHours; - public HoursWorked() { } + private LocalDate fecha; + + + public HoursWorked() {} + + public Actividad getActividad() { + return this.actividad; + } + + public void setActividad(Actividad actividad){ + this.actividad = actividad; + } public UUID getId() { return id; @@ -46,6 +61,8 @@ public class HoursWorked extends BaseEntity { this.weekNumber = weekNumber; } + public LocalDate getFecha() { return this.fecha;} + public double getTotalHours() { return totalHours; } diff --git a/src/main/java/com/primefactorsolutions/repositories/HoursWorkedRepository.java b/src/main/java/com/primefactorsolutions/repositories/HoursWorkedRepository.java index 6e0e69c..b3d0bdb 100644 --- a/src/main/java/com/primefactorsolutions/repositories/HoursWorkedRepository.java +++ b/src/main/java/com/primefactorsolutions/repositories/HoursWorkedRepository.java @@ -8,7 +8,5 @@ import java.util.List; @Repository public interface HoursWorkedRepository extends JpaRepository { - // Puedes definir consultas personalizadas aquí si es necesario. List findByWeekNumber(int weekNumber); - } diff --git a/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java b/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java index 7c1f644..a17d6de 100644 --- a/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java +++ b/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java @@ -4,7 +4,6 @@ import com.primefactorsolutions.model.Employee; import com.primefactorsolutions.model.HoursWorked; import com.primefactorsolutions.service.EmployeeService; import com.primefactorsolutions.service.HoursWorkedService; -import com.vaadin.flow.component.datepicker.DatePicker; import com.vaadin.flow.component.grid.Grid; import com.vaadin.flow.component.notification.Notification; import com.vaadin.flow.component.orderedlayout.HorizontalLayout; @@ -16,12 +15,14 @@ import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.Route; import com.vaadin.flow.spring.annotation.SpringComponent; import jakarta.annotation.security.PermitAll; +import jakarta.persistence.ManyToOne; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Scope; import com.vaadin.flow.component.html.Label; import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.view.InternalResourceViewResolver; +import org.vaadin.firitin.components.datepicker.VDatePicker; import java.time.DayOfWeek; @@ -38,6 +39,9 @@ import java.util.Locale; @PageTitle("Hours Worked") @Route(value = "/hours-worked/me", layout = MainLayout.class) public class HoursWorkedView extends VerticalLayout { + @ManyToOne + private Actividad actividad; + private final List actividades = new ArrayList<>(); private final List actividadesEspecificas = new ArrayList<>(); // Nueva lista para tareas específicas private final Grid grid = new Grid<>(Actividad.class); @@ -64,12 +68,7 @@ public class HoursWorkedView extends VerticalLayout { private final Label numeroSemanaLabel = new Label("Número de la Semana: "); - private DatePicker fechaPicker = new DatePicker("Selecciona una fecha"); - @Autowired - private InternalResourceViewResolver defaultViewResolver; - @Qualifier("defaultServletHandlerMapping") - @Autowired - private HandlerMapping defaultServletHandlerMapping; + private VDatePicker fechaPicker = new VDatePicker("Selecciona una fecha"); public HoursWorkedView(final EmployeeService employeeService, final HoursWorkedService hoursWorkedService) { @@ -100,9 +99,20 @@ public class HoursWorkedView extends VerticalLayout { private void cargarDatos() { if (selectedStartOfWeek != null && weekNumber > 0) { - actividades.clear(); - actividadesEspecificas.clear(); List listaDeHorasTrabajadas = obtenerDatos(); + + for (HoursWorked hours : listaDeHorasTrabajadas) { + LocalDate activityDate = hours.getFecha(); + int activityWeek = getWeekOfYear(activityDate); + if (activityWeek == weekNumber) { + Actividad actividad = hours.getActividad(); + if (actividad != null) { + DayOfWeek dayOfWeek = activityDate.getDayOfWeek(); + agregarOActualizarActividad(actividad, dayOfWeek, hours.getTotalHours()); + } + } + } + grid.setItems(actividades); gridActividadesEspecificas.setItems(actividadesEspecificas); calcularTotalHoras(listaDeHorasTrabajadas); @@ -134,6 +144,7 @@ public class HoursWorkedView extends VerticalLayout { } private void configurarVista() { + fechaPicker.setMax(LocalDate.now()); fechaPicker.addValueChangeListener(event -> { LocalDate selectedDate = event.getValue(); if (selectedDate != null) { @@ -228,6 +239,13 @@ public class HoursWorkedView extends VerticalLayout { Notification.show("Por favor, selecciona una fecha."); return; } + + int selectedWeekNumber = getWeekOfYear(selectedDate); + if (selectedWeekNumber != weekNumber) { + Notification.show("Solo puedes agregar actividades dentro de la semana actual."); + return; + } + DayOfWeek selectedDay = selectedDate.getDayOfWeek(); Actividad.Builder actividadBuilder = new Actividad.Builder() @@ -258,6 +276,10 @@ public class HoursWorkedView extends VerticalLayout { actividades.add(nuevaActividad); grid.setItems(actividades); } + + Actividad nuevaActividad = actividadBuilder.build(); + agregarOActualizarActividad(nuevaActividad, selectedDay, horas); + actualizarTotales(); Notification.show("Actividad agregada correctamente"); actividadNombre.clear(); @@ -272,6 +294,44 @@ public class HoursWorkedView extends VerticalLayout { return new HorizontalLayout(actividadNombre, horasInput, agregarActividadButton); } + private void agregarOActualizarActividad(Actividad nuevaActividad, DayOfWeek dia, double horas) { + Actividad actividadExistente = actividades.stream() + .filter(a -> a.getNombre().equals(nuevaActividad.getNombre())) + .findFirst() + .orElse(null); + + if (actividadExistente != null) { + // Si la actividad ya existe, actualiza las horas para el día correspondiente + switch (dia) { + case MONDAY -> actividadExistente.setLunes(horas); + case TUESDAY -> actividadExistente.setMartes(horas); + case WEDNESDAY -> actividadExistente.setMiercoles(horas); + case THURSDAY -> actividadExistente.setJueves(horas); + case FRIDAY -> actividadExistente.setViernes(horas); + case SATURDAY -> actividadExistente.setSabado(horas); + case SUNDAY -> actividadExistente.setDomingo(horas); + default -> throw new IllegalArgumentException("Día seleccionado no válido: " + dia); + } + } else { + // Si no existe, se agrega como nueva actividad + switch (dia) { + case MONDAY -> nuevaActividad.setLunes(horas); + case TUESDAY -> nuevaActividad.setMartes(horas); + case WEDNESDAY -> nuevaActividad.setMiercoles(horas); + case THURSDAY -> nuevaActividad.setJueves(horas); + case FRIDAY -> nuevaActividad.setViernes(horas); + case SATURDAY -> nuevaActividad.setSabado(horas); + case SUNDAY -> nuevaActividad.setDomingo(horas); + default -> throw new IllegalArgumentException("Día seleccionado no válido: " + dia); + } + actividades.add(nuevaActividad); + } + + // Actualiza el Grid + grid.setItems(actividades); + } + + private HorizontalLayout configurarTareasEspecificasLayout() { Button agregarTareaButton = new Button("Agregar Tarea PFS", e -> {