Subversion Repositories SmartDukaan

Rev

Rev 25835 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
25822 amit.gupta 1
package com.spice.profitmandi.service;
2
 
3
import java.time.LocalDateTime;
4
import java.util.List;
5
 
6
import org.apache.logging.log4j.LogManager;
7
import org.apache.logging.log4j.Logger;
8
import org.springframework.beans.factory.annotation.Autowired;
9
 
10
import com.google.gson.Gson;
11
import com.spice.profitmandi.common.model.SendNotificationModel;
12
import com.spice.profitmandi.dao.entity.dtr.NotificationCampaign;
13
import com.spice.profitmandi.dao.entity.dtr.PushNotifications;
14
import com.spice.profitmandi.dao.entity.dtr.UserCampaign;
15
import com.spice.profitmandi.dao.entity.user.Device;
16
import com.spice.profitmandi.dao.model.SimpleCampaign;
17
import com.spice.profitmandi.dao.model.SimpleCampaignParams;
18
import com.spice.profitmandi.dao.repository.catalog.DeviceRepository;
19
import com.spice.profitmandi.dao.repository.dtr.NotificationCampaignRepository;
20
import com.spice.profitmandi.dao.repository.dtr.PushNotificationRepository;
21
import com.spice.profitmandi.dao.repository.dtr.UserCampaignRepository;
22
 
23
public class NotificationServiceImpl implements NotificationService {
24
 
25
	@Autowired
26
	UserCampaignRepository userCampaignRepository;
27
 
28
	@Autowired
29
	PushNotifications pushNotifications;
30
 
31
	@Autowired
32
	NotificationCampaignRepository notificationCampaignRepository;
33
 
34
	@Autowired
35
	DeviceRepository deviceRepository;
36
 
37
	@Autowired
38
	PushNotificationRepository pushNotificationRepository;
39
 
40
	private Gson gson = new Gson();
41
 
42
	private static final Logger LOGGER = LogManager.getLogger(NotificationService.class);
43
 
44
	@Override
45
	public void sendNotification(SendNotificationModel sendNotificationModel) {
46
 
47
		SimpleCampaignParams scp = new SimpleCampaignParams();
48
		scp.setMessage(sendNotificationModel.getMessage());
49
		scp.setTitle(sendNotificationModel.getTitle());
50
		scp.setImageUrl(sendNotificationModel.getImageUrl());
51
		scp.setType(sendNotificationModel.getType());
52
		scp.setUrl(sendNotificationModel.getUrl());
53
		scp.setShowImage(sendNotificationModel.getShowImage());
54
		scp.setExpireTimestamp(sendNotificationModel.getExpiresat());
55
		SimpleCampaign sc = new SimpleCampaign(scp);
56
		sc.setSimpleCampaignParams(scp);
57
 
58
		NotificationCampaign nc = new NotificationCampaign();
59
		nc.setName(sendNotificationModel.getCampaignName());
60
		nc.setImplementationType("SimpleCampaign");
61
		nc.setImplementationParams(gson.toJson(scp));
62
		nc.setMessageType(sendNotificationModel.getMessageType());
63
		nc.setDocumentId(sendNotificationModel.getDocumentId());
64
		nc.setCreatedTimestamp(LocalDateTime.now());
65
		notificationCampaignRepository.persist(nc);
66
 
67
		UserCampaign uc = null;
68
		List<Integer> userIds = sendNotificationModel.getUserIds();
69
		for (Integer userId : userIds) {
70
			uc = new UserCampaign();
71
			uc.setCampaignId(nc.getId());
72
			uc.setUserId(userId);
73
			uc.setPushTimestamp(LocalDateTime.now());
74
			userCampaignRepository.persist(uc);
75
		}
76
		LOGGER.info("userIds" + userIds);
77
 
78
		List<Device> devices = deviceRepository.selectByUserIdAndModifiedTimestamp(userIds,
79
				LocalDateTime.now().minusMonths(3), LocalDateTime.now());
80
 
81
		LOGGER.info("devices" + devices);
82
		pushNotification(nc.getId(), devices);
83
	}
84
 
85
	public void pushNotification(int cid, List<Device> devices) {
86
		for (Device device : devices) {
87
			PushNotifications pn = new PushNotifications();
88
			pn.setNotificationCampaignid(cid);
89
			pn.setDeviceId(device.getId());
90
			pn.setUserId(device.getUser_id());
91
			pushNotificationRepository.persist(pn);
92
		}
93
 
94
	}
95
 
96
}