From 5f569333bae6ef76fdc116f7e8ecd37dbc7645b9 Mon Sep 17 00:00:00 2001 From: Melina Gutierrez Date: Wed, 13 Nov 2024 15:41:30 -0400 Subject: [PATCH] "primer commit" --- .../model/ActividadesHours.java | 81 +++++++++++++++++++ .../model/HoursWorked.java | 60 ++++++++++---- .../ActividadesHoursRepository.java | 6 ++ .../repositories/EmployeeRepository.java | 2 +- .../service/EmployeeService.java | 2 +- .../views/HoursWorkedView.java | 72 ++++++++++++++--- 6 files changed, 196 insertions(+), 27 deletions(-) create mode 100644 src/main/java/com/primefactorsolutions/model/ActividadesHours.java create mode 100644 src/main/java/com/primefactorsolutions/repositories/ActividadesHoursRepository.java diff --git a/src/main/java/com/primefactorsolutions/model/ActividadesHours.java b/src/main/java/com/primefactorsolutions/model/ActividadesHours.java new file mode 100644 index 0000000..5a93ec4 --- /dev/null +++ b/src/main/java/com/primefactorsolutions/model/ActividadesHours.java @@ -0,0 +1,81 @@ +package com.primefactorsolutions.model; + +import jakarta.persistence.*; +import java.time.LocalDate; +import java.util.UUID; + +@Entity +@Table(name = "Actividades_hours") +public class ActividadesHours { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private UUID id; + + @ManyToOne + @JoinColumn(name = "employee_id", nullable = false) + private Employee employee; + + @ManyToOne + @JoinColumn(name = "actividad_id", nullable = false) + private Actividad actividad; + + @Column(nullable = false) + private double totalHours; + + @Column(nullable = false) + private int weekNumber; + + @Column(nullable = false) + private LocalDate fecha; + + // Getters y Setters + + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + public Employee getEmployee() { + return employee; + } + + public void setEmployee(Employee employee) { + this.employee = employee; + } + + public Actividad getActividad() { + return actividad; + } + + public void setActividad(Actividad actividad) { + this.actividad = actividad; + } + + public double getTotalHours() { + return totalHours; + } + + public void setTotalHours(double totalHours) { + this.totalHours = totalHours; + } + + public int getWeekNumber() { + return weekNumber; + } + + public void setWeekNumber(int weekNumber) { + this.weekNumber = weekNumber; + } + + public LocalDate getFecha() { + return fecha; + } + + public void setFecha(LocalDate fecha) { + this.fecha = fecha; + } +} diff --git a/src/main/java/com/primefactorsolutions/model/HoursWorked.java b/src/main/java/com/primefactorsolutions/model/HoursWorked.java index e23e9b0..0c2b9ad 100644 --- a/src/main/java/com/primefactorsolutions/model/HoursWorked.java +++ b/src/main/java/com/primefactorsolutions/model/HoursWorked.java @@ -5,8 +5,11 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; +import jakarta.persistence.CascadeType; import java.time.LocalDate; +import java.time.temporal.WeekFields; +import java.util.Locale; import java.util.UUID; @Entity @@ -18,25 +21,16 @@ 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 Actividad getActividad() { - return this.actividad; - } - - public void setActividad(Actividad actividad){ - this.actividad = actividad; - } - + // Getters y Setters public UUID getId() { return id; } @@ -49,8 +43,16 @@ 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 Actividad getActividad() { + return actividad; + } + + public void setActividad(final Actividad actividad) { + this.actividad = actividad; } public int getWeekNumber() { @@ -61,8 +63,6 @@ public class HoursWorked extends BaseEntity { this.weekNumber = weekNumber; } - public LocalDate getFecha() { return this.fecha;} - public double getTotalHours() { return totalHours; } @@ -70,4 +70,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/ActividadesHoursRepository.java b/src/main/java/com/primefactorsolutions/repositories/ActividadesHoursRepository.java new file mode 100644 index 0000000..40a0d84 --- /dev/null +++ b/src/main/java/com/primefactorsolutions/repositories/ActividadesHoursRepository.java @@ -0,0 +1,6 @@ +package com.primefactorsolutions.repositories; +import com.primefactorsolutions.model.ActividadesHours; +import org.springframework.data.jpa.repository.JpaRepository; +import java.util.UUID; +public interface ActividadesHoursRepository extends JpaRepository { +} 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/HoursWorkedView.java b/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java index 62ef1cb..33c2930 100644 --- a/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java +++ b/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java @@ -2,6 +2,7 @@ package com.primefactorsolutions.views; import com.primefactorsolutions.model.Actividad; import com.primefactorsolutions.model.Employee; import com.primefactorsolutions.model.HoursWorked; +import com.primefactorsolutions.repositories.ActividadesHoursRepository; import com.primefactorsolutions.service.EmployeeService; import com.primefactorsolutions.service.HoursWorkedService; import com.vaadin.flow.component.grid.Grid; @@ -58,6 +59,11 @@ public class HoursWorkedView extends VerticalLayout { @Autowired private final HoursWorkedService hoursWorkedService; + + @Autowired + private ActividadesHoursRepository actividadesHoursRepository; + + private final H2 equipoLabel = new H2("Tareas del Cliente/Equipo"); private final H2 empresaLabel = new H2("Tareas de la Empresa"); private final Label totalCompletadoLabel = new Label(); @@ -186,6 +192,7 @@ public class HoursWorkedView extends VerticalLayout { add(fechaPicker, filtersLayout, actividadFormLayout, equipoLabel, grid, empresaLabel, tareasEspecificasLayout, gridActividadesEspecificas, buttonsLayout, totalesLayout); + } private void configurarGrid() { @@ -437,24 +444,69 @@ public class HoursWorkedView extends VerticalLayout { return; } + // Verificar que hay actividades para guardar + if (actividades.isEmpty()) { + Notification.show("No hay actividades para guardar."); + return; + } + + // Sumar el total de horas para la semana double totalHorasSemana = actividades.stream() .mapToDouble(this::calcularTotalPorDia) .sum(); - HoursWorked hoursWorked = new HoursWorked(); - hoursWorked.setEmployee(selectedEmployee); - hoursWorked.setWeekNumber(weekNumber); - hoursWorked.setTotalHours(totalHorasSemana); + // Crear un objeto HoursWorked por cada actividad + 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()); + // Recorrer todas las actividades y guardar un registro para cada una + for (Actividad actividad : actividades) { + HoursWorked hoursWorked = new HoursWorked(); + hoursWorked.setEmployee(selectedEmployee); + hoursWorked.setWeekNumber(weekNumber); + hoursWorked.setTotalHours(totalHorasSemana); + + // Asignar cada actividad individualmente + hoursWorked.setActividad(actividad); + hoursWorked.setFecha(LocalDate.now()); // Establecer la fecha actual o la seleccionada si es otra + + // Mostrar información de la actividad para depuración + System.out.println("Guardando actividad: " + actividad.getNombre() + " con " + totalHorasSemana + " horas."); + + try { + // Guardar en el servicio + hoursWorkedService.saveHoursWorked(hoursWorked); + savedHoursWorked.add(hoursWorked); // Agregar a la lista de registros guardados + Notification.show("Actividad guardada correctamente: " + actividad.getNombre()); + } catch (Exception e) { + Notification.show("Error al guardar la actividad: " + actividad.getNombre() + " - " + e.getMessage()); + } } + + // Mostrar los registros guardados + savedHoursWorked.forEach(hw -> System.out.println("Horas trabajadas guardadas: " + hw)); + + // Crear el Grid + Grid grid = new Grid<>(); + +// Configurar columnas para mostrar la información deseada + grid.addColumn(hw -> hw.getEmployee().getFirstName()).setHeader("Empleado"); + grid.addColumn(hw -> hw.getEmployee().getId()).setHeader("ID Empleado"); + grid.addColumn(hw -> hw.getActividad().getNombre()).setHeader("Actividad"); + grid.addColumn(hw -> hw.getTotalHours()).setHeader("Horas Trabajadas"); + grid.addColumn(hw -> hw.getWeekNumber()).setHeader("Número de Semana"); + grid.addColumn(hw -> hw.getFecha().toString()).setHeader("Fecha"); + +// Llenar el Grid con los datos de savedHoursWorked + grid.setItems(savedHoursWorked); + +// Agregar el Grid a la vista + add(grid); + } + + + private double calcularTotalHoras(final List listaDeHorasTrabajadas) { return listaDeHorasTrabajadas.stream() .mapToDouble(HoursWorked::getTotalHours)