Resolved more comments

appm_syncbr
prathabanKavin 5 months ago
parent af8894f950
commit 2b5e682cfa

@ -18,6 +18,9 @@
package io.entgra.device.mgt.core.device.mgt.core.dao.impl;
import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier;
import io.entgra.device.mgt.core.device.mgt.core.operation.mgt.dao.util.OperationDAOUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import io.entgra.device.mgt.core.device.mgt.common.Device;
import io.entgra.device.mgt.core.device.mgt.common.DeviceManagementConstants;
@ -40,6 +43,7 @@ import java.util.List;
import java.util.ArrayList;
public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
private static final Log log = LogFactory.getLog(AbstractEnrollmentDAOImpl.class);
@Override
public EnrolmentInfo addEnrollment(int deviceId, DeviceIdentifier deviceIdentifier, EnrolmentInfo enrolmentInfo,
@ -562,7 +566,7 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
@Override
public OwnerWithDeviceDTO getOwnersWithDevices(String owner, int tenantId)
throws DeviceManagementDAOException {
Connection conn = null;
OwnerWithDeviceDTO ownerDetails = new OwnerWithDeviceDTO();
List<Integer> deviceIds = new ArrayList<>();
int deviceCount = 0;
@ -572,22 +576,26 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
"JOIN DM_DEVICE d ON e.DEVICE_ID = d.ID " +
"WHERE e.OWNER = ? AND e.TENANT_ID = ?";
try (Connection conn = this.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, owner);
stmt.setInt(2, tenantId);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
if (ownerDetails.getUserName() == null) {
ownerDetails.setUserName(rs.getString("OWNER"));
try {
conn = this.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, owner);
stmt.setInt(2, tenantId);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
if (ownerDetails.getUserName() == null) {
ownerDetails.setUserName(rs.getString("OWNER"));
}
deviceIds.add(rs.getInt("DEVICE_ID"));
deviceCount++;
}
deviceIds.add(rs.getInt("DEVICE_ID"));
deviceCount++;
}
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving owners and device IDs for owner: " + owner, e);
String msg = "Error occurred while retrieving owners and device IDs for owner: " + owner;
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
ownerDetails.setDeviceIds(deviceIds);
@ -623,10 +631,10 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
}
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving owner and status for device ID: "
+ deviceId, e);
String msg = "Error occurred while retrieving owner and status for device ID: " + deviceId;
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
return deviceOwnerWithStatus;
}
@ -634,29 +642,30 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
public List<DeviceDetailsDTO> getDevicesByTenantId(int tenantId)
throws DeviceManagementDAOException {
List<DeviceDetailsDTO> devices = new ArrayList<>();
String sql = "SELECT DEVICE_ID, OWNER, STATUS " +
"FROM DM_ENROLMENT " +
"WHERE TENANT_ID = ?";
try (Connection conn = this.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, tenantId);
String sql = "SELECT DEVICE_ID, OWNER, STATUS FROM DM_ENROLMENT WHERE TENANT_ID = ?";
Connection conn = null;
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
DeviceDetailsDTO device = new DeviceDetailsDTO();
device.setDeviceId(rs.getInt("DEVICE_ID"));
device.setOwner(rs.getString("OWNER"));
device.setStatus(rs.getString("STATUS"));
devices.add(device);
try {
conn = this.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, tenantId);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
DeviceDetailsDTO device = new DeviceDetailsDTO();
device.setDeviceId(rs.getInt("DEVICE_ID"));
device.setOwner(rs.getString("OWNER"));
device.setStatus(rs.getString("STATUS"));
devices.add(device);
}
}
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving devices for tenant ID: "
+ tenantId, e);
String msg = "Error occurred while retrieving devices for tenant ID: " + tenantId;
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
return devices;
}
}

