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

📄 doccontent.java

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

import org.apache.log4j.Logger;
import cn.js.fan.db.Conn;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import cn.js.fan.web.Global;
import com.redmoon.kit.util.FileInfo;
import cn.js.fan.util.ErrMsgException;
import cn.js.fan.util.StrUtil;
import java.io.File;
import java.util.Iterator;
import java.util.Vector;
import javax.servlet.ServletContext;
import com.redmoon.forum.SequenceMgr;

public class DocContent implements java.io.Serializable {
    String content;
    int docId;
    int pageNum = 1;

    String connname = "";
    transient Logger logger = Logger.getLogger(DocContent.class.getName());

    private static final String INSERT =
            "INSERT into doc_content (doc_id, content, page_num) VALUES (?,?,?)";
    private static final String LOAD =
            "SELECT content from doc_content WHERE doc_id=? and page_num=?";
    private static final String DEL =
            "DELETE FROM doc_content WHERE doc_id=? and page_num=?";
    private static final String SAVE =
            "UPDATE doc_content SET content=? WHERE doc_id=? and page_num=?";

    private static final String LOAD_DOCUMENT_ATTACHMENTS =
            "SELECT id FROM document_attach WHERE doc_id=? and page_num=? order by orders";

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

    public DocContent(int doc_id, int page_num) {
        this.docId = doc_id;
        this.pageNum = page_num;

        connname = Global.defaultDB;
        if (connname.equals(""))
            logger.info("DocContent:默认数据库名为空!");
        loadFromDb(doc_id, page_num);
    }

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

