1 18 package org.apache.roller.ui.authoring.struts.actions; 19 20 import java.io.IOException ; 21 import java.net.MalformedURLException ; 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Date ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import javax.servlet.ServletException ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.struts.action.ActionErrors; 33 import org.apache.struts.action.ActionForm; 34 import org.apache.struts.action.ActionForward; 35 import org.apache.struts.action.ActionMapping; 36 import org.apache.struts.action.ActionMessage; 37 import org.apache.struts.action.ActionMessages; 38 import org.apache.struts.actions.DispatchAction; 39 import org.apache.struts.util.RequestUtils; 40 import org.apache.roller.RollerException; 41 import org.apache.roller.config.RollerRuntimeConfig; 42 import org.apache.roller.model.Roller; 43 import org.apache.roller.model.RollerFactory; 44 import org.apache.roller.model.WeblogManager; 45 import org.apache.roller.pojos.CommentData; 46 import org.apache.roller.pojos.WeblogEntryData; 47 import org.apache.roller.ui.core.BasePageModel; 48 import org.apache.roller.ui.core.RollerContext; 49 import org.apache.roller.ui.core.RollerRequest; 50 import org.apache.roller.ui.core.RollerSession; 51 import org.apache.roller.util.cache.CacheManager; 52 import org.apache.roller.ui.rendering.servlets.CommentServlet; 53 import org.apache.roller.ui.authoring.struts.formbeans.CommentManagementForm; 54 import org.apache.roller.util.Utilities; 55 56 74 public final class CommentManagementAction extends DispatchAction { 75 76 private static Log logger = 77 LogFactory.getFactory().getInstance(CommentManagementAction.class); 78 79 public ActionForward query( 80 ActionMapping mapping, 81 ActionForm actionForm, 82 HttpServletRequest request, 83 HttpServletResponse response) 84 throws IOException , ServletException , RollerException { 85 86 CommentManagementForm queryForm = (CommentManagementForm)actionForm; 87 RollerRequest rreq = RollerRequest.getRollerRequest(request); 88 RollerSession rses = RollerSession.getRollerSession(request); 89 90 if (rreq.getWeblogEntry() != null) { 91 queryForm.setEntryid(rreq.getWeblogEntry().getId()); 92 queryForm.setWeblog(rreq.getWeblogEntry().getWebsite().getHandle()); 93 } 94 else if (rreq.getWebsite() != null) { 95 queryForm.setWeblog(rreq.getWebsite().getHandle()); 96 } 97 request.setAttribute("model", new CommentManagementPageModel( 98 "commentManagement.title", request, response, mapping, queryForm)); 99 if (request.getAttribute("commentManagementForm") == null) { 100 request.setAttribute("commentManagementForm", actionForm); 101 } 102 103 if (rreq.getWebsite() != null && rses.isUserAuthorized(rreq.getWebsite())) { 105 return mapping.findForward("commentManagement.page"); 106 } 107 else if (rses.isGlobalAdminUser()) { 109 return mapping.findForward("commentManagementGlobal.page"); 110 } 111 else { 112 return mapping.findForward("access-denied"); 113 } 114 } 115 116 public ActionForward update( 117 ActionMapping mapping, 118 ActionForm actionForm, 119 HttpServletRequest request, 120 HttpServletResponse response) 121 throws IOException , ServletException , RollerException { 122 123 CommentManagementForm queryForm = (CommentManagementForm)actionForm; 124 RollerRequest rreq = RollerRequest.getRollerRequest(request); 125 if (rreq.getWeblogEntry() != null) { 126 queryForm.setEntryid(rreq.getWeblogEntry().getId()); 127 queryForm.setWeblog(rreq.getWeblogEntry().getWebsite().getHandle()); 128 } 129 else if (rreq.getWebsite() != null) { 130 queryForm.setWeblog(rreq.getWebsite().getHandle()); 131 } 132 else { 133 RollerSession rses = RollerSession.getRollerSession(request); 135 if (!rses.isGlobalAdminUser()) { 136 return mapping.findForward("access-denied"); 137 } 138 } 139 RollerSession rses = RollerSession.getRollerSession(request); 140 try { 141 if (rses.isGlobalAdminUser() 142 || (rreq.getWebsite()!=null && rses.isUserAuthorizedToAuthor(rreq.getWebsite())) ) { 143 WeblogManager mgr= RollerFactory.getRoller().getWeblogManager(); 144 145 CommentData deleteComment = null; 147 String [] deleteIds = queryForm.getDeleteComments(); 148 List deletedList = Arrays.asList(deleteIds); 149 if (deleteIds != null && deleteIds.length > 0) { 150 for(int j=0; j < deleteIds.length; j++) { 151 deleteComment = mgr.getComment(deleteIds[j]); 152 153 mgr.removeComment(deleteComment); 154 } 155 } 156 157 List approvedComments = new ArrayList (); 160 161 String [] ids = Utilities.stringToStringArray(queryForm.getIds(),","); 163 List flushList = new ArrayList (); 164 for (int i=0; i<ids.length; i++) { 165 if (deletedList.contains(ids[i])) continue; 166 CommentData comment = mgr.getComment(ids[i]); 167 168 List spamIds = Arrays.asList(queryForm.getSpamComments()); 170 if (spamIds.contains(ids[i])) { 171 comment.setSpam(Boolean.TRUE); 172 } else { 173 comment.setSpam(Boolean.FALSE); 174 } 175 176 if (rreq.getWebsite() != null) { 181 182 if (comment.getPending() != null && comment.getPending().booleanValue()) { 184 comment.setPending(Boolean.FALSE); 185 approvedComments.add(comment); 186 } 187 188 List approvedIds = 190 Arrays.asList(queryForm.getApprovedComments()); 191 if (approvedIds.contains(ids[i])) { 192 comment.setApproved(Boolean.TRUE); 193 194 } else { 195 comment.setApproved(Boolean.FALSE); 196 } 197 } 198 mgr.saveComment(comment); 199 flushList.add(comment); 200 } 201 202 RollerFactory.getRoller().flush(); 203 for (Iterator comments=flushList.iterator(); comments.hasNext();) { 204 CacheManager.invalidate((CommentData)comments.next()); 205 } 206 207 sendCommentNotifications(request, approvedComments); 208 209 ActionMessages msgs = new ActionMessages(); 210 msgs.add(ActionMessages.GLOBAL_MESSAGE, 211 new ActionMessage("commentManagement.updateSuccess")); 212 saveMessages(request, msgs); 213 } 214 } catch (Exception e) { 215 ActionMessages errors = new ActionMessages(); 216 errors.add(ActionErrors.GLOBAL_MESSAGE, 217 new ActionMessage("commentManagement.updateError",e.toString())); 218 saveErrors(request, errors); 219 logger.error("ERROR updating comments", e); 220 } 221 CommentManagementPageModel model = new CommentManagementPageModel( 222 "commentManagement.title", request, response, mapping, queryForm); 223 request.setAttribute("model", model); 224 if (request.getAttribute("commentManagementForm") == null) { 225 request.setAttribute("commentManagementForm", actionForm); 226 } 227 228 if (rreq.getWebsite() != null) { 229 return mapping.findForward("commentManagement.page"); 230 } 231 return mapping.findForward("commentManagementGlobal.page"); 232 } 233 234 private void sendCommentNotifications( 235 HttpServletRequest req, List comments) throws RollerException { 236 237 RollerContext rc = RollerContext.getRollerContext(); 238 String rootURL = RollerRuntimeConfig.getAbsoluteContextURL(); 239 try { 240 if (rootURL == null || rootURL.trim().length()==0) { 241 rootURL = RequestUtils.serverURL(req) + req.getContextPath(); 242 } 243 } catch (MalformedURLException e) { 244 logger.error("ERROR: determining URL of site"); 245 return; 246 } 247 248 Iterator iter = comments.iterator(); 249 while (iter.hasNext()) { 250 CommentData comment = (CommentData)iter.next(); 251 252 CommentServlet.sendEmailNotification(comment, rootURL); 254 255 CommentServlet.sendEmailApprovalNotification(comment, rootURL); 257 } 258 } 259 260 public class CommentManagementPageModel extends BasePageModel { 261 private List comments = new ArrayList (); 262 private WeblogEntryData weblogEntry = null; 263 private CommentManagementForm queryForm = null; 264 private boolean more = false; 265 266 public CommentManagementPageModel( 267 String titleKey, 268 HttpServletRequest request, 269 HttpServletResponse response, 270 ActionMapping mapping, 271 CommentManagementForm queryForm) throws RollerException { 272 273 super(titleKey, request, response, mapping); 274 this.queryForm = queryForm; 275 276 Roller roller = RollerFactory.getRoller(); 277 RollerRequest rreq = RollerRequest.getRollerRequest(request); 278 if (rreq.getWeblogEntry() != null) { 279 website = rreq.getWeblogEntry().getWebsite(); 280 weblogEntry = rreq.getWeblogEntry(); 281 } 282 else if (rreq.getWebsite() != null) { 283 website = rreq.getWebsite(); 284 } 285 WeblogManager blogmgr = roller.getWeblogManager(); 286 287 int offset = queryForm.getOffset(); 288 comments = blogmgr.getComments( 289 website, 290 weblogEntry, 291 queryForm.getSearchString(), 292 queryForm.getStartDate(request.getLocale()), 293 queryForm.getEndDate(request.getLocale()), 294 queryForm.getPending(), 295 queryForm.getApproved(), 296 queryForm.getSpam(), 297 true, queryForm.getOffset(), 299 queryForm.getCount() + 1); 300 if (comments.size() > queryForm.getCount()) { 301 more = true; 302 comments.remove(comments.size()-1); 303 } 304 this.queryForm.loadCheckboxes(comments); 305 } 306 307 public List getComments() { 308 return comments; 309 } 310 311 public int getCommentCount() { 312 int ret = comments.size(); 313 return ret > queryForm.getCount() ? queryForm.getCount() : ret; 314 } 315 316 321 public int getPendingCommentCount() { 322 int count = 0; 323 if (getWebsite() != null) { 324 for (Iterator iter = comments.iterator(); iter.hasNext();) { 325 CommentData cd = (CommentData)iter.next(); 326 if (cd.getPending().booleanValue()) count++; 327 } 328 } 329 return count; 330 } 331 332 public Date getEarliestDate() { 333 Date date = null; 334 if (comments.size() > 0) { 335 CommentData earliest = (CommentData)comments.get(comments.size()-1); 336 date = earliest.getPostTime(); 337 } 338 return date; 339 } 340 341 public Date getLatestDate() { 342 Date date = null; 343 if (comments.size() > 0) { 344 CommentData latest = (CommentData)comments.get(0); 345 date = latest.getPostTime(); 346 } 347 return date; 348 } 349 350 public WeblogEntryData getWeblogEntry() { 351 return weblogEntry; 352 } 353 354 public String getLink() { 355 return getQueryLink() + "&offset=" + queryForm.getOffset(); 356 } 357 358 public String getNextLink() { 359 if (more) { 360 int offset = queryForm.getOffset() + queryForm.getCount(); 361 offset = (offset < 0) ? 0 : offset; 362 return getQueryLink() + "&offset=" + offset; 363 } else { 364 return null; 365 } 366 } 367 368 public String getPrevLink() { 369 if (queryForm.getOffset() > 0) { 370 int offset = queryForm.getOffset() - queryForm.getCount(); 371 offset = (offset < 0) ? 0 : offset; 372 return getQueryLink() + "&offset=" + offset; 373 } else { 374 return null; 375 } 376 } 377 378 private String getQueryLink() { 379 StringBuffer sb = new StringBuffer (); 380 sb.append(request.getContextPath()); 381 if (getWebsite() != null) { 382 sb.append("/roller-ui/authoring/commentManagement.do"); sb.append("?method=query"); 384 sb.append("&weblog="); 385 sb.append(getWebsite().getHandle()); 386 } else { 387 sb.append("/roller-ui/admin/commentManagement.do"); sb.append("?method=query"); 389 } 390 sb.append("&count="); 391 sb.append(queryForm.getCount()); 392 return sb.toString(); 393 } 394 } 395 } 396 397 398 399 | Popular Tags |