1 24 package org.riotfamily.riot.editor.ui; 25 26 import java.io.StringWriter ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 33 import org.riotfamily.common.collection.FlatMap; 34 import org.riotfamily.common.util.ResourceUtils; 35 import org.riotfamily.common.web.view.freemarker.ResourceTemplateLoader; 36 import org.riotfamily.riot.editor.EditorConstants; 37 import org.riotfamily.riot.editor.EditorDefinition; 38 import org.riotfamily.riot.editor.EditorDefinitionUtils; 39 import org.riotfamily.riot.editor.EditorRepository; 40 import org.riotfamily.riot.editor.ListDefinition; 41 import org.riotfamily.riot.editor.ViewReference; 42 import org.riotfamily.riot.list.ui.ListService; 43 import org.riotfamily.riot.list.ui.ListSession; 44 import org.springframework.beans.factory.InitializingBean; 45 import org.springframework.context.ResourceLoaderAware; 46 import org.springframework.core.io.ResourceLoader; 47 import org.springframework.util.Assert; 48 import org.springframework.web.servlet.ModelAndView; 49 import org.springframework.web.servlet.mvc.Controller; 50 51 import freemarker.template.Configuration; 52 53 public class ViewController implements Controller, 54 ResourceLoaderAware, InitializingBean { 55 56 private EditorRepository editorRepository; 57 58 private ResourceLoader resourceLoader; 59 60 private ListService listService; 61 62 private String viewName = ResourceUtils.getPath( 63 ViewController.class, "ReadOnlyView.ftl"); 64 65 private Configuration configuration; 66 67 public ViewController(EditorRepository repository, 68 ListService listService) { 69 70 this.editorRepository = repository; 71 this.listService = listService; 72 } 73 74 public void setViewName(String viewName) { 75 this.viewName = viewName; 76 } 77 78 public void setResourceLoader(ResourceLoader resourceLoader) { 79 this.resourceLoader = resourceLoader; 80 } 81 82 public void afterPropertiesSet() throws Exception { 83 configuration = new Configuration(); 84 ResourceTemplateLoader loader = new ResourceTemplateLoader(); 85 loader.setResourceLoader(resourceLoader); 86 configuration.setTemplateLoader(loader); 87 } 88 89 public final ModelAndView handleRequest(HttpServletRequest request, 90 HttpServletResponse response) throws Exception { 91 92 String editorId = (String ) request.getAttribute(EditorConstants.EDITOR_ID); 93 Assert.notNull(editorId, "No editorId in request scope"); 94 95 String objectId = (String ) request.getAttribute(EditorConstants.OBJECT_ID); 96 Assert.notNull(objectId, "No objectId in request scope"); 97 98 EditorDefinition editorDef = editorRepository.getEditorDefinition(editorId); 99 Assert.notNull(editorDef, "No such EditorDefinition: " + editorId); 100 101 StringWriter sw = new StringWriter (); 102 103 Object object = EditorDefinitionUtils.loadBean(editorDef, objectId); 104 Map model = new FlatMap(); 105 model.put("object", object); 106 model.put("request", request); 107 try { 108 String template = ((ViewReference) editorDef).getTemplate(); 109 configuration.getTemplate(template).process(model, sw); 110 } 111 catch (Exception e) { 112 throw new RuntimeException (e); 113 } 114 115 model = new HashMap (); 116 model.put(EditorConstants.EDITOR_ID, editorId); 117 model.put(EditorConstants.OBJECT_ID, objectId); 118 model.put("form", sw.toString()); 119 120 ListDefinition parentListDef = EditorDefinitionUtils 121 .getParentListDefinition(editorDef); 122 123 if (parentListDef != null) { 124 ListSession session = listService.getOrCreateListSession( 125 parentListDef.getId(), 126 EditorDefinitionUtils.getParentId(editorDef, object), 127 null, request); 128 129 model.put("listKey", session.getKey()); 130 } 131 132 return new ModelAndView(viewName, model); 133 } 134 135 public static String getUrl(String editorId, String objectId) { 136 StringBuffer sb = new StringBuffer (); 137 sb.append("/view/").append(editorId); 138 if (objectId != null) { 139 sb.append('/').append(objectId); 140 } 141 return sb.toString(); 142 } 143 144 } 145 | Popular Tags |