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

📄 document.java

📁 源码/软件简介: 云网论坛1.1RC国际版是采用JSP开发的集论坛、CMS(网站内容管理系统)、博客、聊天室、商城、交友、语音灌水等于一体的门户式社区。拥有CWBBS ( Cloud Web BBS
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
package cn.js.fan.module.cms;

import java.io.File;
import java.sql.*;
import java.sql.Date;
import java.util.Iterator;
import java.util.Vector;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import cn.js.fan.base.ITagSupport;
import cn.js.fan.db.Conn;
import cn.js.fan.module.cms.plugin.PluginMgr;
import cn.js.fan.module.cms.plugin.PluginUnit;
import cn.js.fan.module.cms.plugin.base.IPluginRender;
import cn.js.fan.security.SecurityUtil;
import cn.js.fan.util.ErrMsgException;
import cn.js.fan.util.StrUtil;
import cn.js.fan.web.Global;
import com.redmoon.kit.util.FileInfo;
import org.apache.log4j.Logger;
import com.redmoon.kit.util.FileUpload;
import cn.js.fan.util.DateUtil;
import com.redmoon.forum.SequenceMgr;
import cn.js.fan.db.SQLFilter;
import cn.js.fan.db.ListResult;
import cn.js.fan.util.ResKeyException;
import cn.js.fan.web.SkinUtil;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class Document implements java.io.Serializable, ITagSupport {
    public static final int NOTEMPLATE = -1;
    String connname = "";
    int id = -1;
    String title;
    String content;
    String date;
    String class1;
    java.util.Date modifiedDate;
    String summary;
    boolean isHome = false;
    int examine = 0;

    public static final int EXAMINE_NOT = 0; // 未审核
    public static final int EXAMINE_NOTPASS = 1; // 未通过
    public static final int EXAMINE_PASS = 2; // 审核通过

    transient Logger logger = Logger.getLogger(Document.class.getName());

    private static final String INSERT_DOCUMENT =
            "INSERT into document (id, title, class1, type, voteoption, voteresult, nick, keywords, isrelateshow, can_comment, hit, template_id, parent_code, examine, isNew, author, flowTypeCode, modifiedDate) VALUES (?,?,?,?,?,?,?,?,?,?,0,?,?,?,?,?,?,?)";

    private static final String LOAD_DOCUMENT =
            "SELECT title, class1, modifiedDate, can_comment,summary,ishome,type,voteOption,voteResult,examine,nick,keywords,isrelateshow,hit,template_id,page_count,parent_code,isNew,author,flowTypeCode FROM document WHERE id=?";

    private static final String DEL_DOCUMENT =
            "delete FROM document WHERE id=?";
    private static final String SAVE_DOCUMENT =
            "UPDATE document SET title=?, can_comment=?, ishome=?, examine=?,keywords=?,isrelateshow=?,template_id=?,class1=?,isNew=?,author=?,flowTypeCode=?,modifiedDate=?,parent_code=? WHERE id=?";
    private static final String SAVE_SUMMARY =
            "UPDATE document SET summary=? WHERE id=?";
    private static final String SAVE_HIT =
            "UPDATE document SET hit=? WHERE id=?";

    public Document() {
        connname = Global.defaultDB;
        if (connname.equals(""))
            logger.info("Document:默认数据库名为空!");
    }

    /**
     * 从数据库中取出数据
     * @param id int
     * @throws ErrMsgException
     */
    public Document(int id) {
        connname = Global.defaultDB;
        if (connname.equals(""))
            logger.info("Document:conname can not be empty.");
        this.id = id;
        loadFromDB();
    }

    public void renew() {
        if (logger==null)
            logger = Logger.getLogger(Document.class.getName());
    }

    /**
     * 当directory结点的类型为文章时,根据code的值取得文章ID,如果文章不存,则创建文章
     * @param code String
     */
    public int getIDOrCreateByCode(String code, String nick) {
        int myid = getFirstIDByCode(code);
        if (myid != -1) {
            this.id = myid;
            loadFromDB();
        } else { // 文章不存在
            content = " "; // TEXT 类型字段,必须加一空格符,否则在读出时会出错
            Leaf leaf = new Leaf();
            leaf = leaf.getLeaf(code);
            title = leaf.getName();
            create(code, title, content, 0, "", "", nick, leaf.getTemplateId(), nick);
            this.id = getFirstIDByCode(code);
            // 更改目录中的doc_id
            //logger.info("id=" + id);
            leaf.setDocID(id);
            leaf.update();
        }
        return id;
    }

    public void delDocumentByDirCode(String code) throws ErrMsgException {
        Vector v = getDocumentsByDirCode(code);
        Iterator ir = v.iterator();
        while (ir.hasNext()) {
            Document doc = (Document) ir.next();
            doc.del();
        }
    }

    public Vector getDocumentsByDirCode(String code) {
        Vector v = new Vector();
        String sql = "select id from document where class1=" +
                     StrUtil.sqlstr(code);
        Conn conn = new Conn(connname);
        ResultSet rs = null;
        try {
            rs = conn.executeQuery(sql);
            if (rs != null) {
               while (rs.next()) {
                   v.addElement(getDocument(rs.getInt(1)));
               }
            }
        } catch (SQLException e) {
            logger.error(e.getMessage());
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {}
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }
        }
        return v;
    }

    /**
     * 当确定dirCode为文章型结点时,取得其对应的文章,但是当节点对应的文章还没创建时,会出错
     * @param dirCode String
     * @return Document
     */
    public Document getDocumentByDirCode(String dirCode) {
        Leaf leaf = new Leaf();
        leaf = leaf.getLeaf(dirCode);
        //logger.info("dirCode=" + dirCode);

        if (leaf != null && leaf.isLoaded() &&
            leaf.getType() == leaf.TYPE_DOCUMENT) {
            int id = leaf.getDocID();
            return getDocument(id);
        } else
            return null; // throw new ErrMsgException("该结点不是文章型节点或者文章尚未创建!");

    }

    /**
     * 取得结点为code的文章的ID,只取第一个
     * @param code String
     * @return int -1未取得
     */
    public int getFirstIDByCode(String code) {
        String sql = "select id from document where class1=" +
                     StrUtil.sqlstr(code);
        Conn conn = new Conn(connname);
        ResultSet rs = null;
        try {
            rs = conn.executeQuery(sql);
            if (rs != null && rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException e) {
            logger.error(e.getMessage());
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {}
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }
        }
        return -1;
    }

    public String getSummary() {
        return this.summary;
    }

    public int getID() {
        return id;
    }

    public void setID(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public boolean getIsHome() {
        return this.isHome;
    }

    public void setIsHome(boolean h) {
        this.isHome = h;
    }

    public void setType(int type) {
        this.type = type;
    }

    public void setVoteOption(String voteOption) {
        this.voteOption = voteOption;
    }

    public void setVoteResult(String voteResult) {
        this.voteResult = voteResult;
    }

    public String getContent(int pageNum) {
        DocContent dc = new DocContent();
        dc = dc.getDocContent(id, pageNum);
        if (dc!=null)
            return dc.getContent();
        else
            return null;
    }

    public long getDocCountNoCache(String listsql) {
        int total = 0;
        Conn conn = new Conn(connname);
        try {
            // 取得总记录条数
            String countsql = SQLFilter.getCountSql(listsql);
            ResultSet rs = conn.executeQuery(countsql);
            if (rs != null && rs.next()) {
                total = rs.getInt(1);
            }
        } catch (SQLException e) {
            logger.error(e.getMessage());
        } finally {
            if (conn != null) {
                conn.close();
                conn = null;
            }
        }
        return total;
    }

    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;
            }

            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 {
                    Document cmm = getDocument(rs.getInt(1));
                    result.addElement(cmm);
                } while (rs.next());
            }
        } catch (SQLException e) {
            logger.error(e.getMessage());
            throw new ErrMsgException("listResult: DB operate error.");
        } 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;
    }

    /**
     * 列出从 start 至 end 的文章,
     * @param sql String
     * @param start int 索引值, zero based
     * @param end int 索引值, zero based
     * @return Vector
     */
    public Vector list(String sql, int start, int end) {
        start += 1;  // 索引从0开始
        end += 1;

        ResultSet rs = null;
        Vector result = new Vector();
        Conn conn = new Conn(connname);
        try {
            conn.setMaxRows(end); //尽量减少内存的使用
            rs = conn.executeQuery(sql);
            if (rs == null) {
                return result;
            } else {
                // defines the number of rows that will be read from the database when the ResultSet needs more rows
                rs.setFetchSize(end - start + 1); // rs一次从POOL中所获取的记录数

                if (rs.absolute(start) == false) {
                    return result;
                }
                do {
                    int id = rs.getInt(1);
                    Document doc = getDocument(id);
                    result.addElement(doc);
                } while (rs.next());
            }
        } catch (SQLException e) {
            logger.error("list: " + e.getMessage());
        } finally {
            if (conn != null) {
                conn.close();
                conn = null;
            }
        }
        return result;
    }

    /**
     * 获取前count个数据
     * @param sql String

⌨️ 快捷键说明

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