Seperating the dao to Feature, profile, policy and adding the exeception for each ones

revert-70aa11f8
Geeth Munasinghe 10 years ago
parent d7abfbc19b
commit 5bdbcaa077

@ -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<Feature> getAllFeatures() throws FeatureManagerDAOException;
List<Feature> getFeaturesForProfile(int ProfileId) throws FeatureManagerDAOException;
void deleteFeature(int featureId) throws FeatureManagerDAOException;
void deleteFeaturesOfProfile(Profile profile) throws FeatureManagerDAOException;
}

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

@ -45,7 +45,13 @@ public interface PolicyDAO {
void deletePolicy(Policy policy) throws PolicyManagerDAOException; void deletePolicy(Policy policy) throws PolicyManagerDAOException;
Profile addProfile(Profile profile) throws PolicyManagerDAOException;
/* Profile addProfile(Profile profile) throws PolicyManagerDAOException;
Profile updateProfile(Profile profile) throws PolicyManagerDAOException; Profile updateProfile(Profile profile) throws PolicyManagerDAOException;
@ -55,6 +61,9 @@ public interface PolicyDAO {
List<Profile> getProfilesOfDeviceType(String deviceType) throws PolicyManagerDAOException; List<Profile> getProfilesOfDeviceType(String deviceType) throws PolicyManagerDAOException;
List<Feature> getAllFeatures() throws PolicyManagerDAOException; List<Feature> getAllFeatures() throws PolicyManagerDAOException;
List<Feature> getFeaturesForProfile(int ProfileId) throws PolicyManagerDAOException; List<Feature> getFeaturesForProfile(int ProfileId) throws PolicyManagerDAOException;
@ -65,5 +74,5 @@ public interface PolicyDAO {
Feature addFeature(Feature feature) throws PolicyManagerDAOException, FeatureManagementException; Feature addFeature(Feature feature) throws PolicyManagerDAOException, FeatureManagementException;
Feature updateFeature(Feature feature) throws PolicyManagerDAOException, FeatureManagementException; Feature updateFeature(Feature feature) throws PolicyManagerDAOException, FeatureManagementException;*/
} }

@ -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<Profile> getAllProfiles() throws ProfileManagerDAOException;
List<Profile> getProfilesOfDeviceType(String deviceType) throws ProfileManagerDAOException;
}

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

@ -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<Feature> getAllFeatures() throws FeatureManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<Feature> featureList = new ArrayList<Feature>();
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<Feature> getFeaturesForProfile(int profileId) throws FeatureManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<Feature> featureList = new ArrayList<Feature>();
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);
}
}
}

