001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/presentation/tool/src/java/org/theospi/portfolio/presentation/control/PresentationPropertyFileView.java $
003: * $Id:PresentationPropertyFileView.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.io.File;
023: import java.io.FileWriter;
024: import java.util.Map;
025:
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028:
029: import org.apache.velocity.VelocityContext;
030: import org.sakaiproject.metaobj.shared.control.SchemaBean;
031: import org.sakaiproject.metaobj.utils.xml.SchemaNode;
032: import org.theospi.portfolio.presentation.model.Presentation;
033: import org.theospi.portfolio.shared.control.XmlElementView;
034: import org.theospi.portfolio.shared.model.TechnicalMetadata;
035:
036: /**
037: * Created by IntelliJ IDEA.
038: * User: John Ellis
039: * Date: Apr 20, 2004
040: * Time: 3:32:34 PM
041: * To change this template use File | Settings | File Templates.
042: */
043: public class PresentationPropertyFileView extends XmlElementView {
044:
045: /**
046: * Prepare for rendering, and determine the request dispatcher path
047: * to forward to respectively to include.
048: * <p>This implementation simply returns the configured URL.
049: * Subclasses can override this to determine a resource to render,
050: * typically interpreting the URL in a different manner.
051: *
052: * @param request current HTTP request
053: * @param response current HTTP response
054: * @return the request dispatcher path to use
055: * @throws Exception if preparations failed
056: * @see #getUrl
057: * @see org.springframework.web.servlet.view.tiles.TilesView#prepareForRendering
058: */
059: protected String prepareTemplateForRendering(Map model,
060: HttpServletRequest request, HttpServletResponse response)
061: throws Exception {
062: Presentation presentation = (Presentation) model
063: .get("presentation");
064: TechnicalMetadata propertyFileMetadata = (TechnicalMetadata) model
065: .get("propertyFileMetadata");
066:
067: String baseFile = getBaseUrl() + "_"
068: + presentation.getTemplate().getId().toString();
069:
070: File genFile = new File(getWebApplicationContext()
071: .getServletContext().getRealPath(""), baseFile + ".jsp");
072: File customFile = new File(getWebApplicationContext()
073: .getServletContext().getRealPath(""), baseFile
074: + "_custom.jsp");
075:
076: if (customFile.exists()) {
077: return baseFile + "_custom.jsp";
078: }
079:
080: //update jsp only if xsd or velocity template has changed or genFile doesn't exist
081: if (!genFile.exists()
082: || (genFile.lastModified() > propertyFileMetadata
083: .getLastModified().getTime() && genFile
084: .lastModified() > getVelocityTemplate()
085: .getLastModified())) {
086: return createJspFromTemplate(presentation, baseFile
087: + ".jsp", genFile);
088: }
089:
090: return baseFile + ".jsp";
091: }
092:
093: protected String createJspFromTemplate(Presentation presentation,
094: String resultFile, File jspFile) throws Exception {
095: VelocityContext context = new VelocityContext();
096:
097: SchemaNode schema = presentation.getProperties()
098: .getCurrentSchema();
099: context.put("schema", new SchemaBean(schema, presentation
100: .getTemplate().getDocumentRoot(), null, presentation
101: .getTemplate().getDescription()));
102:
103: FileWriter output = new FileWriter(jspFile);
104:
105: getVelocityTemplate().merge(context, output);
106:
107: output.close();
108:
109: return resultFile;
110: }
111: }
|