Trabajando a crear una lista mejor, implementado un buscador, y oredenacion por columnas. Empezando hacer la paginacion

master
vicsash 5 months ago
parent bfe2ce00ab
commit cf90ff5a99

@ -96,16 +96,16 @@ public class DatabaseTest {
); );
ofertaRepository.save(oferta); ofertaRepository.save(oferta);
} }
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); // JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("CREATE FULLTEXT INDEX keywords_index ON empresas(keywords,nombre)"); // jdbcTemplate.execute("CREATE FULLTEXT INDEX keywords_index ON empresas(keywords,nombre)");
jdbcTemplate.execute("CREATE FULLTEXT INDEX keywords_index ON alumnos(keywords,nombre,apellido,apellido2,dni,nia,correo,correo2,nacionalidad,genero)"); // jdbcTemplate.execute("CREATE FULLTEXT INDEX keywords_index ON alumnos(keywords,nombre,apellido,apellido2,dni,nia,correo,correo2,nacionalidad,genero)");
jdbcTemplate.execute("CREATE FULLTEXT INDEX keywords_index ON ofertas(nombre,fecha)"); // jdbcTemplate.execute("CREATE FULLTEXT INDEX keywords_index ON ofertas(nombre,fecha)");
jdbcTemplate.execute("CREATE FULLTEXT INDEX keywords_index ON sucursales(nombre,localidad,direccion)"); // jdbcTemplate.execute("CREATE FULLTEXT INDEX keywords_index ON sucursales(nombre,localidad,direccion)");
jdbcTemplate.execute("CREATE FULLTEXT INDEX keywords_index ON sectores(nombre)"); // jdbcTemplate.execute("CREATE FULLTEXT INDEX keywords_index ON sectores(nombre)");
jdbcTemplate.execute("CREATE FULLTEXT INDEX keywords_index ON ciclos(nombre)"); // jdbcTemplate.execute("CREATE FULLTEXT INDEX keywords_index ON ciclos(nombre)");
jdbcTemplate.execute("CREATE FULLTEXT INDEX keywords_index ON familias(nombre)"); // jdbcTemplate.execute("CREATE FULLTEXT INDEX keywords_index ON familias(nombre)");
jdbcTemplate.execute("CREATE FULLTEXT INDEX keywords_index ON skills(nombre)"); // jdbcTemplate.execute("CREATE FULLTEXT INDEX keywords_index ON skills(nombre)");
jdbcTemplate.execute("CREATE FULLTEXT INDEX keywords_index ON contactos(nombre,apellido,apellido2,correo,telefono)"); // jdbcTemplate.execute("CREATE FULLTEXT INDEX keywords_index ON contactos(nombre,apellido,apellido2,correo,telefono)");
}; };
} }

