Add offset and limit when querying policies from DAO layer

eval-user-logs
commit b53bfdf780

@ -40,6 +40,7 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService;
@ -504,18 +505,13 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
RequestValidationUtil.validatePaginationParameters(offset, limit);
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
List<Policy> policies;
List<Policy> filteredPolicies;
PolicyList targetPolicies = new PolicyList();
PaginationRequest request = new PaginationRequest(offset, limit);
try {
PolicyAdministratorPoint policyAdministratorPoint = policyManagementService.getPAP();
policies = policyAdministratorPoint.getPolicyList();
targetPolicies.setCount(policies.size());
if (offset == 0 && limit == 0) {
targetPolicies.setList(policies);
} else {
filteredPolicies = FilteringUtil.getFilteredList(policies, offset, limit);
targetPolicies.setList(filteredPolicies);
}
policies = policyAdministratorPoint.getPolicyList(request);
targetPolicies.setCount(policyAdministratorPoint.getPolicyCount());
targetPolicies.setList(policies);
} catch (PolicyManagementException e) {
String msg = "Error occurred while retrieving all available policies";
log.error(msg, e);

@ -19,6 +19,7 @@
package org.wso2.carbon.policy.mgt.common;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DynamicTaskContext;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.policy.mgt.Policy;
import org.wso2.carbon.device.mgt.common.policy.mgt.Profile;
@ -164,5 +165,12 @@ public interface PolicyAdministratorPoint {
*/
List<Policy> getPolicies(String policyType) throws PolicyManagementException;
List<Policy> getPolicyList() throws PolicyManagementException;
/**
* Returns a list of policies filtered by offset and limit
* @param request {@link PaginationRequest} contains offset and limit
* @return {@link List<Policy>} - list of policies for current tenant
* @throws PolicyManagementException when there is an error while retrieving the policies from database or
* while retrieving device groups
*/
List<Policy> getPolicyList(PaginationRequest request) throws PolicyManagementException;
}

@ -36,6 +36,7 @@
package org.wso2.carbon.policy.mgt.core.dao;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.policy.mgt.CorrectiveAction;
import org.wso2.carbon.policy.mgt.common.Criterion;
import org.wso2.carbon.device.mgt.common.policy.mgt.DeviceGroupWrapper;
@ -214,4 +215,13 @@ public interface PolicyDAO {
HashMap<Integer, Integer> getAppliedPolicyIdsDeviceIds() throws PolicyManagerDAOException;
List<Policy> getAllPolicies(String policyType) throws PolicyManagerDAOException;
/**
* This method is used to retrieve policies from the database based on the offset and limit
* sent through the PaginationRequest
* @param request {@link PaginationRequest} contains offset and limit
* @return {@link List<Policy>} - list of policies for current tenant
* @throws PolicyManagerDAOException when there is an error while retrieving the policies from database
*/
List<Policy> getAllPolicies(PaginationRequest request) throws PolicyManagerDAOException;
}

@ -26,11 +26,14 @@ import org.wso2.carbon.device.mgt.common.exceptions.UnsupportedDatabaseEngineExc
import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig;
import org.wso2.carbon.policy.mgt.core.config.datasource.JNDILookupDefinition;
import org.wso2.carbon.policy.mgt.core.dao.impl.MonitoringDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.impl.PolicyDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.impl.ProfileDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.impl.feature.GenericFeatureDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.impl.feature.OracleServerFeatureDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.impl.feature.SQLServerFeatureDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.impl.policy.GenericPolicyDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.impl.policy.OraclePolicyDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.impl.policy.PostgreSQLPolicyDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.impl.policy.SQLServerPolicyDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil;
import javax.sql.DataSource;
@ -65,7 +68,22 @@ public class PolicyManagementDAOFactory {
}
public static PolicyDAO getPolicyDAO() {
return new PolicyDAOImpl();
if (databaseEngine != null) {
switch (databaseEngine) {
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL:
return new SQLServerPolicyDAOImpl();
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE:
return new OraclePolicyDAOImpl();
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_POSTGRESQL:
return new PostgreSQLPolicyDAOImpl();
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2:
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL:
return new GenericPolicyDAOImpl();
default:
throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine);
}
}
throw new IllegalStateException("Database engine has not initialized properly.");
}
public static ProfileDAO getProfileDAO() {

@ -33,7 +33,7 @@
* under the License.
*/
package org.wso2.carbon.policy.mgt.core.dao.impl;
package org.wso2.carbon.policy.mgt.core.dao.impl.policy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -66,9 +66,12 @@ import java.util.HashMap;
import java.util.List;
import java.util.Properties;
public class PolicyDAOImpl implements PolicyDAO {
/**
* Abstract implementation of PolicyDAO which holds generic SQL queries.
*/
public abstract class AbstractPolicyDAOImpl implements PolicyDAO {
private static final Log log = LogFactory.getLog(PolicyDAOImpl.class);
private static final Log log = LogFactory.getLog(AbstractPolicyDAOImpl.class);
@Override
public Policy addPolicy(Policy policy) throws PolicyManagerDAOException {
@ -1838,7 +1841,7 @@ public class PolicyDAOImpl implements PolicyDAO {
}
}
private List<Policy> extractPolicyListFromDbResult(ResultSet resultSet, int tenantId) throws SQLException {
protected List<Policy> extractPolicyListFromDbResult(ResultSet resultSet, int tenantId) throws SQLException {
List<Policy> policies = new ArrayList<>();
while (resultSet.next()) {
Policy policy = new Policy();

@ -0,0 +1,70 @@
/*
* Copyright (C) 2018 - 2023 Entgra (Pvt) Ltd, Inc - All Rights Reserved.
*
* Unauthorised copying/redistribution of this file, via any medium is strictly prohibited.
*
* Licensed under the Entgra Commercial License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://entgra.io/licenses/entgra-commercial/1.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.policy.mgt.core.dao.impl.policy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.policy.mgt.Policy;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class GenericPolicyDAOImpl extends AbstractPolicyDAOImpl {
private static final Log log = LogFactory.getLog(GenericPolicyDAOImpl.class);
private Connection getConnection() {
return PolicyManagementDAOFactory.getConnection();
}
@Override
public List<Policy> getAllPolicies(PaginationRequest request) throws PolicyManagerDAOException {
Connection conn;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query = "SELECT * " +
"FROM DM_POLICY " +
"WHERE TENANT_ID = ? " +
"ORDER BY ID LIMIT ?,?";
try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setInt(1, tenantId);
stmt.setInt(2, request.getStartIndex());
stmt.setInt(3, request.getRowCount());
try (ResultSet resultSet = stmt.executeQuery()) {
return this.extractPolicyListFromDbResult(resultSet, tenantId);
}
}
} catch (SQLException e) {
String msg = "Error occurred while reading the policies from the database.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
}
}
}

@ -0,0 +1,69 @@
/*
* Copyright (C) 2018 - 2023 Entgra (Pvt) Ltd, Inc - All Rights Reserved.
*
* Unauthorised copying/redistribution of this file, via any medium is strictly prohibited.
*
* Licensed under the Entgra Commercial License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://entgra.io/licenses/entgra-commercial/1.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.policy.mgt.core.dao.impl.policy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.policy.mgt.Policy;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class OraclePolicyDAOImpl extends AbstractPolicyDAOImpl {
private static final Log log = LogFactory.getLog(OraclePolicyDAOImpl.class);
private Connection getConnection() {
return PolicyManagementDAOFactory.getConnection();
}
@Override
public List<Policy> getAllPolicies(PaginationRequest request) throws PolicyManagerDAOException {
Connection conn;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query = "SELECT * " +
"FROM DM_POLICY " +
"WHERE TENANT_ID = ? " +
"ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setInt(1, tenantId);
stmt.setInt(2, request.getStartIndex());
stmt.setInt(3, request.getRowCount());
try (ResultSet resultSet = stmt.executeQuery()) {
return this.extractPolicyListFromDbResult(resultSet, tenantId);
}
}
} catch (SQLException e) {
String msg = "Error occurred while reading the policies from the database.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
}
}
}

@ -0,0 +1,69 @@
/*
* Copyright (C) 2018 - 2023 Entgra (Pvt) Ltd, Inc - All Rights Reserved.
*
* Unauthorised copying/redistribution of this file, via any medium is strictly prohibited.
*
* Licensed under the Entgra Commercial License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://entgra.io/licenses/entgra-commercial/1.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.policy.mgt.core.dao.impl.policy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.policy.mgt.Policy;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class PostgreSQLPolicyDAOImpl extends AbstractPolicyDAOImpl {
private static final Log log = LogFactory.getLog(PostgreSQLPolicyDAOImpl.class);
private Connection getConnection() {
return PolicyManagementDAOFactory.getConnection();
}
@Override
public List<Policy> getAllPolicies(PaginationRequest request) throws PolicyManagerDAOException {
Connection conn;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query = "SELECT * " +
"FROM DM_POLICY " +
"WHERE TENANT_ID = ? " +
"ORDER BY ID LIMIT ? OFFSET ?";
try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setInt(1, tenantId);
stmt.setInt(2, request.getRowCount());
stmt.setInt(3, request.getStartIndex());
try (ResultSet resultSet = stmt.executeQuery()) {
return this.extractPolicyListFromDbResult(resultSet, tenantId);
}
}
} catch (SQLException e) {
String msg = "Error occurred while reading the policies from the database.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
}
}
}

@ -0,0 +1,68 @@
/*
* Copyright (C) 2018 - 2023 Entgra (Pvt) Ltd, Inc - All Rights Reserved.
*
* Unauthorised copying/redistribution of this file, via any medium is strictly prohibited.
*
* Licensed under the Entgra Commercial License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://entgra.io/licenses/entgra-commercial/1.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.policy.mgt.core.dao.impl.policy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.policy.mgt.Policy;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class SQLServerPolicyDAOImpl extends AbstractPolicyDAOImpl {
private static final Log log = LogFactory.getLog(SQLServerPolicyDAOImpl.class);
private Connection getConnection() {
return PolicyManagementDAOFactory.getConnection();
}
@Override
public List<Policy> getAllPolicies(PaginationRequest request) throws PolicyManagerDAOException {
Connection conn;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query = "SELECT * " +
"FROM DM_POLICY " +
"WHERE TENANT_ID = ? " +
"ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setInt(1, tenantId);
stmt.setInt(2, request.getStartIndex());
stmt.setInt(3, request.getRowCount());
try (ResultSet resultSet = stmt.executeQuery()) {
return this.extractPolicyListFromDbResult(resultSet, tenantId);
}
}
} catch (SQLException e) {
String msg = "Error occurred while reading the policies from the database.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
}
}
}

@ -23,6 +23,7 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DynamicTaskContext;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.policy.mgt.Policy;
import org.wso2.carbon.device.mgt.common.policy.mgt.Profile;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
@ -334,7 +335,7 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
}
@Override
public List<Policy> getPolicyList() throws PolicyManagementException {
return policyManager.getPolicyList();
public List<Policy> getPolicyList(PaginationRequest request) throws PolicyManagementException {
return policyManager.getPolicyList(request);
}
}

@ -20,6 +20,7 @@ package org.wso2.carbon.policy.mgt.core.mgt;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DynamicTaskContext;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.policy.mgt.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.mgt.bean.UpdatedPolicyDeviceListBean;
@ -90,5 +91,12 @@ public interface PolicyManager {
List<Policy> getPolicies(String type) throws PolicyManagementException;
List<Policy> getPolicyList() throws PolicyManagementException;
/**
* Returns list of policies with users, roles and groups attached to that policy
* @param request {@link PaginationRequest} contains offset and limit
* @return {@link List<Policy>} - list of policies for current tenant
* @throws PolicyManagementException when there is an error while retrieving the policies from database or
* while retrieving device groups
*/
List<Policy> getPolicyList(PaginationRequest request) throws PolicyManagementException;
}

@ -41,6 +41,7 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DynamicTaskContext;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
@ -1504,12 +1505,11 @@ public class PolicyManagerImpl implements PolicyManager {
}
@Override
public List<Policy> getPolicyList() throws PolicyManagementException {
public List<Policy> getPolicyList(PaginationRequest request) throws PolicyManagementException {
List<Policy> policyList;
try {
PolicyManagementDAOFactory.openConnection();
policyList = policyDAO.getAllPolicies();
policyList = policyDAO.getAllPolicies(request);
for (Policy policy : policyList) {
policy.setRoles(policyDAO.getPolicyAppliedRoles(policy.getId()));
policy.setUsers(policyDAO.getPolicyAppliedUsers(policy.getId()));
@ -1521,11 +1521,17 @@ public class PolicyManagerImpl implements PolicyManager {
}
Collections.sort(policyList);
} catch (PolicyManagerDAOException e) {
throw new PolicyManagementException("Error occurred while getting all the policies.", e);
String msg = "Error occurred while getting all the policies.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (SQLException e) {
throw new PolicyManagementException("Error occurred while opening a connection to the data source", e);
String msg = "Error occurred while opening a connection to the data source.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (GroupManagementException e) {
throw new PolicyManagementException("Error occurred while getting device groups.", e);
String msg = "Error occurred while getting device groups.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} finally {
PolicyManagementDAOFactory.closeConnection();
}

@ -303,6 +303,7 @@ CREATE TABLE DM_POLICY_CORRECTIVE_ACTION (
CORRECTIVE_POLICY_ID INTEGER DEFAULT NULL,
POLICY_ID INTEGER NOT NULL,
FEATURE_ID INTEGER DEFAULT NULL,
IS_REACTIVE BIT NOT NULL DEFAULT 0,
PRIMARY KEY (ID),
CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION
FOREIGN KEY (POLICY_ID)

Loading…
Cancel
Save