1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.push.service.send;
16
17 import org.apache.log4j.Logger;
18 import org.kuali.mobility.push.entity.Device;
19 import org.kuali.mobility.push.entity.Preference;
20 import org.kuali.mobility.push.entity.Push;
21 import org.kuali.mobility.push.service.PreferenceService;
22 import org.kuali.mobility.push.service.SendService;
23 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.stereotype.Service;
25
26 import java.util.*;
27 import java.util.concurrent.ExecutorService;
28 import java.util.concurrent.Executors;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 @Service("sendServiceDelegator")
46 public class SendServiceDelegator implements SendService {
47 private static final Logger LOG = Logger.getLogger(SendServiceDelegator.class);
48
49
50 private final Map<String, SendService> senders = new HashMap<String, SendService>();
51
52
53 private final ExecutorService exeService = Executors.newCachedThreadPool();
54
55
56 @Autowired
57 private PreferenceService preferenceService;
58
59
60
61
62
63
64 public void setSenders(Map<String, SendService> senders){
65 this.senders.putAll(senders);
66 }
67
68
69
70
71
72
73 private Map<String, SendService> getSenders(){
74 return this.senders;
75 }
76
77
78
79
80
81
82 public List<String> getSupportedDeviceTypes(){
83 return new Vector<String>(this.senders.keySet());
84 }
85
86
87
88
89
90
91
92 public void sendPush(Push push, Device device){
93 this.exeService.execute(new SendRunner(push, device));
94 }
95
96
97
98
99
100
101 public void sendPush(Push push, List<Device> devices){
102 this.exeService.execute(new SendRunner(push, devices));
103 }
104
105
106
107
108
109
110 public void setPreferenceService(PreferenceService preferenceService) {
111 this.preferenceService = preferenceService;
112 }
113
114
115
116
117
118
119 private class SendRunner implements Runnable{
120
121
122 private Push push;
123
124
125 private List<Device> devices;
126
127
128
129
130
131
132 public SendRunner(Push push, Device device){
133 this(push, Collections.nCopies(1, device));
134 }
135
136
137
138
139
140
141
142 public SendRunner(Push push, List<Device> devices){
143 this.push = push;
144 this.devices = devices;
145 }
146
147
148
149
150
151
152 @Override
153 public void run() {
154 for(String type : senders.keySet()){
155 List<Device> deviceList = getDevicesOfType(type, devices);
156 if (deviceList.size() > 0){
157 SendService sender = getSenders().get(type);
158 if (sender != null){
159 sender.sendPush(push, deviceList);
160 }
161 }
162 }
163 }
164
165
166
167
168
169
170
171
172 private List<Device> getDevicesOfType(String deviceType, List<Device> devices){
173 List<Device> deviceList = new ArrayList<Device>();
174 for(Device d : devices){
175 if (deviceType.equals(d.getType())){
176
177 Preference preference = preferenceService.findPreference(d.getUsername(), push.getSender());
178 if(preference == null || !preference.isSenderBlocked()){
179 deviceList.add(d);
180 } else {
181 LOG.debug("Device excluded from push by preferences: "+d.getDeviceId());
182 }
183
184 }
185 }
186 return deviceList;
187 }
188
189 }
190 }