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