noallowedcharserviceimpl.java

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

JAVA
277
字号
/* 
 * Created on 2007-1-15
 * Last modified on 2008-1-1
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.club.service.util.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.yeqiangwei.club.model.NoAllowedChar;
import com.yeqiangwei.club.model.Reply;
import com.yeqiangwei.club.model.Topic;
import com.yeqiangwei.club.model.User;
import com.yeqiangwei.club.service.util.NoAllowedCharService;
import com.yeqiangwei.cache.Cache;
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.NoAllowedCharDAO;
import com.yeqiangwei.club.model.RContent;
import com.yeqiangwei.club.exception.ClubException;
import com.yeqiangwei.club.param.BaseParameter;
import com.yeqiangwei.club.util.MessageUtils;
import com.yeqiangwei.util.StringHelper;
import com.yeqiangwei.util.Validator;

public class NoAllowedCharServiceImpl implements NoAllowedCharService{
	
	private static final Logger logger = Logger.getLogger(NoAllowedCharServiceImpl.class);
	
	private static final Cache<NoAllowedChar> CACHE = CacheFactory.<NoAllowedChar>creator(CacheRegion.NOALLOWEDCHAR);
	
	public static NoAllowedCharDAO getNoAllowedCharDAO(){
		return DAOWrapper.<NoAllowedCharDAO>getSingletonInstance(DAOLocator.NOALLOWEDCHAR);
	}
	
	public User replaceUser(User model) {
		if(Validator.isEmpty(model)){
			model.setIntro(replaceOfUser(model.getIntro()));
			model.setArea(replaceOfUser(model.getArea()));
			model.setCity(replaceOfUser(model.getCity()));
			model.setEmailAddress(replaceOfUser(model.getEmailAddress()));
			model.setHomepage(replaceOfUser(model.getHomepage()));
			model.setPenName(replaceOfUser(model.getPenName()));
			model.setSignatures(replaceOfUser(model.getSignatures()));
			model.setUserName(replaceOfUser(model.getUserName()));
			model.setWork(replaceOfUser(model.getWork()));
		}
		return model;
	}
	
	public Topic replaceTopic(Topic model) {
		if(Validator.isEmpty(model)){
			model.setTitle(this.replaceOfTopic(model.getTitle()));
		}
		return model;
	}

	public Reply replaceReply(Topic model) {
		return null;
	}
	
	public Reply replaceReply(Reply item) {
		if(Validator.isEmpty(item)){
			item.setTitle(this.replaceOfTopic(item.getTitle()));
		}
		return item;
	}

	public RContent replaceRContent(RContent item) {
		if(Validator.isEmpty(item)){
			item.setContent(this.replaceOfTopic(item.getContent()));
		}
		return item;
	}
	
	public boolean permissionUser(User model) {
		if(this.noAllowedOfuser(model.getUserName())
				|| this.noAllowedOfuser(model.getPenName())
				|| this.noAllowedOfuser(model.getIntro())
				|| this.noAllowedOfuser(model.getArea())
				|| this.noAllowedOfuser(model.getCity())
				|| this.noAllowedOfuser(model.getSignatures())
		){
			return false;
		}else{
			return true;
		}
	}

	public boolean permissionTopic(Topic model) 
	{
		boolean permission = true;
		if(!Validator.isEmpty(model)){
			if(this.noAllowedOfTopic(model.getTitle())
					||this.noAllowedOfTopic(model.getTContent().getContent())
			){
				permission = false;
			}
		}
		return permission;
	}

	public boolean permissionTopic(Reply model) 
	{
		boolean permission = true;
		if(!Validator.isEmpty(model)){
			if(this.noAllowedOfTopic(model.getTitle())
					||this.noAllowedOfTopic(model.getRContent().getContent())
			){
				permission = false;
			}
		}
		return permission;
	}
	
	public void createOrUpdate(NoAllowedChar model) throws ClubException {
		if(model.getNoAllowedId()>0){
			this.update(model);
		}
		else{
			this.create(model);
		}
	}

	public void create(NoAllowedChar model) throws ClubException {
		if(!Validator.isEmpty(model)){
			getNoAllowedCharDAO().create(model);
		}else{
			throw new ClubException(MessageUtils.getMessage("error_system"));
		}
	}

	public void update(NoAllowedChar model) throws ClubException {
		if(!Validator.isEmpty(model)){
			getNoAllowedCharDAO().update(model);
			CACHE.remove("NoAllowedChar");
		}else{
			throw new ClubException(MessageUtils.getMessage("error_system"));
		}
	}


	public boolean noAllowedOfuser(String str) {
		//String theChars = findOnly().getNoAllowedUser();
		return this.noAllowedChar(str,findOnly().getNoAllowedUser());
	}

	public String replaceOfUser(String str) {
		//String theChars = findOnly().getReplaceUser();
		return this.replace(str,findOnly().getReplaceUser());
	}

	public boolean noAllowedOfTopic(String str) {
		//String theChars = findOnly().getNoAllowedTopic();
		return this.noAllowedChar(str,findOnly().getNoAllowedTopic());
	}

	public String replaceOfTopic(String str) {
		//String theChars = findOnly().getReplaceTopic();
		return this.replace(str,findOnly().getReplaceTopic());
	}

	public boolean noAllowedOfMessage(String str) {
		//String theChars = findOnly().getNoAllowedMessage();
		return this.noAllowedChar(str,findOnly().getNoAllowedMessage());
	}

	public String replaceOfMessage(String str) {
		return this.replace(str,findOnly().getReplaceMessage());
	}
	
	/**
	 * 
	 * @param str 字符串内容
	 * @param theChars 被摒闭的字符
	 * @return
	 */
	private boolean noAllowedChar(String str, String theChars) {
		boolean bea = false;
        if(!Validator.isEmpty(theChars)){
        	theChars = StringHelper.replace(theChars, "\r", "");
        	theChars = StringHelper.replace(theChars, "\n", "");
        	if(StringHelper.indexOf(theChars, "|")!=-1){
        		theChars = StringHelper.replace(theChars, "|", ",");
        	}
        	String[] ss = theChars.split(",");
        	for(int i=0; i<ss.length; i++){
        		if(!Validator.isEmpty(ss[i])&&StringHelper.indexOf(str, ss[i])!=-1){
        			logger.error("过滤字符:"+ss[i]);
        			bea = true; //表示字符串含过滤字符
        			break;
        		}
        	}
        	/*
            StringTokenizer st = new StringTokenizer(theChars, "|");
            for(int i=0;st.hasMoreTokens();i++){
            	String s = st.nextToken().trim();
                if(str.indexOf(s)!=-1){
                    bea = true;; //表示关键字key含过滤字符
                }
            }*/
        }
		return bea;
	}
	
	private String replace(String str, String theChars) {
		if(!Validator.isEmpty(str) && !Validator.isEmpty(theChars))
		{
			String[] ss = theChars.split("\n");
			for(int i=0; i<ss.length; i++){
				int c = StringHelper.indexOf(ss[i], "|");
				String s = StringHelper.substring(ss[i], 0, c, "");
				String e = StringHelper.substring(ss[i], c+1, ss[i].length(), "");
				logger.debug("replace:"+s+","+e);
				str = StringHelper.replace(str,s,e);
			}
			/*
            StringTokenizer st1 = new StringTokenizer(theChars, "\n");
            String[] sts1 = new String[st1.countTokens()];
            try{
                for(int i1=0; st1.hasMoreTokens(); i1++){
                    sts1[i1] = st1.nextToken().trim();
                    StringTokenizer st2 = new StringTokenizer(sts1[i1], "|");
                    String[] sts2 = new String[st2.countTokens()];
                     for(int i2=0; st2.hasMoreTokens(); i2++){
                         sts2[i2] = st2.nextToken().trim();
                     }
                     if(sts2.length>=2){
                    	 str = str.replaceAll(sts2[0],sts2[1]);
                     }
                }
             }catch(Exception e){
            	 logger.error(e.toString());
            }*/           
        }else{
        	return str;
        }
        return str;
	}


	public NoAllowedChar findById(int id) {
		return null;
	}

	@SuppressWarnings("unchecked")
	public List findByParameter(BaseParameter param) {
		return null;
	}

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

	public NoAllowedChar findOnly() {
		NoAllowedChar model = CACHE.get("NoAllowedChar");
		if(Validator.isEmpty(model)){
			model = getNoAllowedCharDAO().findOnly();
			CACHE.put("NoAllowedChar",model);
		}
		return model;
	}

	public int delete(NoAllowedChar model) {
		return 0;
	}
	

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

⌨️ 快捷键说明

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