ruleserviceimpl.java

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

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

import java.util.List;

import org.apache.log4j.Logger;

import com.yeqiangwei.cache.Cache;
import com.yeqiangwei.club.cache.CacheRegion;
import com.yeqiangwei.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.model.Forum;
import com.yeqiangwei.club.model.Rule;
import com.yeqiangwei.club.exception.ClubException;
import com.yeqiangwei.club.param.RuleParameter;
import com.yeqiangwei.club.service.ServiceLocator;
import com.yeqiangwei.club.service.ServiceWrapper;
import com.yeqiangwei.club.service.forum.ForumService;
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 static final Cache<Rule> CACHE_RULE = CacheFactory.<Rule>creator(CacheRegion.RULE);

	public Rule findById(int id) {
		if(id>0){
			Rule model = CACHE_RULE.get(CacheKeys.getRuleKey(id));
			if(Validator.isEmpty(model)){
				model = this.getRuleDAO().findById(id);
				if(!Validator.isEmpty(model)){
					CACHE_RULE.put(CacheKeys.getRuleKey(model.getRuleId()),model);
				}
			}
			return model;
		}
		return null;
	}
	
	public Rule findGlobalRule(){
		Rule model = this.getRuleDAO().findGlobalRule();
		if(!Validator.isEmpty(model)){
			CACHE_RULE.put(CacheKeys.getRuleKey(model.getRuleId()),model);
		}
		return model;
	}

	public void createOrUpdate(Rule model) throws ClubException {
		if(model.getRuleId()>0){
			this.update(model);
		}else{
			this.create(model);
		}
	}
	
	public void create(Rule model) throws ClubException {
		this.getRuleDAO().create(model);
		model.setRuleId(model.getRuleId()); //PO->BO
		CACHE_RULE.put(CacheKeys.getRuleKey(model.getRuleId()),model);
		if(model.getGlobal()){
			this.getRuleDAO().updateOtherUnglobal(model.getRuleId());
		}
	}

	public void update(Rule model) throws ClubException {
		if(Validator.isEmpty(model)||model.getRuleId()<=0){
			throw new ClubException(MessageUtils.getMessage("error_update_noid"));
		}
		this.getRuleDAO().update(model);
		CACHE_RULE.put(CacheKeys.getRuleKey(model.getRuleId()),model);
		if(model.getGlobal()){
			this.getRuleDAO().updateOtherUnglobal(model.getRuleId());
		}
	}

	public int delete(Rule model) throws ClubException {
		if(model.getRuleId()>0){
			CACHE_RULE.remove(CacheKeys.getRuleKey(model.getRuleId()));
			return this.getRuleDAO().delete(model);
		}else{
			throw new ClubException(MessageUtils.getMessage("error_param"));
		}
	}

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

	public List<Rule> findByParameter(RuleParameter param) {
		return this.getRuleDAO().findByParameter(param);
	}

	public long countByParameter(RuleParameter param) {
		return this.getRuleDAO().countByParameter(param);
	}
	
	/**
	 * 如果版面未制定积分规则,则返回全局规则。
	 */
	public Rule findForumRule(int forumId) {
		Rule model = null;
		Forum forum = this.getForumService().findById(forumId);
		if(!Validator.isEmpty(forum)){
			model = this.findById(forum.getRuleId());
			if(Validator.isEmpty(model)){
				forum.setRuleId(0);
				try {
					this.getForumService().update(forum);
				} catch (ClubException e) {
					logger.error(e.toString());
				}
			}
		}
		if(Validator.isEmpty(model)){
			model = this.getRuleDAO().findGlobalRule();
		}
		if(Validator.isEmpty(model)){
			model = new Rule();
		}
		return model;
	}
	
	public double getElement(int forumId, int which, int num) {
		double d = 0;
		String str = null;
		Rule model = this.findForumRule(forumId);
		switch(which){
			case RuleService.SCORE:
				str = model.getScores();
				logger.debug("scores:"+str);
			break;
			case RuleService.MONEY:
				str = model.getMoneys();
				logger.debug("moneys:"+str);
			break;
			case RuleService.CREDIT:
				str = model.getCredits();
				logger.debug("credits:"+str);
			break;
			case RuleService.VIEWS:
				str = model.getViews();
				logger.debug("views:"+str);
			break;	
		}
		if(str!=null){
			d = TypeChange.stringToDouble(StringHelper.locator(str,num,"|",null));
		}
		return d;
	}

	public void clearCache() {
		CACHE_RULE.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"));
	}
	
	public int getViews(String str, int locator) {
		return TypeChange.stringToInt(StringHelper.locator(str,locator,"|","0"));
	}
	
	private ForumService getForumService() {
		return ServiceWrapper.<ForumService>getSingletonInstance(ServiceLocator.FORUM);
	}

}

⌨️ 快捷键说明

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