package com.megatim.fdxconsultation.dao.searchs;
|
|
import com.megatim.fdxcommons.model.referentiel.Participant;
|
import com.megatim.fdxconsultation.dao.ifaces.searchs.UserParticipantDAO;
|
import com.megatim.fdxconsultation.model.searchentities.ParticipantSearch;
|
import java.util.List;
|
import javax.enterprise.context.Dependent;
|
import javax.persistence.EntityManager;
|
import javax.persistence.PersistenceContext;
|
import javax.persistence.Query;
|
|
/**
|
*
|
* @author Gabuntu
|
*/
|
@Dependent
|
public class UserParticipantDAOIml implements UserParticipantDAO {
|
|
@PersistenceContext(unitName = "fdxConsultationPersistenceUnit")
|
protected EntityManager em;
|
|
@Override
|
public List<Participant> findUserParticipantsWithPagination(String userName, ParticipantSearch participantSearch, int pageNumber, int pageSize) {
|
StringBuilder queryString = new StringBuilder("SELECT t from Participant t ")
|
.append(" WHERE ((SELECT COALESCE(COUNT(u), 0) FROM User u JOIN u.participants tf WHERE u.userName = :username AND tf = t) > 0)");
|
extendsQuery(queryString, participantSearch);
|
Query query = em.createQuery(queryString.toString()).setParameter("username", userName);
|
extendsParameter(query, participantSearch);
|
query.setFirstResult((pageNumber - 1) * pageSize).setMaxResults(pageSize);
|
return (List<Participant>) query.getResultList();
|
}
|
|
@Override
|
public List<Participant> findAllUserParticipants(String userName, ParticipantSearch participantSearch) {
|
StringBuilder queryString = new StringBuilder("SELECT t from Participant t ")
|
.append(" WHERE ((SELECT COALESCE(COUNT(u), 0) FROM User u JOIN u.participants tf WHERE u.userName = :username AND tf = t) > 0)");
|
extendsQuery(queryString, participantSearch);
|
Query query = em.createQuery(queryString.toString()).setParameter("username", userName);
|
extendsParameter(query, participantSearch);
|
return (List<Participant>) query.getResultList();
|
}
|
|
@Override
|
public List<Participant> findUserParticipants(String userName) {
|
return (List<Participant>) em.createQuery("SELECT t from Participant t "
|
+ " WHERE ((SELECT COALESCE(COUNT(u), 0) FROM User u JOIN u.participants tf WHERE u.userName = :username AND tf = t) > 0)")
|
.setParameter("username", userName)
|
.getResultList();
|
}
|
|
@Override
|
public Long countUserParticipants(String userName, ParticipantSearch participantSearch) {
|
StringBuilder queryString = new StringBuilder("SELECT t from Participant t ")
|
.append(" WHERE ((SELECT COALESCE(COUNT(u), 0) FROM User u JOIN u.participants tf WHERE u.userName = :username AND tf = t) > 0)");
|
extendsQuery(queryString, participantSearch);
|
Query query = em.createQuery(queryString.toString()).setParameter("username", userName);
|
extendsParameter(query, participantSearch);
|
return (long) query.getResultList().size();
|
}
|
|
private void extendsQuery(StringBuilder queryString, ParticipantSearch participantSearch) {
|
if (participantSearch.getCode() != null) {
|
queryString.append(" AND t.code LIKE :code");
|
}
|
if (participantSearch.getLibelle() != null) {
|
queryString.append(" AND t.libelle LIKE :libelle");
|
}
|
}
|
|
private void extendsParameter(Query query, ParticipantSearch participantSearch) {
|
if (participantSearch.getCode() != null) {
|
//System.out.println("typeFichierSearch.getCode() " + "%" + typeFichierSearch.getCode() + "%");
|
query.setParameter("code", "%" + participantSearch.getCode() + "%");
|
}
|
if (participantSearch.getLibelle() != null) {
|
//System.out.println("typeFichierSearch.getCode() " + "%" + typeFichierSearch.getCode() + "%");
|
query.setParameter("libelle", "%" + participantSearch.getLibelle() + "%");
|
}
|
}
|
|
}
|