    public boolean createWithoutFile(ServletContext application,
                                     CMSMultiFileUploadBean mfu, int doc_id,
                                     String content, int pageNum) throws
            ErrMsgException {
        String FilePath = StrUtil.getNullString(mfu.getFieldValue("filepath"));
        // 因为mysql JDBC的原因,使得使用下句后路径会变为d:oaupfile/...
        // String tempAttachFilePath = application.getRealPath("/") + FilePath +
        //                             "/";
        String tempAttachFilePath = Global.getRealPath() + FilePath +
                                    "/";
        mfu.setSavePath(tempAttachFilePath); // 取得目录
        File f = new File(tempAttachFilePath);
        if (!f.isDirectory()) {
            f.mkdirs();
        }

        this.docId = doc_id;
        this.content = content;
        this.pageNum = pageNum;

        Conn conn = new Conn(connname);
        boolean re = true;
        try {
            // 处理附件
            int ret = mfu.getRet();
            String sisDdxc = StrUtil.getNullString(mfu.getFieldValue("isDdxc"));
            // 断点续传
            if (sisDdxc.equals("true")) {
                String[] attachFileNames = mfu.getFieldValues("attachFileName");
                String[] clientFilePaths = mfu.getFieldValues("clientFilePath");

                int len = 0;
                if (attachFileNames != null)
                    len = attachFileNames.length;
                String sql = "";
                int orders = 1;
                for (int i = 0; i < len; i++) {
                    String filepath = mfu.getSavePath() + attachFileNames[i];
                    String name = mfu.getUploadFileName(clientFilePaths[i]);
                    long attachId = SequenceMgr.nextID(SequenceMgr.DOCUMENT_ATTACH);
                    sql =
                            "insert into document_attach (id,fullpath,doc_id,name,diskname,visualpath,page_num,orders) values (" +
                            attachId + "," +
                            StrUtil.sqlstr(filepath) + "," + docId + "," +
                            StrUtil.sqlstr(name) + "," +
                            StrUtil.sqlstr(attachFileNames[i]) + "," +
                            StrUtil.sqlstr(FilePath) + "," + pageNum + "," + orders + ")";
                    conn.executeUpdate(sql);
                    orders ++;
                }
            } else {
                if (ret == mfu.RET_SUCCESS) {
                    String filepath = "";
                    String sql = "";
                    // 处理附件
                    mfu.writeAttachment(true); // 用随机名称命名文件
                    Vector attachs = mfu.getAttachments();
                    Iterator ir = attachs.iterator();
                    sql = "";
                    while (ir.hasNext()) {
                        FileInfo fi = (FileInfo) ir.next();
                        filepath = mfu.getSavePath() + fi.getDiskName();
                        long attachId = SequenceMgr.nextID(SequenceMgr.DOCUMENT_ATTACH);

                        sql =
                                "insert into document_attach (id,fullpath,doc_id,name,diskname,visualpath,page_num) values (" +
                                attachId + "," +
                                StrUtil.sqlstr(filepath) + "," + docId + "," +
                                StrUtil.sqlstr(fi.getName()) + "," +
                                StrUtil.sqlstr(fi.getDiskName()) + "," +
                                StrUtil.sqlstr(FilePath) + "," + pageNum + ")";
                        conn.executeUpdate(sql);
                    }
                } else
                    throw new ErrMsgException("上传失败!ret=" + ret);
            }
            // 如果页面是从中间某页后插入,而不是被加至末尾
            Document doc = new Document();
            doc = doc.getDocument(docId);
            PreparedStatement pstmt = null;
            // pageNum=1的时候,是插入新文章,而不是从中间某页后插入新页
            if (pageNum <= doc.getPageCount() && pageNum != 1) {
                // logger.info("pageNum=" + pageNum);
                // 更新其后的页面的页码
                String sql =
                        "update doc_content set page_num=page_num+1 where doc_id=? and page_num>=?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setInt(1, docId);
                pstmt.setInt(2, pageNum);
                conn.executePreUpdate();
                pstmt.close();
                pstmt = null;

                sql = "update cms_images set subkey=subkey+1 where mainkey=? and subkey>=? and kind='document'";
                pstmt = conn.prepareStatement(sql);
                pstmt.setInt(1, docId);
                pstmt.setInt(2, pageNum);
                conn.executePreUpdate();
                pstmt.close();
                pstmt = null;

                sql =
                        "update document_attach set page_num=page_num+1 where doc_id=? and page_num>=?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setInt(1, docId);
                pstmt.setInt(2, pageNum);
                conn.executePreUpdate();
                pstmt.close();
                pstmt = null;
            }

            // 更新缓存
            DocContentCacheMgr dcm = new DocContentCacheMgr();
            for (int i = pageNum; i < doc.getPageCount(); i++)
                dcm.refreshUpdate(docId, pageNum);

            pstmt = conn.prepareStatement(INSERT);
            pstmt.setInt(1, doc_id);
            pstmt.setString(2, content);
            pstmt.setInt(3, pageNum);
            re = conn.executePreUpdate() == 1 ? true : false;

            if (re) {
                // 更新缓存
                dcm.refreshCreate(docId);
            }

            // 更新文章的总页数
            if (pageNum != 1)
                doc.UpdatePageCount(doc.getPageCount() + 1);
        } catch (SQLException e) {
            logger.error(e.getMessage());
        } finally {
            if (conn != null) {
                conn.close();
                conn = null;
            }
        }

        return re;
    }

    public boolean create(int doc_id,
                          String content) {
        this.docId = doc_id;
        this.content = content;
        this.pageNum = 1;

        Conn conn = new Conn(connname);
        boolean re = false;
        try {
            PreparedStatement pstmt = conn.prepareStatement(INSERT);
            pstmt.setInt(1, doc_id);
            pstmt.setString(2, content);
            pstmt.setInt(3, pageNum);
            re = conn.executePreUpdate() == 1 ? true : false;

            if (re) {
                // 更新缓存
                DocContentCacheMgr dcm = new DocContentCacheMgr();
                dcm.refreshCreate(docId);
            }
        } catch (SQLException e) {
            logger.error(e.getMessage());
        } finally {
            if (conn != null) {
                conn.close();
                conn = null;
            }
        }
        return re;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public DocContent getDocContent(int doc_id, int page_num) {
        DocContentCacheMgr dccm = new DocContentCacheMgr();
        return dccm.getDocContent(doc_id, page_num);
    }

    public void loadFromDb(int doc_id, int page_num) {
        Conn conn = new Conn(connname);
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(LOAD);
            this.docId = doc_id;
            this.pageNum = page_num;
            pstmt.setInt(1, doc_id);
            pstmt.setInt(2, page_num);
            rs = conn.executePreQuery();
            if (rs != null) {
                if (rs.next()) {
                    content = rs.getString(1);
                }
            }
            if (pstmt != null) {
                pstmt.close();
                pstmt = null;
            }

            // 取得附件
            pstmt = conn.prepareStatement(LOAD_DOCUMENT_ATTACHMENTS);
            //logger.info(LOAD_DOCUMENT_ATTACHMENTS);
            pstmt.setInt(1, docId);
            pstmt.setInt(2, pageNum);
            rs = conn.executePreQuery();
            if (rs != null) {
                while (rs.next()) {
                    int aid = rs.getInt(1);
                    Attachment am = new Attachment(aid);
                    attachments.addElement(am);
                }
            }
        } catch (Exception e) {
            logger.error("loadFromDb: " + e.getMessage());
        } finally {
            /*
                         if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (Exception e) {}
                pstmt = null;
                         }
             */
            if (conn != null) {
                conn.close();
                conn = null;
            }
        }
    }

