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 fitnesse.*;
006: import fitnesse.authentication.*;
007: import fitnesse.components.MovedPageReferenceRenamer;
008: import fitnesse.components.TagManager;
009: import fitnesse.html.HtmlUtil;
010: import fitnesse.http.*;
011: import fitnesse.responders.*;
012: import fitnesse.wiki.*;
013: import java.util.*;
014:
015: public class MovePageResponder implements SecureResponder {
016: private String nameOfPageToBeMoved;
017:
018: private String newParentName;
019:
020: public Response makeResponse(FitNesseContext context,
021: Request request) throws Exception {
022: newParentName = getNameofNewParent(request);
023:
024: nameOfPageToBeMoved = request.getResource();
025: WikiPagePath path = PathParser.parse(nameOfPageToBeMoved);
026: PageCrawler crawler = context.root.getPageCrawler();
027: WikiPage pageToBeMoved = crawler.getPage(context.root, path);
028: if (pageToBeMoved == null)
029: return new NotFoundResponder().makeResponse(context,
030: request);
031:
032: WikiPagePath newParentPath = PathParser.parse(newParentName);
033: WikiPage newParent = crawler.getPage(context.root,
034: newParentPath);
035: if (newParent == null)
036: return makeErrorMessageResponder(
037: newParentName + " does not exist.").makeResponse(
038: context, request);
039:
040: if (pageCanBeMoved(pageToBeMoved, newParent, path,
041: newParentPath)) {
042: MovedPageReferenceRenamer renamer = new MovedPageReferenceRenamer(
043: context.root);
044: renamer.renameReferences(pageToBeMoved, newParentName);
045: movePage(context.root, crawler.getFullPath(pageToBeMoved),
046: crawler.getFullPath(newParent));
047:
048: SimpleResponse response = new SimpleResponse();
049: response.redirect(createRedirectionUrl(newParent,
050: pageToBeMoved));
051: return response;
052: } else {
053: return makeErrorMessageResponder("").makeResponse(context,
054: request);
055: }
056: }
057:
058: private static String getNameofNewParent(Request request) {
059: String newParentName = (String) request.getInput("newLocation");
060: if (".".equals(newParentName))
061: newParentName = "";
062: return newParentName;
063: }
064:
065: private boolean pageCanBeMoved(WikiPage pageToBeMoved,
066: WikiPage newParent, WikiPagePath pageToBeMovedPath,
067: WikiPagePath newParentPath) throws Exception {
068: return !pageToBeMovedPath.equals(newParentPath)
069: && !selfPage(pageToBeMovedPath, newParentPath)
070: && targetPageDoesntExist(pageToBeMoved.getName(),
071: newParent)
072: && !pageIsAncestorOfNewParent(pageToBeMovedPath,
073: newParentPath);
074: }
075:
076: public boolean pageIsAncestorOfNewParent(
077: WikiPagePath pageToBeMovedPath, WikiPagePath newParentPath)
078: throws Exception {
079: return newParentPath.startsWith(pageToBeMovedPath);
080: }
081:
082: public String createRedirectionUrl(WikiPage newParent,
083: WikiPage pageToBeMoved) throws Exception {
084: PageCrawler crawler = pageToBeMoved.getPageCrawler();
085: if (crawler.isRoot(newParent))
086: return pageToBeMoved.getName();
087: else
088: return PathParser.render(crawler.getFullPath(newParent)
089: .addName(pageToBeMoved.getName()));
090: }
091:
092: public boolean selfPage(WikiPagePath pageToBeMovedPath,
093: WikiPagePath newParentPath) throws Exception {
094: WikiPagePath originalParentPath = pageToBeMovedPath
095: .parentPath();
096: return originalParentPath.equals(newParentPath);
097: }
098:
099: private Responder makeErrorMessageResponder(String message)
100: throws Exception {
101: return new ErrorResponder("Cannot move "
102: + makeLink(nameOfPageToBeMoved) + " below "
103: + newParentName + HtmlUtil.BR + message);
104: }
105:
106: private String makeLink(String page) throws Exception {
107: return HtmlUtil.makeLink(page, page).html();
108: }
109:
110: public static void movePage(WikiPage root,
111: WikiPagePath pageToBeMovedPath, WikiPagePath newParentPath)
112: throws Exception {
113: PageCrawler crawler = root.getPageCrawler();
114: WikiPage movee = crawler.getPage(root, pageToBeMovedPath);
115: WikiPagePath movedPagePath = newParentPath.withNameAdded(movee
116: .getName());
117: WikiPage movedPage = crawler.addPage(root, movedPagePath, movee
118: .getData().getContent());
119: PageData movedData = movedPage.getData();
120: PageData oldData = movee.getData();
121: movedData.setProperties(oldData.getProperties());
122: movedData.setLastModificationTime(oldData
123: .getLastModificationTime());
124: List children = movee.getChildren();
125: if (children.size() > 0)
126: moveChildren(children, root, movedPagePath);
127: movedPage.commit(movedData);
128: WikiPagePath originalParentPath = pageToBeMovedPath
129: .parentPath();
130: WikiPage parentOfMovedPage = crawler.getPage(root,
131: originalParentPath);
132: parentOfMovedPage.removeChildPage(movee.getName());
133: TagManager.INSTANCE.recordTagsForPage(movedPage);
134: TagManager.INSTANCE.removeExistingTagsForPage(movee);
135: }
136:
137: private boolean targetPageDoesntExist(String oldWikiPageName,
138: WikiPage newParent) throws Exception {
139: return !newParent.hasChildPage(oldWikiPageName);
140: }
141:
142: public static void moveChildren(List children, WikiPage root,
143: WikiPagePath newParentPath) throws Exception {
144: for (Iterator iterator = children.iterator(); iterator
145: .hasNext();) {
146: WikiPage page = (WikiPage) iterator.next();
147: movePage(root, page.getPageCrawler().getFullPath(page),
148: newParentPath);
149: }
150: }
151:
152: public SecureOperation getSecureOperation() {
153: return new AlwaysSecureOperation();
154: }
155: }
|