From 236eb90ce27081c53234ca358507473a1d16ac18 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 20 Jun 2016 15:41:53 +0530 Subject: [PATCH] Fixing the limit and offset for activity --- .../impl/ActivityProviderServiceImpl.java | 2 +- .../operation/mgt/OperationManager.java | 2 ++ .../operation/mgt/OperationManagerImpl.java | 15 +++++++++ .../core/operation/mgt/dao/OperationDAO.java | 2 ++ .../mgt/dao/impl/GenericOperationDAOImpl.java | 33 ++++++++++++++++--- ...PushNotificationBasedOperationManager.java | 5 +++ .../DeviceManagementProviderService.java | 2 ++ .../DeviceManagementProviderServiceImpl.java | 5 +++ 8 files changed, 61 insertions(+), 5 deletions(-) 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 90ba5f259a..e3a06c1c80 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 @@ -112,7 +112,7 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService DeviceManagementProviderService dmService; try { dmService = DeviceMgtAPIUtils.getDeviceManagementService(); - activities = dmService.getActivitiesUpdatedAfter(timestamp); + activities = dmService.getActivitiesUpdatedAfter(timestamp, limit, offset); if (activities == null || activities.size() == 0) { if (isIfModifiedSinceSet) { return Response.status(Response.Status.NOT_MODIFIED).entity( 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 82cd262ab9..8e0b42db2a 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 @@ -88,4 +88,6 @@ public interface OperationManager { List getActivitiesUpdatedAfter(long timestamp) throws OperationManagementException; + List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) 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 258f3b5948..5a1652640a 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 @@ -794,6 +794,21 @@ public class OperationManagerImpl implements OperationManager { } } + @Override + public List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementException { + try { + OperationManagementDAOFactory.openConnection(); + return operationDAO.getActivitiesUpdatedAfter(timestamp, limit, offset); + } 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 list 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 a2d3a20ed6..09e04d45be 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 @@ -73,4 +73,6 @@ public interface OperationDAO { List getActivitiesUpdatedAfter(long timestamp) throws OperationManagementDAOException; + List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) 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 811c6314e3..083b221b46 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 @@ -306,7 +306,7 @@ public class GenericOperationDAOImpl implements OperationDAO { List operationResponses = new ArrayList<>(); if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp(String.valueOf(rs.getInt("UPDATED_TIMESTAMP"))); + activityStatus.setUpdatedTimestamp(new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); operationResponses.add(this.getOperationResponse(rs)); } activityStatus.setResponses(operationResponses); @@ -336,6 +336,11 @@ public class GenericOperationDAOImpl implements OperationDAO { @Override public List getActivitiesUpdatedAfter(long timestamp) throws OperationManagementDAOException { + return this.getActivitiesUpdatedAfter(timestamp, 0, 0); + } + + @Override + public List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; List activities = new ArrayList<>(); @@ -352,11 +357,28 @@ public class GenericOperationDAOImpl implements OperationDAO { "INNER JOIN DM_DEVICE_TYPE AS dt ON dt.ID=d.DEVICE_TYPE_ID\n" + "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE AS dor ON dor.ENROLMENT_ID=de.id \n" + "AND dor.OPERATION_ID=eom.OPERATION_ID\n" + - "WHERE eom.UPDATED_TIMESTAMP > ? AND de.TENANT_ID = ?"; + "WHERE eom.UPDATED_TIMESTAMP > ? AND de.TENANT_ID = ? ORDER BY eom.OPERATION_ID"; + + if(limit > 0) { + sql = sql + " LIMIT ?"; + } + + if(offset > 0) { + sql = sql + " OFFSET ?"; + } stmt = conn.prepareStatement(sql); stmt.setLong(1, timestamp); stmt.setInt(2, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + + int increment = 2; + + if(limit > 0) { + stmt.setInt(++increment, limit); + } + if(offset > 0) { + stmt.setInt(++increment, offset); + } rs = stmt.executeQuery(); int operationId = 0; @@ -388,7 +410,9 @@ public class GenericOperationDAOImpl implements OperationDAO { List operationResponses = new ArrayList<>(); if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp(String.valueOf(rs.getInt("UPDATED_TIMESTAMP"))); + activityStatus.setUpdatedTimestamp(new java.util.Date( + rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); + } if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { operationResponses.add(this.getOperationResponse(rs)); @@ -417,7 +441,8 @@ public class GenericOperationDAOImpl implements OperationDAO { List operationResponses = new ArrayList<>(); if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp(String.valueOf(rs.getInt("UPDATED_TIMESTAMP"))); + activityStatus.setUpdatedTimestamp(new java.util.Date( + rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); } if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { operationResponses.add(this.getOperationResponse(rs)); 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 46e382616d..b5e3b85022 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 @@ -126,4 +126,9 @@ public class PushNotificationBasedOperationManager implements OperationManager { return this.operationManager.getActivitiesUpdatedAfter(timestamp); } + @Override + public List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementException { + return this.operationManager.getActivitiesUpdatedAfter(timestamp, limit, offset); + } + } 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 8396e694ff..3d6d0a481e 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 @@ -240,4 +240,6 @@ public interface DeviceManagementProviderService { List getActivitiesUpdatedAfter(long timestamp) throws OperationManagementException; + List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) 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 dd4ae3c212..34d23e4473 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 @@ -1093,6 +1093,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return DeviceManagementDataHolder.getInstance().getOperationManager().getActivitiesUpdatedAfter(timestamp); } + @Override + public List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementException { + return DeviceManagementDataHolder.getInstance().getOperationManager().getActivitiesUpdatedAfter(timestamp, limit, offset); + } + @Override public List getDevicesOfUser(String username) throws DeviceManagementException { List devices = new ArrayList<>();