@ -47,106 +47,108 @@ public class BuscadorController {
String[] word = query.split("\\b(y|o)\\b|[,/]"); String[] word = query.split("\\b(y|o)\\b|[,/]");
if (searchOption.equals("OptEmpresa")) { if (searchOption.equals("OptEmpresa")) {
if(word.length == 1 && word[0].equalsIgnoreCase("all") || word[0].equalsIgnoreCase("todas")){ if(word.length == 1 && word[0].equalsIgnoreCase("all") || word[0].equalsIgnoreCase("todas")){
Set<Empresa> empresas = new HashSet<>(empressaRepository.findAll()); ArrayList<Empresa> empresas = new ArrayList<>(empressaRepository.findAllAsc());
model.addAttribute("empresas", empresas); model.addAttribute("empresas", empresas);
return "/list/list_empresas"; return "/list/empresas";
} }
System.out.println("Buscando empresas"); // if(word.length == 1){
if(word.length == 1){ // ArrayList<Empresa> empresas = new ArrayList<>(empressaRepository.getEmpressaByKeywordsOrName(word[0]));
Set<Empresa> empresas = new HashSet<>(empressaRepository.getEmpressaByKeywordsOrName(word[0])); // model.addAttribute("empresas", empresas);
model.addAttribute("empresas", empresas); // return "/list/empresas";
return "/list/list_empresas"; // }else if(word.length == 2){
}else if(word.length == 2){ // ArrayList<Empresa> empresas = new ArrayList<>(empressaRepository.getEmpressaByKeywordsOrName(word[0]));
Set<Empresa> empresas = new HashSet<>(empressaRepository.getEmpressaByKeywordsOrName(word[0])); // empresas.addAll(empressaRepository.getEmpressaByKeywordsOrName(word[1]));
empresas.addAll(empressaRepository.getEmpressaByKeywordsOrName(word[1])); // model.addAttribute("empresas", empresas);
model.addAttribute("empresas", empresas); // return "/list/empresas";
return "/list/list_empresas"; // }
}
} else if (searchOption.equals("OptAlumno")) { } else if (searchOption.equals("OptAlumno")) {
if (word.length == 1 && word[0].equalsIgnoreCase("all") || word[0].equalsIgnoreCase("todos")) { if (word.length == 1 && word[0].equalsIgnoreCase("all") || word[0].equalsIgnoreCase("todos")) {
Set<Alumno> alumnos = new HashSet<>(alumnoRepository.findAll()); ArrayList<Alumno> alumnos = new ArrayList<>(alumnoRepository.findAll());
model.addAttribute("alumnos", alumnos); model.addAttribute("alumnos", alumnos);
return "/list/list_alumnos"; return "/list/alumnos";
} }
if (word.length == 1) { // if (word.length == 1) {
System.out.println("Buscando alumnos"); // System.out.println("Buscando alumnos");
Set<Alumno> alumnos = new HashSet<>(alumnoRepository.getAlumnoByKeywordsOrName(word[0])); // ArrayList<Alumno> alumnos = new ArrayList<>(alumnoRepository.getAlumnoByKeywordsOrName(word[0]));
model.addAttribute("alumnos", alumnos); // model.addAttribute("alumnos", alumnos);
return "/list/list_alumnos"; // return "/list/alumnos";
} else if (word.length == 2) { // } else if (word.length == 2) {
System.out.println("Buscando alumnos"); // System.out.println("Buscando alumnos");
Set<Alumno> alumnos = new HashSet<>(alumnoRepository.getAlumnoByKeywordsOrName(word[0])); // ArrayList<Alumno> alumnos = new ArrayList<>(alumnoRepository.getAlumnoByKeywordsOrName(word[0]));
alumnos.addAll(alumnoRepository.getAlumnoByKeywordsOrName(word[1])); // alumnos.addAll(alumnoRepository.getAlumnoByKeywordsOrName(word[1]));
model.addAttribute("alumnos", alumnos); // model.addAttribute("alumnos", alumnos);
return "/list/list_alumnos"; // return "/list/alumnos";
} // }
}else if(searchOption.equals("OptOferta")) { }else if(searchOption.equals("OptOferta")) {
if (word.length == 1 && word[0].equalsIgnoreCase("all") || word[0].equalsIgnoreCase("todas")) { if (word.length == 1 && word[0].equalsIgnoreCase("all") || word[0].equalsIgnoreCase("todas")) {
Set<Oferta> ofertas = new HashSet<>(ofertaRepository.findAll()); ArrayList<Oferta> ofertas = new ArrayList<>(ofertaRepository.findAll());
model.addAttribute("ofertas", ofertas);
return "/list/list_ofertas";
}
if (word.length == 1) {
Set<Oferta> ofertas = new HashSet<>(ofertaRepository.getOfertaFullTextSeach(word[0]));
model.addAttribute("ofertas", ofertas);
return "/list/list_ofertas";
} else if (word.length == 2) {
Set<Oferta> ofertas = new HashSet<>(ofertaRepository.getOfertaFullTextSeach(word[0]));
ofertas.addAll(ofertaRepository.getOfertaFullTextSeach(word[1]));
model.addAttribute("ofertas", ofertas); model.addAttribute("ofertas", ofertas);
return "/list/list_ofertas"; return "/list/ofertas";
} }
// if (word.length == 1) {
// ArrayList<Oferta> ofertas = new ArrayList<>(ofertaRepository.getOfertaFullTextSeach(word[0]));
// model.addAttribute("ofertas", ofertas);
// return "/list/ofertas";
// } else if (word.length == 2) {
// ArrayList<Oferta> ofertas = new ArrayList<>(ofertaRepository.getOfertaFullTextSeach(word[0]));
// ofertas.addAll(ofertaRepository.getOfertaFullTextSeach(word[1]));
// model.addAttribute("ofertas", ofertas);
// return "/list/ofertas";
// }
} else if (searchOption.equals("OptSector")) { } else if (searchOption.equals("OptSector")) {
if (word.length == 1 && word[0].equalsIgnoreCase("all") || word[0].equalsIgnoreCase("todas")) { if (word.length == 1 && word[0].equalsIgnoreCase("all") || word[0].equalsIgnoreCase("todas")) {
Set<Sector> sectores = new HashSet<>(sectorRepository.findAll()); ArrayList<Sector> sectores = new ArrayList<>(sectorRepository.findAll());
model.addAttribute("sectores", sectores); model.addAttribute("sectores", sectores);
return "/list/list_sectores"; return "/list/sectores";
} else { } else {
Sector sector = sectorRepository.findByNombre(word[0]); Sector sector = sectorRepository.findByNombre(word[0]);
model.addAttribute("sectores", sector); model.addAttribute("sectores", sector);
return "/list/list_sectores"; return "/list/sectores";
} }
}else if(searchOption.equals("OptSucursal")) { }else if(searchOption.equals("OptSucursal")) {
if (word.length == 1 && word[0].equalsIgnoreCase("all") || word[0].equalsIgnoreCase("todas")) { if (word.length == 1 && word[0].equalsIgnoreCase("all") || word[0].equalsIgnoreCase("todas")) {
Set<Sucursal> sucursalSet = new HashSet<>(sucursalRepository.findAll()); ArrayList<Sucursal> sucursalSet = new ArrayList<>(sucursalRepository.findAll());
model.addAttribute("sucursales", sucursalSet);
return "/list/list_sucursales";
} else {
Set<Sucursal> sucursalSet = new HashSet<>(sucursalRepository.getSucursalFullTextSeach(word[0]));
model.addAttribute("sucursales", sucursalSet); model.addAttribute("sucursales", sucursalSet);
return "/list/list_sucursales"; return "/list/sucursales";
} }
// } else {
// ArrayList<Sucursal> sucursalSet = new ArrayList<>(sucursalRepository.getSucursalFullTextSeach(word[0]));
// model.addAttribute("sucursales", sucursalSet);
// return "/list/sucursales";
// }
}else if(searchOption.equals("OptSkill")){ }else if(searchOption.equals("OptSkill")){
if (word.length == 1 && word[0].equalsIgnoreCase("all") || word[0].equalsIgnoreCase("todas")) { if (word.length == 1 && word[0].equalsIgnoreCase("all") || word[0].equalsIgnoreCase("todas")) {
Set<Skill> skills = new HashSet<>(skillRepository.findAll()); ArrayList<Skill> skills = new ArrayList<>(skillRepository.findAll());
model.addAttribute("skills", skills); model.addAttribute("skills", skills);
return "/list/list_skills"; return "/list/skills";
}else{
Set<Skill> skills = new HashSet<>(skillRepository.getSkillFullTextSeach(word[0]));
model.addAttribute("skills", skills);
return "/list/list_skills";
} }
// else{
// ArrayList<Skill> skills = new ArrayList<>(skillRepository.getSkillFullTextSeach(word[0]));
// model.addAttribute("skills", skills);
// return "/list/skills";
// }
}else if(searchOption.equals("OptContactos")) { }else if(searchOption.equals("OptContactos")) {
if (word.length == 1 && word[0].equalsIgnoreCase("all") || word[0].equalsIgnoreCase("todas")) { if (word.length == 1 && word[0].equalsIgnoreCase("all") || word[0].equalsIgnoreCase("todas")) {
Set<Contacto> contactos = new HashSet<>(contactoRepository.findAll()); ArrayList<Contacto> contactos = new ArrayList<>(contactoRepository.findAll());
model.addAttribute("contactos", contactos);
return "/list/list_contactos";
} else {
Set<Contacto> contactos = new HashSet<>(contactoRepository.getContactoFullTextSeach(word[0]));
model.addAttribute("contactos", contactos); model.addAttribute("contactos", contactos);
return "/list/list_contactos"; return "/list/contactos";
} }
// else {
// ArrayList<Contacto> contactos = new ArrayList<>(contactoRepository.getContactoFullTextSeach(word[0]));
// model.addAttribute("contactos", contactos);
// return "/list/contactos";
// }
}else if(searchOption.equals("OptFamilias")) { }else if(searchOption.equals("OptFamilias")) {
if (word.length == 1 && word[0].equalsIgnoreCase("all") || word[0].equalsIgnoreCase("todas")) { if (word.length == 1 && word[0].equalsIgnoreCase("all") || word[0].equalsIgnoreCase("todas")) {
Set<Familia> familias = new HashSet<>(familiaRepository.findAll()); ArrayList<Familia> familias = new ArrayList<>(familiaRepository.findAll());
model.addAttribute("familias", familias); model.addAttribute("familias", familias);
return "/list/list_familias"; return "/list/familias";
} }
}else if(searchOption.equals("OptCiclos")){ }else if(searchOption.equals("OptCiclos")){
if (word.length == 1 && word[0].equalsIgnoreCase("all") || word[0].equalsIgnoreCase("todas")) { if (word.length == 1 && word[0].equalsIgnoreCase("all") || word[0].equalsIgnoreCase("todas")) {
Set<Ciclo> ciclos = new HashSet<>(cicloRepository.findAll()); ArrayList<Ciclo> ciclos = new ArrayList<>(cicloRepository.findAll());
model.addAttribute("ciclos", ciclos); model.addAttribute("ciclos", ciclos);
return "/list/list_ciclos"; return "/list/ciclos";
} }
}else{ }else{
System.out.println("ERROR EN BUSQUEDA DE UNA PALABRA"); System.out.println("ERROR EN BUSQUEDA DE UNA PALABRA");

@ -2,6 +2,7 @@ package com.example.proyectofinal.controllers.modelControllers;
import com.example.proyectofinal.models.empresas.Contacto; import com.example.proyectofinal.models.empresas.Contacto;
import com.example.proyectofinal.repositories.empresas.ContactoRepository; import com.example.proyectofinal.repositories.empresas.ContactoRepository;
import com.example.proyectofinal.repositories.empresas.EmpressaRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
@ -13,15 +14,17 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
@Controller @Controller
@RequestMapping("/empresa") @RequestMapping("/empresas")
public class EmpressaController { public class EmpressaController {
@Autowired @Autowired
private ContactoRepository contactoRepository; private ContactoRepository contactoRepository;
@Autowired
private EmpressaRepository empressaRepository;
@GetMapping("/{id}") @GetMapping("/{id}")
public String getEmpressaBySector(@PathVariable Long id, Model model) { public String getEmpressaBySector(@PathVariable Long id, Model model) {
Set<Contacto> contactoSet = new HashSet<>(contactoRepository.findBySector(id)); Set<Contacto> contactoSet = new HashSet<>(contactoRepository.findBySector(id));
model.addAttribute("contactos", contactoSet); model.addAttribute("contactos", contactoSet);
return "/list/list_contactos"; return "contactos";
} }
} }

@ -39,6 +39,6 @@ public class FamiliaController {
public String getFamiliaById(@PathVariable Long id, Model model) { public String getFamiliaById(@PathVariable Long id, Model model) {
Set<Ciclo> cicloSet = new HashSet<>(cicloRepository.findCicloByFamiliaId(id)); Set<Ciclo> cicloSet = new HashSet<>(cicloRepository.findCicloByFamiliaId(id));
model.addAttribute("ciclos", cicloSet); model.addAttribute("ciclos", cicloSet);
return "/list/list_ciclo"; return "ciclo";
} }
} }

@ -22,6 +22,6 @@ public class SectorController {
public String getEmpressaBySector(@PathVariable Long id, Model model) { public String getEmpressaBySector(@PathVariable Long id, Model model) {
Set<Empresa> empresaSet = new HashSet<>(empressaRepository.findBySector(id)); Set<Empresa> empresaSet = new HashSet<>(empressaRepository.findBySector(id));
model.addAttribute("empresas", empresaSet); model.addAttribute("empresas", empresaSet);
return "/list/list_empresas"; return "empresas";
} }
} }

@ -37,7 +37,7 @@ public class Empresa {
@Column(length = 2500) @Column(length = 2500)
private String keywords; private String keywords;
@ManyToOne(cascade = {CascadeType.PERSIST,CascadeType.MERGE, CascadeType.REMOVE,CascadeType.REFRESH},fetch = FetchType.LAZY) @ManyToOne(cascade = {CascadeType.PERSIST,CascadeType.MERGE, CascadeType.REMOVE,CascadeType.REFRESH},fetch = FetchType.EAGER)
@JoinColumn(name = "Sector_id",referencedColumnName = "id") @JoinColumn(name = "Sector_id",referencedColumnName = "id")
private Sector sector; private Sector sector;

@ -2,11 +2,12 @@ package com.example.proyectofinal.repositories.empresas;
import com.example.proyectofinal.models.empresas.Empresa; import com.example.proyectofinal.models.empresas.Empresa;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
public interface EmpressaRepository extends JpaRepository<Empresa, Long> { public interface EmpressaRepository extends JpaRepository<Empresa, Long>, JpaSpecificationExecutor<Empresa> {
//there is value = and nativeQuery = true so the query is written in SQL refering to the table in the database //there is value = and nativeQuery = true so the query is written in SQL refering to the table in the database
@ -16,4 +17,9 @@ public interface EmpressaRepository extends JpaRepository<Empresa, Long> {
@Query(value = "SELECT * FROM empresas u WHERE u.sector_id = ?1", nativeQuery = true) @Query(value = "SELECT * FROM empresas u WHERE u.sector_id = ?1", nativeQuery = true)
public ArrayList<Empresa> findBySector(Long id); public ArrayList<Empresa> findBySector(Long id);
@Query(value = "SELECT * FROM empresas order by id Asc", nativeQuery = true)
List<Empresa> findAllAsc();
} }

@ -0,0 +1,64 @@
function myFunction() {
var input, filter, table, tbody, tr, td, i, j, txtValue;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("table");
tbody = table.getElementsByTagName("tbody")[0];
tr = tbody.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
for (j = 0; j < tr[i].cells.length; j++) {
td = tr[i].cells[j];
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
break;
} else {
tr[i].style.display = "none";
}
}
}
}
}
function sortTableCol(n) {
var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
table = document.getElementById("table");
switching = true;
dir = "asc";
while (switching) {
switching = false;
rows = table.rows;
for (i = 1; i < (rows.length - 1); i++) {
shouldSwitch = false;
x = rows[i].getElementsByTagName("TD")[n].innerHTML.trim();
y = rows[i + 1].getElementsByTagName("TD")[n].innerHTML.trim();
var xIsNumber = !isNaN(parseFloat(x)) && isFinite(x);
var yIsNumber = !isNaN(parseFloat(y)) && isFinite(y);
if (dir == "asc") {
if ((xIsNumber && yIsNumber && parseFloat(x) > parseFloat(y)) ||
(!xIsNumber && !yIsNumber && x.localeCompare(y) > 0)) {
shouldSwitch = true;
break;
}
} else if (dir == "desc") {
if ((xIsNumber && yIsNumber && parseFloat(x) < parseFloat(y)) ||
(!xIsNumber && !yIsNumber && x.localeCompare(y) < 0)) {
shouldSwitch = true;
break;
}
}
}
if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
switchcount++;
} else {
if (switchcount === 0 && dir === "asc") {
dir = "desc";
switching = true;
}
}
}
}