@ -165,7 +165,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving groups of groups IDs " + deviceGroupIds.toString()
+ " in tenant: " + tenantId;
+ " in tenant: " + tenantId;
log.error(msg);
throw new GroupManagementDAOException(msg, e);
}
@ -185,7 +185,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
for (int i = 0; i < deviceGroupIdsCount; i++) {
sql += (deviceGroupIdsCount - 1 != i) ? "?," : "?";
}
sql += ")";
sql += ")";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
int paramIndex = 1;
stmt.setInt(paramIndex++, tenantId);
@ -203,7 +203,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving groups of groups IDs " + deviceGroupIds
+ " in tenant: " + tenantId;
+ " in tenant: " + tenantId;
log.error(msg);
throw new GroupManagementDAOException(msg, e);
}
@ -228,7 +228,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
sql += " AND OWNER LIKE ?";
}
if (StringUtils.isNotBlank(request.getParentPath())) {
if(isWithParentPath){
if (isWithParentPath) {
sql += " AND PARENT_PATH LIKE ?";
}
}
@ -251,7 +251,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
stmt.setString(paramIndex++, request.getOwner() + "%");
}
if (StringUtils.isNotBlank(request.getParentPath())) {
if(isWithParentPath){
if (isWithParentPath) {
stmt.setString(paramIndex++, request.getParentPath());
}
}
@ -272,7 +272,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving groups of groups IDs " + deviceGroupIds.toString()
+ " in tenant: " + tenantId;
+ " in tenant: " + tenantId;
log.error(msg);
throw new GroupManagementDAOException(msg, e);
}
@ -485,7 +485,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, OWNER = ?, STATUS = ?, "
+ "PARENT_PATH = ?, PARENT_GROUP_ID = ? WHERE ID = ? AND TENANT_ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)){
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
for (DeviceGroup deviceGroup : deviceGroups) {
stmt.setString(1, deviceGroup.getDescription());
stmt.setString(2, deviceGroup.getName());
@ -610,6 +610,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
throw new GroupManagementDAOException(msg, e);
}
}
@Override
public void deleteGroups(List<Integer> groupIds, int tenantId) throws GroupManagementDAOException {
try {
@ -1167,7 +1168,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
if (StringUtils.isNotBlank(parentPath)) {
sql += " AND g.PARENT_PATH = ? ";
}
sql += "GROUP BY g.ID";
sql += "GROUP BY g.ID";
stmt = conn.prepareStatement(sql);
int index = 0;
while (index++ < rolesCount) {
@ -1280,7 +1281,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
return devices;
}
Connection conn = GroupManagementDAOFactory.getConnection();
StringJoiner joiner = new StringJoiner(",","SELECT "
StringJoiner joiner = new StringJoiner(",", "SELECT "
+ "d1.DEVICE_ID, "
+ "d1.DESCRIPTION, "
+ "d1.NAME AS DEVICE_NAME, "
@ -1445,12 +1446,11 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to get group details and device IDs for group: " + groupName);
}
GroupDetailsDTO groupDetails = new GroupDetailsDTO();
List<Integer> deviceIds = new ArrayList<>();
Map<Integer, String> deviceOwners = new HashMap<>();
Map<Integer, String> deviceStatuses = new HashMap<>();
Map<Integer, String> deviceNames = new HashMap<>(); // New map for device names
Map<Integer, String> deviceNames = new HashMap<>();
String sql =
"SELECT " +
@ -1471,32 +1471,35 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
" AND g.TENANT_ID = ? " +
"LIMIT ? OFFSET ?";
try (Connection conn = GroupManagementDAOFactory.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, groupName);
stmt.setInt(2, tenantId);
stmt.setInt(3, limit);
stmt.setInt(4, offset);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
if (groupDetails.getGroupId() == 0) {
groupDetails.setGroupId(rs.getInt("GROUP_ID"));
groupDetails.setGroupName(rs.getString("GROUP_NAME"));
groupDetails.setGroupOwner(rs.getString("GROUP_OWNER"));
Connection conn = null;
try {
conn = GroupManagementDAOFactory.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, groupName);
stmt.setInt(2, tenantId);
stmt.setInt(3, limit);
stmt.setInt(4, offset);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
if (groupDetails.getGroupId() == 0) {
groupDetails.setGroupId(rs.getInt("GROUP_ID"));
groupDetails.setGroupName(rs.getString("GROUP_NAME"));
groupDetails.setGroupOwner(rs.getString("GROUP_OWNER"));
}
int deviceId = rs.getInt("DEVICE_ID");
deviceIds.add(deviceId);
deviceOwners.put(deviceId, rs.getString("DEVICE_OWNER"));
deviceStatuses.put(deviceId, rs.getString("DEVICE_STATUS"));
deviceNames.put(deviceId, rs.getString("DEVICE_NAME"));
}
int deviceId = rs.getInt("DEVICE_ID");
deviceIds.add(deviceId);
deviceOwners.put(deviceId, rs.getString("DEVICE_OWNER"));
deviceStatuses.put(deviceId, rs.getString("DEVICE_STATUS"));
deviceNames.put(deviceId, rs.getString("DEVICE_NAME"));
}
}
} catch (SQLException e) {
throw new GroupManagementDAOException("Error occurred while retrieving group details and device IDs for group: "
+ groupName, e);
String msg = "Error occurred while retrieving group details and device IDs for group: " + groupName;
log.error(msg, e);
throw new GroupManagementDAOException(msg, e);
}
groupDetails.setDeviceIds(deviceIds);
groupDetails.setDeviceCount(deviceIds.size());
groupDetails.setDeviceOwners(deviceOwners);
@ -1504,4 +1507,4 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
groupDetails.setDeviceNames(deviceNames);
return groupDetails;
}
}
}

