001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.plugin.admin;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.blojsom.blog.*;
035: import org.blojsom.event.EventBroadcaster;
036: import org.blojsom.fetcher.Fetcher;
037: import org.blojsom.fetcher.FetcherException;
038: import org.blojsom.plugin.PluginException;
039: import org.blojsom.plugin.comment.event.CommentApprovedEvent;
040: import org.blojsom.plugin.comment.event.CommentDeletedEvent;
041: import org.blojsom.plugin.comment.event.CommentMarkedSpamEvent;
042: import org.blojsom.plugin.common.ResponseConstants;
043: import org.blojsom.plugin.pingback.event.PingbackApprovedEvent;
044: import org.blojsom.plugin.pingback.event.PingbackDeletedEvent;
045: import org.blojsom.plugin.pingback.event.PingbackMarkedSpamEvent;
046: import org.blojsom.plugin.trackback.event.TrackbackApprovedEvent;
047: import org.blojsom.plugin.trackback.event.TrackbackDeletedEvent;
048: import org.blojsom.plugin.trackback.event.TrackbackMarkedSpamEvent;
049: import org.blojsom.util.BlojsomConstants;
050: import org.blojsom.util.BlojsomUtils;
051:
052: import javax.servlet.http.HttpServletRequest;
053: import javax.servlet.http.HttpServletResponse;
054: import java.util.Date;
055: import java.util.Map;
056:
057: /**
058: * Bulk Response Management plugin
059: *
060: * @author David Czarnecki
061: * @since blojsom 3.0
062: * @version $Id: BulkResponseManagement.java,v 1.6 2007/01/17 02:35:04 czarneckid Exp $
063: */
064: public class BulkResponseManagement extends BaseAdminPlugin {
065:
066: private Log _logger = LogFactory
067: .getLog(BulkResponseManagement.class);
068:
069: private static final String BULK_RESPONSE_MANAGEMENT_PERMISSION = "bulk_response_management_permission";
070: private static final String BLOJSOM_USER_OBJECT = "BLOJSOM_USER_OBJECT";
071:
072: private String[] DEFAULT_RESPONSE_STATUS_LIST = {
073: ResponseConstants.NEW_STATUS, ResponseConstants.SPAM_STATUS };
074:
075: // Localization constants
076: private static final String FAILED_BULK_PERMISSION_KEY = "failed.bulk.permission.text";
077: private static final String SUCCESFUL_BULK_PROCESSING_KEY = "successful.bulk.processing.text";
078:
079: // Pages
080: private static final String BULK_RESPONSE_MANAGEMENT_PAGE = "/org/blojsom/plugin/admin/templates/bulk-response-management";
081:
082: // Actions
083: private static final String BULK_RESPONSE_MANAGEMENT_ACTION = "bulk-response-management";
084:
085: // Context attributes
086: private static final String BULK_RESPONSES = "BULK_RESPONSES";
087:
088: // Form items
089: private static final String DELETE_COMMENTS = "delete_comments";
090: private static final String APPROVE_COMMENTS = "approve_comments";
091: private static final String DELETE_TRACKBACKS = "delete_trackbacks";
092: private static final String APPROVE_TRACKBACKS = "approve_trackbacks";
093: private static final String DELETE_PINGBACKS = "delete_pingbacks";
094: private static final String APPROVE_PINGBACKS = "approve_pingbacks";
095: private static final String MARK_SPAM_COMMENTS = "mark_spam_comments";
096: private static final String MARK_SPAM_TRACKBACKS = "mark_spam_trackbacks";
097: private static final String MARK_SPAM_PINGBACKS = "mark_spam_pingbacks";
098: private static final String QUERY = "query";
099:
100: private Fetcher _fetcher;
101: private EventBroadcaster _eventBroadcaster;
102: private String[] _responseStatusList = DEFAULT_RESPONSE_STATUS_LIST;
103:
104: /**
105: * Create a new instance of the bulk response management plugin.
106: */
107: public BulkResponseManagement() {
108: }
109:
110: /**
111: * Set the {@link Fetcher}
112: *
113: * @param fetcher {@link Fetcher}
114: */
115: public void setFetcher(Fetcher fetcher) {
116: _fetcher = fetcher;
117: }
118:
119: /**
120: * Set the {@link EventBroadcaster}
121: *
122: * @param eventBroadcaster {@link EventBroadcaster}
123: */
124: public void setEventBroadcaster(EventBroadcaster eventBroadcaster) {
125: _eventBroadcaster = eventBroadcaster;
126: }
127:
128: /**
129: * Set the response status codes to search for in bulk management
130: *
131: * @param statusList Status list
132: */
133: public void setResponseStatusList(String[] statusList) {
134: _responseStatusList = statusList;
135: }
136:
137: /**
138: * Process the blog entries
139: *
140: * @param httpServletRequest Request
141: * @param httpServletResponse Response
142: * @param blog {@link Blog} instance
143: * @param context Context
144: * @param entries Blog entries retrieved for the particular request
145: * @return Modified set of blog entries
146: * @throws PluginException If there is an error processing the blog entries
147: */
148: public Entry[] process(HttpServletRequest httpServletRequest,
149: HttpServletResponse httpServletResponse, Blog blog,
150: Map context, Entry[] entries) throws PluginException {
151: if (!authenticateUser(httpServletRequest, httpServletResponse,
152: context, blog)) {
153: httpServletRequest.setAttribute(
154: BlojsomConstants.PAGE_PARAM, ADMIN_LOGIN_PAGE);
155:
156: return entries;
157: }
158:
159: String username = getUsernameFromSession(httpServletRequest,
160: blog);
161: if (!checkPermission(blog, null, username,
162: BULK_RESPONSE_MANAGEMENT_PERMISSION)) {
163: httpServletRequest.setAttribute(
164: BlojsomConstants.PAGE_PARAM,
165: ADMIN_ADMINISTRATION_PAGE);
166: addOperationResultMessage(context, getAdminResource(
167: FAILED_BULK_PERMISSION_KEY,
168: FAILED_BULK_PERMISSION_KEY, blog
169: .getBlogAdministrationLocale()));
170:
171: return entries;
172: }
173:
174: try {
175: context.put(BLOJSOM_USER_OBJECT, _fetcher.loadUser(blog,
176: username));
177: } catch (FetcherException e) {
178: if (_logger.isErrorEnabled()) {
179: _logger.error(e);
180: }
181: }
182:
183: String action = BlojsomUtils.getRequestValue(ACTION_PARAM,
184: httpServletRequest);
185: if (BlojsomUtils.checkNullOrBlank(action)) {
186: if (_logger.isDebugEnabled()) {
187: _logger.debug("User did not request edit action");
188: }
189: httpServletRequest.setAttribute(
190: BlojsomConstants.PAGE_PARAM,
191: ADMIN_ADMINISTRATION_PAGE);
192: } else if (PAGE_ACTION.equals(action)) {
193: if (_logger.isDebugEnabled()) {
194: _logger.debug("User requested bulk response edit page");
195: }
196: } else if (BULK_RESPONSE_MANAGEMENT_ACTION.equals(action)) {
197: if (_logger.isDebugEnabled()) {
198: _logger
199: .debug("User requested bulk response management action");
200: }
201:
202: int commentsApproved = 0;
203: int commentsDeleted = 0;
204: int trackbacksApproved = 0;
205: int trackbacksDeleted = 0;
206: int pingbacksApproved = 0;
207: int pingbacksDeleted = 0;
208:
209: Integer entityID;
210:
211: String[] markspamComments = BlojsomUtils.getRequestValues(
212: MARK_SPAM_COMMENTS, httpServletRequest);
213: for (int i = 0; i < markspamComments.length; i++) {
214: try {
215: String item = markspamComments[i];
216: entityID = Integer.valueOf(item);
217: Comment comment = _fetcher.newComment();
218: comment.setBlogId(blog.getId());
219: comment.setId(entityID);
220:
221: _fetcher.loadComment(blog, comment);
222:
223: CommentMarkedSpamEvent commentMarkedSpamEvent = new CommentMarkedSpamEvent(
224: this , new Date(), comment, blog);
225: _eventBroadcaster
226: .broadcastEvent(commentMarkedSpamEvent);
227:
228: if (_logger.isDebugEnabled()) {
229: _logger
230: .debug("Marked comment as spam for comment ID: "
231: + entityID);
232: }
233: } catch (NumberFormatException e) {
234: } catch (FetcherException e) {
235: if (_logger.isErrorEnabled()) {
236: _logger.error(e);
237: }
238: }
239: }
240:
241: String[] markspamTrackbacks = BlojsomUtils
242: .getRequestValues(MARK_SPAM_TRACKBACKS,
243: httpServletRequest);
244: for (int i = 0; i < markspamTrackbacks.length; i++) {
245: try {
246: String item = markspamTrackbacks[i];
247: entityID = Integer.valueOf(item);
248: Trackback trackback = _fetcher.newTrackback();
249: trackback.setBlogId(blog.getId());
250: trackback.setId(entityID);
251:
252: _fetcher.loadTrackback(blog, trackback);
253:
254: TrackbackMarkedSpamEvent trackbackMarkedSpamEvent = new TrackbackMarkedSpamEvent(
255: this , new Date(), trackback, blog);
256: _eventBroadcaster
257: .broadcastEvent(trackbackMarkedSpamEvent);
258:
259: if (_logger.isDebugEnabled()) {
260: _logger
261: .debug("Marked trackback as spam for trackback ID: "
262: + entityID);
263: }
264: } catch (NumberFormatException e) {
265: } catch (FetcherException e) {
266: if (_logger.isErrorEnabled()) {
267: _logger.error(e);
268: }
269: }
270: }
271:
272: String[] markspamPingbacks = BlojsomUtils.getRequestValues(
273: MARK_SPAM_PINGBACKS, httpServletRequest);
274: for (int i = 0; i < markspamPingbacks.length; i++) {
275: try {
276: String item = markspamPingbacks[i];
277: entityID = Integer.valueOf(item);
278: Pingback pingback = _fetcher.newPingback();
279: pingback.setBlogId(blog.getId());
280: pingback.setId(entityID);
281:
282: _fetcher.loadPingback(blog, pingback);
283:
284: PingbackMarkedSpamEvent pingbackMarkedSpamEvent = new PingbackMarkedSpamEvent(
285: this , new Date(), pingback, blog);
286: _eventBroadcaster
287: .broadcastEvent(pingbackMarkedSpamEvent);
288:
289: if (_logger.isDebugEnabled()) {
290: _logger
291: .debug("Marked pingback as spam for pingback ID: "
292: + entityID);
293: }
294: } catch (NumberFormatException e) {
295: } catch (FetcherException e) {
296: if (_logger.isErrorEnabled()) {
297: _logger.error(e);
298: }
299: }
300: }
301:
302: String[] delete = BlojsomUtils.getRequestValues(
303: DELETE_COMMENTS, httpServletRequest);
304: for (int i = 0; i < delete.length; i++) {
305: String item = delete[i];
306: try {
307: entityID = Integer.valueOf(item);
308: Comment comment = _fetcher.newComment();
309: comment.setBlogId(blog.getId());
310: comment.setId(entityID);
311:
312: _fetcher.loadComment(blog, comment);
313: _fetcher.deleteComment(blog, comment);
314: commentsDeleted++;
315:
316: _eventBroadcaster
317: .broadcastEvent(new CommentDeletedEvent(
318: this , new Date(), comment, blog));
319: if (_logger.isDebugEnabled()) {
320: _logger.debug("Bulk delete comment ID: "
321: + entityID);
322: }
323: } catch (NumberFormatException e) {
324: } catch (FetcherException e) {
325: if (_logger.isErrorEnabled()) {
326: _logger.error(e);
327: }
328: }
329: }
330:
331: String[] approve = BlojsomUtils.getRequestValues(
332: APPROVE_COMMENTS, httpServletRequest);
333: for (int i = 0; i < approve.length; i++) {
334: String item = approve[i];
335: try {
336: entityID = Integer.valueOf(item);
337: Comment comment = _fetcher.newComment();
338: comment.setBlogId(blog.getId());
339: comment.setId(entityID);
340:
341: _fetcher.loadComment(blog, comment);
342: comment
343: .setStatus(ResponseConstants.APPROVED_STATUS);
344: _fetcher.saveComment(blog, comment);
345: commentsApproved += 1;
346:
347: _eventBroadcaster
348: .broadcastEvent(new CommentApprovedEvent(
349: this , new Date(), comment, blog));
350: if (_logger.isDebugEnabled()) {
351: _logger.debug("Bulk approve comment ID: "
352: + entityID);
353: }
354: } catch (NumberFormatException e) {
355: } catch (FetcherException e) {
356: if (_logger.isErrorEnabled()) {
357: _logger.error(e);
358: }
359: }
360: }
361:
362: delete = BlojsomUtils.getRequestValues(DELETE_TRACKBACKS,
363: httpServletRequest);
364: for (int i = 0; i < delete.length; i++) {
365: String item = delete[i];
366: try {
367: entityID = Integer.valueOf(item);
368: Trackback trackback = _fetcher.newTrackback();
369: trackback.setBlogId(blog.getId());
370: trackback.setId(entityID);
371:
372: _fetcher.loadTrackback(blog, trackback);
373: _fetcher.deleteTrackback(blog, trackback);
374: trackbacksDeleted += 1;
375:
376: _eventBroadcaster
377: .broadcastEvent(new TrackbackDeletedEvent(
378: this , new Date(), trackback, blog));
379: if (_logger.isDebugEnabled()) {
380: _logger.debug("Bulk delete trackback ID: "
381: + entityID);
382: }
383: } catch (NumberFormatException e) {
384: } catch (FetcherException e) {
385: if (_logger.isErrorEnabled()) {
386: _logger.error(e);
387: }
388: }
389: }
390:
391: approve = BlojsomUtils.getRequestValues(APPROVE_TRACKBACKS,
392: httpServletRequest);
393: for (int i = 0; i < approve.length; i++) {
394: String item = approve[i];
395: try {
396: entityID = Integer.valueOf(item);
397: Trackback trackback = _fetcher.newTrackback();
398: trackback.setBlogId(blog.getId());
399: trackback.setId(entityID);
400:
401: _fetcher.loadTrackback(blog, trackback);
402: trackback
403: .setStatus(ResponseConstants.APPROVED_STATUS);
404: _fetcher.saveTrackback(blog, trackback);
405: trackbacksApproved += 1;
406:
407: _eventBroadcaster
408: .broadcastEvent(new TrackbackApprovedEvent(
409: this , new Date(), trackback, blog));
410: if (_logger.isDebugEnabled()) {
411: _logger.debug("Bulk approve trackback ID: "
412: + entityID);
413: }
414: } catch (NumberFormatException e) {
415: } catch (FetcherException e) {
416: if (_logger.isErrorEnabled()) {
417: _logger.error(e);
418: }
419: }
420: }
421:
422: delete = BlojsomUtils.getRequestValues(DELETE_PINGBACKS,
423: httpServletRequest);
424: for (int i = 0; i < delete.length; i++) {
425: String item = delete[i];
426: try {
427: entityID = Integer.valueOf(item);
428: Pingback pingback = _fetcher.newPingback();
429: pingback.setBlogId(blog.getId());
430: pingback.setId(entityID);
431:
432: _fetcher.loadPingback(blog, pingback);
433: _fetcher.deletePingback(blog, pingback);
434: pingbacksDeleted += 1;
435:
436: _eventBroadcaster
437: .broadcastEvent(new PingbackDeletedEvent(
438: this , new Date(), pingback, blog));
439: if (_logger.isDebugEnabled()) {
440: _logger.debug("Bulk delete pingback ID: "
441: + entityID);
442: }
443: } catch (NumberFormatException e) {
444: } catch (FetcherException e) {
445: if (_logger.isErrorEnabled()) {
446: _logger.error(e);
447: }
448: }
449: }
450:
451: approve = BlojsomUtils.getRequestValues(APPROVE_PINGBACKS,
452: httpServletRequest);
453: for (int i = 0; i < approve.length; i++) {
454: String item = approve[i];
455: try {
456: entityID = Integer.valueOf(item);
457: Pingback pingback = _fetcher.newPingback();
458: pingback.setBlogId(blog.getId());
459: pingback.setId(entityID);
460:
461: _fetcher.loadPingback(blog, pingback);
462: pingback
463: .setStatus(ResponseConstants.APPROVED_STATUS);
464: _fetcher.savePingback(blog, pingback);
465: pingbacksApproved += 1;
466:
467: _eventBroadcaster
468: .broadcastEvent(new PingbackApprovedEvent(
469: this , new Date(), pingback, blog));
470: if (_logger.isDebugEnabled()) {
471: _logger.debug("Bulk approve pingback ID: "
472: + entityID);
473: }
474: } catch (NumberFormatException e) {
475: } catch (FetcherException e) {
476: if (_logger.isErrorEnabled()) {
477: _logger.error(e);
478: }
479: }
480: }
481:
482: if (commentsApproved > 0 || commentsDeleted > 0
483: || trackbacksApproved > 0 || trackbacksDeleted > 0
484: || pingbacksApproved > 0 || pingbacksDeleted > 0) {
485: addOperationResultMessage(context, formatAdminResource(
486: SUCCESFUL_BULK_PROCESSING_KEY,
487: SUCCESFUL_BULK_PROCESSING_KEY, blog
488: .getBlogAdministrationLocale(),
489: new Object[] { new Integer(commentsApproved),
490: new Integer(commentsDeleted),
491: new Integer(trackbacksApproved),
492: new Integer(trackbacksDeleted),
493: new Integer(pingbacksApproved),
494: new Integer(pingbacksDeleted) }));
495: }
496: }
497:
498: String query = BlojsomUtils.getRequestValue(QUERY,
499: httpServletRequest);
500:
501: // Put the responses on the request
502: try {
503: if (BlojsomUtils.checkNullOrBlank(query)) {
504: context.put(BULK_RESPONSES, _fetcher
505: .findResponsesByStatus(blog,
506: _responseStatusList));
507: } else {
508: context.put(BULK_RESPONSES, _fetcher
509: .findResponsesByQuery(blog, query));
510: context.put(QUERY, BlojsomUtils.escapeString(query));
511: }
512: } catch (FetcherException e) {
513: if (_logger.isErrorEnabled()) {
514: _logger.error(e);
515: }
516: }
517:
518: httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM,
519: BULK_RESPONSE_MANAGEMENT_PAGE);
520:
521: return entries;
522: }
523: }
|