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

Jsp/Servlet

开发平台:

Java

  1. package com.opensource.blog.web.action;
  2. import javax.servlet.http.*;
  3. import org.apache.struts.action.*;
  4. import com.opensource.blog.comm.*;
  5. import com.opensource.blog.exception.*;
  6. import com.opensource.blog.model.Links;
  7. import com.opensource.blog.service.*;
  8. import com.opensource.blog.web.form.*;
  9. import com.opensource.blog.web.servlet.*;
  10. public class AddLinks
  11.     extends BaseAction {
  12.   private LinksService linksService;
  13.   public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm,
  14.                                HttpServletRequest servletRequest,
  15.                                HttpServletResponse servletResponse) {
  16.     ActionMessages errors = new ActionMessages();
  17.     AddLinksForm form = (AddLinksForm) actionForm;
  18.     UserSession us = this.getUserSession(servletRequest);
  19.     if (form.getAction().equalsIgnoreCase("show")) {
  20.       if (form.getLinktype() == 0) {
  21.         form.setAction("addtext");
  22.       }
  23.       if (form.getLinktype() == 1) {
  24.         form.setAction("addpic");
  25.       }
  26.       form.setId(0);
  27.       form.setLinkimgsrc("http://");
  28.       form.setLinktext("");
  29.       form.setLinkurl("http://");
  30.       return actionMapping.findForward("addlinks");
  31.     }
  32.     if (form.getAction().equalsIgnoreCase("addtext")) {
  33.       if (this.getLinksService().getLinksCountByBlogID_Type(us.getBlog().getId().longValue(),
  34.           Constant.LINK_TEXT) >= Constant.LINK_MAX_NUM) {
  35.         errors.add("error.addlinks.linksnummax",
  36.                    new ActionMessage("error.addlinks.linksnummax",
  37.                                      String.valueOf(Constant.LINK_MAX_NUM)));
  38.         saveErrors(servletRequest, errors);
  39.         return actionMapping.findForward("error");
  40.       }
  41.       Links links = new Links();
  42.       links.setBlogid(us.getBlog().getId().longValue());
  43.       links.setLinkimgsrc("");
  44.       links.setLinktext(form.getLinktext());
  45.       links.setLinktype(form.getLinktype());
  46.       links.setLinkurl(form.getLinkurl());
  47.       try {
  48.         links = this.getLinksService().saveLinks(links);
  49.       }
  50.       catch (BlogException ex) {
  51.         errors.add("error.addlinks.addlink",
  52.                    new ActionMessage("error.addlinks.addlink"));
  53.         saveErrors(servletRequest, errors);
  54.         return actionMapping.findForward("error");
  55.       }
  56.       ActionForward f = new ActionForward("/blogInfoHigh.do", true);
  57.       return f;
  58.     }
  59.     if (form.getAction().equalsIgnoreCase("addpic")) {
  60.       if (this.getLinksService().getLinksCountByBlogID_Type(us.getBlog().getId().longValue(),
  61.           Constant.LINK_LOGO) > Constant.LINK_MAX_NUM) {
  62.         errors.add("error.addlinks.linksnummax",
  63.                    new ActionMessage("error.addlinks.linksnummax",
  64.                                      String.valueOf(Constant.LINK_MAX_NUM)));
  65.         saveErrors(servletRequest, errors);
  66.         return actionMapping.findForward("error");
  67.       }
  68.       Links links = new Links();
  69.       links.setBlogid(us.getBlog().getId().longValue());
  70.       links.setLinkimgsrc(form.getLinkimgsrc());
  71.       links.setLinktext(form.getLinktext());
  72.       links.setLinktype(form.getLinktype());
  73.       links.setLinkurl(form.getLinkurl());
  74.       try {
  75.         links = this.getLinksService().saveLinks(links);
  76.       }
  77.       catch (BlogException ex1) {
  78.         errors.add("error.addlinks.addlink", new ActionMessage("error.addlinks.addlink"));
  79.         saveErrors(servletRequest, errors);
  80.         return actionMapping.findForward("error");
  81.       }
  82.       ActionForward f = new ActionForward("/blogInfoHigh.do", true);
  83.       return f;
  84.     }
  85.     if (form.getAction().equalsIgnoreCase("edit")) {
  86.       Links links = this.getLinksService().findLinksByID_BlogID(form.getId(),
  87.           us.getBlog().getId().longValue());
  88.       if (links == null) {
  89.         errors.add("error.addlinks.getlink", new ActionMessage("error.addlinks.getlink"));
  90.         saveErrors(servletRequest, errors);
  91.         return actionMapping.findForward("error");
  92.       }
  93.       form.setAction("editdo");
  94.       form.setId(links.getId().longValue());
  95.       form.setLinkimgsrc(links.getLinkimgsrc());
  96.       form.setLinktext(links.getLinktext());
  97.       form.setLinktype(links.getLinktype());
  98.       form.setLinkurl(links.getLinkurl());
  99.       return actionMapping.findForward("addlinks");
  100.     }
  101.     if (form.getAction().equalsIgnoreCase("editdo")) {
  102.       Links links = this.getLinksService().findLinksByID_BlogID(form.getId(),
  103.           us.getBlog().getId().longValue());
  104.       if (links == null) {
  105.         errors.add("error.addlinks.getlink", new ActionMessage("error.addlinks.getlink"));
  106.         saveErrors(servletRequest, errors);
  107.         return actionMapping.findForward("error");
  108.       }
  109.       links.setLinktext(form.getLinktext());
  110.       links.setLinktype(form.getLinktype());
  111.       links.setLinkurl(form.getLinkurl());
  112.       if (form.getLinktype() == 1) {
  113.         links.setLinkimgsrc(form.getLinkimgsrc());
  114.       }
  115.       try {
  116.         links = this.getLinksService().saveLinks(links);
  117.       }
  118.       catch (BlogException ex2) {
  119.         errors.add("error.addlinks.uplink", new ActionMessage("error.addlinks.uplink"));
  120.         saveErrors(servletRequest, errors);
  121.         return actionMapping.findForward("error");
  122.       }
  123.       ActionForward f = new ActionForward("/blogInfoHigh.do", true);
  124.       return f;
  125.     }
  126.     if (form.getAction().equalsIgnoreCase("del")) {
  127.       Links links = this.getLinksService().findLinksByID_BlogID(form.getId(),
  128.           us.getBlog().getId().longValue());
  129.       if (links == null) {
  130.         errors.add("error.addlinks.getlink", new ActionMessage("error.addlinks.getlink"));
  131.         saveErrors(servletRequest, errors);
  132.         return actionMapping.findForward("error");
  133.       }
  134.       try {
  135.         this.getLinksService().removeLinks(links);
  136.       }
  137.       catch (BlogException ex3) {
  138.         errors.add("error.addlinks.getlink", new ActionMessage("error.addlinks.getlink"));
  139.         saveErrors(servletRequest, errors);
  140.         return actionMapping.findForward("error");
  141.       }
  142.       ActionForward f = new ActionForward("/blogInfoHigh.do", true);
  143.       return f;
  144.     }
  145.     return actionMapping.findForward("error");
  146.   }
  147.   public LinksService getLinksService() {
  148.     return linksService;
  149.   }
  150.   public void setLinksService(LinksService linksService) {
  151.     this.linksService = linksService;
  152.   }
  153. }