From 5f2bc1fdfb2abc444611fb84a3acab1ba6c9ecef Mon Sep 17 00:00:00 2001 From: "konstantina.galouni" Date: Fri, 4 Dec 2020 09:56:36 +0000 Subject: [PATCH] [Trunk | Admin tools]: 1. NotificationsService.java: [NEW] service for notifications with updatePid and deleteByPid methods. 2. LayoutService.java & StatisticsService.java & SubscriberService.java: Logs added. 3. CommunityController.java: a. [Bug fix] On "updateCommunity()" (/update) method update pid for related notifications, layout, statistics and subscribers (old_pid had the value of new_pid, not the old one). b. Add update pid for notifications when updating portal pid | Delete related notifications by pid when deleting a portal. --- .../controllers/CommunityController.java | 12 +++++- .../uoaadmintools/services/LayoutService.java | 10 ++++- .../services/NotificationsService.java | 37 +++++++++++++++++++ .../services/StatisticsService.java | 10 ++++- .../services/SubscriberService.java | 10 ++++- 5 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 src/main/java/eu/dnetlib/uoaadmintools/services/NotificationsService.java diff --git a/src/main/java/eu/dnetlib/uoaadmintools/controllers/CommunityController.java b/src/main/java/eu/dnetlib/uoaadmintools/controllers/CommunityController.java index d85c94e..0bcb8a8 100644 --- a/src/main/java/eu/dnetlib/uoaadmintools/controllers/CommunityController.java +++ b/src/main/java/eu/dnetlib/uoaadmintools/controllers/CommunityController.java @@ -2,6 +2,7 @@ package eu.dnetlib.uoaadmintools.controllers; import eu.dnetlib.uoaadmintools.entities.Layout; import eu.dnetlib.uoaadmintools.services.LayoutService; +import eu.dnetlib.uoaadmintools.services.NotificationsService; import eu.dnetlib.uoaadmintools.services.StatisticsService; import eu.dnetlib.uoaadmintools.services.SubscriberService; import eu.dnetlib.uoaadmintoolslibrary.entities.Portal; @@ -22,6 +23,9 @@ public class CommunityController { @Autowired private LayoutService layoutService; + @Autowired + private NotificationsService notificationsService; + @Autowired private StatisticsService statisticsService; @@ -44,14 +48,17 @@ public class CommunityController { // @PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.PORTAL_ADMIN)") @RequestMapping(value = "/update", method = RequestMethod.POST) public PortalResponse updateCommunity(@RequestBody Portal portal) { + String old_pid = portalService.getPortalById(portal.getId()).getPid(); + String new_pid = portal.getPid(); + PortalResponse portalResponse = portalService.updatePortal(portal); - String old_pid = portalResponse.getPid(); - String new_pid = portal.getPid(); if(!old_pid.equals(new_pid)) { + log.debug("update portal pid - old: "+old_pid + " - new: "+new_pid); statisticsService.updatePid(old_pid, new_pid); subscriberService.updatePid(old_pid, new_pid); layoutService.updatePid(old_pid, new_pid); + notificationsService.updatePid(old_pid, new_pid); } return portalResponse; @@ -78,6 +85,7 @@ public class CommunityController { statisticsService.deleteByPid(pid); subscriberService.deletePortalSubscribers(pid); layoutService.deleteByPid(pid); + notificationsService.deleteByPid(pid); } return true; diff --git a/src/main/java/eu/dnetlib/uoaadmintools/services/LayoutService.java b/src/main/java/eu/dnetlib/uoaadmintools/services/LayoutService.java index 1989330..7cb7a96 100644 --- a/src/main/java/eu/dnetlib/uoaadmintools/services/LayoutService.java +++ b/src/main/java/eu/dnetlib/uoaadmintools/services/LayoutService.java @@ -14,9 +14,15 @@ public class LayoutService { private LayoutDAO layoutDAO; public void updatePid(String old_pid, String new_pid) { + log.debug("layout service: updatePid"); Layout layout = layoutDAO.findByPortalPid(old_pid); - layout.setPortalPid(new_pid); - layoutDAO.save(layout); + log.debug("layout service: layout id: "+(layout != null ? layout.getId() : "not found")); + if(layout != null) { + layout.setPortalPid(new_pid); + log.debug("layout layout: new layout pid: " + layout.getPortalPid()); + layoutDAO.save(layout); + log.debug("layout saved!"); + } } public void deleteByPid(String pid) { diff --git a/src/main/java/eu/dnetlib/uoaadmintools/services/NotificationsService.java b/src/main/java/eu/dnetlib/uoaadmintools/services/NotificationsService.java new file mode 100644 index 0000000..81ec8db --- /dev/null +++ b/src/main/java/eu/dnetlib/uoaadmintools/services/NotificationsService.java @@ -0,0 +1,37 @@ +package eu.dnetlib.uoaadmintools.services; + +import eu.dnetlib.uoaadmintools.dao.NotificationsDAO; +import eu.dnetlib.uoaadmintools.entities.Notifications; +import org.apache.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class NotificationsService { + private final Logger log = Logger.getLogger(this.getClass()); + + @Autowired + private NotificationsDAO notificationsDAO; + + public void updatePid(String old_pid, String new_pid) { + log.debug("notifications service: updatePid"); + List notifications = notificationsDAO.findByPortalPid(old_pid); + if(notifications != null) { + notifications.forEach(notification -> { + notification.setPortalPid(new_pid); + notificationsDAO.save(notification); + }); + log.debug("notifications saved!"); + } + } + + public void deleteByPid(String pid) { + // TODO check maybe we can delete by portalId without find first + List notifications = notificationsDAO.findByPortalPid(pid); + if(notifications != null) { + notifications.forEach(notification -> notificationsDAO.delete(notification.getId())); + } + } +} diff --git a/src/main/java/eu/dnetlib/uoaadmintools/services/StatisticsService.java b/src/main/java/eu/dnetlib/uoaadmintools/services/StatisticsService.java index 4df524f..afccd59 100644 --- a/src/main/java/eu/dnetlib/uoaadmintools/services/StatisticsService.java +++ b/src/main/java/eu/dnetlib/uoaadmintools/services/StatisticsService.java @@ -14,9 +14,15 @@ public class StatisticsService { private StatisticsDAO statisticsDAO; public void updatePid(String old_pid, String new_pid) { + log.debug("statistics service: updatePid"); Statistics statistics = statisticsDAO.findByPid(old_pid); - statistics.setPid(new_pid); - statisticsDAO.save(statistics); + log.debug("statistics service: statistics id: "+(statistics != null ? statistics.getId() : "not found")); + if(statistics != null) { + statistics.setPid(new_pid); + log.debug("statistics service: new statistics pid: " + statistics.getPid()); + statisticsDAO.save(statistics); + log.debug("statistics saved!"); + } } public void createPortalStatistics(String pid) { diff --git a/src/main/java/eu/dnetlib/uoaadmintools/services/SubscriberService.java b/src/main/java/eu/dnetlib/uoaadmintools/services/SubscriberService.java index 0701524..798a032 100644 --- a/src/main/java/eu/dnetlib/uoaadmintools/services/SubscriberService.java +++ b/src/main/java/eu/dnetlib/uoaadmintools/services/SubscriberService.java @@ -14,9 +14,15 @@ public class SubscriberService { private PortalSubscribersDAO portalSubscribersDAO; public void updatePid(String old_pid, String new_pid) { + log.debug("subscriber service: updatePid"); PortalSubscribers portalSubscribers = portalSubscribersDAO.findByPid(old_pid); - portalSubscribers.setPid(new_pid); - portalSubscribersDAO.save(portalSubscribers); + log.debug("subscriber service: portalSubscribers id: "+(portalSubscribers != null ? portalSubscribers.getId() : "not found")); + if(portalSubscribers != null) { + portalSubscribers.setPid(new_pid); + log.debug("subscriber portalSubscribers: new portalSubscribers pid: " + portalSubscribers.getPid()); + portalSubscribersDAO.save(portalSubscribers); + log.debug("portalSubscribers saved!"); + } } public void createPortalSubscribers(String pid) {