📄 documentmgr.java
字号:
package cn.js.fan.module.cms;
import javax.servlet.http.HttpServletRequest;
import cn.js.fan.util.ErrMsgException;
import javax.servlet.ServletContext;
import java.io.IOException;
import org.apache.log4j.Logger;
import cn.js.fan.util.StrUtil;
import cn.js.fan.base.IPrivilege;
import cn.js.fan.util.ParamUtil;
import cn.js.fan.module.pvg.Privilege;
import cn.js.fan.web.Global;
import cn.js.fan.module.cms.plugin.PluginMgr;
import cn.js.fan.module.cms.plugin.PluginUnit;
import cn.js.fan.module.cms.plugin.base.IPluginDocumentAction;
import com.redmoon.kit.util.FileUpload;
import cn.js.fan.web.SkinUtil;
import cn.js.fan.util.ResKeyException;
import java.util.Vector;
import com.cloudwebsoft.framework.template.TemplateLoader;
import cn.js.fan.util.file.FileUtil;
import com.cloudwebsoft.framework.util.LogUtil;
import java.util.Date;
import java.util.Iterator;
import cn.js.fan.util.DateUtil;
import java.io.File;
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);
}
/**
* 当directory的结点code的类型为文章时,取其文章,如果文章不存在,则创建文章
* @param request HttpServletRequest
* @param code String
* @param privilege IPrivilege
* @return Document
* @throws ErrMsgException
*/
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); // 每个文件最大30000K 即近300M
// String[] ext = {"htm", "gif", "bmp", "jpg", "png", "rar", "doc", "hs", "ppt", "rar", "zip", "jar"};
// mfu.setValidExtname(ext);
int ret = 0;
// logger.info("ret=" + ret);
try {
ret = mfu.doUpload(application, request);
if (ret == -3) {
throw new ErrMsgException(mfu.getErrMessage(request));
}
if (ret == -4) {
throw new ErrMsgException(mfu.getErrMessage(request));
}
} catch (IOException e) {
logger.error("doUpload:" + e.getMessage());
}
return mfu;
}
public boolean Operate(ServletContext application,
HttpServletRequest request, IPrivilege privilege) throws
ErrMsgException {
// 检查是否到期,用于猫头鹰
// java.util.Date logicalDay = new java.util.Date(2006, 7, 1);
// if (DateUtil.compare(new java.util.Date(), logicalDay) == 1)
// return false;
CMSMultiFileUploadBean mfu = doUpload(application, request);
String op = StrUtil.getNullStr(mfu.getFieldValue("op"));
String dir_code = StrUtil.getNullStr(mfu.getFieldValue("dir_code"));
// logger.info("Operate dir_code=" + dir_code);
boolean isValid = false;
if (op.equals("contribute")) { // || privilege.isValid(request)) { //isAdmin(user, pwdmd5)) {
isValid = true;
} else {
LeafPriv lp = new LeafPriv();
lp.setDirCode(dir_code);
if (op.equals("edit")) {
if (lp.canUserModify(privilege.getUser(request)))
isValid = true;
} else {
if (lp.canUserAppend(privilege.getUser(request)))
isValid = true;
}
}
if (!isValid)
throw new ErrMsgException(Privilege.MSG_INVALID);
Document doc = new Document();
if (op.equals("edit")) {
String idstr = StrUtil.getNullString(mfu.getFieldValue("id"));
if (!StrUtil.isNumeric(idstr))
throw new ErrMsgException(SkinUtil.LoadString(request,
SkinUtil.ERR_ID));
int id = Integer.parseInt(idstr);
doc = doc.getDocument(id);
LeafPriv lp = new LeafPriv(doc.getDirCode());
if (!lp.canUserModify(privilege.getUser(request)))
throw new ErrMsgException(SkinUtil.LoadString(request,
SkinUtil.PVG_INVALID));
boolean re = doc.Update(application, mfu);
if (re) {
PluginMgr pm = new PluginMgr();
PluginUnit pu = pm.getPluginUnitOfDir(dir_code);
if (pu != null) {
IPluginDocumentAction ipda = pu.getDocumentAction();
re = ipda.update(application, mfu, doc);
}
// 生成静态页面
Config cfg = new Config();
boolean isCreateHtml = cfg.getBooleanProperty("cms.html_auto");
if (isCreateHtml) {
createHtml(request, doc, 1);
}
}
return re;
} else {
LeafPriv lp = new LeafPriv(dir_code);
if (!lp.canUserAppend(privilege.getUser(request)))
throw new ErrMsgException(SkinUtil.LoadString(request,
SkinUtil.PVG_INVALID));
boolean re = false;
try {
re = doc.create(application, mfu, privilege.getUser(request));
} catch (ResKeyException e) {
throw new ErrMsgException(e.getMessage(request));
}
if (re) {
PluginMgr pm = new PluginMgr();
PluginUnit pu = pm.getPluginUnitOfDir(dir_code);
if (pu != null) {
IPluginDocumentAction ipda = pu.getDocumentAction();
doc = doc.getDocument(doc.getID());
re = ipda.create(application, mfu, doc);
}
// 生成静态页面
Config cfg = new Config();
boolean isCreateHtml = cfg.getBooleanProperty("cms.html_auto");
if (isCreateHtml) {
doc = doc.getDocument(doc.getId());
createHtml(request, doc, 1);
}
}
return re;
}
}
public void delBatch(HttpServletRequest request, boolean isDustbin) throws ErrMsgException {
String strids = ParamUtil.get(request, "ids");
String[] ids = StrUtil.split(strids, ",");
if (ids==null)
return;
int len = ids.length;
Document doc = null;
for (int i=0; i<len; i++) {
doc = getDocument(Integer.parseInt(ids[i]));
if (isDustbin)
doc.UpdateExamine(Document.EXAMINE_DUSTBIN);
else
doc.del();
}
}
public void resumeBatch(HttpServletRequest request) throws ErrMsgException {
String strids = ParamUtil.get(request, "ids");
String[] ids = StrUtil.split(strids, ",");
if (ids==null)
return;
int len = ids.length;
Document doc = null;
for (int i=0; i<len; i++) {
doc = getDocument(Integer.parseInt(ids[i]));
doc.UpdateExamine(Document.EXAMINE_PASS);
}
}
public void passExamineBatch(HttpServletRequest request) throws ErrMsgException {
String strids = ParamUtil.get(request, "ids");
String[] ids = StrUtil.split(strids, ",");
if (ids==null)
return;
int len = ids.length;
Document doc = null;
for (int i=0; i<len; i++) {
doc = getDocument(Integer.parseInt(ids[i]));
doc.UpdateExamine(Document.EXAMINE_PASS);
}
}
/**
* 将文件彻底删除或者删至回收站
* @param request HttpServletRequest
* @param id int
* @param privilege IPrivilege
* @return boolean
* @throws ErrMsgException
*/
public boolean del(HttpServletRequest request, int id, IPrivilege privilege, boolean isDustbin) throws
ErrMsgException {
Document doc = new Document();
doc = getDocument(id);
if (doc == null || !doc.isLoaded()) {
throw new ErrMsgException(SkinUtil.LoadString(request,
"res.cms.Document", "err_file_not_found"));
}
LeafPriv lp = new LeafPriv(doc.getDirCode());
if (lp.canUserDel(privilege.getUser(request))) {
if (isDustbin) {
return doc.UpdateExamine(Document.EXAMINE_DUSTBIN);
}
else {
// 彻底删除
return doc.del();
}
}
else
throw new ErrMsgException(Privilege.MSG_INVALID);
}
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(SkinUtil.LoadString(request,
SkinUtil.ERR_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);
}
boolean re = false;
try {
re = doc.UpdateSummary(application, mfu);
} catch (ResKeyException e) {
throw new ErrMsgException(e.getMessage(request));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -