标签:
记录下一个dao层类的写法,方便以后的使用。
1 package com.bupt.auth.dao.impl; 2 3 import java.util.ArrayList; 4 import java.util.HashSet; 5 import java.util.List; 6 import java.util.Set; 7 import java.util.concurrent.CopyOnWriteArraySet; 8 9 import org.springframework.stereotype.Repository; 10 11 import com.bupt.auth.dao.RoleDao; 12 import com.bupt.auth.dao.base.BaseDaoImpl; 13 import com.bupt.auth.entity.Permission; 14 import com.bupt.auth.entity.Role; 15 import com.bupt.auth.exception.MyException; 16 17 @Repository("roleDao") 18 public class RoleDaoImpl extends BaseDaoImpl<Role> implements RoleDao { 19 20 @Override 21 public Role findRoleAdminByUserId(Long id) { 22 // TODO Auto-generated method stub 23 Role role = this.findRoleByRoleNameAndUserId(id, "administrator"); 24 25 return role; 26 } 27 28 @SuppressWarnings("unchecked") 29 @Override 30 public List<Role> findRoleByUserId(Long id) { 31 // TODO Auto-generated method stub\ 32 String sql = "select r from Role r inner join fetch r.ownerUser u inner join fetch r.accessTokens left join fetch r.permissions where u.id = :id"; 33 List<Role> list = getSession().createQuery(sql).setLong("id", id).list(); 34 35 if(list == null || list.size() == 0) 36 return null; 37 38 List<Role> result = new ArrayList<Role>(); 39 for(Role role:list){ 40 if(!result.contains(role)) 41 result.add(role); 42 } 43 44 return result; 45 } 46 47 @SuppressWarnings("unchecked") 48 @Override 49 public Role findRoleByRoleNameAndUserId(Long id, String rolename) { 50 // TODO Auto-generated method stub 51 String sql = "select r from Role r inner join fetch r.ownerUser u inner join fetch r.accessTokens where u.id = :id and r.name = :rolename"; 52 List<Role> list = getSession().createQuery(sql).setLong("id", id).setString("rolename", rolename).list(); 53 54 if(list == null || list.size() == 0) 55 return null; 56 57 return list.get(0); 58 } 59 60 61 @SuppressWarnings("unchecked") 62 @Override 63 public List<Role> findRoleByPermId(Long id) { 64 // TODO Auto-generated method stub 65 String sql = "select r from Role r inner join fetch r.ownerUser inner join fetch r.accessTokens inner join fetch r.permissions p where p.id = :id"; 66 List<Role> list = getSession().createQuery(sql).setLong("id", id).list(); 67 68 if(list == null || list.size() == 0) 69 return null; 70 71 List<Role> result = new ArrayList<Role>(); 72 for(Role role:list){ 73 if(!result.contains(role)) 74 result.add(role); 75 } 76 77 return result; 78 } 79 80 @SuppressWarnings("unchecked") 81 @Override 82 public boolean deleteRolesPermissionByRoleIdAndPermId(Role role, 83 Long permid) throws MyException { 84 // TODO Auto-generated method stub 85 //Role role = this.loadRoleByRoleId(roleid); 86 if(role == null) 87 throw new MyException("Role Not Found", "301"); 88 89 Set<Permission> pset = role.getPermissions(); 90 91 if(pset == null){ 92 throw new MyException("Role has no permissions", ""); 93 } 94 if(pset.size() != 0) 95 { 96 Set<Permission> psetCopy = new HashSet<Permission>(pset); 97 98 for(Permission p : psetCopy) 99 { 100 if(p.getId().longValue() == permid.longValue()){ 101 pset.remove(p); 102 } 103 } 104 } 105 106 107 this.update(role); 108 109 return true; 110 } 111 112 @Override 113 public Set<String> findAccessTokensByRoleId(Long roleid) { 114 // TODO Auto-generated method stub 115 Role role = this.loadRoleByRoleId(roleid); 116 117 if(role == null) 118 return null; 119 120 return role.getAccessTokens(); 121 } 122 123 @Override 124 public boolean deleteAccessTokenByRoleIdAndTokenString(Long roleid, 125 String accessToken) throws MyException { 126 // TODO Auto-generated method stub 127 Role role = this.loadRoleByRoleId(roleid); 128 129 if(role == null) 130 throw new MyException("Role Not Found!", "301"); 131 132 Set<String> accessTokens = role.getAccessTokens(); 133 accessTokens.remove(accessToken); 134 this.update(role); 135 136 return true; 137 } 138 139 @Override 140 public Role loadRoleByRoleId(Long roleid){ 141 String sql = "select r from Role r inner join fetch r.ownerUser inner join fetch r.permissions inner join fetch r.accessTokens where r.id = :id"; 142 List<Role> list = this.getSession().createQuery(sql).setLong("id", roleid).list(); 143 144 if(list == null || list.size() == 0){ 145 String sql2 = "select r from Role r inner join fetch r.ownerUser inner join fetch r.accessTokens where r.id = :id"; 146 List<Role> list2 = this.getSession().createQuery(sql2).setLong("id", roleid).list(); 147 148 if(list2 == null || list2.size() == 0){ 149 return null; 150 } 151 152 return list2.get(0); 153 } 154 155 return list.get(0); 156 } 157 }
标签:
原文地址:http://www.cnblogs.com/godlei/p/5615017.html