Allow retrieving activities for multiple device ids

feature/traccar-sync
Charitha Goonetilleke 3 years ago
parent d7ccaa559b
commit 0284c955cf

@ -48,6 +48,7 @@ import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.List;
/** /**
* Activity related REST-API implementation. * Activity related REST-API implementation.
@ -461,7 +462,7 @@ public interface ActivityInfoProviderService {
name = "deviceId", name = "deviceId",
value = "Device Id to filter" value = "Device Id to filter"
) )
@QueryParam("deviceId") String deviceId, @QueryParam("deviceId") List<String> deviceIds,
@ApiParam( @ApiParam(
name = "type", name = "type",
value = "Operation type to filter" value = "Operation type to filter"

@ -225,7 +225,7 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService
@QueryParam("initiatedBy") String initiatedBy, @QueryParam("initiatedBy") String initiatedBy,
@QueryParam("operationCode") String operationCode, @QueryParam("operationCode") String operationCode,
@QueryParam("deviceType") String deviceType, @QueryParam("deviceType") String deviceType,
@QueryParam("deviceId") String deviceId, @QueryParam("deviceId") List<String> deviceIds,
@QueryParam("type") String type, @QueryParam("type") String type,
@QueryParam("status") String status, @QueryParam("status") String status,
@HeaderParam("If-Modified-Since") String ifModifiedSince, @HeaderParam("If-Modified-Since") String ifModifiedSince,
@ -297,8 +297,8 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService
if (deviceType != null && !deviceType.isEmpty()) { if (deviceType != null && !deviceType.isEmpty()) {
activityPaginationRequest.setDeviceType(deviceType); activityPaginationRequest.setDeviceType(deviceType);
} }
if (deviceId != null && !deviceId.isEmpty()) { if (deviceIds != null && !deviceIds.isEmpty()) {
activityPaginationRequest.setDeviceId(deviceId); activityPaginationRequest.setDeviceIds(deviceIds);
} }
if (type != null && !type.isEmpty()) { if (type != null && !type.isEmpty()) {
activityPaginationRequest.setType(Operation.Type.valueOf(type.toUpperCase())); activityPaginationRequest.setType(Operation.Type.valueOf(type.toUpperCase()));

@ -20,6 +20,8 @@ package org.wso2.carbon.device.mgt.common;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import java.util.List;
/** /**
* This class holds required parameters for a querying a paginated activity response. * This class holds required parameters for a querying a paginated activity response.
*/ */
@ -28,7 +30,7 @@ public class ActivityPaginationRequest {
private int offset; private int offset;
private int limit; private int limit;
private String deviceType; private String deviceType;
private String deviceId; private List<String> deviceIds;
private String operationCode; private String operationCode;
private String initiatedBy; private String initiatedBy;
private long since; private long since;
@ -66,12 +68,12 @@ public class ActivityPaginationRequest {
this.deviceType = deviceType; this.deviceType = deviceType;
} }
public String getDeviceId() { public List<String> getDeviceIds() {
return deviceId; return deviceIds;
} }
public void setDeviceId(String deviceId) { public void setDeviceIds(List<String> deviceIds) {
this.deviceId = deviceId; this.deviceIds = deviceIds;
} }
public String getOperationCode() { public String getOperationCode() {

@ -1724,7 +1724,7 @@ public class GenericOperationDAOImpl implements OperationDAO {
boolean isTimeDurationFilteringProvided = false; boolean isTimeDurationFilteringProvided = false;
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
String sql = "SELECT " + StringBuilder sql = new StringBuilder("SELECT " +
" eom.ENROLMENT_ID," + " eom.ENROLMENT_ID," +
" eom.CREATED_TIMESTAMP," + " eom.CREATED_TIMESTAMP," +
" eom.UPDATED_TIMESTAMP," + " eom.UPDATED_TIMESTAMP," +
@ -1745,72 +1745,82 @@ public class GenericOperationDAOImpl implements OperationDAO {
"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 WHERE TENANT_ID = ? "; " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING WHERE TENANT_ID = ? ");
if (activityPaginationRequest.getDeviceType() != null) { if (activityPaginationRequest.getDeviceType() != null) {
sql += "AND DEVICE_TYPE = ? "; sql.append("AND DEVICE_TYPE = ? ");
}
if (activityPaginationRequest.getDeviceIds() != null && !activityPaginationRequest.getDeviceIds().isEmpty()) {
sql.append("AND DEVICE_IDENTIFICATION IN (");
for (int i = 0; i < activityPaginationRequest.getDeviceIds().size() - 1; i++) {
sql.append("?, ");
} }
if (activityPaginationRequest.getDeviceId() != null) { sql.append("?) ");
sql += "AND DEVICE_IDENTIFICATION = ? ";
} }
if (activityPaginationRequest.getOperationCode() != null) { if (activityPaginationRequest.getOperationCode() != null) {
sql += "AND OPERATION_CODE = ? "; sql.append("AND OPERATION_CODE = ? ");
} }
if (activityPaginationRequest.getInitiatedBy() != null) { if (activityPaginationRequest.getInitiatedBy() != null) {
sql += "AND INITIATED_BY = ? "; sql.append("AND INITIATED_BY = ? ");
} }
if (activityPaginationRequest.getSince() != 0) { if (activityPaginationRequest.getSince() != 0) {
sql += "AND UPDATED_TIMESTAMP > ? "; sql.append("AND UPDATED_TIMESTAMP > ? ");
} }
if (activityPaginationRequest.getStartTimestamp() > 0 && activityPaginationRequest.getEndTimestamp() > 0) { if (activityPaginationRequest.getStartTimestamp() > 0 && activityPaginationRequest.getEndTimestamp() > 0) {
isTimeDurationFilteringProvided = true; isTimeDurationFilteringProvided = true;
sql += "AND CREATED_TIMESTAMP BETWEEN ? AND ? "; sql.append("AND CREATED_TIMESTAMP BETWEEN ? AND ? ");
} }
if (activityPaginationRequest.getType() != null) { if (activityPaginationRequest.getType() != null) {
sql += "AND TYPE = ? "; sql.append("AND TYPE = ? ");
} }
if (activityPaginationRequest.getStatus() != null) { if (activityPaginationRequest.getStatus() != null) {
sql += "AND STATUS = ? "; sql.append("AND STATUS = ? ");
} }
sql += "ORDER BY OPERATION_ID ASC limit ? , ? ) eom_ordered " + sql.append("ORDER BY OPERATION_ID ASC limit ? , ? ) eom_ordered " +
"ON eom_ordered.OPERATION_ID = eom.OPERATION_ID WHERE eom.TENANT_ID = ? "; "ON eom_ordered.OPERATION_ID = eom.OPERATION_ID WHERE eom.TENANT_ID = ? ");
if (activityPaginationRequest.getDeviceType() != null) { if (activityPaginationRequest.getDeviceType() != null) {
sql += "AND eom.DEVICE_TYPE = ? "; sql.append("AND eom.DEVICE_TYPE = ? ");
}
if (activityPaginationRequest.getDeviceIds() != null && !activityPaginationRequest.getDeviceIds().isEmpty()) {
sql.append("AND eom.DEVICE_IDENTIFICATION IN (");
for (int i = 0; i < activityPaginationRequest.getDeviceIds().size() - 1; i++) {
sql.append("?, ");
} }
if (activityPaginationRequest.getDeviceId() != null) { sql.append("?) ");
sql += "AND eom.DEVICE_IDENTIFICATION = ? ";
} }
if (activityPaginationRequest.getOperationCode() != null) { if (activityPaginationRequest.getOperationCode() != null) {
sql += "AND eom.OPERATION_CODE = ? "; sql.append("AND eom.OPERATION_CODE = ? ");
} }
if (activityPaginationRequest.getInitiatedBy() != null) { if (activityPaginationRequest.getInitiatedBy() != null) {
sql += "AND eom.INITIATED_BY = ? "; sql.append("AND eom.INITIATED_BY = ? ");
} }
if (activityPaginationRequest.getSince() != 0) { if (activityPaginationRequest.getSince() != 0) {
sql += "AND eom.UPDATED_TIMESTAMP > ? "; sql.append("AND eom.UPDATED_TIMESTAMP > ? ");
} }
if (isTimeDurationFilteringProvided) { if (isTimeDurationFilteringProvided) {
sql += "AND eom.CREATED_TIMESTAMP BETWEEN ? AND ? "; sql.append("AND eom.CREATED_TIMESTAMP BETWEEN ? AND ? ");
} }
if (activityPaginationRequest.getType() != null) { if (activityPaginationRequest.getType() != null) {
sql += "AND eom.TYPE = ? "; sql.append("AND eom.TYPE = ? ");
} }
if (activityPaginationRequest.getStatus() != null) { if (activityPaginationRequest.getStatus() != null) {
sql += "AND eom.STATUS = ? "; sql.append("AND eom.STATUS = ? ");
} }
sql += "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; sql.append("ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP");
int index = 1; int index = 1;
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql.toString())) {
stmt.setInt(index++, tenantId); stmt.setInt(index++, tenantId);
if (activityPaginationRequest.getDeviceType() != null) { if (activityPaginationRequest.getDeviceType() != null) {
stmt.setString(index++, activityPaginationRequest.getDeviceType()); stmt.setString(index++, activityPaginationRequest.getDeviceType());
} }
if (activityPaginationRequest.getDeviceId() != null) { if (activityPaginationRequest.getDeviceIds() != null && !activityPaginationRequest.getDeviceIds().isEmpty()) {
stmt.setString(index++, activityPaginationRequest.getDeviceId()); for (String deviceId : activityPaginationRequest.getDeviceIds()) {
stmt.setString(index++, deviceId);
}
} }
if (activityPaginationRequest.getOperationCode() != null) { if (activityPaginationRequest.getOperationCode() != null) {
stmt.setString(index++, activityPaginationRequest.getOperationCode()); stmt.setString(index++, activityPaginationRequest.getOperationCode());
@ -1839,8 +1849,10 @@ public class GenericOperationDAOImpl implements OperationDAO {
if (activityPaginationRequest.getDeviceType() != null) { if (activityPaginationRequest.getDeviceType() != null) {
stmt.setString(index++, activityPaginationRequest.getDeviceType()); stmt.setString(index++, activityPaginationRequest.getDeviceType());
} }
if (activityPaginationRequest.getDeviceId() != null) { if (activityPaginationRequest.getDeviceIds() != null && !activityPaginationRequest.getDeviceIds().isEmpty()) {
stmt.setString(index++, activityPaginationRequest.getDeviceId()); for (String deviceId : activityPaginationRequest.getDeviceIds()) {
stmt.setString(index++, deviceId);
}
} }
if (activityPaginationRequest.getOperationCode() != null) { if (activityPaginationRequest.getOperationCode() != null) {
stmt.setString(index++, activityPaginationRequest.getOperationCode()); stmt.setString(index++, activityPaginationRequest.getOperationCode());
@ -1886,43 +1898,49 @@ public class GenericOperationDAOImpl implements OperationDAO {
boolean isTimeDurationFilteringProvided = false; boolean isTimeDurationFilteringProvided = false;
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
String sql = "SELECT count(DISTINCT OPERATION_ID) AS ACTIVITY_COUNT FROM DM_ENROLMENT_OP_MAPPING " + StringBuilder sql = new StringBuilder("SELECT count(DISTINCT OPERATION_ID) AS ACTIVITY_COUNT " +
"WHERE TENANT_ID = ? "; "FROM DM_ENROLMENT_OP_MAPPING WHERE TENANT_ID = ? ");
if (activityPaginationRequest.getDeviceType() != null) { if (activityPaginationRequest.getDeviceType() != null) {
sql += "AND DEVICE_TYPE = ? "; sql.append("AND DEVICE_TYPE = ? ");
}
if (activityPaginationRequest.getDeviceIds() != null && !activityPaginationRequest.getDeviceIds().isEmpty()) {
sql.append("AND DEVICE_IDENTIFICATION IN (");
for (int i = 0; i < activityPaginationRequest.getDeviceIds().size() - 1; i++) {
sql.append("?, ");
} }
if (activityPaginationRequest.getDeviceId() != null) { sql.append("?) ");
sql += "AND DEVICE_IDENTIFICATION = ? ";
} }
if (activityPaginationRequest.getOperationCode() != null) { if (activityPaginationRequest.getOperationCode() != null) {
sql += "AND OPERATION_CODE = ? "; sql.append("AND OPERATION_CODE = ? ");
} }
if (activityPaginationRequest.getInitiatedBy() != null) { if (activityPaginationRequest.getInitiatedBy() != null) {
sql += "AND INITIATED_BY = ? "; sql.append("AND INITIATED_BY = ? ");
} }
if (activityPaginationRequest.getSince() != 0) { if (activityPaginationRequest.getSince() != 0) {
sql += "AND UPDATED_TIMESTAMP > ? "; sql.append("AND UPDATED_TIMESTAMP > ? ");
} }
if (activityPaginationRequest.getType() != null) { if (activityPaginationRequest.getType() != null) {
sql += "AND TYPE = ? "; sql.append("AND TYPE = ? ");
} }
if (activityPaginationRequest.getStatus() != null) { if (activityPaginationRequest.getStatus() != null) {
sql += "AND STATUS = ? "; sql.append("AND STATUS = ? ");
} }
if (activityPaginationRequest.getStartTimestamp() > 0 && activityPaginationRequest.getEndTimestamp() > 0) { if (activityPaginationRequest.getStartTimestamp() > 0 && activityPaginationRequest.getEndTimestamp() > 0) {
isTimeDurationFilteringProvided = true; isTimeDurationFilteringProvided = true;
sql += "AND CREATED_TIMESTAMP BETWEEN ? AND ? "; sql.append("AND CREATED_TIMESTAMP BETWEEN ? AND ? ");
} }
int index = 1; int index = 1;
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql.toString())) {
stmt.setInt(index++, tenantId); stmt.setInt(index++, tenantId);
if (activityPaginationRequest.getDeviceType() != null) { if (activityPaginationRequest.getDeviceType() != null) {
stmt.setString(index++, activityPaginationRequest.getDeviceType()); stmt.setString(index++, activityPaginationRequest.getDeviceType());
} }
if (activityPaginationRequest.getDeviceId() != null) { if (activityPaginationRequest.getDeviceIds() != null && !activityPaginationRequest.getDeviceIds().isEmpty()) {
stmt.setString(index++, activityPaginationRequest.getDeviceId()); for (String deviceId : activityPaginationRequest.getDeviceIds()) {
stmt.setString(index++, deviceId);
}
} }
if (activityPaginationRequest.getOperationCode() != null) { if (activityPaginationRequest.getOperationCode() != null) {
stmt.setString(index++, activityPaginationRequest.getOperationCode()); stmt.setString(index++, activityPaginationRequest.getOperationCode());

Loading…
Cancel
Save