Kenmegne
7 days ago 23a46b4be35277e06ec89f48730eeb694e686be8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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() + "%");
        }
    }
 
}