ActivityResourceBundlePlugin.java
/*
* Copyright (C) 2003-2012 eXo Platform SAS.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.exoplatform.social.core.processor;
import java.util.Iterator;
import java.util.Map;
import org.exoplatform.container.component.BaseComponentPlugin;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.container.xml.ObjectParameter;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
/**
* The activity resource bundle plugin for registering external resource bundle
* for I18N activity type.
*
* Must set titleId for ExoSocialActivity model to indicate that it's i18n activity and that titleId is used to map with message
* bundle key via configuration.
* Sample code:
* <pre>
* <external-component-plugins>
* <target-component>org.exoplatform.social.core.processor.I18NActivityProcessor</target-component>
* <component-plugin>
* <name>exosocial:spaces</name> <!-- activity type -->
* <set-method>addActivityResourceBundlePlugin</set-method>
* <type>org.exoplatform.social.core.processor.ActivityResourceBundlePlugin</type>
* <init-params>
* <object-param>
* <name>locale.social.Core</name> <!-- resource bundle key file -->
* <description>activity key type resource bundle mapping for exosocial:spaces</description>
* <object type="org.exoplatform.social.core.processor.ActivityResourceBundlePlugin">
* <field name="activityKeyTypeMapping">
* <map type="java.util.HashMap">
* <entry>
* <key><string>space_created</string></key>
* <value><string>SpaceActivityPublisher.space_created</string></value>
* </entry>
* </map>
* </field>
* </object>
* </object-param>
* </init-params>
* </component-plugin>
* </external-component-plugins>
* </pre>
*
* @author <a href="http://hoatle.net">hoatle (hoatlevan at gmail dot com)</a>
* @since 1.2.8
* @since Feb 6, 2012.
*/
public class ActivityResourceBundlePlugin extends BaseComponentPlugin {
/**
* The logger
*/
private static final Log LOG = ExoLogger.getLogger(ActivityResourceBundlePlugin.class);
/**
* The associated activity type.
*/
private String activityType;
/**
* The associated titleId and message bundle key mapping for this activity type.
*/
private Map<String, String> activityKeyTypeMapping;
/**
* The associated resource bundle key file
*/
private String resourceBundleKeyFile;
/**
* Default constructor
*/
public ActivityResourceBundlePlugin() {
}
/**
* Constructor for getting activityType, activityKeyTypeMapping, and resourceBundleKeyFile from init params.
*
* @param initParams the init params.
*/
public ActivityResourceBundlePlugin(InitParams initParams) {
if (initParams == null) {
LOG.warn("Failed to register this plugin: initParams is null");
return;
}
/* could be exo-jcr bug
if (this.getName() == null) {
LOG.warn("Failed to register this plugin: must set name with activityType value");
}
activityType = this.getName();
*/
Iterator<ObjectParameter> itr = initParams.getObjectParamIterator();
if (!itr.hasNext()) {
LOG.warn("Failed to register this plugin: no <object-param>");
return;
}
ObjectParameter objectParameter = itr.next();
if (objectParameter.getName() == null || objectParameter.getName().isEmpty()) {
LOG.warn("Failed to register this plugin: must set name with <object-param> as resource bundle key file");
return;
}
resourceBundleKeyFile = objectParameter.getName();
ActivityResourceBundlePlugin pluginConfig = (ActivityResourceBundlePlugin) objectParameter.getObject();
if (pluginConfig.getActivityKeyTypeMapping() == null ||
pluginConfig.getActivityKeyTypeMapping().size() == 0) {
LOG.warn("Failed to register this plugin: no <entry> found for <object-param> config");
return;
}
activityKeyTypeMapping = pluginConfig.getActivityKeyTypeMapping();
}
/**
* Gets the associated activity type from this activity resource bundle plugin.
*
* @return the associated activity type.
*/
public String getActivityType() {
return activityType;
}
/**
* Sets the associated activity type from this activity resource bundle plugin.
*
* @param activityType the associated activity type.
*/
public void setActivityType(String activityType) {
this.activityType = activityType;
}
/**
* Gets the associated activity key type mapping from this activity resource bundle plugin.
* @return
*/
public Map<String, String> getActivityKeyTypeMapping() {
return activityKeyTypeMapping;
}
/**
* Sets the associated activity key type mapping from this activity resource bundle plugin.
*
* @param mapping the hash map of key as titleId and value as message bundle key
*/
public void setActivityKeyTypeMapping(Map<String, String> mapping) {
if (mapping == null || mapping.size() == 0) {
LOG.warn("mapping is null or size = 0");
return;
}
activityKeyTypeMapping = mapping;
}
/**
* Gets the associated resource bundle key file for getting resource bundle via ResourceBundleService.
*
* @return the associated resource bundle key file.
*/
public String getResourceBundleKeyFile() {
return resourceBundleKeyFile;
}
/**
* Checks if this activity resource bundle plugin contains a specific activity titleId.
*
* @param activityTitleId the activity's titleId.
* @return a boolean value
*/
public boolean hasMessageBundleKey(String activityTitleId) {
if (activityKeyTypeMapping == null || activityKeyTypeMapping.size() == 0) {
return false;
}
return activityKeyTypeMapping.containsKey(activityTitleId);
}
/**
* Gets the registered message bundle key for a specific activity's titleId.
*
* @param activityTitleId the activity's titleId.
* @return the found registered message bundle key or null if not found.
*/
public String getMessageBundleKey(String activityTitleId) {
if (hasMessageBundleKey(activityTitleId)) {
return activityKeyTypeMapping.get(activityTitleId);
}
return null;
}
/**
* Checks if this is a valid plugin which means that activityType, resourceBundleKeyFile must
* not be null and activityKeyTypeMapping must contain equal or more than 1 entry.
*
* @return a boolean value.
*/
public boolean isValid() {
return (activityType != null && (activityKeyTypeMapping != null && activityKeyTypeMapping.size() > 0) &&
resourceBundleKeyFile != null);
}
}