WikiActivityChildPlugin.java

  1. /*
  2.  * Copyright (C) 2003-2013 eXo Platform SAS.
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Affero General Public License
  6. * as published by the Free Software Foundation; either version 3
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see<http://www.gnu.org/licenses/>.
  16.  */
  17. package org.exoplatform.wiki.notification.plugin;

  18. import java.util.Map;

  19. import org.exoplatform.commons.api.notification.NotificationContext;
  20. import org.exoplatform.commons.api.notification.model.ArgumentLiteral;
  21. import org.exoplatform.commons.api.notification.model.NotificationInfo;
  22. import org.exoplatform.commons.api.notification.plugin.AbstractNotificationChildPlugin;
  23. import org.exoplatform.commons.api.notification.service.template.TemplateContext;
  24. import org.exoplatform.commons.notification.template.TemplateUtils;
  25. import org.exoplatform.commons.utils.CommonsUtils;
  26. import org.exoplatform.container.xml.InitParams;
  27. import org.exoplatform.social.core.activity.model.ExoSocialActivity;
  28. import org.exoplatform.social.core.manager.ActivityManager;

  29. public class WikiActivityChildPlugin extends AbstractNotificationChildPlugin {
  30.   public final static ArgumentLiteral<String> ACTIVITY_ID = new ArgumentLiteral<String>(String.class, "activityId");
  31.   public static final String ID = "ks-wiki:spaces";
  32.   private ExoSocialActivity activity = null;

  33.   public WikiActivityChildPlugin(InitParams initParams) {
  34.     super(initParams);
  35.   }

  36.   @Override
  37.   public String makeContent(NotificationContext ctx) {
  38.     try {
  39.       ActivityManager activityM = CommonsUtils.getService(ActivityManager.class);

  40.       NotificationInfo notification = ctx.getNotificationInfo();

  41.       String language = getLanguage(notification);
  42.       TemplateContext templateContext = new TemplateContext(ID, language);

  43.       String activityId = notification.getValueOwnerParameter(ACTIVITY_ID.getKey());
  44.       activity = activityM.getActivity(activityId);
  45.       if (activity.isComment()) {
  46.         activity = activityM.getParentActivity(activity);
  47.       }
  48.       templateContext.put("ACTIVITY", activity.getTitle());
  49.       templateContext.put("ACTIVITY_URL", CommonsUtils.getCurrentDomain() + activity.getTemplateParams().get("page_url"));

  50.       //
  51.       String content = TemplateUtils.processGroovy(templateContext);
  52.       return content;
  53.     } catch (Exception e) {
  54.       return (activity != null) ? activity.getTitle() : "";
  55.     }
  56.   }

  57.   public String getActivityParamValue(String key) {
  58.     Map<String, String> params = activity.getTemplateParams();
  59.     if (params != null) {
  60.       return params.get(key) != null ? params.get(key) : "";
  61.     }
  62.     return "";
  63.   }

  64.   @Override
  65.   public String getId() {
  66.     return ID;
  67.   }

  68.   @Override
  69.   public boolean isValid(NotificationContext ctx) {
  70.     return false;
  71.   }
  72. }