001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/rwiki/tags/sakai_2-4-1/rwiki-tool/tool/src/java/uk/ac/cam/caret/sakai/rwiki/tool/bean/ViewBean.java $
003: * $Id: ViewBean.java 27686 2007-03-23 11:56:40Z ian@caret.cam.ac.uk $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package uk.ac.cam.caret.sakai.rwiki.tool.bean;
021:
022: import java.io.UnsupportedEncodingException;
023: import java.net.URLEncoder;
024: import java.util.Iterator;
025: import java.util.Map;
026:
027: import org.sakaiproject.component.cover.ServerConfigurationService;
028:
029: import uk.ac.cam.caret.sakai.rwiki.service.api.RWikiObjectService;
030: import uk.ac.cam.caret.sakai.rwiki.tool.util.WikiPageAction;
031: import uk.ac.cam.caret.sakai.rwiki.utils.NameHelper;
032:
033: /**
034: * This bean is a helper bean for the view view.
035: *
036: * @author andrew
037: */
038: // FIXME: Need a better way of making urls!
039: public class ViewBean {
040:
041: /**
042: * Parameter name for the parameter indicating which panel to use
043: */
044: public static final String PANEL_PARAM = "panel";
045:
046: /**
047: * Value of the parameter panel that indicates the main panel
048: */
049: public static final String MAIN_PANEL = "Main";
050:
051: /**
052: * Parameter name for the parameter indicating what action is required
053: */
054: public static final String ACTION_PARAM = "action";
055:
056: /**
057: * Parameter name for the paramater indicating which pageName to view, edit
058: * etc.
059: */
060: public static final String PAGE_NAME_PARAM = "pageName";
061:
062: /**
063: * Parameter name for the paramater indicating which pageName to view, edit
064: * etc.
065: */
066: public static final String PARAM_BREADCRUMB_NAME = "breadcrumb";
067:
068: /*
069: * These are urlencoded variants of constants from files that actually do
070: * the work...
071: */
072: protected static final String PAGENAME_URL_ENCODED = urlEncode(PAGE_NAME_PARAM);
073:
074: protected static final String DISBLE_BREADCRUMBS_ENCODED = urlEncode(PARAM_BREADCRUMB_NAME)
075: + "=0";
076:
077: protected static final String ACTION_URL_ENCODED = urlEncode(ACTION_PARAM);
078:
079: protected static final String PANEL_URL_ENCODED = urlEncode(PANEL_PARAM);
080:
081: protected static final String MAIN_URL_ENCODED = urlEncode(MAIN_PANEL);
082:
083: protected static final String SEARCH_URL_ENCODED = urlEncode(SearchBean.SEARCH_PARAM);
084:
085: protected static final String PAGE_URL_ENCODED = urlEncode(SearchBean.PAGE_PARAM);
086:
087: protected static final String REALM_URL_ENCODED = urlEncode(SearchBean.REALM_PARAM);
088:
089: /**
090: * The current pageName
091: */
092: private String pageName;
093:
094: /**
095: * The current localSpace
096: */
097: private String localSpace;
098:
099: /**
100: * The anchor to view
101: */
102: private String anchor;
103:
104: /**
105: * The current search criteria
106: */
107: private String search;
108:
109: /**
110: * Simple constructor that creates an empty view bean. You must set the
111: * pageName and the localSpace to make this useful
112: */
113: public ViewBean() {
114: // Beans must have null constructor!
115: }
116:
117: /**
118: * Creates a ViewBean and set's the interested page name and local space
119: *
120: * @param name
121: * page name possibly non-globalised
122: * @param defaultSpace
123: * default space to globalise against
124: */
125: public ViewBean(String name, String defaultSpace) {
126: this .pageName = NameHelper.globaliseName(name, defaultSpace);
127: this .localSpace = defaultSpace;
128: }
129:
130: private String getAnchorString() {
131: if (anchor != null) {
132: return "#" + urlEncode(anchor);
133: }
134: return "";
135: }
136:
137: /**
138: * Returns a public view URL with no breadcrumbs
139: *
140: * @return
141: */
142: public String getExportUrl() {
143: return getPageUrl(pageName, WikiPageAction.EXPORT_ACTION
144: .getName());
145: }
146:
147: /**
148: * Returns a public view URL with no breadcrumbs
149: *
150: * @return
151: */
152: public String getPublicViewUrl() {
153: return getPublicViewUrl(false);
154: }
155:
156: /**
157: * Returns a string representation of an url to perma view the current page
158: *
159: * @return url as string
160: */
161: public String getPublicViewUrl(boolean withBreadcrumbs) {
162: return getPageUrl(pageName, WikiPageAction.PUBLICVIEW_ACTION
163: .getName(), withBreadcrumbs);
164: }
165:
166: /**
167: * Returns a string representation of an url to view the current page
168: *
169: * @return url as string
170: */
171: public String getViewUrl() {
172: return getPageUrl(pageName, WikiPageAction.VIEW_ACTION
173: .getName());
174: }
175:
176: /**
177: * Returns a string representation of an url to view the passed in page
178: *
179: * @param name
180: * possibly non-globalised name to view
181: * @return url as string
182: */
183: public String getViewUrl(String name) {
184: return getPageUrl(NameHelper.globaliseName(name, localSpace),
185: WikiPageAction.VIEW_ACTION.getName());
186: }
187:
188: /**
189: * Returns a string representation of an url to edit the current page
190: *
191: * @return url as string
192: */
193: public String getEditUrl() {
194: return getPageUrl(pageName, WikiPageAction.EDIT_ACTION
195: .getName());
196: }
197:
198: /**
199: * Returns a string representation of an url to edit the passed in page.
200: *
201: * @param name
202: * possibly non-globalised name
203: * @return url as string
204: */
205:
206: public String getEditUrl(String name) {
207: return getPageUrl(NameHelper.globaliseName(name, localSpace),
208: WikiPageAction.EDIT_ACTION.getName());
209: }
210:
211: /**
212: * Returns a string representation of an url to view information about the
213: * current page
214: *
215: * @return url as string
216: */
217: public String getInfoUrl() {
218: return getPageUrl(pageName, WikiPageAction.INFO_ACTION
219: .getName());
220: }
221:
222: /**
223: * Returns a string representation of an url to view information about the
224: * passed in page
225: *
226: * @param name
227: * possibly non-globalised name
228: * @return url as string
229: */
230: public String getInfoUrl(String name) {
231: return getPageUrl(NameHelper.globaliseName(name, localSpace),
232: WikiPageAction.INFO_ACTION.getName());
233: }
234:
235: public String getHistoryUrl() {
236: return getHistoryUrl(pageName);
237: }
238:
239: public String getHistoryUrl(String name) {
240: return getPageUrl(NameHelper.globaliseName(name, localSpace),
241: WikiPageAction.HISTORY_ACTION.getName());
242: }
243:
244: /**
245: * Given a WikiPageAction return an url to the current page for performing
246: * that action.
247: *
248: * @param action
249: * WikiPageAction to perform
250: * @return url as string
251: */
252: public String getActionUrl(WikiPageAction action) {
253: return getActionUrl(action, true);
254: }
255:
256: /**
257: * Given a WikiPageAction return an url to the current page (with
258: * breadcrumbs on or off) for performing that action.
259: *
260: * @param action
261: * WikiPageAction to perform
262: * @param breadcrumbs
263: * false if breadcrumbs should be disabled
264: * @return url as string
265: */
266: public String getActionUrl(WikiPageAction action,
267: boolean breadcrumbs) {
268: return getPageUrl(this .pageName, action.getName(), breadcrumbs);
269: }
270:
271: /**
272: * Given a WikiPageAction return an url to the requested page (with
273: * breadcrumbs on or off) for performing that action.
274: *
275: * @param action
276: * WikiPageAction to perform
277: * @param breadcrumbs
278: * false if breadcrumbs should be disabled
279: * @return url as string
280: */
281: public String getActionUrl(String pageName, WikiPageAction action,
282: boolean breadcrumbs) {
283: return getPageUrl(pageName, action.getName(), breadcrumbs);
284: }
285:
286: /**
287: * Given a WikiPageAction return an url to the current page with the
288: * additional parameters being set.
289: *
290: * @param action
291: * WikiPageAction to perform
292: * @param parameters
293: * Additional query parameters to attach
294: * @return url as String
295: */
296: public String getActionUrl(WikiPageAction action, Map parameters) {
297: return getPageUrl(this .pageName, action.getName(), parameters);
298: }
299:
300: /**
301: * Given a WikiPageAction return an url to the requested page with the
302: * additional parameters being set.
303: *
304: * @param pageName
305: * globalised pagename to perform action on
306: * @param action
307: * WikiPageAction to perform
308: * @param parameters
309: * Additional query parameters to attach
310: * @return url as String
311: */
312: public String getActionUrl(String pageName, WikiPageAction action,
313: Map parameters) {
314: return getPageUrl(pageName, action.getName(), parameters);
315: }
316:
317: /**
318: * Given a page name and an action return an url that represents it.
319: *
320: * @param pageName
321: * globalised pagename to perform action on
322: * @param action
323: * name of action to perform
324: * @return url as string
325: */
326: protected String getPageUrl(String pageName, String action) {
327: return getPageUrl(pageName, action, true);
328: }
329:
330: /**
331: * Given a page name and an action return an url that represents it.
332: *
333: * @param pageName
334: * globalised pagename to perform action on
335: * @param action
336: * name of action to perform
337: * @param withBreadcrumbs
338: * if false, breadcrumb disable is propagated
339: * @return url as string
340: */
341: protected String getPageUrl(String pageName, String action,
342: boolean withBreadcrumbs) {
343: if (withBreadcrumbs) {
344: return "?" + PAGENAME_URL_ENCODED + "="
345: + urlEncode(pageName) + "&" + ACTION_URL_ENCODED
346: + "=" + urlEncode(action) + "&" + PANEL_URL_ENCODED
347: + "=" + MAIN_URL_ENCODED + "&" + REALM_URL_ENCODED
348: + "=" + urlEncode(localSpace) + getAnchorString();
349: } else {
350: return "?" + PAGENAME_URL_ENCODED + "="
351: + urlEncode(pageName) + "&" + ACTION_URL_ENCODED
352: + "=" + urlEncode(action) + "&" + PANEL_URL_ENCODED
353: + "=" + MAIN_URL_ENCODED + "&"
354: + DISBLE_BREADCRUMBS_ENCODED + "&"
355: + REALM_URL_ENCODED + "=" + urlEncode(localSpace)
356: + getAnchorString();
357: }
358:
359: }
360:
361: protected String getPageUrl(String pageName, String action,
362: Map params) {
363: StringBuffer url = new StringBuffer();
364: url.append("?").append(PAGENAME_URL_ENCODED).append('=')
365: .append(urlEncode(pageName));
366: url.append('&').append(ACTION_URL_ENCODED).append('=').append(
367: urlEncode(action));
368: url.append('&').append(PANEL_URL_ENCODED).append('=').append(
369: MAIN_URL_ENCODED);
370: url.append('&').append(REALM_URL_ENCODED).append('=').append(
371: urlEncode(localSpace));
372:
373: for (Iterator it = params.keySet().iterator(); it.hasNext();) {
374: String key = (String) it.next();
375: String value = (String) params.get(key);
376: if (!(PAGE_NAME_PARAM.equals(key)
377: || ACTION_PARAM.equals(key)
378: || PANEL_PARAM.equals(key) || SearchBean.REALM_PARAM
379: .equals(key))) {
380: url.append('&').append(urlEncode(key)).append('=')
381: .append(urlEncode(value));
382: }
383: }
384:
385: return url.append(getAnchorString()).toString();
386: }
387:
388: /**
389: * Creates an appropriate url for searching for the given criteria. XXX this
390: * shouldn't be here!
391: *
392: * @return url as string
393: */
394: protected String getSearchUrl() {
395: return "?" + ACTION_URL_ENCODED + "=" + SEARCH_URL_ENCODED
396: + "&" + SEARCH_URL_ENCODED + "=" + urlEncode(search)
397: + "&" + REALM_URL_ENCODED + "=" + urlEncode(localSpace)
398: + "&" + PANEL_URL_ENCODED + "=" + MAIN_URL_ENCODED;
399: }
400:
401: /**
402: * The Globalised Page Name
403: *
404: * @return globalised page name
405: */
406: public String getPageName() {
407: return pageName;
408: }
409:
410: /**
411: * Set the globalised page name
412: *
413: * @param pageName
414: * globalised page name
415: */
416: public void setPageName(String pageName) {
417: this .pageName = pageName;
418: }
419:
420: /**
421: * The page name localised against the localSpace
422: *
423: * @return localised page name
424: */
425: public String getLocalName() {
426: return NameHelper.localizeName(this .pageName, this .localSpace);
427: }
428:
429: /**
430: * The localSpace
431: *
432: * @return localSpace as string
433: */
434: public String getLocalSpace() {
435: return localSpace;
436: }
437:
438: /**
439: * Set the localSpace
440: *
441: * @param localSpace
442: * the new localSpace
443: */
444: public void setLocalSpace(String localSpace) {
445: this .localSpace = localSpace;
446: }
447:
448: /**
449: * The space of that the page is in
450: *
451: * @return the page's space
452: */
453: public String getPageSpace() {
454: return NameHelper.localizeSpace(pageName, localSpace);
455: }
456:
457: /**
458: * Takes a string to encode and encodes it as a UTF-8 URL-Encoded string.
459: *
460: * @param toEncode
461: * string to encode.
462: * @return url encoded string.
463: */
464: public static String urlEncode(String toEncode) {
465: try {
466: return URLEncoder.encode(toEncode, "UTF-8");
467: } catch (UnsupportedEncodingException e) {
468: throw new RuntimeException(
469: "Not entirely sure how this happened but UTF-8 doesn't "
470: + "represent a valid encoding anymore! Weird!",
471: e);
472: }
473: }
474:
475: /**
476: * Set the current anchor
477: *
478: * @param anchor
479: * anchor to set
480: */
481: public void setAnchor(String anchor) {
482: this .anchor = anchor;
483: }
484:
485: /**
486: * Get the current anchor name
487: *
488: * @return anchor
489: */
490: public String getAnchor() {
491: return anchor;
492: }
493:
494: /**
495: * The current search criteria XXX This shouldn't be here!
496: *
497: * @return search criteria
498: */
499: public String getSearch() {
500: return search;
501: }
502:
503: /**
504: * Set the current search criteria XXX This shouldn't be here!
505: *
506: * @param search
507: * the search criteria
508: */
509: public void setSearch(String search) {
510: this .search = search;
511: }
512:
513: public String getNewCommentURL() {
514: return getPageUrl(pageName, WikiPageAction.NEWCOMMENT_ACTION
515: .getName());
516:
517: }
518:
519: public String getEditCommentURL() {
520: return getPageUrl(pageName, WikiPageAction.EDITCOMMENT_ACTION
521: .getName());
522: }
523:
524: public String getListCommentsURL() {
525: return getPageUrl(pageName, WikiPageAction.LISTCOMMENT_ACTION
526: .getName());
527: }
528:
529: public String getListPresenceURL() {
530: return getPageUrl(pageName, WikiPageAction.LISTPRESENCE_ACTION
531: .getName());
532: }
533:
534: /**
535: * @return
536: */
537: public String getOpenPageChatURL() {
538: return getPageUrl(pageName, WikiPageAction.OPENPAGECHAT_ACTION
539: .getName());
540: }
541:
542: /**
543: * @return
544: */
545: public String getOpenSpaceChatURL() {
546: return getPageUrl(pageName, WikiPageAction.OPENSPACECHAT_ACTION
547: .getName());
548: }
549:
550: /**
551: * @return
552: */
553: public String getListPageChatURL() {
554: return getPageUrl(pageName, WikiPageAction.LISTPAGECHAT_ACTION
555: .getName());
556: }
557:
558: /**
559: * @return
560: */
561: public String getListSpaceChatURL() {
562: return getPageUrl(pageName, WikiPageAction.LISTSPACECHAT_ACTION
563: .getName());
564: }
565:
566: public String getBaseAccessUrl() {
567: return ServerConfigurationService.getAccessUrl()
568: + RWikiObjectService.REFERENCE_ROOT + pageName + ".";
569:
570: }
571:
572: public String getRssAccessUrl() {
573: return ServerConfigurationService.getAccessUrl()
574: + RWikiObjectService.REFERENCE_ROOT + getPageSpace()
575: + "/.20.rss";
576: }
577:
578: public String getPreferencesUrl() {
579: return this .getPageUrl(getPageName(),
580: WikiPageAction.PREFERENCES_ACTION.getName());
581: }
582:
583: /**
584: * Returns a print view URL with no breadcrumbs
585: *
586: * @return
587: */
588: public String getPrintViewUrl() {
589: return getPrintViewUrl(false);
590: }
591:
592: /**
593: * Returns a string representation of an url to perma view the current page
594: *
595: * @return url as string
596: */
597: public String getPrintViewUrl(boolean withBreadcrumbs) {
598: return getPageUrl(pageName, WikiPageAction.PRINTVIEW_ACTION
599: .getName(), withBreadcrumbs);
600: }
601:
602: }
|