documentmgr.java
来自「一个用java编写的功能强大的OA系统」· Java 代码 · 共 324 行
JAVA
324 行
package com.redmoon.oa.netdisk;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import cn.js.fan.base.*;import cn.js.fan.util.*;import cn.js.fan.web.*;import org.apache.log4j.*;import com.redmoon.oa.pvg.Privilege;import com.redmoon.kit.util.FileUpload;public class DocumentMgr { Logger logger = Logger.getLogger(DocumentMgr.class.getName()); public DocumentMgr() { } public Document getDocument(int id) { Document doc = new Document(); return doc.getDocument(id); } public Document getDocument(HttpServletRequest request, int id, IPrivilege privilege) throws ErrMsgException { boolean isValid = false; Document doc = getDocument(id); LeafPriv lp = new LeafPriv(doc.getDirCode()); if (lp.canUserSee(privilege.getUser(request))) { return doc; } if (!isValid) throw new ErrMsgException(Privilege.MSG_INVALID); return getDocument(id); } public Document getDocumentByCode(HttpServletRequest request, String code, IPrivilege privilege) throws ErrMsgException { boolean isValid = false; LeafPriv lp = new LeafPriv(code); if (lp.canUserSee(privilege.getUser(request))) isValid = true; if (!isValid) throw new ErrMsgException(Privilege.MSG_INVALID); Document doc = new Document(); int id = doc.getIDOrCreateByCode(code, privilege.getUser(request)); return getDocument(id); } public CMSMultiFileUploadBean doUpload(ServletContext application, HttpServletRequest request) throws ErrMsgException { CMSMultiFileUploadBean mfu = new CMSMultiFileUploadBean(); mfu.setMaxFileSize(Global.FileSize); com.redmoon.oa.Config cfg = new com.redmoon.oa.Config(); String exts = cfg.get("netdisk_ext").replaceAll(",", ","); String[] ext = StrUtil.split(exts, ","); if (ext!=null) mfu.setValidExtname(ext); int ret = 0; try { ret = mfu.doUpload(application, request); if (ret == -3) { throw new ErrMsgException(mfu.getErrMessage()); } if (ret == -4) { throw new ErrMsgException(mfu.getErrMessage()); } } catch (IOException e) { logger.error("doUpload:" + e.getMessage()); throw new ErrMsgException(e.getMessage()); } return mfu; } public boolean delAttachment(HttpServletRequest request) throws ErrMsgException { int doc_id = ParamUtil.getInt(request, "doc_id"); int attach_id = ParamUtil.getInt(request, "attach_id"); int page_num = ParamUtil.getInt(request, "page_num"); Document doc = new Document(); doc = doc.getDocument(doc_id); LeafPriv lp = new LeafPriv(doc.getDirCode()); Privilege privilege = new Privilege(); if (!lp.canUserDel(privilege.getUser(request))) { throw new ErrMsgException(SkinUtil.LoadString(request, "pvg_invalid")); } DocContent dc = doc.getDocContent(page_num); boolean re = dc.delAttachment(attach_id); return re; } public boolean updateAttachmentName(HttpServletRequest request) throws ErrMsgException { int doc_id = ParamUtil.getInt(request, "doc_id"); int attach_id = ParamUtil.getInt(request, "attach_id"); int page_num = ParamUtil.getInt(request, "page_num"); String newname = ParamUtil.get(request, "newname"); Document doc = getDocument(doc_id); LeafPriv lp = new LeafPriv(); lp.setDirCode(doc.getDirCode()); Privilege privilege = new Privilege(); if (!lp.canUserModify(privilege.getUser(request))) throw new ErrMsgException(SkinUtil.LoadString(request, "pvg_invalid")); DocContent dc = doc.getDocContent(page_num); boolean re = dc.updateAttachmentName(attach_id, newname); return re; } public boolean Operate(ServletContext application, HttpServletRequest request, IPrivilege privilege) throws ErrMsgException { CMSMultiFileUploadBean mfu = doUpload(application, request); String op = StrUtil.getNullStr(mfu.getFieldValue("op")); String dir_code = StrUtil.getNullStr(mfu.getFieldValue("dir_code")); boolean isValid = false; LeafPriv lp = new LeafPriv(dir_code); if (lp.canUserAppend(privilege.getUser(request))) isValid = true; if (!isValid) throw new ErrMsgException(SkinUtil.LoadString(request, "pvg_invalid")); Document doc = new Document(); if (op.equals("edit")) { String idstr = StrUtil.getNullString(mfu.getFieldValue("id")); if (!StrUtil.isNumeric(idstr)) throw new ErrMsgException("标识id=" + idstr + "非法,必须为数字!"); id = Integer.parseInt(idstr); doc = doc.getDocument(id); if (!lp.canUserAppend(privilege.getUser(request))) throw new ErrMsgException(SkinUtil.LoadString(request, "pvg_invalid")); boolean re = doc.Update(application, mfu); return re; } return false; } public boolean del(HttpServletRequest request, int id, IPrivilege privilege) throws ErrMsgException { boolean isValid = false; Document doc = new Document(); doc = getDocument(id); LeafPriv lp = new LeafPriv(doc.getDirCode()); if (lp.canUserDel(privilege.getUser(request))) return doc.del(); if (!isValid) throw new ErrMsgException(Privilege.MSG_INVALID); return doc.del(); } public boolean UpdateSummary(ServletContext application, HttpServletRequest request, IPrivilege privilege) throws ErrMsgException { CMSMultiFileUploadBean mfu = doUpload(application, request); int id = 0; try { id = Integer.parseInt(mfu.getFieldValue("id")); } catch (Exception e) { throw new ErrMsgException("id 非法!"); } Document doc = new Document(); doc = getDocument(id); LeafPriv lp = new LeafPriv(doc.getDirCode()); if (!lp.canUserModify(privilege.getUser(request))) { throw new ErrMsgException(privilege.MSG_INVALID); } return doc.UpdateSummary(application, mfu); } public boolean increaseHit(HttpServletRequest request, int id, IPrivilege privilege) throws ErrMsgException { Document doc = getDocument(id); boolean re = doc.increaseHit(); return re; } public boolean UpdateIsHome(HttpServletRequest request, int id, IPrivilege privilege) throws ErrMsgException { Document doc = new Document(); String v = ParamUtil.get(request, "value"); doc.setID(id); boolean re = doc.UpdateIsHome(v.equals("y") ? true : false); return re; } public boolean vote(HttpServletRequest request, int id) throws ErrMsgException { int votesel = ParamUtil.getInt(request, "votesel"); Document doc = getDocument(id); boolean re = doc.vote(id, votesel); return re; } public boolean OperatePage(ServletContext application, HttpServletRequest request, IPrivilege privilege) throws ErrMsgException { CMSMultiFileUploadBean mfu = doUpload(application, request); String op = StrUtil.getNullStr(mfu.getFieldValue("op")); String dir_code = StrUtil.getNullStr(mfu.getFieldValue("dir_code")); boolean isValid = false; LeafPriv lp = new LeafPriv(); lp.setDirCode(dir_code); if (op.equals("add")) { if (lp.canUserAppend(privilege.getUser(request))) isValid = true; } if (op.equals("edit")) { if (lp.canUserModify(privilege.getUser(request))) isValid = true; } if (!isValid) throw new ErrMsgException(Privilege.MSG_INVALID); String strdoc_id = StrUtil.getNullStr(mfu.getFieldValue("id")); int doc_id = Integer.parseInt(strdoc_id); Document doc = new Document(); doc = doc.getDocument(doc_id); if (op.equals("add")) { String content = StrUtil.getNullStr(mfu.getFieldValue( "htmlcode")); return doc.AddContentPage(application, mfu, content); } if (op.equals("edit")) { return doc.EditContentPage(application, mfu); } return false; } public boolean uploadDocument(ServletContext application, HttpServletRequest request) throws ErrMsgException { FileUpload fu = new FileUpload(); fu.setMaxFileSize(Global.FileSize); com.redmoon.oa.Config cfg = new com.redmoon.oa.Config(); String exts = cfg.get("netdisk_ext").replaceAll(",", ","); String[] ext = StrUtil.split(exts, ","); if (ext!=null) fu.setValidExtname(ext); int ret = 0; try { ret = fu.doUpload(application, request); if (ret != fu.RET_SUCCESS) { throw new ErrMsgException(fu.getErrMessage()); } } catch (Exception e) { logger.error("uploadDocument:" + e.getMessage()); } if (ret == fu.RET_SUCCESS) { String strId = fu.getFieldValue("id"); String strAttachId = fu.getFieldValue("attachId"); if (!StrUtil.isNumeric(strId)) throw new ErrMsgException("id 必须为数字"); if (!StrUtil.isNumeric(strAttachId)) throw new ErrMsgException("attachId 必须为数字"); Document doc = new Document(); doc = doc.getDocument(StrUtil.toInt(strId)); if (doc==null || !doc.isLoaded()) { throw new ErrMsgException("文件不存在!"); } Attachment att = doc.getAttachment(1, StrUtil.toInt(strAttachId)); LeafPriv lp = new LeafPriv(att.getDirCode()); Privilege privilege = new Privilege(); if (!lp.canUserModify(privilege.getUser(request))) { throw new ErrMsgException(SkinUtil.LoadString(request, "pvg_invalid")); } com.redmoon.kit.util.FileInfo fi = (com.redmoon.kit.util.FileInfo)(fu.getFiles().get(0)); return fi.write(Global.getRealPath() + att.getVisualPath() + "/", att.getDiskName()); } else return false; } public int getId() { return id; } private int id;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?