?? article.java
字號:
package com.jeecms.article.entity;
import static com.jeecms.core.Constants.ENCODING;
import static com.jeecms.core.Constants.SPT;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jeecms.article.entity.base.BaseArticle;
import com.jeecms.core.entity.Attachment;
import com.jeecms.core.util.ContentInterface;
import com.ponyjava.common.util.ComUtils;
import com.ponyjava.common.util.StrUtils;
public class Article extends BaseArticle implements ContentInterface {
private static final long serialVersionUID = 1L;
private static final Logger log = LoggerFactory.getLogger(Article.class);
/**
* 文章表內容的存放地址
*/
public static final String ARTICLE_PATH = "article_content";
/**
* 在附件表中的類別
*/
public static final String ATTACHMENT_CTG = "文章";
/**
* 文章系統相對地址
*/
public static final String UPLOAD_PATH = SPT + "article";
public static final String SUFFIX = ".txt";
public static final int SPLIT_COUNT = 1000;
public static final String SPLIT = "<p>[NextPage][/NextPage]</p>";
public static final String SPLIT_REG = "<p>\\[NextPage\\]\\[/NextPage\\]</p>";
/**
* 獲得url地址
*
* @return
*/
public String getUrl() {
if (!StringUtils.isBlank(getOuterUrl())) {
return getOuterUrl();
}
StringBuilder sb = getWebsite().getWebUrlBuf();
String path = getChannel().getPath();
if (!StringUtils.isBlank(path)) {
sb.append(SPT).append(path);
}
sb.append(SPT).append(getId()).append(".").append(
getWebsite().getSuffix());
return sb.toString();
}
/**
* 選擇模板
*
* @return
*/
public String chooseTpl() {
String s = getTplContent();
if (StringUtils.isBlank(s)) {
return getChannel().chooseTplContent();
} else {
return getWebsite().getTplRoot().append(getTplContent()).toString();
}
}
/**
* 獲得第N頁的內容相對路徑。用戶包含文件。
*
* @param pageNo
* @return
*/
public String relPath(int pageNo) {
StringBuilder sb = getWebsite().getUserRoot();
sb.append(SPT).append(ARTICLE_PATH).append(SPT).append(
(getId() / SPLIT_COUNT) + 1).append(SPT).append(getId())
.append("_").append(pageNo).append(SUFFIX);
return sb.toString();
}
/**
* 獲得第N頁的絕對地址
*
* @param root
* @param pageNo
* @return
*/
private String getRealPath(String root, int pageNo) {
StringBuilder sb = new StringBuilder(root);
sb.append(relPath(pageNo));
return sb.toString().replace(SPT, File.separatorChar);
}
/**
* 從文件讀取內容
*
* @return
*/
public String getContentFromFile() {
if (rootReal == null) {
throw new RuntimeException("請先設置服務器絕對路徑rootReal");
}
return getContentFromFile(rootReal);
}
/**
* 從文件讀取內容
*
* @param root
* @return
*/
public String getContentFromFile(String root) {
// @ TODO 處理資源路徑、域名、防盜鏈路徑改變的問題
StringBuilder sb = new StringBuilder();
try {
File f = null;
Integer count = getPageCount();
if (count == null) {
count = 0;
}
for (int i = 0; i < count; i++) {
f = new File(getRealPath(root, i + 1));
sb.append(FileUtils.readFileToString(f, ENCODING));
if (i + 1 < count) {
sb.append(SPLIT);
}
}
} catch (IOException e) {
log.error("讀取文章內容失敗", e);
}
return sb.toString();
}
/**
* 刪除文章文件
*
* @param root
*/
public void deleteContentFile(String root) {
File f = null;
Integer count = getPageCount();
log.debug("刪除文章內容:{},共有 {} 頁", getId(), count);
if (count == null) {
count = 0;
}
for (int i = 0; i < count; i++) {
f = new File(getRealPath(root, i + 1));
if (f.delete()) {
log.info("刪除 文章內容 成功:{}_{},{}", new Object[] { getId(), i + 1,
f.getAbsolutePath() });
} else {
log.warn("刪除 文章內容 失敗:{}_{},{}", new Object[] { getId(), i + 1,
f.getAbsolutePath() });
}
}
}
/**
* 將內容寫入文件。并刪除多余分頁。
*
* @param root
* 系統絕對根路徑
* @param origCount
* 原有分頁。0為原文章沒有內容
*/
public void writeContent(String root, int origCount) {
String c = getContent();
if (c == null) {
c = "";
}
String[] arr = c.split(SPLIT_REG);
try {
int currCount = arr.length;
if (currCount == 0) {
arr = new String[] { "" };
}
// 寫文件
for (int i = 0; i < currCount; i++) {
File f = new File(getRealPath(root, i + 1));
// 防止空串無法寫入文件
if (StringUtils.isEmpty(arr[i])) {
f.createNewFile();
} else {
FileUtils.writeStringToFile(f, arr[i], ENCODING);
}
log.info("寫 文章內容 成功:{}_{},{}", new Object[] { getId(), i + 1,
f.getAbsolutePath() });
}
// 刪除原剩余文件
for (int i = currCount; i < origCount; i++) {
File f = new File(getRealPath(root, i + 1));
if (f.delete()) {
log.info("刪除 文章內容 成功:{}_{},{}", new Object[] { getId(),
i + 1, f.getAbsolutePath() });
} else {
log.warn("刪除 文章內容 失敗:{}_{},{}", new Object[] { getId(),
i + 1, f.getAbsolutePath() });
}
}
} catch (IOException e) {
log.error("寫文章內容失敗", e);
}
}
/**
* 獲得內容中的頁數
*
* @return
*/
public void calculatePageCount() {
String c = getContent();
int count = 1;
if (!StringUtils.isEmpty(c)) {
count = c.split(SPLIT_REG).length;
}
if (count < 1) {
count = 1;
}
setPageCount(count);
}
/**
* 獲得內容圖片的URL地址
*
* @return
*/
public String getCttImgUrl() {
String img = getContentImg();
if (StringUtils.isBlank(img)) {
// TODO 鏈接到圖片默認的提示圖片
return "";
} else {
return getWebsite().getUploadUrlBuf().append(img).toString();
}
}
/**
* 資源URL是否改變。
*
* 如域名、部署路徑、端口號改變,資源URL也會改變。這樣將導致文章中的圖片無法顯示。
*
* @return
*/
public boolean isResPathChannge() {
String ourl = getContentResPath();
// 如果必須的字段為空,則有可能不是持久化對象。
if (StringUtils.isBlank(ourl) || getWebsite() == null
|| StringUtils.isBlank(getWebsite().getResUrl())) {
return false;
}
String curl = getWebsite().getResUrl();
return !ourl.equals(curl);
}
/**
* 更新資源路徑
*/
public void updateResPath() {
String newResPath = getWebsite().getResUrl();
String oldResPath = getContentResPath();
if (StringUtils.equals(newResPath, oldResPath)) {
return;
}
String content = getContentFromFile();
content = content.replace(oldResPath, newResPath);
writeContent(getRootReal(), 0);
setContentResPath(newResPath);
}
public void addToAttachments(Attachment attachment) {
Set<Attachment> attachments = getAttachments();
if (attachments == null) {
attachments = new HashSet<Attachment>();
setAttachments(attachments);
}
attachments.add(attachment);
}
/**
* 獲得置頂時間
*
* @return
*/
public int getTopHour() {
Date date = getSortDate();
if (date == null) {
return 0;
}
long remain = date.getTime() - System.currentTimeMillis();
if (remain <= 0) {
return 0;
}
return (int) remain / (1000 * 60 * 60);
}
public String desc(int len) {
String s = getDescription();
if (StringUtils.isBlank(s)) {
return "";
} else {
return StrUtils.getCn(s, len);
}
}
public String getImgUrl() {
String img = getTitleImg();
if (StringUtils.isBlank(img)) {
// TODO 鏈接到圖片默認的提示圖片
return "";
} else {
return getWebsite().getUploadUrlBuf().append(img).toString();
}
}
public boolean isTitBold() {
return getBold();
}
public String stit(int len) {
String s = getShortTitle();
if (StringUtils.isBlank(s)) {
s = getTitle();
}
if (StringUtils.isBlank(s)) {
return "";
} else {
return StrUtils.getCn(s, len);
}
}
public String tit(int len) {
String s = getTitle();
if (StringUtils.isBlank(s)) {
return "";
} else {
return StrUtils.getCn(s, len);
}
}
public String getTitCol() {
String s = getTitleColor();
if (s == null) {
return "";
} else {
return s;
}
}
public String getCtgName() {
return getChannel().getName();
}
public String getCtgUrl() {
return getChannel().getUrl();
}
public String getWebName() {
return getWebsite().getShortName();
}
public String getWebUrl() {
return getWebsite().getWebUrl();
}
public String getDate(int style) {
Date date = getReleaseDate();
return ComUtils.formatDate(date, style);
}
private String content;
private String rootReal;
/* [CONSTRUCTOR MARKER BEGIN] */
public Article() {
super();
}
/**
* Constructor for primary key
*/
public Article(java.lang.Long id) {
super(id);
}
/**
* Constructor for required fields
*/
public Article(java.lang.Long id,
com.jeecms.cms.entity.ContentCtg contentCtg,
com.jeecms.cms.entity.CmsChannel channel,
com.jeecms.core.entity.Website website,
com.jeecms.cms.entity.CmsConfig config, java.util.Date sortDate,
java.util.Date releaseDate, java.util.Date releaseSysDate,
java.lang.Long visitTotal, java.lang.Long visitToday,
java.lang.Long visitWeek, java.lang.Long visitMonth,
java.lang.Long visitQuarter, java.lang.Long visitYear,
java.lang.Integer checkStep, java.lang.Integer topLevel,
java.lang.Integer commentCount, java.lang.Boolean hasTitleImg,
java.lang.Boolean allowComment, java.lang.Boolean bold,
java.lang.Boolean draft, java.lang.Boolean recommend,
java.lang.Boolean check, java.lang.Boolean disabled,
java.lang.Boolean reject) {
super(id, contentCtg, channel, website, config, sortDate, releaseDate,
releaseSysDate, visitTotal, visitToday, visitWeek, visitMonth,
visitQuarter, visitYear, checkStep, topLevel, commentCount,
hasTitleImg, allowComment, bold, draft, recommend, check,
disabled, reject);
}
/* [CONSTRUCTOR MARKER END] */
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getRootReal() {
return rootReal;
}
public void setRootReal(String rootReal) {
this.rootReal = rootReal;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -