001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/presentation/tool/src/java/org/theospi/portfolio/presentation/control/TemplateBuilderController.java $
003: * $Id:TemplateBuilderController.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.IOException;
023: import java.util.ArrayList;
024: import java.util.Collection;
025: import java.util.HashMap;
026: import java.util.Iterator;
027: import java.util.Map;
028:
029: import org.sakaiproject.metaobj.shared.mgt.HomeFactory;
030: import org.sakaiproject.metaobj.shared.mgt.ReadableObjectHome;
031: import org.sakaiproject.metaobj.shared.mgt.WritableObjectHome;
032: import org.sakaiproject.metaobj.shared.mgt.home.StructuredArtifactHomeInterface;
033: import org.sakaiproject.metaobj.shared.model.Id;
034: import org.sakaiproject.metaobj.shared.model.PersistenceException;
035: import org.sakaiproject.metaobj.utils.mvc.intf.LoadObjectController;
036: import org.sakaiproject.metaobj.utils.xml.SchemaNode;
037: import org.springframework.validation.Errors;
038: import org.springframework.web.servlet.ModelAndView;
039: import org.theospi.portfolio.presentation.model.PresentationItemDefinition;
040: import org.theospi.portfolio.presentation.model.PresentationTemplate;
041: import org.theospi.portfolio.presentation.model.TemplateFileRef;
042:
043: public class TemplateBuilderController extends
044: AbstractPresentationController implements LoadObjectController {
045: private WritableObjectHome fileArtifactHome;
046: private HomeFactory homeFactory;
047:
048: public ModelAndView handleRequest(Object requestModel, Map request,
049: Map session, Map application, Errors errors) {
050: PresentationTemplate template = (PresentationTemplate) requestModel;
051: try {
052: // TODO: 20050810 ContentHosting
053: //FileArtifact artifact = (FileArtifact) getFileArtifactHome().load(template.getRenderer());
054: //artifact.setSize(template.getMarkup().getBytes().length);
055: //artifact.setFile(new ByteArrayInputStream(prepareBody(template.getMarkup()).getBytes()));
056: //getFileArtifactHome().store(artifact);
057: getPresentationManager().storeTemplate(template);
058: Map params = new HashMap();
059: params.put("id", template.getId().getValue());
060: params.put("_target1", "1");
061: params.put("formSubmission", "true");
062: return new ModelAndView("success", params);
063: } catch (PersistenceException e) {
064: //TODO is this right ?
065: errors.reject("markup", e.getMessage());
066: return new ModelAndView("failure");
067: }
068: }
069:
070: public ModelAndView processCancel(Map request, Map session,
071: Map application, Object command, Errors errors)
072: throws Exception {
073: PresentationTemplate template = (PresentationTemplate) command;
074: Map params = new HashMap();
075: params.put("id", template.getId().getValue());
076: params.put("_target1", "1");
077: params.put("formSubmission", "true");
078: return new ModelAndView("success", params);
079: }
080:
081: protected String prepareBody(String body) {
082: StringBuffer buffer = new StringBuffer();
083: buffer
084: .append("<?xml version=\"1.0\" ?>\n"
085: + "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n"
086: + "\t\n\t<xsl:template match=\"ospiPresentation\">");
087: buffer.append(body.replaceAll("\\$\\{(.*?)\\}",
088: "<xsl:copy-of select=\"$1\"/>"));
089: buffer.append("\t</xsl:template>\n\n</xsl:stylesheet>");
090: return buffer.toString();
091: }
092:
093: public Object fillBackingObject(Object incomingModel, Map request,
094: Map session, Map application) throws Exception {
095: PresentationTemplate template = (PresentationTemplate) incomingModel;
096: template = getPresentationManager().getPresentationTemplate(
097: template.getId());
098:
099: Map elements = new HashMap();
100:
101: for (Iterator i = template.getSortedItems().iterator(); i
102: .hasNext();) {
103: PresentationItemDefinition itemDef = (PresentationItemDefinition) i
104: .next();
105: elements.put(itemDef.getName(), findPaths(itemDef));
106: }
107:
108: request.put("elements", elements);
109:
110: Map images = new HashMap();
111:
112: for (Iterator i = template.getFiles().iterator(); i.hasNext();) {
113: TemplateFileRef fileRef = (TemplateFileRef) i.next();
114: // TODO: 20050810 ContentHosting
115: //if (fileRef.getFileArtifact() instanceof FileArtifact){
116: // FileArtifact fileArtifact = (FileArtifact)fileRef.getFileArtifact();
117: // if (fileArtifact.getMimeType().getPrimaryType().equals("image")){
118: // images.put(fileArtifact.getDisplayName(),fileArtifact.getExternalUri());
119: // }
120: //}
121: }
122:
123: session.put("images", images);
124:
125: return template;
126: }
127:
128: /**
129: * places ${ } around each path
130: * @param paths
131: * @return
132: */
133: protected Collection tagPaths(Collection paths) {
134: Collection taggedPaths = new ArrayList();
135: for (Iterator i = paths.iterator(); i.hasNext();) {
136: taggedPaths.add("${" + i.next() + "}");
137: }
138: return taggedPaths;
139: }
140:
141: /**
142: * creates collection of possible xpaths associated with the rendered xml for this item definition
143: * @param itemDef
144: * @return
145: */
146: protected Collection findPaths(PresentationItemDefinition itemDef) {
147: ReadableObjectHome home = getHomeFactory().getHome(
148: itemDef.getType());
149: Collection paths = new ArrayList();
150:
151: paths.add(itemDef.getName() + "/artifact/metaData/id");
152: paths.add(itemDef.getName() + "/artifact/metaData/displayName");
153: paths.add(itemDef.getName() + "/artifact/metaData/type/id");
154: paths.add(itemDef.getName()
155: + "/artifact/metaData/type/description");
156:
157: if (home instanceof StructuredArtifactHomeInterface) {
158: StructuredArtifactHomeInterface structuredArtifactHome = (StructuredArtifactHomeInterface) home;
159: addPath(paths, structuredArtifactHome.getRootSchema(),
160: itemDef.getName() + "/artifact/structuredData");
161: } else if (home != null
162: && home.getType().getId().equals(
163: getFileArtifactHome().getType().getId())) {
164: //TODO deal with technical metadata
165: paths.add(itemDef.getName() + "/artifact/fileArtifact/uri");
166: }
167: return tagPaths(paths);
168: }
169:
170: /**
171: * recursively finds all possible xpaths for given schema
172: * @param paths
173: * @param node
174: * @param parent
175: */
176: protected void addPath(Collection paths, SchemaNode node,
177: String parent) {
178: String path = parent + "/" + node.getName();
179: paths.add(path);
180: if (node.getChildren() != null && node.getChildren().size() > 0) {
181: for (Iterator i = node.getChildren().iterator(); i
182: .hasNext();) {
183: addPath(paths, (SchemaNode) i.next(), path);
184: }
185: }
186: }
187:
188: /**
189: * loads contents of a file into a string
190: * @param fileId
191: * @return
192: */
193: protected String loadContents(Id fileId) throws IOException,
194: PersistenceException {
195: // TODO: 20050810 ContentHosting
196: //FileArtifact artifact = (FileArtifact)getFileArtifactHome().load(fileId);
197: //BufferedReader reader = new BufferedReader(new InputStreamReader(artifact.getFile()));
198: StringBuffer buffer = new StringBuffer();
199: //String line;
200: //while ((line = reader.readLine()) != null){
201: // if (line == null) break;
202: // buffer.append(line + "\n");
203: //}
204: return buffer.toString();
205: }
206:
207: public WritableObjectHome getFileArtifactHome() {
208: return fileArtifactHome;
209: }
210:
211: public void setFileArtifactHome(WritableObjectHome fileArtifactHome) {
212: this .fileArtifactHome = fileArtifactHome;
213: }
214:
215: public HomeFactory getHomeFactory() {
216: return homeFactory;
217: }
218:
219: public void setHomeFactory(HomeFactory homeFactory) {
220: this.homeFactory = homeFactory;
221: }
222: }
|