📄 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;
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());
}
if (ret == -4) {
throw new ErrMsgException(mfu.getErrMessage());
}
} 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);
}
}
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);
}
}
return re;
}
}
public boolean del(HttpServletRequest request, int id, IPrivilege privilege) throws
ErrMsgException {
boolean isValid = false;
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)))
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(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));
}
return re;
}
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);
// logger.info("filepath=" + mfu.getFieldValue("filepath"));
if (op.equals("add")) {
String content = StrUtil.getNullStr(mfu.getFieldValue(
"htmlcode"));
return doc.AddContentPage(application, mfu, content);
}
if (op.equals("edit")) {
// return doc.EditContentPage(content, pageNum);
return doc.EditContentPage(application, mfu);
}
return false;
}
public boolean uploadDocument(ServletContext application, HttpServletRequest request) throws ErrMsgException {
java.util.Date currentTime = new java.util.Date();
long inserttime = currentTime.getTime();
String filenm = String.valueOf(inserttime);
// String[] extnames = {"jpg", "gif", "xls", "rar", "doc", "rm", "avi",
// "bmp", "swf"};
FileUpload TheBean = new FileUpload();
// TheBean.setValidExtname(extnames); // 设置可上传的文件类型
TheBean.setMaxFileSize(Global.FileSize); // 最大35000K
int ret = 0;
try {
ret = TheBean.doUpload(application, request);
if (ret == -3) {
String str = SkinUtil.LoadString(request, "res.cms.Document", "err_upload_size");
str = StrUtil.format(str, new Object[] {"" + Global.FileSize});
throw new ErrMsgException(str);
}
if (ret == -4) {
throw new ErrMsgException(TheBean.getErrMessage());
}
}
catch (Exception e) {
logger.error("uploadDocument:" + e.getMessage());
}
if (ret == 1) {
Document doc = new Document();
boolean re = false;
try {
re = doc.uploadDocument(TheBean);
} catch (ResKeyException e) {
throw new ErrMsgException(e.getMessage(request));
}
return re;
} else
return false;
}
/*
// 使用此函数在上传时可能会导致进度至30%时传不上去,因为使用的是FileUploadBean
public boolean uploadDocument(HttpServletRequest request) throws ErrMsgException {
java.util.Date currentTime = new java.util.Date();
long inserttime = currentTime.getTime();
String filenm = String.valueOf(inserttime);
String[] extnames = {"jpg", "gif", "xls", "rar", "doc", "rm", "avi",
"bmp", "swf"};
FileUploadBean TheBean = new FileUploadBean();
TheBean.setValidExtname(extnames); // 设置可上传的文件类型
TheBean.setDefaultFileSize(35000); // 最大35000K
int ret = 0;
try {
ret = TheBean.doUpload(request, filenm);
if (ret == -3) {
throw new ErrMsgException("您上传的文件太大,请把文件大小限制在35000K以内!");
}
if (ret == -4) {
throw new ErrMsgException("文件非法!");
}
}
catch (Exception e) {
logger.error("uploadDocument:" + e.getMessage());
}
if (ret == 1) {
Document doc = new Document();
return doc.uploadDocument(TheBean);
}
else
return false;
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -