Subversion Repositories SmartDukaan

Rev

Rev 25850 | Rev 28398 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.service;

import java.time.LocalDateTime;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.gson.Gson;
import com.spice.profitmandi.common.model.SendNotificationModel;
import com.spice.profitmandi.dao.entity.dtr.NotificationCampaign;
import com.spice.profitmandi.dao.entity.dtr.PushNotifications;
import com.spice.profitmandi.dao.entity.dtr.UserCampaign;
import com.spice.profitmandi.dao.entity.user.Device;
import com.spice.profitmandi.dao.model.SimpleCampaign;
import com.spice.profitmandi.dao.model.SimpleCampaignParams;
import com.spice.profitmandi.dao.repository.catalog.DeviceRepository;
import com.spice.profitmandi.dao.repository.dtr.NotificationCampaignRepository;
import com.spice.profitmandi.dao.repository.dtr.PushNotificationRepository;
import com.spice.profitmandi.dao.repository.dtr.UserCampaignRepository;

@Component
public class NotificationServiceImpl implements NotificationService {

        @Autowired
        UserCampaignRepository userCampaignRepository;

        @Autowired
        NotificationCampaignRepository notificationCampaignRepository;

        @Autowired
        DeviceRepository deviceRepository;

        @Autowired
        PushNotificationRepository pushNotificationRepository;

        @Autowired
        private Gson gson;

        private static final Logger LOGGER = LogManager.getLogger(NotificationService.class);

        @Override
        public void sendNotification(SendNotificationModel sendNotificationModel) {

                SimpleCampaignParams scp = new SimpleCampaignParams();
                scp.setMessage(sendNotificationModel.getMessage());
                scp.setTitle(sendNotificationModel.getTitle());
                scp.setImageUrl(sendNotificationModel.getImageUrl());
                scp.setType(sendNotificationModel.getType());
                scp.setUrl(sendNotificationModel.getUrl());
                scp.setShowImage(sendNotificationModel.getShowImage());
                scp.setExpireTimestamp(sendNotificationModel.getExpiresat());
                SimpleCampaign sc = new SimpleCampaign(scp);
                sc.setSimpleCampaignParams(scp);

                NotificationCampaign nc = new NotificationCampaign();
                nc.setName(sendNotificationModel.getCampaignName());
                nc.setImplementationType("SimpleCampaign");
                nc.setImplementationParams(gson.toJson(scp));
                nc.setMessageType(sendNotificationModel.getMessageType());
                nc.setDocumentId(sendNotificationModel.getDocumentId());
                nc.setCreatedTimestamp(LocalDateTime.now());
                notificationCampaignRepository.persist(nc);

                UserCampaign uc = null;
                List<Integer> userIds = sendNotificationModel.getUserIds();
                for (Integer userId : userIds) {
                        uc = new UserCampaign();
                        uc.setCampaignId(nc.getId());
                        uc.setUserId(userId);
                        uc.setPushTimestamp(LocalDateTime.now());
                        userCampaignRepository.persist(uc);
                }
                LOGGER.info("userIds" + userIds);

                List<Device> devices = deviceRepository.selectByUserIdAndModifiedTimestamp(userIds,
                                LocalDateTime.now().minusMonths(3), LocalDateTime.now());

                LOGGER.info("devices" + devices);
                pushNotification(nc.getId(), devices);
        }

        public void pushNotification(int cid, List<Device> devices) {
                for (Device device : devices) {
                        PushNotifications pn = new PushNotifications();
                        pn.setNotificationCampaignid(cid);
                        pn.setDeviceId(device.getId());
                        pn.setUserId(device.getUser_id());
                        pushNotificationRepository.persist(pn);
                }

        }

}