001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.plugin.language;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.blojsom.blog.Blog;
035: import org.blojsom.blog.Entry;
036: import org.blojsom.event.Event;
037: import org.blojsom.event.EventBroadcaster;
038: import org.blojsom.event.Listener;
039: import org.blojsom.plugin.Plugin;
040: import org.blojsom.plugin.PluginException;
041: import org.blojsom.plugin.admin.event.ProcessEntryEvent;
042: import org.blojsom.util.BlojsomUtils;
043:
044: import javax.servlet.http.HttpServletRequest;
045: import javax.servlet.http.HttpServletResponse;
046: import java.util.Map;
047: import java.util.TreeMap;
048:
049: /**
050: * Language selection plugin allows you to attach a language attribute to a blog entry.
051: *
052: * @author David Czarnecki
053: * @since blojsom 3.0
054: * @version $Id: LanguageSelectionPlugin.java,v 1.3 2007/01/17 02:35:10 czarneckid Exp $
055: */
056: public class LanguageSelectionPlugin implements Plugin, Listener {
057:
058: private Log _logger = LogFactory
059: .getLog(LanguageSelectionPlugin.class);
060:
061: private static final String LANGUAGE_SELECTION_TEMPLATE = "org/blojsom/plugin/language/templates/admin-language-selection.vm";
062:
063: private static final String BLOJSOM_JVM_LANGUAGES = "BLOJSOM_JVM_LANGUAGES";
064: private static final String BLOJSOM_PLUGIN_CURRENT_LANGUAGE_SELECTION = "BLOJSOM_PLUGIN_CURRENT_LANGUAGE_SELECTION";
065: private static final String METADATA_LANGUAGE = "language";
066:
067: private EventBroadcaster _eventBroadcaster;
068:
069: /**
070: * Create a new instance of the language selection plugin
071: */
072: public LanguageSelectionPlugin() {
073: }
074:
075: /**
076: * Set the {@link org.blojsom.event.EventBroadcaster} event broadcaster
077: *
078: * @param eventBroadcaster {@link org.blojsom.event.EventBroadcaster}
079: */
080: public void setEventBroadcaster(EventBroadcaster eventBroadcaster) {
081: _eventBroadcaster = eventBroadcaster;
082: }
083:
084: /**
085: * Initialize this plugin. This method only called when the plugin is instantiated.
086: *
087: * @throws org.blojsom.plugin.PluginException
088: * If there is an error initializing the plugin
089: */
090: public void init() throws PluginException {
091: _eventBroadcaster.addListener(this );
092: }
093:
094: /**
095: * Process the blog entries
096: *
097: * @param httpServletRequest Request
098: * @param httpServletResponse Response
099: * @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 httpServletRequest,
106: HttpServletResponse httpServletResponse, Blog blog,
107: Map context, Entry[] entries) throws PluginException {
108: return entries;
109: }
110:
111: /**
112: * Perform any cleanup for the plugin. Called after {@link #process}.
113: *
114: * @throws org.blojsom.plugin.PluginException
115: * If there is an error performing cleanup for this plugin
116: */
117: public void cleanup() throws PluginException {
118: }
119:
120: /**
121: * Called when BlojsomServlet is taken out of service
122: *
123: * @throws org.blojsom.plugin.PluginException
124: * If there is an error in finalizing this plugin
125: */
126: public void destroy() throws PluginException {
127: }
128:
129: /**
130: * Handle an event broadcast from another component
131: *
132: * @param event {@link org.blojsom.event.Event} to be handled
133: */
134: public void handleEvent(Event event) {
135: }
136:
137: /**
138: * Process an event from another component
139: *
140: * @param event {@link org.blojsom.event.Event} to be handled
141: */
142: public void processEvent(Event event) {
143: if (event instanceof ProcessEntryEvent) {
144: if (_logger.isDebugEnabled()) {
145: _logger.debug("Handling process blog entry event");
146: }
147: ProcessEntryEvent processBlogEntryEvent = (ProcessEntryEvent) event;
148:
149: String language = BlojsomUtils.getRequestValue(
150: METADATA_LANGUAGE, processBlogEntryEvent
151: .getHttpServletRequest());
152: Map context = processBlogEntryEvent.getContext();
153:
154: Map templateAdditions = (Map) processBlogEntryEvent
155: .getContext().get("BLOJSOM_TEMPLATE_ADDITIONS");
156: if (templateAdditions == null) {
157: templateAdditions = new TreeMap();
158: }
159:
160: templateAdditions.put(getClass().getName(), "#parse('"
161: + LANGUAGE_SELECTION_TEMPLATE + "')");
162: processBlogEntryEvent.getContext().put(
163: "BLOJSOM_TEMPLATE_ADDITIONS", templateAdditions);
164:
165: context.put(BLOJSOM_JVM_LANGUAGES, BlojsomUtils
166: .getLanguagesForSystem(processBlogEntryEvent
167: .getBlog().getBlogAdministrationLocale()));
168:
169: // Preserve the current language selection if none submitted
170: if (processBlogEntryEvent.getEntry() != null) {
171: String currentLanguage = (String) processBlogEntryEvent
172: .getEntry().getMetaData()
173: .get(METADATA_LANGUAGE);
174: if (_logger.isDebugEnabled()) {
175: _logger.debug("Current language: "
176: + currentLanguage);
177: }
178: processBlogEntryEvent.getContext().put(
179: BLOJSOM_PLUGIN_CURRENT_LANGUAGE_SELECTION,
180: currentLanguage);
181: }
182:
183: if (!BlojsomUtils.checkNullOrBlank(language)) {
184: processBlogEntryEvent.getEntry().getMetaData().put(
185: METADATA_LANGUAGE, language);
186: processBlogEntryEvent.getContext().put(
187: BLOJSOM_PLUGIN_CURRENT_LANGUAGE_SELECTION,
188: language);
189: if (_logger.isDebugEnabled()) {
190: _logger
191: .debug("Added/updated language: "
192: + language);
193: }
194: } else {
195: if (processBlogEntryEvent.getEntry() != null) {
196: processBlogEntryEvent.getEntry().getMetaData()
197: .remove(METADATA_LANGUAGE);
198: }
199: processBlogEntryEvent.getContext().remove(
200: BLOJSOM_PLUGIN_CURRENT_LANGUAGE_SELECTION);
201: }
202: }
203: }
204: }
|