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 index 19c9c13715..3b8ecec20c 100644 --- 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 @@ -25,34 +25,102 @@ import org.wso2.carbon.policy.mgt.common.ProfileFeature; import java.util.List; +/** + * This interface represents the key operations related to profile features of device policies. + */ public interface FeatureDAO { -/* Feature addFeature(Feature feature) throws FeatureManagerDAOException; - - List addFeatures(List feature) throws FeatureManagerDAOException; - - Feature updateFeature(Feature feature) throws FeatureManagerDAOException;*/ - + /** + * This method is used to add a feature related to given profile. + * + * @param feature consists of device specific configurations. + * @param profileId id of the profile. + * @return returns ProfileFeature object. + * @throws FeatureManagerDAOException + */ ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException; + /** + * This method is used to update a feature related to given profile. + * @param feature consists of device specific configurations. + * @param profileId id of the profile. + * @return returns updated ProfileFeature object. + * @throws FeatureManagerDAOException + */ ProfileFeature updateProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException; + /** + * This method is used to add set of features to a given profile. + * + * @param features consists of device specific configurations. + * @param profileId id of the profile. + * @return returns list of ProfileFeature objects. + * @throws FeatureManagerDAOException + */ List addProfileFeatures(List features, int profileId) throws FeatureManagerDAOException; + /** + * This method is used to update set of features to a given profile. + * + * @param features consists of device specific configurations. + * @param profileId id of the profile. + * @return returns list of ProfileFeature objects. + * @throws FeatureManagerDAOException + */ List updateProfileFeatures(List features, int profileId) throws FeatureManagerDAOException; + /** + * This method is used to retrieve all the profile features. + * + * @return returns list of ProfileFeature objects. + * @throws FeatureManagerDAOException + */ List getAllProfileFeatures() throws FeatureManagerDAOException; + /** + * This method is used to retrieve all the profile features based on device type. + * + * @return returns list of ProfileFeature objects. + * @throws FeatureManagerDAOException + */ List getAllFeatures(String deviceType) throws FeatureManagerDAOException; - List getFeaturesForProfile(int ProfileId) throws FeatureManagerDAOException; - + /** + * This method is used to retrieve all the profile features of given profile. + * + * @param profileId id of the profile. + * @return returns list of ProfileFeature objects. + * @throws FeatureManagerDAOException + */ + List getFeaturesForProfile(int profileId) throws FeatureManagerDAOException; + + /** + * This method is used remove a feature. + * + * @param featureId id of the removing feature. + * @return returns true if success. + * @throws FeatureManagerDAOException + */ boolean deleteFeature(int featureId) throws FeatureManagerDAOException; + /** + * This method is used to remove set of features of given profile. + * + * @param profile that contains features to be removed. + * @return returns true if success. + * @throws FeatureManagerDAOException + */ boolean deleteFeaturesOfProfile(Profile profile) throws FeatureManagerDAOException; + /** + * This method is used to remove set of features of given profile id. + * + * @param profileId id of the profile. + * @return returns true if success. + * @throws FeatureManagerDAOException + */ boolean deleteFeaturesOfProfile(int profileId) throws FeatureManagerDAOException; } 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 index 174ca08c48..5d91650d7e 100644 --- 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 @@ -24,20 +24,67 @@ import org.wso2.carbon.policy.mgt.common.Profile; import java.util.List; +/** + * This interface represents the key operations related to policy profile. + */ public interface ProfileDAO { + /** + * This method is used to add a profile. + * + * @param profile profile object. + * @return returns added profile object. + * @throws ProfileManagerDAOException + */ Profile addProfile(Profile profile) throws ProfileManagerDAOException; + /** + * This method is used to update a profile + * @param profile profile object. + * @return returns updated profile object. + * @throws ProfileManagerDAOException + */ Profile updateProfile(Profile profile) throws ProfileManagerDAOException; + /** + * This method is used to remove a profile. + * @param profile profile object + * @return returns true if success. + * @throws ProfileManagerDAOException + */ boolean deleteProfile(Profile profile) throws ProfileManagerDAOException; + /** + * This method is used to remove a profile of given policy id. + * @param policyId policy id. + * @return returns true if success. + * @throws ProfileManagerDAOException + */ boolean deleteProfile(int policyId) throws ProfileManagerDAOException; - Profile getProfiles(int profileId) throws ProfileManagerDAOException; + /** + * This method is used to retrieve a profile when id is given. + * @param profileId profile id. + * @return returns profile object. + * @throws ProfileManagerDAOException + */ + Profile getProfile(int profileId) throws ProfileManagerDAOException; + /** + * This method is used to retrieve all the profiles. + * + * @return returns a list of profile objects. + * @throws ProfileManagerDAOException + */ List getAllProfiles() throws ProfileManagerDAOException; + /** + * This method is used to retrieve all the profile of given device type. + * + * @param deviceType device type object. + * @return retruns list of profiles. + * @throws ProfileManagerDAOException + */ List getProfilesOfDeviceType(DeviceType 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/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 index 37dde34099..8670c35b7f 100644 --- 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 @@ -43,115 +43,6 @@ public class FeatureDAOImpl implements FeatureDAO { private static final Log log = LogFactory.getLog(FeatureDAOImpl.class); - -/* @Override - public Feature addFeature(Feature feature) throws FeatureManagerDAOException { - - Connection conn; - PreparedStatement stmt = null; - ResultSet generatedKeys = null; - - try { - conn = this.getConnection(); - String query = "INSERT INTO DM_FEATURES (NAME, CODE, DESCRIPTION) VALUES (?, ?, ?)"; - stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); - stmt.setString(1, feature.getName()); - stmt.setString(2, feature.getCode()); - stmt.setString(3, feature.getDescription()); - int affectedRows = stmt.executeUpdate(); - - if (log.isDebugEnabled()) { - log.debug(affectedRows + " feature is 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(stmt, generatedKeys); - } - return feature; - }*/ - - /* @Override - public List addFeatures(List features) throws FeatureManagerDAOException { - - Connection conn; - PreparedStatement stmt = null; - ResultSet generatedKeys = null; - List featureList = new ArrayList(); - - try { - conn = this.getConnection(); - String query = "INSERT INTO DM_FEATURES (NAME, CODE, DESCRIPTION) VALUES (?, ?, ?)"; - stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); - - for (Feature feature : features) { - stmt.setString(1, feature.getName()); - stmt.setString(2, feature.getCode()); - stmt.setString(3, feature.getDescription()); - stmt.addBatch(); - } - - int[] affectedRows = stmt.executeBatch(); - - generatedKeys = stmt.getGeneratedKeys(); - - if (log.isDebugEnabled()) { - log.debug(affectedRows.length + " features are added to the database."); - } - generatedKeys = stmt.getGeneratedKeys(); - int i = 0; - - while (generatedKeys.next()) { - features.get(i).setId(generatedKeys.getInt(1)); - i++; - } - } 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(stmt, generatedKeys); - } - return featureList; - }*/ - - - /* @Override - public Feature updateFeature(Feature feature) throws FeatureManagerDAOException { - - Connection conn; - PreparedStatement stmt = null; - - try { - conn = this.getConnection(); - String query = "UPDATE DM_FEATURES SET NAME = ?, CODE = ?, DESCRIPTION = ? WHERE ID = ?"; - stmt = conn.prepareStatement(query); - stmt.setString(1, feature.getName()); - stmt.setString(2, feature.getCode()); - stmt.setString(3, feature.getDescription()); - stmt.setInt(4, feature.getId()); - stmt.executeUpdate(); - - } catch (SQLException e) { - String msg = "Error occurred while updating feature " + feature.getName() + " (Feature Name) to the - database."; - log.error(msg, e); - throw new FeatureManagerDAOException(msg, e); - } finally { - PolicyManagementDAOUtil.cleanupResources(stmt, null); - } - - return feature; - }*/ - @Override public ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException { return null; @@ -247,7 +138,6 @@ public class FeatureDAOImpl implements FeatureDAO { @Override public boolean deleteFeaturesOfProfile(Profile profile) throws FeatureManagerDAOException { - Connection conn; PreparedStatement stmt = null; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -258,9 +148,10 @@ public class FeatureDAOImpl implements FeatureDAO { stmt = conn.prepareStatement(query); stmt.setInt(1, profile.getProfileId()); stmt.setInt(2, tenantId); - stmt.executeUpdate(); - return true; - + if (stmt.executeUpdate() > 0) { + return true; + } + return false; } catch (SQLException e) { throw new FeatureManagerDAOException("Error occurred while deleting the feature related to a profile.", e); } finally { @@ -270,7 +161,6 @@ public class FeatureDAOImpl implements FeatureDAO { @Override public boolean deleteFeaturesOfProfile(int profileId) throws FeatureManagerDAOException { - Connection conn; PreparedStatement stmt = null; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -280,9 +170,10 @@ public class FeatureDAOImpl implements FeatureDAO { stmt = conn.prepareStatement(query); stmt.setInt(1, profileId); stmt.setInt(2, tenantId); - stmt.executeUpdate(); - return true; - + if (stmt.executeUpdate() > 0) { + return true; + } + return false; } catch (SQLException e) { throw new FeatureManagerDAOException("Error occurred while deleting the feature related to a profile.", e); } finally { @@ -448,7 +339,6 @@ public class FeatureDAOImpl implements FeatureDAO { @Override public boolean deleteFeature(int featureId) throws FeatureManagerDAOException { - Connection conn; PreparedStatement stmt = null; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -459,9 +349,10 @@ public class FeatureDAOImpl implements FeatureDAO { stmt = conn.prepareStatement(query); stmt.setInt(1, featureId); stmt.setInt(2, tenantId); - stmt.executeUpdate(); - return true; - + if(stmt.executeUpdate() > 0) { + return true; + } + return false; } catch (SQLException e) { throw new FeatureManagerDAOException("Unable to delete the feature " + featureId + " (Feature ID) " + "from database.", e); @@ -471,7 +362,6 @@ public class FeatureDAOImpl implements FeatureDAO { } private Connection getConnection() throws FeatureManagerDAOException { - try { return PolicyManagementDAOFactory.getConnection(); } catch (PolicyManagerDAOException 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 index 58ceefba76..89d06d0a10 100644 --- 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 @@ -133,7 +133,6 @@ public class ProfileDAOImpl implements ProfileDAO { @Override public boolean deleteProfile(Profile profile) throws ProfileManagerDAOException { - Connection conn; PreparedStatement stmt = null; @@ -142,9 +141,10 @@ public class ProfileDAOImpl implements ProfileDAO { String query = "DELETE FROM DM_PROFILE WHERE ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, profile.getProfileId()); - stmt.executeUpdate(); - return true; - + if (stmt.executeUpdate() > 0) { + return true; + } + return false; } catch (SQLException e) { String msg = "Error occurred while deleting the profile from the data base."; log.error(msg); @@ -164,9 +164,10 @@ public class ProfileDAOImpl implements ProfileDAO { String query = "DELETE FROM DM_PROFILE WHERE ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, profileId); - stmt.executeUpdate(); - return true; - + if (stmt.executeUpdate() > 0) { + return true; + } + return false; } catch (SQLException e) { String msg = "Error occurred while deleting the profile from the data base."; log.error(msg); @@ -178,8 +179,7 @@ public class ProfileDAOImpl implements ProfileDAO { @Override - public Profile getProfiles(int profileId) throws ProfileManagerDAOException { - + public Profile getProfile(int profileId) throws ProfileManagerDAOException { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; @@ -217,7 +217,6 @@ public class ProfileDAOImpl implements ProfileDAO { @Override public List getAllProfiles() throws ProfileManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; @@ -259,7 +258,6 @@ public class ProfileDAOImpl implements ProfileDAO { @Override public List getProfilesOfDeviceType(DeviceType deviceType) throws ProfileManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; 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 f026f789d0..1d31fd0738 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 @@ -397,7 +397,7 @@ public class PolicyManagerImpl implements PolicyManager { // policyDAO.getTimesOfPolicy(policy); // policyDAO.getLocationsOfPolicy(policy); - profile = profileDAO.getProfiles(profileId); + profile = profileDAO.getProfile(profileId); policy.setProfile(profile); policy.setRoles(roleNames); @@ -433,7 +433,7 @@ public class PolicyManagerImpl implements PolicyManager { // policyDAO.getTimesOfPolicy(policy); // policyDAO.getLocationsOfPolicy(policy); - Profile profile = profileDAO.getProfiles(policy.getProfileId()); + Profile profile = profileDAO.getProfile(policy.getProfileId()); policy.setProfile(profile); policy.setRoles(roleNames); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java index 2dc77739cd..197b0f933b 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java @@ -147,7 +147,7 @@ public class ProfileManagerImpl implements ProfileManager { try { PolicyManagementDAOFactory.openConnection(); - profile = profileDAO.getProfiles(profileId); + profile = profileDAO.getProfile(profileId); featureList = featureDAO.getFeaturesForProfile(profileId); deviceType = deviceTypeDAO.getDeviceType(profile.getDeviceType().getId());