项目中Service层的写法

时间:2022-01-17 14:48:19

截取自项目中的一个service实现类,记录一下:

base类

 package com.bupt.auth.service.base;

 import javax.annotation.Resource;

 import com.bupt.auth.dao.OauthClientDao;
import com.bupt.auth.dao.PermissionDao;
import com.bupt.auth.dao.ResourceDao;
import com.bupt.auth.dao.RoleDao;
import com.bupt.auth.dao.TokenDao;
import com.bupt.auth.dao.UserDao; public class BaseManagerImpl
{
@Resource
protected UserDao userDao;
@Resource
protected RoleDao roleDao;
@Resource
protected ResourceDao resourceDao;
@Resource
protected PermissionDao permissionDao;
@Resource
protected TokenDao tokenDao;
@Resource(name="oauthDao")
protected OauthClientDao clientDao; public BaseManagerImpl(){} }

具体业务的实现类:

 package com.bupt.auth.service.impl;

 import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet; import org.springframework.stereotype.Service; import com.bupt.auth.dao.RoleDao;
import com.bupt.auth.entity.Permission;
import com.bupt.auth.entity.Resource;
import com.bupt.auth.entity.Role;
import com.bupt.auth.entity.User;
import com.bupt.auth.exception.MyException;
import com.bupt.auth.service.RoleManager;
import com.bupt.auth.service.base.BaseManagerImpl;
@Service("roleManager")
public class RoleManagerImpl extends BaseManagerImpl implements RoleManager
{ @Override
public Role findRoleAdminByUserId(Long id) {
// TODO Auto-generated method stub
Role role = roleDao.findRoleAdminByUserId(id);
role = roleDao.loadRoleByRoleId(role.getId()); return role;
} @Override
public void update(Role role) {
// TODO Auto-generated method stub
roleDao.update(role);
} @Override
public boolean addRole(Role role) throws MyException {
// TODO Auto-generated method stub
if(roleDao.findRoleByRoleNameAndUserId(role.getOwnerUser().getId(), role.getName()) != null){
throw new MyException("This role has already exist!!", "300");
}
try{
roleDao.save(role);
}catch(Exception e){
return false;
}
return true;
} @Override
public List<Role> findRoleByUserId(Long id) {
// TODO Auto-generated method stub
return roleDao.findRoleByUserId(id);
} @Override
public boolean deleteRole(Long id) {
// TODO Auto-generated method stub
try{
roleDao.delete(id);
}catch(Exception e){
return false;
}
return true;
} @Override
public Role getById(Long id) {
// TODO Auto-generated method stub
return roleDao.loadRoleByRoleId(id);
} @Override
public Role findRoleByRoleNameAndUserId(Long id, String rolename) {
// TODO Auto-generated method stub
return roleDao.findRoleByRoleNameAndUserId(id, rolename);
} @Override
public SortedSet<Long> findRoleByVePeId(Set<String> vpId, int veorpe)
{
Set<User> user = getUserFromVPId(vpId, veorpe);
return getRoleFromUser(user); } private SortedSet<Long> getRoleFromUser(Set<User> user)
{
SortedSet<Long> result = new TreeSet<Long>();
for (User u : user)
{
//该用户的所有角色
List<Role> role = roleDao.findRoleByUserId(u.getId());
//将角色对应的ID放到result中
for (Role r : role)//如果角色没有设置规则,那么将id的负值放到result中
{
if (!r.getName().equals("administrator"))
result.add(r.getConstraintRole().equals("public") ? -r.getId() : r.getId());
}
} return result;
} private Set<User> getUserFromVPId(Set<String> vpId, int veorpe)
{
Set<User> user = new HashSet<User>();
for (String id : vpId)
{
Resource resource = resourceDao.findResourceByResId(id, veorpe);
user.add(resource.getUser());
}
return user;
} @Override
public List<Role> findRoleByPermId(Long id) {
// TODO Auto-generated method stub
return roleDao.findRoleByPermId(id);
} @Override
public boolean deleteRolesPermissionByRoleIdAndPermId(Role role,
Long permid) throws MyException {
// TODO Auto-generated method stub
return roleDao.deleteRolesPermissionByRoleIdAndPermId(role, permid);
} @Override
public boolean updateRole(Role role, Set<Long> old,
Set<Long> addperm) throws MyException {
// TODO Auto-generated method stub
//Role role = this.getById(roleid); if(old != null && old.size() != 0){
for(Long permid : old){
this.deleteRolesPermissionByRoleIdAndPermId(role, permid);
}
} if(addperm != null && addperm.size() != 0){
for(Long permid : addperm){
Permission perm = permissionDao.getById(permid);
Set<Permission> permset = role.getPermissions();
permset.add(perm);
this.update(role);
}
} return true;
} @Override
public Role generateRole(String rolename, String description,
Set<Long> permissions, User user) {
// TODO Auto-generated method stub
try{
Role role = new Role(); role.setName(rolename);
role.setConstraintRole("public");
role.setDescription(description);
role.setOwnerUser(user); Set<String> accessTokens = new HashSet<String>();
accessTokens.add(user.getAccesstoken());
role.setAccessTokens(accessTokens); roleDao.save(role);
Role adminrole = roleDao.findRoleAdminByUserId(user.getId());
Set<Permission> totalPerm = adminrole.getPermissions(); if(totalPerm == null || totalPerm.size() == 0)
return null; Set<Permission> set = new HashSet<Permission>();
for(Long perm_id : permissions){
Permission perm = permissionDao.loadPermissionById(perm_id);
if(perm != null && totalPerm.contains(perm)){
set.add(perm);
}
} role.setPermissions(set);
roleDao.update(role);
return role;
}catch(Exception e){
return null;
}
} @Override
public boolean updateRole(Long id, Set<Long> permissions) throws MyException {
// TODO Auto-generated method stub
Set<Long> old = new HashSet<Long>();
Role role = roleDao.loadRoleByRoleId(id); if(role == null){
throw new MyException("Role Not Found", "301");
} Set<Permission> pset = role.getPermissions();
if(pset != null && pset.size() != 0){
for(Permission perm:pset){
old.add(perm.getId());
}
updateRole(role, old, null);
} Role adminrole = roleDao.findRoleAdminByUserId(role.getOwnerUser().getId());
Set<Permission> totalPerm = adminrole.getPermissions();
if(totalPerm == null || totalPerm.size() == 0){
return false;
} for(Long perm_id:permissions){
if(permissionDao.getById(perm_id) == null){
permissions.remove(perm_id);
}
} if(permissions != null && permissions.size() != 0)
updateRole(role, null, permissions); return true;
}
}