/**
*
* @author ilya portnyagin iportnyagin@gmail.com
*/
package ru.portnyagin.helpdeskru.service;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Root;
import ru.portnyagin.helpdeskru.model.Organization;
import ru.portnyagin.helpdeskru.model.Role;
import ru.portnyagin.helpdeskru.model.RoleEnum;
import ru.portnyagin.helpdeskru.model.Role_;
import ru.portnyagin.helpdeskru.model.UserHD;
import ru.portnyagin.helpdeskru.model.UserHD_;
@Stateless
public class UserHDService extends AbstractService<UserHD> {
@PersistenceContext(unitName = "HelpDeskRuPU")
private EntityManager em;
protected EntityManager getEntityManager() {
return em;
}
public UserHDService() {
super(UserHD.class);
}
public UserHD findByLogin(String name) {
UserHD s = null;
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery();
Root<UserHD> z = cq.from(UserHD.class);
cq.where(cb.equal(z.get(UserHD_.login), name));
cq.select(z);
s = (UserHD)getEntityManager().createQuery(cq).getSingleResult();
return s;
}
@Override
public List<UserHD> findAll() {
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery();
Root<UserHD> z = cq.from(UserHD.class);
cq.where(cb.isNotNull(z.get(UserHD_.login))); // пустой логин - пользователь "выключен"
cq.orderBy(cb.asc(z.get(UserHD_.id)));
cq.select(z);
return getEntityManager().createQuery(cq).getResultList();
}
public List<UserHD> findPerformer() {
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery();
Root<UserHD> z = cq.from(UserHD.class);
Join<UserHD, Role> j = z.join(UserHD_.role);
cq.where(cb.equal(j.get(Role_.name), RoleEnum.SERVICE));
cq.orderBy(cb.asc(z.get(UserHD_.name)));
cq.select(z);
List<UserHD> l = getEntityManager().createQuery(cq).getResultList();
return l;
}
public List<UserHD> findCustomer() {
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery();
Root<UserHD> z = cq.from(UserHD.class);
Join<UserHD, Role> j = z.join(UserHD_.role);
cq.where(cb.equal(j.get(Role_.name), RoleEnum.CUSTOMER));
cq.orderBy(cb.asc(z.get(UserHD_.name)));
cq.select(z);
List<UserHD> l = getEntityManager().createQuery(cq).getResultList();
return l;
}
public List<UserHD> findUsersByOrganization(Organization o) {
if (o == null) {
return null;
}
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery();
Root<UserHD> z = cq.from(UserHD.class);
cq.where(cb.equal(z.get(UserHD_.organization), o));
cq.orderBy(cb.asc(z.get(UserHD_.name)));
cq.select(z);
return getEntityManager().createQuery(cq).getResultList();
}
}