KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > web > actions > blog > EntryAction


1 /*
2  * $Id: EntryAction.java,v 1.5 2005/01/17 21:36:03 michelson Exp $
3  *
4  * Copyright (c) 2005 j2biz Group, http://www.j2biz.com Koeln / Duesseldorf ,
5  * Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License as published by the Free Software
12  * Foundation; either version 2 of the License, or (at your option) any later
13  * version.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License along with
21  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  * Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.web.actions.blog;
27
28 import java.util.Set JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import javax.servlet.http.HttpSession JavaDoc;
34
35 import org.apache.commons.lang.StringUtils;
36
37 import com.j2biz.blogunity.IConstants;
38 import com.j2biz.blogunity.dao.CommentDAO;
39 import com.j2biz.blogunity.dao.EntryDAO;
40 import com.j2biz.blogunity.dao.UserDAO;
41 import com.j2biz.blogunity.dao.UserpicDAO;
42 import com.j2biz.blogunity.exception.BlogunityException;
43 import com.j2biz.blogunity.i18n.I18N;
44 import com.j2biz.blogunity.i18n.I18NStatusFactory;
45 import com.j2biz.blogunity.pojo.Blog;
46 import com.j2biz.blogunity.pojo.Comment;
47 import com.j2biz.blogunity.pojo.Entry;
48 import com.j2biz.blogunity.pojo.User;
49 import com.j2biz.blogunity.pojo.Userpic;
50 import com.j2biz.blogunity.util.RenderUtils;
51 import com.j2biz.blogunity.web.ActionResultFactory;
52 import com.j2biz.blogunity.web.FormError;
53 import com.j2biz.blogunity.web.FormErrorList;
54 import com.j2biz.blogunity.web.IActionResult;
55 import com.j2biz.blogunity.web.actions.AbstractAction;
56
57 public class EntryAction extends AbstractAction {
58
59     private static final IActionResult BLOG_SINGLE_ENTRY_FORWARD = ActionResultFactory
60             .buildForward("/entryWithCommentListView.vm");
61
62     private static final IActionResult BLOG_SINGLE_ENTRY_COMMENT_FORWARD = ActionResultFactory
63             .buildForward("/entryWithCommentFormView.vm");
64
65     private EntryDAO entryDAO;
66     
67     private Blog blog;
68
69     private String JavaDoc year;
70
71     private String JavaDoc month;
72
73     private String JavaDoc day;
74
75     private String JavaDoc entry;
76
77     public EntryAction(Blog blog, String JavaDoc year, String JavaDoc month, String JavaDoc day, String JavaDoc entry) {
78         this.blog = blog;
79         this.year = year;
80         this.month = month;
81         this.day = day;
82         this.entry = entry;
83     }
84
85     /*
86      * (non-Javadoc)
87      *
88      * @see com.j2biz.blogunity.web.actions.AbstractAction#execute(javax.servlet.http.HttpServletRequest,
89      * javax.servlet.http.HttpServletResponse)
90      */

