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.text.DateFormat;
019: import java.util.Collection;
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpServletResponse;
022: import org.apache.commons.lang.StringUtils;
023: import org.jamwiki.WikiBase;
024: import org.jamwiki.WikiException;
025: import org.jamwiki.WikiMessage;
026: import org.jamwiki.model.Topic;
027: import org.jamwiki.model.TopicVersion;
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 display the edit history information for a topic.
035: */
036: public class HistoryServlet extends JAMWikiServlet {
037:
038: private static final WikiLogger logger = WikiLogger
039: .getLogger(HistoryServlet.class.getName());
040: protected static final String JSP_HISTORY = "history.jsp";
041:
042: /**
043: *
044: */
045: protected ModelAndView handleJAMWikiRequest(
046: HttpServletRequest request, HttpServletResponse response,
047: ModelAndView next, WikiPageInfo pageInfo) throws Exception {
048: if (!StringUtils
049: .isBlank(request.getParameter("topicVersionId"))) {
050: viewVersion(request, next, pageInfo);
051: } else {
052: history(request, next, pageInfo);
053: }
054: return next;
055: }
056:
057: /**
058: *
059: */
060: private void history(HttpServletRequest request, ModelAndView next,
061: WikiPageInfo pageInfo) throws Exception {
062: String virtualWiki = WikiUtil.getVirtualWikiFromURI(request);
063: String topicName = WikiUtil.getTopicFromRequest(request);
064: if (StringUtils.isBlank(topicName)) {
065: throw new WikiException(new WikiMessage(
066: "common.exception.notopic"));
067: }
068: pageInfo.setContentJsp(JSP_HISTORY);
069: pageInfo.setTopicName(topicName);
070: pageInfo.setPageTitle(new WikiMessage("history.title",
071: topicName));
072: Pagination pagination = ServletUtil.loadPagination(request,
073: next);
074: Collection changes = WikiBase.getDataHandler()
075: .getRecentChanges(virtualWiki, topicName, pagination,
076: true);
077: next.addObject("changes", changes);
078: next.addObject("numChanges", new Integer(changes.size()));
079: }
080:
081: /**
082: *
083: */
084: private void viewVersion(HttpServletRequest request,
085: ModelAndView next, WikiPageInfo pageInfo) throws Exception {
086: // display an older version
087: String virtualWiki = WikiUtil.getVirtualWikiFromURI(request);
088: String topicName = WikiUtil.getTopicFromRequest(request);
089: int topicVersionId = Integer.parseInt(request
090: .getParameter("topicVersionId"));
091: TopicVersion topicVersion = WikiBase.getDataHandler()
092: .lookupTopicVersion(topicVersionId, null);
093: if (topicVersion == null) {
094: throw new WikiException(new WikiMessage(
095: "common.exception.notopic"));
096: }
097: Topic topic = WikiBase.getDataHandler().lookupTopic(
098: virtualWiki, topicName, false, null);
099: if (topic == null) {
100: // the topic may have been deleted
101: throw new WikiException(new WikiMessage(
102: "history.message.notopic", topicName));
103: }
104: topic.setTopicContent(topicVersion.getVersionContent());
105: String versionDate = DateFormat.getDateTimeInstance().format(
106: topicVersion.getEditDate());
107: WikiMessage pageTitle = new WikiMessage("topic.title",
108: topicName + " @" + versionDate);
109: ServletUtil.viewTopic(request, next, pageInfo, pageTitle,
110: topic, false);
111: }
112: }
|