View Javadoc
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.social.notification.mock;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.exoplatform.commons.api.notification.NotificationContext;
27  import org.exoplatform.commons.api.notification.model.NotificationInfo;
28  import org.exoplatform.commons.api.notification.model.UserSetting;
29  import org.exoplatform.commons.api.notification.service.setting.PluginSettingService;
30  import org.exoplatform.commons.api.notification.service.setting.UserSettingService;
31  import org.exoplatform.commons.api.notification.service.storage.NotificationService;
32  import org.exoplatform.commons.notification.channel.WebChannel;
33  import org.exoplatform.commons.utils.CommonsUtils;
34  
35  public class MockNotificationService implements NotificationService {
36  
37    private Map<String, List<NotificationInfo>> storeDigestJCR = new HashMap<>();
38    private Map<String, List<NotificationInfo>> storeInstantly = new HashMap<>();
39    private Map<String, List<NotificationInfo>> storeWebNotifs = new HashMap<>();
40  
41    public int sizeOfInstantly() {
42      return this.storeInstantly.values().stream().mapToInt(List::size).sum();
43    }
44  
45    public int sizeOfWebNotifs() {
46      return this.storeWebNotifs.values().stream().mapToInt(List::size).sum();
47    }
48    
49    public int sizeOfDigestJCR() {
50      return this.storeDigestJCR.values().stream().mapToInt(List::size).sum();
51    }
52  
53    public int sizeOfDigestJCR(String username) {
54      return this.storeDigestJCR.containsKey(username) ? this.storeDigestJCR.get(username).size() : 0;
55    }
56    
57    public void clearAll() {
58      clearOfDigestJCR();
59      clearOfInstantly();
60      clearOfWebNotifs();
61    }
62    
63    public void clearOfDigestJCR() {
64      this.storeDigestJCR.clear();
65    }
66    
67    public void clearOfInstantly() {
68      this.storeInstantly.clear();
69    }
70    
71    public void clearOfWebNotifs() {
72      this.storeWebNotifs.clear();
73    }
74  
75    public List<NotificationInfo> storeDigestJCR(String username) {
76      return this.storeDigestJCR.containsKey(username) ? this.storeDigestJCR.get(username) : Collections.emptyList();
77    }
78    
79    public List<NotificationInfo> storeInstantly(String username) {
80      return this.storeInstantly.containsKey(username) ? this.storeInstantly.get(username) : Collections.emptyList();
81    }
82    
83    public List<NotificationInfo> storeWebNotifs(String username) {
84      return this.storeWebNotifs.containsKey(username) ? this.storeWebNotifs.get(username) : Collections.emptyList();
85    }
86  
87    @Override
88    public void process(NotificationInfo notification) throws Exception {
89      String pluginId = notification.getKey().getId();
90      
91      // if the provider is not active, do nothing
92      PluginSettingService settingService = CommonsUtils.getService(PluginSettingService.class);
93      if (settingService.isActive(UserSetting.EMAIL_CHANNEL, pluginId) == false)
94        return;
95      
96      List<String> userIds = notification.getSendToUserIds();
97      MockUserSettingServiceImpl userSettingService = (MockUserSettingServiceImpl) CommonsUtils.getService(UserSettingService.class);
98      //
99      if (notification.isSendAll()) {
100       userIds = userSettingService.getUserHasSettingPlugin(UserSetting.EMAIL_CHANNEL, pluginId);
101     }
102     
103     for (String userId : userIds) {
104       UserSetting userSetting =  userSettingService.get(userId);
105       
106       if (userSetting == null) {
107         userSetting = userSettingService.getDefaultSettings();
108         userSetting.setUserId(userId);
109       }
110       
111       if (userSetting.isActive(UserSetting.EMAIL_CHANNEL, pluginId)) {
112         if(!this.storeInstantly.containsKey(userId)) {
113           this.storeInstantly.put(userId, new ArrayList<>());
114         }
115         this.storeInstantly.get(userId).add(notification);
116       }
117       
118       if (userSetting.isActive(WebChannel.ID, pluginId)) {
119         if(!this.storeWebNotifs.containsKey(userId)) {
120           this.storeWebNotifs.put(userId, new ArrayList<>());
121         }
122         this.storeWebNotifs.get(userId).add(notification);
123       }
124       
125       if (userSetting.isInDaily(pluginId) || userSetting.isInWeekly(pluginId)) {
126         if(!this.storeDigestJCR.containsKey(userId)) {
127           this.storeDigestJCR.put(userId, new ArrayList<>());
128         }
129         this.storeDigestJCR.get(userId).add(notification);
130       }
131       
132     }
133     
134   }
135 
136 
137   @Override
138   public void process(Collection<NotificationInfo> messages) throws Exception {
139     for (NotificationInfo message : messages) {
140       process(message);
141     }
142   }
143 
144   @Override
145   public void digest(NotificationContext context) throws Exception {
146     // TODO Auto-generated method stub
147   }
148 }