Merge pull request 'Add capability to search policies by device type' (#339) from ashvini/device-mgt-core:Improvement#10585 into master

Reviewed-on: #339
pull/451/head
Pahansith Gunathilake 3 months ago
commit 290952ae53

@ -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" +

@ -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);

@ -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;

@ -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<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 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) {

@ -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) {

@ -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) {

@ -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) {

@ -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<DeviceGroupWrapper> 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();
}

Loading…
Cancel
Save