mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-19 15:20:20 +00:00
trabajando en usuarios
This commit is contained in:
@ -0,0 +1,98 @@
|
||||
package com.imprimelibros.erp.datatables;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.data.domain.*;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class DataTable<T> {
|
||||
|
||||
public interface FilterHook<T> extends BiConsumer<SpecBuilder<T>, DataTablesRequest> {}
|
||||
public interface SpecBuilder<T> { void add(Specification<T> extra); }
|
||||
|
||||
private final JpaSpecificationExecutor<T> repo;
|
||||
private final Class<T> entityClass;
|
||||
private final DataTablesRequest dt;
|
||||
private final List<String> searchable;
|
||||
private final List<Function<T, Map<String,Object>>> adders = new ArrayList<>();
|
||||
private final List<Function<Map<String,Object>, Map<String,Object>>> editors = new ArrayList<>();
|
||||
private final List<FilterHook<T>> filters = new ArrayList<>();
|
||||
private Specification<T> baseSpec = (root,q,cb) -> cb.conjunction();
|
||||
private final ObjectMapper om = new ObjectMapper();
|
||||
|
||||
private DataTable(JpaSpecificationExecutor<T> repo, Class<T> entityClass, DataTablesRequest dt, List<String> searchable) {
|
||||
this.repo = repo; this.entityClass = entityClass; this.dt = dt; this.searchable = searchable;
|
||||
}
|
||||
|
||||
public static <T> DataTable<T> of(JpaSpecificationExecutor<T> repo, Class<T> entityClass, DataTablesRequest dt, List<String> searchable) {
|
||||
return new DataTable<>(repo, entityClass, dt, searchable);
|
||||
}
|
||||
|
||||
/** Equivalente a tu $q->where(...): establece condición base */
|
||||
public DataTable<T> where(Specification<T> spec) { this.baseSpec = this.baseSpec.and(spec); return this; }
|
||||
|
||||
/** add("campo", fn(entity)->valor|Map) */
|
||||
public DataTable<T> add(String field, Function<T, Object> fn) {
|
||||
adders.add(entity -> {
|
||||
Map<String,Object> m = new HashMap<>();
|
||||
m.put(field, fn.apply(entity));
|
||||
return m;
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
/** add(fn(entity)->Map<String,Object>) para devolver objetos anidados como tu "logo" */
|
||||
public DataTable<T> add(Function<T, Map<String,Object>> fn) { adders.add(fn); return this; }
|
||||
|
||||
/** edit("campo", fn(entity)->valor) sobreescribe un campo existente o lo crea si no existe */
|
||||
public DataTable<T> edit(String field, Function<T, Object> fn) {
|
||||
editors.add(row -> { row.put(field, fn.apply((T)row.get("__entity"))); return row; });
|
||||
return this;
|
||||
}
|
||||
|
||||
/** filter((builder, req) -> builder.add(miExtraSpec(req))) */
|
||||
public DataTable<T> filter(FilterHook<T> hook) { filters.add(hook); return this; }
|
||||
|
||||
public DataTablesResponse<Map<String,Object>> toJson(long totalCount) {
|
||||
// Construye spec con búsqueda global + base + filtros custom
|
||||
Specification<T> spec = baseSpec.and(DataTablesSpecification.build(dt, searchable));
|
||||
final Specification<T>[] holder = new Specification[]{ spec };
|
||||
filters.forEach(h -> h.accept(extra -> holder[0] = holder[0].and(extra), dt));
|
||||
spec = holder[0];
|
||||
|
||||
// Sort
|
||||
Sort sort = Sort.unsorted();
|
||||
if (!dt.order.isEmpty() && !dt.columns.isEmpty()) {
|
||||
List<Sort.Order> orders = new ArrayList<>();
|
||||
for (var o : dt.order) {
|
||||
String field = dt.columns.get(o.column).name;
|
||||
orders.add(new Sort.Order("desc".equalsIgnoreCase(o.dir) ? Sort.Direction.DESC : Sort.Direction.ASC, field));
|
||||
}
|
||||
sort = Sort.by(orders);
|
||||
}
|
||||
|
||||
// Page
|
||||
int page = dt.length > 0 ? dt.start / dt.length : 0;
|
||||
Pageable pageable = dt.length > 0 ? PageRequest.of(page, dt.length, sort) : Pageable.unpaged();
|
||||
|
||||
var p = repo.findAll(holder[0], pageable);
|
||||
long filtered = p.getTotalElements();
|
||||
|
||||
// Mapear entidad -> Map base (via Jackson) + add/edit
|
||||
List<Map<String,Object>> data = new ArrayList<>();
|
||||
for (T e : p.getContent()) {
|
||||
Map<String,Object> row = om.convertValue(e, Map.class);
|
||||
row.put("__entity", e); // para editores que necesiten la entidad
|
||||
for (var ad : adders) row.putAll(ad.apply(e));
|
||||
for (var ed : editors) ed.apply(row);
|
||||
row.remove("__entity");
|
||||
data.add(row);
|
||||
}
|
||||
return new DataTablesResponse<>(dt.draw, totalCount, filtered, data);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,43 @@
|
||||
package com.imprimelibros.erp.datatables;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import java.util.*;
|
||||
|
||||
public class DataTablesParser {
|
||||
public static DataTablesRequest from(HttpServletRequest req) {
|
||||
DataTablesRequest dt = new DataTablesRequest();
|
||||
dt.draw = parseInt(req.getParameter("draw"), 0);
|
||||
dt.start = parseInt(req.getParameter("start"), 0);
|
||||
dt.length = parseInt(req.getParameter("length"), 10);
|
||||
|
||||
if (req.getParameter("search[value]") != null) {
|
||||
dt.search.value = req.getParameter("search[value]");
|
||||
dt.search.regex = Boolean.parseBoolean(req.getParameter("search[regex]"));
|
||||
}
|
||||
|
||||
for (int i=0;; i++) {
|
||||
String data = req.getParameter("columns["+i+"][data]");
|
||||
if (data == null) break;
|
||||
DataTablesRequest.Column c = new DataTablesRequest.Column();
|
||||
c.data = data;
|
||||
c.name = Optional.ofNullable(req.getParameter("columns["+i+"][name]")).orElse(data);
|
||||
c.searchable = Boolean.parseBoolean(Optional.ofNullable(req.getParameter("columns["+i+"][searchable]")).orElse("true"));
|
||||
c.orderable = Boolean.parseBoolean(Optional.ofNullable(req.getParameter("columns["+i+"][orderable]")).orElse("true"));
|
||||
c.search.value = Optional.ofNullable(req.getParameter("columns["+i+"][search][value]")).orElse("");
|
||||
dt.columns.add(c);
|
||||
}
|
||||
for (int i=0;; i++) {
|
||||
String colIdx = req.getParameter("order["+i+"][column]");
|
||||
if (colIdx == null) break;
|
||||
DataTablesRequest.Order o = new DataTablesRequest.Order();
|
||||
o.column = parseInt(colIdx,0);
|
||||
o.dir = Optional.ofNullable(req.getParameter("order["+i+"][dir]")).orElse("asc");
|
||||
dt.order.add(o);
|
||||
}
|
||||
|
||||
// guarda TODOS los params crudos (para filtros custom)
|
||||
req.getParameterMap().forEach((k,v) -> dt.raw.put(k, v!=null && v.length>0 ? v[0] : null));
|
||||
return dt;
|
||||
}
|
||||
private static int parseInt(String s, int def){ try{return Integer.parseInt(s);}catch(Exception e){return def;}}
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
package com.imprimelibros.erp.datatables;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class DataTablesRequest {
|
||||
public int draw;
|
||||
public int start;
|
||||
public int length;
|
||||
public Search search = new Search();
|
||||
public List<Order> order = new ArrayList<>();
|
||||
public List<Column> columns = new ArrayList<>();
|
||||
public Map<String,String> raw = new HashMap<>(); // <- params extra
|
||||
|
||||
public static class Search { public String value=""; public boolean regex; }
|
||||
public static class Order { public int column; public String dir; }
|
||||
public static class Column {
|
||||
public String data;
|
||||
public String name;
|
||||
public boolean searchable=true;
|
||||
public boolean orderable=true;
|
||||
public Search search=new Search();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package com.imprimelibros.erp.datatables;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DataTablesResponse<T> {
|
||||
public int draw;
|
||||
public long recordsTotal;
|
||||
public long recordsFiltered;
|
||||
public List<T> data;
|
||||
|
||||
public DataTablesResponse(int draw, long total, long filtered, List<T> data) {
|
||||
this.draw = draw;
|
||||
this.recordsTotal = total;
|
||||
this.recordsFiltered = filtered;
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,48 @@
|
||||
package com.imprimelibros.erp.datatables;
|
||||
|
||||
import org.springframework.data.domain.*;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DataTablesService {
|
||||
|
||||
public static <T> DataTablesResponse<T> handle(
|
||||
DataTablesRequest dt,
|
||||
JpaSpecificationExecutor<T> repo,
|
||||
long totalCount, // count sin filtros (cacheable)
|
||||
List<String> searchableFields,
|
||||
Class<T> entityClass
|
||||
) {
|
||||
// Spec (filtros)
|
||||
Specification<T> spec = DataTablesSpecification.build(dt, searchableFields);
|
||||
|
||||
// Sort
|
||||
Sort sort = Sort.unsorted();
|
||||
if (!dt.order.isEmpty() && !dt.columns.isEmpty()) {
|
||||
List<Sort.Order> orders = new ArrayList<>();
|
||||
for (DataTablesRequest.Order o : dt.order) {
|
||||
String field = dt.columns.get(o.column).name;
|
||||
orders.add(new Sort.Order("desc".equalsIgnoreCase(o.dir) ? Sort.Direction.DESC : Sort.Direction.ASC, field));
|
||||
}
|
||||
sort = Sort.by(orders);
|
||||
}
|
||||
|
||||
// Page
|
||||
int page = dt.length > 0 ? dt.start / dt.length : 0;
|
||||
Pageable pageable = dt.length > 0 ? PageRequest.of(page, dt.length, sort) : Pageable.unpaged();
|
||||
|
||||
// Query
|
||||
Page<T> result = repo.findAll(spec, pageable);
|
||||
long filtered = result.getTotalElements();
|
||||
|
||||
return new DataTablesResponse<>(
|
||||
dt.draw,
|
||||
totalCount,
|
||||
filtered,
|
||||
result.getContent()
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,44 @@
|
||||
package com.imprimelibros.erp.datatables;
|
||||
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import jakarta.persistence.criteria.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DataTablesSpecification {
|
||||
|
||||
/**
|
||||
* Crea una Specification con búsqueda global y por columna (LIKE case-insensitive)
|
||||
* @param dt request de datatables
|
||||
* @param searchableFields campos del entity para el buscador global
|
||||
*/
|
||||
public static <T> Specification<T> build(DataTablesRequest dt, List<String> searchableFields) {
|
||||
return (root, query, cb) -> {
|
||||
List<Predicate> ands = new ArrayList<>();
|
||||
|
||||
// Filtro por columna (si quieres soportarlo)
|
||||
for (int i = 0; i < dt.columns.size(); i++) {
|
||||
DataTablesRequest.Column col = dt.columns.get(i);
|
||||
if (col.searchable && col.search != null && col.search.value != null && !col.search.value.isEmpty()) {
|
||||
ands.add(like(cb, root.get(col.name), col.search.value));
|
||||
}
|
||||
}
|
||||
|
||||
// Búsqueda global
|
||||
if (dt.search != null && dt.search.value != null && !dt.search.value.isEmpty() && !searchableFields.isEmpty()) {
|
||||
String term = "%" + dt.search.value.trim().toLowerCase() + "%";
|
||||
List<Predicate> ors = new ArrayList<>();
|
||||
for (String f : searchableFields) {
|
||||
ors.add(cb.like(cb.lower(root.get(f).as(String.class)), term));
|
||||
}
|
||||
ands.add(cb.or(ors.toArray(new Predicate[0])));
|
||||
}
|
||||
|
||||
return ands.isEmpty() ? cb.conjunction() : cb.and(ands.toArray(new Predicate[0]));
|
||||
};
|
||||
}
|
||||
|
||||
private static Predicate like(CriteriaBuilder cb, Path<?> path, String value) {
|
||||
return cb.like(cb.lower(path.as(String.class)), "%" + value.trim().toLowerCase() + "%");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user