Add capability to search policies by device type

Resolve comments

Resolve comments

Remove unnecessary changes
#11464
Ashvini Wegodapola 9 months ago
parent 2ac2b0fc37
commit 8a82d1666c

@ -934,6 +934,12 @@ public interface PolicyManagementService {
required = false) required = false)
@QueryParam("status") @QueryParam("status")
String status, String status,
@ApiParam(
name = "deviceType",
value = "The device type of the policy that needs filtering.",
required = false)
@QueryParam("deviceType")
String deviceType,
@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" +

@ -488,6 +488,7 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
@QueryParam("name") String name, @QueryParam("name") String name,
@QueryParam("type") String type, @QueryParam("type") String type,
@QueryParam("status") String status, @QueryParam("status") String status,
@QueryParam("deviceType") String deviceType,
@HeaderParam("If-Modified-Since") String ifModifiedSince, @HeaderParam("If-Modified-Since") String ifModifiedSince,
@QueryParam("offset") int offset, @QueryParam("offset") int offset,
@QueryParam("limit") int limit) { @QueryParam("limit") int limit) {
@ -505,6 +506,9 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
if (status != null){ if (status != null){
request.setStatus(status); request.setStatus(status);
} }
if (deviceType != null) {
request.setDeviceType(deviceType);
}
try { try {
PolicyAdministratorPoint policyAdministratorPoint = policyManagementService.getPAP(); PolicyAdministratorPoint policyAdministratorPoint = policyManagementService.getPAP();
policies = policyAdministratorPoint.getPolicyList(request); policies = policyAdministratorPoint.getPolicyList(request);

@ -24,6 +24,7 @@ public class PolicyPaginationRequest {
private String name; private String name;
private String type; private String type;
private String status; private String status;
private String deviceType;
public PolicyPaginationRequest(int start, int rowCount) { public PolicyPaginationRequest(int start, int rowCount) {
this.startIndex = start; this.startIndex = start;
@ -70,6 +71,14 @@ public class PolicyPaginationRequest {
this.status = status; this.status = status;
} }
public String getDeviceType() {
return deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
@Override @Override
public String toString() { public String toString() {
return "Group Name '" + this.name + "' num of rows: " + this.rowCount + " start index: " + this.startIndex; return "Group Name '" + this.name + "' num of rows: " + this.rowCount + " start index: " + this.startIndex;

@ -26,6 +26,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.DeviceGroupWrapper;
import io.entgra.device.mgt.core.device.mgt.common.policy.mgt.Policy; 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.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.common.Criterion;
import io.entgra.device.mgt.core.policy.mgt.core.dao.PolicyDAO; import io.entgra.device.mgt.core.policy.mgt.core.dao.PolicyDAO;
import io.entgra.device.mgt.core.policy.mgt.core.dao.PolicyManagementDAOFactory; import io.entgra.device.mgt.core.policy.mgt.core.dao.PolicyManagementDAOFactory;
@ -1844,4 +1845,66 @@ public abstract class AbstractPolicyDAOImpl implements PolicyDAO {
} }
return policies; 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<Policy> extractPolicyListWithProfileFromDbResult(ResultSet resultSet, int tenantId) throws SQLException {
List<Policy> 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;
}
} }

@ -48,24 +48,27 @@ public class GenericPolicyDAOImpl extends AbstractPolicyDAOImpl {
String name = request.getName(); String name = request.getName();
String type = request.getType(); String type = request.getType();
String status = request.getStatus(); String status = request.getStatus();
String deviceType = request.getDeviceType();
int statusValue = 0; int statusValue = 0;
boolean isPolicyNameProvided = false; boolean isPolicyNameProvided = false;
boolean isPolicyTypeProvided = false; boolean isPolicyTypeProvided = false;
boolean isPolicyStatusProvided = false; boolean isPolicyStatusProvided = false;
boolean isDeviceTypeProvided = false;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT * " + String query = "SELECT * " +
"FROM DM_POLICY " + "FROM DM_POLICY P " +
"WHERE TENANT_ID = ? "; "LEFT JOIN DM_PROFILE PR ON P.PROFILE_ID = PR.ID " +
"WHERE P.TENANT_ID = ? ";
if (name != null && !name.isEmpty()) { if (name != null && !name.isEmpty()) {
query += "AND NAME LIKE ? " ; query += "AND P.NAME LIKE ? " ;
isPolicyNameProvided = true; isPolicyNameProvided = true;
} }
if (type != null && !type.isEmpty()) { if (type != null && !type.isEmpty()) {
query += "AND POLICY_TYPE = ? " ; query += "AND P.POLICY_TYPE = ? " ;
isPolicyTypeProvided = true; isPolicyTypeProvided = true;
} }
@ -73,11 +76,16 @@ public class GenericPolicyDAOImpl extends AbstractPolicyDAOImpl {
if (status.equals("ACTIVE")) { if (status.equals("ACTIVE")) {
statusValue = 1; statusValue = 1;
} }
query += "AND ACTIVE = ? " ; query += "AND P.ACTIVE = ? " ;
isPolicyStatusProvided = true; 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)) { try (PreparedStatement stmt = conn.prepareStatement(query)) {
int paramIdx = 1; int paramIdx = 1;
@ -91,10 +99,13 @@ public class GenericPolicyDAOImpl extends AbstractPolicyDAOImpl {
if (isPolicyStatusProvided) { if (isPolicyStatusProvided) {
stmt.setInt(paramIdx++, statusValue); stmt.setInt(paramIdx++, statusValue);
} }
if (isDeviceTypeProvided) {
stmt.setString(paramIdx++, deviceType);
}
stmt.setInt(paramIdx++, request.getStartIndex()); stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx++, request.getRowCount()); stmt.setInt(paramIdx++, request.getRowCount());
try (ResultSet resultSet = stmt.executeQuery()) { try (ResultSet resultSet = stmt.executeQuery()) {
return this.extractPolicyListFromDbResult(resultSet, tenantId); return this.extractPolicyListWithProfileFromDbResult(resultSet, tenantId);
} }
} }
} catch (SQLException e) { } catch (SQLException e) {

@ -47,24 +47,27 @@ public class OraclePolicyDAOImpl extends AbstractPolicyDAOImpl {
String name = request.getName(); String name = request.getName();
String type = request.getType(); String type = request.getType();
String status = request.getStatus(); String status = request.getStatus();
String deviceType = request.getDeviceType();
int statusValue = 0; int statusValue = 0;
boolean isPolicyNameProvided = false; boolean isPolicyNameProvided = false;
boolean isPolicyTypeProvided = false; boolean isPolicyTypeProvided = false;
boolean isPolicyStatusProvided = false; boolean isPolicyStatusProvided = false;
boolean isDeviceTypeProvided = false;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT * " + String query = "SELECT * " +
"FROM DM_POLICY " + "FROM DM_POLICY P " +
"WHERE TENANT_ID = ? "; "LEFT JOIN DM_PROFILE PR ON P.PROFILE_ID = PR.ID " +
"WHERE P.TENANT_ID = ? ";
if (name != null && !name.isEmpty()) { if (name != null && !name.isEmpty()) {
query += "AND NAME LIKE ? " ; query += "AND P.NAME LIKE ? " ;
isPolicyNameProvided = true; isPolicyNameProvided = true;
} }
if (type != null && !type.isEmpty()) { if (type != null && !type.isEmpty()) {
query += "AND POLICY_TYPE = ? " ; query += "AND P.POLICY_TYPE = ? " ;
isPolicyTypeProvided = true; isPolicyTypeProvided = true;
} }
@ -72,11 +75,16 @@ public class OraclePolicyDAOImpl extends AbstractPolicyDAOImpl {
if (status.equals("ACTIVE")) { if (status.equals("ACTIVE")) {
statusValue = 1; statusValue = 1;
} }
query += "AND ACTIVE = ? " ; query += "AND P.ACTIVE = ? " ;
isPolicyStatusProvided = true; 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)) { try (PreparedStatement stmt = conn.prepareStatement(query)) {
int paramIdx = 1; int paramIdx = 1;
@ -90,10 +98,13 @@ public class OraclePolicyDAOImpl extends AbstractPolicyDAOImpl {
if (isPolicyStatusProvided) { if (isPolicyStatusProvided) {
stmt.setInt(paramIdx++, statusValue); stmt.setInt(paramIdx++, statusValue);
} }
if (isDeviceTypeProvided) {
stmt.setString(paramIdx++, deviceType);
}
stmt.setInt(paramIdx++, request.getStartIndex()); stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx++, request.getRowCount()); stmt.setInt(paramIdx++, request.getRowCount());
try (ResultSet resultSet = stmt.executeQuery()) { try (ResultSet resultSet = stmt.executeQuery()) {
return this.extractPolicyListFromDbResult(resultSet, tenantId); return this.extractPolicyListWithProfileFromDbResult(resultSet, tenantId);
} }
} }
} catch (SQLException e) { } catch (SQLException e) {

@ -47,24 +47,27 @@ public class PostgreSQLPolicyDAOImpl extends AbstractPolicyDAOImpl {
String name = request.getName(); String name = request.getName();
String type = request.getType(); String type = request.getType();
String status = request.getStatus(); String status = request.getStatus();
String deviceType = request.getDeviceType();
int statusValue = 0; int statusValue = 0;
boolean isPolicyNameProvided = false; boolean isPolicyNameProvided = false;
boolean isPolicyTypeProvided = false; boolean isPolicyTypeProvided = false;
boolean isPolicyStatusProvided = false; boolean isPolicyStatusProvided = false;
boolean isDeviceTypeProvided = false;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT * " + String query = "SELECT * " +
"FROM DM_POLICY " + "FROM DM_POLICY P " +
"WHERE TENANT_ID = ? "; "LEFT JOIN DM_PROFILE PR ON P.PROFILE_ID = PR.ID " +
"WHERE P.TENANT_ID = ? ";
if (name != null && !name.isEmpty()) { if (name != null && !name.isEmpty()) {
query += "AND NAME LIKE ? " ; query += "AND P.NAME LIKE ? " ;
isPolicyNameProvided = true; isPolicyNameProvided = true;
} }
if (type != null && !type.isEmpty()) { if (type != null && !type.isEmpty()) {
query += "AND POLICY_TYPE = ? " ; query += "AND P.POLICY_TYPE = ? " ;
isPolicyTypeProvided = true; isPolicyTypeProvided = true;
} }
@ -72,11 +75,16 @@ public class PostgreSQLPolicyDAOImpl extends AbstractPolicyDAOImpl {
if (status.equals("ACTIVE")) { if (status.equals("ACTIVE")) {
statusValue = 1; statusValue = 1;
} }
query += "AND ACTIVE = ? " ; query += "AND P.ACTIVE = ? " ;
isPolicyStatusProvided = true; 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)) { try (PreparedStatement stmt = conn.prepareStatement(query)) {
int paramIdx = 1; int paramIdx = 1;
@ -90,10 +98,13 @@ public class PostgreSQLPolicyDAOImpl extends AbstractPolicyDAOImpl {
if (isPolicyStatusProvided) { if (isPolicyStatusProvided) {
stmt.setInt(paramIdx++, statusValue); stmt.setInt(paramIdx++, statusValue);
} }
if (isDeviceTypeProvided) {
stmt.setString(paramIdx++, deviceType);
}
stmt.setInt(paramIdx++, request.getStartIndex()); stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx++, request.getRowCount()); stmt.setInt(paramIdx++, request.getRowCount());
try (ResultSet resultSet = stmt.executeQuery()) { try (ResultSet resultSet = stmt.executeQuery()) {
return this.extractPolicyListFromDbResult(resultSet, tenantId); return this.extractPolicyListWithProfileFromDbResult(resultSet, tenantId);
} }
} }
} catch (SQLException e) { } catch (SQLException e) {

@ -47,24 +47,27 @@ public class SQLServerPolicyDAOImpl extends AbstractPolicyDAOImpl {
String name = request.getName(); String name = request.getName();
String type = request.getType(); String type = request.getType();
String status = request.getStatus(); String status = request.getStatus();
String deviceType = request.getDeviceType();
int statusValue = 0; int statusValue = 0;
boolean isPolicyNameProvided = false; boolean isPolicyNameProvided = false;
boolean isPolicyTypeProvided = false; boolean isPolicyTypeProvided = false;
boolean isPolicyStatusProvided = false; boolean isPolicyStatusProvided = false;
boolean isDeviceTypeProvided = false;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT * " + String query = "SELECT * " +
"FROM DM_POLICY " + "FROM DM_POLICY P " +
"WHERE TENANT_ID = ? "; "LEFT JOIN DM_PROFILE PR ON P.PROFILE_ID = PR.ID " +
"WHERE P.TENANT_ID = ? ";
if (name != null && !name.isEmpty()) { if (name != null && !name.isEmpty()) {
query += "AND NAME LIKE ? " ; query += "AND P.NAME LIKE ? " ;
isPolicyNameProvided = true; isPolicyNameProvided = true;
} }
if (type != null && !type.isEmpty()) { if (type != null && !type.isEmpty()) {
query += "AND POLICY_TYPE = ? " ; query += "AND P.POLICY_TYPE = ? " ;
isPolicyTypeProvided = true; isPolicyTypeProvided = true;
} }
@ -72,11 +75,16 @@ public class SQLServerPolicyDAOImpl extends AbstractPolicyDAOImpl {
if (status.equals("ACTIVE")) { if (status.equals("ACTIVE")) {
statusValue = 1; statusValue = 1;
} }
query += "AND ACTIVE = ? " ; query += "AND P.ACTIVE = ? " ;
isPolicyStatusProvided = true; 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)) { try (PreparedStatement stmt = conn.prepareStatement(query)) {
int paramIdx = 1; int paramIdx = 1;
@ -90,10 +98,13 @@ public class SQLServerPolicyDAOImpl extends AbstractPolicyDAOImpl {
if (isPolicyStatusProvided) { if (isPolicyStatusProvided) {
stmt.setInt(paramIdx++, statusValue); stmt.setInt(paramIdx++, statusValue);
} }
if (isDeviceTypeProvided) {
stmt.setString(paramIdx++, deviceType);
}
stmt.setInt(paramIdx++, request.getStartIndex()); stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx++, request.getRowCount()); stmt.setInt(paramIdx++, request.getRowCount());
try (ResultSet resultSet = stmt.executeQuery()) { try (ResultSet resultSet = stmt.executeQuery()) {
return this.extractPolicyListFromDbResult(resultSet, tenantId); return this.extractPolicyListWithProfileFromDbResult(resultSet, tenantId);
} }
} }
} catch (SQLException e) { } catch (SQLException e) {

@ -1532,6 +1532,8 @@ public class PolicyManagerImpl implements PolicyManager {
for (Policy policy : policyList) { for (Policy policy : policyList) {
policy.setRoles(policyDAO.getPolicyAppliedRoles(policy.getId())); policy.setRoles(policyDAO.getPolicyAppliedRoles(policy.getId()));
policy.setUsers(policyDAO.getPolicyAppliedUsers(policy.getId())); policy.setUsers(policyDAO.getPolicyAppliedUsers(policy.getId()));
policy.setProfile(profileDAO.getProfile(policy.getId()));
List<DeviceGroupWrapper> deviceGroupWrappers = policyDAO.getDeviceGroupsOfPolicy(policy.getId()); List<DeviceGroupWrapper> deviceGroupWrappers = policyDAO.getDeviceGroupsOfPolicy(policy.getId());
if (!deviceGroupWrappers.isEmpty()) { if (!deviceGroupWrappers.isEmpty()) {
deviceGroupWrappers = this.getDeviceGroupNames(deviceGroupWrappers); deviceGroupWrappers = this.getDeviceGroupNames(deviceGroupWrappers);
@ -1551,6 +1553,10 @@ public class PolicyManagerImpl implements PolicyManager {
String msg = "Error occurred while getting device groups."; String msg = "Error occurred while getting device groups.";
log.error(msg, e); log.error(msg, e);
throw new PolicyManagementException(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 { } finally {
PolicyManagementDAOFactory.closeConnection(); PolicyManagementDAOFactory.closeConnection();
} }

Loading…
Cancel
Save