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
package com.megatim.fdxconsultation.dao.impl.supervision;
 
import com.megatim.fdxconsultation.dao.ifaces.supervision.StandaloneServerStateEntityDAO;
import com.megatim.fdxconsultation.model.supervision.StandaloneServerStateEntity;
import com.megatim.fdxconsultation.model.supervision.StatutConfiguration;
import java.util.Optional;
import javax.enterprise.context.Dependent;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
 
/**
 *
 * @author ASUS
 */
@Dependent
public class StandaloneServerStateEntityDAOImpl implements StandaloneServerStateEntityDAO {
 
    @PersistenceContext(unitName = "fdxConsultationPersistenceUnit")
    protected EntityManager em;
 
    @Override
    public Class<StandaloneServerStateEntity> getManagedEntityClass() {
        return StandaloneServerStateEntity.class;
    }
 
    @Override
    public EntityManager getEntityManager() {
        return em;
    }
 
    @Override
    public StandaloneServerStateEntity getById(Long id) {
        Query query = em.createQuery("SELECT s from StandaloneServerStateEntity s LEFT JOIN FETCH s.agentConfigurations where s.id = :id");
        query.setParameter("id", id);
        Optional<StandaloneServerStateEntity> optionalServer = query.getResultList().stream().findFirst();
 
        return optionalServer.isPresent() ? optionalServer.get() : null;
    }
 
    @Override
    public void archiveServerState() {
        Query query = em.createQuery("UPDATE StandaloneServerStateEntity s SET s.statutConfiguration = :newStatut WHERE s.statutConfiguration = :oldStatut");
        query.setParameter("newStatut", StatutConfiguration.ARCHIVE);
        query.setParameter("oldStatut", StatutConfiguration.EN_COURS);
        query.executeUpdate();
    }
 
    @Override
    public StandaloneServerStateEntity getCurrentStandaloneServerStateEntity() {
        Query query = em.createQuery("SELECT s FROM StandaloneServerStateEntity s LEFT JOIN FETCH s.agentConfigurations WHERE s.statutConfiguration = :statutConfiguration");
        query.setParameter("statutConfiguration", StatutConfiguration.EN_COURS);
        Optional<StandaloneServerStateEntity> optionalServer = query.getResultList().stream().findFirst();
        return optionalServer.isPresent() ? optionalServer.get() : null;
    }
 
}