001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/portal/tags/sakai_2-4-1/portal-impl/impl/src/java/org/sakaiproject/portal/charon/handlers/WorksiteHandler.java $
003: * $Id: WorksiteHandler.java 29143 2007-04-19 01:10:38Z ajpoland@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006, 2007 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 org.sakaiproject.portal.charon.handlers;
021:
022: import java.io.IOException;
023: import java.util.ArrayList;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028:
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031:
032: import org.sakaiproject.component.cover.ServerConfigurationService;
033: import org.sakaiproject.exception.IdUnusedException;
034: import org.sakaiproject.exception.PermissionException;
035: import org.sakaiproject.portal.api.Portal;
036: import org.sakaiproject.portal.api.PortalHandlerException;
037: import org.sakaiproject.portal.api.PortalRenderContext;
038: import org.sakaiproject.site.api.Site;
039: import org.sakaiproject.site.api.SitePage;
040: import org.sakaiproject.site.api.ToolConfiguration;
041: import org.sakaiproject.tool.api.Session;
042: import org.sakaiproject.tool.api.ToolException;
043: import org.sakaiproject.util.Web;
044:
045: /**
046: *
047: * @author ieb
048: * @since Sakai 2.4
049: * @version $Rev: 29143 $
050: *
051: */
052: public class WorksiteHandler extends PageHandler {
053: private static final String INCLUDE_WORKSITE = "include-worksite";
054:
055: private static final String INCLUDE_PAGE_NAV = "include-page-nav";
056:
057: public WorksiteHandler() {
058: urlFragment = "worksite";
059: }
060:
061: @Override
062: public int doPost(String[] parts, HttpServletRequest req,
063: HttpServletResponse res, Session session)
064: throws PortalHandlerException {
065: return doGet(parts, req, res, session);
066: }
067:
068: @Override
069: public int doGet(String[] parts, HttpServletRequest req,
070: HttpServletResponse res, Session session)
071: throws PortalHandlerException {
072: if ((parts.length >= 3) && (parts[1].equals("worksite"))) {
073: try {
074: // recognize an optional page/pageid
075: String pageId = null;
076: if ((parts.length == 5) && (parts[3].equals("page"))) {
077: pageId = parts[4];
078: }
079:
080: doWorksite(req, res, session, parts[2], pageId, req
081: .getContextPath()
082: + req.getServletPath());
083: return END;
084: } catch (Exception ex) {
085: throw new PortalHandlerException(ex);
086: }
087: } else {
088: return NEXT;
089: }
090: }
091:
092: public void doWorksite(HttpServletRequest req,
093: HttpServletResponse res, Session session, String siteId,
094: String pageId, String toolContextPath)
095: throws ToolException, IOException {
096: // if no page id, see if there was a last page visited for this site
097: if (pageId == null) {
098: pageId = (String) session
099: .getAttribute(Portal.ATTR_SITE_PAGE + siteId);
100: }
101:
102: // find the site, for visiting
103: Site site = null;
104: try {
105: site = siteHelper.getSiteVisit(siteId);
106: } catch (IdUnusedException e) {
107: portal.doError(req, res, session, Portal.ERROR_WORKSITE);
108: return;
109: } catch (PermissionException e) {
110: // if not logged in, give them a chance
111: if (session.getUserId() == null) {
112: portal.doLogin(req, res, session, req.getPathInfo(),
113: false);
114: } else {
115: portal
116: .doError(req, res, session,
117: Portal.ERROR_WORKSITE);
118: }
119: return;
120: }
121:
122: // find the page, or use the first page if pageId not found
123: SitePage page = site.getPage(pageId);
124: if (page == null) {
125: // List pages = site.getOrderedPages();
126: List pages = siteHelper.getPermittedPagesInOrder(site);
127: if (!pages.isEmpty()) {
128: page = (SitePage) pages.get(0);
129: }
130: }
131: if (page == null) {
132: portal.doError(req, res, session, Portal.ERROR_WORKSITE);
133: return;
134: }
135:
136: // store the last page visited
137: session.setAttribute(Portal.ATTR_SITE_PAGE + siteId, page
138: .getId());
139:
140: // form a context sensitive title
141: String title = ServerConfigurationService
142: .getString("ui.service")
143: + " : " + site.getTitle() + " : " + page.getTitle();
144:
145: // start the response
146: String siteType = portal.calcSiteType(siteId);
147: PortalRenderContext rcontext = portal.startPageContext(
148: siteType, title, site.getSkin(), req);
149:
150: includeWorksite(rcontext, res, req, session, site, page,
151: toolContextPath, "worksite");
152:
153: portal.includeBottom(rcontext);
154:
155: // end the response
156: portal.sendResponse(rcontext, res, "worksite", null);
157: }
158:
159: public void includeWorksite(PortalRenderContext rcontext,
160: HttpServletResponse res, HttpServletRequest req,
161: Session session, Site site, SitePage page,
162: String toolContextPath, String portalPrefix)
163: throws IOException {
164: if (rcontext.uses(INCLUDE_WORKSITE)) {
165:
166: // add the page navigation with presence
167: includePageNav(rcontext, req, session, site, page,
168: toolContextPath, portalPrefix);
169:
170: // add the page
171: includePage(rcontext, res, req, page, toolContextPath,
172: "content");
173: }
174:
175: }
176:
177: public void includePageNav(PortalRenderContext rcontext,
178: HttpServletRequest req, Session session, Site site,
179: SitePage page, String toolContextPath, String portalPrefix)
180: throws IOException {
181: if (rcontext.uses(INCLUDE_PAGE_NAV)) {
182:
183: includePageList(
184: rcontext,
185: req,
186: session,
187: site,
188: page,
189: toolContextPath,
190: portalPrefix,
191: /* doPages */true,
192: /* resetTools */"true"
193: .equals(ServerConfigurationService
194: .getString(Portal.CONFIG_AUTO_RESET)),
195: /* includeSummary */false);
196:
197: }
198:
199: }
200:
201: protected void includePageList(PortalRenderContext rcontext,
202: HttpServletRequest req, Session session, Site site,
203: SitePage page, String toolContextPath, String portalPrefix,
204: boolean doPages, boolean resetTools, boolean includeSummary)
205: throws IOException {
206: if (rcontext.uses(INCLUDE_PAGE_NAV)) {
207:
208: boolean loggedIn = session.getUserId() != null;
209:
210: String pageUrl = Web.returnUrl(req, "/"
211: + portalPrefix
212: + "/"
213: + Web
214: .escapeUrl(siteHelper
215: .getSiteEffectiveId(site))
216: + "/page/");
217: String toolUrl = Web.returnUrl(req, "/"
218: + portalPrefix
219: + "/"
220: + Web
221: .escapeUrl(siteHelper
222: .getSiteEffectiveId(site)));
223: if (resetTools) {
224: toolUrl = toolUrl + "/tool-reset/";
225: } else {
226: toolUrl = toolUrl + "/tool/";
227: }
228:
229: String pagePopupUrl = Web.returnUrl(req, "/page/");
230: boolean showHelp = ServerConfigurationService.getBoolean(
231: "display.help.menu", true);
232: String iconUrl = site.getIconUrlFull();
233: boolean published = site.isPublished();
234: String type = site.getType();
235:
236: rcontext
237: .put("pageNavPublished", Boolean.valueOf(published));
238: rcontext.put("pageNavType", type);
239: rcontext.put("pageNavIconUrl", iconUrl);
240: // rcontext.put("pageNavSitToolsHead",
241: // Web.escapeHtml(rb.getString("sit_toolshead")));
242:
243: // order the pages based on their tools and the tool order for the
244: // site type
245: // List pages = site.getOrderedPages();
246: List pages = siteHelper.getPermittedPagesInOrder(site);
247:
248: List<Map> l = new ArrayList<Map>();
249: for (Iterator i = pages.iterator(); i.hasNext();) {
250:
251: SitePage p = (SitePage) i.next();
252: List pTools = p.getTools();
253:
254: boolean current = (page != null
255: && p.getId().equals(page.getId()) && !p
256: .isPopUp());
257: String pagerefUrl = pageUrl + Web.escapeUrl(p.getId());
258: if (resetTools) {
259: pagerefUrl = pagerefUrl.replaceFirst("/"
260: + portalPrefix + "/", "/" + portalPrefix
261: + "-reset/");
262: }
263:
264: if (doPages || p.isPopUp()) {
265: Map<String, Object> m = new HashMap<String, Object>();
266: m.put("isPage", Boolean.valueOf(true));
267: m.put("current", Boolean.valueOf(current));
268: m.put("ispopup", Boolean.valueOf(p.isPopUp()));
269: m.put("pagePopupUrl", pagePopupUrl);
270: m.put("pageTitle", Web.escapeHtml(p.getTitle()));
271: m.put("jsPageTitle", Web.escapeJavascript(p
272: .getTitle()));
273: m.put("pageId", Web.escapeUrl(p.getId()));
274: m.put("jsPageId", Web.escapeJavascript(p.getId()));
275: m.put("pageRefUrl", pagerefUrl);
276: if (includeSummary)
277: siteHelper.summarizePage(m, site, p);
278: l.add(m);
279: continue;
280: }
281:
282: // Loop through the tools again and Unroll the tools
283: Iterator iPt = pTools.iterator();
284:
285: while (iPt.hasNext()) {
286: ToolConfiguration placement = (ToolConfiguration) iPt
287: .next();
288:
289: String toolrefUrl = toolUrl
290: + Web.escapeUrl(placement.getId());
291:
292: Map<String, Object> m = new HashMap<String, Object>();
293: m.put("isPage", Boolean.valueOf(false));
294: m.put("toolId", Web.escapeUrl(placement.getId()));
295: m.put("jsToolId", Web.escapeJavascript(placement
296: .getId()));
297: m.put("toolRegistryId", placement.getToolId());
298: m.put("toolTitle", Web.escapeHtml(placement
299: .getTitle()));
300: m.put("jsToolTitle", Web.escapeJavascript(placement
301: .getTitle()));
302: m.put("toolrefUrl", toolrefUrl);
303: l.add(m);
304: }
305:
306: }
307: rcontext.put("pageNavTools", l);
308:
309: String helpUrl = ServerConfigurationService
310: .getHelpUrl(null);
311: rcontext.put("pageNavShowHelp", Boolean.valueOf(showHelp));
312: rcontext.put("pageNavHelpUrl", helpUrl);
313:
314: // rcontext.put("pageNavSitContentshead",
315: // Web.escapeHtml(rb.getString("sit_contentshead")));
316:
317: // Handle Presense
318: boolean showPresence = ServerConfigurationService
319: .getBoolean("display.users.present", true);
320: String presenceUrl = Web.returnUrl(req, "/presence/"
321: + Web.escapeUrl(site.getId()));
322:
323: // rcontext.put("pageNavSitPresenceTitle",
324: // Web.escapeHtml(rb.getString("sit_presencetitle")));
325: // rcontext.put("pageNavSitPresenceFrameTitle",
326: // Web.escapeHtml(rb.getString("sit_presenceiframetit")));
327: rcontext.put("pageNavShowPresenceLoggedIn", Boolean
328: .valueOf(showPresence && loggedIn));
329: rcontext.put("pageNavPresenceUrl", presenceUrl);
330: }
331:
332: }
333:
334: }
|