diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/NotificationManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/NotificationManagementService.java index 62db9aacd83..8610aa34619 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/NotificationManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/NotificationManagementService.java @@ -209,4 +209,32 @@ public interface NotificationManagementService { defaultValue = "1") @PathParam("id") @Max(45) int id); + + + @PUT + @Path("/clear-all") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "PUT", + value = "Clear all notifications", + notes = "When a user needs to mark all the notifications as checked/read this " + + "function can be used to clear all notifications", + tags = "Device Notification Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:notifications:mark-checked") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK"), + @ApiResponse( + code = 500, + message = "Error occurred while clearing notifications.") + } + ) + Response clearAllNotifications(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/NotificationManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/NotificationManagementServiceImpl.java index 8e2322cd0a0..b39493d6a06 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/NotificationManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/NotificationManagementServiceImpl.java @@ -102,4 +102,16 @@ public class NotificationManagementServiceImpl implements NotificationManagement } } + @Override + public Response clearAllNotifications() { + Notification.Status status = Notification.Status.CHECKED; + try { + DeviceMgtAPIUtils.getNotificationManagementService().updateAllNotifications(status); + return Response.status(Response.Status.OK).build(); + } catch (NotificationManagementException e) { + log.error("Error encountered while trying to clear all notifications.", e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + } + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/notification/mgt/NotificationManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/notification/mgt/NotificationManagementService.java index d2a0cfcf6d2..429f9d30e30 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/notification/mgt/NotificationManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/notification/mgt/NotificationManagementService.java @@ -63,6 +63,16 @@ public interface NotificationManagementService { boolean updateNotificationStatus(int notificationId, Notification.Status status) throws NotificationManagementException; + /** + * Method for updating status all notifications. + * + * @return boolean status of the operation. + * @throws NotificationManagementException + * if something goes wrong while updating the Notification. + */ + boolean updateAllNotifications(Notification.Status status) throws + NotificationManagementException; + /** * Method to fetch all the notifications in the database. * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImpl.java index 09e1eca4f92..493fc21989a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImpl.java @@ -141,6 +141,27 @@ public class NotificationManagementServiceImpl implements NotificationManagement return true; } + @Override + public boolean updateAllNotifications(Notification.Status status) throws NotificationManagementException { + if (log.isDebugEnabled()) { + log.debug("Attempting to clear all notifications"); + } + try { + NotificationManagementDAOFactory.beginTransaction(); + notificationDAO.updateAllNotifications(status); + NotificationManagementDAOFactory.commitTransaction(); + } catch (TransactionManagementException e) { + NotificationManagementDAOFactory.rollbackTransaction(); + throw new NotificationManagementException("Error occurred while updating notification", e); + } finally { + NotificationManagementDAOFactory.closeConnection(); + } + if (log.isDebugEnabled()) { + log.debug("All notifications updated successfully."); + } + return true; + } + @Override public List getAllNotifications() throws NotificationManagementException { try { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/NotificationDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/NotificationDAO.java index f9787b45010..59034c159f6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/NotificationDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/NotificationDAO.java @@ -61,6 +61,14 @@ public interface NotificationDAO { int updateNotificationStatus(int notificationId, Notification.Status status) throws NotificationManagementException; + /** + * Update status of all notifications. + * + * @return returns the no of updated records. + * @throws NotificationManagementException + */ + int updateAllNotifications(Notification.Status status) throws NotificationManagementException; + /** * This method is used to get all notifications based on tenant-id. * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/AbstractNotificationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/AbstractNotificationDAOImpl.java index 9ae0a693f43..6b7d3252893 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/AbstractNotificationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/AbstractNotificationDAOImpl.java @@ -142,6 +142,27 @@ public abstract class AbstractNotificationDAOImpl implements NotificationDAO { return rows; } + @Override + public int updateAllNotifications(Notification.Status status) + throws NotificationManagementException { + Connection conn; + PreparedStatement stmt = null; + int rows; + try { + conn = NotificationManagementDAOFactory.getConnection(); + String sql = "UPDATE DM_NOTIFICATION SET STATUS = ?"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, status.toString()); + rows = stmt.executeUpdate(); + } catch (Exception e) { + throw new NotificationManagementException("Error while trying to clear all " + + "notifications", e); + } finally { + NotificationDAOUtil.cleanupResources(stmt, null); + } + return rows; + } + @Override public List getAllNotifications(int tenantId) throws NotificationManagementException { Connection conn; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.navbar.nav-menu/nav-menu.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.navbar.nav-menu/nav-menu.hbs index 2ec63e3b733..d18685ec406 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.navbar.nav-menu/nav-menu.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.navbar.nav-menu/nav-menu.hbs @@ -158,7 +158,11 @@ data-side="right" data-width="320" data-sidebar-fixed="true" data-top="90" data-fixed-offset="90" data-offset-top="50" data-spy="affix"> - + + {{/zone}} {{#zone "bottomJs"}} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.navbar.nav-menu/public/js/nav-menu.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.navbar.nav-menu/public/js/nav-menu.js index 4f10b1bd39c..f622790e493 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.navbar.nav-menu/public/js/nav-menu.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.navbar.nav-menu/public/js/nav-menu.js @@ -425,18 +425,24 @@ $.fn.collapse_nav_sub = function () { $(document).ready(function () { loadNotificationsPanel(); - $("#right-sidebar").on("click", ".new-notification", function () { - var notificationId = $(this).data("id"); + $("#right-sidebar").on("click", ".new-notification", function (e) { + e.preventDefault(); + var notificationId = $(this).parents('li').find('h4 a').data('id'); var redirectUrl = $(this).data("url"); var markAsReadNotificationsAPI = "/api/device-mgt/v1.0/notifications/" + notificationId + "/mark-checked"; var messageSideBar = ".sidebar-messages"; + var clickEvent = $(this).data('click-event'); + var eventHandler = $(this); invokerUtil.put( markAsReadNotificationsAPI, null, function (data) { data = JSON.parse(data); - if (data.statusCode == responseCodes["ACCEPTED"]) { + if(clickEvent && clickEvent === 'remove-notification'){ + $(eventHandler).parents('li').slideUp(); + $("#notification-bubble").html(parseInt($("#notification-bubble").text()) - 1); + }else { location.href = redirectUrl; } }, function () { @@ -448,6 +454,36 @@ $(document).ready(function () { ); }); + $("#right-sidebar").on("click", ".btn", function (e) { + + var clickEvent = $(this).data('click-event'); + + if(clickEvent == "clear-notification"){ + e.preventDefault(); + var markAsReadNotificationsAPI = "/api/device-mgt/v1.0/notifications/clear-all"; + var messageSideBar = ".sidebar-messages"; + var clickEvent = $(this).data('click-event'); + var eventHandler = $(this); + console.log(clickEvent); + + invokerUtil.put( + markAsReadNotificationsAPI, + null, + function (data) { + console.log(data); + $('.message').remove(); + $("#notification-bubble").html(0); + }, function () { + var content = "
  • Warning

    " + + "

    Unexpected error occurred while loading notification. Please refresh the page and" + + " try again

  • "; + $(messageSideBar).html(content); + } + ); + } + + }); + if (typeof $.fn.collapse == 'function') { $('.navbar-collapse.tiles').on('shown.bs.collapse', function () { $(this).collapse_nav_sub(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.navbar.nav-menu/public/templates/notifications.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.navbar.nav-menu/public/templates/notifications.hbs index e209e093edb..f447dc9a733 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.navbar.nav-menu/public/templates/notifications.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.navbar.nav-menu/public/templates/notifications.hbs @@ -1,14 +1,27 @@ {{#each notifications}}
  • -

    - - - {{deviceType}} : {{deviceName}} - -

    -

    {{description}}

    + + + +
    +

    + + + {{deviceType}} : {{deviceName}} + +

    +

    {{description}}

    +
    + + + + + + + +
  • {{/each}} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.theme/public/css/custom-desktop.css b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.theme/public/css/custom-desktop.css index 6f917a9eb5b..b661f08b220 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.theme/public/css/custom-desktop.css +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.theme/public/css/custom-desktop.css @@ -1178,8 +1178,12 @@ header .fw-user:before{ right: 0; } +.all-notifications{ + margin-bottom: 10px !important; +} + .sidebar-wrapper.toggled a.btn{ - margin-bottom: 70px; + margin-bottom: 70px; } .sidebar-wrapper-animation-fix {