2025-03-24 18:55:09 +00:00
|
|
|
package com.ieslamar.GestionInventario.controllers;
|
|
|
|
|
|
2025-04-11 15:54:32 +00:00
|
|
|
import com.ieslamar.GestionInventario.dto.BotonDTO;
|
2025-05-08 15:00:32 +00:00
|
|
|
import com.ieslamar.GestionInventario.entities.*;
|
|
|
|
|
import com.ieslamar.GestionInventario.repos.GenericEntityService;
|
|
|
|
|
import com.ieslamar.GestionInventario.services.CategoriaService;
|
2025-04-14 17:30:42 +00:00
|
|
|
import com.ieslamar.GestionInventario.services.DepartamentoService;
|
2025-03-26 14:54:51 +00:00
|
|
|
import com.ieslamar.GestionInventario.services.UserService;
|
2025-05-08 15:00:32 +00:00
|
|
|
import jakarta.persistence.Entity;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.context.ApplicationContext;
|
2025-04-14 17:00:10 +00:00
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
2025-03-24 18:55:09 +00:00
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
|
import org.springframework.ui.Model;
|
2025-05-08 15:00:32 +00:00
|
|
|
import org.springframework.web.bind.annotation.*;
|
2025-04-14 17:00:10 +00:00
|
|
|
import org.springframework.security.core.GrantedAuthority;
|
2025-04-15 15:32:13 +00:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2025-05-08 15:00:32 +00:00
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.util.*;
|
2025-04-14 17:00:10 +00:00
|
|
|
|
2025-03-24 18:55:09 +00:00
|
|
|
|
|
|
|
|
@Controller
|
|
|
|
|
public class UserController {
|
2025-05-08 15:00:32 +00:00
|
|
|
@Autowired
|
|
|
|
|
private ApplicationContext context;
|
2025-03-24 18:55:09 +00:00
|
|
|
|
|
|
|
|
private final UserService userService;
|
2025-04-14 17:30:42 +00:00
|
|
|
private final DepartamentoService departamentoService;
|
2025-05-08 15:00:32 +00:00
|
|
|
private final CategoriaService categoriaService;
|
2025-04-15 15:32:13 +00:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
|
2025-03-24 18:55:09 +00:00
|
|
|
|
2025-05-08 15:00:32 +00:00
|
|
|
public UserController(UserService userService, DepartamentoService departamentoService, CategoriaService categoriaService) {
|
2025-03-24 18:55:09 +00:00
|
|
|
this.userService = userService;
|
2025-04-14 17:30:42 +00:00
|
|
|
this.departamentoService = departamentoService;
|
2025-05-08 15:00:32 +00:00
|
|
|
this.categoriaService = categoriaService;
|
2025-03-24 18:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
2025-04-14 18:18:58 +00:00
|
|
|
private final BotonDTO gestionUsuarios = new BotonDTO("Gestión de usuarios", "/user_list", true, null);
|
2025-05-08 15:00:32 +00:00
|
|
|
// private final BotonDTO gestionDepartamentosTest = new BotonDTO("Gestión de departamentos generica", "/list/" + Categoria.class.getSimpleName(), true, null);
|
2025-04-14 17:00:10 +00:00
|
|
|
private final BotonDTO gestionProductos = new BotonDTO("Gestión de Productos", "/user_management", true, null);
|
2025-04-14 18:18:58 +00:00
|
|
|
private final BotonDTO gestionDepartamentos = new BotonDTO("Gestión de Departamentos", "/departamento_list", true, null);
|
2025-04-14 17:00:10 +00:00
|
|
|
private final BotonDTO gestionCategorias = new BotonDTO("Gestión de Categorías", "/user_management", true, null);
|
|
|
|
|
private final BotonDTO gestionUbicaciones = new BotonDTO("Gestión de Ubicaciones", "/user_management", true, null);
|
2025-05-08 15:00:32 +00:00
|
|
|
private final BotonDTO gestionInventario = new BotonDTO("Gestión de Inventario", "/inventario", true, null);
|
2025-04-14 17:00:10 +00:00
|
|
|
private final BotonDTO gestionTiposDatos = new BotonDTO("Gestión de Tipos de dato", "/user_management", true, null);
|
|
|
|
|
|
|
|
|
|
private final BotonDTO inventario = new BotonDTO("Inventario", "/management", false, null);
|
|
|
|
|
private final BotonDTO gestion = new BotonDTO("Gestión", "/management", true, null);
|
|
|
|
|
|
|
|
|
|
private final BotonDTO home = new BotonDTO("Salir", "/home", false, "button_salir");
|
|
|
|
|
|
2025-05-08 15:00:32 +00:00
|
|
|
private List<BotonDTO> filtrarBotones(Authentication auth, List<BotonDTO> botones) {
|
2025-04-14 17:00:10 +00:00
|
|
|
List<BotonDTO> botones_response = new ArrayList<>();
|
|
|
|
|
List<String> roles = auth.getAuthorities()
|
2025-05-08 15:00:32 +00:00
|
|
|
.stream()
|
|
|
|
|
.map(GrantedAuthority::getAuthority)
|
|
|
|
|
.toList();
|
2025-04-14 17:00:10 +00:00
|
|
|
if (roles.contains("ROLE_ADMIN")) {
|
|
|
|
|
return botones;
|
|
|
|
|
}
|
|
|
|
|
for (BotonDTO botone : botones) {
|
|
|
|
|
if (!botone.isRequireAdmin()) {
|
|
|
|
|
botones_response.add(botone);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return botones_response;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-24 18:55:09 +00:00
|
|
|
@GetMapping("/login")
|
|
|
|
|
public String loginPage() {
|
|
|
|
|
return "login"; // Retorna la vista login.html
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-14 17:00:10 +00:00
|
|
|
@GetMapping("/home")
|
|
|
|
|
public String homePage(Model model) {
|
|
|
|
|
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
2025-05-08 15:00:32 +00:00
|
|
|
List<BotonDTO> botones = filtrarBotones(auth, new ArrayList<>(List.of(
|
2025-04-14 17:00:10 +00:00
|
|
|
inventario,
|
|
|
|
|
gestion
|
|
|
|
|
)));
|
|
|
|
|
model.addAttribute("buttons", botones);
|
|
|
|
|
return "home";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/management")
|
2025-05-08 15:00:32 +00:00
|
|
|
public String managementPage(Model model) {
|
2025-04-14 17:00:10 +00:00
|
|
|
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
2025-05-08 15:00:32 +00:00
|
|
|
List<BotonDTO> botones = filtrarBotones(auth, new ArrayList<>(List.of(
|
2025-04-14 17:00:10 +00:00
|
|
|
gestionUsuarios,
|
|
|
|
|
gestionProductos,
|
|
|
|
|
gestionDepartamentos,
|
|
|
|
|
gestionCategorias,
|
|
|
|
|
gestionUbicaciones,
|
|
|
|
|
gestionInventario,
|
|
|
|
|
gestionCategorias,
|
|
|
|
|
gestionTiposDatos,
|
2025-05-08 15:00:32 +00:00
|
|
|
// gestionDepartamentosTest,
|
2025-04-14 17:00:10 +00:00
|
|
|
home
|
|
|
|
|
)));
|
|
|
|
|
model.addAttribute("buttons", botones);
|
|
|
|
|
return "management";
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-24 18:55:09 +00:00
|
|
|
|
2025-05-08 15:00:32 +00:00
|
|
|
//USERS--------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
2025-04-15 15:32:13 +00:00
|
|
|
@GetMapping("/userPage")
|
|
|
|
|
public String userPage(Model model) {
|
2025-05-08 15:00:32 +00:00
|
|
|
model.addAttribute("departamentos", departamentoService.getAllDepartamentos());
|
2025-04-15 15:32:13 +00:00
|
|
|
return "userPage"; // Retorna la vista register.html
|
2025-03-24 18:55:09 +00:00
|
|
|
}
|
2025-05-08 15:00:32 +00:00
|
|
|
@PostMapping("/userPage")
|
|
|
|
|
public String userPage(@RequestParam(required = false) Long id,
|
|
|
|
|
@RequestParam String username,
|
|
|
|
|
@RequestParam String password,
|
|
|
|
|
@RequestParam String role,
|
|
|
|
|
@RequestParam String mail,
|
|
|
|
|
@RequestParam(required = false) Integer iddepartamento) {
|
|
|
|
|
Departamento departamento = iddepartamento != null ? departamentoService.getDepartamentoById(iddepartamento) : null;
|
|
|
|
|
if (id != null) {
|
|
|
|
|
// Editar usuario existente
|
|
|
|
|
User user = userService.getUserById(id);
|
|
|
|
|
if (user != null) {
|
|
|
|
|
user.setNombre(username);
|
|
|
|
|
if (!password.isEmpty()) {
|
|
|
|
|
user.setPassword(password);
|
|
|
|
|
}
|
|
|
|
|
user.setRole(Role.valueOf(role));
|
|
|
|
|
user.setMail(mail);
|
|
|
|
|
user.setDepartamento(departamento);
|
|
|
|
|
userService.saveUser(user);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Crear nuevo usuario
|
2025-04-15 15:32:13 +00:00
|
|
|
userService.registerUser(username, password, role, mail, departamento);
|
2025-03-24 18:55:09 +00:00
|
|
|
}
|
2025-05-08 15:00:32 +00:00
|
|
|
return "redirect:/user_list";
|
|
|
|
|
}
|
2025-04-14 18:18:58 +00:00
|
|
|
@PostMapping("/delete_user")
|
|
|
|
|
public String deleteUser(@RequestParam("id") Long id) {
|
2025-04-07 18:32:56 +00:00
|
|
|
User user = userService.getUserById(id);
|
2025-04-14 18:18:58 +00:00
|
|
|
userService.deleteUser(user); // Llama al servicio para eliminar el usuario
|
|
|
|
|
return "redirect:/user_list"; // Redirige a la página de gestión de usuarios
|
2025-04-07 18:32:56 +00:00
|
|
|
}
|
2025-04-14 18:18:58 +00:00
|
|
|
@GetMapping("/user_list")
|
2025-04-30 17:57:10 +00:00
|
|
|
public String userList(Model model) {
|
|
|
|
|
return buildUserList(model, userService.getAllUsers());
|
|
|
|
|
}
|
|
|
|
|
private String buildUserList(Model model, List<User> users) {
|
2025-04-14 18:18:58 +00:00
|
|
|
model.addAttribute("title", "Gestión de usuarios");
|
2025-05-08 15:00:32 +00:00
|
|
|
model.addAttribute("headers", List.of("ID", "Nombre", "Rol", "Email", "Departamento"));
|
2025-04-30 17:57:10 +00:00
|
|
|
model.addAttribute("items", users.stream()
|
2025-05-08 15:00:32 +00:00
|
|
|
.map(user -> Map.of(
|
|
|
|
|
"id", user.getId(),
|
|
|
|
|
"values", List.of(
|
|
|
|
|
user.getId(),
|
|
|
|
|
user.getNombre(),
|
|
|
|
|
user.getRole(),
|
|
|
|
|
user.getMail(),
|
|
|
|
|
user.getDepartamento() != null ? user.getDepartamento().getNombre() : " "
|
|
|
|
|
)
|
|
|
|
|
))
|
|
|
|
|
.toList());
|
2025-04-14 17:30:42 +00:00
|
|
|
model.addAttribute("actionsUrl", Map.of(
|
2025-05-08 15:00:32 +00:00
|
|
|
"edit", "/edit_user",
|
|
|
|
|
"delete", "/delete_user"
|
2025-04-14 17:30:42 +00:00
|
|
|
));
|
|
|
|
|
model.addAttribute("backUrl", "/Inventario/home");
|
2025-04-15 15:32:13 +00:00
|
|
|
model.addAttribute("addUrl", "/Inventario/userPage");
|
2025-04-30 17:57:10 +00:00
|
|
|
|
2025-04-14 18:18:58 +00:00
|
|
|
return "list";
|
2025-04-14 17:30:42 +00:00
|
|
|
}
|
2025-05-08 15:00:32 +00:00
|
|
|
@GetMapping("/edit_user/{id}")
|
|
|
|
|
public String editUser(@PathVariable Long id, Model model) {
|
|
|
|
|
User user = userService.getUserById(id);
|
|
|
|
|
if (user != null) {
|
|
|
|
|
model.addAttribute("user", user);
|
|
|
|
|
model.addAttribute("departamentos", departamentoService.getAllDepartamentos());
|
|
|
|
|
}
|
|
|
|
|
return "userPage"; // Redirige a la página de registro
|
|
|
|
|
}
|
2025-04-14 17:30:42 +00:00
|
|
|
|
2025-04-15 15:32:13 +00:00
|
|
|
|
2025-05-08 15:00:32 +00:00
|
|
|
//DEPARTAMENTOS--------------------------------------------------------------------------------------------------------
|
2025-04-30 17:57:10 +00:00
|
|
|
|
2025-04-14 18:18:58 +00:00
|
|
|
@GetMapping("/departamento_list")
|
2025-04-14 17:30:42 +00:00
|
|
|
public String genericPageDepartamento(Model model) {
|
2025-04-15 15:32:13 +00:00
|
|
|
model.addAttribute("title", "Gestión de Departamentos");
|
2025-04-30 17:57:10 +00:00
|
|
|
model.addAttribute("headers", List.of("ID", "Nombre"));
|
2025-04-15 15:32:13 +00:00
|
|
|
model.addAttribute(
|
|
|
|
|
"items",
|
2025-05-08 15:00:32 +00:00
|
|
|
departamentoService.getAllDepartamentos().stream().map(departamento -> Map.of("id", departamento.getId(),
|
2025-04-15 15:32:13 +00:00
|
|
|
"values", List.of(departamento.getId(), departamento.getNombre()))).toList()
|
|
|
|
|
);
|
2025-04-14 17:30:42 +00:00
|
|
|
model.addAttribute("actionsUrl", Map.of(
|
2025-05-08 15:00:32 +00:00
|
|
|
"edit", "",
|
|
|
|
|
"delete", ""
|
2025-04-14 17:30:42 +00:00
|
|
|
));
|
|
|
|
|
model.addAttribute("backUrl", "/Inventario/home");
|
2025-04-15 15:32:13 +00:00
|
|
|
model.addAttribute("addUrl", "/Inventario/");
|
2025-04-14 18:18:58 +00:00
|
|
|
return "list";
|
2025-04-14 17:30:42 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-08 15:00:32 +00:00
|
|
|
//CATEGORIAS--------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
private String buildCategoriaList(Model model, List<Categoria> categorias) {
|
|
|
|
|
model.addAttribute("title", "Gestión de categorías");
|
|
|
|
|
model.addAttribute("headers", List.of("ID", "Nombre"));
|
|
|
|
|
model.addAttribute("items", categorias.stream()
|
|
|
|
|
.map(categoria -> Map.of(
|
|
|
|
|
"id", categoria.getId(),
|
|
|
|
|
"values", List.of(
|
|
|
|
|
categoria.getId(),
|
|
|
|
|
categoria.getNombre()
|
|
|
|
|
)
|
|
|
|
|
))
|
|
|
|
|
.toList());
|
|
|
|
|
model.addAttribute("actionsUrl", Map.of(
|
|
|
|
|
"edit", "",
|
|
|
|
|
"delete", "/delete_user"
|
|
|
|
|
));
|
|
|
|
|
model.addAttribute("backUrl", "/Inventario/home");
|
|
|
|
|
model.addAttribute("addUrl", "/Inventario/userPage");
|
|
|
|
|
|
|
|
|
|
return "list";
|
|
|
|
|
}
|
2025-04-14 17:30:42 +00:00
|
|
|
|
2025-05-08 15:00:32 +00:00
|
|
|
|
|
|
|
|
//GENERICO--------------------------------------------------------------------------------------------------------
|
|
|
|
|
/**
|
|
|
|
|
@GetMapping("/list/{tabla}")
|
|
|
|
|
public String tablaList(Model model, @PathVariable String entity) {
|
|
|
|
|
String serviceName = entity.toLowerCase() + "Service";
|
|
|
|
|
Object serviceBean = context.getBean(serviceName);
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("/list/{entity}")
|
|
|
|
|
public String entityList(Model model, @PathVariable String entity) {
|
|
|
|
|
//construye el nombre del servicio de la entidad
|
|
|
|
|
String serviceName = entity.toLowerCase() + "Service";
|
|
|
|
|
try {
|
|
|
|
|
//busca el servicio de la entidad
|
|
|
|
|
Object serviceBean = context.getBean(serviceName);
|
|
|
|
|
//si el Bean es una instancia de GenericEntityService
|
|
|
|
|
if (serviceBean instanceof GenericEntityService<?> genericService) {
|
|
|
|
|
//genericService es un Servicio de los que hay, eso trae todas las instancias, entities sería por ejemplo user,categoria,etc
|
|
|
|
|
List<?> entities = genericService.findAll();
|
|
|
|
|
|
|
|
|
|
List<Map<String, Object>> items = entities.stream().map(e ->
|
|
|
|
|
Map.of(
|
|
|
|
|
"id", getId(e),
|
|
|
|
|
"values", genericService.getRowValues(e)
|
|
|
|
|
)
|
|
|
|
|
).toList();
|
|
|
|
|
|
|
|
|
|
model.addAttribute("title", "Gestión de " + entity);
|
|
|
|
|
model.addAttribute("headers", genericService.getHeaders());
|
|
|
|
|
model.addAttribute("items", items);
|
|
|
|
|
model.addAttribute("actionsUrl", Map.of(
|
|
|
|
|
"edit", "",
|
|
|
|
|
"delete", "" // puedes personalizar por entidad
|
|
|
|
|
));
|
|
|
|
|
model.addAttribute("backUrl", "/Inventario/home");
|
|
|
|
|
model.addAttribute("addUrl", "/Inventario/");
|
|
|
|
|
|
|
|
|
|
return "list";
|
|
|
|
|
} else {
|
|
|
|
|
throw new IllegalArgumentException("No es un GenericEntityService válido");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
model.addAttribute("error", "No se pudo cargar la entidad: " + entity);
|
|
|
|
|
return "error";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Object getId(Object entity) {
|
|
|
|
|
try {
|
|
|
|
|
Method method = entity.getClass().getMethod("getId");
|
|
|
|
|
return method.invoke(entity);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
**/
|
2025-03-24 18:55:09 +00:00
|
|
|
}
|
2025-05-08 15:00:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|