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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package com.megatim.fdxconsultation.core.impl.stats;
 
import com.megatim.fdxcommons.core.ifaces.helper.DataInMemoryHandler;
import com.megatim.fdxcommons.model.integration.ColumnDefinition;
import com.megatim.fdxcommons.model.referentiel.TypeFichier;
import com.megatim.fdxcommons.tools.database.connection.DBConnection;
import com.megatim.fdxcommons.tools.database.exceptions.BadQueryCriteriaException;
import com.megatim.fdxcommons.tools.database.exceptions.LocalDateTimeValueParseError;
import com.megatim.fdxcommons.tools.database.tables.FdxTable;
import com.megatim.fdxcommons.tools.database.tables.FdxTableColumns;
import com.megatim.fdxcommons.tools.exceptions.CommonApplicationValidationException;
import com.megatim.fdxcommons.tools.exceptions.CommonRessourceNotFoundException;
import com.megatim.fdxconsultation.core.ifaces.integration.ColumnDefinitionManager;
import com.megatim.fdxconsultation.core.ifaces.referentiel.ReferentielManager;
import com.megatim.fdxconsultation.core.ifaces.stats.TableauBordManager;
import com.megatim.fdxconsultation.core.impl.factory.FdxConsultationTableFactory;
import com.megatim.fdxconsultation.dao.ifaces.abstracts.PaginationWithSearchEnityDAO;
import com.megatim.fdxconsultation.dao.ifaces.stats.TableauBordDAO;
import com.megatim.fdxconsultation.model.administration.User;
import com.megatim.fdxconsultation.model.dto.stats.StatResultDto;
import com.megatim.fdxconsultation.model.stats.TableauBord;
import com.megatim.fdxconsultation.model.enums.TypeConfigurationTableauBord;
import com.megatim.fdxconsultation.model.searchentities.TableauBordSearch;
import com.megatim.fdxconsultation.model.stats.TableConfiguration;
import com.megatim.fdxconsultation.model.stats.dto.TableauBordRequest;
import com.megatim.fdxconsultation.tools.context.AppCommonContext;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.naming.NamingException;
import javax.sql.DataSource;
 
/**
 *
 * @author ASUS
 */
@Dependent
public class TableauBordManagerImpl implements TableauBordManager {
 
    @Resource(lookup = AppCommonContext.JNDI_NAME)
    private DataSource dataSource;
 
    @EJB
    private TableauBordDAO dao;
 
    @Inject
    private DataInMemoryHandler dataInMemoryHandler;
 
    @Inject
    private ReferentielManager refManager;
 
    @Inject
    private ColumnDefinitionManager columnDefinitionManager;
 
    @Override
    public TableauBordDAO getDao() {
        return dao;
    }
 
    @Override
    public PaginationWithSearchEnityDAO<TableauBord, Long, TableauBordSearch> getPaginationDao() {
        return dao;
    }
 
    @Override
    public List<TableauBord> findByReferentiel(String referentielVersion) {
        return getDao().findByReferentiel(referentielVersion);
    }
 
    @Override
    public TableauBord findByUserAndTypeFichierAndReferentiel(Long userId, String codeTypeFichier, String referentielVersion) {
        TableauBord tableauBord = getDao().findByUserAndTypeFichierAndReferentiel(userId, codeTypeFichier, referentielVersion);
        if (tableauBord == null) {
            throw new CommonRessourceNotFoundException("La ressource ayant le fichier " + codeTypeFichier + " et le référentiel " + referentielVersion + " est introuvable");
        }
        return tableauBord;
    }
 
    @Override
    public Map<String, List<String>> userTableauBordToTypeFichiers(String userName) {
        Map<String, List<String>> particpantToFichierDto = new HashMap<>();
        dao.tableauBordToTypeFichiers(userName, dataInMemoryHandler.getDataInMemory().getReferentielEnCours().getVersion()).forEach(p -> {
            String key = p.getCodeParticipant();
            List<String> value = particpantToFichierDto.get(key);
            if (value == null) {
                value = new ArrayList<>();
            }
            value.add(p.getCodeFichier());
            particpantToFichierDto.put(key, value);
        });
        return particpantToFichierDto;
    }
 
    @Override
    public Map<String, List<String>> globalTableauBordToTypeFichiers() {
        Map<String, List<String>> particpantToFichierDto = new HashMap<>();
        dao
                .tableauBordToTypeFichiers(TypeConfigurationTableauBord.GLOBAL, dataInMemoryHandler.getDataInMemory().getReferentielEnCours().getVersion())
                .forEach(p -> {
                    String key = p.getCodeParticipant();
                    List<String> value = particpantToFichierDto.get(key);
                    if (value == null) {
                        value = new ArrayList<>();
                    }
                    value.add(p.getCodeFichier());
                    particpantToFichierDto.put(key, value);
                });
        return particpantToFichierDto;
    }
 
    @Override
    public List<TableauBord> deleteUserTableauBord(User user) {
        //Tous les tableaux de bord associés à l'utilisateur user
        List<TableauBord> tableauxBords = getDao().findByUser(user.getId(), refManager.getReferentielEnCours().getVersion());
        List<TableauBord> tableauxBordsDeleted = new ArrayList<>();
 
        //TypeFichiers auxquels l'utilisateur user à accès
        Map<String, TypeFichier> typeFichiersMap = user.getTypeFichiers()
                .stream()
                .collect(Collectors.toMap(TypeFichier::getCode, Function.identity()));
 
        for (TableauBord t : tableauxBords) {
            //Supprimer les tableaux de bord dont le typefichier n'est plus accessible à user
            if (!typeFichiersMap.containsKey(t.getTypeFichier().getCode())) {
                getDao().delete(t);
                tableauxBordsDeleted.add(t);
            }
        }
        return tableauxBordsDeleted;
    }
 
    @Override
    public TableauBord findByTypeConfigurationAndReferentiel(String codeTypeFichier, String referentielVersion, TypeConfigurationTableauBord typeConfiguration) {
        return getDao().findByTypeConfigurationAndReferentiel(codeTypeFichier, referentielVersion, typeConfiguration);
    }
 
    @Override
    public TableauBord save(TableauBordRequest tableauBordRequest) {
 
        try ( Connection connection = new DBConnection(dataSource).connection()) {
 
            String codeTypeFichier = tableauBordRequest.getTypeFichier().getCode();
            String referentielVersion = tableauBordRequest.getReferentiel().getVersion();
 
            FdxTable fdxTable = FdxConsultationTableFactory.createTable(codeTypeFichier, referentielVersion, connection);
            List<ColumnDefinition> columnDefinitions = fdxTable.tableDefinition(connection).tableDefinition().getColumnDefinitions();
            FdxTableColumns fdxTableColumns = fdxTable.tableColumns(connection);
 
            stopIfRequestIsNotCorrect(codeTypeFichier, referentielVersion, tableauBordRequest, columnDefinitions);
 
            TableauBord tableauBord = new ConvertedTableauBord(tableauBordRequest, columnDefinitions, fdxTableColumns).tableauBord();
            tableauBord.setTypeConfiguration(TypeConfigurationTableauBord.GLOBAL);
            return save(tableauBord);
 
        } catch (BadQueryCriteriaException | LocalDateTimeValueParseError | SQLException | NamingException ex) {
            throw new CommonApplicationValidationException(ex.getMessage());
        }
 
    }
 
    @Override
    public TableauBord save(User user, TableauBordRequest tableauBordRequest) {
 
        try ( Connection connection = new DBConnection(dataSource).connection()) {
 
            String codeTypeFichier = tableauBordRequest.getTypeFichier().getCode();
            String referentielVersion = tableauBordRequest.getReferentiel().getVersion();
 
            FdxTable fdxTable = FdxConsultationTableFactory.createTable(codeTypeFichier, referentielVersion, connection);
            List<ColumnDefinition> columnDefinitions = fdxTable.tableDefinition(connection).tableDefinition().getColumnDefinitions();
            FdxTableColumns fdxTableColumns = fdxTable.tableColumns(connection);
 
            stopIfRequestIsNotCorrect(user.getId(), codeTypeFichier, referentielVersion, tableauBordRequest, columnDefinitions);
 
            TableauBord tableauBord = new ConvertedTableauBord(tableauBordRequest, columnDefinitions, fdxTableColumns).tableauBord();
            tableauBord.setTypeConfiguration(TypeConfigurationTableauBord.USER_SPECIFIC);
            tableauBord.setUser(user);
 
            TableauBord entity = save(tableauBord);
            return entity;
 
        } catch (BadQueryCriteriaException | LocalDateTimeValueParseError | SQLException | NamingException ex) {
            throw new CommonApplicationValidationException(ex.getMessage());
        }
    }
 
    @Override
    public void update(Long id, TableauBordRequest tableauBordRequest) {
 
        try ( Connection connection = new DBConnection(dataSource).connection()) {
 
            TableauBord tableauBord = get(id);
 
            FdxTable fdxTable = FdxConsultationTableFactory.createTable(
                    tableauBord.getTypeFichier().getCode(),
                    tableauBord.getReferentiel().getVersion(),
                    connection
            );
            List<ColumnDefinition> columnDefinitions = fdxTable.tableDefinition(connection).tableDefinition().getColumnDefinitions();
            FdxTableColumns fdxTableColumns = fdxTable.tableColumns(connection);
 
            stopIfTableauBordRequestIncorrect(tableauBordRequest, columnDefinitions);
 
            TableauBord convertedTableauBord
                    = new ConvertedTableauBord(tableauBordRequest, columnDefinitions, fdxTableColumns).tableauBord();
 
            tableauBord.getTableConfigurations().clear();
            for (TableConfiguration tableConfiguration : convertedTableauBord.getTableConfigurations()) {
                tableauBord.getTableConfigurations().add(tableConfiguration);
                tableConfiguration.setTableauBord(tableauBord);
            }
 
            tableauBord.setDescription(convertedTableauBord.getDescription());
 
            save(tableauBord);
 
        } catch (BadQueryCriteriaException | LocalDateTimeValueParseError | SQLException | NamingException ex) {
            throw new CommonApplicationValidationException(ex.getMessage());
        }
    }
 
    @Override
    public TableauBord get(Long id) {
        TableauBord tableauBord = getById(id);
        if (tableauBord == null) {
            throw new CommonRessourceNotFoundException("La ressource ayant l'identifiant " + id + " est introuvable");
        }
        return tableauBord;
    }
 
    @Override
    public void delete(Long id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
 
    @Override
    public StatResultDto getDatas(User user, String referentielVersion, String codeTypeFichier) {
        try {
            return new TableauBordResult(specificTableauBord(user, codeTypeFichier, referentielVersion), dataSource).result();
        } catch (Exception ex) {
            throw new CommonApplicationValidationException(ex.getMessage());
        }
    }
 
    @Override
    public StatResultDto getDatas(String referentielVersion, String codeTypeFichier) {
        try {
            return new TableauBordResult(globalTableauBord(codeTypeFichier, referentielVersion), dataSource).result();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new CommonApplicationValidationException(ex.getMessage());
        }
    }
 
    private TableauBord globalTableauBord(String codeTypeFichier, String referentielVersion) {
        TableauBord tableauBord = getDao().findGlobalTableauBord(codeTypeFichier, referentielVersion);
        if (tableauBord == null) {
            throw new CommonRessourceNotFoundException("La ressource ayant le fichier " + codeTypeFichier + " et le référentiel " + referentielVersion + " est introuvable");
        }
        return tableauBord;
    }
 
    private TableauBord specificTableauBord(User user, String codeTypeFichier, String referentielVersion) {
        TableauBord tableauBord = getDao().findSpecificTableauBord(user.getId(), codeTypeFichier, referentielVersion);
        if (tableauBord == null) {
            throw new CommonRessourceNotFoundException("La ressource ayant le fichier " + codeTypeFichier + " et le référentiel " + referentielVersion + " est introuvable");
        }
        return tableauBord;
    }
 
    private void stopIfRequestIsNotCorrect(String codeTypeFichier, String referentielVersion, TableauBordRequest tableauBordRequest, List<ColumnDefinition> columnDefinitions) {
        stopIfTypeFichierNotStructured(codeTypeFichier, referentielVersion);
        stopIfTableauBordExist(codeTypeFichier, referentielVersion);
        stopIfTableauBordRequestIncorrect(tableauBordRequest, columnDefinitions);
    }
 
    private void stopIfRequestIsNotCorrect(Long userId, String codeTypeFichier, String referentielVersion, TableauBordRequest tableauBordRequest, List<ColumnDefinition> columnDefinitions) {
        stopIfTypeFichierNotStructured(codeTypeFichier, referentielVersion);
        stopIfTableauBordExist(userId, codeTypeFichier, referentielVersion);
        stopIfTableauBordRequestIncorrect(tableauBordRequest, columnDefinitions);
    }
 
    private void stopIfTableauBordRequestIncorrect(TableauBordRequest tableauBordRequest, List<ColumnDefinition> columnDefinitions) throws CommonApplicationValidationException {
        String tableauBordError = new TableauBordRequestError(tableauBordRequest, columnDefinitions).error();
        if (!tableauBordError.isEmpty()) {
            throw new CommonApplicationValidationException(tableauBordError);
        }
    }
 
    private void stopIfTableauBordExist(Long userId, String codeTypeFichier, String referentielVersion) {
        if (dao.findSpecificTableauBord(userId, codeTypeFichier, referentielVersion) != null) {
            throw new CommonApplicationValidationException("Une configuration existe déjà pour ce typefichier, cet utilisateur et ce référentiel");
        }
    }
 
    private void stopIfTableauBordExist(String codeTypeFichier, String referentielVersion) {
        if (dao.findGlobalTableauBord(codeTypeFichier, referentielVersion) != null) {
            throw new CommonApplicationValidationException("Une configuration existe déjà pour ce typefichier, cet utilisateur et ce référentiel");
        }
    }
 
    private void stopIfTypeFichierNotStructured(String codeTypeFichier, String referentielVersion) {
        if (columnDefinitionManager.findByReferentielAndTypeFichier(referentielVersion, codeTypeFichier).isEmpty()) {
            throw new CommonApplicationValidationException("Le type de fichier ciblé n'est pas structuré");
        }
    }
}