1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.mobility.push.service;
17
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Map;
21
22 import javax.ws.rs.GET;
23 import javax.ws.rs.POST;
24 import javax.ws.rs.Path;
25 import javax.ws.rs.FormParam;
26 import javax.ws.rs.QueryParam;
27 import javax.ws.rs.PathParam;
28 import org.apache.commons.collections.map.HashedMap;
29 import org.apache.log4j.Logger;
30 import org.kuali.mobility.push.dao.DeviceDao;
31 import org.kuali.mobility.push.entity.Device;
32 import org.kuali.mobility.push.service.send.SendServiceDelegator;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.beans.factory.annotation.Qualifier;
35 import org.springframework.stereotype.Service;
36 import org.springframework.transaction.annotation.Transactional;
37 import org.springframework.ui.Model;
38 import org.springframework.web.bind.annotation.PathVariable;
39
40
41
42
43
44
45
46 @Service
47 public class DeviceServiceImpl implements DeviceService {
48
49
50 private static final Logger LOG = Logger.getLogger(DeviceServiceImpl.class);
51
52
53 @Autowired
54 private DeviceDao deviceDao;
55
56
57
58
59 @Autowired
60 @Qualifier("sendServiceDelegator")
61 private SendServiceDelegator sendServiceDelegator;
62
63
64
65
66
67 @Override
68 @Transactional
69 public void saveDevice(Device device){
70 deviceDao.saveDevice(device);
71 }
72
73
74
75
76
77
78 @Deprecated
79 @Override
80 public void registerDevice(Device device) {
81 this.saveDevice(device);
82 }
83
84
85
86
87
88
89 @Transactional
90 public List<Device> findAllDevices() {
91 return deviceDao.findAllDevices();
92 }
93
94
95
96
97
98
99 @Transactional
100 public boolean doesDeviceHaveUsername(String deviceid){
101 return deviceDao.doesDeviceHaveUsername(deviceid);
102 }
103
104
105
106
107
108
109 @Transactional
110 public List<Device> findDevicesByUsername(String username) {
111 return deviceDao.findDevicesByUsername(username);
112 }
113
114
115
116
117
118
119 @Transactional
120 public List<Device> findDevicesWithoutUsername() {
121 return deviceDao.findDevicesWithoutUsername();
122 }
123
124
125
126
127
128
129 @Transactional
130 public List<Device> findDevicesById(String id) {
131 return deviceDao.findDevicesById(Long.parseLong(id));
132 }
133
134
135
136
137
138
139 @Transactional
140 public Device findDeviceByRegId(String regid) {
141 return deviceDao.findDeviceByRegId(regid);
142 }
143
144
145
146
147
148 @Transactional
149 public List<Device> findDevicesByDeviceId(String deviceid) {
150 return deviceDao.findDevicesByDeviceId(deviceid);
151 }
152
153
154
155
156
157
158 @Transactional
159 public Device findDeviceByDeviceId(String deviceid) {
160 return deviceDao.findDeviceByDeviceId(deviceid);
161 }
162
163
164
165
166
167
168 @Transactional
169 public Device findDeviceById(Long id) {
170 return deviceDao.findDeviceById(id);
171 }
172
173
174
175
176
177 @Transactional
178 public List<Device> findDevicesByKeyword(String keyword){
179 return deviceDao.findDevicesByKeyword(keyword);
180 }
181
182
183
184
185
186
187 @Transactional
188 public boolean removeDevice(Device device){
189 return deviceDao.removeDevice(device);
190 }
191
192
193
194
195
196
197 @Transactional
198 public Long countDevices(){
199 return deviceDao.countDevices();
200 }
201
202
203
204
205
206
207 @Transactional
208 public Long countDevicesWithoutUsername(){
209 return deviceDao.countDevicesWithoutUsername();
210 }
211
212
213
214
215
216
217 @Override
218 public Long countDevices(String deviceType) {
219 return this.deviceDao.countDevices(deviceType);
220 }
221
222
223
224
225
226
227 @Override
228 public List<Device> findAllDevices(String deviceType) {
229 return this.deviceDao.findAllDevices(deviceType);
230 }
231
232
233
234
235
236
237 @Override
238 public Map<String, List<Device>> findDevicesMap() {
239 Map<String, List<Device>> deviceMapping = new HashedMap();
240 List<String> deviceTypes = this.getSupportedDeviceTypes();
241 List<Device> devices;
242 for (String deviceType : deviceTypes){
243 devices = this.findAllDevices(deviceType);
244 deviceMapping.put(deviceType, devices);
245 }
246 return deviceMapping;
247 }
248
249
250
251
252
253
254 public void setDeviceDao(DeviceDao dao) {
255 this.deviceDao = dao;
256 }
257
258
259
260
261
262
263 public DeviceDao getDeviceDao() {
264 return this.deviceDao;
265 }
266
267
268
269
270
271 @Override
272 public List<String> getSupportedDeviceTypes() {
273 return this.sendServiceDelegator.getSupportedDeviceTypes();
274 }
275
276
277
278
279
280
281 @Override
282 @GET
283 @Path("/keyword/{keyword}")
284 public String devicesByKeyword(@PathParam("keyword") final String keyword) {
285 List<Device> devices = findDevicesByKeyword(keyword);
286 LOG.info(devices.size() + " elements returned.");
287
288 String json = "";
289 if(devices.size() > 0){
290 Iterator<Device> i = devices.iterator();
291 if(devices.size() > 1){
292 json = "{\"total\":\"" + devices.size() + " Devices Found.\" , \"devices\":[";
293 }else{
294 json = "{\"total\":\"" + devices.size() + " Device Found.\" , \"devices\":[";
295 }
296 while(i.hasNext()){
297 json += ((Device)i.next()).toJson();
298 json += ",";
299 }
300 json = json.substring(0, json.length()-1);
301 json += "]}";
302 }else{
303 json = "{\"total\":\"No Devices Found\"}";
304 }
305 return json;
306 }
307
308
309
310
311
312 @Override
313 @POST
314 @Path("/username/")
315 public String devicesFromUsername(@FormParam("username") final String username) {
316 List<Device> devices = findDevicesByUsername(username);
317 String result = "{\"username\":\"" + username + "\",\"devices\":[";
318 String sDevices = "";
319 Iterator<Device> i = devices.iterator();
320
321 while(i.hasNext()){
322 Device d = (Device)i.next();
323 sDevices += "\"" + d.getDeviceId() + "\"," ;
324 }
325
326 if(sDevices.length() > 0){
327 sDevices = sDevices.substring(0, sDevices.length() - 1);
328 }
329
330 result += sDevices + "]}";
331 return result;
332 }
333
334
335
336
337
338 @Override
339 @POST
340 @Path("/deviceid/")
341 public String usernameFromDeviceId(@FormParam("deviceid") final String id) {
342 Device device = findDeviceByDeviceId(id);
343 if(device == null){
344 return "{\"deviceExists\":false}";
345 }
346 if(device.getUsername() == null || device.getUsername().length() == 0){
347 return "{\"deviceExists\":true,\"hasUsername\":false}";
348 }else{
349 return "{\"deviceExists\":true,\"hasUsername\":true,\"username\":\"" + device.getUsername() + "\"}";
350 }
351 }
352
353 }