@ -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<Profile> 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<Profile> 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<Feature> getAllFeatures() throws PolicyManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<Feature> featureList = new ArrayList<Feature>();
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<Feature> getFeaturesForProfile(int profileId) throws PolicyManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<Feature> featureList = new ArrayList<Feature>();
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) {
}
}
/** /**

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

@ -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.PolicyAdministratorPoint;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.common.Profile; 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.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.PolicyDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.impl.ProfileDAOImpl;
public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
private static final Log log = LogFactory.getLog(PolicyAdministratorPointImpl.class); private static final Log log = LogFactory.getLog(PolicyAdministratorPointImpl.class);
PolicyDAOImpl policyDAO; PolicyDAOImpl policyDAO;
FeatureDAOImpl featureDAO;
ProfileDAOImpl profileDAO;
public PolicyAdministratorPointImpl() { public PolicyAdministratorPointImpl() {
policyDAO = new PolicyDAOImpl(); policyDAO = new PolicyDAOImpl();
featureDAO = new FeatureDAOImpl();
profileDAO = new ProfileDAOImpl();
} }
@Override @Override
@ -128,8 +137,8 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
@Override @Override
public Profile addProfile(Profile profile) throws PolicyManagementException { public Profile addProfile(Profile profile) throws PolicyManagementException {
try { try {
profile = policyDAO.addProfile(profile); profile = profileDAO.addProfile(profile);
} catch (PolicyManagerDAOException e) { } catch (ProfileManagerDAOException e) {
String msg = "Error occurred while persisting the policy."; String msg = "Error occurred while persisting the policy.";
log.error(msg, e); log.error(msg, e);
throw new PolicyManagementException(msg, e); throw new PolicyManagementException(msg, e);
@ -146,8 +155,8 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
@Override @Override
public Profile updateProfile(Profile profile) throws PolicyManagementException { public Profile updateProfile(Profile profile) throws PolicyManagementException {
try { try {
profile = policyDAO.updateProfile(profile); profile = profileDAO.updateProfile(profile);
} catch (PolicyManagerDAOException e) { } catch (ProfileManagerDAOException e) {
String msg = "Error occurred while persisting the profile."; String msg = "Error occurred while persisting the profile.";
log.error(msg, e); log.error(msg, e);
throw new PolicyManagementException(msg, e); throw new PolicyManagementException(msg, e);
@ -159,12 +168,8 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
@Override @Override
public Feature addFeature(Feature feature) throws FeatureManagementException { public Feature addFeature(Feature feature) throws FeatureManagementException {
try { try {
feature = policyDAO.addFeature(feature); feature = featureDAO.addFeature(feature);
} catch (FeatureManagementException e) { } catch (FeatureManagerDAOException e) {
String msg = "Error occurred while persisting the feature.";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while persisting the feature."; String msg = "Error occurred while persisting the feature.";
log.error(msg, e); log.error(msg, e);
throw new FeatureManagementException(msg, e); throw new FeatureManagementException(msg, e);
@ -176,12 +181,8 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
@Override @Override
public Feature updateFeature(Feature feature) throws FeatureManagementException { public Feature updateFeature(Feature feature) throws FeatureManagementException {
try { try {
feature = policyDAO.updateFeature(feature); feature = featureDAO.updateFeature(feature);
} catch (PolicyManagerDAOException e) { } catch (FeatureManagerDAOException e) {
String msg = "Error occurred while persisting the feature.";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} catch (FeatureManagementException e) {
String msg = "Error occurred while persisting the feature."; String msg = "Error occurred while persisting the feature.";
log.error(msg, e); log.error(msg, e);
throw new FeatureManagementException(msg, e); throw new FeatureManagementException(msg, e);

@ -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.DBTypes;
import org.wso2.carbon.policy.mgt.core.common.TestDBConfiguration; 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.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.PolicyManagementDAOFactory;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException; 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.dao.impl.PolicyDAOImpl;
import org.wso2.carbon.policy.mgt.core.util.FeatureCreator; import org.wso2.carbon.policy.mgt.core.util.FeatureCreator;
import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil;
@ -144,9 +146,9 @@ public class PolicyDAOTestCase {
} }
@Test @Test
public void addFeatures() throws PolicyManagerDAOException, PolicyManagementException, FeatureManagementException { public void addFeatures() throws FeatureManagerDAOException {
PolicyDAOImpl policyDAO = new PolicyDAOImpl(); FeatureDAOImpl policyDAO = new FeatureDAOImpl();
List<Feature> featureList = FeatureCreator.getFeatureList(); List<Feature> featureList = FeatureCreator.getFeatureList();
for (Feature feature : featureList) { for (Feature feature : featureList) {
policyDAO.addFeature(feature); policyDAO.addFeature(feature);

@ -20,29 +20,29 @@ package org.wso2.carbon.simple.policy.decision.point;
public class PolicyEvaluationException extends Exception { public class PolicyEvaluationException extends Exception {
private String policyErrorMessage; private String policyEvaluationErrorMessage;
public String getPolicyErrorMessage() { public String getPolicyEvaluationErrorMessage() {
return policyErrorMessage; return policyEvaluationErrorMessage;
} }
public void setPolicyErrorMessage(String policyErrorMessage) { public void setPolicyEvaluationErrorMessage(String policyEvaluationErrorMessage) {
this.policyErrorMessage = policyErrorMessage; this.policyEvaluationErrorMessage = policyEvaluationErrorMessage;
} }
public PolicyEvaluationException(String message) { public PolicyEvaluationException(String message) {
super(message); super(message);
setPolicyErrorMessage(message); setPolicyEvaluationErrorMessage(message);
} }
public PolicyEvaluationException(String message, Exception ex) { public PolicyEvaluationException(String message, Exception ex) {
super(message, ex); super(message, ex);
setPolicyErrorMessage(message); setPolicyEvaluationErrorMessage(message);
} }
public PolicyEvaluationException(String message, Throwable cause) { public PolicyEvaluationException(String message, Throwable cause) {
super(message, cause); super(message, cause);
setPolicyErrorMessage(message); setPolicyEvaluationErrorMessage(message);
} }
public PolicyEvaluationException() { public PolicyEvaluationException() {

Loading…
Cancel
Save