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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package com.megatim.fdxconsultation.dao.impl.stats;
 
import com.megatim.fdxcommons.model.referentiel.TypeFichier;
import com.megatim.fdxconsultation.dao.ifaces.stats.CriteriaEntityPersistedDAO;
import com.megatim.fdxconsultation.dao.ifaces.stats.TableConfigurationDAO;
import com.megatim.fdxconsultation.dao.ifaces.stats.TableauBordDAO;
import com.megatim.fdxconsultation.model.dto.ParticipantToFichierDto;
import com.megatim.fdxconsultation.model.stats.CriteriaEntityPersisted;
import com.megatim.fdxconsultation.model.stats.TableauBord;
import com.megatim.fdxconsultation.model.enums.TypeConfigurationTableauBord;
import com.megatim.fdxconsultation.model.searchentities.TypeFichierSearch;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.Tuple;
 
/**
 *
 * @author ASUS
 */
@Stateless
public class TableauBordDAOImpl implements TableauBordDAO {
 
    @PersistenceContext(unitName = "fdxConsultationPersistenceUnit")
    protected EntityManager em;
 
    @EJB
    TableConfigurationDAO tableConfigurationDao;
 
    @EJB
    CriteriaEntityPersistedDAO criteriaDao;
 
    @Override
    public Class<TableauBord> getManagedEntityClass() {
        return TableauBord.class;
    }
 
    @Override
    public EntityManager getEntityManager() {
        return em;
    }
 
    @Override
    public TableauBord getById(Long id) {
        Query query = em.createQuery("SELECT t FROM TableauBord t LEFT JOIN FETCH t.tableConfigurations tc LEFT JOIN FETCH tc.groupingColumns WHERE t.id = :id");
        query.setParameter("id", id);
 
        TableauBord tableauBord = (TableauBord) query.getResultList().stream().findFirst().orElse(null);
        if (tableauBord != null) {
            loadCriteria(tableauBord);
        }
 
        return tableauBord;
    }
 
    @Override
    public TableauBord findByUserAndTypeFichierAndReferentiel(Long userId, String codeTypeFichier, String referentielVersion) {
        Query query = em.createQuery("SELECT t from TableauBord t LEFT JOIN FETCH t.tableConfigurations tc LEFT JOIN FETCH tc.groupingColumns "
                + "WHERE t.user.id = :userId and t.typeFichier.code = :codeTypeFichier AND t.referentiel.version = :referentielVersion")
                .setParameter("codeTypeFichier", codeTypeFichier)
                .setParameter("userId", userId)
                .setParameter("referentielVersion", referentielVersion);
 
        TableauBord tableauBord = (TableauBord) query.getResultList().stream().findFirst().orElse(null);
        if (tableauBord != null) {
            loadCriteria(tableauBord);
        }
 
        return tableauBord;
    }
 
    @Override
    public List<TableauBord> findByUser(Long userId, String refVersion) {
        Query query = em.createQuery("SELECT t FROM TableauBord t"
                + " LEFT JOIN FETCH t.tableConfigurations tc LEFT JOIN FETCH tc.groupingColumns"
                + " WHERE t.user.id = :userId AND t.referentiel.version = :refVersion")
                .setParameter("userId", userId)
                .setParameter("refVersion", refVersion);
 
        List<TableauBord> liste = query.getResultList();
        liste.forEach(t -> loadCriteria(t));
 
        return liste;
    }
 
    @Override
    public List<ParticipantToFichierDto> tableauBordToTypeFichiers(String userName, String referentielVersion) {
        List<Tuple> results = em.createQuery("SELECT t.participant.code, t.code AS code FROM TableauBord tb, TypeFichier t"
                + " WHERE tb.user.userName = :userName AND tb.referentiel.version = :referentielVersion"
                + " AND tb.typeFichier.code = t.code", Tuple.class)
                .setParameter("userName", userName)
                .setParameter("referentielVersion", referentielVersion)
                .getResultList();
 
        return results.stream().map(t -> {
            ParticipantToFichierDto dto = new ParticipantToFichierDto(t.get(0, String.class), t.get(1, String.class));
            return dto;
        }).collect(Collectors.toList());
    }
 
