groupofforumserviceimpl.java

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

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

import java.util.List;

import org.apache.log4j.Logger;

import com.yeqiangwei.cache.Cache;
import com.yeqiangwei.cache.CacheFactory;
import com.yeqiangwei.club.cache.CacheKeys;
import com.yeqiangwei.club.cache.CacheRegion;
import com.yeqiangwei.club.dao.DAOLocator;
import com.yeqiangwei.club.dao.DAOWrapper;
import com.yeqiangwei.club.dao.GroupOfForumDAO;
import com.yeqiangwei.club.model.GroupOfForum;
import com.yeqiangwei.club.exception.ClubException;
import com.yeqiangwei.club.param.GroupOfForumParameter;
import com.yeqiangwei.club.service.ServiceLocator;
import com.yeqiangwei.club.service.ServiceWrapper;
import com.yeqiangwei.club.model.User;
import com.yeqiangwei.club.service.user.UserService;
import com.yeqiangwei.club.util.BeanUtils;
import com.yeqiangwei.club.util.MessageUtils;
import com.yeqiangwei.util.Validator;
import com.yeqiangwei.util.TypeChange;

public class GroupOfForumServiceImpl implements GroupOfForumService{
	
	private static final Logger logger = Logger.getLogger(GroupOfForumServiceImpl.class.getName());
	
	private Cache<GroupOfForum> CACHE_GROUPOFFORUM = CacheFactory.<GroupOfForum>creator(CacheRegion.GROUPOFFORUM); 
	
	private Cache<List<GroupOfForum>> CACHE_LIST = CacheFactory.creator(CacheRegion.LIST); 
	
	private Cache<Boolean> CACHE_BOOLEAN = CacheFactory.<Boolean>creator(CacheRegion.BOOLEAN); 

	public static void main(String args[]){
		com.yeqiangwei.club.dao.hibernate.ConnectionManager.init();
		GroupOfForumServiceImpl s = new GroupOfForumServiceImpl();
		GroupOfForumParameter param = new GroupOfForumParameter();
		List<GroupOfForum> list = s.findByParameter(param);
		for(int i=0; i<list.size(); i++){
			GroupOfForum model = list.get(i);
			System.out.println(model.getUserName());
		}
	}
	
	/**
	 * return GroupOfForum BO
	 */
	public GroupOfForum findById(int id) {
		if(id>0){
			return this.getGroupOfForumDAO().findById(id);
		}else{
			logger.error("GroupOfForum groupOfForumId==0");
			return null;
		}
	}

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

	private GroupOfForum createOrUpdateUtils(GroupOfForum model, boolean iscreate) throws ClubException {
		if(Validator.isEmpty(model)||Validator.isEmpty(model.getUserName())){
			throw new ClubException(MessageUtils.getMessage("error_empty"));
		}else{
			GroupOfForum item = null;
			User user = this.getUserService().findByUserName(model.getUserName());
			if(!Validator.isEmpty(user)){
				model.setUserId(user.getUserId());
				model.setUserName(user.getUserName());
				if(model.getGroupOfForumId()>0){
					item = this.getGroupOfForumDAO().findById(model.getGroupOfForumId());
				}else{
					item = new GroupOfForum();
				}
				BeanUtils.copyProperties(item, model); //BO->PO
				if(iscreate){
					this.getGroupOfForumDAO().create(item);
					if(item.getGroupOfForumId()==0){
						throw new ClubException(MessageUtils.getMessage("error_system"));
					}
				}else{
					this.getGroupOfForumDAO().update(item);
				}
				BeanUtils.copyProperties(model,item); //PO->BO
				CACHE_GROUPOFFORUM.clear();
				CACHE_LIST.clear();
				CACHE_BOOLEAN.clear();
			}else{
				throw new ClubException(MessageUtils.getMessage("error_notfind_user"));
			}
		}
		return model;
	}
	
	public void create(GroupOfForum model) throws ClubException{
		this.createOrUpdateUtils(model,true);
	}
	
