📄 doccontent.java
字号:
DocContentCacheMgr dcm = new DocContentCacheMgr();
dcm.refreshDel(docId, pageNum);
for (int i = pageNum + 1; i < doc.getPageCount(); i++)
dcm.refreshUpdate(docId, pageNum);
} catch (SQLException e) {
re = false;
logger.error(e.getMessage());
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {}
rs = null;
}
if (conn != null) {
conn.close();
conn = null;
}
}
return re;
}
public synchronized boolean save(ServletContext application,
CMSMultiFileUploadBean mfu) throws
ErrMsgException {
String isuploadfile = StrUtil.getNullString(mfu.getFieldValue(
"isuploadfile"));
// logger.info("filepath=" + mfu.getFieldValue("filepath"));
if (isuploadfile.equals("false"))
return saveWithoutFile(application, mfu);
String FilePath = StrUtil.getNullString(mfu.getFieldValue("filepath"));
// String tempAttachFilePath = application.getRealPath("/") + FilePath +
// "/";
String tempAttachFilePath = Global.getRealPath() + FilePath +
"/";
mfu.setSavePath(tempAttachFilePath); //取得目录
File f = new File(tempAttachFilePath);
if (!f.isDirectory()) {
f.mkdirs();
}
ResultSet rs = null;
Conn conn = new Conn(connname);
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);
// 处理图片
int ret = mfu.getRet();
if (ret == 1) {
mfu.writeFile(false);
Vector files = mfu.getFiles();
// logger.info("files size=" + files.size());
java.util.Enumeration e = files.elements();
String filepath = "";
sql = "";
while (e.hasMoreElements()) {
FileInfo fi = (FileInfo) e.nextElement();
filepath = FilePath + "/" + fi.getName();
int imgId = (int)SequenceMgr.nextID(SequenceMgr.CMS_IMAGES);
sql =
"insert into cms_images (id,path,mainkey,kind,subkey) values (" +
imgId + "," +
StrUtil.sqlstr(filepath) + "," + docId +
",'document'," + pageNum + ")";
conn.executeUpdate(sql);
}
} else
throw new ErrMsgException(mfu.getErrMessage());
} catch (Exception e) {
logger.error("save:" + e.getMessage());
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {}
rs = null;
}
if (conn != null) {
conn.close();
conn = null;
}
}
saveWithoutFile(application, mfu);
return true;
}
public synchronized boolean saveWithoutFile(ServletContext application,
CMSMultiFileUploadBean mfu) throws
ErrMsgException {
content = StrUtil.getNullString(mfu.getFieldValue("htmlcode"));
Conn conn = new Conn(connname);
boolean re = true;
try {
String FilePath = StrUtil.getNullString(mfu.getFieldValue(
"filepath"));
// 处理附件
// String tempAttachFilePath = application.getRealPath("/") +
// FilePath +
// "/";
String tempAttachFilePath = Global.getRealPath() + FilePath +
"/";
mfu.setSavePath(tempAttachFilePath); // 取得目录
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 = getAttachmentMaxOrders() + 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 {
File f = new File(tempAttachFilePath);
if (!f.isDirectory()) {
f.mkdirs();
}
// 写入磁盘
mfu.writeAttachment(true);
Vector attachs = mfu.getAttachments();
Iterator ir = attachs.iterator();
String sql = "";
int orders = getAttachmentMaxOrders() + 1;
while (ir.hasNext()) {
FileInfo fi = (FileInfo) ir.next();
String 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,orders) values (" +
attachId + "," +
StrUtil.sqlstr(filepath) + "," + docId + "," +
StrUtil.sqlstr(fi.getName()) +
"," + StrUtil.sqlstr(fi.getDiskName()) + "," +
StrUtil.sqlstr(FilePath) + "," + pageNum + "," + orders + ")";
conn.executeUpdate(sql);
orders ++;
}
}
PreparedStatement pstmt = conn.prepareStatement(SAVE);
pstmt.setString(1, content);
pstmt.setInt(2, docId);
pstmt.setInt(3, pageNum);
re = conn.executePreUpdate() == 1 ? true : false;
pstmt.close();
// 更新缓存
DocContentCacheMgr dcm = new DocContentCacheMgr();
dcm.refreshUpdate(docId, pageNum);
} catch (SQLException e) {
re = false;
logger.error(e.getMessage());
} finally {
if (conn != null) {
conn.close();
conn = null;
}
}
return re;
}
public int getDocId() {
return docId;
}
public int getPageNum() {
return pageNum;
}
/**
*
* @param sql String
* @return int -1 表示sql语句不合法
*/
public static int getContentCount(int doc_id) {
DocContentCacheMgr dcm = new DocContentCacheMgr();
return dcm.getContentCount(doc_id);
}
public boolean create(ServletContext application,
CMSMultiFileUploadBean mfu, int doc_id,
String content, int pageNum) throws
ErrMsgException {
String sql = "";
String FilePath = StrUtil.getNullString(mfu.getFieldValue("filepath"));
// String tempAttachFilePath = application.getRealPath("/") + FilePath +
// "/";
// 用mysql数据库时,从application.getRealPath中获取的路径中的/号会丢失
String tempAttachFilePath = Global.getRealPath() + FilePath +
"/";
mfu.setSavePath(tempAttachFilePath); //取得目录
File f = new File(tempAttachFilePath);
if (!f.isDirectory()) {
f.mkdirs();
}
Conn conn = new Conn(connname);
boolean re = true;
try {
// 处理附件
int ret = mfu.getRet();
// 如果上传成功
if (ret == mfu.RET_SUCCESS) {
// 处理HTMLCODE中的文件
mfu.writeFile(false); // 用文件本来的名称命名文件
Vector files = mfu.getFiles();
java.util.Enumeration e = files.elements();
String filepath = "";
sql = "";
while (e.hasMoreElements()) {
FileInfo fi = (FileInfo) e.nextElement();
filepath = FilePath + "/" + fi.getName();
int imgId = (int)SequenceMgr.nextID(SequenceMgr.CMS_IMAGES);
sql =
"insert into cms_images (id,path,mainkey,kind,subkey) values (" +
imgId + "," +
StrUtil.sqlstr(filepath) + "," + docId +
",'document'," + this.pageNum + ")";
conn.executeUpdate(sql);
}
} else
throw new ErrMsgException(mfu.getErrMessage());
re = createWithoutFile(application, mfu, doc_id, content, pageNum);
} catch (SQLException e) {
logger.error("create:" + e.getMessage());
} finally {
if (conn != null) {
conn.close();
conn = null;
}
}
return re;
}
private Vector attachments = new Vector();
public Vector getAttachments() {
return attachments;
}
public synchronized boolean delAttachment(int attachId) {
Attachment am = new Attachment(attachId);
boolean re = am.del();
// 更新缓存
if (re) {
// 更新缓存
DocContentCacheMgr dcm = new DocContentCacheMgr();
dcm.refreshUpdate(docId, pageNum);
}
return re;
}
public synchronized boolean updateAttachmentName(int attachId,
String newname) {
String sql = "update document_attach set name=? where id=?";
Conn conn = new Conn(connname);
boolean re = false;
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, newname);
pstmt.setInt(2, attachId);
re = conn.executePreUpdate() == 1 ? true : false;
if (re) {
// 更新缓存
DocContentCacheMgr dcm = new DocContentCacheMgr();
dcm.refreshUpdate(docId, pageNum);
}
} catch (SQLException e) {
logger.error(e.getMessage());
} finally {
if (conn != null) {
conn.close();
conn = null;
}
}
return re;
}
public int getAttachmentMaxOrders() {
String GETMAXORDERS =
"select max(orders) from document_attach where doc_id=? and page_num=?";
Conn conn = new Conn(connname);
ResultSet rs = null;
int maxorders = -1;
try {
//更新文件内容
PreparedStatement pstmt = conn.prepareStatement(GETMAXORDERS);
pstmt.setInt(1, docId);
pstmt.setInt(2, pageNum);
rs = conn.executePreQuery();
if (rs != null) {
if (rs.next()) {
maxorders = rs.getInt(1);
}
}
} catch (SQLException e) {
logger.error("getMaxOrders:" + e.getMessage());
} finally {
if (conn != null) {
conn.close();
conn = null;
}
}
return maxorders;
}
public boolean moveAttachment(int attachId, String direction) {
Attachment attach = new Attachment(attachId);
boolean re = false;
int orders = attach.getOrders();
if (direction.equals("up")) {
if (orders == 1)
return true;
else {
Attachment upperAttach = new Attachment(orders - 1, docId,
pageNum);
if (upperAttach.isLoaded()) {
upperAttach.setOrders(orders);
upperAttach.save();
}
attach.setOrders(orders - 1);
re = attach.save();
}
} else {
int maxorders = getAttachmentMaxOrders();
if (orders == maxorders) {
return true;
} else {
Attachment lowerAttach = new Attachment(orders + 1, docId,
pageNum);
if (lowerAttach.isLoaded()) {
lowerAttach.setOrders(orders);
lowerAttach.save();
}
attach.setOrders(orders + 1);
re = attach.save();
}
}
// 更新缓存
DocContentCacheMgr dcm = new DocContentCacheMgr();
dcm.refreshUpdate(docId, pageNum);
return re;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -