Kenmegne
7 days ago 1bc8864f134272c4bf23a9b05831803a423a4771
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
package com.megatim.apifdxweb.core.impl.helper;
 
import com.megatim.apifdxweb.core.ifaces.helper.DataProductionOperations;
import com.megatim.apifdxweb.core.ifaces.helper.RoutingChecker;
import com.megatim.apifdxweb.model.request.UpdateRequest;
import com.megatim.apifdxweb.tools.AppContext;
import com.megatim.commons.tools.exceptions.ApplicationServerException;
import com.megatim.fdxcommons.core.ifaces.helper.DataInMemoryHandler;
import com.megatim.fdxcommons.model.dataproduction.DataProduction;
import com.megatim.fdxcommons.model.dataproduction.DataProductionSource;
import com.megatim.fdxcommons.model.pojo.CriteriaEntityFromView;
import com.megatim.fdxcommons.tools.database.connection.DBConnection;
import com.megatim.fdxcommons.tools.database.queries.metadata.QueryCriterion;
import com.megatim.fdxcommons.tools.database.tables.FdxApiTable;
import com.megatim.fdxcommons.tools.exceptions.CommonRessourceNotFoundException;
import com.megatim.fdxcommons.tools.utils.CriteriaEntityFromViewToQueryCriterion;
import java.sql.Connection;
import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.List;
import javax.annotation.Resource;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.sql.DataSource;
 
/**
 *
 * @author Gabuntu
 */
@Dependent
public class DataProductionOperationsImpl implements DataProductionOperations {
 
    @Resource(lookup = AppContext.JNDI_NAME)
    private DataSource dataSource;
 
    @Inject
    private DataInMemoryHandler dataInMemoryHandler;
 
    @Inject
    private RoutingChecker routingChecker;
 
    @Override
    public void saveAll(String codeTypeFichier, List<LinkedHashMap<String, Object>> datas, String connectedParticipant) {
 
        stopIfProductionNotPermitted(codeTypeFichier, connectedParticipant);
 
        try ( Connection connection = new DBConnection(dataSource).connection()) {
            connection.setAutoCommit(false);
 
            DataProduction dataProduction = newDataProduction(codeTypeFichier, connectedParticipant);
            table(connection, codeTypeFichier).insert((List<LinkedHashMap<String, Object>>) datas, dataProduction);
 
            connection.commit();
        } catch (Exception ex) {
            throw new ApplicationServerException(ex.getMessage());
        }
    }
 
    @Override
    public void saveOne(String codeTypeFichier, LinkedHashMap<String, Object> data, String connectedParticipant) {
 
        stopIfProductionNotPermitted(codeTypeFichier, connectedParticipant);
 
        try ( Connection connection = new DBConnection(dataSource).connection()) {
            connection.setAutoCommit(false);
 
            DataProduction dataProduction = newDataProduction(codeTypeFichier, connectedParticipant);
            table(connection, codeTypeFichier).insert(data, dataProduction);
 
            connection.commit();
        } catch (Exception ex) {
            throw new ApplicationServerException(ex.getMessage());
        }
    }
 
    @Override
    public void updateAll(String codeTypeFichier, UpdateRequest updateRequest, String connectedParticipant) {
 
        stopIfProductionNotPermitted(codeTypeFichier, connectedParticipant);
 
        try ( Connection connection = new DBConnection(dataSource).connection()) {
            connection.setAutoCommit(false);
 
            QueryCriterion queryCriterion = new CriteriaEntityFromViewToQueryCriterion(updateRequest.getCriteriaEntityFromView()).queryCriterion();
            table(connection, codeTypeFichier).update(updateRequest.getDataToUpdate(), queryCriterion);
 
            connection.commit();
        } catch (Exception ex) {
            throw new ApplicationServerException(ex.getMessage());
        }
    }
 
    @Override
    public void updateOne(String codeTypeFichier, UpdateRequest updateRequest, String connectedParticipant) {
 
        stopIfProductionNotPermitted(codeTypeFichier, connectedParticipant);
 
        try ( Connection connection = new DBConnection(dataSource).connection()) {
            connection.setAutoCommit(false);
 
            QueryCriterion queryCriterion = new CriteriaEntityFromViewToQueryCriterion(updateRequest.getCriteriaEntityFromView()).queryCriterion();
            table(connection, codeTypeFichier).updateOne(updateRequest.getDataToUpdate(), queryCriterion);
 
            connection.commit();
        } catch (Exception ex) {
            throw new ApplicationServerException(ex.getMessage());
        }
    }
 
    @Override
    public void deleteAll(String codeTypeFichier, CriteriaEntityFromView criterionFromView, String connectedParticipant) {
 
        stopIfProductionNotPermitted(codeTypeFichier, connectedParticipant);
 
        try ( Connection connection = new DBConnection(dataSource).connection()) {
            connection.setAutoCommit(false);
 
            QueryCriterion queryCriterion = new CriteriaEntityFromViewToQueryCriterion(criterionFromView).queryCriterion();
            table(connection, codeTypeFichier).delete(queryCriterion);
 
            connection.commit();
        } catch (Exception ex) {
            throw new ApplicationServerException(ex.getMessage());
        }
    }
 
    @Override
    public void deleteOne(String codeTypeFichier, CriteriaEntityFromView criterionFromView, String connectedParticipant) {
 
        stopIfProductionNotPermitted(codeTypeFichier, connectedParticipant);
 
        try ( Connection connection = new DBConnection(dataSource).connection()) {
            connection.setAutoCommit(false);
 
            QueryCriterion queryCriterion = new CriteriaEntityFromViewToQueryCriterion(criterionFromView).queryCriterion();
            table(connection, codeTypeFichier).deleteOne(queryCriterion);
 
            connection.commit();
        } catch (Exception ex) {
            throw new ApplicationServerException(ex.getMessage());
        }
    }
 
    private FdxApiTable table(Connection connection, String codeTypeFichier) {
        return new FdxApiTable(codeTypeFichier, versionReferentiel(), connection);
    }
 
    private DataProduction newDataProduction(String codeTypeFichier, String codeParticipant) {
        DataProduction dataProduction = new DataProduction();
        dataProduction.setDateProduction(LocalDateTime.now());
        dataProduction.setReferentielVersion(versionReferentiel());
        dataProduction.setCodeTypeFichier(codeTypeFichier);
        dataProduction.setCodeParticipant(codeParticipant);
        dataProduction.setSource(DataProductionSource.API);
 
        return dataProduction;
    }
 
    private String versionReferentiel() {
        return dataInMemoryHandler.getDataInMemory().getReferentielEnCours().getVersion();
    }
 
    private void stopIfProductionNotPermitted(String codeTypeFichier, String connectedParticipant) {
        if (dataInMemoryHandler.getDataInMemory().getReferentielEnCours() == null) {
            throw new CommonRessourceNotFoundException("Aucun référentiel en cours");
        }
        routingChecker.stopIfTypeFichierNotExists(codeTypeFichier, dataInMemoryHandler.getDataInMemory());
        routingChecker.stopIfNotProducer(codeTypeFichier, dataInMemoryHandler.getDataInMemory(), connectedParticipant);
    }
 
}