📄 doccontent.java
字号:
package com.redmoon.oa.netdisk;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.oa.person.UserDb;public class DocContent implements java.io.Serializable { public static final String IMGKIND = "netdisk_document"; String content; int docId; int pageNum = 1; String connname = ""; transient Logger logger = Logger.getLogger(DocContent.class.getName()); private static final String INSERT = "INSERT into netdisk_doc_content (doc_id, content, page_num) VALUES (?,?,?)"; private static final String LOAD = "SELECT content from netdisk_doc_content WHERE doc_id=? and page_num=?"; private static final String DEL = "DELETE FROM netdisk_doc_content WHERE doc_id=? and page_num=?"; private static final String SAVE = "UPDATE netdisk_doc_content SET content=? WHERE doc_id=? and page_num=?"; private static final String LOAD_DOCUMENT_ATTACHMENTS = "SELECT id FROM netdisk_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")); 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]); sql = "insert into netdisk_document_attach (fullpath,doc_id,name,diskname,visualpath,page_num,orders,uploadDate) values (" + StrUtil.sqlstr(filepath) + "," + docId + "," + StrUtil.sqlstr(name) + "," + StrUtil.sqlstr(attachFileNames[i]) + "," + StrUtil.sqlstr(FilePath) + "," + pageNum + "," + orders + ",NOW())"; 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(); sql = "insert into netdisk_document_attach (fullpath,doc_id,name,diskname,visualpath,page_num,size,ext,uploadDate) values (" + StrUtil.sqlstr(filepath) + "," + docId + "," + StrUtil.sqlstr(fi.getName()) + "," + StrUtil.sqlstr(fi.getDiskName()) + "," + StrUtil.sqlstr(FilePath) + "," + pageNum + "," + fi.getSize() + "," + fi.getExt() + ",NOW())"; conn.executeUpdate(sql); } } else throw new ErrMsgException("上传失败!ret=" + ret); } Document doc = new Document(); doc = doc.getDocument(docId); PreparedStatement pstmt = null; if (pageNum <= doc.getPageCount() && pageNum != 1) { String sql = "update netdisk_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='" + IMGKIND + "'"; pstmt = conn.prepareStatement(sql); pstmt.setInt(1, docId); pstmt.setInt(2, pageNum); conn.executePreUpdate(); pstmt.close(); pstmt = null; sql = "update netdisk_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); 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 (conn != null) { conn.close(); conn = null; } } } public void delDocContentOfDocument(int docId) throws ErrMsgException { Conn conn = new Conn(connname); ResultSet rs = null; try { String sql = "select page_num from netdisk_doc_content where doc_id=" + docId; rs = conn.executeQuery(sql); if (rs!=null) { while (rs.next()) { int pn = rs.getInt(1); DocContent dc = getDocContent(docId, pn); dc.del(); } } } catch (SQLException e) { logger.error("delDocContentOfDocument:" + e.getMessage()); } finally { 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='" + IMGKIND + "' 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='" + IMGKIND + "' and subkey=" + pageNum; conn.executeUpdate(sql); Iterator ir = attachments.iterator(); while (ir.hasNext()) { Attachment att = (Attachment)ir.next(); logger.info("del: attach id=" + att.getId() + " att name=" + att.getName()); att.del(); } 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 netdisk_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='" + IMGKIND + "'"; pstmt = conn.prepareStatement(sql); pstmt.setInt(1, docId); pstmt.setInt(2, pageNum); conn.executePreUpdate(); pstmt.close(); pstmt = null; sql = "update netdisk_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; DocContentCacheMgr dcm = new DocContentCacheMgr();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -