package com.megatim.fdxconsultation.daoimpl.documents;
|
|
import com.bekosoftware.genericdaolayer.dao.impl.AbstractGenericDAO;
|
import com.megatim.fdxcommons.model.enumeration.TypeOperation;
|
import com.megatim.fdxconsultation.dao.ifaces.documents.CategoryDAO;
|
import com.megatim.fdxconsultation.dao.ifaces.documents.DocumentDAO;
|
import com.megatim.fdxconsultation.model.documents.Category;
|
import java.util.List;
|
import java.util.stream.Collectors;
|
import javax.enterprise.context.Dependent;
|
import javax.inject.Inject;
|
import javax.persistence.EntityManager;
|
import javax.persistence.PersistenceContext;
|
import javax.persistence.Query;
|
import javax.transaction.Transactional;
|
|
/**
|
*
|
* @author Gabuntu
|
*/
|
@Dependent
|
public class CategoryDAOImpl extends AbstractGenericDAO<Category, Long> implements CategoryDAO {
|
|
@PersistenceContext(unitName = "fdxConsultationPersistenceUnit")
|
protected EntityManager em;
|
|
@Inject
|
private DocumentDAO documentDAO;
|
|
@Override
|
public EntityManager getEntityManager() {
|
return em;
|
}
|
|
@Override
|
public Class<Category> getManagedEntityClass() {
|
return Category.class;
|
}
|
|
@Override
|
public Category getById(Long id) {
|
Query query = getEntityManager().createQuery("SELECT c FROM Category c WHERE c.id = :id AND c.typeOperation = :typeOperation");
|
query.setParameter("id", id).setParameter("typeOperation", TypeOperation.AJOUTER);
|
List<Category> liste = query.getResultList();
|
return liste != null ? liste.stream().findFirst().get() : null;
|
}
|
|
@Override
|
@Transactional
|
public List<Category> loadAll() {
|
|
/*Query query = getEntityManager().createNativeQuery("SELECT c.*, doc.* FROM category c "
|
+ "LEFT JOIN fdx_document doc ON c.id = doc.category_id AND doc.type_operation = :typeOperation "
|
+ "WHERE c.type_operation = :typeOperation");*/
|
|
Query query = getEntityManager().createQuery("SELECT DISTINCT c FROM Category c "
|
+ "WHERE c.typeOperation = :typeOperation");
|
|
query.setParameter("typeOperation", TypeOperation.AJOUTER);
|
|
List<Category> categories = query.getResultList();
|
categories.forEach(c -> c.setDocuments(documentDAO.getByCategory(c.getId()).stream().collect(Collectors.toSet())));
|
|
return categories;
|
}
|
|
}
|