KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > language > LanguageSelectionPlugin


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

31 package org.blojsom.plugin.language;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.blojsom.blog.Blog;
36 import org.blojsom.blog.Entry;
37 import org.blojsom.event.Event;
38 import org.blojsom.event.EventBroadcaster;
39 import org.blojsom.event.Listener;
40 import org.blojsom.plugin.Plugin;
41 import org.blojsom.plugin.PluginException;
42 import org.blojsom.plugin.admin.event.ProcessEntryEvent;
43 import org.blojsom.util.BlojsomUtils;
44
45 import javax.servlet.http.HttpServletRequest JavaDoc;
46 import javax.servlet.http.HttpServletResponse JavaDoc;
47 import java.util.Map JavaDoc;
48 import java.util.TreeMap JavaDoc;
49
50 /**
51  * Language selection plugin allows you to attach a language attribute to a blog entry.
52  *
53  * @author David Czarnecki
54  * @since blojsom 3.0
55  * @version $Id: LanguageSelectionPlugin.java,v 1.2 2006/03/20 22:50:41 czarneckid Exp $
56  */

57 public class LanguageSelectionPlugin implements Plugin, Listener {
58
59     private Log _logger = LogFactory.getLog(LanguageSelectionPlugin.class);
60
61     private static final String JavaDoc LANGUAGE_SELECTION_TEMPLATE = "org/blojsom/plugin/language/templates/admin-language-selection.vm";
62
63     private static final String JavaDoc BLOJSOM_JVM_LANGUAGES = "BLOJSOM_JVM_LANGUAGES";
64     private static final String JavaDoc BLOJSOM_PLUGIN_CURRENT_LANGUAGE_SELECTION = "BLOJSOM_PLUGIN_CURRENT_LANGUAGE_SELECTION";
65     private static final String JavaDoc METADATA_LANGUAGE = "language";
66
67     private EventBroadcaster _eventBroadcaster;
68
69     /**
70      * Create a new instance of the language selection plugin
71      */

72     public LanguageSelectionPlugin() {
73     }
74
75     /**
76      * Set the {@link org.blojsom.event.EventBroadcaster} event broadcaster
77      *
78      * @param eventBroadcaster {@link org.blojsom.event.EventBroadcaster}
79      */

80     public void setEventBroadcaster(EventBroadcaster eventBroadcaster) {
81         _eventBroadcaster = eventBroadcaster;
82     }
83
84     /**
85      * Initialize this plugin. This method only called when the plugin is instantiated.
86      *
87      * @throws org.blojsom.plugin.PluginException
88      * If there is an error initializing the plugin
89      */

90     public void init() throws PluginException {
91         _eventBroadcaster.addListener(this);
92     }
93
94     /**
95      * Process the blog entries
96      *
97      * @param httpServletRequest Request
98      * @param httpServletResponse Response
99      * @param blog {@link Blog} instance
100      * @param context Context
101      * @param entries Blog entries retrieved for the particular request
102      * @return Modified set of blog entries
103      * @throws PluginException If there is an error processing the blog entries
104      */

105     public Entry[] process(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Blog blog, Map JavaDoc context, Entry[] entries) throws PluginException {
106         return entries;
107     }
108
109     /**
110      * Perform any cleanup for the plugin. Called after {@link #process}.
111      *
112      * @throws org.blojsom.plugin.PluginException
113      * If there is an error performing cleanup for this plugin
114      */

115     public void cleanup() throws PluginException {
116     }
117
118     /**
119      * Called when BlojsomServlet is taken out of service
120      *
121      * @throws org.blojsom.plugin.PluginException
122      * If there is an error in finalizing this plugin
123      */

124     public void destroy() throws PluginException {
125     }
126
127     /**
128      * Handle an event broadcast from another component
129      *
130      * @param event {@link org.blojsom.event.Event} to be handled
131      */

132     public void handleEvent(Event event) {
133     }
134
135     /**
136      * Process an event from another component
137      *
138      * @param event {@link org.blojsom.event.Event} to be handled
139      */

140     public void processEvent(Event event) {
141         if (event instanceof ProcessEntryEvent) {
142             if (_logger.isDebugEnabled()) {
143                 _logger.debug("Handling process blog entry event");
144             }
145             ProcessEntryEvent processBlogEntryEvent = (ProcessEntryEvent) event;
146
147             String JavaDoc language = BlojsomUtils.getRequestValue(METADATA_LANGUAGE, processBlogEntryEvent.getHttpServletRequest());
148             Map JavaDoc context = processBlogEntryEvent.getContext();
149
150             Map JavaDoc templateAdditions = (Map JavaDoc) processBlogEntryEvent.getContext().get("BLOJSOM_TEMPLATE_ADDITIONS");
151             if (templateAdditions == null) {
152                 templateAdditions = new TreeMap JavaDoc();
153             }
154
155             templateAdditions.put(getClass().getName(), "#parse('" + LANGUAGE_SELECTION_TEMPLATE + "')");
156             processBlogEntryEvent.getContext().put("BLOJSOM_TEMPLATE_ADDITIONS", templateAdditions);
157
158             context.put(BLOJSOM_JVM_LANGUAGES, BlojsomUtils.getLanguagesForSystem(processBlogEntryEvent.getBlog().getBlogAdministrationLocale()));
159
160             // Preserve the current language selection if none submitted
161
if (processBlogEntryEvent.getEntry() != null) {
162                 String JavaDoc currentLanguage = (String JavaDoc) processBlogEntryEvent.getEntry().getMetaData().get(METADATA_LANGUAGE);
163                 if (_logger.isDebugEnabled()) {
164                     _logger.debug("Current language: " + currentLanguage);
165                 }
166                 processBlogEntryEvent.getContext().put(BLOJSOM_PLUGIN_CURRENT_LANGUAGE_SELECTION, currentLanguage);
167             }
168
169             if (!BlojsomUtils.checkNullOrBlank(language)) {
170                 processBlogEntryEvent.getEntry().getMetaData().put(METADATA_LANGUAGE, language);
171                 processBlogEntryEvent.getContext().put(BLOJSOM_PLUGIN_CURRENT_LANGUAGE_SELECTION, language);
172                 if (_logger.isDebugEnabled()) {
173                     _logger.debug("Added/updated language: " + language);
174                 }
175             } else {
176                 if (processBlogEntryEvent.getEntry() != null) {
177                     processBlogEntryEvent.getEntry().getMetaData().remove(METADATA_LANGUAGE);
178                 }
179                 processBlogEntryEvent.getContext().remove(BLOJSOM_PLUGIN_CURRENT_LANGUAGE_SELECTION);
180             }
181         }
182     }
183 }
Popular Tags