diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/PolicyManagementService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/PolicyManagementService.java index 8cce7e92b3..4abcf68617 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/PolicyManagementService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/PolicyManagementService.java @@ -934,6 +934,12 @@ public interface PolicyManagementService { required = false) @QueryParam("status") String status, + @ApiParam( + name = "deviceType", + value = "The device type of the policy that needs filtering.", + required = false) + @QueryParam("deviceType") + String deviceType, @ApiParam( name = "If-Modified-Since", value = "Checks if the requested variant was modified, since the specified date-time. \n" + diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/PolicyManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/PolicyManagementServiceImpl.java index 311b4292a7..d882018073 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/PolicyManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/PolicyManagementServiceImpl.java @@ -488,6 +488,7 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { @QueryParam("name") String name, @QueryParam("type") String type, @QueryParam("status") String status, + @QueryParam("deviceType") String deviceType, @HeaderParam("If-Modified-Since") String ifModifiedSince, @QueryParam("offset") int offset, @QueryParam("limit") int limit) { @@ -505,6 +506,9 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { if (status != null){ request.setStatus(status); } + if (deviceType != null) { + request.setDeviceType(deviceType); + } try { PolicyAdministratorPoint policyAdministratorPoint = policyManagementService.getPAP(); policies = policyAdministratorPoint.getPolicyList(request); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/PolicyPaginationRequest.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/PolicyPaginationRequest.java index 8a0a2f3bd4..ddbc3a51a7 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/PolicyPaginationRequest.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/PolicyPaginationRequest.java @@ -24,6 +24,7 @@ public class PolicyPaginationRequest { private String name; private String type; private String status; + private String deviceType; public PolicyPaginationRequest(int start, int rowCount) { this.startIndex = start; @@ -70,6 +71,14 @@ public class PolicyPaginationRequest { this.status = status; } + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + @Override public String toString() { return "Group Name '" + this.name + "' num of rows: " + this.rowCount + " start index: " + this.startIndex; diff --git a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/AbstractPolicyDAOImpl.java b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/AbstractPolicyDAOImpl.java index f69e672cf7..9a7dcc8cb8 100644 --- a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/AbstractPolicyDAOImpl.java +++ b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/AbstractPolicyDAOImpl.java @@ -27,6 +27,7 @@ import io.entgra.device.mgt.core.device.mgt.common.policy.mgt.CorrectiveAction; import io.entgra.device.mgt.core.device.mgt.common.policy.mgt.DeviceGroupWrapper; import io.entgra.device.mgt.core.device.mgt.common.policy.mgt.Policy; import io.entgra.device.mgt.core.device.mgt.common.policy.mgt.PolicyCriterion; +import io.entgra.device.mgt.core.device.mgt.common.policy.mgt.Profile; import io.entgra.device.mgt.core.policy.mgt.common.Criterion; import io.entgra.device.mgt.core.policy.mgt.core.dao.PolicyDAO; import io.entgra.device.mgt.core.policy.mgt.core.dao.PolicyManagementDAOFactory; @@ -1819,4 +1820,66 @@ public abstract class AbstractPolicyDAOImpl implements PolicyDAO { } return policies; } + + /** + * Extracts a list of Policy objects with associated Profile objects from the given ResultSet + * + * @param resultSet The ResultSet containing the policy and profile data + * @param tenantId The tenant ID + * @return A list of Policy objects populated with data from the ResultSet + * @throws SQLException If an SQL error occurs while processing the ResultSet + */ + protected List extractPolicyListWithProfileFromDbResult(ResultSet resultSet, int tenantId) throws SQLException { + List policies = new ArrayList<>(); + while (resultSet.next()) { + Policy policy = createPolicyFromResultSet(resultSet, tenantId); + Profile profile = createProfileFromResultSet(resultSet, tenantId); + policy.setProfile(profile); + policies.add(policy); + } + return policies; + } + + /** + * Creates a Policy object from the current row in the given ResultSet + * + * @param resultSet The ResultSet containing the policy data + * @param tenantId The tenant ID + * @return A Policy object populated with data from the ResultSet + * @throws SQLException If an SQL error occurs while processing the ResultSet + */ + private Policy createPolicyFromResultSet(ResultSet resultSet, int tenantId) throws SQLException { + Policy policy = new Policy(); + policy.setId(resultSet.getInt("ID")); + policy.setProfileId(resultSet.getInt("PROFILE_ID")); + policy.setPolicyName(resultSet.getString("NAME")); + policy.setTenantId(tenantId); + policy.setPriorityId(resultSet.getInt("PRIORITY")); + policy.setCompliance(resultSet.getString("COMPLIANCE")); + policy.setOwnershipType(resultSet.getString("OWNERSHIP_TYPE")); + policy.setUpdated(PolicyManagerUtil.convertIntToBoolean(resultSet.getInt("UPDATED"))); + policy.setActive(PolicyManagerUtil.convertIntToBoolean(resultSet.getInt("ACTIVE"))); + policy.setDescription(resultSet.getString("DESCRIPTION")); + policy.setPolicyType(resultSet.getString("POLICY_TYPE")); + policy.setPolicyPayloadVersion(resultSet.getString("PAYLOAD_VERSION")); + return policy; + } + + /** + * Creates a Profile object from the current row in the given ResultSet + * + * @param resultSet The ResultSet containing the profile data + * @param tenantId The tenant ID + * @return A Profile object populated with data from the ResultSet + * @throws SQLException If an SQL error occurs while processing the ResultSet + */ + private Profile createProfileFromResultSet(ResultSet resultSet, int tenantId) throws SQLException { + Profile profile = new Profile(); + profile.setProfileId(resultSet.getInt("PROFILE_ID")); + profile.setProfileName(resultSet.getString("PROFILE_NAME")); + profile.setTenantId(tenantId); + profile.setDeviceType(resultSet.getString("DEVICE_TYPE")); + return profile; + } + } diff --git a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/GenericPolicyDAOImpl.java b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/GenericPolicyDAOImpl.java index 2e7b3aa87f..772bdbd31d 100644 --- a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/GenericPolicyDAOImpl.java +++ b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/GenericPolicyDAOImpl.java @@ -48,24 +48,27 @@ public class GenericPolicyDAOImpl extends AbstractPolicyDAOImpl { String name = request.getName(); String type = request.getType(); String status = request.getStatus(); + String deviceType = request.getDeviceType(); int statusValue = 0; boolean isPolicyNameProvided = false; boolean isPolicyTypeProvided = false; boolean isPolicyStatusProvided = false; + boolean isDeviceTypeProvided = false; try { conn = this.getConnection(); String query = "SELECT * " + - "FROM DM_POLICY " + - "WHERE TENANT_ID = ? "; + "FROM DM_POLICY P " + + "LEFT JOIN DM_PROFILE PR ON P.PROFILE_ID = PR.ID " + + "WHERE P.TENANT_ID = ? "; if (name != null && !name.isEmpty()) { - query += "AND NAME LIKE ? " ; + query += "AND P.NAME LIKE ? " ; isPolicyNameProvided = true; } if (type != null && !type.isEmpty()) { - query += "AND POLICY_TYPE = ? " ; + query += "AND P.POLICY_TYPE = ? " ; isPolicyTypeProvided = true; } @@ -73,11 +76,16 @@ public class GenericPolicyDAOImpl extends AbstractPolicyDAOImpl { if (status.equals("ACTIVE")) { statusValue = 1; } - query += "AND ACTIVE = ? " ; + query += "AND P.ACTIVE = ? " ; isPolicyStatusProvided = true; } - query += "ORDER BY ID LIMIT ?,?"; + if (deviceType != null && !deviceType.isEmpty()) { + query += "AND PR.DEVICE_TYPE = ? "; + isDeviceTypeProvided = true; + } + + query += "ORDER BY P.ID LIMIT ?,?"; try (PreparedStatement stmt = conn.prepareStatement(query)) { int paramIdx = 1; @@ -91,10 +99,13 @@ public class GenericPolicyDAOImpl extends AbstractPolicyDAOImpl { if (isPolicyStatusProvided) { stmt.setInt(paramIdx++, statusValue); } + if (isDeviceTypeProvided) { + stmt.setString(paramIdx++, deviceType); + } stmt.setInt(paramIdx++, request.getStartIndex()); stmt.setInt(paramIdx++, request.getRowCount()); try (ResultSet resultSet = stmt.executeQuery()) { - return this.extractPolicyListFromDbResult(resultSet, tenantId); + return this.extractPolicyListWithProfileFromDbResult(resultSet, tenantId); } } } catch (SQLException e) { diff --git a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/OraclePolicyDAOImpl.java b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/OraclePolicyDAOImpl.java index 19642f162e..bbc6d1a57a 100644 --- a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/OraclePolicyDAOImpl.java +++ b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/OraclePolicyDAOImpl.java @@ -47,24 +47,27 @@ public class OraclePolicyDAOImpl extends AbstractPolicyDAOImpl { String name = request.getName(); String type = request.getType(); String status = request.getStatus(); + String deviceType = request.getDeviceType(); int statusValue = 0; boolean isPolicyNameProvided = false; boolean isPolicyTypeProvided = false; boolean isPolicyStatusProvided = false; + boolean isDeviceTypeProvided = false; try { conn = this.getConnection(); String query = "SELECT * " + - "FROM DM_POLICY " + - "WHERE TENANT_ID = ? "; + "FROM DM_POLICY P " + + "LEFT JOIN DM_PROFILE PR ON P.PROFILE_ID = PR.ID " + + "WHERE P.TENANT_ID = ? "; if (name != null && !name.isEmpty()) { - query += "AND NAME LIKE ? " ; + query += "AND P.NAME LIKE ? " ; isPolicyNameProvided = true; } if (type != null && !type.isEmpty()) { - query += "AND POLICY_TYPE = ? " ; + query += "AND P.POLICY_TYPE = ? " ; isPolicyTypeProvided = true; } @@ -72,11 +75,16 @@ public class OraclePolicyDAOImpl extends AbstractPolicyDAOImpl { if (status.equals("ACTIVE")) { statusValue = 1; } - query += "AND ACTIVE = ? " ; + query += "AND P.ACTIVE = ? " ; isPolicyStatusProvided = true; } - query += "ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + if (deviceType != null && !deviceType.isEmpty()) { + query += "AND PR.DEVICE_TYPE = ? "; + isDeviceTypeProvided = true; + } + + query += "ORDER BY P.ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; try (PreparedStatement stmt = conn.prepareStatement(query)) { int paramIdx = 1; @@ -90,10 +98,13 @@ public class OraclePolicyDAOImpl extends AbstractPolicyDAOImpl { if (isPolicyStatusProvided) { stmt.setInt(paramIdx++, statusValue); } + if (isDeviceTypeProvided) { + stmt.setString(paramIdx++, deviceType); + } stmt.setInt(paramIdx++, request.getStartIndex()); stmt.setInt(paramIdx++, request.getRowCount()); try (ResultSet resultSet = stmt.executeQuery()) { - return this.extractPolicyListFromDbResult(resultSet, tenantId); + return this.extractPolicyListWithProfileFromDbResult(resultSet, tenantId); } } } catch (SQLException e) { diff --git a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/PostgreSQLPolicyDAOImpl.java b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/PostgreSQLPolicyDAOImpl.java index 26b211e6e2..01baefd69a 100644 --- a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/PostgreSQLPolicyDAOImpl.java +++ b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/PostgreSQLPolicyDAOImpl.java @@ -47,24 +47,27 @@ public class PostgreSQLPolicyDAOImpl extends AbstractPolicyDAOImpl { String name = request.getName(); String type = request.getType(); String status = request.getStatus(); + String deviceType = request.getDeviceType(); int statusValue = 0; boolean isPolicyNameProvided = false; boolean isPolicyTypeProvided = false; boolean isPolicyStatusProvided = false; + boolean isDeviceTypeProvided = false; try { conn = this.getConnection(); String query = "SELECT * " + - "FROM DM_POLICY " + - "WHERE TENANT_ID = ? "; + "FROM DM_POLICY P " + + "LEFT JOIN DM_PROFILE PR ON P.PROFILE_ID = PR.ID " + + "WHERE P.TENANT_ID = ? "; if (name != null && !name.isEmpty()) { - query += "AND NAME LIKE ? " ; + query += "AND P.NAME LIKE ? " ; isPolicyNameProvided = true; } if (type != null && !type.isEmpty()) { - query += "AND POLICY_TYPE = ? " ; + query += "AND P.POLICY_TYPE = ? " ; isPolicyTypeProvided = true; } @@ -72,11 +75,16 @@ public class PostgreSQLPolicyDAOImpl extends AbstractPolicyDAOImpl { if (status.equals("ACTIVE")) { statusValue = 1; } - query += "AND ACTIVE = ? " ; + query += "AND P.ACTIVE = ? " ; isPolicyStatusProvided = true; } - query += "ORDER BY ID LIMIT ? OFFSET ?"; + if (deviceType != null && !deviceType.isEmpty()) { + query += "AND PR.DEVICE_TYPE = ?"; + isDeviceTypeProvided = true; + } + + query += "ORDER BY P.ID LIMIT ? OFFSET ?"; try (PreparedStatement stmt = conn.prepareStatement(query)) { int paramIdx = 1; @@ -90,10 +98,13 @@ public class PostgreSQLPolicyDAOImpl extends AbstractPolicyDAOImpl { if (isPolicyStatusProvided) { stmt.setInt(paramIdx++, statusValue); } + if (isDeviceTypeProvided) { + stmt.setString(paramIdx++, deviceType); + } stmt.setInt(paramIdx++, request.getStartIndex()); stmt.setInt(paramIdx++, request.getRowCount()); try (ResultSet resultSet = stmt.executeQuery()) { - return this.extractPolicyListFromDbResult(resultSet, tenantId); + return this.extractPolicyListWithProfileFromDbResult(resultSet, tenantId); } } } catch (SQLException e) { diff --git a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/SQLServerPolicyDAOImpl.java b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/SQLServerPolicyDAOImpl.java index 6e4df5cb15..5f59491ca5 100644 --- a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/SQLServerPolicyDAOImpl.java +++ b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/SQLServerPolicyDAOImpl.java @@ -47,24 +47,27 @@ public class SQLServerPolicyDAOImpl extends AbstractPolicyDAOImpl { String name = request.getName(); String type = request.getType(); String status = request.getStatus(); + String deviceType = request.getDeviceType(); int statusValue = 0; boolean isPolicyNameProvided = false; boolean isPolicyTypeProvided = false; boolean isPolicyStatusProvided = false; + boolean isDeviceTypeProvided = false; try { conn = this.getConnection(); String query = "SELECT * " + - "FROM DM_POLICY " + - "WHERE TENANT_ID = ? "; + "FROM DM_POLICY P " + + "LEFT JOIN DM_PROFILE PR ON P.PROFILE_ID = PR.ID " + + "WHERE P.TENANT_ID = ? "; if (name != null && !name.isEmpty()) { - query += "AND NAME LIKE ? " ; + query += "AND P.NAME LIKE ? " ; isPolicyNameProvided = true; } if (type != null && !type.isEmpty()) { - query += "AND POLICY_TYPE = ? " ; + query += "AND P.POLICY_TYPE = ? " ; isPolicyTypeProvided = true; } @@ -72,11 +75,16 @@ public class SQLServerPolicyDAOImpl extends AbstractPolicyDAOImpl { if (status.equals("ACTIVE")) { statusValue = 1; } - query += "AND ACTIVE = ? " ; + query += "AND P.ACTIVE = ? " ; isPolicyStatusProvided = true; } - query += "ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + if (deviceType != null && !deviceType.isEmpty()) { + query += "AND PR.DEVICE_TYPE = ?"; + isDeviceTypeProvided = true; + } + + query += "ORDER BY P.ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; try (PreparedStatement stmt = conn.prepareStatement(query)) { int paramIdx = 1; @@ -90,10 +98,13 @@ public class SQLServerPolicyDAOImpl extends AbstractPolicyDAOImpl { if (isPolicyStatusProvided) { stmt.setInt(paramIdx++, statusValue); } + if (isDeviceTypeProvided) { + stmt.setString(paramIdx++, deviceType); + } stmt.setInt(paramIdx++, request.getStartIndex()); stmt.setInt(paramIdx++, request.getRowCount()); try (ResultSet resultSet = stmt.executeQuery()) { - return this.extractPolicyListFromDbResult(resultSet, tenantId); + return this.extractPolicyListWithProfileFromDbResult(resultSet, tenantId); } } } catch (SQLException e) { diff --git a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index 9d362bcc9e..cf6e0da5a8 100644 --- a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -1532,6 +1532,8 @@ public class PolicyManagerImpl implements PolicyManager { for (Policy policy : policyList) { policy.setRoles(policyDAO.getPolicyAppliedRoles(policy.getId())); policy.setUsers(policyDAO.getPolicyAppliedUsers(policy.getId())); + policy.setProfile(profileDAO.getProfile(policy.getId())); + List deviceGroupWrappers = policyDAO.getDeviceGroupsOfPolicy(policy.getId()); if (!deviceGroupWrappers.isEmpty()) { deviceGroupWrappers = this.getDeviceGroupNames(deviceGroupWrappers); @@ -1551,6 +1553,10 @@ public class PolicyManagerImpl implements PolicyManager { String msg = "Error occurred while getting device groups."; log.error(msg, e); throw new PolicyManagementException(msg, e); + } catch (ProfileManagerDAOException e) { + String msg = "Error occurred while getting profiles."; + log.error(msg, e); + throw new PolicyManagementException(msg, e); } finally { PolicyManagementDAOFactory.closeConnection(); }