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.responders.SecureResponder;
007: import fitnesse.authentication.*;
008: import fitnesse.components.TagManager;
009: import fitnesse.html.*;
010: import fitnesse.http.*;
011: import fitnesse.wiki.*;
012: import java.util.List;
013:
014: public class DeletePageResponder implements SecureResponder {
015: public Response makeResponse(FitNesseContext context,
016: Request request) throws Exception {
017: SimpleResponse response = new SimpleResponse();
018: String qualifiedPageName = request.getResource();
019: WikiPagePath path = PathParser.parse(qualifiedPageName);
020: if (qualifiedPageName.equals("FrontPage"))
021: response.redirect("FrontPage");
022: else {
023: String confirmedString = (String) request
024: .getInput("confirmed");
025: if ("yes".equals(confirmedString)) {
026: WikiPage pageToBeDeleted = context.root
027: .getPageCrawler().getPage(context.root, path);
028: TagManager.INSTANCE
029: .removeExistingTagsForPage(pageToBeDeleted);
030: String nameOfPageToBeDeleted = path.last();
031: path.pop();
032: WikiPage parentOfPageToBeDeleted = context.root
033: .getPageCrawler().getPage(context.root, path);
034: if (parentOfPageToBeDeleted != null)
035: parentOfPageToBeDeleted
036: .removeChildPage(nameOfPageToBeDeleted);
037: redirect(path, response);
038: } else
039: response.setContent(buildConfirmationHtml(context.root,
040: qualifiedPageName, context));
041: }
042:
043: return response;
044: }
045:
046: private void redirect(WikiPagePath path, SimpleResponse response) {
047: String location = PathParser.render(path);
048: if (location == null || location.length() == 0)
049: response.redirect("root");
050: else
051: response.redirect(location);
052: }
053:
054: private String buildConfirmationHtml(WikiPage root,
055: String qualifiedPageName, FitNesseContext context)
056: throws Exception {
057: HtmlPage html = context.htmlPageFactory.newPage();
058: html.title.use("Delete Confirmation");
059: html.header.use(HtmlUtil.makeBreadCrumbsWithPageType(
060: qualifiedPageName, "/", "Confirm Deletion"));
061: html.main.use(makeMainContent(root, qualifiedPageName));
062: return html.html();
063: }
064:
065: private String makeMainContent(WikiPage root,
066: String qualifiedPageName) throws Exception {
067: WikiPagePath path = PathParser.parse(qualifiedPageName);
068: WikiPage pageToDelete = root.getPageCrawler().getPage(root,
069: path);
070: List children = pageToDelete.getChildren();
071: boolean addSubPageWarning = true;
072: if (children == null || children.size() == 0)
073: addSubPageWarning = false;
074:
075: HtmlTag divTag = HtmlUtil.makeDivTag("centered");
076: divTag
077: .add(makeHeadingTag(addSubPageWarning,
078: qualifiedPageName));
079: divTag.add(HtmlUtil.BR);
080: divTag.add(HtmlUtil.makeLink(qualifiedPageName
081: + "?responder=deletePage&confirmed=yes", "Yes"));
082: divTag.add(" ");
083: divTag.add(HtmlUtil.makeLink(qualifiedPageName, "No"));
084:
085: return divTag.html();
086: }
087:
088: private HtmlTag makeHeadingTag(boolean addSubPageWarning,
089: String qualifiedPageName) {
090: HtmlTag h3Tag = new HtmlTag("H3");
091: if (addSubPageWarning) {
092: h3Tag
093: .add("Warning, this page contains one or more subpages.");
094: h3Tag.add(HtmlUtil.BR);
095: }
096: h3Tag.add("Are you sure you want to delete "
097: + qualifiedPageName + "?");
098: return h3Tag;
099: }
100:
101: public SecureOperation getSecureOperation() {
102: return new AlwaysSecureOperation();
103: }
104: }
|