    @Override
    public List<ParticipantToFichierDto> tableauBordToTypeFichiers(TypeConfigurationTableauBord tableauBordConfiguration, String referentielVersion) {
        List<Tuple> results = em.createQuery("SELECT t.participant.code, t.code AS code from TableauBord tb, TypeFichier t"
                + " WHERE tb.typeConfiguration = :tableauBordConfiguration"
                + " AND tb.referentiel.version = :referentielVersion AND tb.typeFichier.code = t.code ", Tuple.class)
                .setParameter("tableauBordConfiguration", tableauBordConfiguration)
                .setParameter("referentielVersion", referentielVersion)
                .getResultList();
 
        return results.stream().map(t -> {
            ParticipantToFichierDto dto = new ParticipantToFichierDto(t.get(0, String.class), t.get(1, String.class));
            return dto;
        }).collect(Collectors.toList());
    }
 
    @Override
    public List<TableauBord> findByReferentiel(String referentielVersion) {
        return em.createQuery("SELECT t from TableauBord t"
                + " WHERE t.referentiel.version = :referentielVersion", TableauBord.class)
                .setParameter("referentielVersion", referentielVersion)
                .getResultList();
 
    }
 
    @Override
    public TableauBord findByTypeConfigurationAndReferentiel(String codeTypeFichier, String referentielVersion, TypeConfigurationTableauBord typeConfiguration) {
        Query query = em.createQuery("SELECT t from TableauBord t LEFT JOIN FETCH t.tableConfigurations tc LEFT JOIN FETCH tc.groupingColumns "
                + "WHERE t.typeConfiguration = :typeConfiguration and t.typeFichier.code = :codeTypeFichier AND t.referentiel.version = :referentielVersion")
                .setParameter("codeTypeFichier", codeTypeFichier)
                .setParameter("typeConfiguration", typeConfiguration)
                .setParameter("referentielVersion", referentielVersion);
 
        Optional<TableauBord> optionalTableauBord = query.getResultList().stream().findFirst();
        TableauBord tableauBord = optionalTableauBord.isPresent() ? optionalTableauBord.get() : null;
 
        if (tableauBord == null) {
            return null;
        }
        loadCriteria(tableauBord);
 
        return tableauBord;
    }
 
    private void loadCriteria(TableauBord tableauBord) {
        if (tableauBord.getTableConfigurations() != null && !tableauBord.getTableConfigurations().isEmpty()) {
            tableauBord.getTableConfigurations().stream().forEach(t -> {
                if (t.getCriterion() != null) {
                    CriteriaEntityPersisted criterion = criteriaDao.loadSubCriteria(t.getCriterion());
                    t.setCriterion(criterion);
                }
            });
        }
    }
 
    @Override
    public List<TypeFichier> tableauBordTypeFichiers(String userName, String referentielVersion, TypeFichierSearch typeFichierSearch, int pageNumber, int pageSize) {
 
        StringBuilder builder = new StringBuilder();
        builder.append("SELECT t.typeFichier FROM TableauBord t")
                .append(" ")
                .append("WHERE t.typeConfiguration = :typeConfiguration AND t.user.userName = :userName AND t.referentiel.version = :referentielVersion");
 
        addQuerySearchPart(builder, typeFichierSearch);
 
        Query query = em.createQuery(builder.toString());
        query.setParameter("userName", userName);
 
        addQuerySearchParam(query, referentielVersion, TypeConfigurationTableauBord.USER_SPECIFIC, typeFichierSearch);
        query.setFirstResult((pageNumber - 1) * pageSize).setMaxResults(pageSize);
 
        return query.getResultList();
    }
 
    @Override
    public Long countTableauBordTypeFichiers(String userName, String referentielVersion, TypeFichierSearch typeFichierSearch) {
 
        StringBuilder builder = new StringBuilder();
        builder.append("SELECT COALESCE(COUNT(t.typeFichier), 0) FROM TableauBord t")
                .append(" ")
                .append("WHERE t.typeConfiguration = :typeConfiguration AND t.user.userName = :userName AND t.referentiel.version = :referentielVersion");
 
        addQuerySearchPart(builder, typeFichierSearch);
 
        Query query = em.createQuery(builder.toString());
        query.setParameter("userName", userName);
 
        addQuerySearchParam(query, referentielVersion, TypeConfigurationTableauBord.USER_SPECIFIC, typeFichierSearch);
 
        return (Long) query.getSingleResult();
    }
 
