roleserviceimpl.java

来自「社区文章采用的是平板、树形自由选择的两种展示方式」· Java 代码 · 共 190 行

JAVA
190
字号
/* 
 * Created on 2007-2-22
 * Last modified on 2008-1-4
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.club.service.security;

import java.util.ArrayList;
import java.util.List;

import com.yeqiangwei.cache.Cache;
import com.yeqiangwei.club.cache.CacheKeys;
import com.yeqiangwei.club.cache.CacheRegion;
import com.yeqiangwei.cache.singleton.CacheFactory;
import com.yeqiangwei.club.dao.DAOLocator;
import com.yeqiangwei.club.dao.DAOWrapper;
import com.yeqiangwei.club.dao.RoleDAO;

import org.apache.log4j.Logger;

import com.yeqiangwei.club.model.Role;
import com.yeqiangwei.club.exception.ClubException;
import com.yeqiangwei.club.param.RoleParameter;
import com.yeqiangwei.club.service.security.RoleService;
import com.yeqiangwei.club.util.BeanUtils;
import com.yeqiangwei.club.util.MessageUtils;
import com.yeqiangwei.util.Validator;
import com.yeqiangwei.util.StringHelper;
import com.yeqiangwei.util.TypeChange;

public class RoleServiceImpl implements RoleService {
	
	private static final Logger logger = Logger.getLogger(RoleServiceImpl.class);
	
	private static final Cache<Role> CACHE_ROLE = CacheFactory.creator(CacheRegion.ROLE);
	
	private static final Cache<String> CACHE_POWER_LOCATOR = CacheFactory.<String>creator(CacheRegion.ROLE_POWER_LOCATOR);
	
	public static void main(String args[]){
		com.yeqiangwei.club.dao.hibernate.ConnectionManager.init();
		Role m = new Role();
		m.setRoleId(12);
		m.setRoleName("444");
		//RoleServiceImpl rs = new RoleServiceImpl();
	}

	public Role findById(int id) {
		if(id>0){
			Role model = CACHE_ROLE.get(CacheKeys.getRoleKey(id));
			if(Validator.isEmpty(model)){
				model = this.getRoleDAO().findById(id);
				CACHE_ROLE.put(CacheKeys.getRoleKey(id), model);
			}
			return model;
		}
		return null;
	}

	@Override
	public void createOrUpdate(Role model, String[] forumIds)
			throws ClubException {
		if(Validator.isEmpty(forumIds)){
			throw new ClubException(MessageUtils.getMessage("error_empty"));
		}
		List<Integer> list = new ArrayList<Integer>();
		for(int i=0; i<forumIds.length; i++){
			int forumId = TypeChange.stringToInt(forumIds[i]);
			list.add(forumId);
			Role role = this.getRoleDAO().findByForumId(forumId);
			if(!Validator.isEmpty(role)){
				role.setAdminPower(model.getAdminPower());
				role.setBrowsePower(model.getBrowsePower());
				role.setForumId(forumId);
				role.setOperatePower(model.getOperatePower());
				role.setRoleName(model.getRoleName());
				this.update(role);
			}else{
				model.setRoleId(0);
				model.setForumId(forumId);
				role = new Role();
				BeanUtils.copyProperties(role, model);
				this.create(role);
			}
		}
	}

	public void createOrUpdate(Role model) throws ClubException {
		if(model.getRoleId()>0){
			this.update(model);
		}else{
			this.create(model);
		}
	}

	public void create(Role model) throws ClubException {
		if(Validator.isEmpty(model)||Validator.isEmpty(model.getRoleName())){
			throw new ClubException(MessageUtils.getMessage("error_empty"));
		}else{
			this.getRoleDAO().create(model);
			CACHE_POWER_LOCATOR.clear();
			CACHE_ROLE.put(CacheKeys.getRoleKey(model.getRoleId()), model);
			UserPower.CACHE_PERMISSION.clear();
			UserPower.CACHE_POWER.clear();
		}
	}

	public void update(Role model) throws ClubException {
		if(Validator.isEmpty(model)||Validator.isEmpty(model.getRoleName())){
			throw new ClubException(MessageUtils.getMessage("error_empty"));
		}else{
			Role role = this.getRoleDAO().findById(model.getRoleId());
			BeanUtils.copyProperties(role,model);
			this.getRoleDAO().update(role);
			CACHE_ROLE.put(CacheKeys.getRoleKey(role.getRoleId()), role);
			CACHE_POWER_LOCATOR.clear();
			UserPower.CACHE_PERMISSION.clear();
			UserPower.CACHE_POWER.clear();
		}
	}

	public int delete(Role model) throws ClubException {
		if(Validator.isEmpty(model)||model.getRoleId()==0){
			throw new ClubException(MessageUtils.getMessage("error_delete_noid"));
		}else{
			CACHE_ROLE.remove(CacheKeys.getRoleKey(model.getRoleId()));
			CACHE_POWER_LOCATOR.clear();
			UserPower.CACHE_PERMISSION.clear();
			UserPower.CACHE_POWER.clear();
			return this.getRoleDAO().delete(model);
		}
	}

	public int delete(String[] ids) {
		int c = 0;
		if(!Validator.isEmpty(ids)){
			for(int i=0; i<ids.length; i++){
				int id = TypeChange.stringToInt(ids[i]);
				Role model = new Role();
				model.setRoleId(id);
				try {
					c+=this.delete(model);
				} catch (ClubException e) {
					logger.error(e.toString());
				}
			}
		}
		return c;
	}

	public List<Role> findByParameter(RoleParameter param){
		return this.getRoleDAO().findByParameter(param);
	}

	public long countByParameter(RoleParameter param) {
		return 0;
	}

	private RoleDAO getRoleDAO() {
		return DAOWrapper.<RoleDAO>getSingletonInstance(DAOLocator.ROLE);
	}

	public String getBrowsePower(String str, int locator) {
		String s = CACHE_POWER_LOCATOR.get("BrowsePower="+str+",locator="+locator);
		if(Validator.isEmpty(s)){
			s = StringHelper.locator(str,locator,"|","0");
			CACHE_POWER_LOCATOR.put("BrowsePower="+str+",locator="+locator,s);
		}
		return s;
	}

	public String getOperatePower(String str, int locator) {
		String s = CACHE_POWER_LOCATOR.get("OperatePower="+str+",locator="+locator);
		if(Validator.isEmpty(s)){
			s = StringHelper.locator(str,locator,"|","0");
			CACHE_POWER_LOCATOR.put("BrowsePower="+str+",locator="+locator,s);
		}
		return s;
	}

	public String getAdminPower(String str, int locator) {
		String s = CACHE_POWER_LOCATOR.get("AdminPower="+str+",locator="+locator);
		if(Validator.isEmpty(s)){
			s = StringHelper.locator(str,locator,"|","0");
			CACHE_POWER_LOCATOR.put("BrowsePower="+str+",locator="+locator,s);
		}
		return s;
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?