From 1c4e5323fe202152b3abbe550f6ef36b6b1efce5 Mon Sep 17 00:00:00 2001 From: Melina Gutierrez Date: Wed, 13 Nov 2024 13:00:39 -0400 Subject: [PATCH] #45-Registro Semanal de Horas Trabajadas guardar solo una actividad --- .../primefactorsolutions/model/Actividad.java | 10 +++- .../model/HoursWorked.java | 55 ++++++++++++++++--- .../repositories/EmployeeRepository.java | 2 +- .../service/EmployeeService.java | 2 +- .../views/EmployeeView.java | 1 + .../views/HoursWorkedView.java | 34 +++++++----- 6 files changed, 78 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/model/Actividad.java b/src/main/java/com/primefactorsolutions/model/Actividad.java index 7c7df34..717e3aa 100644 --- a/src/main/java/com/primefactorsolutions/model/Actividad.java +++ b/src/main/java/com/primefactorsolutions/model/Actividad.java @@ -4,11 +4,16 @@ import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; import java.util.UUID; - +@Data @Entity +@AllArgsConstructor +@EqualsAndHashCode(callSuper = true) public class Actividad extends BaseEntity { @Id @GeneratedValue(strategy = GenerationType.UUID) @@ -129,7 +134,6 @@ public class Actividad extends BaseEntity { return horas; } - // Builder para crear instancias de Actividad public static class Builder { private String nombre; private double lunes; @@ -139,7 +143,7 @@ public class Actividad extends BaseEntity { private double viernes; private double sabado; private double domingo; - private String tarea; // Cambié 'tarea' por 'descripcion' + private String tarea; private double horas; public Builder tarea(final String tarea, final double horas) { diff --git a/src/main/java/com/primefactorsolutions/model/HoursWorked.java b/src/main/java/com/primefactorsolutions/model/HoursWorked.java index e23e9b0..585ee9b 100644 --- a/src/main/java/com/primefactorsolutions/model/HoursWorked.java +++ b/src/main/java/com/primefactorsolutions/model/HoursWorked.java @@ -5,11 +5,20 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; +import jakarta.persistence.CascadeType; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; import java.time.LocalDate; +import java.time.temporal.WeekFields; +import java.util.Locale; import java.util.UUID; +@Data @Entity +@AllArgsConstructor +@EqualsAndHashCode(callSuper = true) public class HoursWorked extends BaseEntity { @Id @GeneratedValue(strategy = GenerationType.UUID) @@ -18,16 +27,20 @@ public class HoursWorked extends BaseEntity { @ManyToOne private Employee employee; - @ManyToOne + @ManyToOne(cascade = CascadeType.PERSIST) // Añadimos cascade para que persista la actividad automáticamente private Actividad actividad; private int weekNumber; private double totalHours; - private LocalDate fecha; - public HoursWorked() {} + public HoursWorked(Employee employee) { + this.employee = employee; + this.weekNumber = 0; + this.totalHours = 0; + this.fecha = LocalDate.now(); + } public Actividad getActividad() { return this.actividad; @@ -49,8 +62,8 @@ public class HoursWorked extends BaseEntity { return employee; } - public void setEmployee(final Employee value) { - this.employee = value; + public void setEmployee(final Employee employee) { + this.employee = employee; } public int getWeekNumber() { @@ -61,8 +74,6 @@ public class HoursWorked extends BaseEntity { this.weekNumber = weekNumber; } - public LocalDate getFecha() { return this.fecha;} - public double getTotalHours() { return totalHours; } @@ -70,4 +81,34 @@ public class HoursWorked extends BaseEntity { public void setTotalHours(final double totalHours) { this.totalHours = totalHours; } + + public LocalDate getFecha() { + return fecha; + } + + public void setFecha(final LocalDate fecha) { + this.fecha = fecha; + // Actualiza el número de semana automáticamente al establecer la fecha + if (fecha != null) { + this.weekNumber = calculateWeekNumber(fecha); + } + } + + // Método adicional para calcular el número de semana basado en la fecha + private int calculateWeekNumber(LocalDate date) { + WeekFields weekFields = WeekFields.of(Locale.getDefault()); + return date.get(weekFields.weekOfWeekBasedYear()); + } + + @Override + public String toString() { + return "HoursWorked{" + + "id=" + id + + ", employee=" + (employee != null ? employee.getFirstName() : "N/A") + + ", actividad=" + (actividad != null ? actividad.getNombre() : "N/A") + + ", weekNumber=" + weekNumber + + ", totalHours=" + totalHours + + ", fecha=" + fecha + + '}'; + } } diff --git a/src/main/java/com/primefactorsolutions/repositories/EmployeeRepository.java b/src/main/java/com/primefactorsolutions/repositories/EmployeeRepository.java index d57397f..3f09154 100644 --- a/src/main/java/com/primefactorsolutions/repositories/EmployeeRepository.java +++ b/src/main/java/com/primefactorsolutions/repositories/EmployeeRepository.java @@ -11,7 +11,7 @@ public interface EmployeeRepository extends JpaRepository { Optional findByUsername(String username); Optional findByPersonalEmail(String personalEmail); - Optional findByTeamId(UUID teamId); + Optional findByTeamIdAndLeadManagerTrue(UUID teamId); List findByTeamName(String teamName); } diff --git a/src/main/java/com/primefactorsolutions/service/EmployeeService.java b/src/main/java/com/primefactorsolutions/service/EmployeeService.java index 52c15dd..646278a 100644 --- a/src/main/java/com/primefactorsolutions/service/EmployeeService.java +++ b/src/main/java/com/primefactorsolutions/service/EmployeeService.java @@ -50,7 +50,7 @@ public class EmployeeService { public String getTeamLeadName(final UUID teamId) { // Encuentra al empleado con el rol de lead_manager en el equipo especificado - Optional leadManager = employeeRepository.findByTeamId(teamId); + Optional leadManager = employeeRepository.findByTeamIdAndLeadManagerTrue(teamId); return leadManager.map(employee -> employee.getFirstName() + " " + employee.getLastName()) .orElse("No asignado"); diff --git a/src/main/java/com/primefactorsolutions/views/EmployeeView.java b/src/main/java/com/primefactorsolutions/views/EmployeeView.java index 35b99ad..4cdbc08 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeeView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeeView.java @@ -176,6 +176,7 @@ public class EmployeeView extends BeanValidationForm implements HasUrl editButton.setVisible(true); reportButton.setVisible(true); birthday.addValueChangeListener(event -> calculateAge()); + birthday.setMax(java.time.LocalDate.now()); reportButton.addClickListener((ComponentEventListener>) buttonClickEvent -> { var employee = getEntity(); diff --git a/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java b/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java index 62ef1cb..6614aa2 100644 --- a/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java +++ b/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java @@ -305,7 +305,6 @@ public class HoursWorkedView extends VerticalLayout { 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); @@ -318,9 +317,8 @@ public class HoursWorkedView extends VerticalLayout { } actividades.add(nuevaActividad); } - - // Actualiza el Grid grid.setItems(actividades); + guardarActividades(); } @@ -437,22 +435,29 @@ public class HoursWorkedView extends VerticalLayout { return; } + if (actividades.isEmpty()) { + Notification.show("No hay actividades para guardar."); + return; + } + double totalHorasSemana = actividades.stream() .mapToDouble(this::calcularTotalPorDia) .sum(); - HoursWorked hoursWorked = new HoursWorked(); - hoursWorked.setEmployee(selectedEmployee); - hoursWorked.setWeekNumber(weekNumber); - hoursWorked.setTotalHours(totalHorasSemana); + List savedHoursWorked = new ArrayList<>(); // Para almacenar los objetos guardados - try { - hoursWorkedService.saveHoursWorked(hoursWorked); // Usa saveHoursWorked directamente - Notification.show("Actividades guardadas correctamente."); - System.out.println(hoursWorked); - } catch (Exception e) { - Notification.show("Error al guardar actividades: " + e.getMessage()); - } + actividades.forEach(actividad -> { + HoursWorked hoursWorked = new HoursWorked(); + hoursWorked.setActividad(actividad); + hoursWorked.setFecha(fechaPicker.getValue()); + hoursWorked.setEmployee(selectedEmployee); + hoursWorked.setWeekNumber(weekNumber); + hoursWorked.setTotalHours(totalHorasSemana); + + hoursWorkedService.saveHoursWorked(hoursWorked); + + }); + Notification.show("Actividades guardadas correctamente."); } private double calcularTotalHoras(final List listaDeHorasTrabajadas) { @@ -468,4 +473,5 @@ public class HoursWorkedView extends VerticalLayout { private void closeView() { getUI().ifPresent(ui -> ui.navigate(HoursWorkedView.class)); } + } \ No newline at end of file