001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/presentation/tool/src/java/org/theospi/portfolio/presentation/control/ViewPresentationControl.java $
003: * $Id:ViewPresentationControl.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 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 org.theospi.portfolio.presentation.control;
021:
022: import java.util.Hashtable;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: import javax.xml.transform.Transformer;
027: import javax.xml.transform.TransformerConfigurationException;
028: import javax.xml.transform.TransformerFactory;
029: import javax.xml.transform.URIResolver;
030: import javax.xml.transform.stream.StreamSource;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.jdom.Document;
035: import org.sakaiproject.exception.IdUnusedException;
036: import org.sakaiproject.metaobj.shared.ArtifactFinder;
037: import org.sakaiproject.metaobj.shared.ArtifactFinderManager;
038: import org.sakaiproject.metaobj.shared.mgt.HomeFactory;
039: import org.sakaiproject.metaobj.shared.model.Agent;
040: import org.sakaiproject.metaobj.shared.model.Id;
041: import org.sakaiproject.metaobj.shared.model.PersistenceException;
042: import org.sakaiproject.metaobj.utils.mvc.intf.LoadObjectController;
043: import org.sakaiproject.site.api.Site;
044: import org.sakaiproject.site.api.ToolConfiguration;
045: import org.sakaiproject.site.cover.SiteService;
046: import org.sakaiproject.tool.api.ToolSession;
047: import org.sakaiproject.tool.cover.SessionManager;
048: import org.sakaiproject.tool.cover.ToolManager;
049: import org.sakaiproject.util.Tool;
050: import org.springframework.validation.BindException;
051: import org.springframework.validation.Errors;
052: import org.springframework.web.servlet.ModelAndView;
053: import org.theospi.portfolio.presentation.PresentationFunctionConstants;
054: import org.theospi.portfolio.presentation.PresentationManager;
055: import org.theospi.portfolio.presentation.model.Presentation;
056: import org.theospi.portfolio.presentation.model.PresentationComment;
057: import org.theospi.portfolio.presentation.model.PresentationLog;
058: import org.theospi.portfolio.security.AuthorizationFacade;
059: import org.theospi.portfolio.shared.model.Node;
060: import org.theospi.portfolio.shared.model.OspException;
061:
062: /**
063: * Created by IntelliJ IDEA.
064: * User: John Ellis
065: * Date: May 25, 2004
066: * Time: 1:52:39 PM
067: * To change this template use File | Settings | File Templates.
068: */
069: public class ViewPresentationControl extends
070: AbstractPresentationController implements LoadObjectController {
071:
072: protected final Log logger = LogFactory.getLog(getClass());
073: private HomeFactory homeFactory = null;
074: private ArtifactFinder artifactFinder = null;
075: private AuthorizationFacade authzManager = null;
076: private ArtifactFinderManager artifactFinderManager;
077: private Hashtable presentationTemplateCache = new Hashtable();
078: private URIResolver uriResolver;
079:
080: public Object fillBackingObject(Object incomingModel, Map request,
081: Map session, Map application) throws Exception {
082: PresentationManager presentationManager = getPresentationManager();
083: Presentation presentation = (Presentation) incomingModel;
084: if (presentation.getSecretExportKey() != null) {
085: String secretExportKey = presentation.getSecretExportKey();
086: presentation = presentationManager.getPresentation(
087: presentation.getId(), secretExportKey);
088: presentation.setSecretExportKey(secretExportKey);
089: return presentation;
090: } else {
091: // if it exists, get the presentation from memory that is being edited
092: Presentation previewPres = (Presentation) session
093: .get("presentation");
094: if (previewPres != null
095: && previewPres.getId().getValue().equals(
096: presentation.getId().getValue())) {
097:
098: //side step any authz issues as the presentation only exists in the users session
099: previewPres.setIsPublic(true);
100: previewPres.setIsPreview(true);
101:
102: return previewPres;
103: }
104:
105: return getPresentationManager().getLightweightPresentation(
106: presentation.getId());
107: }
108: }
109:
110: public ModelAndView handleRequest(Object requestModel, Map request,
111: Map session, Map application, Errors errors) {
112: Presentation pres = (Presentation) requestModel;
113:
114: if (pres.getSecretExportKey() == null) {
115: if (!pres.getIsPublic()) {
116: if (getAuthManager().getAgent().isInRole(
117: Agent.ROLE_ANONYMOUS)) {
118: try {
119: Site site = SiteService.getSite(pres
120: .getSiteId());
121: ToolConfiguration toolConfig = site
122: .getToolForCommonId(PresentationFunctionConstants.PRES_TOOL_ID);
123: String placement = toolConfig.getId();
124: ToolSession ts = SessionManager
125: .getCurrentSession().getToolSession(
126: placement);
127: SessionManager.setCurrentToolSession(ts);
128:
129: SessionManager.getCurrentSession()
130: .setAttribute(Tool.HELPER_DONE_URL,
131: pres.getExternalUri());
132:
133: Map model = new Hashtable();
134: model.put("sakai.tool.placement.id", placement);
135: return new ModelAndView("authnRedirect", model);
136:
137: } catch (IdUnusedException e) {
138: logger.error("", e);
139: }
140: } else {
141:
142: getAuthzManager()
143: .checkPermission(
144: PresentationFunctionConstants.VIEW_PRESENTATION,
145: pres.getId());
146: }
147: }
148:
149: if (pres.isExpired()
150: && !pres.getOwner().getId().equals(
151: getAuthManager().getAgent().getId())) {
152: return new ModelAndView("expired");
153: }
154: }
155:
156: if (!pres.isPreview()) {
157: logViewedPresentation(pres);
158: }
159:
160: Hashtable model = new Hashtable();
161:
162: try {
163: model.put("presentation", pres);
164: Document doc = null;
165:
166: if (pres.getPresentationType().equals(
167: Presentation.TEMPLATE_TYPE))
168: doc = getPresentationManager().createDocument(pres);
169: else {
170: String page = (String) request.get("page");
171: if (pres.isPreview()) {
172: doc = getPresentationManager()
173: .getPresentationPreviewLayoutAsXml(pres,
174: page);
175: } else {
176: doc = getPresentationManager()
177: .getPresentationLayoutAsXml(pres, page);
178: }
179: }
180: Site site = SiteService.getSite(pres.getSiteId());
181: getAuthzManager().pushAuthzGroups(site.getId());
182: ToolConfiguration toolConfig = site
183: .getToolForCommonId(PresentationFunctionConstants.PRES_TOOL_ID);
184: String placement = toolConfig.getId();
185: model.put("placementId", placement);
186: if (doc != null)
187: model.put("document", doc);
188: else
189: return new ModelAndView("notFound", model);
190: model.put("renderer", getTransformer(pres, request));
191: model.put("uriResolver", getUriResolver());
192:
193: if (!getAuthManager().getAgent().isInRole(
194: Agent.ROLE_ANONYMOUS)) {
195: model.put("currentAgent", getAuthManager().getAgent());
196: }
197:
198: if (!pres.isPreview()) {
199: model.put("comments", getPresentationManager()
200: .getPresentationComments(pres.getId(),
201: getAuthManager().getAgent()));
202:
203: boolean allowComments = getAuthzManager()
204: .isAuthorized(
205: PresentationFunctionConstants.COMMENT_PRESENTATION,
206: pres.getId());
207: model.put("allowComments", allowComments);
208: } else {
209: model.put("allowComments", pres.isAllowComments());
210: }
211:
212: if (request.get(BindException.ERROR_KEY_PREFIX
213: + "newComment") == null) {
214: request.put(BindException.ERROR_KEY_PREFIX
215: + "newComment", new BindException(
216: new PresentationComment(), "newComment"));
217: }
218:
219: } catch (PersistenceException e) {
220: logger.error("", e);
221: throw new OspException(e);
222: } catch (IdUnusedException e) {
223: logger.error("", e);
224: }
225:
226: boolean headers = pres.getTemplate().isIncludeHeaderAndFooter();
227: String viewName = "withoutHeader";
228:
229: if (headers) {
230: if (ToolManager.getCurrentPlacement() == null) {
231: viewName = "withHeaderStandalone";
232: } else {
233: viewName = "withHeader";
234: }
235: }
236: return new ModelAndView(viewName, model);
237: }
238:
239: /**
240: * creates a new log that this presentation has been viewed
241: * @param pres
242: */
243: protected void logViewedPresentation(Presentation pres) {
244: PresentationLog log = new PresentationLog();
245: log.setPresentation(pres);
246: log.setViewDate(new java.util.Date());
247: log.setViewer(getAuthManager().getAgent());
248: getPresentationManager().storePresentationLog(log);
249: }
250:
251: // cache the template...
252: protected Transformer getTransformer(Presentation presentation,
253: Map request) throws PersistenceException {
254: Id renderer = presentation.getTemplate().getRenderer();
255: TransformerWrapper wrapper = (TransformerWrapper) presentationTemplateCache
256: .get(renderer);
257:
258: if (wrapper == null) {
259: wrapper = new TransformerWrapper();
260: wrapper.modified = 0;
261: }
262:
263: Node xsl = getPresentationManager().getNode(renderer);
264:
265: if (xsl.getTechnicalMetadata().getLastModified().getTime() > wrapper.modified) {
266: try {
267: wrapper.transformer = TransformerFactory.newInstance()
268: .newTransformer(
269: new StreamSource(xsl.getInputStream()));
270: wrapper.modified = xsl.getTechnicalMetadata()
271: .getLastModified().getTime();
272: } catch (TransformerConfigurationException e) {
273: throw new OspException(e);
274: }
275: }
276:
277: wrapper.transformer.clearParameters();
278:
279: //send request params in as transform params
280: for (Iterator i = request.keySet().iterator(); i.hasNext();) {
281: String paramName = (String) i.next();
282: wrapper.transformer.setParameter(paramName, request
283: .get(paramName));
284: }
285:
286: presentationTemplateCache.put(renderer, wrapper);
287:
288: return wrapper.transformer;
289: }
290:
291: private class TransformerWrapper {
292: public long modified;
293: public Transformer transformer;
294: }
295:
296: public HomeFactory getHomeFactory() {
297: return homeFactory;
298: }
299:
300: public void setHomeFactory(HomeFactory homeFactory) {
301: this .homeFactory = homeFactory;
302: }
303:
304: public ArtifactFinder getArtifactFinder() {
305: return artifactFinder;
306: }
307:
308: public void setArtifactFinder(ArtifactFinder artifactFinder) {
309: this .artifactFinder = artifactFinder;
310: }
311:
312: public AuthorizationFacade getAuthzManager() {
313: return authzManager;
314: }
315:
316: public void setAuthzManager(AuthorizationFacade authzManager) {
317: this .authzManager = authzManager;
318: }
319:
320: public ArtifactFinderManager getArtifactFinderManager() {
321: return artifactFinderManager;
322: }
323:
324: public void setArtifactFinderManager(
325: ArtifactFinderManager artifactFinderManager) {
326: this .artifactFinderManager = artifactFinderManager;
327: }
328:
329: public Hashtable getPresentationTemplateCache() {
330: return presentationTemplateCache;
331: }
332:
333: public void setPresentationTemplateCache(
334: Hashtable presentationTemplateCache) {
335: this .presentationTemplateCache = presentationTemplateCache;
336: }
337:
338: public URIResolver getUriResolver() {
339: return uriResolver;
340: }
341:
342: public void setUriResolver(URIResolver uriResolver) {
343: this.uriResolver = uriResolver;
344: }
345: }
|