MyArticle.java
资源名称:Myblog.rar [点击查看]
上传用户:wlfwy2004
上传日期:2016-12-12
资源大小:33978k
文件大小:10k
源码类别:
Jsp/Servlet
开发平台:
Java
- package com.opensource.blog.web.action;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.Locale;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.apache.struts.action.ActionMessage;
- import org.apache.struts.action.ActionMessages;
- import org.apache.struts.util.LabelValueBean;
- import org.apache.struts.util.MessageResources;
- import com.laoer.comm.util.Util;
- import com.opensource.blog.exception.BlogException;
- import com.opensource.blog.model.Article;
- import com.opensource.blog.model.Blog;
- import com.opensource.blog.model.Sort;
- import com.opensource.blog.service.ArticleService;
- import com.opensource.blog.service.SortService;
- import com.opensource.blog.web.form.ArticleForm;
- import com.opensource.blog.web.servlet.UserSession;
- public class MyArticle
- extends BaseAction {
- private SortService sortService;
- private ArticleService articleService;
- public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm,
- HttpServletRequest servletRequest,
- HttpServletResponse servletResponse) {
- ActionMessages errors = new ActionMessages();
- ArticleForm form = (ArticleForm) actionForm;
- UserSession us = this.getUserSession(servletRequest);
- MessageResources messages = getResources(servletRequest);
- Locale locale = getLocale(servletRequest);
- List sortSelList = new ArrayList();
- List sortlist = this.getSortService().findSortsByBlogID(us.getBlog().getId().longValue());
- sortSelList.add(new LabelValueBean(messages.getMessage(locale, "blog.select"), "0"));
- for (int i = 0; i < sortlist.size(); i++) {
- Sort sort = (Sort) sortlist.get(i);
- sortSelList.add(new LabelValueBean(sort.getSortname(), String.valueOf(sort.getId())));
- }
- servletRequest.setAttribute("sortSelList", sortSelList);
- if (form.getAction().equalsIgnoreCase("new")) {
- form.setAction("add");
- form.setArtid(0);
- form.setArtkey("");
- form.setBlogsortid(us.getBlog().getSort());
- form.setContent("");
- form.setContentsize(0);
- form.setDay(0);
- form.setFace(1);
- form.setFaceplace(0);
- form.setHour(0);
- form.setIshide(1);
- form.setMin(0);
- form.setOpencomment(0);
- form.setPostmonth(0);
- form.setSec(0);
- form.setSortid(0);
- form.setTitle("");
- form.setUsesign(1);
- form.setYear(0);
- return actionMapping.findForward("article");
- }
- if (form.getAction().equalsIgnoreCase("add")) {
- Article article = new Article();
- long nowtime = Util.getLongTime();
- int postmonth = Util.getMonth(nowtime);
- String postdate = "";
- if (form.getYear() != 0 && form.getPostmonth() != 0 && form.getDay() != 0) {
- nowtime = Util.Time2Long(form.getYear(), form.getPostmonth(),
- form.getDay(), form.getHour(), form.getMin(),
- form.getSec());
- postmonth = form.getPostmonth();
- }
- postdate = Util.formatDate7(new Date(nowtime));
- article.setArtkey(form.getArtkey());
- article.setBlogid(us.getBlog().getId().longValue());
- article.setBlogname(us.getBlog().getBlogname());
- article.setBlogsortid(form.getBlogsortid());
- article.setCommentnum(0);
- article.setContent(form.getContent());
- article.setUserid(us.getUserInfo().getId().longValue());
- article.setUsername(us.getUserInfo().getUsername());
- article.setTrackback("");
- article.setTrackbacknum(0);
- article.setContentsize(form.getContentsize());
- //article.setFace(form.getFace());
- article.setFace(1);
- article.setFaceplace(form.getFaceplace());
- article.setFaceid(us.getUserInfo().getFace());
- article.setIshide(form.getIshide());
- article.setOpencomment(form.getOpencomment());
- article.setPostdate(postdate);
- article.setPostmonth(postmonth);
- article.setPosttime(new Date());
- article.setSign(us.getUserInfo().getSign());
- article.setSortid(form.getSortid());
- article.setTitle(form.getTitle());
- article.setUsesign(form.getUsesign());
- Sort sort = this.getSortService().findSortByID_BlogID(form.getSortid(),
- us.getBlog().getId().longValue());
- if (sort == null) {
- article.setSortname("");
- }
- else {
- article.setSortname(sort.getSortname());
- }
- try {
- Object[] o = this.getArticleService().createArticle(article, us.getBlog());
- this.saveUserSession(servletRequest, (Blog) o[1]);
- return actionMapping.findForward("addok");
- }
- catch (BlogException ex) {
- errors.add("error.newart.add", new ActionMessage("error.newart.add"));
- saveErrors(servletRequest, errors);
- return actionMapping.findForward("error");
- }
- }
- if (form.getAction().equalsIgnoreCase("edit")) {
- Article article = this.getArticleService().findArticleByID_BlogID(form.getArtid(),
- us.getBlog().getId().longValue());
- if (article == null) {
- errors.add("error.read.getart", new ActionMessage("error.read.getart"));
- saveErrors(servletRequest, errors);
- return actionMapping.findForward("error");
- }
- form.setAction("editdo");
- form.setArtkey(article.getArtkey());
- form.setBlogsortid(article.getBlogsortid());
- form.setContent(article.getContent());
- form.setContentsize(article.getContentsize());
- form.setDay(Util.getDay(article.getPosttime()));
- form.setFace(article.getFace());
- form.setFaceplace(article.getFaceplace());
- form.setHour(Util.getHour(article.getPosttime()));
- form.setIshide(article.getIshide());
- form.setMin(Util.getMinute(article.getPosttime()));
- form.setOpencomment(article.getOpencomment());
- form.setPostmonth(article.getPostmonth());
- form.setSec(Util.getSecond(article.getPosttime()));
- form.setSortid(article.getSortid());
- form.setTitle(article.getTitle());
- form.setUsesign(article.getUsesign());
- form.setYear(Util.getYear(article.getPosttime()));
- return actionMapping.findForward("article");
- }
- if (form.getAction().equalsIgnoreCase("editdo")) {
- Article article = this.getArticleService().findArticleByID_BlogID(form.getArtid(),
- us.getBlog().getId().longValue());
- if (article == null) {
- errors.add("error.read.getart", new ActionMessage("error.read.getart"));
- saveErrors(servletRequest, errors);
- return actionMapping.findForward("error");
- }
- long nowtime = Util.getLongTime();
- int postmonth = Util.getMonth(nowtime);
- String postdate = "";
- if (form.getYear() != 0 && form.getPostmonth() != 0 && form.getDay() != 0) {
- nowtime = Util.Time2Long(form.getYear(), form.getPostmonth(),
- form.getDay(), form.getHour(), form.getMin(),
- form.getSec());
- postmonth = form.getPostmonth();
- }
- postdate = Util.formatDate7(new Date(nowtime));
- article.setArtkey(form.getArtkey());
- article.setBlogsortid(form.getBlogsortid());
- article.setContent(form.getContent());
- article.setFace(form.getFace());
- article.setFaceplace(form.getFaceplace());
- article.setFaceid(us.getUserInfo().getFace());
- article.setIshide(form.getIshide());
- article.setOpencomment(form.getOpencomment());
- article.setPostdate(postdate);
- article.setPostmonth(postmonth);
- article.setPosttime(new Date(nowtime));
- article.setSign(us.getUserInfo().getSign());
- article.setSortid(form.getSortid());
- article.setTitle(form.getTitle());
- article.setUsesign(form.getUsesign());
- Sort sort = this.getSortService().findSortByID_BlogID(form.getSortid(),
- us.getBlog().getId().longValue());
- if (sort == null) {
- article.setSortname("");
- }
- else {
- article.setSortname(sort.getSortname());
- }
- try {
- article = this.getArticleService().saveArticle(article);
- }
- catch (BlogException ex1) {
- errors.add("error.newart.add", new ActionMessage("error.newart.add"));
- saveErrors(servletRequest, errors);
- return actionMapping.findForward("error");
- }
- return actionMapping.findForward("addok");
- }
- if (form.getAction().equalsIgnoreCase("del")) {
- Article article = this.getArticleService().findArticleByID_BlogID(form.getArtid(),
- us.getBlog().getId().longValue());
- if (article == null) {
- errors.add("error.read.getart", new ActionMessage("error.read.getart"));
- saveErrors(servletRequest, errors);
- return actionMapping.findForward("error");
- }
- try {
- this.getArticleService().removeArticle(article);
- }
- catch (BlogException ex2) {
- errors.add("error.newart.del", new ActionMessage("error.newart.del"));
- saveErrors(servletRequest, errors);
- return actionMapping.findForward("error");
- }
- ActionForward f = new ActionForward("/artManage.do", true);
- return f;
- }
- return actionMapping.findForward("error");
- }
- public SortService getSortService() {
- return sortService;
- }
- public ArticleService getArticleService() {
- return articleService;
- }
- public void setSortService(SortService sortService) {
- this.sortService = sortService;
- }
- public void setArticleService(ArticleService articleService) {
- this.articleService = articleService;
- }
- }