1 package org.itracker.persistence.dao; 2 3 import org.hibernate.HibernateException; 4 import org.hibernate.Query; 5 import org.itracker.model.Permission; 6 import org.itracker.model.PermissionType; 7 import org.itracker.model.Project; 8 import org.itracker.model.User; 9 10 import java.util.List; 11 12 public class PermissionDAOImpl extends BaseHibernateDAOImpl<Permission> 13 implements PermissionDAO { 14 15 @SuppressWarnings("unchecked") 16 public List<Permission> findByUserId(Integer userId) { 17 List<Permission> permissions; 18 19 if (getSession().get(User.class, userId) == null) { 20 throw new NoSuchEntityException("User " + userId + " not found."); 21 } 22 23 try { 24 Query query = getSession().getNamedQuery( 25 "PermissionsByUserQuery"); 26 query.setInteger("userId", userId); 27 permissions = query.list(); 28 } catch (HibernateException ex) { 29 throw convertHibernateAccessException(ex); 30 } 31 return permissions; 32 } 33 34 35 @Deprecated 36 @SuppressWarnings("unchecked") 37 public List<Permission> findByProjectIdAndPermission(Integer projectId, 38 int permissionType) { 39 return findByProjectIdAndPermission(projectId, PermissionType.valueOf(permissionType)); 40 } 41 42 @Override 43 public List<Permission> findByProjectIdAndPermission(Integer projectId, PermissionType permissionType) { 44 45 List<Permission> permissions; 46 47 if (getSession().get(Project.class, projectId) == null) { 48 throw new NoSuchEntityException("Project " + projectId + " not found."); 49 } 50 51 try { 52 Query query = getSession().getNamedQuery( 53 "PermissionsByProjectAndTypeQuery"); 54 query.setInteger("projectId", projectId); 55 query.setParameter("permissionType", permissionType); 56 permissions = query.list(); 57 } catch (HibernateException ex) { 58 throw convertHibernateAccessException(ex); 59 } 60 return permissions; 61 } 62 63 }