Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* The MIT License
*
* Copyright (c) 2009-2023 PrimeTek Informatics
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.primefaces.model;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.el.ELException;
import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.*;
import org.primefaces.util.BeanUtils;
import org.primefaces.util.LangUtils;
import org.primefaces.util.Lazy;
import org.primefaces.util.SerializableSupplier;
/**
* Basic {@link LazyDataModel} implementation with JPA and Criteria API.
*
* @param The model class.
*/
public class JpaLazyDataModel extends LazyDataModel implements Serializable {
private static final Logger LOG = Logger.getLogger(JpaLazyDataModel.class.getName());
protected Class entityClass;
protected SerializableSupplier entityManager;
protected String rowKeyField;
private transient Lazy rowKeyGetter;
/**
* For serialization only
*/
public JpaLazyDataModel() {
}
/**
* Constructs a JpaLazyDataModel for usage without enabled selection.
*
* @param entityClass The entity class
* @param entityManager The {@link EntityManager}
*/
public JpaLazyDataModel(Class entityClass, SerializableSupplier entityManager) {
this.entityClass = entityClass;
this.entityManager = entityManager;
}
/**
* Constructs a JpaLazyDataModel with selection support.
*
* @param entityClass The entity class
* @param entityManager The {@link EntityManager}
* @param rowKeyField The name of the rowKey property (e.g. "id")
*/
public JpaLazyDataModel(Class entityClass, SerializableSupplier entityManager, String rowKeyField) {
this(entityClass, entityManager);
this.rowKeyField = rowKeyField;
}
/**
* Constructs a JpaLazyDataModel with selection support, with an already existing {@link Converter}.
*
* @param entityClass The entity class
* @param entityManager The {@link EntityManager}
* @param converter The converter, which will be used for converting the entity to a rowKey and vice versa
*/
public JpaLazyDataModel(Class entityClass, SerializableSupplier entityManager, Converter converter) {
super(converter);
this.entityClass = entityClass;
this.entityManager = entityManager;
}
@Override
public int count(Map filterBy) {
EntityManager em = this.entityManager.get();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(Long.class);
Root root = cq.from(entityClass);
cq = cq.select(cb.count(root));
applyFilters(cb, cq, root, filterBy);
return em.createQuery(cq).getSingleResult().intValue();
}
@Override
public List load(int first, int pageSize, Map sortBy, Map filterBy) {
EntityManager em = this.entityManager.get();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(entityClass);
Root root = cq.from(entityClass);
cq = cq.select(root);
applyFilters(cb, cq, root, filterBy);
applySort(cb, cq, root, sortBy);
TypedQuery query = em.createQuery(cq);
query.setFirstResult(first);
query.setMaxResults(pageSize);
List result = query.getResultList();
return result;
}
protected void applyFilters(CriteriaBuilder cb,
CriteriaQuery cq,
Root root,
Map filterBy) {
List predicates = new ArrayList<>();
applyGlobalFilters(filterBy, cb, cq, root, predicates);
if (filterBy != null) {
for (FilterMeta filter : filterBy.values()) {
if (filter.getField() == null || filter.getFilterValue() == null || filter.isGlobalFilter()) {
continue;
}
Field filterField = LangUtils.getFieldRecursive(entityClass, filter.getField());
Object filterValue = filter.getFilterValue();
Object convertedFilterValue;
Class filterValueClass = filterValue.getClass();
if (filterValueClass.isArray() || Collection.class.isAssignableFrom(filterValueClass)) {
convertedFilterValue = filterValue;
}
else {
convertedFilterValue = convertToType(filterValue, filterField.getType());
}
Expression fieldExpression = resolveFieldExpression(cb, cq, root, filter.getField());
Predicate predicate = createPredicate(filter, filterField, root, cb, fieldExpression, convertedFilterValue);
predicates.add(predicate);
}
}
if (!predicates.isEmpty()) {
cq.where(
cb.and(predicates.toArray(new Predicate[predicates.size()])));
}
}
protected void applyGlobalFilters(Map filterBy, CriteriaBuilder cb, CriteriaQuery cq,
Root root, List predicates) {
}
protected Predicate createPredicate(FilterMeta filter, Field filterField,
Root root, CriteriaBuilder cb, Expression fieldExpression, Object filterValue) {
Lazy> fieldExpressionAsString = new Lazy(() -> fieldExpression.as(String.class));
Lazy> filterValueAsCollection = new Lazy(
() -> filterValue.getClass().isArray() ? Arrays.asList((Object[]) filterValue)
: (Collection