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;
|
}
|
|
}
|