WebNotificationServiceImpl.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.commons.notification.impl.service;

  18. import java.util.ArrayList;
  19. import java.util.List;

  20. import org.apache.commons.lang.StringUtils;
  21. import org.exoplatform.commons.api.notification.NotificationContext;
  22. import org.exoplatform.commons.api.notification.channel.AbstractChannel;
  23. import org.exoplatform.commons.api.notification.channel.template.AbstractTemplateBuilder;
  24. import org.exoplatform.commons.api.notification.model.ChannelKey;
  25. import org.exoplatform.commons.api.notification.model.MessageInfo;
  26. import org.exoplatform.commons.api.notification.model.NotificationInfo;
  27. import org.exoplatform.commons.api.notification.model.WebNotificationFilter;
  28. import org.exoplatform.commons.api.notification.service.WebNotificationService;
  29. import org.exoplatform.commons.api.notification.service.storage.WebNotificationStorage;
  30. import org.exoplatform.commons.notification.channel.WebChannel;
  31. import org.exoplatform.commons.notification.impl.NotificationContextImpl;
  32. import org.exoplatform.services.log.ExoLogger;
  33. import org.exoplatform.services.log.Log;

  34. public class WebNotificationServiceImpl implements WebNotificationService {
  35.   /** logger */
  36.   private static final Log LOG = ExoLogger.getLogger(WebNotificationServiceImpl.class);

  37.   /** storage */
  38.   private final WebNotificationStorage storage;
  39.  
  40.   public WebNotificationServiceImpl(WebNotificationStorage webStorage) {
  41.     this.storage = webStorage;
  42.   }

  43.   @Override
  44.   public void save(NotificationInfo notification) {
  45.     storage.save(notification);
  46.   }

  47.   @Override
  48.   public void markRead(String notificationId) {
  49.     storage.markRead(notificationId);
  50.   }

  51.   @Override
  52.   public void markAllRead(String userId) throws Exception {
  53.     storage.markAllRead(userId);
  54.   }

  55.   @Override
  56.   public List<String> get(WebNotificationFilter filter, int offset, int limit) {
  57.     List<String> result = new ArrayList<String>();
  58.     List<NotificationInfo> gotList = getNotificationInfos(filter, offset, limit);
  59.     NotificationContext ctx = NotificationContextImpl.cloneInstance();
  60.     ctx.append(POPUP_OVER, filter.isOnPopover());
  61.     AbstractChannel channel = ctx.getChannelManager().getChannel(ChannelKey.key(WebChannel.ID));
  62.     //
  63.     for (NotificationInfo notification : gotList) {
  64.       AbstractTemplateBuilder builder = channel.getTemplateBuilder(notification.getKey());
  65.       MessageInfo msg = null;
  66.       try {
  67.         msg = builder.buildMessage(ctx.setNotificationInfo(notification));
  68.       } catch (Exception e) {
  69.         LOG.error("Error while building message for notification with id = " + notification.getId(), e);
  70.       }
  71.       if (msg != null && msg.getBody() != null && !msg.getBody().isEmpty()) {
  72.         result.add(msg.getBody());
  73.       }
  74.       // if have any exception when template transformation
  75.       // ignore to display the notification
  76.       if (ctx.isFailed()) {
  77.         LOG.warn(ctx.getException().getMessage(), ctx.getException());
  78.       }
  79.     }
  80.     return result;
  81.   }

  82.   @Override
  83.   public List<NotificationInfo> getNotificationInfos(WebNotificationFilter filter, int offset, int limit) {
  84.     return storage.get(filter, offset, limit);
  85.   }

  86.   @Override
  87.   public boolean remove(String notificationId) {
  88.     return storage.remove(notificationId);
  89.   }

  90.   @Override
  91.   public void hidePopover(String notificationId) {
  92.     storage.hidePopover(notificationId);
  93.   }

  94.   @Override
  95.   public void resetNumberOnBadge(String userId) {
  96.     storage.resetNumberOnBadge(userId);
  97.   }

  98.   @Override
  99.   public int getNumberOnBadge(String userId) {
  100.     if (StringUtils.isNotBlank(userId)) {
  101.       try {
  102.         return storage.getNumberOnBadge(userId);
  103.       } catch (Exception e) {
  104.         if (LOG.isDebugEnabled()) {
  105.           LOG.error("Exception raising when getNumberOnBadge() ", e);
  106.         } else {
  107.             LOG.warn("Exception raising when getNumberOnBadge() associated to the userId " + userId);
  108.         }
  109.       }
  110.       return 0;
  111.     } else {
  112.       LOG.warn("Can't getNumberOnBadge(). The userId is null");
  113.       return 0;
  114.     }
  115.   }
  116. }