-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RI-112: [admin/companies] ajoute un composant de filtre au tableau de…
…s compagnies (#168)
- Loading branch information
1 parent
252dcdd
commit c22af2e
Showing
31 changed files
with
421 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...src/main/java/com/dynonuggets/refonteimplicaction/repository/CompanyRepositoryCustom.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.dynonuggets.refonteimplicaction.repository; | ||
|
||
import com.dynonuggets.refonteimplicaction.model.Company; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
public interface CompanyRepositoryCustom { | ||
/** | ||
* @param pageable l'objet de pagination | ||
* @param keyword la chaîne de caractère à rechercher dans les champs keyword, description | ||
* @return la liste de résultats paginée des Company correspondant aux critères | ||
*/ | ||
Page<Company> findAllWithCriteria(final Pageable pageable, final String keyword); | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
.../main/java/com/dynonuggets/refonteimplicaction/repository/impl/CompanyRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.dynonuggets.refonteimplicaction.repository.impl; | ||
|
||
import com.dynonuggets.refonteimplicaction.model.Company; | ||
import com.dynonuggets.refonteimplicaction.repository.CompanyRepositoryCustom; | ||
import lombok.AllArgsConstructor; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.query.QueryUtils; | ||
import org.springframework.data.support.PageableExecutionUtils; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.criteria.CriteriaBuilder; | ||
import javax.persistence.criteria.CriteriaQuery; | ||
import javax.persistence.criteria.Predicate; | ||
import javax.persistence.criteria.Root; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@AllArgsConstructor | ||
public class CompanyRepositoryImpl implements CompanyRepositoryCustom { | ||
private final EntityManager entityManager; | ||
|
||
@Override | ||
public Page<Company> findAllWithCriteria(Pageable pageable, String keyword) { | ||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); | ||
CriteriaQuery<Company> query = criteriaBuilder.createQuery(Company.class); | ||
Root<Company> queryRoot = query.from(Company.class); | ||
List<Predicate> predicates = new ArrayList<>(); | ||
|
||
// gestion du tri des résultats | ||
if (!pageable.getSort().isEmpty()) { | ||
query.orderBy(QueryUtils.toOrders(pageable.getSort(), queryRoot, criteriaBuilder)); | ||
} | ||
|
||
// recherche par mot clé dans le nom et description | ||
if (StringUtils.isNotEmpty(keyword)) { | ||
keyword = "%" + keyword + "%"; | ||
Predicate namePredicate = criteriaBuilder.like(queryRoot.get("name"), keyword); | ||
Predicate descriptionPredicate = criteriaBuilder.like(queryRoot.get("description"), keyword); | ||
predicates.add(criteriaBuilder.or(namePredicate, descriptionPredicate)); | ||
} | ||
|
||
|
||
// combinaison des différents prédicats | ||
Predicate finalPredicate = criteriaBuilder.and(predicates.toArray(new Predicate[0])); | ||
|
||
query.where(finalPredicate); | ||
|
||
CriteriaQuery<Long> countQuery = criteriaBuilder.createQuery(Long.class); | ||
countQuery.select(criteriaBuilder.count(countQuery.from(Company.class))); | ||
final Long totalCount = entityManager.createQuery(countQuery).getSingleResult(); | ||
final int firstResult = pageable.getPageNumber() * pageable.getPageSize(); | ||
|
||
// lancement de la requête et mise en place de la pagination | ||
final List<Company> results = entityManager.createQuery(query) | ||
.setFirstResult(firstResult) | ||
.setMaxResults(pageable.getPageSize()) | ||
.getResultList(); | ||
return PageableExecutionUtils.getPage(results, pageable, () -> totalCount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.