mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-30 15:48:49 +00:00
añadido todo lo referente a paises
This commit is contained in:
82
src/main/java/com/imprimelibros/erp/paises/Paises.java
Normal file
82
src/main/java/com/imprimelibros/erp/paises/Paises.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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);
|
||||||
|
}
|
||||||
@ -0,0 +1,52 @@
|
|||||||
|
databaseChangeLog:
|
||||||
|
- changeSet:
|
||||||
|
id: 0003-create-paises
|
||||||
|
author: jjo
|
||||||
|
changes:
|
||||||
|
- createTable:
|
||||||
|
tableName: paises
|
||||||
|
columns:
|
||||||
|
- column:
|
||||||
|
name: keyword
|
||||||
|
type: varchar(64)
|
||||||
|
constraints:
|
||||||
|
nullable: false
|
||||||
|
primaryKey: true
|
||||||
|
primaryKeyName: pk_paises
|
||||||
|
- column:
|
||||||
|
name: code
|
||||||
|
type: char(2)
|
||||||
|
constraints:
|
||||||
|
nullable: false
|
||||||
|
- column:
|
||||||
|
name: code3
|
||||||
|
type: char(3)
|
||||||
|
constraints:
|
||||||
|
nullable: false
|
||||||
|
- column:
|
||||||
|
name: currency
|
||||||
|
type: char(3)
|
||||||
|
constraints:
|
||||||
|
nullable: false
|
||||||
|
|
||||||
|
- addUniqueConstraint:
|
||||||
|
tableName: paises
|
||||||
|
columnNames: code
|
||||||
|
constraintName: uq_paises_code
|
||||||
|
|
||||||
|
- addUniqueConstraint:
|
||||||
|
tableName: paises
|
||||||
|
columnNames: code3
|
||||||
|
constraintName: uq_paises_code3
|
||||||
|
|
||||||
|
- loadData:
|
||||||
|
tableName: paises
|
||||||
|
file: db/changelog/data/paises.csv
|
||||||
|
separator: ","
|
||||||
|
encoding: UTF-8
|
||||||
|
quotchar: '"'
|
||||||
|
|
||||||
|
rollback:
|
||||||
|
- dropTable:
|
||||||
|
tableName: paises
|
||||||
|
cascadeConstraints: true
|
||||||
40
src/main/resources/db/changelog/data/paises.csv
Normal file
40
src/main/resources/db/changelog/data/paises.csv
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
keyword,code,code3,currency
|
||||||
|
argentina,ar,arg,ARS
|
||||||
|
austria,at,aut,EUR
|
||||||
|
australia,au,aus,AUD
|
||||||
|
bosnia_herzegovina,ba,bih,BAM
|
||||||
|
belgica,be,bel,EUR
|
||||||
|
bulgaria,bg,bgr,BGN
|
||||||
|
canada,ca,can,CAD
|
||||||
|
suiza,ch,che,CHF
|
||||||
|
chile,cl,chl,CLP
|
||||||
|
colombia,co,col,COP
|
||||||
|
republica_checa,cz,cze,CZK
|
||||||
|
alemania,de,deu,EUR
|
||||||
|
dinamarca,dk,dnk,DKK
|
||||||
|
estonia,ee,est,EUR
|
||||||
|
espania,es,esp,EUR
|
||||||
|
finlandia,fi,fin,EUR
|
||||||
|
francia,fr,fra,EUR
|
||||||
|
reino_unido,gb,gbr,GBP
|
||||||
|
grecia,gr,grc,EUR
|
||||||
|
croacia,hr,hrv,EUR
|
||||||
|
hungria,hu,hun,HUF
|
||||||
|
irlanda,ie,irl,EUR
|
||||||
|
israel,il,isr,ILS
|
||||||
|
italia,it,ita,EUR
|
||||||
|
lituania,lt,ltu,EUR
|
||||||
|
luxemburgo,lu,lux,EUR
|
||||||
|
letonia,lv,lva,EUR
|
||||||
|
mexico,mx,mex,MXN
|
||||||
|
holanda,nl,nld,EUR
|
||||||
|
noruega,no,nor,NOK
|
||||||
|
peru,pe,per,PEN
|
||||||
|
polonia,pl,pol,PLN
|
||||||
|
portugal,pt,prt,EUR
|
||||||
|
rumania,ro,rou,RON
|
||||||
|
serbia,rs,srb,RSD
|
||||||
|
suecia,se,swe,SEK
|
||||||
|
eslovenia,si,svn,EUR
|
||||||
|
eslovaquia,sk,svk,EUR
|
||||||
|
eeuu,us,usa,USD
|
||||||
|
@ -2,4 +2,6 @@ databaseChangeLog:
|
|||||||
- include:
|
- include:
|
||||||
file: db/changelog/changesets/0001-baseline.yml
|
file: db/changelog/changesets/0001-baseline.yml
|
||||||
- include:
|
- include:
|
||||||
file: db/changelog/changesets/0002-create-pedidos.yml
|
file: db/changelog/changesets/0002-create-pedidos.yml
|
||||||
|
- include:
|
||||||
|
file: db/changelog/changesets/0003-create-paises.yml
|
||||||
39
src/main/resources/i18n/paises_en.properties
Normal file
39
src/main/resources/i18n/paises_en.properties
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
paises.argentina=Argentina
|
||||||
|
paises.austria=Austria
|
||||||
|
paises.australia=Australia
|
||||||
|
paises.bosnia_herzegovina=Bosnia and Herzegovina
|
||||||
|
paises.belgica=Belgium
|
||||||
|
paises.bulgaria=Bulgaria
|
||||||
|
paises.canada=Canada
|
||||||
|
paises.suiza=Switzerland
|
||||||
|
paises.chile=Chile
|
||||||
|
paises.colombia=Colombia
|
||||||
|
paises.republica_checa=Czech Republic
|
||||||
|
paises.alemania=Germany
|
||||||
|
paises.dinamarca=Denmark
|
||||||
|
paises.estonia=Estonia
|
||||||
|
paises.espania=Spain
|
||||||
|
paises.finlandia=Finland
|
||||||
|
paises.francia=France
|
||||||
|
paises.reino_unido=United Kingdom
|
||||||
|
paises.grecia=Greece
|
||||||
|
paises.croacia=Croatia
|
||||||
|
paises.hungria=Hungary
|
||||||
|
paises.irlanda=Ireland
|
||||||
|
paises.israel=Israel
|
||||||
|
paises.italia=Italy
|
||||||
|
paises.lituania=Lithuania
|
||||||
|
paises.luxemburgo=Luxembourg
|
||||||
|
paises.letonia=Latvia
|
||||||
|
paises.mexico=Mexico
|
||||||
|
paises.holanda=Netherlands
|
||||||
|
paises.noruega=Norway
|
||||||
|
paises.peru=Peru
|
||||||
|
paises.polonia=Poland
|
||||||
|
paises.portugal=Portugal
|
||||||
|
paises.rumania=Romania
|
||||||
|
paises.serbia=Serbia
|
||||||
|
paises.suecia=Sweden
|
||||||
|
paises.eslovenia=Slovenia
|
||||||
|
paises.eslovaquia=Slovakia
|
||||||
|
paises.eeuu=United States
|
||||||
39
src/main/resources/i18n/paises_es.properties
Normal file
39
src/main/resources/i18n/paises_es.properties
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
paises.argentina=Argentina
|
||||||
|
paises.austria=Austria
|
||||||
|
paises.australia=Australia
|
||||||
|
paises.bosnia_herzegovina=Bosnia y Herzegovina
|
||||||
|
paises.belgica=Bélgica
|
||||||
|
paises.bulgaria=Bulgaria
|
||||||
|
paises.canada=Canadá
|
||||||
|
paises.suiza=Suiza
|
||||||
|
paises.chile=Chile
|
||||||
|
paises.colombia=Colombia
|
||||||
|
paises.republica_checa=República Checa
|
||||||
|
paises.alemania=Alemania
|
||||||
|
paises.dinamarca=Dinamarca
|
||||||
|
paises.estonia=Estonia
|
||||||
|
paises.espania=España
|
||||||
|
paises.finlandia=Finlandia
|
||||||
|
paises.francia=Francia
|
||||||
|
paises.reino_unido=Reino Unido
|
||||||
|
paises.grecia=Grecia
|
||||||
|
paises.croacia=Croacia
|
||||||
|
paises.hungria=Hungría
|
||||||
|
paises.irlanda=Irlanda
|
||||||
|
paises.israel=Israel
|
||||||
|
paises.italia=Italia
|
||||||
|
paises.lituania=Lituania
|
||||||
|
paises.luxemburgo=Luxemburgo
|
||||||
|
paises.letonia=Letonia
|
||||||
|
paises.mexico=México
|
||||||
|
paises.holanda=Países Bajos
|
||||||
|
paises.noruega=Noruega
|
||||||
|
paises.peru=Perú
|
||||||
|
paises.polonia=Polonia
|
||||||
|
paises.portugal=Portugal
|
||||||
|
paises.rumania=Rumanía
|
||||||
|
paises.serbia=Serbia
|
||||||
|
paises.suecia=Suecia
|
||||||
|
paises.eslovenia=Eslovenia
|
||||||
|
paises.eslovaquia=Eslovaquia
|
||||||
|
paises.eeuu=Estados Unidos
|
||||||
Reference in New Issue
Block a user