KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > editor > ui > ViewController


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.riot.editor.ui;
25
26 import java.io.StringWriter JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
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 JavaDoc 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 JavaDoc 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 JavaDoc {
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 JavaDoc request,
90             HttpServletResponse JavaDoc response) throws Exception JavaDoc {
91
92         String JavaDoc editorId = (String JavaDoc) request.getAttribute(EditorConstants.EDITOR_ID);
93         Assert.notNull(editorId, "No editorId in request scope");
94
95         String JavaDoc objectId = (String JavaDoc) 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 JavaDoc sw = new StringWriter JavaDoc();
102
103         Object JavaDoc object = EditorDefinitionUtils.loadBean(editorDef, objectId);
104         Map JavaDoc model = new FlatMap();
105         model.put("object", object);
106         model.put("request", request);
107         try {
108             String JavaDoc template = ((ViewReference) editorDef).getTemplate();
109             configuration.getTemplate(template).process(model, sw);
110         }
111         catch (Exception JavaDoc e) {
112             throw new RuntimeException JavaDoc(e);
113         }
114
115         model = new HashMap JavaDoc();
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 JavaDoc getUrl(String JavaDoc editorId, String JavaDoc objectId) {
136         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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