Cambiando las opciones de fechas en buscador principal

master
vicsash 4 months ago
parent 3927234375
commit 31eed8f958

@ -200,6 +200,7 @@ public class BuscadorController {
@RequestParam(defaultValue = "") String secondaryOption,
Model model) throws ParseException {
System.out.println("Query: TEST " + query);
System.out.println("SecondaryOption: TEST " + secondaryOption);
String[] queryMultiWord = query.split(",");
List<String> queryList = new ArrayList<>();
for (String queryForList : queryMultiWord) {
@ -209,7 +210,7 @@ public class BuscadorController {
}
}
List<Integer> itemsPage = Arrays.asList(5, 10, 15, 20, 25, 50);
Page<Oferta> page = null;
Page<Oferta> page;
if(queryMultiWord.length>1){
page = ofertasService.getPageMultiWord(pageNum, size, sortField, sortDir, queryList, secondaryOption);

@ -28,18 +28,14 @@ import java.util.Set;
@Controller
public class UserController {
@Autowired
private CicloService cicloService;
@Autowired
private SkillService skillService;
@Autowired
private UsuarioService usuarioService;
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private AlumnoService alumnoService;
@ -122,20 +118,14 @@ public class UserController {
@GetMapping("/usuario/info")
public String showUserInfo(Model model, Authentication authentication) {
// Get the current user
String username = authentication.getName();
Usuario usuario = usuarioService.findByLogInName(username);
if(usuario.getRol().getId() ==1){
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "No tienes permisos para acceder a esta página");
}
Alumno alumno = alumnoService.findByEmail(usuario.getEmail());
model.addAttribute("usuario", usuario);
model.addAttribute("alumno", alumno);
// Return the name of the view
return "user/info_user";
}
}

@ -17,9 +17,8 @@ import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.text.SimpleDateFormat;
import java.util.*;
@Controller
@RequestMapping()
@ -132,4 +131,9 @@ public class OfertaController {
}
}
@GetMapping("api/distinct-years")
@ResponseBody
public List<Integer> getDistinctYears() {
return ofertaService.findDistinctYears();
}
}