91     public IActionResult execute(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
92             throws BlogunityException {
93
94         IActionResult forward;
95         
96         entryDAO = new EntryDAO();
97
98         String JavaDoc mode = request.getParameter("mode");
99         if (StringUtils.isNotEmpty(mode) && mode.equals("comment")) {
100             // single blog entry commenting requested
101
forward = handleSingleEntryComment(request, response);
102         } else if (StringUtils.isNotEmpty(mode) && mode.equals("cpost")) {
103             // post comment requested
104
forward = handleSinglePostComment(request, response);
105         } else {
106             // single blog entry requested
107
forward = handleSingleEntry(request, response);
108         }
109         return forward;
110     }
111
112     private IActionResult handleSingleEntry(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
113             throws NumberFormatException JavaDoc, BlogunityException {
114
115         Entry _entry = entryDAO.getEntryByIDorAliasname(entry, blog.getUrlName());
116         if (_entry == null) { throw new BlogunityException(I18NStatusFactory.create(
117                 I18N.ERRORS.NOT_FOUND, entry)); }
118
119         request.setAttribute(IConstants.Request.BLOG_ENTRY, _entry);
120
121         return BLOG_SINGLE_ENTRY_FORWARD;
122
123     }
124
125     private IActionResult handleSingleEntryComment(HttpServletRequest JavaDoc request,
126             HttpServletResponse JavaDoc response) throws BlogunityException {
127
128         Entry _entry = entryDAO.getEntryByIDorAliasname(entry, blog.getUrlName());
129
130         if (_entry == null) { throw new BlogunityException(I18NStatusFactory.create(
131                 I18N.ERRORS.NOT_FOUND, entry)); }
132
133         // check, if this comment is answer to another one
134
String JavaDoc commentId = request.getParameter("commentId");
135         if (StringUtils.isNotEmpty(commentId)) {
136             request.setAttribute("commentId", commentId);
137         }
138
139         request.setAttribute(IConstants.Request.BLOG_ENTRY, _entry);
140
141         User user = getUserFromSession(request);
142
143         if (user != null) {
144             User u = ( new UserDAO()).getUserByID(user.getId());
145             Set JavaDoc pics = u.getUserpics();
146             request.setAttribute("userpics", pics);
147         }
148
149         return BLOG_SINGLE_ENTRY_COMMENT_FORWARD;
150
151     }
152
153     private IActionResult handleSinglePostComment(HttpServletRequest JavaDoc request,
154             HttpServletResponse JavaDoc response) throws BlogunityException {
155
156         Entry _entry = entryDAO.getEntryByIDorAliasname(entry, blog.getUrlName());
157
158         if (_entry == null) { throw new BlogunityException(I18NStatusFactory.create(
159                 I18N.ERRORS.NOT_FOUND, entry)); }
160
161         String JavaDoc title = request.getParameter("title");
162         String JavaDoc rawBody = request.getParameter("comment");
163
164         Comment comment = new Comment();
165         comment.setRawBody(rawBody);
166         comment.setRawTitle(title);
167         comment.setBody(RenderUtils.renderText(rawBody));
168         comment.setTitle(RenderUtils.renderText(title));
169
170         User user = getUserFromSession(request);
171         if (user != null) {
172             comment.setAuthor(user);
173             String JavaDoc userpicId = request.getParameter("userpicId");
174
175             if (StringUtils.isEmpty(userpicId)) {
176                 comment.setUserpic(null);
177             } else {
178                 // get id
179
try {
180                     StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(userpicId, "|");
181                     String JavaDoc _id = t.nextToken();
182                     Userpic pic = ( new UserpicDAO()).getUserpicByID(Long.parseLong(_id));
183                     comment.setUserpic(pic);
184                 } catch (NumberFormatException JavaDoc e1) {
185                     comment.setUserpic(null);
186                 }
187             }
188
189         } else {
190             comment.setAuthor(null);
191             comment.setUserpic(null);
192         }
193         comment.setCommentedEntry(_entry);
194
195         CommentDAO commentDAO = new CommentDAO();
196         
197         String JavaDoc commentId = request.getParameter("commentId");
198         if (StringUtils.isNotEmpty(commentId)) {
199             try {
200                 Comment repliedComment = commentDAO.getCommentByID(Long.parseLong(commentId));
201                 comment.setRepliedComment(repliedComment);
202             } catch (Exception JavaDoc e) {
203                 comment.setRepliedComment(null);
204             }
205         } else {
206             comment.setRepliedComment(null);
207         }
208
209         request.setAttribute("comment", comment);
210         // request.setAttribute(IConstants.Request.BLOG_ENTRY, entry);
211
FormErrorList errors = new FormErrorList();
212         if (StringUtils.isEmpty(title)) {
213             errors.add(new FormError("title", "Comment title is empty!"));
214         }
215
216         // return, if errors exists
217
if (errors.size() > 0) {
218             if (StringUtils.isNotEmpty(commentId)) {
219                 request.setAttribute("commentId", commentId);
220             }
221             request.setAttribute("errors", errors);
222             return handleSingleEntryComment(request, response);
223         }
224
225         commentDAO.createComment(comment);
226
227         request.setAttribute(IConstants.Request.BLOG_ENTRY, _entry);
228
229         String JavaDoc str = "/blogs/" + _entry.getBlog().getUrlName() + _entry.getPermalink()
230                 + "#commentList";
231         return ActionResultFactory.buildRedirect(str);
232
233     }
234
235     private User getUserFromSession(HttpServletRequest JavaDoc request) {
236         User user = null;
237         HttpSession JavaDoc session = request.getSession(false);
238         if (session != null) user = (User) session.getAttribute(IConstants.Session.USER);
239
240         return user;
241     }
242
243 }
Popular Tags