⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 groupserviceimpl.java

📁 这是一款最新的野蔷薇论坛源码,有需要的朋友可以尽情下载
💻 JAVA
字号:
/* 
 * Created on 2007-2-23
 * Last modified on 2007-8-22
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.club.service.security;

import java.util.List;

import org.apache.log4j.Logger;

import com.yeqiangwei.club.cache.Cache;
import com.yeqiangwei.club.cache.CacheRegion;
import com.yeqiangwei.club.cache.singleton.CacheFactory;
import com.yeqiangwei.club.cache.CacheKeys;
import com.yeqiangwei.club.dao.DAOLocator;
import com.yeqiangwei.club.dao.DAOWrapper;
import com.yeqiangwei.club.dao.GroupDAO;
import com.yeqiangwei.club.dao.model.Group;
import com.yeqiangwei.club.exception.ClubException;
import com.yeqiangwei.club.param.GroupParameter;
import com.yeqiangwei.club.service.ServiceLocator;
import com.yeqiangwei.club.service.ServiceWrapper;
import com.yeqiangwei.club.service.model.GroupModel;
import com.yeqiangwei.club.service.model.GroupOfForumModel;
import com.yeqiangwei.club.service.model.UserModel;
import com.yeqiangwei.club.util.BeanLocator;
import com.yeqiangwei.club.util.BeanUtils;
import com.yeqiangwei.club.util.MessageUtils;
import com.yeqiangwei.util.Validator;
import com.yeqiangwei.util.TypeChange;

public class GroupServiceImpl implements GroupService {
	
	private static final Logger logger = Logger.getLogger(GroupServiceImpl.class.getName());

	private Cache groupCache = CacheFactory.creator(CacheRegion.GROUP);
	
	public static void main(String args[]){
		com.yeqiangwei.club.dao.hibernate.ConnectionManager.init();
		GroupServiceImpl s = new GroupServiceImpl();
		GroupModel m = s.userInGroup(null,10);
		System.out.println(m.getGroupName());
		//m.getGroupName();
	}
	
	/**
	 * 登录用户所在的用户组
	 */
	public GroupModel userInGroup(UserModel user, int forumId) {
		GroupModel groupModel = null;
		if(!Validator.isEmpty(user)){
			//查看用户所在用户组是否是全局权限
			groupModel = this.findById(user.getGroupId());
			logger.debug("用户所在用户组ID:"+user.getGroupId());
			if(forumId>0){
				//根据用户ID和版面ID取出用户所在版面的用户组ID
				GroupOfForumModel groupOfForumModel = this.getGroupOfForumService().findByForumIdAndUserId(forumId,user.getUserId()); 
				if(!Validator.isEmpty(groupOfForumModel)){
					//重新确定用户所在的用户组 
					//如果设定组高于用户所在版面组则以设定的全局组为准
					//if(groupOfForumModel.getGroupId()<groupModel.getGroupId()){
						groupModel = this.findById(groupOfForumModel.getGroupId());
						logger.debug("用户所在版面用户组ID:"+groupModel.getGroupId());
					//}
				}else{//如果用户没有被指定在单独版面所在的用户组则获取登录用户权限
					//groupModel = this.findByRegisterDefault();
					logger.debug("用户默认注册用户组ID:"+groupModel.getGroupId());
				}
			}
		}else{//过客组
			groupModel = this.findByGuestDefault();
			logger.debug("用户默认过客组组ID:"+groupModel.getGroupId());
		}
		return groupModel;
	}

	public GroupModel findById(int id) {
		if(id>0){
			GroupModel model = (GroupModel) groupCache.get(CacheKeys.getGroupKey(id));
			if(Validator.isEmpty(model)){
				Group item = this.getGroupDAO().findById(id);
				if(!Validator.isEmpty(item)){
					model = new GroupModel();
					BeanUtils.copyProperties(model,item);
					groupCache.put(CacheKeys.getGroupKey(id),model);
				}
			}
			return model;
		}else{
			return null;
		}
	}

	public GroupModel createOrUpdate(GroupModel model) throws ClubException {
		if(model.getGroupId()>0){
			return this.update(model);
		}else{
			return this.create(model);
		}
	}

	public GroupModel create(GroupModel model) throws ClubException {
		if(Validator.isEmpty(model.getGroupName())){
			throw new ClubException(MessageUtils.getMessage("error_empty"));
		}
		Group item = new Group();
		BeanUtils.copyProperties(item,model);
		this.getGroupDAO().create(item);
		model.setGroupId(item.getGroupId());
		groupCache.put(CacheKeys.getKey(model),model);
		if(model.getGuestDefault()){
			groupCache.put(CacheKeys.getGroupKeyByGuestDefault(),model);
			this.getGroupDAO().updateGuestDefaultNoId(model.getGroupId(),false);
		}
		if(model.getRegisterDefault()){
			groupCache.put(CacheKeys.getGroupKeyByRegisterDefault(),model);
			this.getGroupDAO().updateRegisterDefaultNoId(model.getGroupId(),false);
		}
		return model;
	}

	public GroupModel update(GroupModel model) throws ClubException {
		if(Validator.isEmpty(model.getGroupName())){
			throw new ClubException(MessageUtils.getMessage("error_empty"));
		}else{
			Group item = this.getGroupDAO().findById(model.getGroupId());
			if(Validator.isEmpty(item)){
				throw new ClubException(MessageUtils.getMessage("error_system"));
			}else{
				BeanUtils.copyProperties(item,model);
				this.getGroupDAO().update(item);
				model.setGroupId(item.getGroupId());
				groupCache.put(CacheKeys.getKey(model),model);
				if(item.getGuestDefault()){
					groupCache.put(CacheKeys.getGroupKeyByGuestDefault(),model);
					this.getGroupDAO().updateGuestDefaultNoId(item.getGroupId(),false);
				}
				if(item.getRegisterDefault()){
					groupCache.put(CacheKeys.getGroupKeyByRegisterDefault(),model);
					this.getGroupDAO().updateRegisterDefaultNoId(item.getGroupId(),false);
				}
			}
		}
		return model;
	}

	public int delete(GroupModel model) throws ClubException {
		if(model.getGroupId()==0){
			throw new ClubException(MessageUtils.getMessage("error_parameter"));
		}
		model = this.findById(model.getGroupId());
		if(Validator.isEmpty(model)){
			throw new ClubException(MessageUtils.getMessage("error_delete_noid"));
		}else{
			if(model.getGuestDefault()){
				throw new ClubException(MessageUtils.getMessage("error_delete_guestDefault"));
			}
			else if(model.getRegisterDefault()){
				//this.setMessage(super.getMessage("error_delete_registerDefault"));
				throw new ClubException(MessageUtils.getMessage("error_delete_registerDefault"));
			}else{
				Group item = new Group();
				BeanUtils.copyProperties(item,model);
				int c = this.getGroupDAO().delete(item);
				groupCache.remove(CacheKeys.getKey(model));
				if(item.getGuestDefault()){
					groupCache.remove(CacheKeys.getGroupKeyByGuestDefault());
				}
				if(item.getRegisterDefault()){
					groupCache.remove(CacheKeys.getGroupKeyByRegisterDefault());
				}
				return c;
			}
		}
	}

	public int delete(String[] ids) throws ClubException {
		if(Validator.isEmpty(ids)){
			throw new ClubException(MessageUtils.getMessage("error_parameter"));
		}else{
			int c = 0;
			for(int i=0; i<ids.length; i++){
				int id = TypeChange.stringToInt(ids[i]);
				GroupModel model = new GroupModel();
				model.setGroupId(id);
				try {
					c += this.delete(model);
				} catch (ClubException e) {
					logger.error(e.toString());
				}
			}
			return c;
		}
	}

	public List<GroupModel> findByParameter(GroupParameter param) {
		List<Group> list = this.getGroupDAO().findByParameter(param);
		List<GroupModel> mlist = BeanUtils.<Group,GroupModel>copyList(list,BeanLocator.GROUPMODEL);
		return mlist;
	}

	public long countByParameter(GroupParameter param) {
		return this.getGroupDAO().countByParameter(param);
	}

	public GroupDAO getGroupDAO() {
		return DAOWrapper.<GroupDAO>getSingletonInstance(DAOLocator.GROUP);
	}
	

	public GroupModel findByGuestDefault() {
		GroupModel model = (GroupModel) groupCache.get(CacheKeys.getGroupKeyByGuestDefault());
		if(Validator.isEmpty(model)){
			Group item = this.getGroupDAO().findByGuestDefault();
			model = new GroupModel();
			BeanUtils.copyProperties(model,item);
			groupCache.put(CacheKeys.getGroupKeyByGuestDefault(),model);
		}
		return model;
	}

	public GroupModel findByRegisterDefault() {
		GroupModel model = (GroupModel) groupCache.get(CacheKeys.getGroupKeyByRegisterDefault());
		if(Validator.isEmpty(model)){
			Group item = this.getGroupDAO().findByRegisterDefault();
			model = new GroupModel();
			BeanUtils.copyProperties(model,item);
			groupCache.put(CacheKeys.getGroupKeyByRegisterDefault(),model);
		}
		return model;
	}
	
	public GroupOfForumService getGroupOfForumService() {
		return ServiceWrapper.<GroupOfForumService>getSingletonInstance(ServiceLocator.GROUPOFFORUM);
	}

}

⌨️ 快捷键说明

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