@ -52,17 +52,11 @@ public interface OfertaRepository extends JpaRepository<Oferta, Long> {
@Query("SELECT o FROM Oferta o WHERE o.nombre LIKE %:query%")
Page<Oferta> findOfertaByNombre(String query, PageRequest of);
@Query("SELECT o FROM Oferta o WHERE o.fecha < :query")
Page<Oferta> findOfertaByFechaAntes(Date query, PageRequest of);
@Query("SELECT o FROM Oferta o WHERE o.fecha > :query")
Page<Oferta> findOfertaByFechaDespues(Date query, PageRequest of);
@Query("SELECT o FROM Oferta o WHERE o.sucursal.empresa.nombre LIKE %:query%")
Page<Oferta> findOfertaByEmpresa(@Param("query") String query, Pageable pageable);
@Query(value = "SELECT * FROM ofertas o WHERE YEAR(o.fecha) = YEAR(:dateQuery)", nativeQuery = true)
Page<Oferta> findOfertaByFechaAnyo(@Param("dateQuery") Date dateQuery, Pageable pageable);
@Query(value = "SELECT * FROM ofertas o WHERE YEAR(o.fecha) = :yearQuery", nativeQuery = true)
Page<Oferta> findOfertaByFechaAnyo(@Param("yearQuery") Integer yearQuery, Pageable pageable);
@Query("SELECT o FROM Oferta o WHERE lower(o.nombre) IN :queryList")
Page<Oferta> findOfertasByNombres(@Param("queryList") String queryList, Pageable pageable);
@ -79,15 +73,12 @@ public interface OfertaRepository extends JpaRepository<Oferta, Long> {
@Query("SELECT o FROM Oferta o WHERE o.sucursal.empresa.nombre LIKE %:query%")
Collection<Oferta> ofertasByEmpresaCust(@Param("query") String query);
@Query("SELECT o FROM Oferta o WHERE o.fecha < :query")
Collection<Oferta> ofertasByFechaAntesCust(@Param("query") Date query);
@Query("SELECT o FROM Oferta o WHERE o.fecha > :query")
Collection<Oferta> ofertasByFechaDespuesCust(@Param("query") Date query);
@Query("SELECT o FROM Oferta o JOIN o.skills s WHERE s.nombre LIKE %:query%")
Collection<Oferta> ofertasBySkillCust(@Param("query") String query);
@Query(value = "SELECT * FROM ofertas o WHERE YEAR(o.fecha) = YEAR(:dateQuery)", nativeQuery = true)
Collection<? extends Oferta> ofertasByFechaAnyoCust(Date dateQuery);
@Query(value = "SELECT DISTINCT YEAR(o.fecha) FROM ofertas o", nativeQuery = true)
List<Integer> findDistinctYears();
}

@ -108,10 +108,10 @@ public class OfertaService implements IOferta {
}
@Override
public Page<Oferta> getPage(int pageNum, int size, String sortField, String sortDir, String query, String secondaryOption) throws ParseException {
Date dateQuery = null;
if(isDate(query)){
dateQuery = new SimpleDateFormat("yyyy-MM-dd").parse(query);
System.out.println("Date query TEST: " + dateQuery);
Integer yearQuery = null;
if(isYear(query)){
yearQuery = Integer.parseInt(query);
System.out.println("Year query TEST: " + yearQuery);
}
if(secondaryOption.equalsIgnoreCase("Todo")){
return findAllPaginated(pageNum, size, sortField, sortDir);
@ -119,12 +119,8 @@ public class OfertaService implements IOferta {
return ofertaRepository.findOfertaBySkill(query, PageRequest.of(pageNum - 1, size, Sort.by(sortField).ascending()));
} else if(secondaryOption.equals("Nombre")) {
return ofertaRepository.findOfertaByNombre(query, PageRequest.of(pageNum - 1, size, Sort.by(sortField).ascending()));
} else if(secondaryOption.equals("Fecha antes")) {
return ofertaRepository.findOfertaByFechaAntes(dateQuery, PageRequest.of(pageNum - 1, size, Sort.by(sortField).ascending()));
} else if(secondaryOption.equals("Fecha despues")) {
return ofertaRepository.findOfertaByFechaDespues(dateQuery, PageRequest.of(pageNum - 1, size, Sort.by(sortField).ascending()));
} else if(secondaryOption.equals("Año")) {
return ofertaRepository.findOfertaByFechaAnyo(dateQuery, PageRequest.of(pageNum - 1, size, Sort.by(sortField).ascending()));
return ofertaRepository.findOfertaByFechaAnyo(yearQuery, PageRequest.of(pageNum - 1, size, Sort.by(sortField).ascending()));
} else if(secondaryOption.equals("Empresa")) {
return ofertaRepository.findOfertaByEmpresa(query, PageRequest.of(pageNum - 1, size, Sort.by(sortField).ascending()));
} else {
@ -154,13 +150,12 @@ public class OfertaService implements IOferta {
return new PageImpl<>(allMatches.subList(start, end), PageRequest.of(pageNum - 1, size), allMatches.size());
}
public boolean isDate(String query) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); // Adjust this to match the date format of your query
public boolean isYear(String query) {
try {
Date date = formatter.parse(query);
return true; // The query is a date
} catch (ParseException e) {
return false; // The query is not a date
Integer.parseInt(query);
return true;
} catch (NumberFormatException e) {
return false;
}
}
@ -179,21 +174,6 @@ public class OfertaService implements IOferta {
listEmpPrime.addAll(ofertaRepository.ofertasByEmpresaCust(query));
} else if (secondaryOption.equals("Skill")) {
listEmpPrime.addAll(ofertaRepository.ofertasBySkillCust(query));
}else if(secondaryOption.equals("Fecha antes")){
try {
Date dateQuery = new SimpleDateFormat("yyyy-MM-dd").parse(query);
listEmpPrime.addAll(ofertaRepository.ofertasByFechaAntesCust(dateQuery));
} catch (ParseException e) {
e.printStackTrace();
}
}else if(secondaryOption.equals("Fecha despues")){
try {
Date dateQuery = new SimpleDateFormat("yyyy-MM-dd").parse(query);
listEmpPrime.addAll(ofertaRepository.ofertasByFechaDespuesCust(dateQuery));
} catch (ParseException e) {
e.printStackTrace();
}
}else if(secondaryOption.equals("Año")){
try {
Date dateQuery = new SimpleDateFormat("yyyy-MM-dd").parse(query);
@ -228,6 +208,7 @@ public class OfertaService implements IOferta {
}
}
}
public List<Integer> findDistinctYears() {
return ofertaRepository.findDistinctYears();
}
}

@ -47,9 +47,6 @@
justify-content: space-between;
width: 100%;
}
#date{
margin-top: 15px;
}
#user{
position: relative;
margin-left: 50px;
@ -120,14 +117,15 @@
<select id="secondaryDropdown"></select>
<input type="hidden" name="secondaryOption" id="hiddenSecondaryOption">
</div>
<div id="date">
<input type="date" id="datePicker" name="query" style="display: none;">
<div id="yearDropdownContainer" style="display: none;">
<select id="yearDropdown"></select>
</div>
</form>
</div>
<script>
//MODAL
// Get the modal
var modal = document.getElementById("userModal");
// Get the button that opens the modal
@ -225,80 +223,41 @@
}
});
}
$(document).ready(function() {
const urlParams = new URLSearchParams(window.location.search);
const userDeleted = urlParams.get('userDeleted');
if (userDeleted === 'true') {
alert('Usuario Borrado');
userDeleted === 'false';
}
});
// Get reference to the date picker
var datePicker = document.getElementById('datePicker');
// Add an event listener to the date picker
datePicker.addEventListener('change', function() {
var searchBar = document.querySelector('input[name="query"]');
var secondaryOption = document.getElementById('secondaryDropdown').value;
// If the selected option is 'Fecha antes', 'Fecha despues', or 'Año', set the value of the search bar to the value of the date picker
if (['Fecha antes', 'Fecha despues', 'Año'].includes(secondaryOption)) {
searchBar.value = datePicker.value;
}
// Log the value of the search bar to the console
//console.log("Query: " + searchBar.value);
});
document.getElementById('searchForm').addEventListener('submit', function() {
var searchOption = document.getElementById('searchOption').value;
var searchOption = document.getElementById('searchOption'); // Removed .value
var secondaryOption = document.getElementById('secondaryDropdown').value;
var datePicker = document.getElementById('datePicker');
var searchBar = document.querySelector('input[name="query"]');
// If the selected option is 'Fecha antes', 'Fecha despues', or 'Año', set the value of the search bar to the value of the date picker
if (['Fecha antes', 'Fecha despues', 'Año'].includes(secondaryOption)) {
searchBar.value = datePicker.value;
}
// Update the value of the hidden input field
document.getElementById('hiddenSecondaryOption').value = secondaryOption;
this.action = "/buscador/" + searchOption + "/page/1";
// Update the searchBar value to the selected year
if (secondaryOption === 'Año') {
searchBar.value = document.getElementById('yearDropdown').value;
console.log('searchBar.value after assignment: ', searchBar.value);
}
this.action = "/buscador/" + searchOption.value + "/page/1";
});
// Get references to the primary dropdown, the search bar, and the secondary dropdown
var primaryDropdown = document.getElementById('searchOption');
var searchBar = document.querySelector('input[name="query"]');
var secondaryDropdown = document.getElementById('secondaryDropdown');
primaryDropdown.addEventListener('change', function() {
if (primaryDropdown.value === 'YourCondition') { // replace 'YourCondition' with the actual condition
secondaryDropdown.style.display = 'none';
} else {
secondaryDropdown.style.display = 'block';
}
});
secondaryDropdown.addEventListener('change', function() {
var datePicker = document.getElementById('datePicker');
if (['Fecha antes', 'Fecha despues', 'Año'].includes(secondaryDropdown.value)) {
searchBar.value = '';
searchBar.disabled = true;
datePicker.style.display = 'block';
} else if (secondaryDropdown.value === 'Todo') {
if (secondaryDropdown.value === 'Todo') {
searchBar.value = 'Todo';
searchBar.disabled = true;
datePicker.style.display = 'none';
} else {
searchBar.value = '';
searchBar.disabled = false;
datePicker.style.display = 'none';
}
});
primaryDropdown.addEventListener('change', function() {
// Clear the secondary dropdown
secondaryDropdown.innerHTML = '';
// Determine what to add to the secondary dropdown based on the selected option of the primary dropdown
@ -314,7 +273,7 @@
options = ['Todo', 'Nombre', 'Empresa'];
break;
case'ofertas':
options = ['Todo', 'Nombre', 'Empresa','Fecha'];
options = ['Todo', 'Nombre', 'Empresa','Año'];
break;
case'sucursales':
options = ['Todo', 'Nombre', 'Empresa','Localidad'];
@ -338,50 +297,6 @@
//console.log("Primary dropdown value: " + primaryDropdown.value); // Add this line
//console.log("Options to add to secondary dropdown: " + options); // Add this line
// Add the new options to the secondary dropdown
for (var i = 0; i < options.length; i++) {
var option = document.createElement('option');
option.text = options[i];
secondaryDropdown.add(option);
}
});// Add an event listener to the primary dropdown
primaryDropdown.addEventListener('change', function() {
// Clear the secondary dropdown
secondaryDropdown.innerHTML = '';
// Determine what to add to the secondary dropdown based on the selected option of the primary dropdown
var options;
switch (primaryDropdown.value) {
case 'empresas':
options = ['Todo', 'Nombre', 'Sector','Keywords'];
break;
case 'sectores':
options = ['Todo'];
break;
case 'contactos':
options = ['Todo', 'Nombre', 'Empresa'];
break;
case'ofertas':
options = ['Todo', 'Nombre', 'Empresa','Fecha antes','Fecha despues','Año','Skill'];
break;
case'sucursales':
options = ['Todo', 'Nombre', 'Empresa','Localidad'];
break;
case'skills':
options = ['Todo'];
break;
case'familias':
options = ['Todo'];
break;
case'ciclos':
options = ['Todo','Familia','Codigo'];
break;
case'alumnos':
options = ['Todo', 'Nombre', 'Apellido','Ciclo','Keywords','Nia' ,'Dni'];
break;
default:
options = [];
}
// Add the new options to the secondary dropdown
for (var i = 0; i < options.length; i++) {
var option = document.createElement('option');
@ -389,40 +304,74 @@
secondaryDropdown.add(option);
}
});
// Trigger the change event manually to populate the secondary dropdown when the page loads
primaryDropdown.dispatchEvent(new Event('change'));
// Reset the form
document.querySelector('form').reset();
// Set the value of the search bar to "Todo" and disable it
searchBar.value = 'Todo';
searchBar.disabled = true;
function submitForm() {
var searchOption = document.getElementById('searchOption').value;
var searchOption = document.getElementById('searchOption');
var secondaryOption = document.getElementById('secondaryDropdown').value;
var datePicker = document.getElementById('datePicker');
document.getElementById('hiddenSecondaryOption').value = secondaryOption;
// If the selected option is 'Fecha antes', 'Fecha despues', or 'Año', set the value of the search bar to the value of the date picker
if (['Fecha antes', 'Fecha despues', 'Año'].includes(secondaryOption)) {
datePicker.name = "query";
} else {
datePicker.name = "";
// Update the searchOption value to the selected year
// Update the searchBar value to the selected year
if (secondaryOption === 'Año') {
searchBar.value = document.getElementById('yearDropdown').value;
console.log('searchBar.value after assignment: ', searchBar.value);
}
this.action = "/buscador/" + searchOption.value + "/page/1";
return true;
}
// Log the query to the console
//console.log("Query SENT TEST: " + datePicker.value);
////////////////////////////////////////DATE//////////////////////////////////////
// Fetch the distinct years from the server
// Fetch the distinct years from the server
fetch('/api/distinct-years')
.then(response => response.json()) // Convert the response to JSON
.then(years => {
// Get a reference to the yearDropdown select element
var yearDropdown = document.getElementById('yearDropdown');
// Update the value of the hidden input field
document.getElementById('hiddenSecondaryOption').value = secondaryOption;
this.action = "/buscador/" + searchOption + "/page/1";
return true;
// Remove any existing options in the dropdown
yearDropdown.innerHTML = '';
// Iterate over each year
years.forEach(year => {
// Create a new option element
var option = document.createElement('option');
option.value = year;
option.text = year;
// Append the option to the yearDropdown select element
yearDropdown.appendChild(option);
});
})
.catch(error => console.error('Error:', error));
// Get a reference to the yearDropdownContainer div
// Add an event listener to the secondaryDropdown select element
secondaryDropdown.addEventListener('change', function() {
// Check if the selected value is "Año"
if (secondaryDropdown.value === 'Año') {
// If it is, make the yearDropdownContainer visible
yearDropdownContainer.style.display = 'block';
searchBar.disabled = true;
} else {
// If it's not, hide the yearDropdownContainer
yearDropdownContainer.style.display = 'none';
searchBar.disabled = false;
}
window.onload = function() {
primaryDropdown.dispatchEvent(new Event('change'));
};
});
var yearDropdown = document.getElementById('yearDropdown');
var searchOption = document.getElementById('searchOption');
yearDropdown.addEventListener('change', function() {
searchBar.value = yearDropdown.value;
});
</script>
</body>
</html>

@ -121,9 +121,6 @@
<select id="secondaryDropdown"></select>
<input type="hidden" name="secondaryOption" id="hiddenSecondaryOption">
</div>
<div id="date">
<input type="date" id="datePicker" name="query" style="display: none;">
</div>
</form>
</div>
@ -210,34 +207,9 @@
userUpdated === 'false';
}
});
// Get reference to the date picker
var datePicker = document.getElementById('datePicker');
// Add an event listener to the date picker
datePicker.addEventListener('change', function() {
var searchBar = document.querySelector('input[name="query"]');
var secondaryOption = document.getElementById('secondaryDropdown').value;
// If the selected option is 'Fecha antes', 'Fecha despues', or 'Año', set the value of the search bar to the value of the date picker
if (['Fecha antes', 'Fecha despues', 'Año'].includes(secondaryOption)) {
searchBar.value = datePicker.value;
}
// Log the value of the search bar to the console
//console.log("Query: " + searchBar.value);
});
document.getElementById('searchForm').addEventListener('submit', function() {
var searchOption = document.getElementById('searchOption').value;
var secondaryOption = document.getElementById('secondaryDropdown').value;
var datePicker = document.getElementById('datePicker');
var searchBar = document.querySelector('input[name="query"]');
// If the selected option is 'Fecha antes', 'Fecha despues', or 'Año', set the value of the search bar to the value of the date picker
if (['Fecha antes', 'Fecha despues', 'Año'].includes(secondaryOption)) {
searchBar.value = datePicker.value;
}
// Update the value of the hidden input field
document.getElementById('hiddenSecondaryOption').value = secondaryOption;
this.action = "/buscador/" + searchOption + "/page/1";
});
@ -246,34 +218,17 @@
var searchBar = document.querySelector('input[name="query"]');
var secondaryDropdown = document.getElementById('secondaryDropdown');
primaryDropdown.addEventListener('change', function() {
if (primaryDropdown.value === 'YourCondition') { // replace 'YourCondition' with the actual condition
secondaryDropdown.style.display = 'none';
} else {
secondaryDropdown.style.display = 'block';
}
});
secondaryDropdown.addEventListener('change', function() {
var datePicker = document.getElementById('datePicker');
if (['Fecha antes', 'Fecha despues', 'Año'].includes(secondaryDropdown.value)) {
searchBar.value = '';
searchBar.disabled = true;
datePicker.style.display = 'block';
} else if (secondaryDropdown.value === 'Todo') {
if (secondaryDropdown.value === 'Todo') {
searchBar.value = 'Todo';
searchBar.disabled = true;
datePicker.style.display = 'none';
} else {
searchBar.value = '';
searchBar.disabled = false;
datePicker.style.display = 'none';
}
});
primaryDropdown.addEventListener('change', function() {
// Clear the secondary dropdown
secondaryDropdown.innerHTML = '';
// Determine what to add to the secondary dropdown based on the selected option of the primary dropdown
@ -289,7 +244,7 @@
options = ['Todo', 'Nombre', 'Empresa'];
break;
case'ofertas':
options = ['Todo', 'Nombre', 'Empresa','Fecha'];
options = ['Todo', 'Nombre', 'Empresa','Año'];
break;
case'sucursales':
options = ['Todo', 'Nombre', 'Empresa','Localidad'];
@ -320,77 +275,18 @@
secondaryDropdown.add(option);
}
});// Add an event listener to the primary dropdown
primaryDropdown.addEventListener('change', function() {
// Clear the secondary dropdown
secondaryDropdown.innerHTML = '';
// Determine what to add to the secondary dropdown based on the selected option of the primary dropdown
var options;
switch (primaryDropdown.value) {
case 'empresas':
options = ['Todo', 'Nombre', 'Sector','Keywords'];
break;
case 'sectores':
options = ['Todo'];
break;
case 'contactos':
options = ['Todo', 'Nombre', 'Empresa'];
break;
case'ofertas':
options = ['Todo', 'Nombre', 'Empresa','Fecha antes','Fecha despues','Año','Skill'];
break;
case'sucursales':
options = ['Todo', 'Nombre', 'Empresa','Localidad'];
break;
case'skills':
options = ['Todo'];
break;
case'familias':
options = ['Todo'];
break;
case'ciclos':
options = ['Todo','Familia','Codigo'];
break;
case'alumnos':
options = ['Todo', 'Nombre', 'Apellido','Ciclo','Keywords','Nia' ,'Dni'];
break;
default:
options = [];
}
// Add the new options to the secondary dropdown
for (var i = 0; i < options.length; i++) {
var option = document.createElement('option');
option.text = options[i];
secondaryDropdown.add(option);
}
});
// Trigger the change event manually to populate the secondary dropdown when the page loads
primaryDropdown.dispatchEvent(new Event('change'));
// Reset the form
document.querySelector('form').reset();
// Set the value of the search bar to "Todo" and disable it
searchBar.value = 'Todo';
searchBar.disabled = true;
function submitForm() {
var searchOption = document.getElementById('searchOption').value;
var secondaryOption = document.getElementById('secondaryDropdown').value;
var datePicker = document.getElementById('datePicker');
// If the selected option is 'Fecha antes', 'Fecha despues', or 'Año', set the value of the search bar to the value of the date picker
if (['Fecha antes', 'Fecha despues', 'Año'].includes(secondaryOption)) {
datePicker.name = "query";
} else {
datePicker.name = "";
}
// Log the query to the console
//console.log("Query SENT TEST: " + datePicker.value);
// Update the value of the hidden input field
document.getElementById('hiddenSecondaryOption').value = secondaryOption;
this.action = "/buscador/" + searchOption + "/page/1";
return true;

Loading…
Cancel
Save

Powered by INFORMATICA.FP.EDU.ES.