From 5bdbcaa0775cfd57872a2597cd8869af0500dc27 Mon Sep 17 00:00:00 2001 From: Geeth Munasinghe Date: Wed, 18 Mar 2015 14:00:21 +0530 Subject: [PATCH] Seperating the dao to Feature, profile, policy and adding the exeception for each ones --- .../policy/mgt/core/dao/FeatureDAO.java | 42 +++ .../core/dao/FeatureManagerDAOException.java | 56 ++++ .../carbon/policy/mgt/core/dao/PolicyDAO.java | 13 +- .../policy/mgt/core/dao/ProfileDAO.java | 38 +++ .../core/dao/ProfileManagerDAOException.java | 55 ++++ .../mgt/core/dao/impl/FeatureDAOImpl.java | 196 +++++++++++++ .../mgt/core/dao/impl/PolicyDAOImpl.java | 277 ------------------ .../mgt/core/dao/impl/ProfileDAOImpl.java | 195 ++++++++++++ .../impl/PolicyAdministratorPointImpl.java | 33 ++- .../policy/mgt/core/PolicyDAOTestCase.java | 6 +- .../point/PolicyEvaluationException.java | 16 +- 11 files changed, 622 insertions(+), 305 deletions(-) create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureManagerDAOException.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileManagerDAOException.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java new file mode 100644 index 0000000000..1014756202 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java @@ -0,0 +1,42 @@ +/* +* Copyright (c) 2015 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you under the Apache License, +* Version 2.0 (the "License"); you may not use this file except +* in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.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; + +import org.wso2.carbon.policy.mgt.common.Feature; +import org.wso2.carbon.policy.mgt.common.FeatureManagementException; +import org.wso2.carbon.policy.mgt.common.Profile; + +import java.util.List; + +public interface FeatureDAO { + + Feature addFeature(Feature feature) throws FeatureManagerDAOException; + + Feature updateFeature(Feature feature) throws FeatureManagerDAOException; + + List getAllFeatures() throws FeatureManagerDAOException; + + List getFeaturesForProfile(int ProfileId) throws FeatureManagerDAOException; + + void deleteFeature(int featureId) throws FeatureManagerDAOException; + + void deleteFeaturesOfProfile(Profile profile) throws FeatureManagerDAOException; + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureManagerDAOException.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureManagerDAOException.java new file mode 100644 index 0000000000..02ff4a00d6 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureManagerDAOException.java @@ -0,0 +1,56 @@ +/* +* Copyright (c) 2015 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you under the Apache License, +* Version 2.0 (the "License"); you may not use this file except +* in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.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; + +public class FeatureManagerDAOException extends Exception { + + private String featureDAOErrorMessage; + + public String getFeatureDAOErrorMessage() { + return featureDAOErrorMessage; + } + + public void setFeatureDAOErrorMessage(String featureDAOErrorMessage) { + this.featureDAOErrorMessage = featureDAOErrorMessage; + } + + public FeatureManagerDAOException(String message) { + super(message); + setFeatureDAOErrorMessage(message); + } + + public FeatureManagerDAOException(String message, Exception ex) { + super(message, ex); + setFeatureDAOErrorMessage(message); + } + + public FeatureManagerDAOException(String message, Throwable cause) { + super(message, cause); + setFeatureDAOErrorMessage(message); + } + + public FeatureManagerDAOException() { + super(); + } + + public FeatureManagerDAOException(Throwable cause) { + super(cause); + } + +} 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 0ed1d8e529..8915e234ec 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 @@ -45,7 +45,13 @@ public interface PolicyDAO { void deletePolicy(Policy policy) throws PolicyManagerDAOException; - Profile addProfile(Profile profile) throws PolicyManagerDAOException; + + + + + + + /* Profile addProfile(Profile profile) throws PolicyManagerDAOException; Profile updateProfile(Profile profile) throws PolicyManagerDAOException; @@ -55,6 +61,9 @@ public interface PolicyDAO { List getProfilesOfDeviceType(String deviceType) throws PolicyManagerDAOException; + + + List getAllFeatures() throws PolicyManagerDAOException; List getFeaturesForProfile(int ProfileId) throws PolicyManagerDAOException; @@ -65,5 +74,5 @@ public interface PolicyDAO { Feature addFeature(Feature feature) throws PolicyManagerDAOException, FeatureManagementException; - Feature updateFeature(Feature feature) throws PolicyManagerDAOException, FeatureManagementException; + Feature updateFeature(Feature feature) throws PolicyManagerDAOException, FeatureManagementException;*/ } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java new file mode 100644 index 0000000000..b35354d8ad --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java @@ -0,0 +1,38 @@ +/* +* Copyright (c) 2015 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you under the Apache License, +* Version 2.0 (the "License"); you may not use this file except +* in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.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; + +import org.wso2.carbon.policy.mgt.common.Profile; + +import java.util.List; + +public interface ProfileDAO { + + Profile addProfile(Profile profile) throws ProfileManagerDAOException; + + Profile updateProfile(Profile profile) throws ProfileManagerDAOException; + + void deleteProfile(Profile profile) throws ProfileManagerDAOException; + + List getAllProfiles() throws ProfileManagerDAOException; + + List getProfilesOfDeviceType(String deviceType) throws ProfileManagerDAOException; + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileManagerDAOException.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileManagerDAOException.java new file mode 100644 index 0000000000..ba859ed784 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileManagerDAOException.java @@ -0,0 +1,55 @@ +/* +* Copyright (c) 2015 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you under the Apache License, +* Version 2.0 (the "License"); you may not use this file except +* in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.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; + +public class ProfileManagerDAOException extends Exception{ + + private String profileDAOErrorMessage; + + public String getProfileDAOErrorMessage() { + return profileDAOErrorMessage; + } + + public void setProfileDAOErrorMessage(String profileDAOErrorMessage) { + this.profileDAOErrorMessage = profileDAOErrorMessage; + } + + public ProfileManagerDAOException(String message) { + super(message); + setProfileDAOErrorMessage(message); + } + + public ProfileManagerDAOException(String message, Exception ex) { + super(message, ex); + setProfileDAOErrorMessage(message); + } + + public ProfileManagerDAOException(String message, Throwable cause) { + super(message, cause); + setProfileDAOErrorMessage(message); + } + + public ProfileManagerDAOException() { + super(); + } + + public ProfileManagerDAOException(Throwable cause) { + super(cause); + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java new file mode 100644 index 0000000000..42913cc7d6 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java @@ -0,0 +1,196 @@ +/* +* Copyright (c) 2015 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you under the Apache License, +* Version 2.0 (the "License"); you may not use this file except +* in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.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; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.policy.mgt.common.Feature; +import org.wso2.carbon.policy.mgt.common.FeatureManagementException; +import org.wso2.carbon.policy.mgt.common.Profile; +import org.wso2.carbon.policy.mgt.core.dao.*; +import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class FeatureDAOImpl implements FeatureDAO { + + private static final Log log = LogFactory.getLog(FeatureDAOImpl.class); + + + @Override + public Feature addFeature(Feature feature) throws FeatureManagerDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + + try { + conn = this.getConnection(); + String query = "INSERT INTO DM_FEATURES (NAME, CODE, DESCRIPTION, EVALUVATION_RULE) VALUES (?, ?, ?, ?)"; + stmt = conn.prepareStatement(query, stmt.RETURN_GENERATED_KEYS); + stmt.setString(1, feature.getName()); + stmt.setString(2, feature.getCode()); + stmt.setString(3, feature.getDescription()); + stmt.setString(4, feature.getRuleValue()); + + int affectedRows = stmt.executeUpdate(); + + if (log.isDebugEnabled()) { + log.debug(affectedRows + " Features are added."); + } + generatedKeys = stmt.getGeneratedKeys(); + while (generatedKeys.next()) { + feature.setId(generatedKeys.getInt(1)); + } + } catch (SQLException e) { + String msg = "Error occurred while adding feature to the database."; + log.error(msg, e); + throw new FeatureManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys); + } + return feature; + } + + + @Override + public Feature updateFeature(Feature feature) throws FeatureManagerDAOException { + return feature; + } + + @Override + public void deleteFeaturesOfProfile(Profile profile) throws FeatureManagerDAOException { + Connection conn = null; + PreparedStatement stmt = null; + + try { + conn = this.getConnection(); + String query = "DELETE FROM DM_PROFILE_FEATURES WHERE PROFILE_ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, profile.getProfileId()); + stmt.executeUpdate(); + } catch (SQLException e) { + String msg = "Error occurred while deleting the feature related to a profile."; + log.error(msg); + throw new FeatureManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, null); + } + } + + + @Override + public List getAllFeatures() throws FeatureManagerDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet resultSet = null; + + List featureList = new ArrayList(); + + try { + conn = this.getConnection(); + String query = "SELECT ID, NAME, CODE, EVALUVATION_RULE FROM DM_FEATURES"; + stmt = conn.prepareStatement(query); + resultSet = stmt.executeQuery(); + + while (resultSet.next()) { + Feature feature = new Feature(); + feature.setId(resultSet.getInt("ID")); + feature.setCode(resultSet.getString("CODE")); + feature.setName(resultSet.getString("NAME")); + feature.setRuleValue(resultSet.getString("EVALUVATION_RULE")); + featureList.add(feature); + } + + } catch (SQLException e) { + String msg = "Unable to get the list of the features from database."; + log.error(msg); + throw new FeatureManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet); + } + return featureList; + } + + @Override + public List getFeaturesForProfile(int profileId) throws FeatureManagerDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet resultSet = null; + + List featureList = new ArrayList(); + + try { + conn = this.getConnection(); + String query = "SELECT PF.FEATURE_ID FEATURE_ID, F.NAME NAME, F.CODE CODE, " + + "F.EVALUVATION_RULE RULE, F.CONTENT AS CONTENT FROM DM_PROFILE_FEATURES AS PF " + + "JOIN DM_FEATURES AS F ON F.ID = PF.FEATURE_ID WHERE PROFILE_ID=?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, profileId); + resultSet = stmt.executeQuery(); + + while (resultSet.next()) { + Feature feature = new Feature(); + feature.setId(resultSet.getInt("FEATURE_ID")); + feature.setCode(resultSet.getString("CODE")); + feature.setName(resultSet.getString("NAME")); + feature.setAttribute(resultSet.getObject("CONTENT")); + feature.setRuleValue(resultSet.getString("RULE")); + featureList.add(feature); + } + + } catch (SQLException e) { + String msg = "Unable to get the list of the features from database."; + log.error(msg); + throw new FeatureManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet); + } + return featureList; + } + + @Override + public void deleteFeature(int featureId) throws FeatureManagerDAOException { + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + + try { + conn = this.getConnection(); + String query = ""; + stmt = conn.prepareStatement(query); + } catch (SQLException e) { + + } + } + + private Connection getConnection() throws FeatureManagerDAOException { + try { + return PolicyManagementDAOFactory.getDataSource().getConnection(); + } catch (SQLException e) { + throw new FeatureManagerDAOException("Error occurred while obtaining a connection from the policy " + + "management metadata repository datasource", e); + } + } +} 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/PolicyDAOImpl.java index 20b46af07c..c28a5ca087 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/PolicyDAOImpl.java @@ -258,287 +258,10 @@ public class PolicyDAOImpl implements PolicyDAO { } - private void persistFeatures(Profile profile) throws PolicyManagerDAOException { - Connection conn = null; - PreparedStatement stmt = null; - try { - conn = this.getConnection(); - String query = "INSERT INTO DM_PROFILE_FEATURES (PROFILE_ID, FEATURE_ID, CONTENT) VALUES (?, ?, ?)"; - - stmt = conn.prepareStatement(query); - for (Feature feature : profile.getFeaturesList()) { - stmt.setInt(1, profile.getProfileId()); - stmt.setInt(2, feature.getId()); - stmt.setObject(3, feature.getAttribute()); - stmt.addBatch(); - //Not adding the logic to check the size of the stmt and execute if the size records added is over 1000 - } - stmt.executeBatch(); - } catch (SQLException e) { - String msg = "Error occurred while adding the feature list to the database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); - } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, null); - } - } - - public Profile addProfile(Profile profile) throws PolicyManagerDAOException { - Connection conn = null; - PreparedStatement stmt = null; - ResultSet generatedKeys = null; - - // TODO : find a way to get the tenant Id. - int tenantId = -1234; - try { - conn = this.getConnection(); - String query = "INSERT INTO DM_PROFILE (PROFILE_NAME,TENANT_ID, DEVICE_TYPE_ID, CREATED_TIME, UPDATED_TIME) VALUES (?, ?, ?)"; - stmt = conn.prepareStatement(query, stmt.RETURN_GENERATED_KEYS); - - stmt.setString(1, profile.getProfileName()); - stmt.setInt(2, tenantId); - stmt.setLong(3, profile.getDeviceType().getId()); - stmt.setTimestamp(4, profile.getCreatedDate()); - stmt.setTimestamp(5, profile.getUpdatedDate()); - - int affectedRows = stmt.executeUpdate(); - - if (affectedRows == 0 && log.isDebugEnabled()) { - String msg = "No rows are updated on the profile table."; - log.debug(msg); - } - generatedKeys = stmt.getGeneratedKeys(); - - if (generatedKeys.next()) { - profile.setProfileId(generatedKeys.getInt(1)); - } - // Checking the profile id here, because profile id could have been passed from the calling method. - if (profile.getProfileId() == 0) { - throw new RuntimeException("Profile id is 0, this could be an issue."); - } - - persistFeatures(profile); - - } catch (SQLException e) { - String msg = "Error occurred while adding the profile to database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); - } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys); - } - - return profile; - } - - - public Profile updateProfile(Profile profile) throws PolicyManagerDAOException { - return profile; - } - - @Override - public void deleteProfile(Profile profile) throws PolicyManagerDAOException { - // First delete the features related to the profile - deleteFeaturesOfProfile(profile); - Connection conn = null; - PreparedStatement stmt = null; - try { - conn = this.getConnection(); - String query = "DELETE FROM DM_PROFILE WHERE ID = ?"; - stmt = conn.prepareStatement(query); - stmt.setInt(1, profile.getProfileId()); - stmt.executeUpdate(); - } catch (SQLException e) { - String msg = "Error occurred while deleting the profile from the data base."; - log.error(msg); - throw new PolicyManagerDAOException(msg, e); - } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, null); - } - } - @Override - public void deleteFeaturesOfProfile(Profile profile) throws PolicyManagerDAOException { - Connection conn = null; - PreparedStatement stmt = null; - try { - conn = this.getConnection(); - String query = "DELETE FROM DM_PROFILE_FEATURES WHERE PROFILE_ID = ?"; - stmt = conn.prepareStatement(query); - stmt.setInt(1, profile.getProfileId()); - stmt.executeUpdate(); - } catch (SQLException e) { - String msg = "Error occurred while deleting the feature related to a profile."; - log.error(msg); - throw new PolicyManagerDAOException(msg, e); - } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, null); - } - } - - @Override - public Feature addFeature(Feature feature) throws PolicyManagerDAOException, FeatureManagementException { - Connection conn = null; - PreparedStatement stmt = null; - ResultSet generatedKeys = null; - - try { - conn = this.getConnection(); - String query = "INSERT INTO DM_FEATURES (NAME, CODE, DESCRIPTION, EVALUVATION_RULE) VALUES (?, ?, ?, ?)"; - stmt = conn.prepareStatement(query, stmt.RETURN_GENERATED_KEYS); - stmt.setString(1, feature.getName()); - stmt.setString(2, feature.getCode()); - stmt.setString(3, feature.getDescription()); - stmt.setString(4, feature.getRuleValue()); - - int affectedRows = stmt.executeUpdate(); - - if (log.isDebugEnabled()) { - log.debug(affectedRows + " Features are added."); - } - generatedKeys = stmt.getGeneratedKeys(); - while (generatedKeys.next()) { - feature.setId(generatedKeys.getInt(1)); - } - } catch (SQLException e) { - String msg = "Error occurred while adding feature to the database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); - } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys); - } - return feature; - } - - - @Override - public Feature updateFeature(Feature feature) throws PolicyManagerDAOException, FeatureManagementException { - return feature; - } - - @Override - public List getAllProfiles() throws PolicyManagerDAOException { - Connection conn = null; - PreparedStatement stmt = null; - ResultSet generatedKeys = null; - - try { - conn = this.getConnection(); - String query = ""; - stmt = conn.prepareStatement(query); - } catch (SQLException e) { - - } - - return null; - } - - @Override - public List getProfilesOfDeviceType(String deviceType) throws PolicyManagerDAOException { - - - Connection conn = null; - PreparedStatement stmt = null; - ResultSet generatedKeys = null; - - try { - conn = this.getConnection(); - String query = ""; - stmt = conn.prepareStatement(query); - } catch (SQLException e) { - - } - - return null; - } - - @Override - public List getAllFeatures() throws PolicyManagerDAOException { - Connection conn = null; - PreparedStatement stmt = null; - ResultSet resultSet = null; - - List featureList = new ArrayList(); - - try { - conn = this.getConnection(); - String query = "SELECT ID, NAME, CODE, EVALUVATION_RULE FROM DM_FEATURES"; - stmt = conn.prepareStatement(query); - resultSet = stmt.executeQuery(); - - while (resultSet.next()) { - Feature feature = new Feature(); - feature.setId(resultSet.getInt("ID")); - feature.setCode(resultSet.getString("CODE")); - feature.setName(resultSet.getString("NAME")); - feature.setRuleValue(resultSet.getString("EVALUVATION_RULE")); - featureList.add(feature); - } - - } catch (SQLException e) { - String msg = "Unable to get the list of the features from database."; - log.error(msg); - throw new PolicyManagerDAOException(msg, e); - } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet); - } - return featureList; - } - - @Override - public List getFeaturesForProfile(int profileId) throws PolicyManagerDAOException { - Connection conn = null; - PreparedStatement stmt = null; - ResultSet resultSet = null; - - List featureList = new ArrayList(); - - try { - conn = this.getConnection(); - String query = "SELECT PF.FEATURE_ID FEATURE_ID, F.NAME NAME, F.CODE CODE, " + - "F.EVALUVATION_RULE RULE, F.CONTENT AS CONTENT FROM DM_PROFILE_FEATURES AS PF " + - "JOIN DM_FEATURES AS F ON F.ID = PF.FEATURE_ID WHERE PROFILE_ID=?"; - stmt = conn.prepareStatement(query); - stmt.setInt(1, profileId); - resultSet = stmt.executeQuery(); - - while (resultSet.next()) { - Feature feature = new Feature(); - feature.setId(resultSet.getInt("FEATURE_ID")); - feature.setCode(resultSet.getString("CODE")); - feature.setName(resultSet.getString("NAME")); - feature.setAttribute(resultSet.getObject("CONTENT")); - feature.setRuleValue(resultSet.getString("RULE")); - featureList.add(feature); - } - - } catch (SQLException e) { - String msg = "Unable to get the list of the features from database."; - log.error(msg); - throw new PolicyManagerDAOException(msg, e); - } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet); - } - return featureList; - } - - @Override - public void deleteFeature(int featureId) throws PolicyManagerDAOException { - - Connection conn = null; - PreparedStatement stmt = null; - ResultSet generatedKeys = null; - - try { - conn = this.getConnection(); - String query = ""; - stmt = conn.prepareStatement(query); - } catch (SQLException e) { - - } - } /** diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java new file mode 100644 index 0000000000..0c09c36eb9 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java @@ -0,0 +1,195 @@ +/* +* Copyright (c) 2015 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you under the Apache License, +* Version 2.0 (the "License"); you may not use this file except +* in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.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; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.policy.mgt.common.Feature; +import org.wso2.carbon.policy.mgt.common.Profile; +import org.wso2.carbon.policy.mgt.core.dao.*; +import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; + +public class ProfileDAOImpl implements ProfileDAO { + + private static final Log log = LogFactory.getLog(ProfileDAOImpl.class); + + + public Profile addProfile(Profile profile) throws ProfileManagerDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + + // TODO : find a way to get the tenant Id. + int tenantId = -1234; + try { + conn = this.getConnection(); + String query = "INSERT INTO DM_PROFILE (PROFILE_NAME,TENANT_ID, DEVICE_TYPE_ID, CREATED_TIME, UPDATED_TIME) VALUES (?, ?, ?)"; + stmt = conn.prepareStatement(query, stmt.RETURN_GENERATED_KEYS); + + stmt.setString(1, profile.getProfileName()); + stmt.setInt(2, tenantId); + stmt.setLong(3, profile.getDeviceType().getId()); + stmt.setTimestamp(4, profile.getCreatedDate()); + stmt.setTimestamp(5, profile.getUpdatedDate()); + + int affectedRows = stmt.executeUpdate(); + + if (affectedRows == 0 && log.isDebugEnabled()) { + String msg = "No rows are updated on the profile table."; + log.debug(msg); + } + generatedKeys = stmt.getGeneratedKeys(); + + if (generatedKeys.next()) { + profile.setProfileId(generatedKeys.getInt(1)); + } + // Checking the profile id here, because profile id could have been passed from the calling method. + if (profile.getProfileId() == 0) { + throw new RuntimeException("Profile id is 0, this could be an issue."); + } + + persistFeatures(profile); + + } catch (SQLException e) { + String msg = "Error occurred while adding the profile to database."; + log.error(msg, e); + throw new ProfileManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys); + } + + return profile; + } + + + public Profile updateProfile(Profile profile) throws ProfileManagerDAOException { + return profile; + } + + @Override + public void deleteProfile(Profile profile) throws ProfileManagerDAOException { + + // First delete the features related to the profile + FeatureDAOImpl featureDAO = new FeatureDAOImpl(); + try { + featureDAO.deleteFeaturesOfProfile(profile); + } catch (FeatureManagerDAOException e) { + String msg = "Error occurred while deleting features."; + log.error(msg, e); + throw new ProfileManagerDAOException(msg, e); + } + + + Connection conn = null; + PreparedStatement stmt = null; + + try { + conn = this.getConnection(); + String query = "DELETE FROM DM_PROFILE WHERE ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, profile.getProfileId()); + stmt.executeUpdate(); + } catch (SQLException e) { + String msg = "Error occurred while deleting the profile from the data base."; + log.error(msg); + throw new ProfileManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, null); + } + } + + private void persistFeatures(Profile profile) throws ProfileManagerDAOException { + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String query = "INSERT INTO DM_PROFILE_FEATURES (PROFILE_ID, FEATURE_ID, CONTENT) VALUES (?, ?, ?)"; + + stmt = conn.prepareStatement(query); + for (Feature feature : profile.getFeaturesList()) { + stmt.setInt(1, profile.getProfileId()); + stmt.setInt(2, feature.getId()); + stmt.setObject(3, feature.getAttribute()); + stmt.addBatch(); + //Not adding the logic to check the size of the stmt and execute if the size records added is over 1000 + } + stmt.executeBatch(); + } catch (SQLException e) { + String msg = "Error occurred while adding the feature list to the database."; + log.error(msg, e); + throw new ProfileManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, null); + } + } + + + @Override + public List getAllProfiles() throws ProfileManagerDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + + try { + conn = this.getConnection(); + String query = ""; + stmt = conn.prepareStatement(query); + } catch (SQLException e) { + + } + + return null; + } + + @Override + public List getProfilesOfDeviceType(String deviceType) throws ProfileManagerDAOException { + + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + + try { + conn = this.getConnection(); + String query = ""; + stmt = conn.prepareStatement(query); + } catch (SQLException e) { + + } + + return null; + } + + + private Connection getConnection() throws ProfileManagerDAOException { + try { + return PolicyManagementDAOFactory.getDataSource().getConnection(); + } catch (SQLException e) { + throw new ProfileManagerDAOException("Error occurred while obtaining a connection from the policy " + + "management metadata repository datasource", 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 3cb9ceaff1..c21bf86d69 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 @@ -26,16 +26,25 @@ import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.common.Profile; +import org.wso2.carbon.policy.mgt.core.dao.FeatureManagerDAOException; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException; +import org.wso2.carbon.policy.mgt.core.dao.ProfileManagerDAOException; +import org.wso2.carbon.policy.mgt.core.dao.impl.FeatureDAOImpl; import org.wso2.carbon.policy.mgt.core.dao.impl.PolicyDAOImpl; +import org.wso2.carbon.policy.mgt.core.dao.impl.ProfileDAOImpl; public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { private static final Log log = LogFactory.getLog(PolicyAdministratorPointImpl.class); + PolicyDAOImpl policyDAO; + FeatureDAOImpl featureDAO; + ProfileDAOImpl profileDAO; public PolicyAdministratorPointImpl() { policyDAO = new PolicyDAOImpl(); + featureDAO = new FeatureDAOImpl(); + profileDAO = new ProfileDAOImpl(); } @Override @@ -128,8 +137,8 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { @Override public Profile addProfile(Profile profile) throws PolicyManagementException { try { - profile = policyDAO.addProfile(profile); - } catch (PolicyManagerDAOException e) { + profile = profileDAO.addProfile(profile); + } catch (ProfileManagerDAOException e) { String msg = "Error occurred while persisting the policy."; log.error(msg, e); throw new PolicyManagementException(msg, e); @@ -146,8 +155,8 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { @Override public Profile updateProfile(Profile profile) throws PolicyManagementException { try { - profile = policyDAO.updateProfile(profile); - } catch (PolicyManagerDAOException e) { + profile = profileDAO.updateProfile(profile); + } catch (ProfileManagerDAOException e) { String msg = "Error occurred while persisting the profile."; log.error(msg, e); throw new PolicyManagementException(msg, e); @@ -159,12 +168,8 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { @Override public Feature addFeature(Feature feature) throws FeatureManagementException { try { - feature = policyDAO.addFeature(feature); - } catch (FeatureManagementException e) { - String msg = "Error occurred while persisting the feature."; - log.error(msg, e); - throw new FeatureManagementException(msg, e); - } catch (PolicyManagerDAOException e) { + feature = featureDAO.addFeature(feature); + } catch (FeatureManagerDAOException e) { String msg = "Error occurred while persisting the feature."; log.error(msg, e); throw new FeatureManagementException(msg, e); @@ -176,12 +181,8 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { @Override public Feature updateFeature(Feature feature) throws FeatureManagementException { try { - feature = policyDAO.updateFeature(feature); - } catch (PolicyManagerDAOException e) { - String msg = "Error occurred while persisting the feature."; - log.error(msg, e); - throw new FeatureManagementException(msg, e); - } catch (FeatureManagementException e) { + feature = featureDAO.updateFeature(feature); + } catch (FeatureManagerDAOException e) { String msg = "Error occurred while persisting the feature."; log.error(msg, e); throw new FeatureManagementException(msg, e); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java index cddc3f834c..2c4d670cca 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java @@ -31,8 +31,10 @@ import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.core.common.DBTypes; import org.wso2.carbon.policy.mgt.core.common.TestDBConfiguration; import org.wso2.carbon.policy.mgt.core.common.TestDBConfigurations; +import org.wso2.carbon.policy.mgt.core.dao.FeatureManagerDAOException; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException; +import org.wso2.carbon.policy.mgt.core.dao.impl.FeatureDAOImpl; import org.wso2.carbon.policy.mgt.core.dao.impl.PolicyDAOImpl; import org.wso2.carbon.policy.mgt.core.util.FeatureCreator; import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; @@ -144,9 +146,9 @@ public class PolicyDAOTestCase { } @Test - public void addFeatures() throws PolicyManagerDAOException, PolicyManagementException, FeatureManagementException { + public void addFeatures() throws FeatureManagerDAOException { - PolicyDAOImpl policyDAO = new PolicyDAOImpl(); + FeatureDAOImpl policyDAO = new FeatureDAOImpl(); List featureList = FeatureCreator.getFeatureList(); for (Feature feature : featureList) { policyDAO.addFeature(feature); diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationException.java b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationException.java index f3dcdb6cd5..b779b6ab6c 100644 --- a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationException.java +++ b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationException.java @@ -20,29 +20,29 @@ package org.wso2.carbon.simple.policy.decision.point; public class PolicyEvaluationException extends Exception { - private String policyErrorMessage; + private String policyEvaluationErrorMessage; - public String getPolicyErrorMessage() { - return policyErrorMessage; + public String getPolicyEvaluationErrorMessage() { + return policyEvaluationErrorMessage; } - public void setPolicyErrorMessage(String policyErrorMessage) { - this.policyErrorMessage = policyErrorMessage; + public void setPolicyEvaluationErrorMessage(String policyEvaluationErrorMessage) { + this.policyEvaluationErrorMessage = policyEvaluationErrorMessage; } public PolicyEvaluationException(String message) { super(message); - setPolicyErrorMessage(message); + setPolicyEvaluationErrorMessage(message); } public PolicyEvaluationException(String message, Exception ex) { super(message, ex); - setPolicyErrorMessage(message); + setPolicyEvaluationErrorMessage(message); } public PolicyEvaluationException(String message, Throwable cause) { super(message, cause); - setPolicyErrorMessage(message); + setPolicyEvaluationErrorMessage(message); } public PolicyEvaluationException() {