KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.StringWriter JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Locale JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.directwebremoting.WebContext;
39 import org.directwebremoting.WebContextFactory;
40 import org.riotfamily.common.util.FormatUtils;
41 import org.riotfamily.common.web.util.CapturingResponseWrapper;
42 import org.riotfamily.common.web.util.ServletUtils;
43 import org.riotfamily.components.ComponentList;
44 import org.riotfamily.components.ComponentRepository;
45 import org.riotfamily.components.ComponentVersion;
46 import org.riotfamily.components.VersionContainer;
47 import org.riotfamily.components.config.ComponentListConfiguration;
48 import org.riotfamily.components.context.PageRequestUtils;
49 import org.riotfamily.components.context.RequestContextExpiredException;
50 import org.riotfamily.components.dao.ComponentDao;
51 import org.riotfamily.riot.security.AccessController;
52 import org.riotfamily.riot.security.session.LoginManager;
53 import org.springframework.context.MessageSource;
54 import org.springframework.context.MessageSourceAware;
55 import org.springframework.util.Assert;
56 import org.springframework.util.StringUtils;
57 import org.springframework.web.servlet.support.RequestContextUtils;
58
59 /**
60  * Service bean to edit ComponentLists and ComponentVersions.
61  */

62 public class ComponentEditorImpl implements ComponentEditor, MessageSourceAware {
63
64     private Log log = LogFactory.getLog(ComponentEditorImpl.class);
65
66     private ComponentDao dao;
67
68     private ComponentRepository repository;
69
70     private MessageSource messageSource;
71
72     private Map JavaDoc editorConfigs;
73
74     private boolean instantPublish = false;
75
76
77     public ComponentEditorImpl(ComponentDao dao, ComponentRepository repository) {
78         this.dao = dao;
79         this.repository = repository;
80     }
81
82     public void setMessageSource(MessageSource messageSource) {
83         this.messageSource = messageSource;
84     }
85
86     public Map JavaDoc getEditorConfigs() {
87         return this.editorConfigs;
88     }
89
90     public void setEditorConfigs(Map JavaDoc editorConfigs) {
91         this.editorConfigs = editorConfigs;
92     }
93
94     public void setInstantPublish(boolean instantPublish) {
95         this.instantPublish = instantPublish;
96     }
97
98     protected ComponentVersion getVersionToEdit(VersionContainer container) {
99         return dao.getOrCreateVersion(container, null, instantPublish);
100     }
101
102     protected ComponentVersion getVersionToDisplay(VersionContainer container) {
103         return instantPublish
104                 ? container.getLiveVersion()
105                 : container.getLatestVersion();
106     }
107
108     protected List JavaDoc getContainersToEdit(ComponentList componentList) {
109         return instantPublish
110                 ? componentList.getLiveContainers()
111                 : dao.getOrCreatePreviewContainers(componentList);
112     }
113
114     /**
115      * Returns the value of the given property.
116      */

117     public String JavaDoc getText(Long JavaDoc containerId, String JavaDoc property) {
118         VersionContainer container = dao.loadVersionContainer(containerId);
119         ComponentVersion version = getVersionToDisplay(container);
120         return version.getProperty(property);
121     }
122
123     /**
124      * Sets the given property to a new value and returns the updated HTML.
125      */

126     public void updateText(Long JavaDoc containerId, String JavaDoc property, String JavaDoc text) {
127         VersionContainer container = dao.loadVersionContainer(containerId);
128         ComponentVersion version = getVersionToEdit(container);
129         version.setProperty(property, text);
130         dao.updateComponentVersion(version);
131     }
132
133     /**
134      *
135      */

136     public void updateTextChunks(Long JavaDoc containerId, String JavaDoc property,
137             String JavaDoc[] chunks) {
138
139         log.debug("Inserting chunks " + StringUtils.arrayToCommaDelimitedString(chunks));
140         VersionContainer container = dao.loadVersionContainer(containerId);
141         ComponentVersion version = getVersionToEdit(container);
142         version.setProperty(property, chunks[0]);
143         dao.updateComponentVersion(version);
144
145         ComponentList list = container.getList();
146         int offset = getContainersToEdit(list).indexOf(container);
147         for (int i = 1; i < chunks.length; i++) {
148             insertComponent(list.getId(), offset + i, version.getType(),
149                     Collections.singletonMap(property, chunks[i]));
150         }
151     }
152
153     /**
154      * Returns a list of TypeInfo beans indicating which component types are
155      * valid for the given controller.
156      */

157     public List JavaDoc getValidTypes(String JavaDoc controllerId) {
158         ComponentListConfiguration cfg = repository.getListConfiguration(controllerId);
159         Assert.notNull(cfg, "No such controller: " + controllerId);
160         String JavaDoc[] types = cfg.getValidComponentTypes();
161
162         Locale JavaDoc locale = getLocale();
163         ArrayList JavaDoc result = new ArrayList JavaDoc();
164         for (int i = 0; i < types.length; i++) {
165             String JavaDoc id = types[i];
166             String JavaDoc description = messageSource.getMessage("component." + id,
167                     null, FormatUtils.xmlToTitleCase(id), locale);
168
169             result.add(new TypeInfo(id, description));
170         }
171         return result;
172     }
173
174     /**
175      * Creates a new VersionContainer and inserts it in the list identified
176      * by the given id.
177      */

178     public Long JavaDoc insertComponent(Long JavaDoc listId, int position, String JavaDoc type,
179             Map JavaDoc properties) {
180
181         ComponentList componentList = dao.loadComponentList(listId);
182         VersionContainer container = dao.insertContainer(componentList, type,
183                 properties, position, instantPublish);
184
185         return container.getId();
186     }
187
188     public void setType(Long JavaDoc containerId, String JavaDoc type) {
189         VersionContainer container = dao.loadVersionContainer(containerId);
190         ComponentVersion version = getVersionToEdit(container);
191         version.setType(type);
192         dao.updateComponentVersion(version);
193     }
194
195     private String JavaDoc getHtml(String JavaDoc url, Object JavaDoc key, boolean live)
196             throws RequestContextExpiredException {
197
198         try {
199             StringWriter JavaDoc sw = new StringWriter JavaDoc();
200             HttpServletRequest JavaDoc request = getWrappedRequest(key);
201             HttpServletResponse JavaDoc response = getCapturingResponse(sw);
202             EditModeUtils.include(request, response, url, live);
203             return sw.toString();
204
205         }
206         catch (RequestContextExpiredException e) {
207             throw e;
208         }
209         catch (Exception JavaDoc e) {
210             log.error("Error rendering component.", e);
211             throw new RuntimeException JavaDoc(e);
212         }
213     }
214
215     public String JavaDoc getLiveListHtml(String JavaDoc controllerId, Long JavaDoc listId)
216             throws RequestContextExpiredException {
217
218         Object JavaDoc contextKey = listId;
219         if (contextKey == null) {
220             contextKey = controllerId;
221         }
222         return getHtml(controllerId, contextKey, true);
223     }
224
225     public String JavaDoc getPreviewListHtml(String JavaDoc controllerId, Long JavaDoc listId)
226             throws RequestContextExpiredException {
227
228         Object JavaDoc contextKey = listId;
229         if (contextKey == null) {
230             contextKey = controllerId;
231         }
232         return getHtml(controllerId, contextKey, false);
233     }
234
235     public void moveComponent(Long JavaDoc containerId, Long JavaDoc nextContainerId) {
236         VersionContainer container = dao.loadVersionContainer(containerId);
237         ComponentList componentList = container.getList();
238         List JavaDoc containers = getContainersToEdit(componentList);
239         containers.remove(container);
240         if (nextContainerId != null) {
241             for (int i = 0; i < containers.size(); i++) {
242                 VersionContainer c = (VersionContainer) containers.get(i);
243                 if (c.getId().equals(nextContainerId)) {
244                     containers.add(i, container);
245                     break;
246                 }
247             }
248         }
249         else {
250             containers.add(container);
251         }
252         if (!instantPublish) {
253             componentList.setDirty(true);
254         }
255         dao.updateComponentList(componentList);
256     }
257
258     public void deleteComponent(Long JavaDoc containerId) {
259         VersionContainer container = dao.loadVersionContainer(containerId);
260         ComponentList componentList = container.getList();
261         List JavaDoc containers = getContainersToEdit(componentList);
262         containers.remove(container);
263         if (!instantPublish) {
264             componentList.setDirty(true);
265         }
266         dao.updateComponentList(componentList);
267         if (!componentList.getLiveContainers().contains(container)) {
268             dao.deleteVersionContainer(container);
269         }
270     }
271
272     public void publish(Long JavaDoc listId, Long JavaDoc[] containerIds) {
273         if (listId != null) {
274             publishList(listId);
275         }
276         if (containerIds != null) {
277             publishContainers(containerIds);
278         }
279     }
280
281     public void discard(Long JavaDoc listId, Long JavaDoc[] containerIds) {
282         if (listId != null) {
283             discardList(listId);
284         }
285         if (containerIds != null) {
286             discardContainers(containerIds);
287         }
288     }
289
290     /**
291      * Discards all changes made to the VersionContainers identified by the
292      * given IDs.
293      */

294     private void discardContainers(Long JavaDoc[] ids) {
295         for (int i = 0; i < ids.length; i++) {
296             VersionContainer container = dao.loadVersionContainer(ids[i]);
297             dao.discardContainer(container);
298         }
299     }
300
301     /**
302      * Discards all changes made to the ComponentList identified by the
303      * given ID.
304      */

305     private void discardList(Long JavaDoc listId) {
306         ComponentList componentList = dao.loadComponentList(listId);
307         dao.discardList(componentList);
308     }
309
310     /**
311      * Publishes all changes made to the VersionContainers identified by the
312      * given IDs.
313      */

314     private void publishContainers(Long JavaDoc[] ids) {
315         for (int i = 0; i < ids.length; i++) {
316             VersionContainer container = dao.loadVersionContainer(ids[i]);
317             dao.publishContainer(container);
318         }
319     }
320
321     /**
322      * Publishes the ComponentList identified by the given ID.
323      */

324     public void publishList(Long JavaDoc listId) {
325         ComponentList list = dao.loadComponentList(listId);
326         if (AccessController.isGranted("publish", list.getLocation())) {
327             dao.publishList(list);
328         }
329         else {
330             throw new RuntimeException JavaDoc(messageSource.getMessage(
331                 "components.error.publishNotGranted", null, getLocale()));
332         }
333     }
334
335     /**
336      * This method is invoked by the Riot toolbar to inform the server that
337      * the specified URL is still being edited.
338      */

339     public void keepAlive() {
340         WebContext ctx = WebContextFactory.get();
341         HttpServletRequest JavaDoc request = ctx.getHttpServletRequest();
342         String JavaDoc path = ServletUtils.getPath(ctx.getCurrentPage());
343         PageRequestUtils.touchContext(request, path);
344     }
345
346     /**
347      * Performs a logout.
348      */

349     public void logout() {
350         WebContext ctx = WebContextFactory.get();
351         LoginManager.logout(ctx.getHttpServletRequest(),
352                 ctx.getHttpServletResponse());
353     }
354
355     /* Utility methods */
356
357     private HttpServletRequest JavaDoc getWrappedRequest(Object JavaDoc key)
358             throws RequestContextExpiredException {
359
360         WebContext ctx = WebContextFactory.get();
361         HttpServletRequest JavaDoc request = ctx.getHttpServletRequest();
362         String JavaDoc path = ServletUtils.getPath(ctx.getCurrentPage());
363         return PageRequestUtils.wrapRequest(request, path, key);
364     }
365
366     private HttpServletResponse JavaDoc getCapturingResponse(StringWriter JavaDoc sw) {
367         WebContext ctx = WebContextFactory.get();
368         return new CapturingResponseWrapper(ctx.getHttpServletResponse(), sw);
369     }
370
371     private Locale JavaDoc getLocale() {
372         WebContext ctx = WebContextFactory.get();
373         HttpServletRequest JavaDoc request = ctx.getHttpServletRequest();
374         return RequestContextUtils.getLocale(request);
375     }
376
377 }
378
Popular Tags