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

Jsp/Servlet

开发平台:

Java

  1. package com.opensource.blog.web.action;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.List;
  5. import java.util.Locale;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import org.apache.struts.action.ActionForm;
  9. import org.apache.struts.action.ActionForward;
  10. import org.apache.struts.action.ActionMapping;
  11. import org.apache.struts.action.ActionMessage;
  12. import org.apache.struts.action.ActionMessages;
  13. import org.apache.struts.util.LabelValueBean;
  14. import org.apache.struts.util.MessageResources;
  15. import com.laoer.comm.util.Util;
  16. import com.opensource.blog.exception.BlogException;
  17. import com.opensource.blog.model.Article;
  18. import com.opensource.blog.model.Blog;
  19. import com.opensource.blog.model.Sort;
  20. import com.opensource.blog.service.ArticleService;
  21. import com.opensource.blog.service.SortService;
  22. import com.opensource.blog.web.form.ArticleForm;
  23. import com.opensource.blog.web.servlet.UserSession;
  24. public class MyArticle
  25.     extends BaseAction {
  26.   private SortService sortService;
  27.   private ArticleService articleService;
  28.   public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm,
  29.                                HttpServletRequest servletRequest,
  30.                                HttpServletResponse servletResponse) {
  31.     ActionMessages errors = new ActionMessages();
  32.     ArticleForm form = (ArticleForm) actionForm;
  33.     UserSession us = this.getUserSession(servletRequest);
  34.     MessageResources messages = getResources(servletRequest);
  35.     Locale locale = getLocale(servletRequest);
  36.     List sortSelList = new ArrayList();
  37.     List sortlist = this.getSortService().findSortsByBlogID(us.getBlog().getId().longValue());
  38.     sortSelList.add(new LabelValueBean(messages.getMessage(locale, "blog.select"), "0"));
  39.     for (int i = 0; i < sortlist.size(); i++) {
  40.       Sort sort = (Sort) sortlist.get(i);
  41.       sortSelList.add(new LabelValueBean(sort.getSortname(), String.valueOf(sort.getId())));
  42.     }
  43.     servletRequest.setAttribute("sortSelList", sortSelList);
  44.     if (form.getAction().equalsIgnoreCase("new")) {
  45.       form.setAction("add");
  46.       form.setArtid(0);
  47.       form.setArtkey("");
  48.       form.setBlogsortid(us.getBlog().getSort());
  49.       form.setContent("");
  50.       form.setContentsize(0);
  51.       form.setDay(0);
  52.       form.setFace(1);
  53.       form.setFaceplace(0);
  54.       form.setHour(0);
  55.       form.setIshide(1);
  56.       form.setMin(0);
  57.       form.setOpencomment(0);
  58.       form.setPostmonth(0);
  59.       form.setSec(0);
  60.       form.setSortid(0);
  61.       form.setTitle("");
  62.       form.setUsesign(1);
  63.       form.setYear(0);
  64.       return actionMapping.findForward("article");
  65.     }
  66.     if (form.getAction().equalsIgnoreCase("add")) {
  67.       Article article = new Article();
  68.       long nowtime = Util.getLongTime();
  69.       int postmonth = Util.getMonth(nowtime);
  70.       String postdate = "";
  71.       if (form.getYear() != 0 && form.getPostmonth() != 0 && form.getDay() != 0) {
  72.         nowtime = Util.Time2Long(form.getYear(), form.getPostmonth(),
  73.                                  form.getDay(), form.getHour(), form.getMin(),
  74.                                  form.getSec());
  75.         postmonth = form.getPostmonth();
  76.       }
  77.       postdate = Util.formatDate7(new Date(nowtime));
  78.       article.setArtkey(form.getArtkey());
  79.       article.setBlogid(us.getBlog().getId().longValue());
  80.       article.setBlogname(us.getBlog().getBlogname());
  81.       article.setBlogsortid(form.getBlogsortid());
  82.       article.setCommentnum(0);
  83.       article.setContent(form.getContent());
  84.       article.setUserid(us.getUserInfo().getId().longValue());
  85.       article.setUsername(us.getUserInfo().getUsername());
  86.       article.setTrackback("");
  87.       article.setTrackbacknum(0);
  88.       article.setContentsize(form.getContentsize());
  89.       //article.setFace(form.getFace());
  90.       article.setFace(1);
  91.       article.setFaceplace(form.getFaceplace());
  92.       article.setFaceid(us.getUserInfo().getFace());
  93.       article.setIshide(form.getIshide());
  94.       article.setOpencomment(form.getOpencomment());
  95.       article.setPostdate(postdate);
  96.       article.setPostmonth(postmonth);
  97.       article.setPosttime(new Date());
  98.       article.setSign(us.getUserInfo().getSign());
  99.       article.setSortid(form.getSortid());
  100.       article.setTitle(form.getTitle());
  101.       article.setUsesign(form.getUsesign());
  102.       Sort sort = this.getSortService().findSortByID_BlogID(form.getSortid(),
  103.           us.getBlog().getId().longValue());
  104.       if (sort == null) {
  105.         article.setSortname("");
  106.       }
  107.       else {
  108.         article.setSortname(sort.getSortname());
  109.       }
  110.       try {
  111.         Object[] o = this.getArticleService().createArticle(article, us.getBlog());
  112.         this.saveUserSession(servletRequest, (Blog) o[1]);
  113.         return actionMapping.findForward("addok");
  114.       }
  115.       catch (BlogException ex) {
  116.         errors.add("error.newart.add", new ActionMessage("error.newart.add"));
  117.         saveErrors(servletRequest, errors);
  118.         return actionMapping.findForward("error");
  119.       }
  120.     }
  121.     if (form.getAction().equalsIgnoreCase("edit")) {
  122.       Article article = this.getArticleService().findArticleByID_BlogID(form.getArtid(),
  123.           us.getBlog().getId().longValue());
  124.       if (article == null) {
  125.         errors.add("error.read.getart", new ActionMessage("error.read.getart"));
  126.         saveErrors(servletRequest, errors);
  127.         return actionMapping.findForward("error");
  128.       }
  129.       form.setAction("editdo");
  130.       form.setArtkey(article.getArtkey());
  131.       form.setBlogsortid(article.getBlogsortid());
  132.       form.setContent(article.getContent());
  133.       form.setContentsize(article.getContentsize());
  134.       form.setDay(Util.getDay(article.getPosttime()));
  135.       form.setFace(article.getFace());
  136.       form.setFaceplace(article.getFaceplace());
  137.       form.setHour(Util.getHour(article.getPosttime()));
  138.       form.setIshide(article.getIshide());
  139.       form.setMin(Util.getMinute(article.getPosttime()));
  140.       form.setOpencomment(article.getOpencomment());
  141.       form.setPostmonth(article.getPostmonth());
  142.       form.setSec(Util.getSecond(article.getPosttime()));
  143.       form.setSortid(article.getSortid());
  144.       form.setTitle(article.getTitle());
  145.       form.setUsesign(article.getUsesign());
  146.       form.setYear(Util.getYear(article.getPosttime()));
  147.       return actionMapping.findForward("article");
  148.     }
  149.     if (form.getAction().equalsIgnoreCase("editdo")) {
  150.       Article article = this.getArticleService().findArticleByID_BlogID(form.getArtid(),
  151.           us.getBlog().getId().longValue());
  152.       if (article == null) {
  153.         errors.add("error.read.getart", new ActionMessage("error.read.getart"));
  154.         saveErrors(servletRequest, errors);
  155.         return actionMapping.findForward("error");
  156.       }
  157.       long nowtime = Util.getLongTime();
  158.       int postmonth = Util.getMonth(nowtime);
  159.       String postdate = "";
  160.       if (form.getYear() != 0 && form.getPostmonth() != 0 && form.getDay() != 0) {
  161.         nowtime = Util.Time2Long(form.getYear(), form.getPostmonth(),
  162.                                  form.getDay(), form.getHour(), form.getMin(),
  163.                                  form.getSec());
  164.         postmonth = form.getPostmonth();
  165.       }
  166.       postdate = Util.formatDate7(new Date(nowtime));
  167.       article.setArtkey(form.getArtkey());
  168.       article.setBlogsortid(form.getBlogsortid());
  169.       article.setContent(form.getContent());
  170.       article.setFace(form.getFace());
  171.       article.setFaceplace(form.getFaceplace());
  172.       article.setFaceid(us.getUserInfo().getFace());
  173.       article.setIshide(form.getIshide());
  174.       article.setOpencomment(form.getOpencomment());
  175.       article.setPostdate(postdate);
  176.       article.setPostmonth(postmonth);
  177.       article.setPosttime(new Date(nowtime));
  178.       article.setSign(us.getUserInfo().getSign());
  179.       article.setSortid(form.getSortid());
  180.       article.setTitle(form.getTitle());
  181.       article.setUsesign(form.getUsesign());
  182.       Sort sort = this.getSortService().findSortByID_BlogID(form.getSortid(),
  183.           us.getBlog().getId().longValue());
  184.       if (sort == null) {
  185.         article.setSortname("");
  186.       }
  187.       else {
  188.         article.setSortname(sort.getSortname());
  189.       }
  190.       try {
  191.         article = this.getArticleService().saveArticle(article);
  192.       }
  193.       catch (BlogException ex1) {
  194.         errors.add("error.newart.add", new ActionMessage("error.newart.add"));
  195.         saveErrors(servletRequest, errors);
  196.         return actionMapping.findForward("error");
  197.       }
  198.       return actionMapping.findForward("addok");
  199.     }
  200.     if (form.getAction().equalsIgnoreCase("del")) {
  201.       Article article = this.getArticleService().findArticleByID_BlogID(form.getArtid(),
  202.           us.getBlog().getId().longValue());
  203.       if (article == null) {
  204.         errors.add("error.read.getart", new ActionMessage("error.read.getart"));
  205.         saveErrors(servletRequest, errors);
  206.         return actionMapping.findForward("error");
  207.       }
  208.       try {
  209.         this.getArticleService().removeArticle(article);
  210.       }
  211.       catch (BlogException ex2) {
  212.         errors.add("error.newart.del", new ActionMessage("error.newart.del"));
  213.         saveErrors(servletRequest, errors);
  214.         return actionMapping.findForward("error");
  215.       }
  216.       ActionForward f = new ActionForward("/artManage.do", true);
  217.       return f;
  218.     }
  219.     return actionMapping.findForward("error");
  220.   }
  221.   public SortService getSortService() {
  222.     return sortService;
  223.   }
  224.   public ArticleService getArticleService() {
  225.     return articleService;
  226.   }
  227.   public void setSortService(SortService sortService) {
  228.     this.sortService = sortService;
  229.   }
  230.   public void setArticleService(ArticleService articleService) {
  231.     this.articleService = articleService;
  232.   }
  233. }