001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/common/tool-lib/src/java/org/theospi/portfolio/shared/control/XmlElementView.java $
003: * $Id:XmlElementView.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.shared.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.Template;
030: import org.apache.velocity.VelocityContext;
031: import org.apache.velocity.app.VelocityEngine;
032: import org.apache.velocity.exception.ResourceNotFoundException;
033: import org.sakaiproject.metaobj.shared.control.EditedArtifactStorage;
034: import org.sakaiproject.metaobj.shared.control.SchemaBean;
035: import org.sakaiproject.metaobj.shared.mgt.HomeFactory;
036: import org.sakaiproject.metaobj.shared.mgt.home.StructuredArtifactHomeInterface;
037: import org.sakaiproject.metaobj.shared.model.StructuredArtifact;
038: import org.sakaiproject.metaobj.utils.mvc.impl.TemplateJstlView;
039: import org.sakaiproject.metaobj.utils.mvc.intf.VelocityEngineFactory;
040: import org.sakaiproject.metaobj.utils.xml.SchemaNode;
041: import org.springframework.beans.BeansException;
042: import org.springframework.beans.factory.BeanFactoryUtils;
043: import org.springframework.beans.factory.NoSuchBeanDefinitionException;
044: import org.springframework.context.ApplicationContextException;
045:
046: /**
047: * Created by IntelliJ IDEA.
048: * User: John Ellis
049: * Date: Apr 20, 2004
050: * Time: 3:32:34 PM
051: * To change this template use File | Settings | File Templates.
052: */
053: public class XmlElementView extends TemplateJstlView {
054:
055: private String templateName = "";
056: private VelocityEngine velocityEngine;
057: private Template template;
058: private String baseUrl = null;
059:
060: public void render(Map model, HttpServletRequest request,
061: HttpServletResponse response) throws Exception {
062: setBody(prepareTemplateForRendering(model, request, response));
063: super .render(model, request, response);
064: }
065:
066: /**
067: * Prepare for rendering, and determine the request dispatcher path
068: * to forward to respectively to include.
069: * <p>This implementation simply returns the configured URL.
070: * Subclasses can override this to determine a resource to render,
071: * typically interpreting the URL in a different manner.
072: *
073: * @param request current HTTP request
074: * @param response current HTTP response
075: * @return the request dispatcher path to use
076: * @throws Exception if preparations failed
077: * @see #getUrl
078: * @see org.springframework.web.servlet.view.tiles.TilesView#prepareForRendering
079: */
080: protected String prepareTemplateForRendering(Map model,
081: HttpServletRequest request, HttpServletResponse response)
082: throws Exception {
083: HomeFactory factory = (HomeFactory) getWebApplicationContext()
084: .getBean("homeFactory");
085:
086: SchemaNode schema = null;
087: StructuredArtifactHomeInterface home = null;
088: String schemaName = null;
089: schemaName = request.getParameter("schema");
090: if (schemaName == null) {
091:
092: schemaName = (String) request.getAttribute("schema");
093:
094: if (schemaName == null) {
095: schemaName = request.getParameter("artifactType");
096: }
097: }
098:
099: home = (StructuredArtifactHomeInterface) factory
100: .getHome(schemaName);
101:
102: if (request
103: .getAttribute(EditedArtifactStorage.STORED_ARTIFACT_FLAG) != null) {
104: EditedArtifactStorage sessionBean = (EditedArtifactStorage) request
105: .getSession()
106: .getAttribute(
107: EditedArtifactStorage.EDITED_ARTIFACT_STORAGE_SESSION_KEY);
108:
109: if (!(sessionBean.getCurrentElement() instanceof StructuredArtifact)) {
110: home = (StructuredArtifactHomeInterface) sessionBean
111: .getHome();
112: schema = sessionBean.getCurrentSchemaNode();
113: schemaName += "." + sessionBean.getCurrentPath();
114: }
115: }
116:
117: String baseFile = getBaseUrl() + "_" + schemaName;
118:
119: File genFile = new File(getWebApplicationContext()
120: .getServletContext().getRealPath(""), baseFile + ".jsp");
121: File customFile = new File(getWebApplicationContext()
122: .getServletContext().getRealPath(""), baseFile
123: + "_custom.jsp");
124:
125: if (customFile.exists()) {
126: return baseFile + "_custom.jsp";
127: }
128:
129: if (genFile.exists()) {
130: if (genFile.lastModified() > home.getModified().getTime()
131: && genFile.lastModified() > getVelocityTemplate()
132: .getLastModified()) {
133: return baseFile + ".jsp";
134: }
135: }
136:
137: return createJspFromTemplate(home, baseFile + ".jsp", genFile,
138: schemaName, schema);
139: }
140:
141: protected String createJspFromTemplate(
142: StructuredArtifactHomeInterface home, String resultFile,
143: File jspFile, String schemaName, SchemaNode schema)
144: throws Exception {
145: VelocityContext context = new VelocityContext();
146:
147: if (schema != null) {
148: context.put("schema", new SchemaBean(schema, home
149: .getRootNode(), null, home.getType()
150: .getDescription()));
151: } else {
152: context.put("schema", new SchemaBean(home.getRootNode(),
153: home.getSchema(), schemaName, home.getType()
154: .getDescription()));
155: }
156:
157: context.put("instruction", home.getInstruction());
158:
159: FileWriter output = new FileWriter(jspFile);
160:
161: getVelocityTemplate().merge(context, output);
162:
163: output.close();
164:
165: return resultFile;
166: }
167:
168: /**
169: * Invoked on startup. Looks for a single VelocityConfig bean to
170: * find the relevant VelocityEngine for this factory.
171: */
172: protected void initApplicationContext() throws BeansException {
173: super .initApplicationContext();
174:
175: if (this .velocityEngine == null) {
176: try {
177: VelocityEngineFactory velocityConfig = (VelocityEngineFactory) BeanFactoryUtils
178: .beanOfTypeIncludingAncestors(
179: getApplicationContext(),
180: VelocityEngineFactory.class, true, true);
181: this .velocityEngine = velocityConfig
182: .getVelocityEngine();
183: } catch (NoSuchBeanDefinitionException ex) {
184: throw new ApplicationContextException(
185: "Must define a single VelocityConfig bean in this web application "
186: + "context (may be inherited): VelocityConfigurer is the usual implementation. "
187: + "This bean may be given any name.",
188: ex);
189: }
190: }
191:
192: try {
193: // check that we can get the template, even if we might subsequently get it again
194: this .template = getVelocityTemplate();
195: } catch (ResourceNotFoundException ex) {
196: throw new ApplicationContextException(
197: "Cannot find Velocity template for URL ["
198: + getBaseUrl()
199: + "]: Did you specify the correct resource loader path?",
200: ex);
201: } catch (Exception ex) {
202: throw new ApplicationContextException(
203: "Cannot load Velocity template for URL ["
204: + getBaseUrl() + "]", ex);
205: }
206: }
207:
208: /**
209: * Retrieve the Velocity template.
210: *
211: * @return the Velocity template to process
212: * @throws Exception if thrown by Velocity
213: */
214: protected Template getVelocityTemplate() throws Exception {
215: return this .velocityEngine.getTemplate(templateName);
216: }
217:
218: public String getTemplateName() {
219: return templateName;
220: }
221:
222: public void setTemplateName(String templateName) {
223: this .templateName = templateName;
224: }
225:
226: public String getBaseUrl() {
227: return baseUrl;
228: }
229:
230: public void setBaseUrl(String baseUrl) {
231: this.baseUrl = baseUrl;
232: }
233: }
|