@ -5356,63 +5356,66 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
try {
DeviceManagementDAOFactory.openConnection();
ownerWithDeviceDTO = this.enrollmentDAO.getOwnersWithDevices(owner, tenantId);
} catch (DeviceManagementDAOException | SQLException e) {
String msg = "Error occurred while retrieving device IDs for owner: " + owner;
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
if (ownerWithDeviceDTO != null) {
if (ownerWithDeviceDTO == null) {
String msg = "No data found for owner: " + owner;
log.error(msg);
throw new DeviceManagementDAOException(msg);
}
List<Integer> deviceIds = ownerWithDeviceDTO.getDeviceIds();
if (deviceIds != null) {
ownerWithDeviceDTO.setDeviceCount(deviceIds.size());
} else {
ownerWithDeviceDTO.setDeviceCount(0);
}
} catch (DeviceManagementDAOException | SQLException e) {
String msg = "Error occurred while retrieving device IDs for owner: " + owner;
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
return ownerWithDeviceDTO;
}
@Override
public OwnerWithDeviceDTO getOwnerWithDeviceByDeviceId(int deviceId)
throws DeviceManagementDAOException {
public OwnerWithDeviceDTO getOwnerWithDeviceByDeviceId(int deviceId) throws DeviceManagementDAOException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
OwnerWithDeviceDTO deviceOwnerWithStatus;
try {
DeviceManagementDAOFactory.openConnection();
deviceOwnerWithStatus = enrollmentDAO.getOwnerWithDeviceByDeviceId(deviceId, tenantId);
} catch (DeviceManagementDAOException | SQLException e) {
String msg = "Error occurred while retrieving owner and status for device ID: " + deviceId;
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
if (deviceOwnerWithStatus != null) {
if (deviceOwnerWithStatus == null) {
throw new DeviceManagementDAOException("No data found for device ID: " + deviceId);
}
List<Integer> deviceIds = deviceOwnerWithStatus.getDeviceIds();
if (deviceIds != null) {
deviceOwnerWithStatus.setDeviceCount(deviceIds.size());
} else {
deviceOwnerWithStatus.setDeviceCount(0);
}
} catch (DeviceManagementDAOException | SQLException e) {
String msg = "Error occurred while retrieving owner and status for device ID: " + deviceId;
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
return deviceOwnerWithStatus;
}
@Override
public List<DeviceDetailsDTO> getDevicesByTenantId(int tenantId)
throws DeviceManagementDAOException {
public List<DeviceDetailsDTO> getDevicesByTenantId(int tenantId) throws DeviceManagementDAOException {
List<DeviceDetailsDTO> devices;
try {
DeviceManagementDAOFactory.openConnection();
devices = enrollmentDAO.getDevicesByTenantId(tenantId);
if (devices == null || devices.isEmpty()) {
String msg = "No devices found for tenant ID: " + tenantId;
log.error(msg);
throw new DeviceManagementDAOException(msg);
}
} catch (DeviceManagementDAOException | SQLException e) {
String msg = "Error occurred while retrieving devices for tenant ID: " + tenantId;
log.error(msg, e);
@ -5420,10 +5423,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} finally {
DeviceManagementDAOFactory.closeConnection();
}
return devices;
}
@Override
public OperationDTO getOperationDetailsById(int operationId) throws OperationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
@ -5431,6 +5434,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
try {
OperationManagementDAOFactory.openConnection();
operationDetails = this.operationDAO.getOperationDetailsById(operationId, tenantId);
if (operationDetails == null) {
String msg = "No operation details found for operation ID: " + operationId;
log.error(msg);
throw new OperationManagementException(msg);
}
} catch (SQLException | OperationManagementDAOException e) {
String msg = "Error occurred while retrieving operation details for operation ID: " + operationId;
log.error(msg, e);
@ -5438,7 +5446,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} finally {
OperationManagementDAOFactory.closeConnection();
}
return operationDetails;
}

Loading…
Cancel
Save