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

📄 ruleserviceimpl.java

📁 野蔷薇论坛源码 java 自己看看吧。 学习用
💻 JAVA
字号:
/* 
 * Created on 2007-1-30
 * Last modified on 2007-11-3
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.club.service.user;

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.RuleDAO;
import com.yeqiangwei.club.dao.model.Rule;
import com.yeqiangwei.club.exception.ClubException;
import com.yeqiangwei.club.exception.DAOException;
import com.yeqiangwei.club.param.RuleParameter;
import com.yeqiangwei.club.service.model.RuleModel;
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.StringHelper;
import com.yeqiangwei.util.TypeChange;

public class RuleServiceImpl implements RuleService{
	
	private static final Logger logger = Logger.getLogger(RuleServiceImpl.class);

	private Cache cache = CacheFactory.creator(CacheRegion.RULE);

	public RuleModel findOnly() {
		return this.findOnly(new RuleParameter());
	}
	
	public RuleModel findById(int id) {
		if(id>0){
			Rule item = this.getRuleDAO().findById(id);
			if(Validator.isEmpty(item)){
				logger.error("RuleModel("+id+") is null!");
			}else{
				RuleModel model = new RuleModel();
				BeanUtils.copyProperties(model,item);
				cache.put(CacheKeys.getRuleKey(model.getForumId()),model);
				return model;
			}
		}else{
			logger.error("RuleModel("+id+") is null!");
		}
		return null;
	}

	public void createOrUpdate(RuleModel model) throws ClubException {
		if(model.getRuleId()>0){
			this.update(model);
		}else{
			this.create(model);
		}
	}
	
	public void create(RuleModel model) throws ClubException {
		Rule item = new Rule();
		BeanUtils.copyProperties(item,model);
		try {
			this.getRuleDAO().create(item);
		} catch (DAOException e) {
			logger.error(e.toString());
			throw new ClubException(MessageUtils.getMessage("error_system"));
		}
		model.setRuleId(item.getRuleId()); //PO->BO
		cache.put(CacheKeys.getRuleKey(model.getForumId()),model);
	}

	public void update(RuleModel model) throws ClubException {
		RuleParameter param = new RuleParameter();
		param.setForumId(model.getForumId());
		Rule item = this.getRuleDAO().findOnly(param);
		if(Validator.isEmpty(item)){
			throw new ClubException(MessageUtils.getMessage("error_update_noid"));
		}
		model.setRuleId(item.getRuleId());
		BeanUtils.copyProperties(item,model);
		try {
			this.getRuleDAO().update(item);
		} catch (DAOException e) {
			logger.error(e.toString());
			throw new ClubException(MessageUtils.getMessage("error_system"));
		}
		cache.put(CacheKeys.getRuleKey(model.getForumId()),model);
	}

	public int delete(RuleModel model) throws ClubException {
		if(model.getRuleId()>0){
			Rule item = new Rule();
			BeanUtils.copyProperties(item,model);
			int c = 0;
			try {
				c = this.getRuleDAO().delete(item);
			} catch (DAOException e) {
				logger.error(e.toString());
				throw new ClubException(MessageUtils.getMessage("error_system"));
			}
			cache.remove(CacheKeys.getRuleKey(item.getForumId()));
			return c;
		}else{
			throw new ClubException(MessageUtils.getMessage("error_parameter"));
		}
	}

	public int delete(String[] ids) {
		return 0;
	}

	public List<RuleModel> findByParameter(RuleParameter param) {
		List<Rule> list = this.getRuleDAO().findByParameter(param);
		List<RuleModel> mlist = BeanUtils.<Rule,RuleModel>copyList(list,BeanLocator.RULEMODEL);
		return mlist;
	}

	public long countByParameter(RuleParameter param) {
		return this.getRuleDAO().countByParameter(param);
	}
	
	/**
	 * 如果有对应版面的制度则取版面的,如果没有则取全局制度 forum is null
	 * 此方法不允许返回NULL
	 */
	public RuleModel findOnly(RuleParameter param) {
		Integer forumId = param.getForumId();
		RuleModel model = (RuleModel) cache.get(CacheKeys.getRuleKey(forumId));
		/*Boolean isput = (Boolean) cache.get(CacheKeys.getRuleKey(forumId)+"-isput");*/
		if(Validator.isEmpty(model)/*&&Validator.isEmpty(isput)*/){
			Rule item = this.getRuleDAO().findOnly(param);
			if(Validator.isEmpty(item)){
				logger.info("Rule at Forum:"+forumId+" is null");
				param.setForumId(null);
				item = this.getRuleDAO().findOnly(param);
			}
			model = new RuleModel();
			BeanUtils.copyProperties(model,item);
			cache.put(CacheKeys.getRuleKey(forumId),model);
			//cache.put(CacheKeys.getRuleKey(forumId)+"-isput", new Boolean(true));
		}
		return model;
	}
	
	/**
	 * 此方法可以返回NULL
	 */
	public RuleModel findByForumId(int forumId) {
		RuleModel model = (RuleModel) cache.get(CacheKeys.getRuleKey(new Integer(forumId)));
		if(Validator.isEmpty(model)||model.getForumId()==0){
			Rule item = this.getRuleDAO().findByForumId(forumId);
			if(!Validator.isEmpty(item)){
				item = this.getRuleDAO().findByForumId(forumId);
				model = new RuleModel();
				BeanUtils.copyProperties(model,item);
			}
		}
		return model;
	}
	
	public double getElement(int forumId, int which, int num) {
		double d = 0;
		String str = new String();
		RuleParameter param = new RuleParameter();
		param.setForumId(forumId);
		RuleModel model = this.findOnly(param);
		/*
		if(Validator.isEmpty(model)){
			param = new RuleParameter();
			param.setForumId(null);
			model = this.findOnly(param);
		}
		*/
		if(model!=null){
			switch(which){
				case 1:
					str = model.getScores();
					logger.debug("scores:"+str);
				break;
				case 2:
					str = model.getMoneys();
					logger.debug("moneys:"+str);
				break;
				case 3:
					str = model.getCredits();
					logger.debug("credits:"+str);
				break;			
			}
		}else{
			logger.error("RuleModel is null");
		}
		if(str!=null){
			d = TypeChange.stringToDouble(StringHelper.locator(str,num,"|",null));
		}
		return d;
	}

	public void cacheClear() {
		cache.clear();
	}

	public RuleDAO getRuleDAO() {
		return DAOWrapper.<RuleDAO>getSingletonInstance(DAOLocator.RULE);
	}

	public double getCredit(String str, int locator) {
		return TypeChange.stringToDouble(StringHelper.locator(str,locator,"|","0"));
	}

	public double getMoney(String str, int locator) {
		return TypeChange.stringToDouble(StringHelper.locator(str,locator,"|","0"));
	}

	public double getScore(String str, int locator) {
		return TypeChange.stringToDouble(StringHelper.locator(str,locator,"|","0"));
	}

}







⌨️ 快捷键说明

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