Improve activity retrieval logic

revert-70ac1926
Charitha Goonetilleke 4 years ago
parent 2959cab240
commit 10d114ba1e

@ -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);
}
}

@ -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);
}

@ -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<Activity> 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.");
if (initiatedBy != null && !initiatedBy.isEmpty()) {
activityPaginationRequest.setInitiatedBy(initiatedBy);
}
count = dmService.getActivityCountUpdatedAfter(timestamp);
if (log.isDebugEnabled()) {
log.debug("Activity count: " + count);
if (operationCode != null && !operationCode.isEmpty()) {
activityPaginationRequest.setOperationCode(operationCode);
}
} else {
activities = dmService.getActivitiesUpdatedAfterByUser(timestamp, initiatedBy, limit, offset);
if (log.isDebugEnabled()) {
log.debug("Calling database to get activity count with timestamp and user.");
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()));
}
count = dmService.getActivityCountUpdatedAfterByUser(timestamp, initiatedBy);
activityPaginationRequest.setSince(timestamp);
if (log.isDebugEnabled()) {
log.debug("Activity count: " + count);
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();

@ -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;
}
}

@ -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,6 +122,11 @@ public interface OperationManager {
List<Activity> getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementException;
List<Activity> getActivities(ActivityPaginationRequest activityPaginationRequest) throws OperationManagementException;
int getActivitiesCount(ActivityPaginationRequest activityPaginationRequest)
throws OperationManagementException;
List<Activity> getFilteredActivities(String operationCode, int limit, int offset) throws OperationManagementException;
int getTotalCountOfFilteredActivities(String operationCode) throws OperationManagementException;

@ -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<Activity> 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<Activity> 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() + "'";

@ -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,6 +91,7 @@ public interface OperationDAO {
/**
* This method provides operation mappings for given status
*
* @param opStatus Operation status
* @param pushNotificationStatus Push notification Status
* @param limit Limit for no devices
@ -99,4 +101,10 @@ public interface OperationDAO {
Map<Integer, List<OperationMapping>> getOperationMappingsByStatus(Operation.Status opStatus, Operation.PushNotificationStatus pushNotificationStatus,
int limit) throws OperationManagementDAOException;
List<Activity> getActivities(ActivityPaginationRequest activityPaginationRequest)
throws OperationManagementDAOException;
int getActivitiesCount(ActivityPaginationRequest activityPaginationRequest)
throws OperationManagementDAOException;
}

@ -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.
@ -1619,4 +1620,221 @@ public class GenericOperationDAOImpl implements OperationDAO {
}
return operationMappingsTenantMap;
}
@Override
public List<Activity> 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<Integer> largeResponseIDs = activityHolder.getLargeResponseIDs();
List<Activity> 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;
}
}

@ -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<Device> getDevicesByIdentifiersAndStatuses(List<String> deviceIdentifiers, List<EnrolmentInfo.Status> statuses)
throws DeviceManagementException;
List<Activity> getActivities(ActivityPaginationRequest activityPaginationRequest) throws OperationManagementException;
int getActivitiesCount(ActivityPaginationRequest activityPaginationRequest)
throws OperationManagementException;
}

@ -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<Activity> 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<MonitoringOperation> getMonitoringOperationList(String deviceType) {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();

Loading…
Cancel
Save