    public boolean del() throws ErrMsgException {
        Conn conn = new Conn(connname);
        boolean re = false;
        ResultSet rs = null;
        try {
            // 从磁盘删除图像文件
            String sql = "select path from cms_images where mainkey=" + docId +
                         " and kind='document' and subkey=" + pageNum;
            rs = conn.executeQuery(sql);
            if (rs != null) {
                String fpath = "";
                while (rs.next()) {
                    fpath = rs.getString(1);
                    if (fpath != null) {
                        File virtualFile = new File(Global.getRealPath() + fpath);
                        virtualFile.delete();
                    }
                }
            }
            if (rs != null) {
                rs.close();
                rs = null;
            }
            // 从数据库中删除图像文件
            sql = "delete from cms_images where mainkey=" + docId +
                  " and kind='document' and subkey=" + pageNum;
            conn.executeUpdate(sql);

            // 从磁盘删除附件
            sql = "select fullpath from document_attach where doc_id=" + docId +
                  " and page_num=" + pageNum;
            rs = conn.executeQuery(sql);
            if (rs != null) {
                String fpath = "";
                while (rs.next()) {
                    fpath = rs.getString(1);
                    if (fpath != null) {
                        File virtualFile = new File(fpath);
                        virtualFile.delete();
                    }
                }
            }
            if (rs != null) {
                rs.close();
                rs = null;
            }
            // 从数据库中删除附件
            sql = "delete from document_attach where doc_id=" + docId +
                  " and page_num=" + pageNum;
            conn.executeUpdate(sql);

            // 从数据库中删除
            PreparedStatement pstmt = conn.prepareStatement(DEL);
            pstmt.setInt(1, docId);
            pstmt.setInt(2, pageNum);
            re = conn.executePreUpdate() == 1 ? true : false;
            pstmt.close();
            pstmt = null;

            // 如果该页是最后一页,则不用更新
            Document doc = new Document();
            doc = doc.getDocument(docId);
            if (pageNum != doc.getPageCount()) {
                // 更新其后的页面的页码
                sql =
                        "update doc_content set page_num=page_num-1 where doc_id=? and page_num>?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setInt(1, docId);
                pstmt.setInt(2, pageNum);
                conn.executePreUpdate();
                pstmt.close();
                pstmt = null;

                sql = "update cms_images set subkey=subkey-1 where mainkey=? and subkey>? and kind='document'";
                pstmt = conn.prepareStatement(sql);
                pstmt.setInt(1, docId);
                pstmt.setInt(2, pageNum);
                conn.executePreUpdate();
                pstmt.close();
                pstmt = null;

                sql =
                        "update document_attach set page_num=page_num-1 where doc_id=? and page_num>?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setInt(1, docId);
                pstmt.setInt(2, pageNum);
                conn.executePreUpdate();
                pstmt.close();
                pstmt = null;
            }
            // 更新文章的总页数
            if (doc.getPageCount() > 1)
                doc.UpdatePageCount(doc.getPageCount() - 1);

            re = true;
            // 更新缓存

⌨️ 快捷键说明

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