1 25 26 package com.j2biz.blogunity.web.actions.blog; 27 28 import java.util.Set ; 29 import java.util.StringTokenizer ; 30 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 import javax.servlet.http.HttpSession ; 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 year; 70 71 private String month; 72 73 private String day; 74 75 private String entry; 76 77 public EntryAction(Blog blog, String year, String month, String day, String entry) { 78 this.blog = blog; 79 this.year = year; 80 this.month = month; 81 this.day = day; 82 this.entry = entry; 83 } 84 85 91 public IActionResult execute(HttpServletRequest request, HttpServletResponse response) 92 throws BlogunityException { 93 94 IActionResult forward; 95 96 entryDAO = new EntryDAO(); 97 98 String mode = request.getParameter("mode"); 99 if (StringUtils.isNotEmpty(mode) && mode.equals("comment")) { 100 forward = handleSingleEntryComment(request, response); 102 } else if (StringUtils.isNotEmpty(mode) && mode.equals("cpost")) { 103 forward = handleSinglePostComment(request, response); 105 } else { 106 forward = handleSingleEntry(request, response); 108 } 109 return forward; 110 } 111 112 private IActionResult handleSingleEntry(HttpServletRequest request, HttpServletResponse response) 113 throws NumberFormatException , 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 request, 126 HttpServletResponse 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 String 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 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 request, 154 HttpServletResponse 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 title = request.getParameter("title"); 162 String 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 userpicId = request.getParameter("userpicId"); 174 175 if (StringUtils.isEmpty(userpicId)) { 176 comment.setUserpic(null); 177 } else { 178 try { 180 StringTokenizer t = new StringTokenizer (userpicId, "|"); 181 String _id = t.nextToken(); 182 Userpic pic = ( new UserpicDAO()).getUserpicByID(Long.parseLong(_id)); 183 comment.setUserpic(pic); 184 } catch (NumberFormatException 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 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 e) { 203 comment.setRepliedComment(null); 204 } 205 } else { 206 comment.setRepliedComment(null); 207 } 208 209 request.setAttribute("comment", comment); 210 FormErrorList errors = new FormErrorList(); 212 if (StringUtils.isEmpty(title)) { 213 errors.add(new FormError("title", "Comment title is empty!")); 214 } 215 216 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 str = "/blogs/" + _entry.getBlog().getUrlName() + _entry.getPermalink() 230 + "#commentList"; 231 return ActionResultFactory.buildRedirect(str); 232 233 } 234 235 private User getUserFromSession(HttpServletRequest request) { 236 User user = null; 237 HttpSession session = request.getSession(false); 238 if (session != null) user = (User) session.getAttribute(IConstants.Session.USER); 239 240 return user; 241 } 242 243 } | Popular Tags |