| 1 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 ; 46 import javax.servlet.http.HttpServletResponse ; 47 import java.util.Map ; 48 import java.util.TreeMap ; 49 50 57 public class LanguageSelectionPlugin implements Plugin, Listener { 58 59 private Log _logger = LogFactory.getLog(LanguageSelectionPlugin.class); 60 61 private static final String LANGUAGE_SELECTION_TEMPLATE = "org/blojsom/plugin/language/templates/admin-language-selection.vm"; 62 63 private static final String BLOJSOM_JVM_LANGUAGES = "BLOJSOM_JVM_LANGUAGES"; 64 private static final String BLOJSOM_PLUGIN_CURRENT_LANGUAGE_SELECTION = "BLOJSOM_PLUGIN_CURRENT_LANGUAGE_SELECTION"; 65 private static final String METADATA_LANGUAGE = "language"; 66 67 private EventBroadcaster _eventBroadcaster; 68 69 72 public LanguageSelectionPlugin() { 73 } 74 75 80 public void setEventBroadcaster(EventBroadcaster eventBroadcaster) { 81 _eventBroadcaster = eventBroadcaster; 82 } 83 84 90 public void init() throws PluginException { 91 _eventBroadcaster.addListener(this); 92 } 93 94 105 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 106 return entries; 107 } 108 109 115 public void cleanup() throws PluginException { 116 } 117 118 124 public void destroy() throws PluginException { 125 } 126 127 132 public void handleEvent(Event event) { 133 } 134 135 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 language = BlojsomUtils.getRequestValue(METADATA_LANGUAGE, processBlogEntryEvent.getHttpServletRequest()); 148 Map context = processBlogEntryEvent.getContext(); 149 150 Map templateAdditions = (Map ) processBlogEntryEvent.getContext().get("BLOJSOM_TEMPLATE_ADDITIONS"); 151 if (templateAdditions == null) { 152 templateAdditions = new TreeMap (); 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 if (processBlogEntryEvent.getEntry() != null) { 162 String currentLanguage = (String ) 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 |