KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > components > ComponentController


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) 2007
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.components;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.riotfamily.cachius.TaggingContext;
30 import org.riotfamily.cachius.spring.AbstractCacheableController;
31 import org.riotfamily.components.dao.ComponentDao;
32 import org.springframework.transaction.PlatformTransactionManager;
33 import org.springframework.transaction.TransactionStatus;
34 import org.springframework.transaction.support.TransactionCallback;
35 import org.springframework.transaction.support.TransactionTemplate;
36 import org.springframework.util.Assert;
37 import org.springframework.web.servlet.ModelAndView;
38
39 /**
40  * Controller that exposes the properties of the requested
41  * {@link ComponentVersion} to the model.
42  *
43  * @author Felix Gnass [fgnass at neteye dot de]
44  * @since 6.4
45  */

46 public class ComponentController extends AbstractCacheableController {
47
48     private PlatformTransactionManager transactionManager;
49
50     private ComponentDao componentDao;
51
52     private ComponentRepository componentRepository;
53
54     private String JavaDoc requiredType;
55
56     private String JavaDoc viewName;
57
58
59     public void setTransactionManager(PlatformTransactionManager transactionManager) {
60         this.transactionManager = transactionManager;
61     }
62
63     public ComponentDao getComponentDao() {
64         return this.componentDao;
65     }
66
67     public void setComponentDao(ComponentDao componentDao) {
68         this.componentDao = componentDao;
69     }
70
71     public ComponentRepository getComponentRepository() {
72         return this.componentRepository;
73     }
74
75     public void setComponentRepository(ComponentRepository componentRepository) {
76         this.componentRepository = componentRepository;
77     }
78
79     public void setRequiredType(String JavaDoc requiredType) {
80         this.requiredType = requiredType;
81     }
82
83     public void setViewName(String JavaDoc viewName) {
84         this.viewName = viewName;
85     }
86
87     protected Long JavaDoc getVersionId(HttpServletRequest JavaDoc request) {
88         String JavaDoc s = request.getParameter("id");
89         return s != null? new Long JavaDoc(s) : null;
90     }
91
92     protected void appendCacheKey(StringBuffer JavaDoc key,
93             HttpServletRequest JavaDoc request) {
94
95         super.appendCacheKey(key, request);
96         key.append("?id=").append(getVersionId(request));
97     }
98
99     public long getTimeToLive(HttpServletRequest JavaDoc request) {
100         return CACHE_ETERNALLY;
101     }
102
103     public ModelAndView handleRequest(final HttpServletRequest JavaDoc request,
104             final HttpServletResponse JavaDoc response) throws Exception JavaDoc {
105
106         return (ModelAndView) new TransactionTemplate(transactionManager)
107                 .execute(new TransactionCallback() {
108
109             public Object JavaDoc doInTransaction(TransactionStatus status) {
110                 return handleRequestInTransaction(request, response);
111             }
112         });
113     }
114
115     protected ModelAndView handleRequestInTransaction(
116             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
117
118         Long JavaDoc id = getVersionId(request);
119         ComponentVersion version = componentDao.loadComponentVersion(id);
120         Assert.notNull(version, "No such component: " + id);
121
122         ComponentList list = version.getContainer().getList();
123         TaggingContext.tag(request, list.getLocation().toString());
124
125         Component component = componentRepository.getComponent(version);
126
127         Assert.isTrue(requiredType == null ||
128                 version.getType().equals(requiredType),
129                 "Component must be of type " + requiredType);
130
131         return new ModelAndView(viewName, component.buildModel(version));
132     }
133
134 }
135
Popular Tags