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; package org.wso2.carbon.device.mgt.jaxrs.beans;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.Gson;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@ApiModel(value = "List of activities", description = "This contains a set of activities that matches a given" @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 @Override
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); return new Gson().toJson(this);
sb.append("{\n");
sb.append(" count: ").append(getCount()).append(",\n");
sb.append(" devices: [").append(activities).append("\n");
sb.append("]}\n");
return sb.toString();
} }
} }

@ -39,6 +39,7 @@ import org.wso2.carbon.device.mgt.jaxrs.util.Constants;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam; import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path; import javax.ws.rs.Path;
@ -422,37 +423,60 @@ public interface ActivityInfoProviderService {
response = ErrorResponse.class) response = ErrorResponse.class)
}) })
Response getActivities( 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( @ApiParam(
name = "since", name = "since",
value = "Checks if the requested variant was created since the specified date-time.\n" + 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" + "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", "Example: Mon, 05 Jan 2014 15:10:00 +0200"
required = false) )
@QueryParam("since") String since, @QueryParam("since") String since,
@ApiParam( @ApiParam(
name = "initiatedBy", name = "initiatedBy",
value = "The user, who initiated the operation. If is done by the task, the SYSTEM will be returned." + 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", " And if a user adds the operation, username is returned"
required = false) )
@QueryParam("initiatedBy") String initiatedBy, @QueryParam("initiatedBy") String initiatedBy,
@ApiParam( @ApiParam(
name = "offset", name = "operationCode",
value = "The starting pagination index for the complete list of qualified items.", value = "Operation Code to filter"
required = false, )
defaultValue = "0") @QueryParam("operationCode") String operationCode,
@QueryParam("offset") int offset,
@ApiParam( @ApiParam(
name = "limit", name = "deviceType",
value = "Provide how many activity details you require from the starting pagination index/offset.", value = "Device Type to filter"
required = false, )
defaultValue = "5") @QueryParam("deviceType") String deviceType,
@QueryParam("limit") int limit, @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( @ApiParam(
name = "If-Modified-Since", name = "If-Modified-Since",
value = "Checks if the requested variant was modified, since the specified date-time\n." + 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." + "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", "Example: Mon, 05 Jan 2014 15:10:00 +0200"
required = false) )
@HeaderParam("If-Modified-Since") String ifModifiedSince); @HeaderParam("If-Modified-Since") String ifModifiedSince);
} }

