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
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.megatim.apifdxweb.core.impl.tmp;
 
import com.megatim.apifdxweb.core.ifaces.helper.RoutingChecker;
import com.megatim.apifdxweb.core.ifaces.tmp.TokenTmpManager;
import com.megatim.apifdxweb.dao.ifaces.tmp.TokenTmpDAO;
import com.megatim.apifdxweb.model.tmp.StatutTmp;
import com.megatim.apifdxweb.model.tmp.TokenTmp;
import com.megatim.fdxcommons.core.ifaces.helper.DataInMemoryHandler;
import com.mgt.rs.security.core.service.AuthenticationTokenService;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;
import javax.ejb.EJB;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.transaction.Transactional;
 
/**
 *
 * @author ASUS
 */
@Dependent
public class TokenTmpManagerImpl implements TokenTmpManager {
 
    @EJB
    private TokenTmpDAO dao;
 
    @Inject
    private AuthenticationTokenService authenticationTokenService;
 
    @Inject
    private DataInMemoryHandler dataInMemoryHandler;
 
    @Inject
    private RoutingChecker routingChecker;
 
    @Override
    public TokenTmpDAO getDao() {
        return this.dao;
    }
 
    @Override
    @Transactional
    public TokenTmp saveEntity(TokenTmp entity, String connectedParticipant) {
        
        stopIfProductionNotPermitted(entity.getCodeTypeFichier(), connectedParticipant);
        
        TokenTmp foundToken = dao.getByParticipantAndTypeFichier(connectedParticipant, entity.getCodeTypeFichier());
        if (foundToken != null) {
            foundToken.setStatutTmp(StatutTmp.EXPIRE);
            save(foundToken);
        }
        
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String str = LocalDateTime.now().format(formatter);
        UUID uuid = UUID.nameUUIDFromBytes((str + entity.toString()).getBytes());
 
        entity.setToken(uuid.toString());
        entity.setCodeParticipant(connectedParticipant);
        entity.setDateExpiration(entity.getDateCreation().plusMinutes(entity.getValidite()));
 
        return save(entity);
 
    }
 
    @Override
    public List<TokenTmp> getByStatutTmp(StatutTmp statut) {
        return dao.getByStatutTmp(statut);
    }
 
    @Override
    public List<TokenTmp> getExpiredToken() {
        return dao.getExpiredToken();
    }
 
    private void stopIfProductionNotPermitted(String codeTypeFichier, String connectedParticipant) {
        
        routingChecker.stopIfCurrentReferentielNotExists(dataInMemoryHandler.getDataInMemory());
        routingChecker.stopIfTypeFichierNotExists(codeTypeFichier, dataInMemoryHandler.getDataInMemory());
        routingChecker.stopIfNotProducer(codeTypeFichier, dataInMemoryHandler.getDataInMemory(), connectedParticipant);
    
    }
 
}