/src/main/java/com/ohmuk/folitics/controller/NotificationController.java

https://bitbucket.org/shyamite/govrnn-after-unibera-mainapp · Java · 142 lines · 110 code · 22 blank · 10 comment · 9 complexity · 72e621eebd74fffdc89b48a02d0d3586 MD5 · raw file

  1. package com.ohmuk.folitics.controller;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10. import org.springframework.web.bind.annotation.ResponseBody;
  11. import com.ohmuk.folitics.businessDelegate.interfaces.IUserBusinessDelegate;
  12. import com.ohmuk.folitics.dto.ResponseDto;
  13. import com.ohmuk.folitics.hibernate.entity.User;
  14. import com.ohmuk.folitics.hibernate.entity.UserUINotification;
  15. import com.ohmuk.folitics.mongodb.entity.NotificationMongo;
  16. import com.ohmuk.folitics.mongodb.service.INotificationMongoService;
  17. import com.ohmuk.folitics.ouput.model.GeneralNotificationOutputModel;
  18. import com.ohmuk.folitics.ouput.model.OpinionNotificationOutputModel;
  19. import com.ohmuk.folitics.service.IUserService;
  20. import com.ohmuk.folitics.service.IUserUINotificationService;
  21. /**
  22. * @author Soumya
  23. *
  24. */
  25. @Controller
  26. @RequestMapping("/notification")
  27. public class NotificationController {
  28. @Autowired
  29. private volatile IUserBusinessDelegate businessDelegate;
  30. @Autowired
  31. INotificationMongoService notificationMongoService;
  32. @Autowired
  33. private IUserUINotificationService userUINotificationService;
  34. @Autowired
  35. private IUserService userService;
  36. @Autowired
  37. private static Logger logger = LoggerFactory.getLogger(NotificationController.class);
  38. @RequestMapping(value = "/getOpinionNotifications", method = RequestMethod.GET)
  39. public @ResponseBody ResponseDto<OpinionNotificationOutputModel> getOpinionNotifications(Long userId,
  40. String componentType) throws Exception {
  41. logger.info("Inside NotificationController getOpinionNotifications method");
  42. List<OpinionNotificationOutputModel> opinionNotificationOutputModels;
  43. // Currently it is getting the top notification for the newly created
  44. // opinion
  45. opinionNotificationOutputModels = getAllOpinionNotifications(userId, componentType);
  46. logger.info("Exiting NotificationController getOpinionNotifications method");
  47. return new ResponseDto<OpinionNotificationOutputModel>(true, opinionNotificationOutputModels);
  48. }
  49. @RequestMapping(value = "/getGeneralNotifications", method = RequestMethod.GET)
  50. public @ResponseBody ResponseDto<GeneralNotificationOutputModel> getGeneralNotifications(Long userId,
  51. String componentType) throws Exception {
  52. logger.info("Inside NotificationController getGeneralNotifications method");
  53. List<GeneralNotificationOutputModel> generalNotificationOutputModels;
  54. // TO DO : this will late changed accordingly to get appropriate values
  55. generalNotificationOutputModels = getGeneralNotificationsForUser(userId, componentType);
  56. logger.info("Exiting NotificationController getGeneralNotifications method");
  57. return new ResponseDto<GeneralNotificationOutputModel>(true, generalNotificationOutputModels);
  58. }
  59. @RequestMapping(value = "/getUnreadNotificationsCount", method = RequestMethod.GET)
  60. public @ResponseBody ResponseDto<Integer> getUnreadNotifications(Long userId, String notificationType) {
  61. logger.info("Inside NotificationControl.getUnreadNotificationsCount method");
  62. try {
  63. if (null != userId) {
  64. User dbUser = userService.findUserById(userId);
  65. UserUINotification userUINotification = userUINotificationService.read(dbUser, notificationType);
  66. if(!userUINotification.getEnabled()){
  67. return new ResponseDto<Integer>(true, 0);
  68. }
  69. else{
  70. List<NotificationMongo> unreadNotifications = notificationMongoService.getUnreadNotificationForUser(
  71. userId, notificationType);
  72. if (unreadNotifications != null && !unreadNotifications.isEmpty()) {
  73. return new ResponseDto<Integer>(true, unreadNotifications.size());
  74. }
  75. }
  76. }
  77. } catch (Exception exception) {
  78. logger.error("Exception in reading all unread notifications count in NotificationControl.getUnreadNotifications with userId:"
  79. + userId);
  80. logger.error("Exception: " + exception);
  81. logger.info("Exiting from NotificationControl.getUnreadNotificationsCount method");
  82. return new ResponseDto<Integer>(false);
  83. }
  84. return new ResponseDto<Integer>(false, 0);
  85. }
  86. @RequestMapping(value = "/updateNotificationreadStatus", method = RequestMethod.GET)
  87. public @ResponseBody ResponseDto<NotificationMongo> updateNotificationreadStatus(String notificationMongoId) {
  88. logger.info("Inside NotificationControl.updateNotificationreadStatus method");
  89. // BigInteger bigInteger = new
  90. // BigDecimal(notificationMongoId).toBigInteger();
  91. try {
  92. if (null != notificationMongoId) {
  93. return new ResponseDto<NotificationMongo>(notificationMongoService.update(notificationMongoId));
  94. } else {
  95. return new ResponseDto<NotificationMongo>(false);
  96. }
  97. } catch (Exception exception) {
  98. logger.error("Exception in updating notification mongo db object in NotificationControl.updateNotificationreadStatus with id:"
  99. + notificationMongoId);
  100. logger.error("Exception: " + exception);
  101. logger.info("Exiting from NotificationControl.updateNotificationreadStatus method");
  102. return new ResponseDto<NotificationMongo>(false);
  103. }
  104. }
  105. private List<OpinionNotificationOutputModel> getAllOpinionNotifications(Long userId, String componentType)
  106. throws Exception {
  107. List<OpinionNotificationOutputModel> opinionNotificationOutputModels = new ArrayList<>();
  108. opinionNotificationOutputModels = notificationMongoService.getTopOpinionNotificationForUser(userId,
  109. componentType);
  110. /* Collections.reverse(opinionNotificationOutputModels); */
  111. return opinionNotificationOutputModels;
  112. }
  113. private List<GeneralNotificationOutputModel> getGeneralNotificationsForUser(Long userId, String componentType)
  114. throws Exception {
  115. List<GeneralNotificationOutputModel> generalNotificationOutputModels = new ArrayList<>();
  116. generalNotificationOutputModels = notificationMongoService.getAllGeneralNotificationForUser(userId,
  117. componentType);
  118. return generalNotificationOutputModels;
  119. }
  120. }