Primefaces 10.0.0: Map<String, FilterMeta> filters in LazyModel

UI Components for JSF
iexpertise
Posts: 26
Joined: 28 Oct 2014, 15:21

27 May 2021, 19:27

NourDev wrote:
27 May 2021, 19:08
Thanks, man. Can you please complete the body of getFilterMap function?

@iexpertise: Did you encounter the same scenario of empty Map<String, FilterMeta> filters trying to filter?
No, the filter works correctly, the Map <String, FilterMeta> filterBy object is filled with what the user typed. If the filter was not made by the user, the object is not null, however, empty.

Below I will put some code snippets.

Code: Select all

package br.com.iexpertise.common.bean;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.primefaces.model.FilterMeta;
import org.primefaces.model.SortMeta;

/**
 * Classe com métodos utilitários que auxiliam o desenvolvimento dos formBeans.
 * 
 * @author Rogério Cortina
 * @since 17/06/2007
 */
public class JsfBean implements Serializable {

	private static final long serialVersionUID = 1L;

	public JsfBean() {
		super();
	}
	
	/**
     * Função utilizada para efeito de compatibilidade com as funções já existentes no sistema, evitando assim o refactory de tudo.
     * 
     * @param sortBy
     * @return
     */
	protected String getSortField(Map<String, SortMeta> sortBy) {
    	
    	if(sortBy != null && sortBy.isEmpty() == false) {
    		SortMeta sortM = sortBy.entrySet().stream().findFirst().get().getValue();
    		return sortM.getField().trim();
    	}
    	
    	return null;
    }
    
    /**
     * Função utilizada para efeito de compatibilidade com as funções já existentes no sistema, evitando assim o refactory de tudo.
     * 
     * @param sortBy
     * @return
     */
    protected String getSortOrder(Map<String, SortMeta> sortBy) {
    	
    	if(sortBy != null && sortBy.isEmpty() == false) {
    		SortMeta sortM = sortBy.entrySet().stream().findFirst().get().getValue();
    		return sortM.getOrder().toString();
    	}
    	
    	return null;
    }
    
    /**
     * Realiza a conversão do padrão do Primefaces para o nosso padrão, de modo que a camada de negócio não conhecerá a existência de primefaces.
     * 
     * @param sortBy
     * @return O primeiro elemento do map é a chave e o segundo é se o mesmo é ASCENDING OU DESCENDING
     */
    protected Map<String, String> getSortMap(Map<String, SortMeta> sortBy) {
    	
    	if(sortBy != null) {
    		Map<String, String> map = new HashMap<String, String>();
    		
    		for(String k : sortBy.keySet()) {
    			map.put(sortBy.get(k).getField(), sortBy.get(k).getOrder().toString());
    		}
    		
    		return map;
    	}
    	
    	return null;
    }
    
    /**
     * Realiza a conversão do padrão do Primefaces para o nosso padrão, de modo que a camada de negócio não conhecerá a existência de primefaces.
     * 
     * @param sortBy
     * @return O primeiro elemento do map é a chave e o segundo é o valor propriamente dito.
     */
    protected Map<String, Object> getFilterMap(Map<String, FilterMeta> filterBy) {
    	
    	if(filterBy != null) {
    		Map<String, Object> map = new HashMap<String, Object>();
    		
    		for(String k : filterBy.keySet()) {    			
    			String filterField = filterBy.get(k).getField();
    			Object filterValue = filterBy.get(k).getFilterValue();
    			
    			if(filterValue != null) {
    				map.put(filterField, filterValue);
    			}
    		}
    		
    		return map;
    	}
    	
    	return null;
    }
}
and the other example.

Code: Select all

lazyModel = new LazyDataModel<DisciplinaVO>() {

			List<DisciplinaVO> lista;

			@Override
			public List<DisciplinaVO> load(int first, int pageSize, Map<String, SortMeta> sortBy, Map<String, FilterMeta> filterBy) {

				try {

					String sortField = getSortField(sortBy);
					String sortOrder = getSortOrder(sortBy);
					Map<String, Object> filters = getFilterMap(filterBy);

					lista = educacaoBeanDFLocal.listarDisciplinas(firstAux, pageSize, sortField, sortOrder, filters, getClienteSelecionado(), null);

					int qtdRows = educacaoBeanDFLocal.listarDisciplinasCount(filters, getClienteSelecionado(), null);

					this.setRowCount(qtdRows);
					return lista;

				} catch (BOValidationException e) {
					logger.error("Ocorreu um erro ao tentar realizar a pesquisa de disciplinas: " + e.getMessage());
					
				} catch (BOException e) {
					logger.error("Ocorreu um erro ao tentar realizar a pesquisa de disciplinas: " + e.getMessage());
				}

				return null;
			}

			@Override
			public DisciplinaVO getRowData(String rowKey) {

				for (DisciplinaVO aux : lista) {
					if (aux.getId().equals(new BigInteger(rowKey))) {
						return aux;
					}
				}
				return null;
			}

			@Override
			public String getRowKey(DisciplinaVO disciplina) {
				return disciplina.getId().toString();
			}

			/**
			 * Corrige o problema da Divisão por Zero
			 */
			@Override
			public void setRowIndex(int rowIndex) {

				if (rowIndex == -1 || getPageSize() == 0) {
					super.setRowIndex(-1);
				} else
					super.setRowIndex(rowIndex % getPageSize());
			}
		};

NourDev
Posts: 30
Joined: 13 Oct 2015, 20:25

08 Jun 2021, 18:02

Thanks. I test and fix filters on LazyModel.it works.
I get the below exception after updating the date and refresh dataTable content.
com.sun.faces.context.PartialViewContextImpl processPartial
INFO: java.lang.ClassCastException: class javax.faces.component.UIPanel cannot be cast to class javax.faces.component.ValueHolder (javax.faces.component.UIPanel and javax.faces.component.ValueHolder are in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @435ce306)
java.lang.ClassCastException: class javax.faces.component.UIPanel cannot be cast to class javax.faces.component.ValueHolder (javax.faces.component.UIPanel and javax.faces.component.ValueHolder are in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @435ce306)
at org.primefaces.component.api.UITable.lambda$updateFilterByValuesWithFilterRequest$3(UITable.java:253)
at org.primefaces.component.api.ColumnAware.forEachColumn(ColumnAware.java:77)
at org.primefaces.component.api.ColumnAware.forEachColumn(ColumnAware.java:52)
at org.primefaces.component.api.ColumnAware.forEachColumn(ColumnAware.java:48)
at org.primefaces.component.api.UITable.updateFilterByValuesWithFilterRequest(UITable.java:232)
at org.primefaces.component.datatable.feature.FilterFeature.decode(FilterFeature.java:78)
at org.primefaces.component.datatable.DataTable.processValidators(DataTable.java:295)
at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:509)
at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
at org.primefaces.component.api.UIData.visitTree(UIData.java:691)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1623)
at javax.faces.component.UIForm.visitTree(UIForm.java:371)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1623)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1623)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1623)
at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:377)
at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:252)
at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:65)
at org.richfaces.context.ExtendedPartialViewContextImpl.processPartial(ExtendedPartialViewContextImpl.java:199)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1170)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)

iexpertise
Posts: 26
Joined: 28 Oct 2014, 15:21

10 Jun 2021, 22:06

NourDev wrote:
08 Jun 2021, 18:02
Thanks. I test and fix filters on LazyModel.it works.
I have never seen this problem before. Sorry!

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 26 guests