001: /**
002: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the latest version of the GNU Lesser General
006: * Public License as published by the Free Software Foundation;
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program (LICENSE.txt); if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: */package org.jamwiki.servlets;
017:
018: import java.util.Collection;
019: import javax.servlet.http.HttpServletRequest;
020: import javax.servlet.http.HttpServletResponse;
021: import org.apache.commons.lang.StringUtils;
022: import org.jamwiki.WikiBase;
023: import org.jamwiki.WikiException;
024: import org.jamwiki.WikiMessage;
025: import org.jamwiki.authentication.WikiUserAuth;
026: import org.jamwiki.model.Role;
027: import org.jamwiki.model.Watchlist;
028: import org.jamwiki.utils.Pagination;
029: import org.jamwiki.utils.WikiLogger;
030: import org.jamwiki.utils.WikiUtil;
031: import org.springframework.web.servlet.ModelAndView;
032:
033: /**
034: * Used to handle updating and viewing a user's watchlist.
035: */
036: public class WatchlistServlet extends JAMWikiServlet {
037:
038: /** Logger for this class and subclasses. */
039: private static final WikiLogger logger = WikiLogger
040: .getLogger(WatchlistServlet.class.getName());
041: protected static final String JSP_WATCHLIST = "watchlist.jsp";
042:
043: /**
044: *
045: */
046: protected ModelAndView handleJAMWikiRequest(
047: HttpServletRequest request, HttpServletResponse response,
048: ModelAndView next, WikiPageInfo pageInfo) throws Exception {
049: String topic = request.getParameter("topic");
050: if (!StringUtils.isBlank(topic)) {
051: update(request, next, pageInfo);
052: } else {
053: view(request, next, pageInfo);
054: }
055: return next;
056: }
057:
058: /**
059: *
060: */
061: private void update(HttpServletRequest request, ModelAndView next,
062: WikiPageInfo pageInfo) throws Exception {
063: WikiUserAuth user = ServletUtil.currentUser();
064: if (!user.hasRole(Role.ROLE_USER)) {
065: throw new WikiException(new WikiMessage(
066: "watchlist.error.loginrequired"));
067: }
068: String topicName = WikiUtil.getTopicFromRequest(request);
069: String virtualWiki = WikiUtil.getVirtualWikiFromURI(request);
070: Watchlist watchlist = ServletUtil.currentWatchlist(request,
071: virtualWiki);
072: WikiBase.getDataHandler().writeWatchlistEntry(watchlist,
073: virtualWiki, topicName, user.getUserId(), null);
074: String article = WikiUtil.extractTopicLink(topicName);
075: if (watchlist.containsTopic(topicName)) {
076: // added to watchlist
077: next.addObject("message", new WikiMessage(
078: "watchlist.caption.added", article));
079: } else {
080: // removed from watchlist
081: next.addObject("message", new WikiMessage(
082: "watchlist.caption.removed", article));
083: }
084: this .view(request, next, pageInfo);
085: }
086:
087: /**
088: *
089: */
090: private void view(HttpServletRequest request, ModelAndView next,
091: WikiPageInfo pageInfo) throws Exception {
092: String virtualWiki = WikiUtil.getVirtualWikiFromURI(request);
093: Pagination pagination = ServletUtil.loadPagination(request,
094: next);
095: WikiUserAuth user = ServletUtil.currentUser();
096: if (!user.hasRole(Role.ROLE_USER)) {
097: throw new WikiException(new WikiMessage(
098: "watchlist.error.loginrequired"));
099: }
100: Collection changes = WikiBase.getDataHandler().getWatchlist(
101: virtualWiki, user.getUserId(), pagination);
102: next.addObject("numChanges", new Integer(changes.size()));
103: next.addObject("changes", changes);
104: pageInfo.setPageTitle(new WikiMessage("watchlist.title"));
105: pageInfo.setContentJsp(JSP_WATCHLIST);
106: pageInfo.setSpecial(true);
107: }
108: }
|