Skip to content

Commit

Permalink
添加评论显示
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou committed Mar 9, 2015
1 parent 0387128 commit 67046ad
Show file tree
Hide file tree
Showing 48 changed files with 653 additions and 292 deletions.
9 changes: 5 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,12 @@
<version>1.2.3</version>
</dependency>

<!-- xmlrpc支持 -->
<dependency>
<groupId>org.kohsuke.redstone</groupId>
<artifactId>redstone</artifactId>
<version>1.1.1</version>
</dependency>
<groupId>org.kohsuke.redstone</groupId>
<artifactId>redstone</artifactId>
<version>1.1.1</version>
</dependency>

</dependencies>
<build>
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/com/zblog/biz/CommentManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.zblog.biz;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.zblog.core.plugin.MapContainer;
import com.zblog.core.plugin.TreeUtils;
import com.zblog.core.util.constants.CommentConstants;
import com.zblog.service.CommentService;
import com.zblog.service.PostService;

@Component
public class CommentManager{
@Autowired
private CommentService commentService;
@Autowired
private PostService postService;

public List<MapContainer> getAsTree(String postid, String creator){
List<MapContainer> list = commentService.listByPost(postid, creator);
TreeUtils.rebuildTree(list);

return list;
}

/**
* 更改评论状态,同时更改该评论对应的post的评论数
*
* @param commentid
* @param newStatus
*/
@Transactional
public void setStatus(String commentid, String newStatus){
commentService.setStatus(commentid, newStatus);
postService.addCcount(commentid, CommentConstants.TYPE_APPROVE.equals(newStatus) ? 1 : -1);
}

/**
* 删除评论,同时删除对应文章的评论数
*
* @param commentid
*/
@Transactional
public void deleteComment(String commentid){
commentService.deleteById(commentid);
postService.addCcount(commentid, -1);
}

}
13 changes: 5 additions & 8 deletions src/main/java/com/zblog/biz/MetaWeblogManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import redstone.xmlrpc.XmlRpcException;
import redstone.xmlrpc.XmlRpcStruct;

import com.zblog.core.dal.entity.Category;
import com.zblog.core.dal.entity.Post;
import com.zblog.core.dal.entity.Upload;
import com.zblog.core.dal.entity.User;
Expand Down Expand Up @@ -182,17 +181,15 @@ public Object getRecentPosts(String blogid, String username, String pwd, int num
if(user == null)
loginError();

List<MapContainer> list = postService.listRecent(numberOfPosts);
List<MapContainer> list = postManager.listRecent(numberOfPosts);
MapContainer[] result = new MapContainer[list.size()];
for(int i = 0; i < list.size(); i++){
MapContainer temp = list.get(i);
Category category = categoryService.loadById(temp.getAsString("categoryid"));
result[i] = new MapContainer("dateCreated", temp.getAsDate("createTime"))
.put("userid", temp.getAsString("creator")).put("postid", temp.getAsString("id")).put("description", "")
.put("title", temp.getAsString("title")).put("link", toLink("/posts/" + temp.get("id")))
.put("permaLink", toLink("/posts/" + temp.get("id"))).put("categories", Arrays.asList(category.getName()))
.put("post_status", "publish");
;
.put("userid", temp.getAsString("creator")).put("postid", temp.getAsString("id"))
.put("description", temp.getAsString("content")).put("title", temp.getAsString("title"))
.put("link", toLink("/posts/" + temp.get("id"))).put("permaLink", toLink("/posts/" + temp.get("id")))
.put("categories", Arrays.asList(temp.getAsString("categoryName"))).put("post_status", "publish");
}
return result;
}
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/zblog/biz/PostManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.zblog.biz.aop.PostIndexManager;
import com.zblog.core.dal.entity.Post;
import com.zblog.core.plugin.MapContainer;
import com.zblog.core.plugin.PageModel;
import com.zblog.core.util.CollectionUtils;
import com.zblog.core.util.JsoupUtils;
import com.zblog.core.util.constants.PostConstants;
Expand All @@ -28,6 +30,8 @@ public class PostManager{
private UploadService uploadService;
@Autowired
private OptionsService optionsService;
@Autowired
private PostIndexManager postIndexManager;

/**
* 插入文章,同时更新上传文件的postid
Expand Down Expand Up @@ -106,6 +110,28 @@ public Collection<MapContainer> listPageAsTree(){
return tree;
}

public List<MapContainer> listRecent(int nums){
List<String> list = postService.listRecent(nums);
List<MapContainer> result = new ArrayList<>(list.size());
for(String id : list){
result.add(postService.loadReadById(id));
}

return result;
}

public PageModel search(String word, int pageIndex){
PageModel page = postIndexManager.search(word, pageIndex);
/* 填充其他属性,更好的做法是:搜索结果只包含对象id,详细资料到数据库查询(缓存) */
for(MapContainer mc : page.getContent()){
MapContainer all = postService.loadReadById(mc.getAsString("id"));
mc.put("createTime", all.get("createTime")).put("nickName", all.get("nickName"))
.put("rcount", all.get("rcount"));
}

return page;
}

/**
* 去掉图片中src地址的http域名前缀
*
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/com/zblog/biz/VisitStatManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.zblog.biz;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zblog.service.PostService;

/**
* 文章的访问统计
*
* @author zhou
*
*/
@Component
public class VisitStatManager{
private static final Logger logger = LoggerFactory.getLogger(VisitStatManager.class);
@Autowired
private PostService postService;
private ConcurrentMap<String, Integer> visit = new ConcurrentHashMap<>();

public void flush(){
ConcurrentMap<String, Integer> copy = visit;
visit = new ConcurrentHashMap<String, Integer>();
if(!copy.isEmpty()){
logger.debug("flush visit stat to database");
}

for(Map.Entry<String, Integer> entry : copy.entrySet()){
postService.addRcount(entry.getKey(), entry.getValue());
}
copy.clear();
copy = null;
}

public void record(String postid){
Integer count = visit.get(postid);
/* 该数据,并发问题忽略 */
visit.put(postid, count == null ? 1 : count + 1);
}

}
12 changes: 1 addition & 11 deletions src/main/java/com/zblog/biz/aop/PostIndexManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
import org.apache.lucene.index.Term;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zblog.core.dal.entity.Post;
import com.zblog.core.lucene.LuceneUtils;
import com.zblog.core.lucene.QueryBuilder;
import com.zblog.core.lucene.SearchEnginer;
import com.zblog.core.plugin.MapContainer;
import com.zblog.core.plugin.PageModel;
import com.zblog.core.util.JsoupUtils;
import com.zblog.core.util.constants.PostConstants;
import com.zblog.service.PostService;

/**
* 文章Lucene索引管理器
Expand All @@ -27,8 +24,6 @@
@Component
public class PostIndexManager{
private static final Logger logger = LoggerFactory.getLogger(PostIndexManager.class);
@Autowired
private PostService postService;

/**
* 只有添加文章才插入Lucene索引
Expand Down Expand Up @@ -61,12 +56,8 @@ public PageModel search(String word, int pageIndex){
PageModel result = new PageModel(pageIndex, 15);
QueryBuilder builder = new QueryBuilder(SearchEnginer.postEnginer().getAnalyzer());
builder.addShould("title", word).addShould("excerpt", word);
builder.addLighter("title","excerpt");
SearchEnginer.postEnginer().searchHighlight(builder, result);
/* 填充其他属性 */
for(MapContainer mc : result.getContent()){
MapContainer all = postService.loadReadById(mc.getAsString("id"));
mc.put("createTime", all.get("createTime")).put("nickName", all.get("nickName")).put("rcount", all.get("rcount"));
}

