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

📄 commentmgr.java

📁 云网论坛CWBBS 源码,内容丰富,学习,参考,教学的好资料,具体见内说明,
💻 JAVA
字号:
package cn.js.fan.module.cms;

import java.util.Vector;
import org.apache.log4j.Logger;
import java.util.Iterator;
import cn.js.fan.web.Global;
import javax.servlet.http.HttpServletRequest;
import cn.js.fan.util.ErrMsgException;
import java.sql.ResultSet;
import cn.js.fan.db.Conn;
import cn.js.fan.cache.jcs.*;
import cn.js.fan.base.IPrivilege;
import cn.js.fan.db.ListResult;
import cn.js.fan.db.SQLFilter;
import java.sql.SQLException;

public class CommentMgr {
    String connname = "";
    String cachePrix = "cmt";
    String cachePrixList = "cmt_list";
    RMCache rmCache;
    Logger logger = Logger.getLogger(CommentMgr.class.getName());

    public CommentMgr() {
        rmCache = RMCache.getInstance();
        connname = Global.defaultDB;
        if (connname.equals(""))
            logger.info("CommentMgr:connname is empty.");
    }

    /**
     * 取得doc_id的评论
     * @param doc_id int
     * @return Iterator
     */
    public Iterator getList(int doc_id) {
        Vector list  = (Vector) rmCache.get(this.
                cachePrixList + doc_id);

        Iterator listids = null;
        if (list!=null)
            listids = list.iterator();
        if (listids == null) {
            String sql = "select id from cms_comment where doc_id=" + doc_id +
                         " order by add_date";
            Conn conn = new Conn(connname);
            ResultSet rs = null;
            try {
                rs = conn.executeQuery(sql);
                if (rs!=null) {
                    Vector v = new Vector();
                    while (rs.next()) {
                        int id = rs.getInt(1);
                        v.addElement("" + id);
                    }
                    rmCache.put(cachePrixList + doc_id, v);
                    listids = v.iterator();
                }
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
            finally {
                if (conn!=null) {
                    conn.close(); conn = null;
                }
            }
        }

        Vector v = new Vector();
        while (listids.hasNext()) {
            String id = (String) listids.next();
            int intid = Integer.parseInt(id);
            v.addElement(getComment(intid));
        }
        return v.iterator();
    }

    public ListResult listResult(String listsql, int curPage, int pageSize) throws
            ErrMsgException {
        int total = 0;
        ResultSet rs = null;
        ListResult lr = new ListResult();
        Vector result = new Vector();
        lr.setTotal(total);
        lr.setResult(result);
        Conn conn = new Conn(connname);
        try {
            // 取得总记录条数
            String countsql = SQLFilter.getCountSql(listsql);
            rs = conn.executeQuery(countsql);
            if (rs != null && rs.next()) {
                total = rs.getInt(1);
            }
            if (rs != null) {
                rs.close();
                rs = null;
            }

            // 防止受到攻击时,curPage被置为很大,或者很小
            int totalpages = (int) Math.ceil((double) total / pageSize);
            if (curPage > totalpages)
                curPage = totalpages;
            if (curPage <= 0)
                 curPage = 1;
            if (total != 0)
                conn.setMaxRows(curPage * pageSize); //尽量减少内存的使用

            rs = conn.executeQuery(listsql);
            if (rs == null) {
                return lr;
            } else {
                rs.setFetchSize(pageSize);
                int absoluteLocation = pageSize * (curPage - 1) + 1;
                if (rs.absolute(absoluteLocation) == false) {
                    return lr;
                }
                do {
                    Comment cmm = getComment(rs.getInt(1));
                    result.addElement(cmm);
                } while (rs.next());
            }
        } catch (SQLException e) {
            logger.error(e.getMessage());
            // throw new ErrMsgException(SkinUtil.LoadString(req, SkinUtil.ERR_DB));
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {}
                rs = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }
        }
        lr.setResult(result);
        lr.setTotal(total);
        return lr;
    }

    public Comment getComment(int id) {
        Comment cmt = (Comment) rmCache.get(cachePrix + id);
        if (cmt == null) {
            cmt = new Comment(id);
            try {
                rmCache.put(cachePrix + id, cmt);
            } catch (Exception e) {
                logger.error("getComment:" + e.getMessage());
            }
            return cmt;
        } else {
            cmt.renew();
            return cmt;
        }
    }

    public boolean insert(HttpServletRequest request) throws ErrMsgException {
        CommentCheck cc = new CommentCheck();
        cc.checkInsert(request);
        Comment cmt = new Comment();
        boolean re = cmt.insert(cc.getDocId(), cc.getNick(), cc.getLink(),
                                cc.getContent(), cc.getIp());
        if (re) {
            //更新缓存
            try {
                rmCache.remove(this.cachePrixList + cc.getDocId());
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
        }
        return re;
    }

    public boolean del(HttpServletRequest request, IPrivilege privilege) throws
            ErrMsgException {
        CommentCheck cc = new CommentCheck();
        cc.checkDel(request);
        Comment cmt = getComment(cc.getId());
        boolean re = cmt.del(cc.getId());
        if (re) {
            //更新缓存
            try {
                rmCache.remove(cachePrixList + cmt.getDocId());
                rmCache.remove(cachePrix + cc.getId());
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
        }
        return re;
    }

    /**
     * 删除对应于doc_id文章的所有评论
     * @param request HttpServletRequest
     * @param privilege IPrivilege
     * @throws ErrMsgException
     */
    public void delAll(HttpServletRequest request, IPrivilege privilege) throws
            ErrMsgException {
        CommentCheck cc = new CommentCheck();
        cc.init();
        cc.chkDocId(request);
        cc.report();

        Iterator ir = getList(cc.getDocId());
        Comment cmt = null;
        // 删除并更新缓存
        try {
            while (ir.hasNext()) {
                cmt = (Comment) ir.next();
                cmt.del(cmt.getId());
                rmCache.remove(this.cachePrix + cc.getId());
            }
            rmCache.remove(this.cachePrixList + cc.getDocId());
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }
}

⌨️ 快捷键说明

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