001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.responders.refactoring;
004:
005: import java.util.Iterator;
006: import java.util.List;
007:
008: import fitnesse.FitNesseContext;
009: import fitnesse.Responder;
010: import fitnesse.authentication.AlwaysSecureOperation;
011: import fitnesse.authentication.SecureOperation;
012: import fitnesse.components.PageReferenceRenamer;
013: import fitnesse.components.TagManager;
014: import fitnesse.html.HtmlUtil;
015: import fitnesse.http.Request;
016: import fitnesse.http.Response;
017: import fitnesse.http.SimpleResponse;
018: import fitnesse.responders.ErrorResponder;
019: import fitnesse.responders.NotFoundResponder;
020: import fitnesse.responders.SecureResponder;
021: import fitnesse.wiki.PageCrawler;
022: import fitnesse.wiki.PageData;
023: import fitnesse.wiki.PathParser;
024: import fitnesse.wiki.WikiPage;
025: import fitnesse.wiki.WikiPagePath;
026: import fitnesse.wikitext.widgets.WikiWordWidget;
027:
028: public class RenamePageResponder implements SecureResponder {
029: private String qualifiedName;
030:
031: private String newName;
032:
033: public Response makeResponse(FitNesseContext context,
034: Request request) throws Exception {
035: qualifiedName = request.getResource();
036: newName = (String) request.getInput("newName");
037:
038: Response response;
039:
040: if (newName != null && !qualifiedName.equals("FrontPage")
041: && WikiWordWidget.isSingleWikiWord(newName)) {
042: PageCrawler pageCrawler = context.root.getPageCrawler();
043:
044: WikiPagePath subjectPath = PathParser.parse(qualifiedName);
045: WikiPage subjectPage = pageCrawler.getPage(context.root,
046: subjectPath);
047: if (subjectPage == null)
048: response = new NotFoundResponder().makeResponse(
049: context, request);
050: else {
051: WikiPagePath parentPath = subjectPath.parentPath();
052: WikiPage parent = pageCrawler.getPage(context.root,
053: parentPath);
054: final boolean pageExists = pageCrawler.pageExists(
055: parent, PathParser.parse(newName));
056: if (!pageExists) {
057: qualifiedName = doRename(context.root, subjectPage,
058: parent, newName, subjectPath);
059: response = new SimpleResponse();
060: response.redirect(qualifiedName);
061: } else // already exists
062: {
063: response = makeErrorMessageResponder(
064: makeLink(newName) + " already exists")
065: .makeResponse(context, request);
066: }
067: }
068: } else {
069: response = makeErrorMessageResponder(
070: newName + " is not a valid simple page name.")
071: .makeResponse(context, request);
072: }
073:
074: return response;
075: }
076:
077: private Responder makeErrorMessageResponder(String message)
078: throws Exception {
079: return new ErrorResponder("Cannot rename "
080: + makeLink(qualifiedName) + " to " + newName
081: + HtmlUtil.BR + message);
082: }
083:
084: private String makeLink(String page) throws Exception {
085: return HtmlUtil.makeLink(page, page).html();
086: }
087:
088: private String doRename(WikiPage root, WikiPage pageToRename,
089: WikiPage parent, String newName, WikiPagePath subjectPath)
090: throws Exception {
091: PageReferenceRenamer renamer = new PageReferenceRenamer(root);
092: renamer.renameReferences(pageToRename, newName);
093: rename(parent, pageToRename.getName(), newName, root);
094:
095: subjectPath.pop();
096: subjectPath.addName(newName);
097: return PathParser.render(subjectPath);
098: }
099:
100: private static boolean rename(WikiPage context, String oldName,
101: String newName, WikiPage root) throws Exception {
102: if (context.hasChildPage(oldName)
103: && !context.hasChildPage(newName)) {
104: WikiPage originalPage = context.getChildPage(oldName);
105: TagManager.INSTANCE.removeExistingTagsForPage(originalPage);
106:
107: PageCrawler crawler = originalPage.getPageCrawler();
108: PageData data = originalPage.getData();
109:
110: WikiPage renamedPage = context.addChildPage(newName);
111: renamedPage.commit(data);
112: TagManager.INSTANCE.recordTagsForPage(renamedPage);
113: List children = originalPage.getChildren();
114: for (Iterator iterator = children.iterator(); iterator
115: .hasNext();) {
116: WikiPage child = (WikiPage) iterator.next();
117: MovePageResponder.movePage(root, crawler
118: .getFullPath(child), crawler
119: .getFullPath(renamedPage));
120: }
121:
122: context.removeChildPage(oldName);
123: return true;
124: }
125: return false;
126: }
127:
128: public SecureOperation getSecureOperation() {
129: return new AlwaysSecureOperation();
130: }
131: }
|