return result;
}
Expand All @@ -77,7 +68,6 @@ private Document convert(Post post){
doc.add(new Field("title", post.getTitle(), LuceneUtils.searchType()));
/* 用jsoup剔除html标签 */
doc.add(new Field("excerpt", JsoupUtils.plainText(post.getContent()), LuceneUtils.searchType()));
doc.add(new Field("creator", post.getCreator(), LuceneUtils.storeType()));
// doc.add(new LongField("createTime", post.getCreateTime().getTime(),
// LuceneUtils.storeType()));
return doc;
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/zblog/biz/aop/StaticTemplate.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package com.zblog.biz.aop;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;

import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -10,6 +17,7 @@
import com.zblog.biz.PostManager;
import com.zblog.core.dal.entity.Post;
import com.zblog.core.plugin.MapContainer;
import com.zblog.core.util.constants.Constants;
import com.zblog.core.util.constants.PostConstants;
import com.zblog.core.util.constants.WebConstants;
import com.zblog.service.CategoryService;
Expand All @@ -35,6 +43,23 @@ public class StaticTemplate{
@Autowired
private LinkService linksService;

public boolean staticIndex(String url, File file){
boolean result = true;
Writer writer = null;
try{
String text = Jsoup.connect(url).execute().body();
writer = new OutputStreamWriter(new FileOutputStream(file), Charset.forName(Constants.ENCODING_UTF_8));
IOUtils.write(text, writer);
}catch(IOException e){
logger.error("staticIndex error-->" + url, e);
result = true;
}finally{
IOUtils.closeQuietly(writer);
}

return result;
}

/**
* 静态化导航栏
*/
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/zblog/core/dal/mapper/CommentMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
public interface CommentMapper extends BaseMapper{

List<MapContainer> listRecent();

public List<MapContainer> listCountByGroupStatus();

/**
* 根据postid获取被批准的评论和指定creator的评论
Expand All @@ -24,6 +26,6 @@ public interface CommentMapper extends BaseMapper{
*
* @param commentid
*/
void setStutas(@Param("commentid")String commentid, @Param("status")String status);
void setStatus(@Param("commentid")String commentid, @Param("status")String status);

}
12 changes: 7 additions & 5 deletions src/main/java/com/zblog/core/dal/mapper/PostMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.zblog.core.plugin.PageModel;

public interface PostMapper extends BaseMapper{

public MapContainer loadReadById(String postid);

/**
Expand All @@ -36,14 +36,16 @@ public interface PostMapper extends BaseMapper{
*/
List<MapContainer> listPage(boolean onlyParent);

public void addRcount(@Param("postid") String postid, @Param("count") int count);

public void addCcount(@Param("commentid") String commentid, @Param("count") int count);

/**
* 获取最近发表文章
*
* @return
* @return 文章id
*/
List<MapContainer> listRecent(int nums);

List<MapContainer> listRss();
List<String> listRecent(int nums);

List<MapContainer> listByCategory(PageModel model);

Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/zblog/core/dal/mybatis/CommentMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
</select>

<select id="list" parameterType="PageModel" resultType="MapContainer">
select c.id, c.postid, c.url, c.email, c.ip, c.content, c.creator, c.status, c.createTime, p.title
from comment c left join post p on c.postid=p.id
select c.id, c.postid, c.url, c.email, c.ip, c.content, c.creator, c.status, c.createTime, p.title, t.id as pid, t.creator as pcreator
from comment c left join post p on c.postid=p.id left join comment t on c.parent=t.id
where c.status in <foreach collection="query.status" open="(" close=")" item="item" separator=",">#{item}</foreach>
</select>

<select id="listCountByGroupStatus" resultType="MapContainer">
select status, count(*) as count from comment group by status
</select>

<select id="listRecent" resultType="MapContainer">
select c.id, c.content, c.creator, c.createTime, p.title
from comment c left join post p on c.postid=p.id order by c.createTime desc limit 10
Expand All @@ -30,7 +34,7 @@
values(#{id},#{postid},#{parent},#{email},#{url},#{ip},#{agent},#{content},#{creator},#{createTime})
</insert>

<update id="setStutas">
<update id="setStatus">
update comment set status=#{status} where id=#{commentid}
</update>

Expand Down
Loading

0 comments on commit 67046ad

Please sign in to comment.