NoteAction.java
上传用户:wlfwy2004
上传日期:2016-12-12
资源大小:33978k
文件大小:4k
源码类别:

Jsp/Servlet

开发平台:

Java

  1. package com.opensource.blog.web.action;
  2. import java.util.*;
  3. import javax.servlet.http.*;
  4. import org.apache.struts.action.*;
  5. import com.opensource.blog.exception.*;
  6. import com.opensource.blog.model.*;
  7. import com.opensource.blog.service.*;
  8. import com.opensource.blog.service.cache.app.*;
  9. import com.opensource.blog.web.form.*;
  10. public class NoteAction
  11.     extends Action {
  12.   private BlogCache blogCache;
  13.   private NoteService noteService;
  14.   private ArticleService articleService;
  15.   private BlogService blogService;
  16.   public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm,
  17.                                HttpServletRequest servletRequest,
  18.                                HttpServletResponse servletResponse) {
  19.     ActionMessages errors = new ActionMessages();
  20.     NoteForm form = (NoteForm) actionForm;
  21.     if (form.getAction().equalsIgnoreCase("add")) {
  22.       HttpSession session = servletRequest.getSession();
  23.       String authCode = (String) session.getAttribute("authcode");
  24.       if (authCode == null || !authCode.equals(form.getAuthCode())) {
  25.         errors.add("error.note.authcode", new ActionMessage("error.note.authcode"));
  26.         saveErrors(servletRequest, errors);
  27.         return actionMapping.findForward("error");
  28.       }
  29.       Blog blog = this.getBlogCache().getFromCache(form.getUsername());
  30.       if (blog == null) {
  31.         errors.add("error.blogblog.getinfo", new ActionMessage("error.blogblog.getinfo"));
  32.         saveErrors(servletRequest, errors);
  33.         return actionMapping.findForward("error");
  34.       }
  35.       Article art = this.getArticleService().findArticleByID_BlogID(form.getArtid(), form.getBlogid());
  36.       if (art == null) {
  37.         errors.add("error.read.getart", new ActionMessage("error.read.getart"));
  38.         saveErrors(servletRequest, errors);
  39.         return actionMapping.findForward("error");
  40.       }
  41.       Note note = new Note();
  42.       note.setArtid(form.getArtid());
  43.       note.setBlogid(form.getBlogid());
  44.       note.setContent(form.getContent());
  45.       note.setEmail(form.getEmail());
  46.       note.setFace(0);
  47.       note.setHomepage(form.getHomepage());
  48.       note.setName(form.getName());
  49.       note.setCreatetime(new Date());
  50.       note.setIp(servletRequest.getRemoteAddr());
  51.       try {
  52.         note = this.getNoteService().saveNote(note);
  53.       }
  54.       catch (BlogException ex) {
  55.         errors.add("error.note.add.error", new ActionMessage("error.note.add.error"));
  56.         saveErrors(servletRequest, errors);
  57.         return actionMapping.findForward("error");
  58.       }
  59.       int total = this.getNoteService().getNoteCountByArtID(form.getArtid());
  60.       art.setCommentnum(total);
  61.       try {
  62.         this.getArticleService().saveArticle(art);
  63.       }
  64.       catch (BlogException ex1) {
  65.         ex1.printStackTrace();
  66.       }
  67.       total = this.getNoteService().getNoteCountByBlogID(form.getBlogid());
  68.       blog.setNotetotal(total);
  69.       try {
  70.         this.getBlogService().saveBlog(blog);
  71.       }
  72.       catch (BlogException ex2) {
  73.         ex2.printStackTrace();
  74.       }
  75.       this.getBlogCache().removeFromCache(form.getUsername());
  76.       /*
  77.              ActionForward f = new ActionForward("/read.do?artid=" + form.getArtid() +
  78.                                           "&blogid=" + form.getBlogid() + "&username=" +
  79.                                           form.getUsername(), true);*/
  80.       ActionForward f = new ActionForward("/" + form.getUsername() + "-" + form.getBlogid() + "-" +
  81.                                           form.getArtid() + ".html", true);
  82.       return f;
  83.     }
  84.     return actionMapping.findForward("error");
  85.   }
  86.   public BlogCache getBlogCache() {
  87.     return blogCache;
  88.   }
  89.   public NoteService getNoteService() {
  90.     return noteService;
  91.   }
  92.   public ArticleService getArticleService() {
  93.     return articleService;
  94.   }
  95.   public BlogService getBlogService() {
  96.     return blogService;
  97.   }
  98.   public void setBlogCache(BlogCache blogCache) {
  99.     this.blogCache = blogCache;
  100.   }
  101.   public void setNoteService(NoteService noteService) {
  102.     this.noteService = noteService;
  103.   }
  104.   public void setArticleService(ArticleService articleService) {
  105.     this.articleService = articleService;
  106.   }
  107.   public void setBlogService(BlogService blogService) {
  108.     this.blogService = blogService;
  109.   }
  110. }