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.editing;
004:
005: import fitnesse.FitNesseContext;
006: import fitnesse.Responder;
007: import fitnesse.authentication.SecureOperation;
008: import fitnesse.authentication.SecureWriteOperation;
009: import fitnesse.components.SaveRecorder;
010: import fitnesse.html.HtmlUtil;
011: import fitnesse.http.Request;
012: import fitnesse.http.Response;
013: import fitnesse.http.SimpleResponse;
014: import fitnesse.responders.ErrorResponder;
015: import fitnesse.responders.SecureResponder;
016: import fitnesse.responders.refactoring.NewStiqPageResponder;
017: import fitnesse.util.PropertiesUtil;
018: import fitnesse.wiki.PageCrawler;
019: import fitnesse.wiki.PageData;
020: import fitnesse.wiki.PathParser;
021: import fitnesse.wiki.WikiPage;
022: import fitnesse.wiki.WikiPagePath;
023: import fitnesse.wikitext.widgets.TagSuiteWidget;
024: import fitnesse.wikitext.widgets.WikiWordWidget;
025:
026: public class CreateStiqPageResponder implements SecureResponder {
027:
028: public static String RESPONDER_KEY = "createStiqPage";
029:
030: public Response makeResponse(FitNesseContext context,
031: Request request) throws Exception {
032: Response response = null;
033: String resource = request.getResource();
034: String testPageName = ((String) request
035: .getInput(NewStiqPageResponder.STIQ_PAGE_NAME_INPUT_NAME))
036: .trim();
037: String testPageType = (String) request
038: .getInput(NewStiqPageResponder.STIQ_PAGE_TYPE_SELECT_NAME);
039:
040: WikiPage parentPage = getPage(resource, context);
041: if (parentPage == null) {
042: return makeErrorMessageResponder(testPageName,
043: "Parent page does not exist.").makeResponse(
044: context, request);
045: }
046: if (testPageName == null
047: || !WikiWordWidget.isSingleWikiWord(testPageName)) {
048: return makeErrorMessageResponder(
049: testPageName,
050: "Page name '" + testPageName + "' is not a "
051: + getWikiWordLink() + ".").makeResponse(
052: context, request);
053: }
054: if (pageExists(context, request, testPageName)) {
055: return makeErrorMessageResponder(testPageName,
056: "Page already exists.").makeResponse(context,
057: request);
058: }
059: if (!NewStiqPageResponder.STIQ_PAGE_TYPES
060: .contains(testPageType)) {
061: return makeErrorMessageResponder(testPageName,
062: "Illegal test type.")
063: .makeResponse(context, request);
064: }
065:
066: WikiPage testPage = parentPage.addChildPage(testPageName);
067: String user = request.getAuthorizationUsername();
068: PageData data = testPage.getData();
069: String responseRedirect = request.getResource();
070:
071: // Create default content based on type
072: if (NewStiqPageResponder.STIQ_PAGE_TYPE_TEST
073: .equals(testPageType)) {
074: data.setContent("| Test description |\n\n");
075: data.setAttribute(WikiPage.STIQ_TEST);
076: } else if (NewStiqPageResponder.STIQ_PAGE_TYPE_TEST_SUITE
077: .equals(testPageType)) {
078: data.setContent("!suite\n");
079: data.setAttribute(WikiPage.STIQ_SUITE);
080: } else if (NewStiqPageResponder.STIQ_PAGE_TYPE_COMPONENT
081: .equals(testPageType)) {
082: data
083: .setContent("| comment | Component description | |\n\n");
084: data.setAttribute(WikiPage.STIQ_COMPONENT);
085: } else if (NewStiqPageResponder.STIQ_PAGE_TYPE_COMPONENT_SUITE
086: .equals(testPageType)) {
087: data.setContent("!components\n");
088: data.setAttribute(WikiPage.STIQ_SUITE);
089: } else if (NewStiqPageResponder.STIQ_PAGE_TYPE_CONTAINER
090: .equals(testPageType)) {
091: data.setContent("!contents\n");
092: } else if (NewStiqPageResponder.STIQ_PAGE_TYPE_TAG_SUITE
093: .equals(testPageType)) {
094: data.setContent("!tagsuite "
095: + getTagsuiteStartPageDefault() + " "
096: + getTagsuiteTagDefault() + "\n");
097: data.setAttribute(WikiPage.STIQ_SUITE);
098: }
099: setData(data, user);
100: testPage.commit(data);
101:
102: response = new SimpleResponse();
103: response.redirect(responseRedirect);
104: return response;
105: }
106:
107: private boolean pageExists(FitNesseContext context,
108: Request request, String testPageName) {
109: String parentName = request.getResource();
110: boolean exists = true;
111:
112: if (testPageName != null
113: && WikiWordWidget.isSingleWikiWord(testPageName)) {
114: PageCrawler pageCrawler = context.root.getPageCrawler();
115: WikiPagePath subjectPath = PathParser.parse(parentName);
116: WikiPage subjectPage;
117: try {
118: subjectPage = pageCrawler.getPage(context.root,
119: subjectPath);
120: if (subjectPage != null) {
121: WikiPagePath parentPath = subjectPath.parentPath();
122: WikiPage parent = pageCrawler.getPage(context.root,
123: parentPath);
124: exists = pageCrawler.pageExists(parent, PathParser
125: .parse(testPageName));
126: }
127: } catch (Exception e) {
128: exists = false;
129: }
130: }
131: return exists;
132: }
133:
134: private Responder makeErrorMessageResponder(String testPageName,
135: String message) throws Exception {
136: return new ErrorResponder("Cannot create " + testPageName
137: + HtmlUtil.BR + message);
138: }
139:
140: private WikiPage getPage(String resource, FitNesseContext context)
141: throws Exception {
142: WikiPagePath path = PathParser.parse(resource);
143: PageCrawler pageCrawler = context.root.getPageCrawler();
144: WikiPage page = pageCrawler.getPage(context.root, path);
145: return page;
146: }
147:
148: private void setData(PageData data, String user) throws Exception {
149: SaveRecorder.pageSaved(data);
150: if (user != null)
151: data.setAttribute(WikiPage.LAST_MODIFYING_USER, user);
152: else
153: data.removeAttribute(WikiPage.LAST_MODIFYING_USER);
154: }
155:
156: public SecureOperation getSecureOperation() {
157: return new SecureWriteOperation();
158: }
159:
160: private String getWikiWordLink() {
161: return "<a href=\"http://storytestiq.solutionsiq.com/wiki/WikiWord\" target=\"STIQ Help\">WikiWord</a>";
162: }
163:
164: private String getTagsuiteStartPageDefault() {
165: return PropertiesUtil.getProperty(
166: TagSuiteWidget.DEFAULT_START_PAGE_PROPERTY,
167: TagSuiteWidget.DEFAULT_START_PAGE_DEFAULT).trim();
168: }
169:
170: private String getTagsuiteTagDefault() {
171: return PropertiesUtil.getProperty(
172: TagSuiteWidget.DEFAULT_TAG_PROPERTY,
173: TagSuiteWidget.DEFAULT_TAG_DEFAULT).trim();
174: }
175: }
|