	public void update(GroupOfForum model) throws ClubException {
		this.createOrUpdateUtils(model,false);
	}

	public int delete(GroupOfForum model) throws ClubException {
		if(model.getGroupOfForumId()<1){
			throw new ClubException(MessageUtils.getMessage("error_parameter"));
		}else{
			CACHE_GROUPOFFORUM.clear();
			CACHE_LIST.clear();
			CACHE_BOOLEAN.clear();
			return this.getGroupOfForumDAO().delete(model);
		}
	}

	public int delete(String[] ids) throws ClubException {
		int c = 0;
		int e = 0;
		if(Validator.isEmpty(ids)){
			//this.setMessage(super.getMessage("error_delete_noid"));
			throw new ClubException(MessageUtils.getMessage("error_parameter"));
		}else{
			for(int i=0; i<ids.length; i++){
				int id = TypeChange.stringToInt(ids[i]);
				GroupOfForum model =  new GroupOfForum();
				model.setGroupOfForumId(id);
				if(this.delete(model)>0){
					c++;
				}else{
					e++;
				}
			}
		}
		return c;
	}

	/**
	 * 根据组ID和版面ID查找,以确定是否为这个版面设定了单独设定了组权限
	 */
	public List<GroupOfForum> findByGroupIdAndForumId(int groupId, int forumId) {
		StringBuffer sb = new StringBuffer("gid");
		sb.append(groupId);
		sb.append("fid");
		sb.append(forumId);
		Boolean isput = CACHE_BOOLEAN.get(sb.toString()+"-isput");
		List<GroupOfForum> list = CACHE_LIST.get(sb.toString());
		if(Validator.isEmpty(list)&&Validator.isEmpty(isput)){
			GroupOfForumParameter param = new GroupOfForumParameter();
			param.setForumId(forumId);
			param.setGroupId(groupId);
			list = this.findByParameter(param);
			CACHE_LIST.put(sb.toString(),list);
			CACHE_BOOLEAN.put(sb.toString()+"-isput",new Boolean(true));
		}
		return list;
	}

	public List<GroupOfForum> findByParameter(GroupOfForumParameter param) {
		return this.getGroupOfForumDAO().findByParameter(param);
	}

	public long countByParameter(GroupOfForumParameter param) {
		return this.getGroupOfForumDAO().countByParameter(param);
	}
	
	/**
	 * 
	 */
	public List<GroupOfForum> findByForumId(int forumId) {
		Boolean isput = CACHE_BOOLEAN.get(CacheKeys.getGroupOfForumKeyByForumId(forumId)+"-isput");
		List<GroupOfForum> list = CACHE_LIST.get(CacheKeys.getGroupOfForumKeyByForumId(forumId));
		if(Validator.isEmpty(list)&&Validator.isEmpty(isput)){
			GroupOfForumParameter param = new GroupOfForumParameter();
			param.setForumId(forumId);
			list = this.findByParameter(param);
			CACHE_LIST.put(CacheKeys.getGroupOfForumKeyByForumId(forumId),list);
			CACHE_BOOLEAN.put(CacheKeys.getGroupOfForumKeyByForumId(forumId)+"-isput", new Boolean(true));
		}
		return list;
	}
	
	/**
	 * 
	 */
	public GroupOfForum findByForumIdAndUserId(int forumId, int userId){
		List<GroupOfForum>list = this.findByForumId(forumId);
		if(!Validator.isEmpty(list)){
			for(int i=0; i<list.size(); i++){
				GroupOfForum model = list.get(i);
				if(!Validator.isEmpty(model)){
					if(model.getUserId()==userId){
						return model;
					}
				}
			}
		}
		return null;
	}

	public GroupOfForumDAO getGroupOfForumDAO() {
		return DAOWrapper.<GroupOfForumDAO>getSingletonInstance(DAOLocator.GROUPOFFORUM);
	}

	public UserService getUserService() {
		return ServiceWrapper.<UserService>getSingletonInstance(ServiceLocator.USER);
	}

}

⌨️ 快捷键说明

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