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 90ba5f259a1..e3a06c1c80c 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 82cd262ab95..8e0b42db2a5 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 258f3b59482..5a1652640ac 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 a2d3a20ed68..09e04d45bec 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 811c6314e35..083b221b46f 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 46e382616d0..b5e3b850229 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 8396e694ff7..3d6d0a481ec 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 dd4ae3c212c..34d23e44737 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<>();