
org.javabeanstack.web.jsf.controller.LazyDataRows Maven / Gradle / Ivy
/*
* JavaBeanStack FrameWork
*
* Copyright (C) 2018 Jorge Enciso
* Email: [email protected]
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.javabeanstack.web.jsf.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortMeta;
import org.primefaces.model.SortOrder;
import org.javabeanstack.data.DataInfo;
import org.javabeanstack.data.IDataRow;
import org.javabeanstack.util.Strings;
import org.javabeanstack.error.ErrorManager;
/**
*
* @author jenciso
* @param
*/
public class LazyDataRows extends LazyDataModel {
private static final Logger LOGGER = Logger.getLogger(LazyDataRows.class);
public AbstractDataController context;
public LazyDataRows(AbstractDataController context) {
this.context = context;
}
public List getRows() {
if (context != null) {
return context.getDataRows();
}
return null;
}
@Override
public T getRowData(String rowKey) {
Long id = Long.parseLong(rowKey);
for (T row : getRows()) {
if (row.getId().equals(id)) {
return row;
}
}
T row;
try {
row = (T)context.getDAO().find(getEntityClass(), id);
context.getDataRows().add(row);
context.moveLast();
return row;
} catch (Exception ex) {
ErrorManager.showError(ex, LOGGER);
}
return null;
}
@Override
public Object getRowKey(T row) {
return row.getId();
}
public Class getEntityClass() {
if (context != null) {
return context.getType();
}
return null;
}
/**
* Devuelve una lista de datos. Es un evento ejecutado desde el primeface
* cuando se puebla por primera vez la grilla, o cuando se pasa de una
* pagina a otra, o se filtra los datos en la grilla
*
* @param first a partir de que nro de registro va a devolver los datos.
* @param pageSize cantidad de registros a devolver.
* @param multiSortMeta
* @param filters filtros (campo, valor)
* @return lista de datos resultante de los parametros introducidos.
*/
@Override
public List load(int first, int pageSize, final List multiSortMeta, Map filters) {
try {
String order = "";
if (multiSortMeta != null) {
for (SortMeta sortMeta : multiSortMeta) {
final SortOrder orders = sortMeta.getSortOrder();
if (orders == SortOrder.ASCENDING || orders == SortOrder.DESCENDING) {
order = order + " " + sortMeta.getSortField() + ((orders == SortOrder.ASCENDING) ? " asc" : " desc") + ",";
}
}
if (!Strings.isNullorEmpty(order)) {
order = order.substring(0, order.length() - 1);
}
}
return load(first, pageSize, order, filters);
} catch (Exception ex) {
ErrorManager.showError(ex, LOGGER);
}
return null;
}
@Override
public List load(int first, int pageSize, String sortField, SortOrder sortOrder, Map filters) {
try {
String order = "";
if (sortField != null) {
order = sortField;
order += (sortOrder == SortOrder.ASCENDING) ? " asc" : " desc";
}
return load(first, pageSize, order, filters);
} catch (Exception ex) {
ErrorManager.showError(ex, LOGGER);
}
return null;
}
private List load(int first, int pageSize, String order, Map filters) {
try {
if (context.getNoLazyRowsLoad()){
return context.getDataRows();
}
if (context.getFacesCtx().getFacesContext().getAttributes().get("nolazyload") != null){
Boolean noLazyLoad = (Boolean)context.getFacesCtx().getFacesContext().getAttributes().get("nolazyload");
if (noLazyLoad){
return context.getDataRows();
}
context.getFacesCtx().getFacesContext().getAttributes().put("nolazyload", false);
}
String extraFilter = "";
Map params = getParams(filters);
if (!filters.isEmpty()) {
extraFilter = getFilterString(filters);
}
context.setFirstRow(first);
context.setMaxRows(pageSize);
context.setFilterExtra(extraFilter);
if (params != null && !params.isEmpty()){
context.setFilterParams(params);
}
else{
params = context.getFilterParams();
}
if (Strings.isNullorEmpty(order) &&
!Strings.isNullorEmpty(context.getOrder())){
context.setOrder(context.getOrder());
}
else{
context.setOrder(order);
}
context.requery();
List rows = context.getDataRows();
if (rows != null) {
setRowCount(context.getDAO().getCount(context.getLastQuery(), params).intValue());
} else {
setRowCount(0);
}
context.setRowSelected(null);
return rows;
} catch (Exception ex) {
ErrorManager.showError(ex, LOGGER);
}
return null;
}
/**
* Utilizado por el metodo load, devuelve un map con el nombre del campo
* como clave y el valor del campo con su tipo de dato correspondiente.
*
* @param filters filtro (campo, valor)
* @return
*/
private Map getParams(Map filters) {
Map params = new HashMap<>();
for (Map.Entry e : filters.entrySet()) {
Class clase;
String key = e.getKey().toString().replace(".", "");
clase = DataInfo.getFieldType(getEntityClass(), (String) e.getKey());
if (String.class.isAssignableFrom(clase)) {
params.put(key, "%"+ ((String) e.getValue()).trim() + "%");
} else if (Long.class.isAssignableFrom(clase)) {
params.put(key, Long.valueOf((String) e.getValue()));
} else if (Integer.class.isAssignableFrom(clase)) {
params.put(key, Integer.valueOf((String) e.getValue()));
} else if (Short.class.isAssignableFrom(clase)) {
params.put(key, Short.valueOf((String) e.getValue()));
} else {
params.put(key, e.getValue());
}
}
return params;
}
/**
* Utilizado en el metodo load, devuelve la sentencia WHERE que filtra los
* datos de la lista
*
* @param filters filtros (campo, valor)
* @return
*/
private String getFilterString(Map filters) {
String queryWhere = "";
String filter;
String separador = "";
for (Map.Entry e : filters.entrySet()) {
Class clase = DataInfo.getFieldType(getEntityClass(), (String) e.getKey());
String key = e.getKey().toString().replace(".", "");
// Si el campo es string buscar un valor contenido en el campo
if (clase != null && String.class.isAssignableFrom(clase)) {
filter = separador + " upper(o." + e.getKey() + ") like upper(:" + key + ")";
} else {
filter = separador + " o." + e.getKey() + " = :" + key;
}
queryWhere += filter;
separador = " and ";
}
return queryWhere;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy