/*
|
* 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.fdxconsultation.dao.impl.stats;
|
|
import com.megatim.fdxcommons.model.pojo.CriteriaEntity;
|
import com.megatim.fdxcommons.tools.database.tables.dto.JpqlQueryElement;
|
import com.megatim.fdxcommons.model.pojo.OrderByDefinition;
|
import com.megatim.fdxconsultation.dao.ifaces.stats.CriteriaEntityPersistedDAO;
|
import com.megatim.fdxconsultation.model.stats.CriteriaEntityPersisted;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Optional;
|
import javax.ejb.Stateless;
|
import javax.persistence.EntityManager;
|
import javax.persistence.PersistenceContext;
|
import javax.persistence.Query;
|
|
/**
|
*
|
* @author ASUS
|
*/
|
@Stateless
|
public class CriteriaEntityPersistedDAOImpl implements CriteriaEntityPersistedDAO {
|
|
@PersistenceContext(unitName = "fdxConsultationPersistenceUnit")
|
protected EntityManager em;
|
|
@Override
|
public Class<CriteriaEntityPersisted> getManagedEntityClass() {
|
return CriteriaEntityPersisted.class;
|
}
|
|
@Override
|
public EntityManager getEntityManager() {
|
return em;
|
}
|
|
@Override
|
public CriteriaEntityPersisted getById(Long id) {
|
Query query = em.createQuery("SELECT c from CriteriaEntityPersisted "
|
+ "where c.id = :id ");
|
query.setParameter("id", id);
|
|
Optional<CriteriaEntityPersisted> optionalCriteria = query.getResultList().stream().findFirst();
|
CriteriaEntityPersisted criterion = optionalCriteria.isPresent() ? optionalCriteria.get() : null;
|
|
if (criterion == null) {
|
return null;
|
}
|
|
return loadSubCriteria(criterion);
|
}
|
|
@Override
|
public CriteriaEntityPersisted loadSubCriteria(CriteriaEntityPersisted criterion) {
|
List<CriteriaEntityPersisted> children = getChildren(criterion.getId());
|
|
List<CriteriaEntityPersisted> subCriteria = new ArrayList<>();
|
|
for (CriteriaEntityPersisted c : children) {
|
subCriteria.add(loadSubCriteria(c));
|
}
|
criterion.setSubCriterias(subCriteria);
|
|
return criterion;
|
}
|
|
private List<CriteriaEntityPersisted> getChildren(Long id) {
|
Query query = em.createQuery("SELECT c from CriteriaEntityPersisted c where c.parent.id = :id ");
|
query.setParameter("id", id);
|
|
return (List<CriteriaEntityPersisted>) query.getResultList();
|
}
|
|
@Override
|
public List<CriteriaEntityPersisted> subCriterias(Long id) {
|
Query query = em.createQuery("SELECT c FROM CriteriaEntityPersisted c WHERE c.parent.id = :id ");
|
query.setParameter("id", id);
|
return query.getResultList();
|
}
|
|
}
|