@ -18,10 +18,13 @@
*/ */
package org.wso2.carbon.device.mgt.jaxrs.service.impl; 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.Log;
import org.apache.commons.logging.LogFactory; 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.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; 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.common.operation.mgt.OperationManagementException;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.jaxrs.beans.ActivityList; 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.validation.constraints.Size;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam; import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path; import javax.ws.rs.Path;
@ -213,17 +217,22 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService
} }
} }
@GET @GET
@Override @Override
public Response getActivities(@QueryParam("since") String since, @QueryParam("initiatedBy")String initiatedBy, public Response getActivities(@DefaultValue("0") @QueryParam("offset") int offset,
@QueryParam("offset") int offset, @QueryParam("limit") int limit, @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) { @HeaderParam("If-Modified-Since") String ifModifiedSince) {
long ifModifiedSinceTimestamp; long ifModifiedSinceTimestamp;
long sinceTimestamp; long sinceTimestamp;
long timestamp = 0; long timestamp = 0;
boolean isIfModifiedSinceSet = false;
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("getActivities since: " + since + " , offset: " + offset + " ,limit: " + limit + " ," + log.debug("getActivities since: " + since + " , offset: " + offset + " ,limit: " + limit + " ," +
"ifModifiedSince: " + ifModifiedSince); "ifModifiedSince: " + ifModifiedSince);
@ -240,7 +249,6 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService
"Invalid date string is provided in 'If-Modified-Since' header").build()).build(); "Invalid date string is provided in 'If-Modified-Since' header").build()).build();
} }
ifModifiedSinceTimestamp = ifSinceDate.getTime(); ifModifiedSinceTimestamp = ifSinceDate.getTime();
isIfModifiedSinceSet = true;
timestamp = ifModifiedSinceTimestamp / 1000; timestamp = ifModifiedSinceTimestamp / 1000;
} else if (since != null && !since.isEmpty()) { } else if (since != null && !since.isEmpty()) {
Date sinceDate; Date sinceDate;
@ -266,41 +274,45 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService
} }
Response response = validateAdminUser(); Response response = validateAdminUser();
if (response == null) { if (response == null) {
List<Activity> activities;
int count = 0;
ActivityList activityList = new ActivityList(); ActivityList activityList = new ActivityList();
DeviceManagementProviderService dmService; DeviceManagementProviderService dmService;
ActivityPaginationRequest activityPaginationRequest = new ActivityPaginationRequest(offset, limit);
try { try {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Calling database to get activities."); log.debug("Calling database to get activities.");
} }
dmService = DeviceMgtAPIUtils.getDeviceManagementService(); dmService = DeviceMgtAPIUtils.getDeviceManagementService();
if (initiatedBy == null || initiatedBy.isEmpty()) { if (initiatedBy != null && !initiatedBy.isEmpty()) {
activities = dmService.getActivitiesUpdatedAfter(timestamp, limit, offset); activityPaginationRequest.setInitiatedBy(initiatedBy);
}
if (log.isDebugEnabled()) { if (operationCode != null && !operationCode.isEmpty()) {
log.debug("Calling database to get activity count with timestamp."); activityPaginationRequest.setOperationCode(operationCode);
} }
count = dmService.getActivityCountUpdatedAfter(timestamp); if (deviceType != null && !deviceType.isEmpty()) {
if (log.isDebugEnabled()) { activityPaginationRequest.setDeviceType(deviceType);
log.debug("Activity count: " + count); }
} if (deviceId != null && !deviceId.isEmpty()) {
} else { activityPaginationRequest.setDeviceId(deviceId);
activities = dmService.getActivitiesUpdatedAfterByUser(timestamp, initiatedBy, limit, offset); }
if (type != null && !type.isEmpty()) {
if (log.isDebugEnabled()) { activityPaginationRequest.setType(Operation.Type.valueOf(type.toUpperCase()));
log.debug("Calling database to get activity count with timestamp and user."); }
} if (status != null && !status.isEmpty()) {
count = dmService.getActivityCountUpdatedAfterByUser(timestamp, initiatedBy); activityPaginationRequest.setStatus(Operation.Status.valueOf(status.toUpperCase()));
if (log.isDebugEnabled()) { }
log.debug("Activity count: " + count); 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); activityList.setCount(count);
if (activities == null || activities.size() == 0) { if (log.isDebugEnabled()) {
if (isIfModifiedSinceSet) { log.debug("Filtered Activity count: " + count);
return Response.notModified().build(); if (count > 0) {
log.debug("Fetched Activity count: " + activityList.getList().size());
} }
} }
return Response.ok().entity(activityList).build(); 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; 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.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; 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.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult; 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 org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy;
import java.util.List; import java.util.List;
@ -121,9 +122,14 @@ public interface OperationManager {
List<Activity> getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementException; 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; List<Activity> getFilteredActivities(String operationCode, int limit, int offset) throws OperationManagementException;
int getTotalCountOfFilteredActivities(String operationCode) throws OperationManagementException; int getTotalCountOfFilteredActivities(String operationCode) throws OperationManagementException;
List<Activity> getActivitiesUpdatedAfterByUser(long timestamp, String user, int limit, int offset) throws OperationManagementException; List<Activity> getActivitiesUpdatedAfterByUser(long timestamp, String user, int limit, int offset) throws OperationManagementException;

@ -22,6 +22,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext; 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.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
@ -151,9 +152,7 @@ public class OperationManagerImpl implements OperationManager {
return null; return null;
} }
notificationStrategies.put(tenantId, provider.getNotificationStrategy(pushNoteConfig)); notificationStrategies.put(tenantId, provider.getNotificationStrategy(pushNoteConfig));
} else if (notificationStrategies.containsKey(tenantId)) { } else notificationStrategies.remove(tenantId);
notificationStrategies.remove(tenantId);
}
lastUpdatedTimeStamps.put(tenantId, Calendar.getInstance().getTimeInMillis()); lastUpdatedTimeStamps.put(tenantId, Calendar.getInstance().getTimeInMillis());
} }
return notificationStrategies.get(tenantId); 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 @Override
public List<Activity> getFilteredActivities(String operationCode, int limit, int offset) throws OperationManagementException { 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. org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation deviceSpecificOperation = operationDAO.
getOperationByDeviceAndId(enrolmentInfo.getId(), getOperationByDeviceAndId(enrolmentInfo.getId(),
operationId); operationId);
if (deviceSpecificOperation == null) { return deviceSpecificOperation != null;
return false;
} else {
return true;
}
} catch (OperationManagementDAOException e) { } catch (OperationManagementDAOException e) {
String msg = "Error occurred while checking if operation with operation id " String msg = "Error occurred while checking if operation with operation id "
+ operationId +" exist for " + deviceId.getType() + "' device '" + deviceId.getId() + "'"; + operationId +" exist for " + deviceId.getType() + "' device '" + deviceId.getId() + "'";

@ -18,6 +18,7 @@
*/ */
package org.wso2.carbon.device.mgt.core.operation.mgt.dao; 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.PaginationRequest;
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; 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 * This method provides operation mappings for given status
* @param opStatus Operation status *
* @param opStatus Operation status
* @param pushNotificationStatus Push notification Status * @param pushNotificationStatus Push notification Status
* @param limit Limit for no devices * @param limit Limit for no devices
* @return Tenant based operation mappings list * @return Tenant based operation mappings list
* @throws OperationManagementDAOException * @throws OperationManagementDAOException
*/ */
Map<Integer, List<OperationMapping>> getOperationMappingsByStatus(Operation.Status opStatus, Operation.PushNotificationStatus pushNotificationStatus, Map<Integer, List<OperationMapping>> getOperationMappingsByStatus(Operation.Status opStatus, Operation.PushNotificationStatus pushNotificationStatus,
int limit) throws OperationManagementDAOException; 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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext; 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.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.PaginationRequest; 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.Activity;
@ -46,12 +47,12 @@ import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; 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. * 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(); Connection conn = OperationManagementDAOFactory.getConnection();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
String sql = "SELECT " + String sql = "SELECT " +
" eom.ENROLMENT_ID," + " eom.ENROLMENT_ID," +
" eom.CREATED_TIMESTAMP," + " eom.CREATED_TIMESTAMP," +
" eom.UPDATED_TIMESTAMP," + " eom.UPDATED_TIMESTAMP," +
" eom.OPERATION_ID," + " eom.OPERATION_ID," +
" eom.OPERATION_CODE," + " eom.OPERATION_CODE," +
" eom.INITIATED_BY," + " eom.INITIATED_BY," +
" eom.TYPE," + " eom.TYPE," +
" eom.STATUS," + " eom.STATUS," +
" eom.DEVICE_ID," + " eom.DEVICE_ID," +
" eom.DEVICE_IDENTIFICATION," + " eom.DEVICE_IDENTIFICATION," +
" eom.DEVICE_TYPE," + " eom.DEVICE_TYPE," +
" opr.ID AS OP_RES_ID," + " opr.ID AS OP_RES_ID," +
" opr.RECEIVED_TIMESTAMP," + " opr.RECEIVED_TIMESTAMP," +
" opr.OPERATION_RESPONSE," + " opr.OPERATION_RESPONSE," +
" opr.IS_LARGE_RESPONSE " + " opr.IS_LARGE_RESPONSE " +
"FROM " + "FROM " +
" DM_ENROLMENT_OP_MAPPING eom " + " DM_ENROLMENT_OP_MAPPING eom " +
"LEFT JOIN " + "LEFT JOIN " +
" DM_DEVICE_OPERATION_RESPONSE opr ON opr.EN_OP_MAP_ID = eom.ID " + " DM_DEVICE_OPERATION_RESPONSE opr ON opr.EN_OP_MAP_ID = eom.ID " +
"INNER JOIN " + "INNER JOIN " +
" (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING ORDER BY OPERATION_ID ASC limit ? , ? ) eom_ordered " + " (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 " + " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " +
"WHERE " + "WHERE " +
" eom.UPDATED_TIMESTAMP > ? " + " eom.UPDATED_TIMESTAMP > ? " +
" AND eom.TENANT_ID = ? " + " AND eom.TENANT_ID = ? " +
" AND eom.INITIATED_BY = ? " + " AND eom.INITIATED_BY = ? " +
"ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP";
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, offset); stmt.setInt(1, offset);
@ -1012,32 +1013,32 @@ public class GenericOperationDAOImpl implements OperationDAO {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
String sql = "SELECT " + String sql = "SELECT " +
" eom.ENROLMENT_ID," + " eom.ENROLMENT_ID," +
" eom.CREATED_TIMESTAMP," + " eom.CREATED_TIMESTAMP," +
" eom.UPDATED_TIMESTAMP," + " eom.UPDATED_TIMESTAMP," +
" eom.OPERATION_ID," + " eom.OPERATION_ID," +
" eom.OPERATION_CODE," + " eom.OPERATION_CODE," +
" eom.INITIATED_BY," + " eom.INITIATED_BY," +
" eom.TYPE," + " eom.TYPE," +
" eom.STATUS," + " eom.STATUS," +
" eom.DEVICE_ID," + " eom.DEVICE_ID," +
" eom.DEVICE_IDENTIFICATION," + " eom.DEVICE_IDENTIFICATION," +
" eom.DEVICE_TYPE," + " eom.DEVICE_TYPE," +
" ops.ID AS OP_RES_ID," + " ops.ID AS OP_RES_ID," +
" ops.RECEIVED_TIMESTAMP," + " ops.RECEIVED_TIMESTAMP," +
" ops.OPERATION_RESPONSE," + " ops.OPERATION_RESPONSE," +
" ops.IS_LARGE_RESPONSE " + " ops.IS_LARGE_RESPONSE " +
"FROM " + "FROM " +
" DM_ENROLMENT_OP_MAPPING AS eom " + " DM_ENROLMENT_OP_MAPPING AS eom " +
"INNER JOIN " + "INNER JOIN " +
" (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING ORDER BY OPERATION_ID ASC limit ? , ? ) AS eom_ordered " + " (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 " + " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " +
"LEFT JOIN " + "LEFT JOIN " +
" DM_DEVICE_OPERATION_RESPONSE AS ops ON ops.EN_OP_MAP_ID = eom.ID " + " DM_DEVICE_OPERATION_RESPONSE AS ops ON ops.EN_OP_MAP_ID = eom.ID " +
"WHERE " + "WHERE " +
" eom.UPDATED_TIMESTAMP > ? " + " eom.UPDATED_TIMESTAMP > ? " +
" AND eom.TENANT_ID = ? " + " AND eom.TENANT_ID = ? " +
"ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP";
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, offset); stmt.setInt(1, offset);
@ -1056,7 +1057,7 @@ public class GenericOperationDAOImpl implements OperationDAO {
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while getting the operation details from the database."; 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); throw new OperationManagementDAOException(msg, e);
} }
} }
@ -1619,4 +1620,221 @@ public class GenericOperationDAOImpl implements OperationDAO {
} }
return operationMappingsTenantMap; 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; package org.wso2.carbon.device.mgt.core.service;
import org.apache.commons.collections.map.SingletonMap; 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.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.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.FeatureManager; 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.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult; 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.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.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.device.details.DeviceLocationHistorySnapshot;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException; 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.InvalidDeviceException;
import org.wso2.carbon.device.mgt.common.exceptions.UnauthorizedDeviceAccessException; 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.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.invitation.mgt.DeviceEnrollmentInvitationDetails;
import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.common.license.mgt.License;
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; 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) List<Device> getDevicesByIdentifiersAndStatuses(List<String> deviceIdentifiers, List<EnrolmentInfo.Status> statuses)
throws DeviceManagementException; 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.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException; 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.Device;
import org.wso2.carbon.device.mgt.common.DeviceEnrollmentInfoNotification; import org.wso2.carbon.device.mgt.common.DeviceEnrollmentInfoNotification;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
@ -2054,6 +2055,19 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return DeviceManagementDataHolder.getInstance().getOperationManager().getActivityCountUpdatedAfterByUser(timestamp, user); 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 @Override
public List<MonitoringOperation> getMonitoringOperationList(String deviceType) { public List<MonitoringOperation> getMonitoringOperationList(String deviceType) {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();

Loading…
Cancel
Save