Adding the final policy management classes, this includes the manager classes which manages the jta transactions from DAO, Test cases are added too

revert-70aa11f8
Geeth Munasinghe 10 years ago
parent 4f4240f176
commit cf4a2f8fc4

@ -18,7 +18,9 @@
package org.wso2.carbon.policy.mgt.common;
public class Feature {
import java.io.Serializable;
public class Feature implements Serializable {
private int id;
private String code;

@ -19,8 +19,8 @@
package org.wso2.carbon.policy.mgt.common;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.core.dto.Device;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import java.sql.Timestamp;

@ -22,7 +22,6 @@ package org.wso2.carbon.policy.mgt.common;
import org.wso2.carbon.device.mgt.core.dto.Device;
import java.sql.Date;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
@ -33,12 +32,14 @@ public class Policy implements Comparable<Policy> {
private int id; // Identifier of the policy.
private int priorityId; // Priority of the policies. This will be used only for simple evaluation.
private Profile profile; // Profile id
private Profile profile; // Profile
private int profileId;
private String policyName; // Name of the policy.
private boolean generic; // If true, this should be applied to all related device.
private List<String> roleList; // Roles which this policy should be applied.
private String ownershipType; // Ownership type (COPE, BYOD, CPE)
private List<Device> DeviceList; // Individual devices this policy should be applied
private List<String> users;
/*Dynamic policy attributes*/
@ -85,6 +86,14 @@ public class Policy implements Comparable<Policy> {
this.profile = profile;
}
public int getProfileId() {
return profileId;
}
public void setProfileId(int profileId) {
this.profileId = profileId;
}
public String getPolicyName() {
return policyName;
}
@ -125,6 +134,14 @@ public class Policy implements Comparable<Policy> {
DeviceList = deviceList;
}
public List<String> getUsers() {
return users;
}
public void setUsers(List<String> users) {
this.users = users;
}
public int getStartTime() {
return startTime;
}

@ -43,21 +43,21 @@ public interface PolicyAdministratorPoint {
/**
* This method adds a policy per device which should be implemented by the related plugins.
*
* @param deviceIdentifierr
* @param deviceIdentifierList
* @param policy
* @return primary key (generated key)
* @return
* @throws PolicyManagementException
*/
Policy addPolicyToDevice(DeviceIdentifier deviceIdentifierr, Policy policy) throws FeatureManagementException, PolicyManagementException;
Policy addPolicyToDevice(List<DeviceIdentifier> deviceIdentifierList, Policy policy) throws PolicyManagementException;
/**
* This method adds the policy to specific role.
*
* @param roleName
* @param roleNames
* @param policy
* @return primary key (generated key)
*/
Policy addPolicyToRole(String roleName, Policy policy) throws FeatureManagementException, PolicyManagementException;
Policy addPolicyToRole(List<String> roleNames, Policy policy) throws PolicyManagementException;
/**
* This method returns the policy of whole platform
@ -70,12 +70,11 @@ public interface PolicyAdministratorPoint {
/**
* This method gives the device specific policy.
*
* @param deviceId
* @param deviceType
* @param deviceIdentifier
* @return Policy
*/
List<Policy> getPoliciesOfDevice(String deviceId, String deviceType) throws FeatureManagementException, PolicyManagementException;
List<Policy> getPoliciesOfDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException;
/**
* This method returns the device type specific policy.
@ -84,7 +83,7 @@ public interface PolicyAdministratorPoint {
* @return Policy
*/
List<Policy> getPoliciesOfDeviceType(String deviceType) throws FeatureManagementException, PolicyManagementException;
List<Policy> getPoliciesOfDeviceType(String deviceType) throws PolicyManagementException;
/**
* This method returns the role specific policy.
@ -93,7 +92,10 @@ public interface PolicyAdministratorPoint {
* @return
*/
List<Policy> getPoliciesOfRole(String roleName) throws FeatureManagementException, PolicyManagementException;
List<Policy> getPoliciesOfRole(String roleName) throws PolicyManagementException;
List<Policy> getPoliciesOfUser(String username) throws PolicyManagementException;
/**
@ -138,6 +140,6 @@ public interface PolicyAdministratorPoint {
Feature updateFeature(Feature feature) throws FeatureManagementException;
void deleteFeature(int featureId) throws FeatureManagementException;
boolean deleteFeature(int featureId) throws FeatureManagementException;
}

@ -0,0 +1,43 @@
/*
* 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.common;
import java.sql.Date;
public class PolicyDates {
private Date startDate; // Start date to apply the policy
private Date endDate; // After this date policy will not be applied.
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
}

@ -0,0 +1,41 @@
/*
* 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.common;
public class PolicyLocations {
private String latitude; // Latitude
private String longitude; // Longitude
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
}

@ -0,0 +1,43 @@
/*
* 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.common;
import java.sql.Date;
public class PolicyTimes {
private int startTime; // Start time to apply the policy.
private int endTime; // After this time policy will not be applied
public int getStartTime() {
return startTime;
}
public void setStartTime(int startTime) {
this.startTime = startTime;
}
public int getEndTime() {
return endTime;
}
public void setEndTime(int endTime) {
this.endTime = endTime;
}
}

@ -18,7 +18,9 @@
package org.wso2.carbon.policy.mgt.common;
public class ProfileFeature {
import java.io.Serializable;
public class ProfileFeature implements Serializable {
private int id;
private Feature feature;

@ -0,0 +1,55 @@
/*
* Copyright (c) 2014, 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.common;
public class ProfileManagementException extends Exception {
private String profileErrorMessage;
public String getProfileErrorMessage() {
return profileErrorMessage;
}
public void setProfileErrorMessage(String profileErrorMessage) {
this.profileErrorMessage = profileErrorMessage;
}
public ProfileManagementException(String message) {
super(message);
setProfileErrorMessage(message);
}
public ProfileManagementException(String message, Exception ex) {
super(message, ex);
setProfileErrorMessage(message);
}
public ProfileManagementException(String message, Throwable cause) {
super(message, cause);
setProfileErrorMessage(message);
}
public ProfileManagementException() {
super();
}
public ProfileManagementException(Throwable cause) {
super(cause);
}
}

@ -25,7 +25,7 @@ import org.wso2.carbon.policy.mgt.common.*;
import java.util.List;
public interface PolicyManager {
public interface PolicyManagerService {
Feature addFeature(Feature feature) throws FeatureManagementException;

@ -20,7 +20,6 @@ package org.wso2.carbon.policy.mgt.core;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.policy.mgt.common.*;
import org.wso2.carbon.policy.mgt.core.impl.PolicyAdministratorPointImpl;
@ -28,13 +27,13 @@ import org.wso2.carbon.policy.mgt.core.impl.PolicyInformationPointImpl;
import java.util.List;
public class PolicyManagerImpl implements PolicyManager {
public class PolicyManagerServiceImpl implements PolicyManagerService {
private static final Log log = LogFactory.getLog(PolicyManagerImpl.class);
private static final Log log = LogFactory.getLog(PolicyManagerServiceImpl.class);
PolicyAdministratorPointImpl policyAdministratorPoint;
public PolicyManagerImpl() {
public PolicyManagerServiceImpl() {
policyAdministratorPoint = new PolicyAdministratorPointImpl();
}

@ -30,6 +30,8 @@ public interface FeatureDAO {
Feature addFeature(Feature feature) throws FeatureManagerDAOException;
List<Feature> addFeatures(List<Feature> feature) throws FeatureManagerDAOException;
Feature updateFeature(Feature feature) throws FeatureManagerDAOException;
ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException;
@ -42,6 +44,8 @@ public interface FeatureDAO {
List<Feature> getAllFeatures() throws FeatureManagerDAOException;
List<ProfileFeature> getAllProfileFeatures() throws FeatureManagerDAOException;
List<Feature> getAllFeatures(String deviceType) throws FeatureManagerDAOException;
List<ProfileFeature> getFeaturesForProfile(int ProfileId) throws FeatureManagerDAOException;

@ -19,11 +19,10 @@
package org.wso2.carbon.policy.mgt.core.dao;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.policy.mgt.common.Feature;
import org.wso2.carbon.policy.mgt.common.FeatureManagementException;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.Profile;
import org.wso2.carbon.device.mgt.core.dto.Device;
import org.wso2.carbon.policy.mgt.common.*;
import java.sql.Date;
import java.util.List;
public interface PolicyDAO {
@ -32,9 +31,17 @@ public interface PolicyDAO {
Policy addPolicy(String deviceType, Policy policy) throws PolicyManagerDAOException;
Policy addPolicyToRole(String roleName, Policy policy) throws PolicyManagerDAOException;
Policy addPolicyToRole(List<String> roleNames, Policy policy) throws PolicyManagerDAOException;
Policy addPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagerDAOException;
Policy addPolicyToUser(List<String> usernameList, Policy policy) throws PolicyManagerDAOException;
Policy addPolicyToDevice(List<Device> devices, Policy policy) throws PolicyManagerDAOException;
Policy addDatesToPolicy(Date startDate, Date endDate, Policy policy) throws PolicyManagerDAOException;
Policy addTimesToPolicy(int startTime, int endTime, Policy policy) throws PolicyManagerDAOException;
Policy addLocationToPolicy(String latitude, String longitude, Policy policy) throws PolicyManagerDAOException;
Policy updatePolicy(Policy policy) throws PolicyManagerDAOException;
@ -42,14 +49,28 @@ public interface PolicyDAO {
Policy getPolicyByProfileID(int profileId) throws PolicyManagerDAOException;
List<Policy> getPolicy() throws PolicyManagerDAOException;
List<Integer> getPolicyAppliedDevicesIds(int policyId) throws PolicyManagerDAOException;
List<Policy> getAllPolicies() throws PolicyManagerDAOException;
List<Policy> getPolicyOfDeviceType(String deviceType) throws PolicyManagerDAOException;
List<Integer> getPolicyIdsOfDevice(Device device) throws PolicyManagerDAOException;
List<Integer> getPolicyOfRole(String roleName) throws PolicyManagerDAOException;
List<Integer> getPolicyOfUser(String username) throws PolicyManagerDAOException;
boolean deletePolicy(Policy policy) throws PolicyManagerDAOException;
boolean deleteAllPolicyRelatedConfigs(int policyId) throws PolicyManagerDAOException;
List<Policy> getPolicy(String deviceType) throws PolicyManagerDAOException;
List<String> getPolicyAppliedRoles(int policyId) throws PolicyManagerDAOException;
List<Policy> getPolicy(DeviceIdentifier deviceIdentifier) throws PolicyManagerDAOException;
PolicyTimes getTimesOfPolicy(Policy policy) throws PolicyManagerDAOException;
List<Policy> getPolicyOfRole(String roleName) throws PolicyManagerDAOException;
PolicyDates getDatesOfPolicy(Policy policy) throws PolicyManagerDAOException;
void deletePolicy(Policy policy) throws PolicyManagerDAOException;
PolicyLocations getLocationsOfPolicy(Policy policy) throws PolicyManagerDAOException;
}

@ -20,12 +20,17 @@ package org.wso2.carbon.policy.mgt.core.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig;
import org.wso2.carbon.policy.mgt.core.config.datasource.JNDILookupDefinition;
import org.wso2.carbon.policy.mgt.core.dao.impl.FeatureDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.impl.PolicyDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.impl.ProfileDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.List;
@ -33,8 +38,7 @@ public class PolicyManagementDAOFactory {
private static DataSource dataSource;
private static final Log log = LogFactory.getLog(PolicyManagementDAOFactory.class);
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
public static void init(DataSourceConfig config) {
dataSource = resolveDataSource(config);
@ -51,6 +55,18 @@ public class PolicyManagementDAOFactory {
throw new RuntimeException("Data source is not yet configured.");
}
public static PolicyDAO getPolicyDAO() {
return new PolicyDAOImpl();
}
public static ProfileDAO getProfileDAO() {
return new ProfileDAOImpl();
}
public static FeatureDAO getFeatureDAO() {
return new FeatureDAOImpl();
}
/**
* Resolve data source from the data source definition
*
@ -85,4 +101,76 @@ public class PolicyManagementDAOFactory {
return dataSource;
}
public static void beginTransaction() throws PolicyManagerDAOException {
try {
Connection conn = dataSource.getConnection();
conn.setAutoCommit(false);
currentConnection.set(conn);
} catch (SQLException e) {
throw new PolicyManagerDAOException("Error occurred while retrieving datasource connection", e);
}
}
public static Connection getConnection() throws PolicyManagerDAOException {
if (currentConnection.get() == null) {
try {
Connection conn = dataSource.getConnection();
conn.setAutoCommit(false);
currentConnection.set(conn);
} catch (SQLException e) {
throw new PolicyManagerDAOException("Error occurred while retrieving data source connection",
e);
}
}
return currentConnection.get();
}
public static void closeConnection() throws PolicyManagerDAOException {
Connection con = currentConnection.get();
try {
con.close();
} catch (SQLException e) {
log.error("Error occurred while close the connection");
}
currentConnection.remove();
}
public static void commitTransaction() throws PolicyManagerDAOException {
try {
Connection conn = currentConnection.get();
if (conn != null) {
conn.commit();
} else {
if (log.isDebugEnabled()) {
log.debug("Datasource connection associated with the current thread is null, hence commit " +
"has not been attempted");
}
}
} catch (SQLException e) {
throw new PolicyManagerDAOException("Error occurred while committing the transaction", e);
} finally {
closeConnection();
}
}
public static void rollbackTransaction() throws PolicyManagerDAOException {
try {
Connection conn = currentConnection.get();
if (conn != null) {
conn.rollback();
} else {
if (log.isDebugEnabled()) {
log.debug("Datasource connection associated with the current thread is null, hence rollback " +
"has not been attempted");
}
}
} catch (SQLException e) {
throw new PolicyManagerDAOException("Error occurred while rollbacking the transaction", e);
} finally {
closeConnection();
}
}
}

@ -19,6 +19,7 @@
package org.wso2.carbon.policy.mgt.core.dao;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.policy.mgt.common.Profile;
import java.util.List;
@ -29,12 +30,12 @@ public interface ProfileDAO {
Profile updateProfile(Profile profile) throws ProfileManagerDAOException;
void deleteProfile(Profile profile) throws ProfileManagerDAOException;
boolean deleteProfile(Profile profile) throws ProfileManagerDAOException;
Profile getProfiles(int profileId) throws ProfileManagerDAOException;
List<Profile> getAllProfiles() throws ProfileManagerDAOException;
List<Profile> getProfilesOfDeviceType(String deviceType) throws ProfileManagerDAOException;
List<Profile> getProfilesOfDeviceType(DeviceType deviceType) throws ProfileManagerDAOException;
}

@ -21,10 +21,12 @@ 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.common.ProfileFeature;
import org.wso2.carbon.policy.mgt.core.dao.*;
import org.wso2.carbon.policy.mgt.core.dao.FeatureDAO;
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.util.PolicyManagementDAOUtil;
import java.sql.Connection;
@ -41,44 +43,94 @@ public class FeatureDAOImpl implements FeatureDAO {
@Override
public Feature addFeature(Feature feature) throws FeatureManagerDAOException {
Connection conn = null;
Connection conn;
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);
String query = "INSERT INTO DM_FEATURES (NAME, CODE, DESCRIPTION, EVALUVATION_RULE, DEVICE_TYPE_ID) VALUES (?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
stmt.setString(1, feature.getName());
stmt.setString(2, feature.getCode());
stmt.setString(3, feature.getDescription());
stmt.setString(4, feature.getRuleValue());
stmt.setInt(5, feature.getDeviceTypeId());
int affectedRows = stmt.executeUpdate();
if (log.isDebugEnabled()) {
log.debug(affectedRows + " Features are added.");
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(conn, stmt, generatedKeys);
PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys);
}
return feature;
}
@Override
public List<Feature> addFeatures(List<Feature> features) throws FeatureManagerDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
List<Feature> featureList = new ArrayList<Feature>();
try {
conn = this.getConnection();
String query = "INSERT INTO DM_FEATURES (NAME, CODE, DESCRIPTION, EVALUVATION_RULE, DEVICE_TYPE_ID) 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.setString(4, feature.getRuleValue());
stmt.setInt(5, feature.getDeviceTypeId());
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 = null;
Connection conn;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
try {
conn = this.getConnection();
String query = "UPDATE DM_FEATURES SET NAME = ?, CODE = ?, DESCRIPTION = ?, EVALUVATION_RULE = ? WHERE ID = ?";
@ -88,14 +140,14 @@ public class FeatureDAOImpl implements FeatureDAO {
stmt.setString(3, feature.getDescription());
stmt.setString(4, feature.getRuleValue());
stmt.setInt(5, 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(conn, stmt, generatedKeys);
PolicyManagementDAOUtil.cleanupResources(stmt, null);
}
return feature;
@ -113,17 +165,19 @@ public class FeatureDAOImpl implements FeatureDAO {
@Override
public List<ProfileFeature> addProfileFeatures(List<ProfileFeature> features, int profileId) throws FeatureManagerDAOException {
Connection conn = null;
Connection conn;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
try {
conn = this.getConnection();
String query = "INSERT INTO DM_PROFILE_FEATURES (PROFILE_ID, FEATURE_ID, CONTENT) VALUES (?, ?, ?)";
stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
stmt = conn.prepareStatement(query, stmt.RETURN_GENERATED_KEYS);
for (ProfileFeature feature : features) {
stmt.setInt(1, profileId);
stmt.setInt(2, feature.getId());
stmt.setInt(2, feature.getFeature().getId());
stmt.setObject(3, feature.getContent());
stmt.addBatch();
//Not adding the logic to check the size of the stmt and execute if the size records added is over 1000
@ -132,33 +186,37 @@ public class FeatureDAOImpl implements FeatureDAO {
generatedKeys = stmt.getGeneratedKeys();
int i = 0;
while (generatedKeys.next()) {
features.get(i).setId(generatedKeys.getInt(i));
features.get(i).setId(generatedKeys.getInt(1));
i++;
}
} catch (SQLException e) {
String msg = "Error occurred while adding the feature list to the database.";
log.error(msg, e);
throw new FeatureManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys);
PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys);
}
return features;
}
@Override
public List<ProfileFeature> updateProfileFeatures(List<ProfileFeature> features, int profileId) throws FeatureManagerDAOException {
Connection conn = null;
Connection conn;
PreparedStatement stmt = null;
try {
conn = this.getConnection();
String query = "UPDATE DM_PROFILE_FEATURES SET CONTENT = ? WHERE PROFILE_ID = ? , FEATURE_ID = ? ";
stmt = conn.prepareStatement(query, stmt.RETURN_GENERATED_KEYS);
stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
for (ProfileFeature feature : features) {
stmt.setObject(1, feature.getContent());
stmt.setInt(2, profileId);
stmt.setInt(3, feature.getId());
stmt.setInt(3, feature.getFeature().getId());
stmt.addBatch();
//Not adding the logic to check the size of the stmt and execute if the size records added is over 1000
}
@ -169,14 +227,15 @@ public class FeatureDAOImpl implements FeatureDAO {
log.error(msg, e);
throw new FeatureManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, null);
PolicyManagementDAOUtil.cleanupResources(stmt, null);
}
return features;
}
@Override
public boolean deleteFeaturesOfProfile(Profile profile) throws FeatureManagerDAOException {
Connection conn = null;
Connection conn;
PreparedStatement stmt = null;
try {
@ -186,22 +245,23 @@ public class FeatureDAOImpl implements FeatureDAO {
stmt.setInt(1, profile.getProfileId());
stmt.executeUpdate();
return true;
} 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);
PolicyManagementDAOUtil.cleanupResources(stmt, null);
}
}
@Override
public List<Feature> getAllFeatures() throws FeatureManagerDAOException {
Connection conn = null;
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<Feature> featureList = new ArrayList<Feature>();
try {
@ -211,6 +271,7 @@ public class FeatureDAOImpl implements FeatureDAO {
resultSet = stmt.executeQuery();
while (resultSet.next()) {
Feature feature = new Feature();
feature.setId(resultSet.getInt("ID"));
feature.setCode(resultSet.getString("CODE"));
@ -225,7 +286,50 @@ public class FeatureDAOImpl implements FeatureDAO {
log.error(msg);
throw new FeatureManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet);
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
}
return featureList;
}
@Override
public List<ProfileFeature> getAllProfileFeatures() throws FeatureManagerDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<ProfileFeature> featureList = new ArrayList<ProfileFeature>();
try {
conn = this.getConnection();
String query = "SELECT PF.ID AS ID, 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";
stmt = conn.prepareStatement(query);
resultSet = stmt.executeQuery();
while (resultSet.next()) {
ProfileFeature profileFeature = new ProfileFeature();
Feature feature = new Feature();
feature.setId(resultSet.getInt("FEATURE_ID"));
feature.setCode(resultSet.getString("CODE"));
feature.setName(resultSet.getString("NAME"));
feature.setRuleValue(resultSet.getString("RULE"));
profileFeature.setFeature(feature);
profileFeature.setId(resultSet.getInt("ID"));
profileFeature.setContent(resultSet.getObject("CONTENT"));
featureList.add(profileFeature);
}
} 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(stmt, resultSet);
this.closeConnection();
}
return featureList;
}
@ -233,10 +337,11 @@ public class FeatureDAOImpl implements FeatureDAO {
@Override
public List<Feature> getAllFeatures(String deviceType) throws FeatureManagerDAOException {
Connection conn = null;
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<Feature> featureList = new ArrayList<Feature>();
try {
conn = this.getConnection();
String query = "SELECT f.ID ID, f.NAME NAME, f.CODE CODE, f.DEVICE_TYPE_ID DEVICE_TYPE_ID," +
@ -247,6 +352,7 @@ public class FeatureDAOImpl implements FeatureDAO {
resultSet = stmt.executeQuery();
while (resultSet.next()) {
Feature feature = new Feature();
feature.setId(resultSet.getInt("ID"));
feature.setCode(resultSet.getString("CODE"));
@ -255,23 +361,24 @@ public class FeatureDAOImpl implements FeatureDAO {
feature.setRuleValue(resultSet.getString("EVALUVATION_RULE"));
featureList.add(feature);
}
return featureList;
} catch (SQLException e) {
String msg = "Unable to get the list of the features related device type from database.";
log.error(msg);
throw new FeatureManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet);
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
}
return featureList;
}
@Override
public List<ProfileFeature> getFeaturesForProfile(int profileId) throws FeatureManagerDAOException {
Connection conn = null;
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<ProfileFeature> featureList = new ArrayList<ProfileFeature>();
try {
@ -289,7 +396,6 @@ public class FeatureDAOImpl implements FeatureDAO {
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"));
profileFeature.setFeature(feature);
@ -303,7 +409,8 @@ public class FeatureDAOImpl implements FeatureDAO {
log.error(msg);
throw new FeatureManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet);
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
}
return featureList;
}
@ -311,7 +418,7 @@ public class FeatureDAOImpl implements FeatureDAO {
@Override
public boolean deleteFeature(int featureId) throws FeatureManagerDAOException {
Connection conn = null;
Connection conn;
PreparedStatement stmt = null;
try {
@ -321,21 +428,32 @@ public class FeatureDAOImpl implements FeatureDAO {
stmt.setInt(1, featureId);
stmt.executeUpdate();
return true;
} catch (SQLException e) {
String msg = "Unable to delete the feature " + featureId + " (Feature ID) from database.";
log.error(msg);
throw new FeatureManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, null);
PolicyManagementDAOUtil.cleanupResources(stmt, null);
}
}
private Connection getConnection() throws FeatureManagerDAOException {
try {
return PolicyManagementDAOFactory.getDataSource().getConnection();
} catch (SQLException e) {
return PolicyManagementDAOFactory.getConnection();
} catch (PolicyManagerDAOException e) {
throw new FeatureManagerDAOException("Error occurred while obtaining a connection from the policy " +
"management metadata repository datasource", e);
}
}
private void closeConnection() {
try {
PolicyManagementDAOFactory.closeConnection();
} catch (PolicyManagerDAOException e) {
log.warn("Unable to close the database connection.");
}
}
}

@ -20,14 +20,12 @@ 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.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dao.impl.DeviceTypeDAOImpl;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.policy.mgt.common.Feature;
import org.wso2.carbon.policy.mgt.common.Profile;
import org.wso2.carbon.policy.mgt.common.ProfileFeature;
import org.wso2.carbon.policy.mgt.core.dao.*;
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.ProfileDAO;
import org.wso2.carbon.policy.mgt.core.dao.ProfileManagerDAOException;
import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
@ -41,20 +39,19 @@ import java.util.List;
public class ProfileDAOImpl implements ProfileDAO {
private static final Log log = LogFactory.getLog(ProfileDAOImpl.class);
FeatureDAOImpl featureDAO = new FeatureDAOImpl();
DeviceTypeDAO deviceTypeDAO = new DeviceTypeDAOImpl(PolicyManagementDAOFactory.getDataSource());
public Profile addProfile(Profile profile) throws ProfileManagerDAOException {
Connection conn = null;
Connection conn;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
int tenantId = MultitenantConstants.SUPER_TENANT_ID;
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);
String query = "INSERT INTO DM_PROFILE " +
"(PROFILE_NAME,TENANT_ID, DEVICE_TYPE_ID, CREATED_TIME, UPDATED_TIME) VALUES (?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
stmt.setString(1, profile.getProfileName());
stmt.setInt(2, tenantId);
@ -78,38 +75,29 @@ public class ProfileDAOImpl implements ProfileDAO {
throw new RuntimeException("Profile id is 0, this could be an issue.");
}
featureDAO.addProfileFeatures(profile.getProfileFeaturesList(), profile.getProfileId());
// persistFeatures(profile);
} catch (SQLException e) {
String msg = "Error occurred while adding the profile to database.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} catch (FeatureManagerDAOException e) {
String msg = "Error occurred while adding the features to database.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys);
PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys);
}
return profile;
}
public Profile updateProfile(Profile profile) throws ProfileManagerDAOException {
Connection conn = null;
Connection conn;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
int tenantId = MultitenantConstants.SUPER_TENANT_ID;
try {
conn = this.getConnection();
String query = "UPDATE DM_PROFILE SET PROFILE_NAME = ? ,TENANT_ID = ?, DEVICE_TYPE_ID = ? , UPDATED_TIME = ? " +
"WHERE ID = ?";
stmt = conn.prepareStatement(query, stmt.RETURN_GENERATED_KEYS);
stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
stmt.setString(1, profile.getProfileName());
stmt.setInt(2, tenantId);
stmt.setLong(3, profile.getDeviceType().getId());
@ -132,39 +120,20 @@ public class ProfileDAOImpl implements ProfileDAO {
throw new RuntimeException("Profile id is 0, this could be an issue.");
}
// TODO : Check to update the features.
featureDAO.updateProfileFeatures(profile.getProfileFeaturesList(), profile.getProfileId());
//persistFeatures(profile);
} catch (SQLException e) {
String msg = "Error occurred while updating the profile (" + profile.getProfileName() + ") in database.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} catch (FeatureManagerDAOException e) {
String msg = "Error occurred while updating the profile in database.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys);
PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys);
}
return profile;
}
@Override
public void deleteProfile(Profile profile) throws ProfileManagerDAOException {
// First delete the features related to the profile
try {
featureDAO.deleteFeaturesOfProfile(profile);
} catch (FeatureManagerDAOException e) {
String msg = "Error occurred while deleting features.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
}
public boolean deleteProfile(Profile profile) throws ProfileManagerDAOException {
Connection conn = null;
Connection conn;
PreparedStatement stmt = null;
try {
@ -173,156 +142,116 @@ public class ProfileDAOImpl implements ProfileDAO {
stmt = conn.prepareStatement(query);
stmt.setInt(1, profile.getProfileId());
stmt.executeUpdate();
return true;
} 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);
PolicyManagementDAOUtil.cleanupResources(stmt, null);
}
}
@Override
public Profile getProfiles(int profileId) throws ProfileManagerDAOException {
Connection conn = null;
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
Profile profile = new Profile();
DeviceType deviceType = new DeviceType();
try {
List<ProfileFeature> featureList = featureDAO.getFeaturesForProfile(profileId);
conn = this.getConnection();
String query = "SELECT * FROM DM_PROFILE WHERE ID = ?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, profileId);
resultSet = stmt.executeQuery();
//ID PROFILE_NAME TENANT_ID DEVICE_TYPE_ID CREATED_TIME UPDATED_TIME
while (resultSet.next()) {
profile.setProfileFeaturesList(featureList);
deviceType.setId(resultSet.getInt("DEVICE_TYPE_ID"));
profile.setProfileId(profileId);
profile.setProfileName(resultSet.getString("PROFILE_NAME"));
profile.setTenantId(resultSet.getInt("TENANT_ID"));
profile.setDeviceType(deviceTypeDAO.getDeviceType(resultSet.getInt("DEVICE_TYPE_ID")));
profile.setDeviceType(deviceType);
profile.setCreatedDate(resultSet.getTimestamp("CREATED_TIME"));
profile.setUpdatedDate(resultSet.getTimestamp("UPDATED_TIME"));
}
return profile;
} catch (SQLException e) {
String msg = "Error occurred while reading the profile from the database.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred when getting the device type name by device type id.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} catch (FeatureManagerDAOException e) {
String msg = "Error occurred when getting the features related to a profile.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet);
}
}
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 (ProfileFeature feature : profile.getProfileFeaturesList()) {
stmt.setInt(1, profile.getProfileId());
stmt.setInt(2, feature.getId());
stmt.setObject(3, feature.getContent());
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.";
String msg = "Error occurred while reading the profile from the database.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, null);
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
}
return profile;
}
@Override
public List<Profile> getAllProfiles() throws ProfileManagerDAOException {
Connection conn = null;
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<Profile> profileList = new ArrayList<Profile>();
try {
conn = this.getConnection();
String query = "SELECT * FROM DM_PROFILE";
stmt = conn.prepareStatement(query);
resultSet = stmt.executeQuery();
//ID PROFILE_NAME TENANT_ID DEVICE_TYPE_ID CREATED_TIME UPDATED_TIME
while (resultSet.next()) {
Profile profile = new Profile();
int profileId = resultSet.getInt("ID");
List<ProfileFeature> featureList = featureDAO.getFeaturesForProfile(profileId);
profile.setProfileFeaturesList(featureList);
profile.setProfileId(profileId);
profile.setProfileId(resultSet.getInt("ID"));
profile.setProfileName(resultSet.getString("PROFILE_NAME"));
profile.setTenantId(resultSet.getInt("TENANT_ID"));
profile.setDeviceType(deviceTypeDAO.getDeviceType(resultSet.getInt("DEVICE_TYPE_ID")));
profile.setCreatedDate(resultSet.getTimestamp("CREATED_TIME"));
profile.setUpdatedDate(resultSet.getTimestamp("UPDATED_TIME"));
DeviceType deviceType = new DeviceType();
deviceType.setId(resultSet.getInt("DEVICE_TYPE_ID"));
profile.setDeviceType(deviceType);
profileList.add(profile);
}
return profileList;
} catch (SQLException e) {
String msg = "Error occurred while reading the profile list from the database.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting the device type.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} catch (FeatureManagerDAOException e) {
String msg = "Error occurred while getting the features related to a profile.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
}
return profileList;
}
@Override
public List<Profile> getProfilesOfDeviceType(String deviceTypeName) throws ProfileManagerDAOException {
public List<Profile> getProfilesOfDeviceType(DeviceType deviceType) throws ProfileManagerDAOException {
Connection conn = null;
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<Profile> profileList = new ArrayList<Profile>();
try {
DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName);
conn = this.getConnection();
String query = "SELECT * FROM DM_PROFILE WHERE DEVICE_TYPE_ID = ?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, deviceType.getId());
resultSet = stmt.executeQuery();
//ID PROFILE_NAME TENANT_ID DEVICE_TYPE_ID CREATED_TIME UPDATED_TIME
while (resultSet.next()) {
Profile profile = new Profile();
int profileId = resultSet.getInt("ID");
List<ProfileFeature> featureList = featureDAO.getFeaturesForProfile(profileId);
profile.setProfileFeaturesList(featureList);
profile.setProfileId(profileId);
profile.setProfileId(resultSet.getInt("ID"));
profile.setProfileName(resultSet.getString("PROFILE_NAME"));
profile.setTenantId(resultSet.getInt("TENANT_ID"));
profile.setDeviceType(deviceType);
@ -331,31 +260,35 @@ public class ProfileDAOImpl implements ProfileDAO {
profileList.add(profile);
}
return profileList;
} catch (SQLException e) {
String msg = "Error occurred while reading the profile list from the database.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting the device type.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} catch (FeatureManagerDAOException e) {
String msg = "Error occurred while getting the features related to a profile.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
}
return profileList;
}
private Connection getConnection() throws ProfileManagerDAOException {
try {
return PolicyManagementDAOFactory.getDataSource().getConnection();
} catch (SQLException e) {
return PolicyManagementDAOFactory.getConnection();
} catch (PolicyManagerDAOException e) {
throw new ProfileManagerDAOException("Error occurred while obtaining a connection from the policy " +
"management metadata repository datasource", e);
}
}
private void closeConnection() {
try {
PolicyManagementDAOFactory.closeConnection();
} catch (PolicyManagerDAOException e) {
log.warn("Unable to close the database connection.");
}
}
}

@ -69,4 +69,8 @@ public class PolicyManagementDAOUtil {
}
}
public static void cleanupResources(PreparedStatement stmt, ResultSet rs) {
cleanupResources(null, stmt, rs);
}
}

@ -21,18 +21,13 @@ package org.wso2.carbon.policy.mgt.core.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.policy.mgt.common.Feature;
import org.wso2.carbon.policy.mgt.common.FeatureManagementException;
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;
import org.wso2.carbon.policy.mgt.common.*;
import org.wso2.carbon.policy.mgt.core.mgt.FeatureManager;
import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager;
import org.wso2.carbon.policy.mgt.core.mgt.ProfileManager;
import org.wso2.carbon.policy.mgt.core.mgt.impl.FeatureManagerImpl;
import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl;
import org.wso2.carbon.policy.mgt.core.mgt.impl.ProfileManagerImpl;
import java.util.List;
@ -40,104 +35,61 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
private static final Log log = LogFactory.getLog(PolicyAdministratorPointImpl.class);
PolicyDAOImpl policyDAO;
FeatureDAOImpl featureDAO;
ProfileDAOImpl profileDAO;
private PolicyManager policyManager;
private ProfileManager profileManager;
private FeatureManager featureManager;
public PolicyAdministratorPointImpl() {
policyDAO = new PolicyDAOImpl();
featureDAO = new FeatureDAOImpl();
profileDAO = new ProfileDAOImpl();
policyManager = new PolicyManagerImpl();
profileManager = new ProfileManagerImpl();
featureManager = new FeatureManagerImpl();
}
@Override
public Policy addPolicy(Policy policy) throws PolicyManagementException {
try {
policy = policyDAO.addPolicy(policy);
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while persisting the policy.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return policy;
return policyManager.addPolicy(policy);
}
@Override
public Policy updatePolicy(Policy policy) throws PolicyManagementException {
try {
policy = policyDAO.updatePolicy(policy);
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while updating the policy.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return policy;
return policyManager.updatePolicy(policy);
}
@Override
public Policy addPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy)
throws FeatureManagementException, PolicyManagementException {
try {
policy = policyDAO.addPolicyToDevice(deviceIdentifier, policy);
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while persisting the policy.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return policy;
public Policy addPolicyToDevice(List<DeviceIdentifier> deviceIdentifierList, Policy policy) throws PolicyManagementException {
return policyManager.addPolicyToDevice(deviceIdentifierList, policy);
}
@Override
public Policy addPolicyToRole(String roleName, Policy policy)
throws FeatureManagementException, PolicyManagementException {
try {
policy = policyDAO.addPolicyToRole(roleName, policy);
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while persisting the policy.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return policy;
public Policy addPolicyToRole(List<String> roleNames, Policy policy) throws PolicyManagementException {
return policyManager.addPolicyToRole(roleNames, policy);
}
@Override
public List<Policy> getPolicies() throws PolicyManagementException {
try {
return policyDAO.getPolicy();
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting the policies.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return policyManager.getPolicies();
}
@Override
public List<Policy> getPoliciesOfDevice(String deviceId, String deviceType)
throws FeatureManagementException, PolicyManagementException {
return null;
public List<Policy> getPoliciesOfDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
return policyManager.getPoliciesOfDevice(deviceIdentifier);
}
@Override
public List<Policy> getPoliciesOfDeviceType(String deviceType)
throws FeatureManagementException, PolicyManagementException {
try {
return policyDAO.getPolicy(deviceType);
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting the policy related to device type.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
public List<Policy> getPoliciesOfDeviceType(String deviceType) throws PolicyManagementException {
return policyManager.getPoliciesOfDeviceType(deviceType);
}
@Override
public List<Policy> getPoliciesOfRole(String roleName) throws FeatureManagementException, PolicyManagementException {
try {
return policyDAO.getPolicyOfRole(roleName);
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting the policy related to role name.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
public List<Policy> getPoliciesOfRole(String roleName) throws PolicyManagementException {
return policyManager.getPoliciesOfRole(roleName);
}
@Override
public List<Policy> getPoliciesOfUser(String username) throws PolicyManagementException {
return policyManager.getPoliciesOfUser(username);
}
@Override
@ -158,14 +110,12 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
@Override
public Profile addProfile(Profile profile) throws PolicyManagementException {
try {
profile = profileDAO.addProfile(profile);
} catch (ProfileManagerDAOException e) {
return profileManager.addProfile(profile);
} catch (ProfileManagementException e) {
String msg = "Error occurred while persisting the policy.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return profile;
}
@Override
@ -176,46 +126,41 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
@Override
public Profile updateProfile(Profile profile) throws PolicyManagementException {
try {
profile = profileDAO.updateProfile(profile);
} catch (ProfileManagerDAOException e) {
return profileManager.updateProfile(profile);
} catch (ProfileManagementException e) {
String msg = "Error occurred while persisting the profile.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return profile;
}
@Override
public Feature addFeature(Feature feature) throws FeatureManagementException {
try {
feature = featureDAO.addFeature(feature);
} catch (FeatureManagerDAOException e) {
return featureManager.addFeature(feature);
} catch (FeatureManagementException e) {
String msg = "Error occurred while persisting the feature.";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
}
return feature;
}
@Override
public Feature updateFeature(Feature feature) throws FeatureManagementException {
try {
feature = featureDAO.updateFeature(feature);
} catch (FeatureManagerDAOException e) {
return featureManager.updateFeature(feature);
} catch (FeatureManagementException e) {
String msg = "Error occurred while persisting the feature.";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
}
return feature;
}
@Override
public void deleteFeature(int featureId) throws FeatureManagementException {
public boolean deleteFeature(int featureId) throws FeatureManagementException {
try {
featureDAO.deleteFeature(featureId);
} catch (FeatureManagerDAOException e) {
return featureManager.deleteFeature(featureId);
} catch (FeatureManagementException e) {
String msg = "Error occurred while deleting the feature.";
log.error(msg, e);
throw new FeatureManagementException(msg, e);

@ -21,18 +21,17 @@ package org.wso2.carbon.policy.mgt.core.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dao.impl.DeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.impl.DeviceTypeDAOImpl;
import org.wso2.carbon.device.mgt.core.dto.Device;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementService;
import org.wso2.carbon.policy.mgt.common.*;
import org.wso2.carbon.policy.mgt.core.dao.*;
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.internal.PolicyManagementDataHolder;
import org.wso2.carbon.policy.mgt.core.mgt.FeatureManager;
import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager;
import org.wso2.carbon.policy.mgt.core.mgt.impl.FeatureManagerImpl;
import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl;
import org.wso2.carbon.user.api.UserStoreException;
import java.util.ArrayList;
@ -43,22 +42,30 @@ import java.util.Map;
public class PolicyInformationPointImpl implements PolicyInformationPoint {
private static final Log log = LogFactory.getLog(PolicyInformationPointImpl.class);
DeviceDAOImpl deviceDAO = new DeviceDAOImpl(PolicyManagementDAOFactory.getDataSource());
DeviceTypeDAO deviceTypeDAO = new DeviceTypeDAOImpl(PolicyManagementDAOFactory.getDataSource());
FeatureDAO featureDAO = new FeatureDAOImpl();
PolicyDAO policyDAO = new PolicyDAOImpl();
PolicyManager policyManager;
FeatureManager featureManager;
DeviceManagementService deviceManagementService;
public PolicyInformationPointImpl() {
deviceDAO = new DeviceDAOImpl(DeviceManagementDAOFactory.getDataSource());
deviceManagementService =
PolicyManagementDataHolder.getInstance().getDeviceManagementService();
policyManager = new PolicyManagerImpl();
featureManager = new FeatureManagerImpl();
}
@Override
public PIPDevice getDeviceData(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
PIPDevice pipDevice = new PIPDevice();
Device device;
org.wso2.carbon.device.mgt.common.Device device;
// TODO : Find
DeviceType deviceType = new DeviceType();
deviceType.setName(deviceIdentifier.getType());
try {
device = deviceDAO.getDevice(deviceIdentifier);
DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceIdentifier.getType());
device = deviceManagementService.getDevice(deviceIdentifier);
/*deviceManagementService.getDeviceType(deviceIdentifier.getType());*/
pipDevice.setDevice(device);
pipDevice.setRoles(getRoleOfDevice(device));
pipDevice.setDeviceType(deviceType);
@ -68,7 +75,7 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint {
// pipDevice.setLongitude();
// pipDevice.setAltitude();
// pipDevice.setTimestamp();
} catch (DeviceManagementDAOException e) {
} catch (DeviceManagementException e) {
String msg = "Error occurred when retrieving the data related to device from the database.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
@ -83,17 +90,17 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint {
List<List<Policy>> policies = new ArrayList<List<Policy>>();
try {
// Get the device type related policies
policies.add(policyDAO.getPolicy(pipDevice.getDeviceType().getName()));
policies.add(policyManager.getPoliciesOfDeviceType(pipDevice.getDeviceType().getName()));
// Get the roles related policies
for (String role : pipDevice.getRoles()) {
policies.add(policyDAO.getPolicyOfRole(role));
policies.add(policyManager.getPoliciesOfRole(role));
}
// Get policy related to the device
policies.add(policyDAO.getPolicy(pipDevice.getDeviceIdentifier()));
policies.add(policyManager.getPoliciesOfDevice(pipDevice.getDeviceIdentifier()));
return removeDuplicatePolicies(policies);
} catch (PolicyManagerDAOException e) {
} catch (PolicyManagementException e) {
String msg = "Error occurred when retrieving related to given device " +
pipDevice.getDeviceIdentifier().getId() + " " + pipDevice.getDeviceIdentifier().getType() + ".";
log.error(msg, e);
@ -104,8 +111,8 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint {
@Override
public List<Feature> getRelatedFeatures(String deviceType) throws FeatureManagementException {
try {
return featureDAO.getAllFeatures(deviceType);
} catch (FeatureManagerDAOException e) {
return featureManager.getAllFeatures(deviceType);
} catch (FeatureManagementException e) {
String msg = "Error occurred when retrieving features related to device type.";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
@ -115,7 +122,7 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint {
private String[] getRoleOfDevice(Device device) throws PolicyManagementException {
try {
return CarbonContext.getThreadLocalCarbonContext().getUserRealm().
getUserStoreManager().getRoleListOfUser(device.getOwnerId());
getUserStoreManager().getRoleListOfUser(device.getOwner());
} catch (UserStoreException e) {
String msg = "Error occurred when retrieving roles related to user name.";
log.error(msg, e);

@ -18,6 +18,7 @@
package org.wso2.carbon.policy.mgt.core.internal;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementService;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint;
import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint;
import org.wso2.carbon.user.core.service.RealmService;
@ -29,6 +30,7 @@ public class PolicyManagementDataHolder {
private TenantManager tenantManager;
private PolicyEvaluationPoint policyEvaluationPoint;
private PolicyInformationPoint policyInformationPoint;
private DeviceManagementService deviceManagementService;
private static PolicyManagementDataHolder thisInstance = new PolicyManagementDataHolder();
private PolicyManagementDataHolder() {}
@ -72,4 +74,12 @@ public class PolicyManagementDataHolder {
public void setPolicyInformationPoint(PolicyInformationPoint policyInformationPoint) {
this.policyInformationPoint = policyInformationPoint;
}
public DeviceManagementService getDeviceManagementService() {
return deviceManagementService;
}
public void setDeviceManagementService(DeviceManagementService deviceManagementService) {
this.deviceManagementService = deviceManagementService;
}
}

@ -21,9 +21,10 @@ package org.wso2.carbon.policy.mgt.core.internal;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementService;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint;
import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint;
import org.wso2.carbon.policy.mgt.core.PolicyManager;
import org.wso2.carbon.policy.mgt.core.PolicyManagerService;
import org.wso2.carbon.policy.mgt.core.config.PolicyConfigurationManager;
import org.wso2.carbon.policy.mgt.core.config.PolicyManagementConfig;
import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig;
@ -51,6 +52,12 @@ import org.wso2.carbon.user.core.service.RealmService;
* policy="dynamic"
* bind="setPEPService"
* unbind="unsetPEPService"
* @scr.reference name="org.wso2.carbon.device.manager"
* interface="org.wso2.carbon.device.mgt.core.service.DeviceManagementService"
* cardinality="1..1"
* policy="dynamic"
* bind="setDeviceManagementService"
* unbind="unsetDeviceManagementService"
*/
public class PolicyManagementServiceComponent {
@ -66,7 +73,7 @@ public class PolicyManagementServiceComponent {
PolicyManagementDAOFactory.init(dsConfig);
componentContext.getBundleContext().registerService(
PolicyManager.class.getName(), new PolicyManagementService(), null);
PolicyManagerService.class.getName(), new PolicyManagementService(), null);
} catch (Throwable t) {
String msg = "Error occurred while initializing the Policy management core.";
@ -130,4 +137,18 @@ public class PolicyManagementServiceComponent {
PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint(null);
}
protected void setDeviceManagementService(DeviceManagementService deviceManagerService) {
if (log.isDebugEnabled()) {
log.debug("Setting Device Management Service");
}
PolicyManagementDataHolder.getInstance().setDeviceManagementService(deviceManagerService);
}
protected void unsetDeviceManagementService(DeviceManagementService deviceManagementService) {
if (log.isDebugEnabled()) {
log.debug("Unsetting Device Management Service");
}
PolicyManagementDataHolder.getInstance().setDeviceManagementService(null);
}
}

@ -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.mgt;
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.common.ProfileFeature;
import java.util.List;
public interface FeatureManager {
Feature addFeature(Feature feature) throws FeatureManagementException;
public List<Feature> addFeatures(List<Feature> features) throws FeatureManagementException;
Feature updateFeature(Feature feature) throws FeatureManagementException;
boolean deleteFeature(Feature feature) throws FeatureManagementException;
ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagementException;
ProfileFeature updateProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagementException;
List<ProfileFeature> addProfileFeatures(List<ProfileFeature> features, int profileId) throws FeatureManagementException;
List<ProfileFeature> updateProfileFeatures(List<ProfileFeature> features, int profileId) throws FeatureManagementException;
List<Feature> getAllFeatures() throws FeatureManagementException;
List<Feature> getAllFeatures(String deviceType) throws FeatureManagementException;
List<ProfileFeature> getFeaturesForProfile(int profileId) throws FeatureManagementException;
boolean deleteFeature(int featureId) throws FeatureManagementException;
boolean deleteFeaturesOfProfile(Profile profile) throws FeatureManagementException;
}

@ -0,0 +1,57 @@
/*
* 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.mgt;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.core.dto.Device;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import java.util.List;
public interface PolicyManager {
Policy addPolicy(Policy policy) throws PolicyManagementException;
Policy updatePolicy(Policy policy) throws PolicyManagementException;
boolean deletePolicy(Policy policy) throws PolicyManagementException;
Policy addPolicyToDevice(List<DeviceIdentifier> deviceIdentifierList, Policy policy) throws PolicyManagementException;
Policy addPolicyToRole(List<String> roleNames, Policy policy) throws PolicyManagementException;
Policy addPolicyToUser(List<String> usernameList, Policy policy) throws PolicyManagementException;
Policy getPolicyByProfileID(int profileId) throws PolicyManagementException;
Policy getPolicy(int policyId) throws PolicyManagementException;
List<Policy> getPolicies() throws PolicyManagementException;
List<Policy> getPoliciesOfDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException;
List<Policy> getPoliciesOfDeviceType(String deviceType) throws PolicyManagementException;
List<Policy> getPoliciesOfRole(String roleName) throws PolicyManagementException;
List<Policy> getPoliciesOfUser(String username) throws PolicyManagementException;
List<Device> getPolicyAppliedDevicesIds(int policyId) throws PolicyManagementException;
}

@ -0,0 +1,40 @@
/*
* 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.mgt;
import org.wso2.carbon.policy.mgt.common.Profile;
import org.wso2.carbon.policy.mgt.common.ProfileManagementException;
import java.util.List;
public interface ProfileManager {
Profile addProfile(Profile profile) throws ProfileManagementException;
Profile updateProfile(Profile profile) throws ProfileManagementException;
boolean deleteProfile(Profile profile) throws ProfileManagementException;
Profile getProfiles(int profileId) throws ProfileManagementException;
List<Profile> getAllProfiles() throws ProfileManagementException;
List<Profile> getProfilesOfDeviceType(String deviceType) throws ProfileManagementException;
}

@ -0,0 +1,368 @@
/*
* 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.mgt.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.common.ProfileFeature;
import org.wso2.carbon.policy.mgt.core.dao.FeatureDAO;
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.mgt.FeatureManager;
import java.util.List;
public class FeatureManagerImpl implements FeatureManager {
private FeatureDAO featureDAO;
private static Log log = LogFactory.getLog(FeatureManagerImpl.class);
public FeatureManagerImpl() {
featureDAO = PolicyManagementDAOFactory.getFeatureDAO();
}
@Override
public Feature addFeature(Feature feature) throws FeatureManagementException {
try {
PolicyManagementDAOFactory.beginTransaction();
feature = featureDAO.addFeature(feature);
PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Unable to roll back the transaction");
}
String msg = "Error occurred while adding feature (" + feature.getName() + ")";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Unable to roll back the transaction");
}
String msg = "Error occurred while adding feature (" + feature.getName() + ")";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
}
return feature;
}
@Override
public List<Feature> addFeatures(List<Feature> features) throws FeatureManagementException {
try {
PolicyManagementDAOFactory.beginTransaction();
features = featureDAO.addFeatures(features);
PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Unable to roll back the transaction");
}
String msg = "Error occurred while adding feature (" + features.size()+ ")";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Unable to roll back the transaction");
}
String msg = "Error occurred while adding feature (" + features.size() + ")";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
}
return features;
}
@Override
public Feature updateFeature(Feature feature) throws FeatureManagementException {
try {
PolicyManagementDAOFactory.beginTransaction();
feature = featureDAO.updateFeature(feature);
PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Unable to roll back the transaction");
}
String msg = "Error occurred while updating feature (" + feature.getName() + ")";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Unable to roll back the transaction");
}
String msg = "Error occurred while updating feature (" + feature.getName() + ")";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
}
return feature;
}
@Override
public boolean deleteFeature(Feature feature) throws FeatureManagementException {
boolean bool;
try {
PolicyManagementDAOFactory.beginTransaction();
bool = featureDAO.deleteFeature(feature.getId());
PolicyManagementDAOFactory.commitTransaction();
} catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Unable to roll back the transaction");
}
String msg = "Error occurred while deleting the feature (" + feature.getName() + ")";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Unable to roll back the transaction");
}
String msg = "Error occurred while deleting the feature (" + feature.getName() + ") from database";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
}
return bool;
}
@Override
public ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagementException {
try {
PolicyManagementDAOFactory.beginTransaction();
feature = featureDAO.addProfileFeature(feature, profileId);
PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while adding profile feature (" +
feature.getFeature().getName() + " - " + profileId + ")";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Unable to roll back the transaction");
}
String msg = "Error occurred while adding profile feature (" +
feature.getFeature().getName() + " - " + profileId + ") to database.";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
}
return feature;
}
@Override
public ProfileFeature updateProfileFeature(ProfileFeature feature, int profileId) throws
FeatureManagementException {
try {
PolicyManagementDAOFactory.beginTransaction();
feature = featureDAO.updateProfileFeature(feature, profileId);
PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Unable to roll back the transaction");
}
String msg = "Error occurred while updating feature (" +
feature.getFeature().getName() + " - " + profileId + ")";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Unable to roll back the transaction");
}
String msg = "Error occurred while updating feature (" +
feature.getFeature().getName() + " - " + profileId + ") in database.";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
}
return feature;
}
@Override
public List<ProfileFeature> addProfileFeatures(List<ProfileFeature> features, int profileId) throws
FeatureManagementException {
try {
PolicyManagementDAOFactory.beginTransaction();
features = featureDAO.addProfileFeatures(features, profileId);
PolicyManagementDAOFactory.commitTransaction();
} catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Unable to roll back the transaction");
}
String msg = "Error occurred while adding the features to profile id (" + profileId + ")";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Unable to roll back the transaction");
}
String msg = "Error occurred while adding the features to profile id (" + profileId + ") to the database";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
}
return features;
}
@Override
public List<ProfileFeature> updateProfileFeatures(List<ProfileFeature> features, int profileId) throws
FeatureManagementException {
try {
PolicyManagementDAOFactory.beginTransaction();
features = featureDAO.updateProfileFeatures(features, profileId);
PolicyManagementDAOFactory.commitTransaction();
} catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Unable to roll back the transaction");
}
String msg = "Error occurred while updating the features to profile id (" + profileId + ")";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Unable to roll back the transaction");
}
String msg = "Error occurred while updating the features to profile id (" + profileId + ") to the database";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
}
return features;
}
@Override
public List<Feature> getAllFeatures() throws FeatureManagementException {
try {
return featureDAO.getAllFeatures();
} catch (FeatureManagerDAOException e) {
String msg = "Error occurred while getting the features.";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
}
}
@Override
public List<Feature> getAllFeatures(String deviceType) throws FeatureManagementException {
try {
return featureDAO.getAllFeatures(deviceType);
} catch (FeatureManagerDAOException e) {
String msg = "Error occurred while getting the features.";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
}
}
@Override
public List<ProfileFeature> getFeaturesForProfile(int profileId) throws FeatureManagementException {
try {
return featureDAO.getFeaturesForProfile(profileId);
} catch (FeatureManagerDAOException e) {
String msg = "Error occurred while getting the features.";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
}
}
@Override
public boolean deleteFeature(int featureId) throws FeatureManagementException {
boolean bool;
try {
PolicyManagementDAOFactory.beginTransaction();
bool = featureDAO.deleteFeature(featureId);
PolicyManagementDAOFactory.commitTransaction();
} catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Unable to roll back the transaction");
}
String msg = "Error occurred while deleting the feature - id (" + featureId + ")";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Unable to roll back the transaction");
}
String msg = "Error occurred while deleting the feature - id (" + featureId + ") from database.";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
}
return bool;
}
@Override
public boolean deleteFeaturesOfProfile(Profile profile) throws FeatureManagementException {
boolean bool;
try {
PolicyManagementDAOFactory.beginTransaction();
bool = featureDAO.deleteFeaturesOfProfile(profile);
PolicyManagementDAOFactory.commitTransaction();
} catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Unable to roll back the transaction");
}
String msg = "Error occurred while deleting the feature of - profile (" + profile.getProfileName() + ")";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Unable to roll back the transaction");
}
String msg = "Error occurred while deleting the feature of - profile (" +
profile.getProfileName() + ") from database";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
}
return bool;
}
}

@ -0,0 +1,568 @@
/*
* 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.mgt.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dto.Device;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.common.Profile;
import org.wso2.carbon.policy.mgt.core.dao.*;
import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager;
import java.util.ArrayList;
import java.util.List;
public class PolicyManagerImpl implements PolicyManager {
private PolicyDAO policyDAO;
private ProfileDAO profileDAO;
private FeatureDAO featureDAO;
private DeviceDAO deviceDAO;
private DeviceTypeDAO deviceTypeDAO;
private static Log log = LogFactory.getLog(PolicyManagerImpl.class);
public PolicyManagerImpl() {
this.policyDAO = PolicyManagementDAOFactory.getPolicyDAO();
this.profileDAO = PolicyManagementDAOFactory.getProfileDAO();
this.featureDAO = PolicyManagementDAOFactory.getFeatureDAO();
this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
}
@Override
public Policy addPolicy(Policy policy) throws PolicyManagementException {
try {
PolicyManagementDAOFactory.beginTransaction();
if (policy.getProfile() != null && policy.getProfile().getProfileId() == 0) {
profileDAO.addProfile(policy.getProfile());
featureDAO.addProfileFeatures(policy.getProfile().getProfileFeaturesList(),
policy.getProfile().getProfileId());
}
policy = policyDAO.addPolicy(policy);
if (policy.getUsers() != null) {
policyDAO.addPolicyToUser(policy.getUsers(), policy);
}
if (policy.getRoleList() != null) {
policyDAO.addPolicyToRole(policy.getRoleList(), policy);
}
if (policy.getDeviceList() != null) {
policyDAO.addPolicyToDevice(policy.getDeviceList(), policy);
}
if (policy.getEndDate() != null & policy.getStartDate() != null) {
policyDAO.addDatesToPolicy(policy.getStartDate(), policy.getEndDate(), policy);
}
if (policy.getStartTime() != 0 & policy.getEndTime() != 0) {
policyDAO.addTimesToPolicy(policy.getStartTime(), policy.getEndTime(), policy);
}
if (policy.getLatitude() != null && policy.getLongitude() != null) {
policyDAO.addLocationToPolicy(policy.getLatitude(), policy.getLongitude(), policy);
}
PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Error occurred while adding the policy (" +
policy.getId() + " - " + policy.getPolicyName() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (ProfileManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Error occurred while adding the profile related to policy (" +
policy.getId() + " - " + policy.getPolicyName() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Error occurred while adding the features of profile related to policy (" +
policy.getId() + " - " + policy.getPolicyName() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return policy;
}
@Override
public Policy updatePolicy(Policy policy) throws PolicyManagementException {
try {
PolicyManagementDAOFactory.beginTransaction();
policy = policyDAO.updatePolicy(policy);
policyDAO.deleteAllPolicyRelatedConfigs(policy.getId());
if (policy.getUsers() != null) {
policyDAO.addPolicyToUser(policy.getUsers(), policy);
}
if (policy.getRoleList() != null) {
policyDAO.addPolicyToRole(policy.getRoleList(), policy);
}
if (policy.getDeviceList() != null) {
policyDAO.addPolicyToDevice(policy.getDeviceList(), policy);
}
if (policy.getEndDate() != null & policy.getStartDate() != null) {
policyDAO.addDatesToPolicy(policy.getStartDate(), policy.getEndDate(), policy);
}
if (policy.getStartTime() != 0 & policy.getEndTime() != 0) {
policyDAO.addTimesToPolicy(policy.getStartTime(), policy.getEndTime(), policy);
}
if (policy.getLatitude() != null && policy.getLongitude() != null) {
policyDAO.addLocationToPolicy(policy.getLatitude(), policy.getLongitude(), policy);
}
PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Error occurred while updating the policy ("
+ policy.getId() + " - " + policy.getPolicyName() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return policy;
}
@Override
public boolean deletePolicy(Policy policy) throws PolicyManagementException {
boolean bool;
try {
PolicyManagementDAOFactory.beginTransaction();
bool = policyDAO.deletePolicy(policy);
PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Error occurred while deleting the policy ("
+ policy.getId() + " - " + policy.getPolicyName() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return bool;
}
@Override
public Policy addPolicyToDevice(List<DeviceIdentifier> deviceIdentifierList, Policy policy) throws
PolicyManagementException {
try {
PolicyManagementDAOFactory.beginTransaction();
if (policy.getId() == 0) {
policyDAO.addPolicy(policy);
}
List<Device> deviceList = new ArrayList<Device>();
for (DeviceIdentifier deviceIdentifier : deviceIdentifierList) {
deviceList.add(deviceDAO.getDevice(deviceIdentifier));
}
policy = policyDAO.addPolicyToDevice(deviceList, policy);
PolicyManagementDAOFactory.commitTransaction();
if (policy.getDeviceList() != null) {
if (log.isDebugEnabled()) {
log.debug("Device list of policy is not null.");
}
policy.getDeviceList().addAll(deviceList);
} else {
if (log.isDebugEnabled()) {
log.debug("Device list of policy is null. So added the first device to the list.");
}
policy.setDeviceList(deviceList);
}
} catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Error occurred while adding the policy ("
+ policy.getId() + " - " + policy.getPolicyName() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (DeviceManagementDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Error occurred while adding the policy to device list";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return policy;
}
@Override
public Policy addPolicyToRole(List<String> roleNames, Policy policy) throws PolicyManagementException {
try {
PolicyManagementDAOFactory.beginTransaction();
if (policy.getId() == 0) {
policyDAO.addPolicy(policy);
}
policy = policyDAO.addPolicyToRole(roleNames, policy);
PolicyManagementDAOFactory.commitTransaction();
if (policy.getRoleList() != null) {
if (log.isDebugEnabled()) {
log.debug("New roles list is added to the policy ");
}
policy.getRoleList().addAll(roleNames);
} else {
if (log.isDebugEnabled()) {
log.debug("Roles list was null, new roles are added.");
}
policy.setRoleList(roleNames);
}
} catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Error occurred while adding the policy ("
+ policy.getId() + " - " + policy.getPolicyName() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return policy;
}
@Override
public Policy addPolicyToUser(List<String> usernameList, Policy policy) throws PolicyManagementException {
try {
PolicyManagementDAOFactory.beginTransaction();
if (policy.getId() == 0) {
policyDAO.addPolicy(policy);
}
policy = policyDAO.addPolicyToUser(usernameList, policy);
PolicyManagementDAOFactory.commitTransaction();
if (policy.getRoleList() != null) {
if (log.isDebugEnabled()) {
log.debug("New users list is added to the policy ");
}
policy.getRoleList().addAll(usernameList);
} else {
if (log.isDebugEnabled()) {
log.debug("Users list was null, new users list is added.");
}
policy.setRoleList(usernameList);
}
} catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Error occurred while adding the policy ("
+ policy.getId() + " - " + policy.getPolicyName() + ") to user list.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return policy;
}
@Override
public Policy getPolicyByProfileID(int profileId) throws PolicyManagementException {
Policy policy;
Profile profile;
List<Device> deviceList;
List<String> roleNames;
try {
policy = policyDAO.getPolicyByProfileID(profileId);
deviceList = getPolicyAppliedDevicesIds(policy.getId());
roleNames = policyDAO.getPolicyAppliedRoles(policy.getId());
policyDAO.getDatesOfPolicy(policy);
policyDAO.getTimesOfPolicy(policy);
policyDAO.getLocationsOfPolicy(policy);
profile = profileDAO.getProfiles(profileId);
policy.setProfile(profile);
policy.setRoleList(roleNames);
policy.setDeviceList(deviceList);
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting the policy related to profile ID (" + profileId + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (ProfileManagerDAOException e) {
String msg = "Error occurred while getting the profile related to profile ID (" + profileId + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return policy;
}
@Override
public Policy getPolicy(int policyId) throws PolicyManagementException {
Policy policy;
List<Device> deviceList;
List<String> roleNames;
try {
policy = policyDAO.getPolicy(policyId);
deviceList = getPolicyAppliedDevicesIds(policyId);
roleNames = policyDAO.getPolicyAppliedRoles(policyId);
policyDAO.getDatesOfPolicy(policy);
policyDAO.getTimesOfPolicy(policy);
policyDAO.getLocationsOfPolicy(policy);
Profile profile = profileDAO.getProfiles(policy.getProfile().getProfileId());
policy.setProfile(profile);
policy.setRoleList(roleNames);
policy.setDeviceList(deviceList);
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting the policy related to policy ID (" + policyId + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (ProfileManagerDAOException e) {
String msg = "Error occurred while getting the profile related to policy ID (" + policyId + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return policy;
}
@Override
public List<Policy> getPolicies() throws PolicyManagementException {
List<Policy> policyList;
try {
policyList = policyDAO.getAllPolicies();
List<Profile> profileList = profileDAO.getAllProfiles();
for (Policy policy : policyList) {
for (Profile profile : profileList) {
if (policy.getProfileId() == profile.getProfileId()) {
policy.setProfile(profile);
}
}
policy.setDeviceList(getPolicyAppliedDevicesIds(policy.getId()));
policy.setRoleList(policyDAO.getPolicyAppliedRoles(policy.getId()));
policyDAO.getDatesOfPolicy(policy);
policyDAO.getTimesOfPolicy(policy);
policyDAO.getLocationsOfPolicy(policy);
}
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting all the policies.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (ProfileManagerDAOException e) {
String msg = "Error occurred while getting all the profiles.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return policyList;
}
@Override
public List<Policy> getPoliciesOfDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
List<Integer> policyIdList;
List<Policy> policies = new ArrayList<Policy>();
try {
Device device = deviceDAO.getDevice(deviceIdentifier);
policyIdList = policyDAO.getPolicyIdsOfDevice(device);
List<Policy> tempPolicyList = policyDAO.getAllPolicies();
for (Policy policy : tempPolicyList) {
for (Integer i : policyIdList) {
if (policy.getId() == i) {
policies.add(policy);
}
}
}
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting the policies for device identifier (" +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting device related to device identifier (" +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return policies;
}
@Override
public List<Policy> getPoliciesOfDeviceType(String deviceTypeName) throws PolicyManagementException {
List<Policy> policies = new ArrayList<Policy>();
try {
DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName);
List<Profile> profileList = profileDAO.getProfilesOfDeviceType(deviceType);
List<Policy> allPolicies = policyDAO.getAllPolicies();
for (Profile profile : profileList) {
for (Policy policy : allPolicies) {
if (policy.getProfileId() == profile.getProfileId()) {
policies.add(policy);
}
}
}
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting all the policies.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (ProfileManagerDAOException e) {
String msg = "Error occurred while getting the profiles related to device type (" + deviceTypeName + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting device type object related to (" + deviceTypeName + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return policies;
}
@Override
public List<Policy> getPoliciesOfRole(String roleName) throws PolicyManagementException {
List<Policy> policies = new ArrayList<Policy>();
List<Integer> policyIdList;
try {
policyIdList = policyDAO.getPolicyOfRole(roleName);
List<Policy> tempPolicyList = policyDAO.getAllPolicies();
for (Policy policy : tempPolicyList) {
for (Integer i : policyIdList) {
if (policy.getId() == i) {
policies.add(policy);
}
}
}
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting the policies.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return policies;
}
@Override
public List<Policy> getPoliciesOfUser(String username) throws PolicyManagementException {
List<Policy> policies = new ArrayList<Policy>();
List<Integer> policyIdList;
try {
policyIdList = policyDAO.getPolicyOfUser(username);
List<Policy> tempPolicyList = policyDAO.getAllPolicies();
for (Policy policy : tempPolicyList) {
for (Integer i : policyIdList) {
if (policy.getId() == i) {
policies.add(policy);
}
}
}
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting the policies.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return policies;
}
@Override
public List<Device> getPolicyAppliedDevicesIds(int policyId) throws PolicyManagementException {
List<Device> deviceList = new ArrayList<Device>();
List<Integer> deviceIdList;
try {
deviceIdList = policyDAO.getPolicyAppliedDevicesIds(policyId);
for (Integer integer : deviceIdList) {
deviceList.add(deviceDAO.getDevice(integer));
}
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting the device ids related to policy id (" + policyId + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting the devices related to policy id (" + policyId + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return deviceList;
}
}

@ -0,0 +1,283 @@
/*
* 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.mgt.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.policy.mgt.common.Profile;
import org.wso2.carbon.policy.mgt.common.ProfileFeature;
import org.wso2.carbon.policy.mgt.common.ProfileManagementException;
import org.wso2.carbon.policy.mgt.core.dao.*;
import org.wso2.carbon.policy.mgt.core.mgt.ProfileManager;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class ProfileManagerImpl implements ProfileManager {
private static Log log = LogFactory.getLog(ProfileManagerImpl.class);
private ProfileDAO profileDAO;
private FeatureDAO featureDAO;
private DeviceDAO deviceDAO;
private DeviceTypeDAO deviceTypeDAO;
public ProfileManagerImpl() {
profileDAO = PolicyManagementDAOFactory.getProfileDAO();
featureDAO = PolicyManagementDAOFactory.getFeatureDAO();
deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
}
@Override
public Profile addProfile(Profile profile) throws ProfileManagementException {
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
profile.setCreatedDate(currentTimestamp);
profile.setUpdatedDate(currentTimestamp);
try {
PolicyManagementDAOFactory.beginTransaction();
profile = profileDAO.addProfile(profile);
featureDAO.addProfileFeatures(profile.getProfileFeaturesList(), profile.getProfileId());
PolicyManagementDAOFactory.commitTransaction();
} catch (ProfileManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Error occurred while adding the profile (" + profile.getProfileName() + ")";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Error occurred while adding the profile features (" + profile.getProfileName() + ")";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Error occurred while adding the profile (" + profile.getProfileName() + ") to the database";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
}
return profile;
}
@Override
public Profile updateProfile(Profile profile) throws ProfileManagementException {
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
profile.setUpdatedDate(currentTimestamp);
try {
PolicyManagementDAOFactory.beginTransaction();
profile = profileDAO.updateProfile(profile);
featureDAO.updateProfileFeatures(profile.getProfileFeaturesList(), profile.getProfileId());
PolicyManagementDAOFactory.commitTransaction();
} catch (ProfileManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Error occurred while updating the profile (" + profile.getProfileName() + ")";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Error occurred while updating the profile features (" + profile.getProfileName() + ")";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Error occurred while updating the profile (" + profile.getProfileName() + ") to the database";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
}
return profile;
}
@Override
public boolean deleteProfile(Profile profile) throws ProfileManagementException {
boolean bool = true;
try {
PolicyManagementDAOFactory.beginTransaction();
featureDAO.deleteFeaturesOfProfile(profile);
bool = profileDAO.deleteProfile(profile);
PolicyManagementDAOFactory.commitTransaction();
} catch (ProfileManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Error occurred while deleting the profile (" + profile.getProfileName() + ")";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Error occurred while deleting the features from profile (" + profile.getProfileName() + ")";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Error occurred while deleting the profile (" + profile.getProfileName() + ") from database";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
}
return bool;
}
@Override
public Profile getProfiles(int profileId) throws ProfileManagementException {
Profile profile;
List<ProfileFeature> featureList;
DeviceType deviceType;
try {
profile = profileDAO.getProfiles(profileId);
featureList = featureDAO.getFeaturesForProfile(profileId);
deviceType = deviceTypeDAO.getDeviceType(profile.getDeviceType().getId());
profile.setProfileFeaturesList(featureList);
profile.setDeviceType(deviceType);
} catch (ProfileManagerDAOException e) {
String msg = "Error occurred while getting profile id (" + profileId + ")";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (FeatureManagerDAOException e) {
String msg = "Error occurred while getting features related profile id (" + profileId + ")";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting device type related profile id (" + profileId + ")";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
}
return profile;
}
@Override
public List<Profile> getAllProfiles() throws ProfileManagementException {
List<Profile> profileList;
try {
profileList = profileDAO.getAllProfiles();
List<ProfileFeature> featureList = featureDAO.getAllProfileFeatures();
List<DeviceType> deviceTypes = deviceTypeDAO.getDeviceTypes();
for (Profile profile : profileList) {
List<ProfileFeature> list = new ArrayList<ProfileFeature>();
for (ProfileFeature profileFeature : featureList) {
if (profile.getProfileId() == profileFeature.getProfileId()) {
list.add(profileFeature);
}
}
profile.setProfileFeaturesList(list);
for (DeviceType deviceType : deviceTypes) {
if (profile.getDeviceType().getId() == deviceType.getId()) {
profile.setDeviceType(deviceType);
}
}
}
} catch (ProfileManagerDAOException e) {
String msg = "Error occurred while getting profiles";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (FeatureManagerDAOException e) {
String msg = "Error occurred while getting features related to profiles";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting device types related to profiles";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
}
return profileList;
}
@Override
public List<Profile> getProfilesOfDeviceType(String deviceTypeName) throws ProfileManagementException {
List<Profile> profileList;
List<ProfileFeature> featureList;
try {
DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName);
profileList = profileDAO.getProfilesOfDeviceType(deviceType);
featureList = featureDAO.getAllProfileFeatures();
for (Profile profile : profileList) {
List<ProfileFeature> profileFeatureList = new ArrayList<ProfileFeature>();
for (ProfileFeature profileFeature : featureList) {
if (profile.getProfileId() == profileFeature.getProfileId()) {
profileFeatureList.add(profileFeature);
}
}
profile.setProfileFeaturesList(profileFeatureList);
}
} catch (ProfileManagerDAOException e) {
String msg = "Error occurred while getting profiles";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting device types";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (FeatureManagerDAOException e) {
String msg = "Error occurred while getting profile features types";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
}
return profileList;
}
}

@ -18,80 +18,79 @@
package org.wso2.carbon.policy.mgt.core.service;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.policy.mgt.common.*;
import org.wso2.carbon.policy.mgt.core.PolicyManager;
import org.wso2.carbon.policy.mgt.core.PolicyManagerImpl;
import org.wso2.carbon.policy.mgt.core.PolicyManagerService;
import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl;
import java.util.List;
public class PolicyManagementService implements PolicyManager {
public class PolicyManagementService implements PolicyManagerService {
PolicyManager policyManager;
PolicyManagerService policyManagerService;
public PolicyManagementService() {
policyManager = new PolicyManagerImpl();
policyManagerService = new PolicyManagerServiceImpl();
}
@Override
public Feature addFeature(Feature feature) throws FeatureManagementException {
return policyManager.addFeature(feature);
return policyManagerService.addFeature(feature);
}
@Override
public Feature updateFeature(Feature feature) throws FeatureManagementException {
return policyManager.updateFeature(feature);
return policyManagerService.updateFeature(feature);
}
@Override
public Profile addProfile(Profile profile) throws PolicyManagementException {
return policyManager.addProfile(profile);
return policyManagerService.addProfile(profile);
}
@Override
public Profile updateProfile(Profile profile) throws PolicyManagementException {
return policyManager.updateProfile(profile);
return policyManagerService.updateProfile(profile);
}
@Override
public Policy addPolicy(Policy policy) throws PolicyManagementException {
return policyManager.addPolicy(policy);
return policyManagerService.addPolicy(policy);
}
@Override
public Policy updatePolicy(Policy policy) throws PolicyManagementException {
return policyManager.updatePolicy(policy);
return policyManagerService.updatePolicy(policy);
}
@Override
public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
return policyManager.getEffectivePolicy(deviceIdentifier);
return policyManagerService.getEffectivePolicy(deviceIdentifier);
}
@Override
public Policy getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws FeatureManagementException {
return policyManager.getEffectiveFeatures(deviceIdentifier);
return policyManagerService.getEffectiveFeatures(deviceIdentifier);
}
@Override
public List<Policy> getPolicies(String deviceType) throws PolicyManagementException {
return policyManager.getPolicies(deviceType);
return policyManagerService.getPolicies(deviceType);
}
@Override
public List<Feature> getFeatures() throws FeatureManagementException {
return policyManager.getFeatures();
return policyManagerService.getFeatures();
}
@Override
public PolicyAdministratorPoint getPAP() throws PolicyManagementException {
return policyManager.getPAP();
return policyManagerService.getPAP();
}
@Override
public PolicyInformationPoint getPIP() throws PolicyManagementException {
return policyManager.getPIP();
return policyManagerService.getPIP();
}
}

@ -25,19 +25,33 @@ import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dto.Device;
import org.wso2.carbon.policy.mgt.common.Feature;
import org.wso2.carbon.policy.mgt.common.FeatureManagementException;
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.common.ProfileFeature;
import org.wso2.carbon.policy.mgt.common.ProfileManagementException;
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;
import org.wso2.carbon.policy.mgt.core.impl.PolicyAdministratorPointImpl;
import org.wso2.carbon.policy.mgt.core.mgt.FeatureManager;
import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager;
import org.wso2.carbon.policy.mgt.core.mgt.ProfileManager;
import org.wso2.carbon.policy.mgt.core.mgt.impl.FeatureManagerImpl;
import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl;
import org.wso2.carbon.policy.mgt.core.mgt.impl.ProfileManagerImpl;
import org.wso2.carbon.policy.mgt.core.util.*;
import javax.sql.DataSource;
import javax.xml.bind.JAXBContext;
@ -46,12 +60,18 @@ import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class PolicyDAOTestCase {
private DataSource dataSource;
private static DataSource dataSource;
private List<Feature> featureList;
private List<ProfileFeature> profileFeatureList;
private Profile profile;
private Policy policy;
private List<Device> devices;
private static final Log log = LogFactory.getLog(PolicyDAOTestCase.class);
@BeforeClass
@ -75,6 +95,7 @@ public class PolicyDAOTestCase {
properties.setPassword(dbConfig.getPwd());
dataSource = new org.apache.tomcat.jdbc.pool.DataSource(properties);
PolicyManagementDAOFactory.init(dataSource);
DeviceManagementDAOFactory.init(dataSource);
break;
case H2:
@ -86,6 +107,7 @@ public class PolicyDAOTestCase {
dataSource = new org.apache.tomcat.jdbc.pool.DataSource(properties);
this.initH2SQLScript();
PolicyManagementDAOFactory.init(dataSource);
DeviceManagementDAOFactory.init(dataSource);
break;
default:
@ -125,7 +147,6 @@ public class PolicyDAOTestCase {
stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/CreateH2TestDB.sql'");
} finally {
TestUtils.cleanupResources(conn, stmt, null);
}
}
@ -146,14 +167,140 @@ public class PolicyDAOTestCase {
}
@Test
public void addFeatures() throws FeatureManagerDAOException {
public void addDeviceType() throws DeviceManagementDAOException {
DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
deviceTypeDAO.addDeviceType(DeviceTypeCreator.getDeviceType());
}
@Test(dependsOnMethods = ("addDeviceType"))
public void addDevice() throws DeviceManagementDAOException {
DeviceDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceDAO();
devices = DeviceCreator.getDeviceList(DeviceTypeCreator.getDeviceType());
for (Device device : devices) {
deviceTypeDAO.addDevice(device);
}
}
FeatureDAOImpl policyDAO = new FeatureDAOImpl();
List<Feature> featureList = FeatureCreator.getFeatureList();
@Test(dependsOnMethods = ("addDevice"))
public void addFeatures() throws FeatureManagementException {
FeatureManager featureManager = new FeatureManagerImpl();
featureList = FeatureCreator.getFeatureList();
//featureManager.addFeatures(featureList);
for (Feature feature : featureList) {
policyDAO.addFeature(feature);
featureManager.addFeature(feature);
}
}
@Test(dependsOnMethods = ("addFeatures"))
public void addProfileFeatures() throws ProfileManagementException {
ProfileManager profileManager = new ProfileManagerImpl();
profile = ProfileCreator.getProfile(featureList);
profileManager.addProfile(profile);
profileFeatureList = profile.getProfileFeaturesList();
}
@Test(dependsOnMethods = ("addProfileFeatures"))
public void addPolicy() throws PolicyManagementException {
PolicyManager policyManager = new PolicyManagerImpl();
policy = PolicyCreator.createPolicy(profile);
policyManager.addPolicy(policy);
}
@Test(dependsOnMethods = ("addPolicy"))
public void addPolicyToRole() throws PolicyManagementException {
PolicyManager policyManager = new PolicyManagerImpl();
List<String> roles = new ArrayList<String>();
roles.add("Test_ROLE_01");
roles.add("Test_ROLE_02");
roles.add("Test_ROLE_03");
policyManager.addPolicyToRole(roles, policy);
}
@Test(dependsOnMethods = ("addPolicyToRole"))
public void addPolicyToDevice() throws PolicyManagementException {
PolicyManager policyManager = new PolicyManagerImpl();
Device device = DeviceCreator.getSingleDevice();
List<DeviceIdentifier> deviceIdentifierList = new ArrayList<DeviceIdentifier>();
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(device.getDeviceIdentificationId());
deviceIdentifier.setType("ANDROID");
deviceIdentifierList.add(deviceIdentifier);
policyManager.addPolicyToDevice(deviceIdentifierList, policy);
}
@Test(dependsOnMethods = ("addPolicyToDevice"))
public void addNewPolicy() throws PolicyManagementException {
PolicyManager policyManager = new PolicyManagerImpl();
policy = PolicyCreator.createPolicy2(profile);
policyManager.addPolicy(policy);
}
@Test(dependsOnMethods = ("addNewPolicy"))
public void getPolicies() throws PolicyManagementException {
PolicyAdministratorPoint policyAdministratorPoint = new PolicyAdministratorPointImpl();
List<Policy> policyList = policyAdministratorPoint.getPolicies();
log.debug("----------All policies---------");
for (Policy policy : policyList) {
log.debug("Policy Id : " + policy.getId() + " Policy Name : " + policy.getPolicyName());
}
}
@Test(dependsOnMethods = ("getPolicies"))
public void getDeviceTypeRelatedPolicy() throws PolicyManagementException {
PolicyAdministratorPoint policyAdministratorPoint = new PolicyAdministratorPointImpl();
List<Policy> policyList = policyAdministratorPoint.getPoliciesOfDeviceType("ANDROID");
log.debug("----------Device type related policy---------");
for (Policy policy : policyList) {
log.debug("Policy Id : " + policy.getId() + " Policy Name : " + policy.getPolicyName());
}
}
@Test(dependsOnMethods = ("getDeviceTypeRelatedPolicy"))
public void getUserRelatedPolicy() throws PolicyManagementException {
PolicyAdministratorPoint policyAdministratorPoint = new PolicyAdministratorPointImpl();
List<Policy> policyList = policyAdministratorPoint.getPoliciesOfUser("Dilshan");
log.debug("----------User related policy---------");
for (Policy policy : policyList) {
log.debug("Policy Id : " + policy.getId() + " Policy Name : " + policy.getPolicyName());
}
}
@Test(dependsOnMethods = ("getDeviceTypeRelatedPolicy"))
public void getRoleRelatedPolicy() throws PolicyManagementException {
PolicyAdministratorPoint policyAdministratorPoint = new PolicyAdministratorPointImpl();
List<Policy> policyList = policyAdministratorPoint.getPoliciesOfRole("Test_ROLE_01");
log.debug("----------Roles related policy---------");
for (Policy policy : policyList) {
log.debug("Policy Id : " + policy.getId() + " Policy Name : " + policy.getPolicyName());
}
}
}

@ -0,0 +1,71 @@
/*
* 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.util;
import org.wso2.carbon.device.mgt.core.dto.Device;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.dto.Status;
import java.util.ArrayList;
import java.util.List;
public class DeviceCreator {
private static List<Device> deviceList = new ArrayList<Device>();
public static List<Device> getDeviceList(DeviceType deviceType) {
Device device = new Device();
device.setId(1);
device.setDeviceTypeId(1);
device.setName("Galaxy S6");
device.setOwnerId("geeth");
device.setOwnerShip("BYOD");
device.setTenantId(-1234);
device.setDeviceType(deviceType);
device.setStatus(Status.ACTIVE);
device.setDeviceIdentificationId("aaaaaaaaaaaaaaaaaaaaaaaaaa");
Device device2 = new Device();
device2.setId(2);
device2.setDeviceTypeId(1);
device2.setName("Nexus 5");
device2.setOwnerId("manoj");
device2.setOwnerShip("BYOD");
device2.setTenantId(-1234);
device2.setDeviceType(deviceType);
device.setStatus(Status.ACTIVE);
device2.setDeviceIdentificationId("bbbbbbbbbbbbbbbbbbbbbbbb");
deviceList.add(device);
// deviceList.add(device2);
return deviceList;
}
public static Device getSingleDevice() {
return deviceList.get(0);
}
}

@ -0,0 +1,33 @@
/*
* 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.util;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
public class DeviceTypeCreator {
public static DeviceType getDeviceType(){
DeviceType deviceType = new DeviceType();
deviceType.setName("ANDROID");
deviceType.setId(1);
return deviceType;
}
}

@ -32,6 +32,7 @@ public class FeatureCreator {
feature1.setCode("C001");
feature1.setDescription("Camera");
feature1.setRuleValue("permit_override");
feature1.setDeviceTypeId(1);
Feature feature2 = new Feature();
@ -39,6 +40,7 @@ public class FeatureCreator {
feature2.setCode("L001");
feature2.setDescription("Lock the phone");
feature2.setRuleValue("deny_override");
feature2.setDeviceTypeId(1);
Feature feature3 = new Feature();
@ -46,18 +48,21 @@ public class FeatureCreator {
feature3.setCode("W001");
feature3.setDescription("Wifi configuration for the device");
feature3.setRuleValue("all_available");
feature3.setDeviceTypeId(1);
Feature feature4 = new Feature();
feature4.setName("RING");
feature4.setCode("R001");
feature4.setDescription("Ring the mobile");
feature4.setRuleValue("first_applicable");
feature4.setDeviceTypeId(1);
Feature feature5 = new Feature();
feature5.setName("LDAP");
feature5.setCode("L002");
feature5.setDescription("LDAP Configurations");
feature5.setRuleValue("all_available");
feature5.setDeviceTypeId(1);
Feature feature6 = new Feature();
@ -65,37 +70,43 @@ public class FeatureCreator {
feature6.setCode("V001");
feature6.setDescription("VPN config for accessing the company network from out side");
feature6.setRuleValue("all_available");
feature6.setDeviceTypeId(1);
Feature feature7 = new Feature();
feature7.setName("PASSWORD");
feature7.setCode("P001");
feature7.setDescription("Setting the password for the mobile");
feature7.setRuleValue("first_applicable");
feature7.setDeviceTypeId(1);
Feature feature8 = new Feature();
feature8.setName("WIPE");
feature8.setCode("W002");
feature8.setDescription("Wiping the company profile created to access the company secure data");
feature8.setRuleValue("permit_override");
feature8.setDeviceTypeId(1);
Feature feature9 = new Feature();
feature9.setName("ENCRYPTION");
feature9.setCode("E001");
feature9.setDescription("Adding the encryption for the phone and SD card.");
feature9.setRuleValue("permit_override");
feature9.setDeviceTypeId(1);
Feature feature10 = new Feature();
feature10.setName("APP");
feature10.setCode("A001");
feature10.setDescription("Installing an application to the phone");
feature10.setRuleValue("permit_override");
feature10.setDeviceTypeId(1);
Feature feature11 = new Feature();
feature11.setName("EMAIL");
feature11.setCode("E002");
feature11.setDescription("Email configurations of the phone.");
feature11.setRuleValue("all_applicable");
feature11.setDeviceTypeId(1);
List<Feature> featureList = new ArrayList<Feature>();
featureList.add(feature1);

@ -0,0 +1,81 @@
/*
* 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.util;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.Profile;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
public class PolicyCreator {
public static Policy createPolicy(Profile profile) {
Policy policy = new Policy();
policy.setPolicyName("Test_Policy_01");
policy.setGeneric(true);
policy.setProfile(profile);
List<String> users = new ArrayList<String>();
users.add("Dilshan");
policy.setUsers(users);
return policy;
}
public static Policy createPolicy2(Profile profile) {
Policy policy = new Policy();
policy.setPolicyName("New test Policy");
policy.setGeneric(true);
policy.setProfile(profile);
policy.setDeviceList(DeviceCreator.getDeviceList(DeviceTypeCreator.getDeviceType()));
List<String> roles = new ArrayList<String>();
roles.add("Role_01");
roles.add("Role_02");
roles.add("Role_03");
policy.setRoleList(roles);
List<String> users = new ArrayList<String>();
users.add("Geeth");
users.add("Manoj");
users.add("Milan");
users.add("Dulitha");
policy.setUsers(users);
policy.setLatitude("6.927079");
policy.setLongitude("79.861243");
/* DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd ");
java.util.Date date = new java.util.Date();
policy.setStartDate(new java.sql.Timestamp(date.getDate()));*/
return policy;
}
}

@ -0,0 +1,44 @@
/*
* 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.util;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.policy.mgt.common.Feature;
import org.wso2.carbon.policy.mgt.common.Profile;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import java.util.List;
public class ProfileCreator {
public static Profile getProfile(List<Feature> features) {
Profile profile = new Profile();
DeviceType deviceType = new DeviceType();
deviceType.setId(1);
deviceType.setName("ANDROID");
profile.setProfileFeaturesList(ProfileFeatureCreator.getProfileFeature(features));
profile.setProfileName("Test Profile");
profile.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
profile.setDeviceType(deviceType);
return profile;
}
}

@ -0,0 +1,44 @@
/*
* 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.util;
import org.wso2.carbon.policy.mgt.common.Feature;
import org.wso2.carbon.policy.mgt.common.ProfileFeature;
import java.util.ArrayList;
import java.util.List;
public class ProfileFeatureCreator {
public static List<ProfileFeature> getProfileFeature(List<Feature> features) {
List<ProfileFeature> profileFeatureList = new ArrayList<ProfileFeature>();
for (Feature feature : features) {
ProfileFeature profileFeature = new ProfileFeature();
profileFeature.setContent(feature);
// profileFeature.setContent("rrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
// profileFeature.setProfileId(1);
profileFeature.setFeature(feature);
profileFeatureList.add(profileFeature);
}
return profileFeatureList;
}
}

@ -1,36 +1,40 @@
-- -----------------------------------------------------
-- Table `DM_DEVICE_TYPE`
-- Table DM_DEVICE_TYPE
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `DM_DEVICE_TYPE` (
`ID` INT(11) NOT NULL ,
`NAME` VARCHAR(300) NULL DEFAULT NULL ,
PRIMARY KEY (`ID`) )
CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE (
ID INT(11) AUTO_INCREMENT ,
NAME VARCHAR(300) NULL DEFAULT NULL ,
PRIMARY KEY (ID) )
;
--INSERT INTO DM_DEVICE_TYPE (NAME) VALUES ('ANDROID');
--INSERT INTO DM_DEVICE_TYPE (NAME) VALUES ('IOS');
-- -----------------------------------------------------
-- Table `DM_DEVICE`
-- Table DM_DEVICE
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `DM_DEVICE` (
`ID` VARCHAR(20) NOT NULL ,
`DESCRIPTION` TEXT NULL DEFAULT NULL ,
`NAME` VARCHAR(100) NULL DEFAULT NULL ,
`DATE_OF_ENROLLMENT` BIGINT(20) NULL DEFAULT NULL ,
`DATE_OF_LAST_UPDATE` BIGINT(20) NULL DEFAULT NULL ,
`OWNERSHIP` VARCHAR(45) NULL DEFAULT NULL ,
`STATUS` VARCHAR(15) NULL DEFAULT NULL ,
`DEVICE_TYPE_ID` INT(11) NULL DEFAULT NULL ,
`DEVICE_IDENTIFICATION` VARCHAR(300) NULL DEFAULT NULL ,
`OWNER` VARCHAR(45) NULL DEFAULT NULL ,
`TENANT_ID` INT(11) NULL DEFAULT '0' ,
PRIMARY KEY (`ID`) ,
CONSTRAINT `fk_DM_DEVICE_DM_DEVICE_TYPE2`
FOREIGN KEY (`DEVICE_TYPE_ID` )
REFERENCES `DM_DEVICE_TYPE` (`ID` )
CREATE TABLE IF NOT EXISTS DM_DEVICE (
ID INT(11) AUTO_INCREMENT NOT NULL ,
DESCRIPTION TEXT NULL DEFAULT NULL ,
NAME VARCHAR(100) NULL DEFAULT NULL ,
DATE_OF_ENROLLMENT BIGINT(20) NULL DEFAULT NULL ,
DATE_OF_LAST_UPDATE BIGINT(20) NULL DEFAULT NULL ,
OWNERSHIP VARCHAR(45) NULL DEFAULT NULL ,
STATUS VARCHAR(15) NULL DEFAULT NULL ,
DEVICE_TYPE_ID INT(11) NULL DEFAULT NULL ,
DEVICE_IDENTIFICATION VARCHAR(300) NULL DEFAULT NULL ,
OWNER VARCHAR(45) NULL DEFAULT NULL ,
TENANT_ID INT(11) NULL DEFAULT '0' ,
PRIMARY KEY (ID) ,
CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2
FOREIGN KEY (DEVICE_TYPE_ID )
REFERENCES DM_DEVICE_TYPE (ID )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
@ -38,38 +42,41 @@ CREATE TABLE IF NOT EXISTS `DM_DEVICE` (
-- -----------------------------------------------------
-- Table `DM_PROFILE`
-- Table DM_PROFILE
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `DM_PROFILE` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`PROFILE_NAME` VARCHAR(45) NOT NULL ,
`TENANT_ID` INT NOT NULL ,
`DEVICE_TYPE_ID` INT NOT NULL ,
`CREATED_TIME` DATETIME NOT NULL ,
`UPDATED_TIME` DATETIME NOT NULL ,
PRIMARY KEY (`ID`) ,
CONSTRAINT `DM_PROFILE_DEVICE_TYPE`
FOREIGN KEY (`DEVICE_TYPE_ID` )
REFERENCES `DM_DEVICE_TYPE` (`ID` )
CREATE TABLE IF NOT EXISTS DM_PROFILE (
ID INT NOT NULL AUTO_INCREMENT ,
PROFILE_NAME VARCHAR(45) NOT NULL ,
TENANT_ID INT NOT NULL ,
DEVICE_TYPE_ID INT NOT NULL ,
CREATED_TIME DATETIME NOT NULL ,
UPDATED_TIME DATETIME NOT NULL ,
PRIMARY KEY (ID) ,
CONSTRAINT DM_PROFILE_DEVICE_TYPE
FOREIGN KEY (DEVICE_TYPE_ID )
REFERENCES DM_DEVICE_TYPE (ID )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
;
-- -----------------------------------------------------
-- Table `DM_POLICY`
-- Table DM_POLICY
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `DM_POLICY` (
`ID` INT(11) NOT NULL AUTO_INCREMENT ,
`NAME` VARCHAR(45) NULL DEFAULT NULL ,
`TENANT_ID` INT(11) NOT NULL ,
`PROFILE_ID` INT(11) NOT NULL ,
PRIMARY KEY (`ID`) ,
CONSTRAINT `FK_DM_PROFILE_DM_POLICY`
FOREIGN KEY (`PROFILE_ID` )
REFERENCES `DM_PROFILE` (`ID` )
CREATE TABLE IF NOT EXISTS DM_POLICY (
ID INT(11) NOT NULL AUTO_INCREMENT ,
NAME VARCHAR(45) NULL DEFAULT NULL ,
TENANT_ID INT(11) NOT NULL ,
PROFILE_ID INT(11) NOT NULL ,
PRIORITY INT NOT NULL ,
PRIMARY KEY (ID) ,
CONSTRAINT FK_DM_PROFILE_DM_POLICY
FOREIGN KEY (PROFILE_ID )
REFERENCES DM_PROFILE (ID )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
@ -77,137 +84,184 @@ CREATE TABLE IF NOT EXISTS `DM_POLICY` (
-- -----------------------------------------------------
-- Table `DM_DEVICE_POLICY`
-- Table DM_DEVICE_POLICY
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `DM_DEVICE_POLICY` (
`ID` INT(11) NOT NULL AUTO_INCREMENT ,
`DEVICE_ID` VARCHAR(20) NOT NULL ,
`POLICY_ID` INT(11) NOT NULL ,
PRIMARY KEY (`ID`) ,
CONSTRAINT `FK_POLICY_DEVICE_POLICY`
FOREIGN KEY (`POLICY_ID` )
REFERENCES `DM_POLICY` (`ID` )
CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY (
ID INT(11) NOT NULL AUTO_INCREMENT ,
DEVICE_ID INT(11) NOT NULL ,
POLICY_ID INT(11) NOT NULL ,
PRIMARY KEY (ID) ,
CONSTRAINT FK_POLICY_DEVICE_POLICY
FOREIGN KEY (POLICY_ID )
REFERENCES DM_POLICY (ID )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FK_DEVICE_DEVICE_POLICY`
FOREIGN KEY (`DEVICE_ID` )
REFERENCES `DM_DEVICE` (`ID` )
CONSTRAINT FK_DEVICE_DEVICE_POLICY
FOREIGN KEY (DEVICE_ID )
REFERENCES DM_DEVICE (ID )
ON DELETE NO ACTION
ON UPDATE NO ACTION);
ON UPDATE NO ACTION)
;
-- -----------------------------------------------------
-- Table `DM_DEVICE_TYPE_POLICY`
-- Table DM_DEVICE_TYPE_POLICY
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `DM_DEVICE_TYPE_POLICY` (
`ID` INT(11) NOT NULL ,
`DEVICE_TYPE_ID` INT(11) NOT NULL ,
`POLICY_ID` INT(11) NOT NULL ,
PRIMARY KEY (`ID`) ,
CONSTRAINT `FK_DEVICE_TYPE_POLICY`
FOREIGN KEY (`POLICY_ID` )
REFERENCES `DM_POLICY` (`ID` )
CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE_POLICY (
ID INT(11) NOT NULL ,
DEVICE_TYPE_ID INT(11) NOT NULL ,
POLICY_ID INT(11) NOT NULL ,
PRIMARY KEY (ID) ,
CONSTRAINT FK_DEVICE_TYPE_POLICY
FOREIGN KEY (POLICY_ID )
REFERENCES DM_POLICY (ID )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FK_DEVICE_TYPE_POLICY_DEVICE_TYPE`
FOREIGN KEY (`DEVICE_TYPE_ID` )
REFERENCES `DM_DEVICE_TYPE` (`ID` )
CONSTRAINT FK_DEVICE_TYPE_POLICY_DEVICE_TYPE
FOREIGN KEY (DEVICE_TYPE_ID )
REFERENCES DM_DEVICE_TYPE (ID )
ON DELETE NO ACTION
ON UPDATE NO ACTION);
ON UPDATE NO ACTION)
;
-- -----------------------------------------------------
-- Table `DM_FEATURES`
-- Table DM_FEATURES
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `DM_FEATURES` (
`ID` INT(11) NOT NULL AUTO_INCREMENT ,
`NAME` VARCHAR(256) NOT NULL ,
`CODE` VARCHAR(45) NULL DEFAULT NULL ,
`DESCRIPTION` TEXT NULL DEFAULT NULL ,
`EVALUVATION_RULE` VARCHAR(60) NOT NULL ,
PRIMARY KEY (`ID`) );
CREATE TABLE IF NOT EXISTS DM_FEATURES (
ID INT(11) NOT NULL AUTO_INCREMENT ,
NAME VARCHAR(256) NOT NULL ,
CODE VARCHAR(45) NULL DEFAULT NULL ,
DEVICE_TYPE_ID INT NOT NULL ,
DESCRIPTION TEXT NULL DEFAULT NULL ,
EVALUVATION_RULE VARCHAR(60) NOT NULL ,
PRIMARY KEY (ID) ,
CONSTRAINT DM_FEATURES_DEVICE_TYPE
FOREIGN KEY (DEVICE_TYPE_ID )
REFERENCES DM_DEVICE_TYPE (ID )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
;
-- -----------------------------------------------------
-- Table `DM_PROFILE_FEATURES`
-- Table DM_PROFILE_FEATURES
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `DM_PROFILE_FEATURES` (
`ID` INT(11) NOT NULL AUTO_INCREMENT ,
`PROFILE_ID` INT(11) NOT NULL ,
`FEATURE_ID` INT(11) NOT NULL ,
`CONTENT` BLOB NULL DEFAULT NULL ,
PRIMARY KEY (`ID`) ,
CONSTRAINT `fk_DM_POLICY_FEATURES_DM_FEATURES1`
FOREIGN KEY (`FEATURE_ID` )
REFERENCES `DM_FEATURES` (`ID` )
CREATE TABLE IF NOT EXISTS DM_PROFILE_FEATURES (
ID INT(11) NOT NULL AUTO_INCREMENT ,
PROFILE_ID INT(11) NOT NULL ,
FEATURE_ID INT(11) NOT NULL ,
CONTENT BLOB NULL DEFAULT NULL ,
PRIMARY KEY (ID) ,
CONSTRAINT FK_DM_POLICY_FEATURES_DM_FEATURES
FOREIGN KEY (FEATURE_ID )
REFERENCES DM_FEATURES (ID )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FK_DM_PROFILE_DM_POLICY_FEATURES`
FOREIGN KEY (`PROFILE_ID` )
REFERENCES `DM_PROFILE` (`ID` )
CONSTRAINT FK_DM_PROFILE_DM_POLICY_FEATURES
FOREIGN KEY (PROFILE_ID )
REFERENCES DM_PROFILE (ID )
ON DELETE NO ACTION
ON UPDATE NO ACTION);
ON UPDATE NO ACTION)
;
-- -----------------------------------------------------
-- Table `DM_ROLE_POLICY`
-- Table DM_ROLE_POLICY
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS DM_ROLE_POLICY (
`ID` INT(11) NOT NULL ,
`ROLE_NAME` VARCHAR(45) NOT NULL ,
`POLICY_ID` INT(11) NOT NULL ,
PRIMARY KEY (`ID`) ,
CONSTRAINT `FK_ROLE_POLICY_POLICY`
FOREIGN KEY (`POLICY_ID` )
REFERENCES `DM_POLICY` (`ID` )
ID INT(11) NOT NULL AUTO_INCREMENT ,
ROLE_NAME VARCHAR(45) NOT NULL ,
POLICY_ID INT(11) NOT NULL ,
PRIMARY KEY (ID) ,
CONSTRAINT FK_ROLE_POLICY_POLICY
FOREIGN KEY (POLICY_ID )
REFERENCES DM_POLICY (ID )
ON DELETE NO ACTION
ON UPDATE NO ACTION);
ON UPDATE NO ACTION)
;
-- -----------------------------------------------------
-- Table `DM_LOCATION`
-- Table DM_LOCATION
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS DM_LOCATION (
`LAT` VARCHAR(45) NOT NULL ,
`LONG` VARCHAR(45) NOT NULL ,
`POLICY_ID` INT(11) NOT NULL ,
CONSTRAINT `FK_DM_POLICY_DM_LOCATION`
FOREIGN KEY (`POLICY_ID` )
REFERENCES `DM_POLICY` (`ID` )
LATITUDE VARCHAR(45) NOT NULL ,
LONGITUDE VARCHAR(45) NOT NULL ,
POLICY_ID INT(11) NOT NULL ,
CONSTRAINT FK_DM_POLICY_DM_LOCATION
FOREIGN KEY (POLICY_ID )
REFERENCES DM_POLICY (ID )
ON DELETE NO ACTION
ON UPDATE NO ACTION);
ON UPDATE NO ACTION)
;
-- -----------------------------------------------------
-- Table `DM_TIME`
-- Table DM_TIME
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS DM_TIME (
`STARTING_TIME` DATETIME NOT NULL ,
`ENDING_TIME` DATETIME NOT NULL ,
`POLICY_ID` INT(11) NOT NULL ,
CONSTRAINT `FK_DM_POLICY_DM_TIME`
FOREIGN KEY (`POLICY_ID` )
REFERENCES `DM_POLICY` (`ID` )
STARTING_TIME DATETIME NOT NULL ,
ENDING_TIME DATETIME NOT NULL ,
POLICY_ID INT(11) NOT NULL ,
CONSTRAINT FK_DM_POLICY_DM_TIME
FOREIGN KEY (POLICY_ID )
REFERENCES DM_POLICY (ID )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
;
-- -----------------------------------------------------
-- Table DM_DATE
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS DM_DATE (
START_DATE DATE NOT NULL ,
END_DATE DATE NOT NULL ,
POLICY_ID INT NOT NULL ,
CONSTRAINT DM_DATE_POLICY
FOREIGN KEY (POLICY_ID )
REFERENCES DM_POLICY (ID )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
;
-- -----------------------------------------------------
-- Table .DM_USER_POLICY
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS DM_USER_POLICY (
ID INT NOT NULL AUTO_INCREMENT ,
POLICY_ID INT NOT NULL ,
USERNAME VARCHAR(45) NOT NULL ,
PRIMARY KEY (ID) ,
CONSTRAINT DM_POLICY_USER_POLICY
FOREIGN KEY (POLICY_ID )
REFERENCES DM_POLICY (ID )
ON DELETE NO ACTION
ON UPDATE NO ACTION);
ON UPDATE NO ACTION)
;

@ -12,7 +12,7 @@ USE `WSO2CDM` ;
DROP TABLE IF EXISTS `WSO2CDM`.`DM_DEVICE_TYPE` ;
CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE_TYPE` (
`ID` INT(11) NOT NULL ,
`ID` INT(11) NOT NULL AUTO_INCREMENT ,
`NAME` VARCHAR(300) NULL DEFAULT NULL ,
PRIMARY KEY (`ID`) )
ENGINE = InnoDB
@ -25,7 +25,7 @@ DEFAULT CHARACTER SET = latin1;
DROP TABLE IF EXISTS `WSO2CDM`.`DM_DEVICE` ;
CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE` (
`ID` VARCHAR(20) NOT NULL ,
`ID` INT(11) NOT NULL AUTO_INCREMENT ,
`DESCRIPTION` TEXT NULL DEFAULT NULL ,
`NAME` VARCHAR(100) NULL DEFAULT NULL ,
`DATE_OF_ENROLLMENT` BIGINT(20) NULL DEFAULT NULL ,
@ -98,7 +98,7 @@ DROP TABLE IF EXISTS `WSO2CDM`.`DM_DEVICE_POLICY` ;
CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE_POLICY` (
`ID` INT(11) NOT NULL AUTO_INCREMENT ,
`DEVICE_ID` VARCHAR(20) NOT NULL ,
`DEVICE_ID` INT(11) NOT NULL ,
`POLICY_ID` INT(11) NOT NULL ,
PRIMARY KEY (`ID`) ,
INDEX `FK_POLICY_DEVICE_POLICY` (`POLICY_ID` ASC) ,
@ -199,7 +199,7 @@ DEFAULT CHARACTER SET = latin1;
DROP TABLE IF EXISTS `WSO2CDM`.`DM_ROLE_POLICY` ;
CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_ROLE_POLICY` (
`ID` INT(11) NOT NULL ,
`ID` INT(11) NOT NULL AUTO_INCREMENT ,
`ROLE_NAME` VARCHAR(45) NOT NULL ,
`POLICY_ID` INT(11) NOT NULL ,
PRIMARY KEY (`ID`) ,
@ -219,8 +219,8 @@ DEFAULT CHARACTER SET = latin1;
DROP TABLE IF EXISTS `WSO2CDM`.`DM_LOCATION` ;
CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_LOCATION` (
`LAT` VARCHAR(45) NOT NULL ,
`LONG` VARCHAR(45) NOT NULL ,
`LATITUDE` VARCHAR(45) NOT NULL ,
`LONGITUDE` VARCHAR(45) NOT NULL ,
`POLICY_ID` INT(11) NOT NULL ,
INDEX `FK_DM_POLICY_DM_LOCATION` (`POLICY_ID` ASC) ,
CONSTRAINT `FK_DM_POLICY_DM_LOCATION`
@ -267,6 +267,25 @@ CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DATE` (
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `WSO2CDM`.`DM_USER_POLICY`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `WSO2CDM`.`DM_USER_POLICY` ;
CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_USER_POLICY` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`POLICY_ID` INT NOT NULL ,
`USERNAME` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`ID`) ,
INDEX `DM_POLICY_USER_POLICY` (`POLICY_ID` ASC) ,
CONSTRAINT `DM_POLICY_USER_POLICY`
FOREIGN KEY (`POLICY_ID` )
REFERENCES `WSO2CDM`.`DM_POLICY` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;

@ -25,7 +25,7 @@
<pwd>root</pwd>
</DBType>
<DBType typeName="H2">
<connectionurl>jdbc:h2:mem:cdm-test-db;DB_CLOSE_DELAY=-1</connectionurl>
<connectionurl>jdbc:h2:mem:WSO2_TEST_DB;DB_CLOSE_ON_EXIT=FALSE;LOCK_TIMEOUT=60000</connectionurl>
<driverclass>org.h2.Driver</driverclass>
<userName>wso2carbon</userName>
<pwd>wso2carbon</pwd>

@ -25,7 +25,7 @@ import org.wso2.carbon.policy.mgt.common.PIPDevice;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.PolicyManager;
import org.wso2.carbon.policy.mgt.core.PolicyManagerService;
import org.wso2.carbon.simple.policy.decision.point.internal.PolicyDecisionPointDataHolder;
import java.util.Collections;
@ -34,20 +34,20 @@ import java.util.List;
public class SimpleEvaluationImpl implements SimpleEvaluation {
private static final Log log = LogFactory.getLog(SimpleEvaluationImpl.class);
private PolicyManager policyManager;
private PolicyManagerService policyManagerService;
private List<Policy> policyList;
public SimpleEvaluationImpl() {
policyManager = PolicyDecisionPointDataHolder.getInstance().getPolicyManager();
policyManagerService = PolicyDecisionPointDataHolder.getInstance().getPolicyManagerService();
}
@Override
public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException {
try {
if (policyManager == null && policyList == null) {
PIPDevice pipDevice = policyManager.getPIP().getDeviceData(deviceIdentifier);
policyList = policyManager.getPIP().getRelatedPolicies(pipDevice);
if (policyManagerService == null && policyList == null) {
PIPDevice pipDevice = policyManagerService.getPIP().getDeviceData(deviceIdentifier);
policyList = policyManagerService.getPIP().getRelatedPolicies(pipDevice);
}
sortPolicy();
} catch (PolicyManagementException e) {

@ -18,13 +18,13 @@
package org.wso2.carbon.simple.policy.decision.point.internal;
import org.wso2.carbon.policy.mgt.core.PolicyManager;
import org.wso2.carbon.policy.mgt.core.PolicyManagerService;
import org.wso2.carbon.user.core.service.RealmService;
public class PolicyDecisionPointDataHolder {
private RealmService realmService;
private PolicyManager policyManager;
private PolicyManagerService policyManagerService;
private static PolicyDecisionPointDataHolder dataHolder = new PolicyDecisionPointDataHolder();
@ -43,11 +43,11 @@ public class PolicyDecisionPointDataHolder {
this.realmService = realmService;
}
public PolicyManager getPolicyManager() {
return policyManager;
public PolicyManagerService getPolicyManagerService() {
return policyManagerService;
}
public void setPolicyManager(PolicyManager policyManager) {
this.policyManager = policyManager;
public void setPolicyManagerService(PolicyManagerService policyManagerService) {
this.policyManagerService = policyManagerService;
}
}

@ -22,7 +22,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint;
import org.wso2.carbon.policy.mgt.core.PolicyManager;
import org.wso2.carbon.policy.mgt.core.PolicyManagerService;
import org.wso2.carbon.simple.policy.decision.point.PolicyEvaluationServiceImpl;
import org.wso2.carbon.user.core.service.RealmService;
@ -35,7 +35,7 @@ import org.wso2.carbon.user.core.service.RealmService;
* bind="setRealmService"
* unbind="unsetRealmService"
* @scr.reference name="org.wso2.carbon.devicemgt.policy.manager"
* interface="org.wso2.carbon.policy.mgt.core.PolicyManager"
* interface="org.wso2.carbon.policy.mgt.core.PolicyManagerService"
* cardinality="0..1"
* policy="dynamic"
* bind="setPolicyManagerService"
@ -91,19 +91,19 @@ public class PolicyEvaluationServiceComponent {
}
protected void setPolicyManagerService(PolicyManager policyManagerService) {
protected void setPolicyManagerService(PolicyManagerService policyManagerService) {
if (log.isDebugEnabled()) {
log.debug("Unsetting PolicyManager Service");
log.debug("Unsetting PolicyManagerService Service");
}
PolicyDecisionPointDataHolder.getInstance().setPolicyManager(policyManagerService);
PolicyDecisionPointDataHolder.getInstance().setPolicyManagerService(policyManagerService);
}
protected void unsetPolicyManagerService(PolicyManager policyManagerService) {
protected void unsetPolicyManagerService(PolicyManagerService policyManagerService) {
if (log.isDebugEnabled()) {
log.debug("Unsetting PolicyManager Service");
log.debug("Unsetting PolicyManagerService Service");
}
PolicyDecisionPointDataHolder.getInstance().setPolicyManager(null);
PolicyDecisionPointDataHolder.getInstance().setPolicyManagerService(null);
}
}

Loading…
Cancel
Save