añadido todo lo referente a paises

This commit is contained in:
2025-10-23 21:38:35 +02:00
parent 59f7d315d9
commit 1526600ee1
9 changed files with 344 additions and 1 deletions

View File

@ -0,0 +1,82 @@
package com.imprimelibros.erp.paises;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name = "paises")
public class Paises {
@Id
@Column(name = "keyword", length = 64, nullable = false)
private String keyword;
@Column(name = "code", length = 2, nullable = false, unique = true)
private String code;
@Column(name = "code3", length = 3, nullable = false, unique = true)
private String code3;
@Column(name = "currency", length = 3, nullable = false)
private String currency;
// --- Getters & Setters ---
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getCode3() {
return code3;
}
public void setCode3(String code3) {
this.code3 = code3;
}
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
// --- toString, equals & hashCode ---
@Override
public String toString() {
return "Paises{" +
"keyword='" + keyword + '\'' +
", code='" + code + '\'' +
", code3='" + code3 + '\'' +
", currency='" + currency + '\'' +
'}';
}
@Override
public int hashCode() {
return keyword != null ? keyword.hashCode() : 0;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Paises other)) return false;
return keyword != null && keyword.equals(other.keyword);
}
}

View File

@ -0,0 +1,73 @@
package com.imprimelibros.erp.paises;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.*;
import java.text.Collator;
import java.util.*;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/api/paises")
public class PaisesController {
private final PaisesRepository paisesRepository;
private final MessageSource messageSource;
public PaisesController(PaisesRepository paisesRepository, MessageSource messageSource) {
this.paisesRepository = paisesRepository;
this.messageSource = messageSource;
}
/**
* Compatible con Select2 (AJAX):
* - Soporta parámetros opcionales:
* - q / term : texto a buscar
* - lang : ej. "es", "en" (si se omite usa Locale actual)
*
* Respuesta:
* { "results": [ { "id": "espania", "text": "España" }, ... ] }
*/
@GetMapping
public Map<String, Object> getPaises(
@RequestParam(value = "q", required = false) String q1,
@RequestParam(value = "term", required = false) String q2,
Locale locale) {
// Termino de búsqueda (Select2 usa 'q' o 'term' según versión/config)
String search = Optional.ofNullable(q1).orElse(q2);
if (search != null) {
search = search.trim();
}
final String q = (search == null || search.isEmpty())
? null
: search.toLowerCase(locale);
List<Paises> all = paisesRepository.findAll();
// Mapear a opciones id/text con i18n y filtrar por búsqueda si llega
List<Map<String, String>> options = all.stream()
.map(cc -> {
String key = cc.getKeyword();
String text = messageSource.getMessage("paises." + key, null, key, locale);
Map<String, String> m = new HashMap<>();
m.put("id", key); // lo normal en Select2: id = valor que guardarás (keyword)
m.put("text", text); // texto mostrado, i18n con fallback a keyword
return m;
})
.filter(opt -> {
if (q == null || q.isEmpty())
return true;
String text = opt.get("text").toLowerCase(locale);
String id = opt.get("id").toLowerCase(locale);
return text.contains(q) || id.contains(q);
})
.sorted(Comparator.comparing(m -> m.get("text"), Collator.getInstance(locale)))
.collect(Collectors.toList());
// Estructura Select2
Map<String, Object> resp = new HashMap<>();
resp.put("results", options);
return resp;
}
}

View File

@ -0,0 +1,16 @@
package com.imprimelibros.erp.paises;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface PaisesRepository extends JpaRepository<Paises, String> {
Optional<Paises> findByCode(String code);
Optional<Paises> findByCode3(String code3);
Optional<Paises> findByCurrency(String currency);
}