From 708d6be59d450ffc2349aecdcb25d91b6717c31c Mon Sep 17 00:00:00 2001 From: navodzoysa Date: Tue, 28 Feb 2023 21:22:25 +0530 Subject: [PATCH] Add offset and limit when querying policies from DAO layer --- .../impl/PolicyManagementServiceImpl.java | 14 ++-- .../mgt/common/PolicyAdministratorPoint.java | 10 ++- .../carbon/policy/mgt/core/dao/PolicyDAO.java | 10 +++ .../core/dao/PolicyManagementDAOFactory.java | 22 +++++- .../AbstractPolicyDAOImpl.java} | 11 +-- .../dao/impl/policy/GenericPolicyDAOImpl.java | 70 +++++++++++++++++++ .../dao/impl/policy/OraclePolicyDAOImpl.java | 69 ++++++++++++++++++ .../impl/policy/PostgreSQLPolicyDAOImpl.java | 69 ++++++++++++++++++ .../impl/policy/SQLServerPolicyDAOImpl.java | 68 ++++++++++++++++++ .../impl/PolicyAdministratorPointImpl.java | 5 +- .../policy/mgt/core/mgt/PolicyManager.java | 10 ++- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 18 +++-- .../main/resources/dbscripts/cdm/mssql.sql | 1 + 13 files changed, 352 insertions(+), 25 deletions(-) rename components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/{PolicyDAOImpl.java => policy/AbstractPolicyDAOImpl.java} (99%) create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/policy/GenericPolicyDAOImpl.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/policy/OraclePolicyDAOImpl.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/policy/PostgreSQLPolicyDAOImpl.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/policy/SQLServerPolicyDAOImpl.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java index 6a2dbe68a3..93c8bb4192 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java @@ -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; @@ -508,18 +509,13 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { RequestValidationUtil.validatePaginationParameters(offset, limit); PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); List policies; - List 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); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java index 591761db5f..5c12cead8d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java @@ -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 getPolicies(String policyType) throws PolicyManagementException; - List getPolicyList() throws PolicyManagementException; + /** + * Returns a list of policies filtered by offset and limit + * @param request {@link PaginationRequest} contains offset and limit + * @return {@link List} - 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 getPolicyList(PaginationRequest request) throws PolicyManagementException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java index 20b53f81a9..c5ae7c0a52 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java @@ -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 getAppliedPolicyIdsDeviceIds() throws PolicyManagerDAOException; List 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} - list of policies for current tenant + * @throws PolicyManagerDAOException when there is an error while retrieving the policies from database + */ + List getAllPolicies(PaginationRequest request) throws PolicyManagerDAOException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java index 78d5434d01..a0a83c454d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java @@ -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() { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/policy/AbstractPolicyDAOImpl.java similarity index 99% rename from components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java rename to components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/policy/AbstractPolicyDAOImpl.java index 063b5c47a8..02dc400578 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/policy/AbstractPolicyDAOImpl.java @@ -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 extractPolicyListFromDbResult(ResultSet resultSet, int tenantId) throws SQLException { + protected List extractPolicyListFromDbResult(ResultSet resultSet, int tenantId) throws SQLException { List policies = new ArrayList<>(); while (resultSet.next()) { Policy policy = new Policy(); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/policy/GenericPolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/policy/GenericPolicyDAOImpl.java new file mode 100644 index 0000000000..d8916c95a9 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/policy/GenericPolicyDAOImpl.java @@ -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 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); + } + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/policy/OraclePolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/policy/OraclePolicyDAOImpl.java new file mode 100644 index 0000000000..c8f89a906a --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/policy/OraclePolicyDAOImpl.java @@ -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 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); + } + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/policy/PostgreSQLPolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/policy/PostgreSQLPolicyDAOImpl.java new file mode 100644 index 0000000000..afb57d3466 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/policy/PostgreSQLPolicyDAOImpl.java @@ -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 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); + } + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/policy/SQLServerPolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/policy/SQLServerPolicyDAOImpl.java new file mode 100644 index 0000000000..dc7e402439 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/policy/SQLServerPolicyDAOImpl.java @@ -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 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); + } + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java index 42e34bf490..f3249e4814 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java @@ -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 getPolicyList() throws PolicyManagementException { - return policyManager.getPolicyList(); + public List getPolicyList(PaginationRequest request) throws PolicyManagementException { + return policyManager.getPolicyList(request); } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java index b0b2c241d0..8f45900a21 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java @@ -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 getPolicies(String type) throws PolicyManagementException; - List 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} - 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 getPolicyList(PaginationRequest request) throws PolicyManagementException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index 13e8665102..1bd77a398f 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -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 getPolicyList() throws PolicyManagementException { - + public List getPolicyList(PaginationRequest request) throws PolicyManagementException { List 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(); } diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index 1c22ba25fc..4d05ebab17 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -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)