@ -25,12 +25,11 @@
width: 40px; width: 40px;
height: 40px; height: 40px;
} }
th { th {
background-color: #f2f2f2; /* Change this to your preferred color */ background-color: #f2f2f2;
color: black; /* Change this to your preferred color */ color: black;
padding: 10px; padding: 10px;
border: 1px solid #ddd; /* Adds border to the table headers */ border: 1px solid #ddd;
} }
h1 { h1 {
@ -49,7 +48,6 @@
width: 100%; width: 100%;
position: relative; position: relative;
} }
.table { .table {
padding-top: 5px; padding-top: 5px;
width: 100%; width: 100%;
@ -59,21 +57,17 @@
.cell{ .cell{
height: 10px; height: 10px;
} }
@media screen and (max-width: 600px) { @media screen and (max-width: 600px) {
.table { .table {
} }
} }
.table tr { .table tr {
height: 10%; height: 10%;
} }
body { body {
margin: 0; margin: 0;
padding: 0px; padding: 0px;
} }
.header { .header {
display: flex; display: flex;
align-items: center; align-items: center;
@ -82,19 +76,15 @@
padding: 0; padding: 0;
box-sizing: border-box; box-sizing: border-box;
} }
.header h1 { .header h1 {
margin: 0; margin: 0;
} }
.header button { .header button {
margin-right: auto; margin-right: auto;
} }
.logout-button { .logout-button {
position: relative; position: relative;
} }
.logout-button { .logout-button {
position: relative; position: relative;
} }
@ -110,17 +100,21 @@
<h1>Listado de Empresas</h1> <h1>Listado de Empresas</h1>
<a href="/logout" class="logout-button"><i class="fas fa-door-open"></i></a> <a href="/logout" class="logout-button"><i class="fas fa-door-open"></i></a>
</div> </div>
<input type="text" id="myInput" placeholder="Buscar por....">
<div class="table-container"> <div class="table-container">
<table class="table"> <table class="table" id="table">
<thead class="thread-light"> <thead class="thread-light">
<tr> <tr>
<th>Id</th> <th onclick="sortTableCol(0)">Id</th>
<th>Nombre</th> <th onclick="sortTableCol(1)">Nombre</th>
<th>Cif</th> <th onclick="sortTableCol(2)">Cif</th>
<th>Correo</th> <th onclick="sortTableCol(3)">Correo</th>
<th>Telefono</th> <th onclick="sortTableCol(4)">Telefono</th>
<th class="keywords-cell">Keywords</th> <th onclick="sortTableCol(5)">Keywords</th>
<th>Sector</th> <th onclick="sortTableCol(6)">Sector</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -130,19 +124,23 @@
<td>[[${empresa.cif}]]</td> <td>[[${empresa.cif}]]</td>
<td>[[${empresa.correo}]]</td> <td>[[${empresa.correo}]]</td>
<td>[[${empresa.telefono}]]</td> <td>[[${empresa.telefono}]]</td>
<td class="keywords-cell"> <td>[[${empresa.keywords}]]</td>
<div th:each="keyword : ${#strings.arraySplit(empresa.keywords, ',')}">[[${keyword}]]</div>
</td>
<td>[[${empresa.sector.nombre}]]</td> <td>[[${empresa.sector.nombre}]]</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
</div> </div>
<script src="/orderTable.js"></script>
<script> <script>
function goBack() { function goBack() {
window.history.back(); window.history.back();
} }
document.querySelector('#myInput').addEventListener('keyup', myFunction);
document.querySelector('form').addEventListener('submit', function(event) {
event.preventDefault();
myFunction();
});
</script> </script>
</body> </body>
</html> </html>
Loading…
Cancel
Save

Powered by INFORMATICA.FP.EDU.ES.