From c07914eb3620e04c69649a64f1ae3770337cd9d5 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Tue, 21 Jun 2016 12:56:57 +0530 Subject: [PATCH] Adding the activity count for the activty paginantion --- .../impl/ActivityProviderServiceImpl.java | 5 +++- .../operation/mgt/OperationManager.java | 2 ++ .../operation/mgt/OperationManagerImpl.java | 15 +++++++++++ .../core/operation/mgt/dao/OperationDAO.java | 2 ++ .../mgt/dao/impl/GenericOperationDAOImpl.java | 25 +++++++++++++++++++ ...PushNotificationBasedOperationManager.java | 5 ++++ .../DeviceManagementProviderService.java | 2 ++ .../DeviceManagementProviderServiceImpl.java | 5 ++++ 8 files changed, 60 insertions(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImpl.java index e3a06c1c80..848b0749a7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImpl.java @@ -113,6 +113,9 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService try { dmService = DeviceMgtAPIUtils.getDeviceManagementService(); activities = dmService.getActivitiesUpdatedAfter(timestamp, limit, offset); + activityList.setList(activities); + int count = dmService.getActivityCountUpdatedAfter(timestamp); + activityList.setCount(count); if (activities == null || activities.size() == 0) { if (isIfModifiedSinceSet) { return Response.status(Response.Status.NOT_MODIFIED).entity( @@ -132,6 +135,6 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService throw new UnexpectedServerErrorException(new ErrorResponse.ErrorResponseBuilder().setCode(500l) .setMessage(msg).build()); } - return Response.status(Response.Status.OK).entity(activities).build(); + return Response.status(Response.Status.OK).entity(activityList).build(); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java index 8e0b42db2a..a73a0daef9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java @@ -90,4 +90,6 @@ public interface OperationManager { List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementException; + int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementException; + } \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 5a1652640a..96b5f61cd3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -809,6 +809,21 @@ public class OperationManagerImpl implements OperationManager { } } + @Override + public int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementException { + try { + OperationManagementDAOFactory.openConnection(); + return operationDAO.getActivityCountUpdatedAfter(timestamp); + } catch (SQLException e) { + throw new OperationManagementException("Error occurred while opening a connection to the data source.", e); + } catch (OperationManagementDAOException e) { + throw new OperationManagementException("Error occurred while getting the activity count changed after a " + + "given time.", e); + } finally { + OperationManagementDAOFactory.closeConnection(); + } + } + private OperationDAO lookupOperationDAO(Operation operation) { if (operation instanceof CommandOperation) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java index 09e04d45be..51a9ba4df7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java @@ -75,4 +75,6 @@ public interface OperationDAO { List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementDAOException; + int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementDAOException; + } \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index 083b221b46..6e06aad36c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -474,6 +474,31 @@ public class GenericOperationDAOImpl implements OperationDAO { return activities; } + @Override + public int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "SELECT COUNT(*) AS COUNT FROM DM_ENROLMENT_OP_MAPPING AS m \n" + + "INNER JOIN DM_ENROLMENT AS d ON m.ENROLMENT_ID = d.ID \n" + + "WHERE m.UPDATED_TIMESTAMP > ? AND d.TENANT_ID = ?;"; + stmt = conn.prepareStatement(sql); + stmt.setLong(1, timestamp); + stmt.setInt(2, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + rs = stmt.executeQuery(); + if(rs.next()){ + return rs.getInt("COUNT"); + } + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while getting the activity count from " + + "the database.", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt, rs); + } + return 0; + } + private OperationResponse getOperationResponse(ResultSet rs) throws ClassNotFoundException, IOException, SQLException { OperationResponse response = new OperationResponse(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/PushNotificationBasedOperationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/PushNotificationBasedOperationManager.java index b5e3b85022..7274452c38 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/PushNotificationBasedOperationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/PushNotificationBasedOperationManager.java @@ -131,4 +131,9 @@ public class PushNotificationBasedOperationManager implements OperationManager { return this.operationManager.getActivitiesUpdatedAfter(timestamp, limit, offset); } + @Override + public int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementException { + return this.operationManager.getActivityCountUpdatedAfter(timestamp); + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 3d6d0a481e..123c0662da 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -242,4 +242,6 @@ public interface DeviceManagementProviderService { List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementException; + int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementException; + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 34d23e4473..0975ac8992 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -1098,6 +1098,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return DeviceManagementDataHolder.getInstance().getOperationManager().getActivitiesUpdatedAfter(timestamp, limit, offset); } + @Override + public int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementException { + return DeviceManagementDataHolder.getInstance().getOperationManager().getActivityCountUpdatedAfter(timestamp); + } + @Override public List getDevicesOfUser(String username) throws DeviceManagementException { List devices = new ArrayList<>();