KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > components > editor > ComponentFormController


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.components.editor;
25
26 import java.util.Map JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29
30 import org.riotfamily.components.Component;
31 import org.riotfamily.components.ComponentRepository;
32 import org.riotfamily.components.ComponentVersion;
33 import org.riotfamily.components.VersionContainer;
34 import org.riotfamily.components.dao.ComponentDao;
35 import org.riotfamily.forms.factory.FormRepository;
36 import org.springframework.web.bind.ServletRequestUtils;
37
38 /**
39  * Controller that displays a form to edit the properties of a ComponentVersion.
40  *
41  * @author Felix Gnass [fgnass at neteye dot de]
42  * @since 6.5
43  */

44 public class ComponentFormController extends AbstractComponentFormController {
45
46     private ComponentDao componentDao;
47
48     private ComponentRepository componentRepository;
49
50     private String JavaDoc containerIdAttribute = "containerId";
51
52     public ComponentFormController(FormRepository formRepository,
53             ComponentRepository componentRepository,
54             ComponentDao componentDao) {
55
56         super(formRepository);
57         this.componentRepository = componentRepository;
58         this.componentDao = componentDao;
59     }
60
61     protected ComponentVersion getVersion(HttpServletRequest JavaDoc request) {
62         Long JavaDoc id = new Long JavaDoc((String JavaDoc) request.getAttribute(containerIdAttribute));
63         VersionContainer container = componentDao.loadVersionContainer(id);
64         boolean live = ServletRequestUtils.getBooleanParameter(request, "live", false);
65         return componentDao.getOrCreateVersion(container, null, live);
66     }
67
68     protected Object JavaDoc getFormBackingObject(HttpServletRequest JavaDoc request) {
69         ComponentVersion version = getVersion(request);
70         Component component = componentRepository.getComponent(version);
71         return component.buildModel(version);
72     }
73
74     protected void onSave(Object JavaDoc object, HttpServletRequest JavaDoc request) {
75         ComponentVersion version = getVersion(request);
76         Component component = componentRepository.getComponent(version);
77         Map JavaDoc properties = (Map JavaDoc) object;
78         component.updateProperties(version, properties);
79         componentDao.updateComponentVersion(version);
80     }
81
82 }
Popular Tags