From 86e564713db41340c17e87e0eb06787f68d4d9a5 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Thu, 26 May 2016 21:16:01 +0530 Subject: [PATCH] Adding the changes for getting activity ids which are changes after a certain time --- .../device/mgt/jaxrs/api/Operation.java | 24 ++- .../mgt/jaxrs/api/impl/OperationImpl.java | 25 ++- .../operation/mgt/OperationManager.java | 4 + .../operation/mgt/OperationManagerImpl.java | 22 ++- .../core/operation/mgt/dao/OperationDAO.java | 4 + .../mgt/dao/impl/GenericOperationDAOImpl.java | 163 +++++++++++++++++- ...PushNotificationBasedOperationManager.java | 10 ++ .../DeviceManagementProviderService.java | 2 + .../DeviceManagementProviderServiceImpl.java | 5 + 9 files changed, 246 insertions(+), 13 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Operation.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Operation.java index 3f49b5d318..d9165c934f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Operation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Operation.java @@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.jaxrs.api; import io.swagger.annotations.*; import org.wso2.carbon.apimgt.annotations.api.*; import org.wso2.carbon.device.mgt.common.app.mgt.Application; +import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.jaxrs.api.common.MDMAPIException; import org.wso2.carbon.device.mgt.jaxrs.api.context.DeviceOperationContext; import org.wso2.carbon.device.mgt.jaxrs.beans.ApplicationWrapper; @@ -182,7 +183,8 @@ public interface Operation { produces = MediaType.APPLICATION_JSON + ", " + MediaType.APPLICATION_XML, httpMethod = "GET", value = "Retrieving the operation details.", - notes = "This will return the operation details including the responses from the devices") + notes = "This will return the operation details including the responses from the devices", + response = Activity.class) @ApiResponses(value = {@ApiResponse(code = 200, message = "Activity details provided successfully.."), @ApiResponse(code = 500, message = "Error occurred while fetching the activity for the supplied id.")}) @Permission(scope = "operation-view", permissions = {"/permission/admin/device-mgt/admin/devices/view"}) @@ -190,4 +192,24 @@ public interface Operation { required = true) @PathParam("id") String id) throws MDMAPIException; + + @GET + @Path("activity/after/{timestamp}") + @ApiOperation( + consumes = MediaType.APPLICATION_JSON + ", " + MediaType.APPLICATION_XML, + produces = MediaType.APPLICATION_JSON + ", " + MediaType.APPLICATION_XML, + httpMethod = "GET", + value = "Retrieving the operation details updated after given timestamp. Timestamp should be given as unix " + + "value in seconds.", + notes = "This will return the operation details including the responses from the devices update after given" + + "time.", + response = Activity.class, + responseContainer = "List") + @ApiResponses(value = {@ApiResponse(code = 200, message = "Activity details provided successfully.."), + @ApiResponse(code = 500, message = "Error occurred while fetching the activity for the supplied id.")}) + @Permission(scope = "operation-view", permissions = {"/permission/admin/device-mgt/admin/devices/view"}) + Response getActivityUpdatedAfter(@ApiParam(name = "timestamp", value = "Provide the timestamp as unix in seconds.", + required = true) @PathParam("timestamp") String timestamp) + throws MDMAPIException; + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/OperationImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/OperationImpl.java index 9c8abbc5fc..269f27eb6e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/OperationImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/OperationImpl.java @@ -240,17 +240,36 @@ public class OperationImpl implements org.wso2.carbon.device.mgt.jaxrs.api.Opera @Path("activity/{id}") public Response getActivity( @PathParam("id") String id) throws MDMAPIException { - Activity operation; + Activity activity; DeviceManagementProviderService dmService; try { dmService = DeviceMgtAPIUtils.getDeviceManagementService(); - operation = dmService.getOperationByActivityId(id); + activity = dmService.getOperationByActivityId(id); } catch (OperationManagementException e) { String msg = "Error occurred while fetching the activity for the supplied id."; log.error(msg, e); throw new MDMAPIException(msg, e); } - return Response.status(Response.Status.OK).entity(operation).build(); + return Response.status(Response.Status.OK).entity(activity).build(); + } + + + @Override + @GET + @Path("activity/after/{timestamp}") + public Response getActivityUpdatedAfter(@PathParam("timestamp") String timestamp) + throws MDMAPIException { + List activities; + DeviceManagementProviderService dmService; + try { + dmService = DeviceMgtAPIUtils.getDeviceManagementService(); + activities = dmService.getActivitiesUpdatedAfter(Long.parseLong(timestamp)); + } catch (OperationManagementException e) { + String msg = "Error occurred while fetching the activities updated after given time stamp."; + log.error(msg, e); + throw new MDMAPIException(msg, e); + } + return Response.status(Response.Status.OK).entity(activities).build(); } } \ No newline at end of file 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 2b523dadfc..a27ca502f2 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 @@ -87,4 +87,8 @@ public interface OperationManager { Activity getOperationByActivityId(String activity) throws OperationManagementException; + List getOperationUpdatedAfter(long timestamp) throws OperationManagementException; + + List getActivitiesUpdatedAfter(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 17f13d18ae..637e81cda4 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 @@ -768,7 +768,7 @@ public class OperationManagerImpl implements OperationManager { act.setActivityId(activity); return act; } catch (SQLException e) { - throw new OperationManagementException("Error occurred while opening a connection to the data source", e); + throw new OperationManagementException("Error occurred while opening a connection to the data source.", e); } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the operation with activity Id '" + activity, e); @@ -777,6 +777,26 @@ public class OperationManagerImpl implements OperationManager { } } + @Override + public List getOperationUpdatedAfter(long timestamp) throws OperationManagementException { + return null; + } + + @Override + public List getActivitiesUpdatedAfter(long timestamp) throws OperationManagementException { + try { + OperationManagementDAOFactory.openConnection(); + return operationDAO.getActivitiesUpdatedAfter(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 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 ecfcdad19f..d941679d21 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 @@ -69,4 +69,8 @@ public interface OperationDAO { int getEnrolmentIdFromMappingId(int enrollmentOpMappingId) throws OperationManagementDAOException; + List getOperationsUpdatedAfter(long timestamp) throws OperationManagementDAOException; + + List getActivitiesUpdatedAfter(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 aef1367c28..4848d090d9 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 @@ -326,19 +326,125 @@ public class GenericOperationDAOImpl implements OperationDAO { return activity; } + @Override + public List getActivitiesUpdatedAfter(long timestamp) throws OperationManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + List activities = new ArrayList<>(); + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "SELECT eom.ENROLMENT_ID, eom.OPERATION_ID, eom.ID AS EOM_MAPPING_ID, dor.ID AS OP_RES_ID,\n" + + "de.DEVICE_ID, d.DEVICE_IDENTIFICATION, \n" + + "d.DEVICE_TYPE_ID, dt.NAME AS DEVICE_TYPE_NAME, eom.STATUS, eom.CREATED_TIMESTAMP, \n" + + "eom.UPDATED_TIMESTAMP, op.OPERATION_CODE, op.TYPE AS OPERATION_TYPE, dor.OPERATION_RESPONSE, \n" + + "dor.RECEIVED_TIMESTAMP FROM DM_ENROLMENT_OP_MAPPING AS eom \n" + + "INNER JOIN DM_OPERATION AS op ON op.ID=eom.OPERATION_ID\n" + + "INNER JOIN DM_ENROLMENT AS de ON de.ID=eom.ENROLMENT_ID\n" + + "INNER JOIN DM_DEVICE AS d ON d.ID=de.DEVICE_ID \n" + + "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 = ?"; + + stmt = conn.prepareStatement(sql); + stmt.setLong(1, timestamp); + stmt.setInt(2, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + rs = stmt.executeQuery(); + + int operationId = 0; + int enrolmentId = 0; + Activity activity = null; + ActivityStatus activityStatus = null; + while (rs.next()) { + + if(operationId != rs.getInt("OPERATION_ID")) { + activity = new Activity(); + activities.add(activity); + List statusList = new ArrayList<>(); + activityStatus = new ActivityStatus(); + + operationId = rs.getInt("OPERATION_ID"); + enrolmentId = rs.getInt("ENROLMENT_ID"); + + activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); + activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP"))).toString()); + activity.setCode(rs.getString("OPERATION_CODE")); + + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); + deviceIdentifier.setType(rs.getString("DEVICE_TYPE_NAME")); + activityStatus.setDeviceIdentifier(deviceIdentifier); + + activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); + + List operationResponses = new ArrayList<>(); + if (rs.getInt("UPDATED_TIMESTAMP") != 0) { + operationResponses.add(this.getOperationResponse(rs)); + } + activityStatus.setResponses(operationResponses); + statusList.add(activityStatus); + activity.setActivityStatus(statusList); + activity.setActivityId(this.getActivityId(rs.getInt("OPERATION_ID"))); + + } + + if(operationId == rs.getInt("OPERATION_ID") && enrolmentId != rs.getInt("ENROLMENT_ID")) { + activityStatus = new ActivityStatus(); + + activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); + activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP"))).toString()); + activity.setCode(rs.getString("OPERATION_CODE")); + + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); + deviceIdentifier.setType(rs.getString("DEVICE_TYPE_NAME")); + activityStatus.setDeviceIdentifier(deviceIdentifier); + + activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); + + List operationResponses = new ArrayList<>(); + if (rs.getTimestamp("RECEIVED_TIMESTAMP") !=(null)) { + operationResponses.add(this.getOperationResponse(rs)); + } + activityStatus.setResponses(operationResponses); + activity.getActivityStatus().add(activityStatus); + + enrolmentId = rs.getInt("ENROLMENT_ID"); + } else { + if (rs.getTimestamp("RECEIVED_TIMESTAMP") !=(null)) { + activityStatus.getResponses().add(this.getOperationResponse(rs)); + } + } + } + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while getting the operation details from " + + "the database.", e); + } catch (ClassNotFoundException e) { + throw new OperationManagementDAOException("Error occurred while converting the operation response to string.", e); + } catch (IOException e) { + throw new OperationManagementDAOException("IO exception occurred while converting the operations responses.", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt, rs); + } + return activities; + } + private OperationResponse getOperationResponse(ResultSet rs) throws ClassNotFoundException, IOException, SQLException { OperationResponse response = new OperationResponse(); - response.setRecievedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); + if(rs.getTimestamp("RECEIVED_TIMESTAMP") !=(null)) { + response.setRecievedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); + } ByteArrayInputStream bais = null; ObjectInputStream ois = null; byte[] contentBytes; try { - contentBytes = (byte[]) rs.getBytes("OPERATION_RESPONSE"); - bais = new ByteArrayInputStream(contentBytes); - ois = new ObjectInputStream(bais); - response.setResponse(ois.readObject().toString()); - + if(rs.getBytes("OPERATION_RESPONSE") != null) { + contentBytes = (byte[]) rs.getBytes("OPERATION_RESPONSE"); + bais = new ByteArrayInputStream(contentBytes); + ois = new ObjectInputStream(bais); + response.setResponse(ois.readObject().toString()); + } } finally { if (bais != null) { try { @@ -382,6 +488,42 @@ public class GenericOperationDAOImpl implements OperationDAO { return -1; } + @Override + public List getOperationsUpdatedAfter(long timestamp) throws OperationManagementDAOException { + + PreparedStatement stmt = null; + ResultSet rs = null; + List operations = new ArrayList<>(); + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, OPERATION_CODE " + + "FROM DM_OPERATION AS o \n" + + "INNER JOIN DM_ENROLMENT_OP_MAPPING AS eom ON eom.OPERATION_ID=o.ID WHERE eom.UPDATED_TIMESTAMP = ?"; + stmt = conn.prepareStatement(sql); + stmt.setLong(1, timestamp); + rs = stmt.executeQuery(); + + if (rs.next()) { + Operation operation = new Operation(); + operation.setId(rs.getInt("ID")); + operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); + operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); + operation.setCode(rs.getString("OPERATION_CODE")); + + operations.add(operation); + } + + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while retrieving the operations updated " + + "after a given time" , e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt, rs); + } + return operations; + } + + + @Override public void deleteOperation(int id) throws OperationManagementDAOException { PreparedStatement stmt = null; @@ -839,8 +981,13 @@ public class GenericOperationDAOImpl implements OperationDAO { return Operation.Type.valueOf(type); } - private void setActivityId(Operation operation, int enrolmentId) { - operation.setActivityId(DeviceManagementConstants.OperationAttributes.ACTIVITY + enrolmentId); + private void setActivityId(Operation operation, int operationId) { + operation.setActivityId(DeviceManagementConstants.OperationAttributes.ACTIVITY + operationId); + } + + + private String getActivityId( int operationId) { + return DeviceManagementConstants.OperationAttributes.ACTIVITY + operationId; } 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 a8d93f3bde..d83a94939f 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 @@ -118,4 +118,14 @@ public class PushNotificationBasedOperationManager implements OperationManager { return this.operationManager.getOperationByActivityId(activity); } + @Override + public List getOperationUpdatedAfter(long timestamp) throws OperationManagementException { + return this.operationManager.getOperationUpdatedAfter(timestamp); + } + + @Override + public List getActivitiesUpdatedAfter(long timestamp) throws OperationManagementException { + return this.operationManager.getActivitiesUpdatedAfter(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 3629eb9c7b..9d407108bb 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 @@ -245,4 +245,6 @@ public interface DeviceManagementProviderService { Activity getOperationByActivityId(String activity) throws OperationManagementException; + List getActivitiesUpdatedAfter(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 672b0fa5f8..0ec30d848a 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 @@ -879,6 +879,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return DeviceManagementDataHolder.getInstance().getOperationManager().getOperationByActivityId(activity); } + @Override + public List getActivitiesUpdatedAfter(long timestamp) throws OperationManagementException { + return DeviceManagementDataHolder.getInstance().getOperationManager().getActivitiesUpdatedAfter(timestamp); + } + @Override public List getDevicesOfUser(String username) throws DeviceManagementException { List devices = new ArrayList<>();