From 10d114ba1e027ed8aa2749d85679f4d7ddfb6fd0 Mon Sep 17 00:00:00 2001 From: charitha Date: Fri, 27 Nov 2020 22:53:26 +0530 Subject: [PATCH] Improve activity retrieval logic --- .../device/mgt/jaxrs/beans/ActivityList.java | 10 +- .../api/ActivityInfoProviderService.java | 56 ++- .../impl/ActivityProviderServiceImpl.java | 74 ++-- .../mgt/common/ActivityPaginationRequest.java | 116 ++++++ .../operation/mgt/OperationManager.java | 12 +- .../operation/mgt/OperationManagerImpl.java | 40 ++- .../core/operation/mgt/dao/OperationDAO.java | 14 +- .../mgt/dao/impl/GenericOperationDAOImpl.java | 332 +++++++++++++++--- .../DeviceManagementProviderService.java | 24 +- .../DeviceManagementProviderServiceImpl.java | 14 + 10 files changed, 557 insertions(+), 135 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/ActivityPaginationRequest.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/ActivityList.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/ActivityList.java index 08e6f268df..36b2be9b61 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/ActivityList.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/ActivityList.java @@ -19,11 +19,11 @@ package org.wso2.carbon.device.mgt.jaxrs.beans; import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.gson.Gson; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; -import java.util.ArrayList; import java.util.List; @ApiModel(value = "List of activities", description = "This contains a set of activities that matches a given" @@ -45,13 +45,7 @@ public class ActivityList extends BasePaginatedResult { @Override public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("{\n"); - sb.append(" count: ").append(getCount()).append(",\n"); - sb.append(" devices: [").append(activities).append("\n"); - sb.append("]}\n"); - return sb.toString(); + return new Gson().toJson(this); } - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ActivityInfoProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ActivityInfoProviderService.java index ec72dc6d16..18241eab75 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ActivityInfoProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ActivityInfoProviderService.java @@ -39,6 +39,7 @@ import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import javax.validation.constraints.Size; import javax.ws.rs.Consumes; +import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.HeaderParam; import javax.ws.rs.Path; @@ -422,37 +423,60 @@ public interface ActivityInfoProviderService { response = ErrorResponse.class) }) Response getActivities( + @ApiParam( + name = "offset", + value = "The starting pagination index for the complete list of qualified items.", + defaultValue = "0") + @DefaultValue("0") @QueryParam("offset") int offset, + @ApiParam( + name = "limit", + value = "Provide how many activity details you require from the starting pagination index/offset.", + defaultValue = "5") + @DefaultValue("20") @QueryParam("limit") int limit, @ApiParam( name = "since", value = "Checks if the requested variant was created since the specified date-time.\n" + "Provide the value in the following format: EEE, d MMM yyyy HH:mm:ss Z.\n" + - "Example: Mon, 05 Jan 2014 15:10:00 +0200", - required = false) + "Example: Mon, 05 Jan 2014 15:10:00 +0200" + ) @QueryParam("since") String since, @ApiParam( name = "initiatedBy", value = "The user, who initiated the operation. If is done by the task, the SYSTEM will be returned." + - " And if a user adds the operation, username is returned", - required = false) + " And if a user adds the operation, username is returned" + ) @QueryParam("initiatedBy") String initiatedBy, @ApiParam( - name = "offset", - value = "The starting pagination index for the complete list of qualified items.", - required = false, - defaultValue = "0") - @QueryParam("offset") int offset, + name = "operationCode", + value = "Operation Code to filter" + ) + @QueryParam("operationCode") String operationCode, @ApiParam( - name = "limit", - value = "Provide how many activity details you require from the starting pagination index/offset.", - required = false, - defaultValue = "5") - @QueryParam("limit") int limit, + name = "deviceType", + value = "Device Type to filter" + ) + @QueryParam("deviceType") String deviceType, + @ApiParam( + name = "deviceId", + value = "Device Id to filter" + ) + @QueryParam("deviceId") String deviceId, + @ApiParam( + name = "type", + value = "Operation type to filter" + ) + @QueryParam("type") String type, + @ApiParam( + name = "status", + value = "Operation response status to filter" + ) + @QueryParam("status") String status, @ApiParam( name = "If-Modified-Since", value = "Checks if the requested variant was modified, since the specified date-time\n." + "Provide the value in the following format: EEE, d MMM yyyy HH:mm:ss Z\n." + - "Example: Mon, 05 Jan 2014 15:10:00 +0200", - required = false) + "Example: Mon, 05 Jan 2014 15:10:00 +0200" + ) @HeaderParam("If-Modified-Since") String ifModifiedSince); } 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 d115659e18..48b4b8835b 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 @@ -18,10 +18,13 @@ */ package org.wso2.carbon.device.mgt.jaxrs.service.impl; +import com.google.gson.Gson; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.ActivityPaginationRequest; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.jaxrs.beans.ActivityList; @@ -34,6 +37,7 @@ import org.wso2.carbon.user.api.UserStoreException; import javax.validation.constraints.Size; import javax.ws.rs.Consumes; +import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.HeaderParam; import javax.ws.rs.Path; @@ -213,17 +217,22 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService } } - @GET @Override - public Response getActivities(@QueryParam("since") String since, @QueryParam("initiatedBy")String initiatedBy, - @QueryParam("offset") int offset, @QueryParam("limit") int limit, + public Response getActivities(@DefaultValue("0") @QueryParam("offset") int offset, + @DefaultValue("20") @QueryParam("limit") int limit, + @QueryParam("since") String since, + @QueryParam("initiatedBy") String initiatedBy, + @QueryParam("operationCode") String operationCode, + @QueryParam("deviceType") String deviceType, + @QueryParam("deviceId") String deviceId, + @QueryParam("type") String type, + @QueryParam("status") String status, @HeaderParam("If-Modified-Since") String ifModifiedSince) { long ifModifiedSinceTimestamp; long sinceTimestamp; long timestamp = 0; - boolean isIfModifiedSinceSet = false; if (log.isDebugEnabled()) { log.debug("getActivities since: " + since + " , offset: " + offset + " ,limit: " + limit + " ," + "ifModifiedSince: " + ifModifiedSince); @@ -240,7 +249,6 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService "Invalid date string is provided in 'If-Modified-Since' header").build()).build(); } ifModifiedSinceTimestamp = ifSinceDate.getTime(); - isIfModifiedSinceSet = true; timestamp = ifModifiedSinceTimestamp / 1000; } else if (since != null && !since.isEmpty()) { Date sinceDate; @@ -266,41 +274,45 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService } Response response = validateAdminUser(); if (response == null) { - List activities; - int count = 0; ActivityList activityList = new ActivityList(); DeviceManagementProviderService dmService; + ActivityPaginationRequest activityPaginationRequest = new ActivityPaginationRequest(offset, limit); try { if (log.isDebugEnabled()) { log.debug("Calling database to get activities."); } dmService = DeviceMgtAPIUtils.getDeviceManagementService(); - if (initiatedBy == null || initiatedBy.isEmpty()) { - activities = dmService.getActivitiesUpdatedAfter(timestamp, limit, offset); - - if (log.isDebugEnabled()) { - log.debug("Calling database to get activity count with timestamp."); - } - count = dmService.getActivityCountUpdatedAfter(timestamp); - if (log.isDebugEnabled()) { - log.debug("Activity count: " + count); - } - } else { - activities = dmService.getActivitiesUpdatedAfterByUser(timestamp, initiatedBy, limit, offset); - - if (log.isDebugEnabled()) { - log.debug("Calling database to get activity count with timestamp and user."); - } - count = dmService.getActivityCountUpdatedAfterByUser(timestamp, initiatedBy); - if (log.isDebugEnabled()) { - log.debug("Activity count: " + count); - } + if (initiatedBy != null && !initiatedBy.isEmpty()) { + activityPaginationRequest.setInitiatedBy(initiatedBy); + } + if (operationCode != null && !operationCode.isEmpty()) { + activityPaginationRequest.setOperationCode(operationCode); + } + if (deviceType != null && !deviceType.isEmpty()) { + activityPaginationRequest.setDeviceType(deviceType); + } + if (deviceId != null && !deviceId.isEmpty()) { + activityPaginationRequest.setDeviceId(deviceId); + } + if (type != null && !type.isEmpty()) { + activityPaginationRequest.setType(Operation.Type.valueOf(type.toUpperCase())); + } + if (status != null && !status.isEmpty()) { + activityPaginationRequest.setStatus(Operation.Status.valueOf(status.toUpperCase())); + } + activityPaginationRequest.setSince(timestamp); + if (log.isDebugEnabled()) { + log.debug("Activity request: " + new Gson().toJson(activityPaginationRequest)); + } + int count = dmService.getActivitiesCount(activityPaginationRequest); + if (count > 0) { + activityList.setList(dmService.getActivities(activityPaginationRequest)); } - activityList.setList(activities); activityList.setCount(count); - if (activities == null || activities.size() == 0) { - if (isIfModifiedSinceSet) { - return Response.notModified().build(); + if (log.isDebugEnabled()) { + log.debug("Filtered Activity count: " + count); + if (count > 0) { + log.debug("Fetched Activity count: " + activityList.getList().size()); } } return Response.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/ActivityPaginationRequest.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/ActivityPaginationRequest.java new file mode 100644 index 0000000000..a6527b359c --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/ActivityPaginationRequest.java @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.mgt.common; + +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; + +/** + * This class holds required parameters for a querying a paginated activity response. + */ +public class ActivityPaginationRequest { + + private int offset; + private int limit; + private String deviceType; + private String deviceId; + private String operationCode; + private String initiatedBy; + private long since; + private Operation.Type type; + private Operation.Status status; + + public ActivityPaginationRequest(int offset, int limit) { + this.offset = offset; + this.limit = limit; + } + + public int getOffset() { + return offset; + } + + public void setOffset(int offset) { + this.offset = offset; + } + + public int getLimit() { + return limit; + } + + public void setLimit(int limit) { + this.limit = limit; + } + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getDeviceId() { + return deviceId; + } + + public void setDeviceId(String deviceId) { + this.deviceId = deviceId; + } + + public String getOperationCode() { + return operationCode; + } + + public void setOperationCode(String operationCode) { + this.operationCode = operationCode; + } + + public String getInitiatedBy() { + return initiatedBy; + } + + public void setInitiatedBy(String initiatedBy) { + this.initiatedBy = initiatedBy; + } + + public long getSince() { + return since; + } + + public void setSince(long since) { + this.since = since; + } + + + public Operation.Type getType() { + return type; + } + + public void setType(Operation.Type type) { + this.type = type; + } + + public Operation.Status getStatus() { + return status; + } + + public void setStatus(Operation.Status status) { + this.status = status; + } + +} 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 9faa732090..9dcf1b2edf 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 @@ -17,12 +17,13 @@ */ package org.wso2.carbon.device.mgt.common.operation.mgt; +import org.wso2.carbon.device.mgt.common.ActivityPaginationRequest; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; -import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; +import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; import java.util.List; @@ -121,9 +122,14 @@ public interface OperationManager { List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementException; + List getActivities(ActivityPaginationRequest activityPaginationRequest) throws OperationManagementException; + + int getActivitiesCount(ActivityPaginationRequest activityPaginationRequest) + throws OperationManagementException; + List getFilteredActivities(String operationCode, int limit, int offset) throws OperationManagementException; - int getTotalCountOfFilteredActivities(String operationCode) throws OperationManagementException; + int getTotalCountOfFilteredActivities(String operationCode) throws OperationManagementException; List getActivitiesUpdatedAfterByUser(long timestamp, String user, 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/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 b2e7b769ae..251d1788ba 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 @@ -22,6 +22,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.ActivityPaginationRequest; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; @@ -151,9 +152,7 @@ public class OperationManagerImpl implements OperationManager { return null; } notificationStrategies.put(tenantId, provider.getNotificationStrategy(pushNoteConfig)); - } else if (notificationStrategies.containsKey(tenantId)) { - notificationStrategies.remove(tenantId); - } + } else notificationStrategies.remove(tenantId); lastUpdatedTimeStamps.put(tenantId, Calendar.getInstance().getTimeInMillis()); } return notificationStrategies.get(tenantId); @@ -1224,6 +1223,35 @@ public class OperationManagerImpl implements OperationManager { } } + @Override + public List getActivities(ActivityPaginationRequest activityPaginationRequest) + throws OperationManagementException { + try { + OperationManagementDAOFactory.openConnection(); + return operationDAO.getActivities(activityPaginationRequest); + } 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.", e); + } finally { + OperationManagementDAOFactory.closeConnection(); + } + } + + @Override + public int getActivitiesCount(ActivityPaginationRequest activityPaginationRequest) + throws OperationManagementException { + try { + OperationManagementDAOFactory.openConnection(); + return operationDAO.getActivitiesCount(activityPaginationRequest); + } 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.", e); + } finally { + OperationManagementDAOFactory.closeConnection(); + } + } @Override public List getFilteredActivities(String operationCode, int limit, int offset) throws OperationManagementException { @@ -1525,11 +1553,7 @@ public class OperationManagerImpl implements OperationManager { org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation deviceSpecificOperation = operationDAO. getOperationByDeviceAndId(enrolmentInfo.getId(), operationId); - if (deviceSpecificOperation == null) { - return false; - } else { - return true; - } + return deviceSpecificOperation != null; } catch (OperationManagementDAOException e) { String msg = "Error occurred while checking if operation with operation id " + operationId +" exist for " + deviceId.getType() + "' device '" + deviceId.getId() + "'"; 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 0b9d953ed2..ef2fa2b6a6 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 @@ -18,6 +18,7 @@ */ package org.wso2.carbon.device.mgt.core.operation.mgt.dao; +import org.wso2.carbon.device.mgt.common.ActivityPaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; @@ -90,13 +91,20 @@ public interface OperationDAO { /** * This method provides operation mappings for given status - * @param opStatus Operation status + * + * @param opStatus Operation status * @param pushNotificationStatus Push notification Status - * @param limit Limit for no devices + * @param limit Limit for no devices * @return Tenant based operation mappings list * @throws OperationManagementDAOException */ Map> getOperationMappingsByStatus(Operation.Status opStatus, Operation.PushNotificationStatus pushNotificationStatus, - int limit) throws OperationManagementDAOException; + int limit) throws OperationManagementDAOException; + + List getActivities(ActivityPaginationRequest activityPaginationRequest) + throws OperationManagementDAOException; + + int getActivitiesCount(ActivityPaginationRequest activityPaginationRequest) + 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 373b0d3a9b..cd310b8b91 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 @@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.ActivityPaginationRequest; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; @@ -46,12 +47,12 @@ import java.sql.SQLException; import java.sql.Timestamp; import java.text.DateFormat; import java.text.SimpleDateFormat; -import java.util.LinkedList; import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.HashMap; -import java.util.Date; /** * This class holds the generic implementation of OperationDAO which can be used to support ANSI db syntax. @@ -952,33 +953,33 @@ public class GenericOperationDAOImpl implements OperationDAO { Connection conn = OperationManagementDAOFactory.getConnection(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); String sql = "SELECT " + - " eom.ENROLMENT_ID," + - " eom.CREATED_TIMESTAMP," + - " eom.UPDATED_TIMESTAMP," + - " eom.OPERATION_ID," + - " eom.OPERATION_CODE," + - " eom.INITIATED_BY," + - " eom.TYPE," + - " eom.STATUS," + - " eom.DEVICE_ID," + - " eom.DEVICE_IDENTIFICATION," + - " eom.DEVICE_TYPE," + - " opr.ID AS OP_RES_ID," + - " opr.RECEIVED_TIMESTAMP," + - " opr.OPERATION_RESPONSE," + - " opr.IS_LARGE_RESPONSE " + - "FROM " + - " DM_ENROLMENT_OP_MAPPING eom " + - "LEFT JOIN " + - " DM_DEVICE_OPERATION_RESPONSE opr ON opr.EN_OP_MAP_ID = eom.ID " + - "INNER JOIN " + - " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING ORDER BY OPERATION_ID ASC limit ? , ? ) eom_ordered " + - " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " + - "WHERE " + - " eom.UPDATED_TIMESTAMP > ? " + - " AND eom.TENANT_ID = ? " + - " AND eom.INITIATED_BY = ? " + - "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; + " eom.ENROLMENT_ID," + + " eom.CREATED_TIMESTAMP," + + " eom.UPDATED_TIMESTAMP," + + " eom.OPERATION_ID," + + " eom.OPERATION_CODE," + + " eom.INITIATED_BY," + + " eom.TYPE," + + " eom.STATUS," + + " eom.DEVICE_ID," + + " eom.DEVICE_IDENTIFICATION," + + " eom.DEVICE_TYPE," + + " opr.ID AS OP_RES_ID," + + " opr.RECEIVED_TIMESTAMP," + + " opr.OPERATION_RESPONSE," + + " opr.IS_LARGE_RESPONSE " + + "FROM " + + " DM_ENROLMENT_OP_MAPPING eom " + + "LEFT JOIN " + + " DM_DEVICE_OPERATION_RESPONSE opr ON opr.EN_OP_MAP_ID = eom.ID " + + "INNER JOIN " + + " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING ORDER BY OPERATION_ID ASC limit ? , ? ) eom_ordered " + + " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " + + "WHERE " + + " eom.UPDATED_TIMESTAMP > ? " + + " AND eom.TENANT_ID = ? " + + " AND eom.INITIATED_BY = ? " + + "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, offset); @@ -1012,32 +1013,32 @@ public class GenericOperationDAOImpl implements OperationDAO { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); String sql = "SELECT " + - " eom.ENROLMENT_ID," + - " eom.CREATED_TIMESTAMP," + - " eom.UPDATED_TIMESTAMP," + - " eom.OPERATION_ID," + - " eom.OPERATION_CODE," + - " eom.INITIATED_BY," + - " eom.TYPE," + - " eom.STATUS," + - " eom.DEVICE_ID," + - " eom.DEVICE_IDENTIFICATION," + - " eom.DEVICE_TYPE," + - " ops.ID AS OP_RES_ID," + - " ops.RECEIVED_TIMESTAMP," + - " ops.OPERATION_RESPONSE," + - " ops.IS_LARGE_RESPONSE " + - "FROM " + - " DM_ENROLMENT_OP_MAPPING AS eom " + - "INNER JOIN " + - " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING ORDER BY OPERATION_ID ASC limit ? , ? ) AS eom_ordered " + - " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " + - "LEFT JOIN " + - " DM_DEVICE_OPERATION_RESPONSE AS ops ON ops.EN_OP_MAP_ID = eom.ID " + - "WHERE " + - " eom.UPDATED_TIMESTAMP > ? " + - " AND eom.TENANT_ID = ? " + - "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; + " eom.ENROLMENT_ID," + + " eom.CREATED_TIMESTAMP," + + " eom.UPDATED_TIMESTAMP," + + " eom.OPERATION_ID," + + " eom.OPERATION_CODE," + + " eom.INITIATED_BY," + + " eom.TYPE," + + " eom.STATUS," + + " eom.DEVICE_ID," + + " eom.DEVICE_IDENTIFICATION," + + " eom.DEVICE_TYPE," + + " ops.ID AS OP_RES_ID," + + " ops.RECEIVED_TIMESTAMP," + + " ops.OPERATION_RESPONSE," + + " ops.IS_LARGE_RESPONSE " + + "FROM " + + " DM_ENROLMENT_OP_MAPPING AS eom " + + "INNER JOIN " + + " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING ORDER BY OPERATION_ID ASC limit ? , ? ) AS eom_ordered " + + " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " + + "LEFT JOIN " + + " DM_DEVICE_OPERATION_RESPONSE AS ops ON ops.EN_OP_MAP_ID = eom.ID " + + "WHERE " + + " eom.UPDATED_TIMESTAMP > ? " + + " AND eom.TENANT_ID = ? " + + "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, offset); @@ -1056,7 +1057,7 @@ public class GenericOperationDAOImpl implements OperationDAO { } } catch (SQLException e) { String msg = "Error occurred while getting the operation details from the database."; - log.error(msg,e); + log.error(msg, e); throw new OperationManagementDAOException(msg, e); } } @@ -1619,4 +1620,221 @@ public class GenericOperationDAOImpl implements OperationDAO { } return operationMappingsTenantMap; } + + @Override + public List getActivities(ActivityPaginationRequest activityPaginationRequest) + throws OperationManagementDAOException { + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + String sql = "SELECT " + + " eom.ENROLMENT_ID," + + " eom.CREATED_TIMESTAMP," + + " eom.UPDATED_TIMESTAMP," + + " eom.OPERATION_ID," + + " eom.OPERATION_CODE," + + " eom.INITIATED_BY," + + " eom.TYPE," + + " eom.STATUS," + + " eom.DEVICE_ID," + + " eom.DEVICE_IDENTIFICATION," + + " eom.DEVICE_TYPE," + + " opr.ID AS OP_RES_ID," + + " opr.RECEIVED_TIMESTAMP," + + " opr.OPERATION_RESPONSE," + + " opr.IS_LARGE_RESPONSE " + + "FROM " + + " DM_ENROLMENT_OP_MAPPING eom " + + "LEFT JOIN " + + " DM_DEVICE_OPERATION_RESPONSE opr ON opr.EN_OP_MAP_ID = eom.ID " + + "INNER JOIN " + + " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING WHERE TENANT_ID = ? "; + + if (activityPaginationRequest.getDeviceType() != null) { + sql += "AND DEVICE_TYPE = ? "; + } + if (activityPaginationRequest.getDeviceId() != null) { + sql += "AND DEVICE_IDENTIFICATION = ? "; + } + if (activityPaginationRequest.getOperationCode() != null) { + sql += "AND OPERATION_CODE = ? "; + } + if (activityPaginationRequest.getInitiatedBy() != null) { + sql += "AND INITIATED_BY = ? "; + } + if (activityPaginationRequest.getSince() != 0) { + sql += "AND UPDATED_TIMESTAMP > ? "; + } + if (activityPaginationRequest.getType() != null) { + sql += "AND TYPE = ? "; + } + if (activityPaginationRequest.getStatus() != null) { + sql += "AND STATUS = ? "; + } + + sql += "ORDER BY OPERATION_ID ASC limit ? , ? ) eom_ordered " + + "ON eom_ordered.OPERATION_ID = eom.OPERATION_ID WHERE eom.TENANT_ID = ? "; + + if (activityPaginationRequest.getDeviceType() != null) { + sql += "AND eom.DEVICE_TYPE = ? "; + } + if (activityPaginationRequest.getDeviceId() != null) { + sql += "AND eom.DEVICE_IDENTIFICATION = ? "; + } + if (activityPaginationRequest.getOperationCode() != null) { + sql += "AND eom.OPERATION_CODE = ? "; + } + if (activityPaginationRequest.getInitiatedBy() != null) { + sql += "AND eom.INITIATED_BY = ? "; + } + if (activityPaginationRequest.getSince() != 0) { + sql += "AND eom.UPDATED_TIMESTAMP > ? "; + } + if (activityPaginationRequest.getType() != null) { + sql += "AND eom.TYPE = ? "; + } + if (activityPaginationRequest.getStatus() != null) { + sql += "AND eom.STATUS = ? "; + } + + sql += "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; + + int index = 1; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(index++, tenantId); + if (activityPaginationRequest.getDeviceType() != null) { + stmt.setString(index++, activityPaginationRequest.getDeviceType()); + } + if (activityPaginationRequest.getDeviceId() != null) { + stmt.setString(index++, activityPaginationRequest.getDeviceId()); + } + if (activityPaginationRequest.getOperationCode() != null) { + stmt.setString(index++, activityPaginationRequest.getOperationCode()); + } + if (activityPaginationRequest.getInitiatedBy() != null) { + stmt.setString(index++, activityPaginationRequest.getInitiatedBy()); + } + if (activityPaginationRequest.getSince() != 0) { + stmt.setLong(index++, activityPaginationRequest.getSince()); + } + if (activityPaginationRequest.getType() != null) { + stmt.setString(index++, activityPaginationRequest.getType().name()); + } + if (activityPaginationRequest.getStatus() != null) { + stmt.setString(index++, activityPaginationRequest.getStatus().name()); + } + + stmt.setInt(index++, activityPaginationRequest.getOffset()); + stmt.setInt(index++, activityPaginationRequest.getLimit()); + stmt.setInt(index++, tenantId); + + if (activityPaginationRequest.getDeviceType() != null) { + stmt.setString(index++, activityPaginationRequest.getDeviceType()); + } + if (activityPaginationRequest.getDeviceId() != null) { + stmt.setString(index++, activityPaginationRequest.getDeviceId()); + } + if (activityPaginationRequest.getOperationCode() != null) { + stmt.setString(index++, activityPaginationRequest.getOperationCode()); + } + if (activityPaginationRequest.getInitiatedBy() != null) { + stmt.setString(index++, activityPaginationRequest.getInitiatedBy()); + } + if (activityPaginationRequest.getSince() != 0) { + stmt.setLong(index++, activityPaginationRequest.getSince()); + } + if (activityPaginationRequest.getType() != null) { + stmt.setString(index++, activityPaginationRequest.getType().name()); + } + if (activityPaginationRequest.getStatus() != null) { + stmt.setString(index, activityPaginationRequest.getStatus().name()); + } + + try (ResultSet rs = stmt.executeQuery()) { + ActivityHolder activityHolder = OperationDAOUtil.getActivityHolder(rs); + List largeResponseIDs = activityHolder.getLargeResponseIDs(); + List activities = activityHolder.getActivityList(); + if (!largeResponseIDs.isEmpty()) { + populateLargeOperationResponses(activities, largeResponseIDs); + } + return activities; + } + } + } catch (SQLException e) { + String msg = "Error occurred while getting the operation details from the database."; + log.error(msg, e); + throw new OperationManagementDAOException(msg, e); + } + } + + @Override + public int getActivitiesCount(ActivityPaginationRequest activityPaginationRequest) + throws OperationManagementDAOException { + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + String sql = "SELECT count(DISTINCT OPERATION_ID) AS ACTIVITY_COUNT FROM DM_ENROLMENT_OP_MAPPING " + + "WHERE TENANT_ID = ? "; + + if (activityPaginationRequest.getDeviceType() != null) { + sql += "AND DEVICE_TYPE = ? "; + } + if (activityPaginationRequest.getDeviceId() != null) { + sql += "AND DEVICE_IDENTIFICATION = ? "; + } + if (activityPaginationRequest.getOperationCode() != null) { + sql += "AND OPERATION_CODE = ? "; + } + if (activityPaginationRequest.getInitiatedBy() != null) { + sql += "AND INITIATED_BY = ? "; + } + if (activityPaginationRequest.getSince() != 0) { + sql += "AND UPDATED_TIMESTAMP > ? "; + } + if (activityPaginationRequest.getType() != null) { + sql += "AND TYPE = ? "; + } + if (activityPaginationRequest.getStatus() != null) { + sql += "AND STATUS = ? "; + } + + int index = 1; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(index++, tenantId); + if (activityPaginationRequest.getDeviceType() != null) { + stmt.setString(index++, activityPaginationRequest.getDeviceType()); + } + if (activityPaginationRequest.getDeviceId() != null) { + stmt.setString(index++, activityPaginationRequest.getDeviceId()); + } + if (activityPaginationRequest.getOperationCode() != null) { + stmt.setString(index++, activityPaginationRequest.getOperationCode()); + } + if (activityPaginationRequest.getInitiatedBy() != null) { + stmt.setString(index++, activityPaginationRequest.getInitiatedBy()); + } + if (activityPaginationRequest.getSince() != 0) { + stmt.setLong(index++, activityPaginationRequest.getSince()); + } + if (activityPaginationRequest.getType() != null) { + stmt.setString(index++, activityPaginationRequest.getType().name()); + } + if (activityPaginationRequest.getStatus() != null) { + stmt.setString(index, activityPaginationRequest.getStatus().name()); + } + + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + return rs.getInt("ACTIVITY_COUNT"); + } + } + } + } catch (SQLException e) { + String msg = "Error occurred while getting the operation details from the database."; + log.error(msg, e); + throw new OperationManagementDAOException(msg, e); + } + return 0; + } + } 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 2fb5ce6f3e..3aacbe11da 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 @@ -36,17 +36,23 @@ package org.wso2.carbon.device.mgt.core.service; import org.apache.commons.collections.map.SingletonMap; +import org.wso2.carbon.device.mgt.common.ActivityPaginationRequest; import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceTransferRequest; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.FeatureManager; +import org.wso2.carbon.device.mgt.common.MonitoringOperation; +import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.DeviceTransferRequest; -import org.wso2.carbon.device.mgt.common.MonitoringOperation; import org.wso2.carbon.device.mgt.common.StartupOperationConfig; -import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; +import org.wso2.carbon.device.mgt.common.configuration.mgt.AmbiguousConfigurationException; +import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException; +import org.wso2.carbon.device.mgt.common.configuration.mgt.DeviceConfiguration; +import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; +import org.wso2.carbon.device.mgt.common.device.details.DeviceData; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistorySnapshot; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException; @@ -54,11 +60,6 @@ import org.wso2.carbon.device.mgt.common.exceptions.DeviceTypeNotFoundException; import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.exceptions.UnauthorizedDeviceAccessException; import org.wso2.carbon.device.mgt.common.exceptions.UserNotFoundException; -import org.wso2.carbon.device.mgt.common.configuration.mgt.AmbiguousConfigurationException; -import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException; -import org.wso2.carbon.device.mgt.common.configuration.mgt.DeviceConfiguration; -import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; -import org.wso2.carbon.device.mgt.common.device.details.DeviceData; import org.wso2.carbon.device.mgt.common.invitation.mgt.DeviceEnrollmentInvitationDetails; import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; @@ -932,4 +933,9 @@ public interface DeviceManagementProviderService { List getDevicesByIdentifiersAndStatuses(List deviceIdentifiers, List statuses) throws DeviceManagementException; + List getActivities(ActivityPaginationRequest activityPaginationRequest) throws OperationManagementException; + + int getActivitiesCount(ActivityPaginationRequest activityPaginationRequest) + 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 2afa4bbdb6..41e8ddf696 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 @@ -49,6 +49,7 @@ import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException; +import org.wso2.carbon.device.mgt.common.ActivityPaginationRequest; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceEnrollmentInfoNotification; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; @@ -2054,6 +2055,19 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return DeviceManagementDataHolder.getInstance().getOperationManager().getActivityCountUpdatedAfterByUser(timestamp, user); } + @Override + public List getActivities(ActivityPaginationRequest activityPaginationRequest) + throws OperationManagementException { + return DeviceManagementDataHolder.getInstance().getOperationManager().getActivities(activityPaginationRequest); + } + + @Override + public int getActivitiesCount(ActivityPaginationRequest activityPaginationRequest) + throws OperationManagementException { + return DeviceManagementDataHolder.getInstance().getOperationManager() + .getActivitiesCount(activityPaginationRequest); + } + @Override public List getMonitoringOperationList(String deviceType) { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();