    @Override
    public List<TypeFichier> tableauBordGlobalTypeFichiers(String referentielVersion, TypeFichierSearch typeFichierSearch, int pageNumber, int pageSize) {
 
        StringBuilder builder = new StringBuilder();
        builder.append("SELECT t.typeFichier FROM TableauBord t")
                .append(" ")
                .append("WHERE t.typeConfiguration = :typeConfiguration AND t.referentiel.version = :referentielVersion");
 
        addQuerySearchPart(builder, typeFichierSearch);
 
        Query query = em.createQuery(builder.toString());
        addQuerySearchParam(query, referentielVersion, TypeConfigurationTableauBord.GLOBAL, typeFichierSearch);
        query.setFirstResult((pageNumber - 1) * pageSize).setMaxResults(pageSize);
 
        return query.getResultList();
    }
 
    @Override
    public Long countTableauBordGlobalTypeFichiers(String referentielVersion, TypeFichierSearch typeFichierSearch) {
 
        StringBuilder builder = new StringBuilder();
        builder.append("SELECT COALESCE(COUNT(t.typeFichier), 0) FROM TableauBord t")
                .append(" ")
                .append("WHERE t.typeConfiguration = :typeConfiguration AND t.referentiel.version = :referentielVersion");
 
        addQuerySearchPart(builder, typeFichierSearch);
 
        Query query = em.createQuery(builder.toString());
        addQuerySearchParam(query, referentielVersion, TypeConfigurationTableauBord.GLOBAL, typeFichierSearch);
 
        return (Long) query.getSingleResult();
    }
 
    private void addQuerySearchParam(Query query, String referentielVersion, TypeConfigurationTableauBord typeConfigurationTableauBord, TypeFichierSearch typeFichierSearch) {
 
        query.setParameter("referentielVersion", referentielVersion)
                .setParameter("typeConfiguration", typeConfigurationTableauBord);
 
        if (typeFichierSearch.getCode() != null) {
            query.setParameter("code", "%" + typeFichierSearch.getCode() + "%");
        }
 
        if (typeFichierSearch.getLibelle() != null) {
            query.setParameter("libelle", "%" + typeFichierSearch.getLibelle() + "%");
        }
 
        if (typeFichierSearch.getDescription() != null) {
            query.setParameter("description", "%" + typeFichierSearch.getDescription() + "%");
        }
    }
 
    private void addQuerySearchPart(StringBuilder builder, TypeFichierSearch typeFichierSearch) {
 
        if (typeFichierSearch.getCode() != null) {
            builder.append(" AND ");
            builder.append("t.typeFichier.code LIKE :code");
        }
 
        if (typeFichierSearch.getLibelle() != null) {
            builder.append(" AND ");
            builder.append("t.typeFichier.libelle LIKE :libelle");
        }
 
        if (typeFichierSearch.getDescription() != null) {
            builder.append(" AND ");
            builder.append("t.typeFichier.description LIKE :description");
        }
    }
 
    @Override
    public TableauBord findSpecificTableauBord(Long userId, String codeTypeFichier, String referentielVersion) {
 
        Query query = em.createQuery("SELECT t from TableauBord t LEFT JOIN FETCH t.tableConfigurations tc LEFT JOIN FETCH tc.groupingColumns "
                + "WHERE t.user.id = :userId and t.typeFichier.code = :codeTypeFichier AND t.referentiel.version = :referentielVersion "
                + "AND t.typeConfiguration = :typeConfiguration")
                .setParameter("codeTypeFichier", codeTypeFichier)
                .setParameter("userId", userId)
                .setParameter("referentielVersion", referentielVersion)
                .setParameter("typeConfiguration", TypeConfigurationTableauBord.USER_SPECIFIC);
 
        TableauBord tableauBord = (TableauBord) query.getResultList().stream().findFirst().orElse(null);
        if (tableauBord != null) {
            loadCriteria(tableauBord);
        }
 
        return tableauBord;
    }
 
    @Override
    public TableauBord findGlobalTableauBord(String codeTypeFichier, String referentielVersion) {
 
        Query query = em.createQuery("SELECT t FROM TableauBord t LEFT JOIN FETCH t.tableConfigurations tc LEFT JOIN FETCH tc.groupingColumns "
                + "WHERE t.typeFichier.code = :codeTypeFichier AND t.referentiel.version = :referentielVersion "
                + "AND t.typeConfiguration = :typeConfiguration")
                .setParameter("codeTypeFichier", codeTypeFichier)
                .setParameter("referentielVersion", referentielVersion)
                .setParameter("typeConfiguration", TypeConfigurationTableauBord.GLOBAL);
 
        TableauBord tableauBord = (TableauBord) query.getResultList().stream().findFirst().orElse(null);
        if (tableauBord != null) {
            loadCriteria(tableauBord);
        }
 
        return tableauBord;
    }
}