Rearrange getOperations by eliminating the necessity of the owner param

device-apps-api
Sasini Sandamali 1 year ago
parent 9ac38127b9
commit b9ecb8ce40

@ -1760,7 +1760,6 @@ public interface DeviceManagementService {
@ApiParam(
name = "owner",
value = "Provides the owner of the required device.",
required = true,
defaultValue = "")
@QueryParam("owner")
String owner,

@ -1070,10 +1070,11 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
@QueryParam("operationStatus") List<String> status) {
OperationList operationsList = new OperationList();
RequestValidationUtil requestValidationUtil = new RequestValidationUtil();
RequestValidationUtil.validateOwnerParameter(owner);
RequestValidationUtil.validatePaginationParameters(offset, limit);
PaginationRequest request = new PaginationRequest(offset, limit);
request.setOwner(owner);
if(owner != null){
request.setOwner(owner);
}
try {
//validating the operation log filters
OperationLogFilters olf = requestValidationUtil.validateOperationLogFilters(operationCode, createdFrom,

@ -641,23 +641,34 @@ public class OperationManagerImpl implements OperationManager {
deviceId.getType() + "' device, which carries the identifier '" +
deviceId.getId() + "' of owner '" + owner + "'");
}
EnrolmentInfo enrolmentInfo = this.getEnrolmentInfo(deviceId, request);
if (enrolmentInfo == null){
throw new OperationManagementException("Enrollment info not found for given device which has device "
+ "Identifier:" + deviceId.getId() + " and device type: " + deviceId.getType() + "Further, device "
+ "is own to: " + owner);
paginationResult = new PaginationResult();
int enrolmentId = 0;
List<? extends io.entgra.device.mgt.core.device.mgt.core.dto.operation.mgt.Operation> operationList;
int count;
if (owner != null) {
EnrolmentInfo enrolmentInfo = this.getEnrolmentInfo(deviceId, request);
if (enrolmentInfo == null) {
throw new OperationManagementException("Enrollment info not found for given device which has device "
+ "Identifier:" + deviceId.getId() + " and device type: " + deviceId.getType() + "Further, device "
+ "is own to: " + owner);
}
enrolmentId = enrolmentInfo.getId();
}
int enrolmentId = enrolmentInfo.getId();
try {
OperationManagementDAOFactory.openConnection();
List<? extends io.entgra.device.mgt.core.device.mgt.core.dto.operation.mgt.Operation> operationList =
operationDAO.getOperationsForDevice(enrolmentId, request);
if (owner != null) {
operationList = operationDAO.getOperationsForDevice(enrolmentId, request);
count = operationDAO.getOperationCountForDevice(enrolmentId, request);
} else {
operationList = operationDAO.getOperationsForDeviceByDeviceIdentifier(deviceId, request);
count = operationDAO.getOperationCountForDeviceWithDeviceIdentifier(deviceId, request);
}
for (io.entgra.device.mgt.core.device.mgt.core.dto.operation.mgt.Operation dtoOperation : operationList) {
Operation operation = OperationDAOUtil.convertOperation(dtoOperation);
operations.add(operation);
}
paginationResult = new PaginationResult();
int count = operationDAO.getOperationCountForDevice(enrolmentId, request);
paginationResult.setData(operations);
paginationResult.setRecordsTotal(count);
paginationResult.setRecordsFiltered(count);
@ -1587,7 +1598,7 @@ public class OperationManagerImpl implements OperationManager {
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() + "'";
+ operationId + " exist for " + deviceId.getType() + "' device '" + deviceId.getId() + "'";
log.error(msg, e);
throw new OperationManagementException(msg, e);
} catch (SQLException e) {

@ -18,6 +18,7 @@
package io.entgra.device.mgt.core.device.mgt.core.operation.mgt.dao;
import io.entgra.device.mgt.core.device.mgt.common.ActivityPaginationRequest;
import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier;
import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest;
import io.entgra.device.mgt.core.device.mgt.common.operation.mgt.Activity;
import io.entgra.device.mgt.core.device.mgt.common.operation.mgt.OperationResponse;
@ -46,8 +47,12 @@ public interface OperationDAO {
int getOperationCountForDevice(int enrolmentId, PaginationRequest request) throws OperationManagementDAOException;
int getOperationCountForDeviceWithDeviceIdentifier(DeviceIdentifier deviceId, PaginationRequest request) throws OperationManagementDAOException;
List<? extends Operation> getOperationsForDevice(int enrolmentId, PaginationRequest request) throws OperationManagementDAOException;
List<? extends Operation> getOperationsForDeviceByDeviceIdentifier(DeviceIdentifier deviceId, PaginationRequest request) throws OperationManagementDAOException;
Operation getNextOperation(int enrolmentId, Operation.Status status) throws OperationManagementDAOException;
boolean updateOperationStatus(int enrolmentId, int operationId,Operation.Status status)

@ -1486,6 +1486,146 @@ public class GenericOperationDAOImpl implements OperationDAO {
return operations;
}
@Override
public List<? extends Operation> getOperationsForDeviceByDeviceIdentifier(DeviceIdentifier deviceId, PaginationRequest request)
throws OperationManagementDAOException {
Operation operation;
List<Operation> operations = new ArrayList<>();
String createdTo = null;
String createdFrom = null;
DateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
boolean isCreatedDayProvided = false;
boolean isUpdatedDayProvided = false; //updated day = received day
boolean isOperationCodeProvided = false;
boolean isStatusProvided = false;
if (request.getOperationLogFilters().getCreatedDayFrom() != null) {
createdFrom = simple.format(request.getOperationLogFilters().getCreatedDayFrom());
}
if (request.getOperationLogFilters().getCreatedDayTo() != null) {
createdTo = simple.format(request.getOperationLogFilters().getCreatedDayTo());
}
Long updatedFrom = request.getOperationLogFilters().getUpdatedDayFrom();
Long updatedTo = request.getOperationLogFilters().getUpdatedDayTo();
List<String> operationCode = request.getOperationLogFilters().getOperationCode();
List<String> status = request.getOperationLogFilters().getStatus();
StringBuilder sql = new StringBuilder("SELECT " +
"o.ID, " +
"TYPE, " +
"o.CREATED_TIMESTAMP, " +
"o.RECEIVED_TIMESTAMP, " +
"o.OPERATION_CODE, " +
"o.INITIATED_BY, " +
"om.STATUS, " +
"om.ID AS OM_MAPPING_ID, " +
"om.UPDATED_TIMESTAMP " +
"FROM " +
"DM_OPERATION o " +
"INNER JOIN " +
"(SELECT dm.OPERATION_ID, " +
"dm.ID, " +
"dm.STATUS, " +
"dm.UPDATED_TIMESTAMP " +
"FROM " +
"DM_ENROLMENT_OP_MAPPING dm " +
"WHERE " +
"dm.DEVICE_IDENTIFICATION = ?");
if (updatedFrom != null && updatedFrom != 0 && updatedTo != null && updatedTo != 0) {
sql.append(" AND dm.UPDATED_TIMESTAMP BETWEEN ? AND ?");
isUpdatedDayProvided = true;
}
sql.append(") om ON o.ID = om.OPERATION_ID ");
if (createdFrom != null && !createdFrom.isEmpty() && createdTo != null && !createdTo.isEmpty()) {
sql.append(" WHERE o.CREATED_TIMESTAMP BETWEEN ? AND ?");
isCreatedDayProvided = true;
}
if ((isCreatedDayProvided) && (status != null && !status.isEmpty())) {
int size = status.size();
sql.append(" AND (om.STATUS = ? ");
for (int i = 0; i < size - 1; i++) {
sql.append(" OR om.STATUS = ?");
}
sql.append(")");
isStatusProvided = true;
} else if ((!isCreatedDayProvided) && (status != null && !status.isEmpty())) {
int size = status.size();
sql.append(" WHERE (om.STATUS = ? ");
for (int i = 0; i < size - 1; i++) {
sql.append(" OR om.STATUS = ?");
}
sql.append(")");
isStatusProvided = true;
}
if ((isCreatedDayProvided || isStatusProvided) && (operationCode != null && !operationCode.isEmpty())) {
int size = operationCode.size();
sql.append(" AND (o.OPERATION_CODE = ? ");
for (int i = 0; i < size - 1; i++) {
sql.append(" OR o.OPERATION_CODE = ?");
}
sql.append(")");
isOperationCodeProvided = true;
} else if ((!isCreatedDayProvided && !isStatusProvided) && (operationCode != null && !operationCode.isEmpty())) {
int size = operationCode.size();
sql.append(" WHERE (o.OPERATION_CODE = ? ");
for (int i = 0; i < size - 1; i++) {
sql.append(" OR o.OPERATION_CODE = ?");
}
sql.append(")");
isOperationCodeProvided = true;
}
sql.append(" ORDER BY o.CREATED_TIMESTAMP DESC LIMIT ?,?");
try {
Connection conn = OperationManagementDAOFactory.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql.toString())) {
int paramIndex = 1;
stmt.setString(paramIndex++, deviceId.getId());
if (isUpdatedDayProvided) {
stmt.setLong(paramIndex++, updatedFrom);
stmt.setLong(paramIndex++, updatedTo);
}
if (isCreatedDayProvided) {
stmt.setString(paramIndex++, createdFrom);
stmt.setString(paramIndex++, createdTo);
}
if (isStatusProvided) {
for (String s : status) {
stmt.setString(paramIndex++, s);
}
}
if (isOperationCodeProvided) {
for (String s : operationCode) {
stmt.setString(paramIndex++, s);
}
}
stmt.setInt(paramIndex++, request.getStartIndex());
stmt.setInt(paramIndex, request.getRowCount());
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
operation = new Operation();
operation.setId(rs.getInt("ID"));
operation.setType(Operation.Type.valueOf(rs.getString("TYPE")));
operation.setCreatedTimeStamp(new Timestamp(rs.getLong("CREATED_TIMESTAMP") * 1000L).toString());
if (rs.getLong("UPDATED_TIMESTAMP") == 0) {
operation.setReceivedTimeStamp("");
} else {
operation.setReceivedTimeStamp(
new Timestamp((rs.getLong("UPDATED_TIMESTAMP") * 1000)).toString());
}
operation.setCode(rs.getString("OPERATION_CODE"));
operation.setInitiatedBy(rs.getString("INITIATED_BY"));
operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS")));
OperationDAOUtil.setActivityId(operation, rs.getInt("ID"));
operations.add(operation);
}
}
}
} catch (SQLException e) {
throw new OperationManagementDAOException("SQL error occurred while retrieving the operation " +
"available for the device'" + deviceId + "' with status '", e);
}
return operations;
}
@Override
public int getOperationCountForDevice(int enrolmentId, PaginationRequest request)
throws OperationManagementDAOException {
@ -1592,6 +1732,112 @@ public class GenericOperationDAOImpl implements OperationDAO {
return 0;
}
@Override
public int getOperationCountForDeviceWithDeviceIdentifier(DeviceIdentifier deviceId, PaginationRequest request)
throws OperationManagementDAOException {
String createdTo = null;
String createdFrom = null;
DateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
if (request.getOperationLogFilters().getCreatedDayFrom() != null) {
createdFrom = simple.format(request.getOperationLogFilters().getCreatedDayFrom());
}
if (request.getOperationLogFilters().getCreatedDayTo() != null) {
createdTo = simple.format(request.getOperationLogFilters().getCreatedDayTo());
}
Long updatedFrom = request.getOperationLogFilters().getUpdatedDayFrom();
Long updatedTo = request.getOperationLogFilters().getUpdatedDayTo();
List<String> operationCodes = request.getOperationLogFilters().getOperationCode();
List<String> status = request.getOperationLogFilters().getStatus();
boolean isCreatedDayProvided = false;
boolean isUpdatedDayProvided = false;
boolean isOperationCodeProvided = false;
boolean isStatusProvided = false;
String sql = "SELECT "
+ "COUNT(o.ID) AS OPERATION_COUNT "
+ "FROM "
+ "DM_OPERATION o "
+ "INNER JOIN "
+ "(SELECT dm.OPERATION_ID, "
+ "dm.ID, "
+ "dm.STATUS, "
+ "dm.UPDATED_TIMESTAMP "
+ "FROM "
+ "DM_ENROLMENT_OP_MAPPING dm "
+ "WHERE "
+ "dm.DEVICE_IDENTIFICATION = ?";
if (updatedFrom != null && updatedFrom != 0 && updatedTo != null && updatedTo != 0) {
sql += " AND dm.UPDATED_TIMESTAMP BETWEEN ? AND ?";
isUpdatedDayProvided = true;
}
sql += ") om ON o.ID = om.OPERATION_ID ";
if (createdFrom != null && !createdFrom.isEmpty() && createdTo != null && !createdTo.isEmpty()) {
sql += " WHERE o.CREATED_TIMESTAMP BETWEEN ? AND ?";
isCreatedDayProvided = true;
}
if (status != null && !status.isEmpty()) {
if (isCreatedDayProvided) {
sql += " AND (om.STATUS = ? ";
} else {
sql += " WHERE (om.STATUS = ? ";
}
sql = IntStream.range(0, status.size() - 1).mapToObj(i -> " OR om.STATUS = ?")
.collect(Collectors.joining("", sql, ""));
sql += ")";
isStatusProvided = true;
}
if (operationCodes != null && !operationCodes.isEmpty()) {
if (isCreatedDayProvided || isStatusProvided) {
sql += " AND (o.OPERATION_CODE = ? ";
} else {
sql += " WHERE (o.OPERATION_CODE = ? ";
}
sql = IntStream.range(0, operationCodes.size() - 1).mapToObj(i -> " OR o.OPERATION_CODE = ?")
.collect(Collectors.joining("", sql, ""));
sql += ")";
isOperationCodeProvided = true;
}
try {
Connection conn = OperationManagementDAOFactory.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
int paramIndex = 1;
stmt.setString(paramIndex++, deviceId.getId());
if (isUpdatedDayProvided) {
stmt.setLong(paramIndex++, updatedFrom);
stmt.setLong(paramIndex++, updatedTo);
}
if (isCreatedDayProvided) {
stmt.setString(paramIndex++, createdFrom);
stmt.setString(paramIndex++, createdTo);
}
if (isStatusProvided) {
for (String s : status) {
stmt.setString(paramIndex++, s);
}
}
if (isOperationCodeProvided) {
for (String s : operationCodes) {
stmt.setString(paramIndex++, s);
}
}
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
return rs.getInt("OPERATION_COUNT");
}
}
}
} catch (SQLException e) {
String msg = "SQL error occurred while retrieving the operation count of the device" + deviceId
+ " for search query";
log.error(msg, e);
throw new OperationManagementDAOException(msg, e);
}
return 0;
}
@Override
public Operation getNextOperation(int enrolmentId, Operation.Status status) throws OperationManagementDAOException {
PreparedStatement stmt = null;

@ -127,6 +127,7 @@ public class OperationDAOUtil {
operation.setEnabled(dtoOperation.isEnabled());
operation.setProperties(dtoOperation.getProperties());
operation.setActivityId(dtoOperation.getActivityId());
operation.setInitiatedBy(dtoOperation.getInitiatedBy());
return operation;

Loading…
Cancel
Save