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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.megatim.apifdxweb.dao.ifaces.referentiel;
 
import com.megatim.apifdxweb.model.searchentities.RoutageSearch;
import com.megatim.fdxcommons.dao.ifaces.abstracts.CustomDAOIfaces;
import com.megatim.fdxcommons.model.referentiel.Participant;
import com.megatim.fdxcommons.model.referentiel.Routage;
import com.megatim.fdxcommons.model.referentiel.RoutageId;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import javax.persistence.EntityManager;
import javax.persistence.Query;
 
/**
 *
 * @author ASUS
 */
public interface RoutageDAO extends CustomDAOIfaces<Routage, RoutageId, RoutageSearch> {
 
    @Override
    EntityManager getEntityManager();
 
    @Override
    public default Class<Routage> getManagedEntityClass() {
        return (Routage.class);
    }
 
    @Override
    public default Routage getById(RoutageId id) {
        Query query = getEntityManager().createQuery("SELECT r FROM Routage r"
                + " LEFT JOIN FETCH r.groupeNoeudsEnvoi LEFT JOIN FETCH r.groupeNoeudsReception"
                + " LEFT JOIN FETCH r.groupeParticipantsEnvoi LEFT JOIN FETCH r.groupeParticipantsReception"
                + " LEFT JOIN FETCH r.noeudsEnvoi LEFT JOIN FETCH r.noeudsReception"
                + " LEFT JOIN FETCH r.groupeNoeudsReception LEFT JOIN FETCH r.participantsReception"
                + " LEFT JOIN FETCH r.groupeParticipantsReception LEFT JOIN FETCH r.participantsEnvoi"
                + " WHERE r.referentiel.version = :referentielVersion AND r.typeFichier.code = :codeTypeFichier");
 
        query.setParameter("codeTypeFichier", id.getTypeFichier());
        query.setParameter("referentielVersion", id.getReferentiel());
 
        Optional<Routage> optionalRoutage = query.getResultList().stream().findFirst();
 
        if (optionalRoutage.isPresent()) {
            return optionalRoutage.get();
        } else {
            return null;
        }
    }
 
    public default List<Participant> getParticipantsForSend(String versionReferentiel, String codeTypeFichier) {
        List<Participant> participantsEnvoi = new ArrayList<>();
        Optional<Routage> optRoutage = getEntityManager().createQuery("SELECT r FROM Routage r "
                + "LEFT JOIN r.participantsEnvoi p "
                + "LEFT JOIN r.noeudsEnvoi n "
                + "LEFT JOIN r.groupeParticipantsEnvoi gp LEFT JOIN gp.participants pa "
                + "LEFT JOIN r.groupeNoeudsEnvoi gn LEFT JOIN gn.noeuds no "
                + "WHERE r.referentiel.version = :versionReferentiel AND r.typeFichier.code = :codeTypeFichier ")
                .setParameter("versionReferentiel", versionReferentiel)
                .setParameter("codeTypeFichier", codeTypeFichier)
                .getResultList().stream().findFirst();
 
        if (optRoutage.isPresent()) {
            Routage routage = optRoutage.get();
            participantsEnvoi.addAll(routage.getParticipantsEnvoi());
            routage.getNoeudsEnvoi().stream().forEach(n -> {
                participantsEnvoi.add(n.getParticipant());
            });
            routage.getGroupeNoeudsEnvoi().stream().forEach(gn -> gn.getNoeuds().stream().forEach(n -> participantsEnvoi.add(n.getParticipant())));
            routage.getGroupeParticipantsEnvoi().stream().forEach(gp -> participantsEnvoi.addAll(gp.getParticipants()));
        }
 
        return participantsEnvoi;
    }
 
    default List<Participant> getParticipantForReceive(String versionReferentiel, String codeTypeFichier) {
        List<Participant> participantsReception = new ArrayList<>();
        Optional<Routage> optRoutage = getEntityManager().createQuery("SELECT r FROM Routage r "
                + "LEFT JOIN r.participantsReception p "
                + "LEFT JOIN r.noeudsReception n "
                + "LEFT JOIN r.groupeParticipantsReception gp LEFT JOIN gp.participants pa "
                + "LEFT JOIN r.groupeNoeudsReception gn LEFT JOIN gn.noeuds no "
                + "WHERE r.referentiel.version = :versionReferentiel AND r.typeFichier.code = :codeTypeFichier ")
                .setParameter("versionReferentiel", versionReferentiel)
                .setParameter("codeTypeFichier", codeTypeFichier)
                .getResultList().stream().findFirst();
 
        if (optRoutage.isPresent()) {
            Routage routage = optRoutage.get();
            participantsReception.addAll(routage.getParticipantsReception());
            routage.getNoeudsReception().stream().forEach(n -> {
                participantsReception.add(n.getParticipant());
            });
            routage.getGroupeNoeudsReception().stream().forEach(gn -> gn.getNoeuds().stream().forEach(n -> participantsReception.add(n.getParticipant())));
            routage.getGroupeParticipantsReception().stream().forEach(gp -> participantsReception.addAll(gp.getParticipants()));
        }
 
        return participantsReception;
    }
 
    Long countRoutageByReferentielTypeFichierForParticipantForReceive(String versionReferentiel, String codeTypeFichier, Participant participant);
 
    Long countRoutageByReferentielTypeFichierForParticipantForSend(String versionReferentiel, String codeTypeFichier, Participant participant);
 
}