From cf4a2f8fc4cd2e9cca97f1759800070bbaaa8c94 Mon Sep 17 00:00:00 2001 From: Geeth Munasinghe Date: Fri, 24 Apr 2015 18:46:46 +0530 Subject: [PATCH] Adding the final policy management classes, this includes the manager classes which manages the jta transactions from DAO, Test cases are added too --- .../carbon/policy/mgt/common/Feature.java | 4 +- .../carbon/policy/mgt/common/PIPDevice.java | 2 +- .../wso2/carbon/policy/mgt/common/Policy.java | 21 +- .../mgt/common/PolicyAdministratorPoint.java | 26 +- .../carbon/policy/mgt/common/PolicyDates.java | 43 ++ .../policy/mgt/common/PolicyLocations.java | 41 ++ .../carbon/policy/mgt/common/PolicyTimes.java | 43 ++ .../policy/mgt/common/ProfileFeature.java | 4 +- .../common/ProfileManagementException.java | 55 ++ ...Manager.java => PolicyManagerService.java} | 2 +- ...mpl.java => PolicyManagerServiceImpl.java} | 7 +- .../policy/mgt/core/dao/FeatureDAO.java | 4 + .../carbon/policy/mgt/core/dao/PolicyDAO.java | 43 +- .../core/dao/PolicyManagementDAOFactory.java | 92 ++- .../policy/mgt/core/dao/ProfileDAO.java | 5 +- .../mgt/core/dao/impl/FeatureDAOImpl.java | 194 ++++-- .../mgt/core/dao/impl/PolicyDAOImpl.java | 622 ++++++++++-------- .../mgt/core/dao/impl/ProfileDAOImpl.java | 195 ++---- .../dao/util/PolicyManagementDAOUtil.java | 4 + .../impl/PolicyAdministratorPointImpl.java | 145 ++-- .../core/impl/PolicyInformationPointImpl.java | 57 +- .../internal/PolicyManagementDataHolder.java | 10 + .../PolicyManagementServiceComponent.java | 25 +- .../policy/mgt/core/mgt/FeatureManager.java | 56 ++ .../policy/mgt/core/mgt/PolicyManager.java | 57 ++ .../policy/mgt/core/mgt/ProfileManager.java | 40 ++ .../mgt/core/mgt/impl/FeatureManagerImpl.java | 368 +++++++++++ .../mgt/core/mgt/impl/PolicyManagerImpl.java | 568 ++++++++++++++++ .../mgt/core/mgt/impl/ProfileManagerImpl.java | 283 ++++++++ .../core/service/PolicyManagementService.java | 35 +- .../policy/mgt/core/PolicyDAOTestCase.java | 169 ++++- .../policy/mgt/core/util/DeviceCreator.java | 71 ++ .../mgt/core/util/DeviceTypeCreator.java | 33 + .../policy/mgt/core/util/FeatureCreator.java | 13 +- .../policy/mgt/core/util/PolicyCreator.java | 81 +++ .../policy/mgt/core/util/ProfileCreator.java | 44 ++ .../mgt/core/util/ProfileFeatureCreator.java | 44 ++ .../src/test/resources/sql/CreateH2TestDB.sql | 288 ++++---- .../test/resources/sql/CreateMySqlTestDB.sql | 31 +- .../src/test/resources/testdbconfig.xml | 2 +- .../decision/point/SimpleEvaluationImpl.java | 12 +- .../PolicyDecisionPointDataHolder.java | 12 +- .../PolicyEvaluationServiceComponent.java | 16 +- 43 files changed, 3086 insertions(+), 781 deletions(-) create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyDates.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyLocations.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyTimes.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/ProfileManagementException.java rename components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/{PolicyManager.java => PolicyManagerService.java} (97%) rename components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/{PolicyManagerImpl.java => PolicyManagerServiceImpl.java} (95%) create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/FeatureManager.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/ProfileManager.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/DeviceCreator.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/DeviceTypeCreator.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/PolicyCreator.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileCreator.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileFeatureCreator.java diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Feature.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Feature.java index 87d6dcf034..90c1aae45a 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Feature.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Feature.java @@ -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; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PIPDevice.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PIPDevice.java index 5cdaa1f070..2605895a2c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PIPDevice.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PIPDevice.java @@ -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; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java index afe7b4abec..82d8601011 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java @@ -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 { 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 roleList; // Roles which this policy should be applied. private String ownershipType; // Ownership type (COPE, BYOD, CPE) private List DeviceList; // Individual devices this policy should be applied + private List users; /*Dynamic policy attributes*/ @@ -85,6 +86,14 @@ public class Policy implements Comparable { 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 { DeviceList = deviceList; } + public List getUsers() { + return users; + } + + public void setUsers(List users) { + this.users = users; + } + public int getStartTime() { return startTime; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java index 557647955b..dd29e6bf57 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java @@ -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 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 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 getPoliciesOfDevice(String deviceId, String deviceType) throws FeatureManagementException, PolicyManagementException; + List getPoliciesOfDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; /** * This method returns the device type specific policy. @@ -84,7 +83,7 @@ public interface PolicyAdministratorPoint { * @return Policy */ - List getPoliciesOfDeviceType(String deviceType) throws FeatureManagementException, PolicyManagementException; + List getPoliciesOfDeviceType(String deviceType) throws PolicyManagementException; /** * This method returns the role specific policy. @@ -93,7 +92,10 @@ public interface PolicyAdministratorPoint { * @return */ - List getPoliciesOfRole(String roleName) throws FeatureManagementException, PolicyManagementException; + List getPoliciesOfRole(String roleName) throws PolicyManagementException; + + + List 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; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyDates.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyDates.java new file mode 100644 index 0000000000..e10e58245a --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyDates.java @@ -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; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyLocations.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyLocations.java new file mode 100644 index 0000000000..9533c39ae1 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyLocations.java @@ -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; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyTimes.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyTimes.java new file mode 100644 index 0000000000..fedf615fdb --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyTimes.java @@ -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; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/ProfileFeature.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/ProfileFeature.java index 85075a0418..6c9f8498af 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/ProfileFeature.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/ProfileFeature.java @@ -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; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/ProfileManagementException.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/ProfileManagementException.java new file mode 100644 index 0000000000..a582034929 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/ProfileManagementException.java @@ -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); + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java similarity index 97% rename from components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManager.java rename to components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java index 533365b8a2..b38a5b2c01 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java @@ -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; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java similarity index 95% rename from components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerImpl.java rename to components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java index 919cf07673..39065e764e 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java @@ -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(); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java index 33de73c16e..3f2f130a83 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java @@ -30,6 +30,8 @@ public interface FeatureDAO { Feature addFeature(Feature feature) throws FeatureManagerDAOException; + List addFeatures(List 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 getAllFeatures() throws FeatureManagerDAOException; + List getAllProfileFeatures() throws FeatureManagerDAOException; + List getAllFeatures(String deviceType) throws FeatureManagerDAOException; List getFeaturesForProfile(int ProfileId) throws FeatureManagerDAOException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java index b61ff5a60f..5a63fdaf12 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java @@ -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 roleNames, Policy policy) throws PolicyManagerDAOException; - Policy addPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagerDAOException; + Policy addPolicyToUser(List usernameList, Policy policy) throws PolicyManagerDAOException; + + Policy addPolicyToDevice(List 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 getPolicy() throws PolicyManagerDAOException; + List getPolicyAppliedDevicesIds(int policyId) throws PolicyManagerDAOException; + + List getAllPolicies() throws PolicyManagerDAOException; + + List getPolicyOfDeviceType(String deviceType) throws PolicyManagerDAOException; + + List getPolicyIdsOfDevice(Device device) throws PolicyManagerDAOException; + + List getPolicyOfRole(String roleName) throws PolicyManagerDAOException; + + List getPolicyOfUser(String username) throws PolicyManagerDAOException; + + boolean deletePolicy(Policy policy) throws PolicyManagerDAOException; + + boolean deleteAllPolicyRelatedConfigs(int policyId) throws PolicyManagerDAOException; - List getPolicy(String deviceType) throws PolicyManagerDAOException; + List getPolicyAppliedRoles(int policyId) throws PolicyManagerDAOException; - List getPolicy(DeviceIdentifier deviceIdentifier) throws PolicyManagerDAOException; + PolicyTimes getTimesOfPolicy(Policy policy) throws PolicyManagerDAOException; - List getPolicyOfRole(String roleName) throws PolicyManagerDAOException; + PolicyDates getDatesOfPolicy(Policy policy) throws PolicyManagerDAOException; - void deletePolicy(Policy policy) throws PolicyManagerDAOException; + PolicyLocations getLocationsOfPolicy(Policy policy) throws PolicyManagerDAOException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java index 2df823b03c..ab0c5e3697 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java @@ -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 currentConnection = new ThreadLocal(); 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(); + } + } + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java index 32835c4e9a..811a52d1ac 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java @@ -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 getAllProfiles() throws ProfileManagerDAOException; - List getProfilesOfDeviceType(String deviceType) throws ProfileManagerDAOException; + List getProfilesOfDeviceType(DeviceType deviceType) throws ProfileManagerDAOException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java index 2af11af6a5..ed740f4ab6 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java @@ -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 addFeatures(List features) throws FeatureManagerDAOException { + + Connection conn; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + List featureList = new ArrayList(); + + try { + conn = this.getConnection(); + String query = "INSERT INTO DM_FEATURES (NAME, CODE, DESCRIPTION, 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 addProfileFeatures(List 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 updateProfileFeatures(List 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 getAllFeatures() throws FeatureManagerDAOException { - Connection conn = null; + + Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; - List featureList = new ArrayList(); 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 getAllProfileFeatures() throws FeatureManagerDAOException { + + Connection conn; + PreparedStatement stmt = null; + ResultSet resultSet = null; + List featureList = new ArrayList(); + + 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 getAllFeatures(String deviceType) throws FeatureManagerDAOException { - Connection conn = null; + Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; List featureList = new ArrayList(); + 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 getFeaturesForProfile(int profileId) throws FeatureManagerDAOException { - Connection conn = null; + + Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; - List featureList = new ArrayList(); 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."); + } + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java index 536c7d535c..21f29a8c62 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java @@ -20,29 +20,28 @@ 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.common.DeviceIdentifier; -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.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.core.dto.DeviceType; import org.wso2.carbon.policy.mgt.common.Policy; -import org.wso2.carbon.policy.mgt.common.Profile; -import org.wso2.carbon.policy.mgt.core.dao.*; +import org.wso2.carbon.policy.mgt.common.PolicyDates; +import org.wso2.carbon.policy.mgt.common.PolicyLocations; +import org.wso2.carbon.policy.mgt.common.PolicyTimes; +import org.wso2.carbon.policy.mgt.core.dao.PolicyDAO; +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 org.wso2.carbon.utils.multitenancy.MultitenantConstants; -import java.sql.*; +import java.sql.Connection; +import java.sql.Date; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class PolicyDAOImpl implements PolicyDAO { private static final Log log = LogFactory.getLog(PolicyDAOImpl.class); - DeviceDAOImpl deviceDAO = new DeviceDAOImpl(PolicyManagementDAOFactory.getDataSource()); - DeviceTypeDAO deviceTypeDAO = new DeviceTypeDAOImpl(PolicyManagementDAOFactory.getDataSource()); - ProfileDAO profileDAO = new ProfileDAOImpl(); - @Override public Policy addPolicy(Policy policy) throws PolicyManagerDAOException { @@ -52,19 +51,14 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public Policy addPolicy(String deviceType, Policy policy) throws PolicyManagerDAOException { - // First persist the policy to the data base. - persistPolicy(policy); - - Connection conn = null; + Connection conn; PreparedStatement stmt = null; - ResultSet resultSet = null; try { conn = this.getConnection(); String query = "INSERT INTO DM_DEVICE_TYPE_POLICY (DEVICE_TYPE_ID, POLICY_ID) VALUES (?, ?)"; stmt = conn.prepareStatement(query); stmt.setInt(1, getDeviceTypeId(deviceType)); stmt.setInt(2, policy.getId()); - stmt.executeQuery(); } catch (SQLException e) { @@ -72,106 +66,195 @@ public class PolicyDAOImpl implements PolicyDAO { log.error(msg, e); throw new PolicyManagerDAOException(msg, e); } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet); + PolicyManagementDAOUtil.cleanupResources(stmt, null); } - return policy; } @Override - public Policy addPolicyToRole(String roleName, Policy policy) throws PolicyManagerDAOException { - // First persist the policy to the data base. - persistPolicy(policy); + public Policy addPolicyToRole(List roleNames, Policy policy) throws PolicyManagerDAOException { - Connection conn = null; + Connection conn; PreparedStatement stmt = null; - ResultSet resultSet = null; try { conn = this.getConnection(); String query = "INSERT INTO DM_ROLE_POLICY (ROLE_NAME, POLICY_ID) VALUES (?, ?)"; stmt = conn.prepareStatement(query); - stmt.setString(1, roleName); - stmt.setInt(2, policy.getId()); + for (String role : roleNames) { + stmt.setString(1, role); + stmt.setInt(2, policy.getId()); + stmt.addBatch(); + } + stmt.executeBatch(); - stmt.executeQuery(); - policy.getRoleList().add(roleName); } catch (SQLException e) { String msg = "Error occurred while adding the role name with policy to database."; log.error(msg, e); throw new PolicyManagerDAOException(msg, e); } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet); + PolicyManagementDAOUtil.cleanupResources(stmt, null); } - return policy; } @Override - public Policy addPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagerDAOException { + public Policy addPolicyToUser(List usernameList, Policy policy) throws PolicyManagerDAOException { - // First persist the policy to the data base. - persistPolicy(policy); + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String query = "INSERT INTO DM_USER_POLICY (POLICY_ID, USERNAME) VALUES (?, ?)"; + stmt = conn.prepareStatement(query); + for (String username : usernameList) { + stmt.setInt(1, policy.getId()); + stmt.setString(2, username); + stmt.addBatch(); + } + stmt.executeBatch(); - Connection conn = null; + } catch (SQLException e) { + String msg = "Error occurred while adding the user name with policy to database."; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, null); + } + return policy; + } + + @Override + public Policy addPolicyToDevice(List devices, Policy policy) throws PolicyManagerDAOException { + + Connection conn; PreparedStatement stmt = null; - ResultSet generatedKeys = null; try { - Device device = deviceDAO.getDevice(deviceIdentifier); conn = this.getConnection(); String query = "INSERT INTO DM_DEVICE_POLICY (DEVICE_ID, POLICY_ID) VALUES (?, ?)"; stmt = conn.prepareStatement(query); - stmt.setInt(1, device.getId()); - stmt.setInt(2, policy.getId()); + for (Device device : devices) { + stmt.setInt(1, device.getId()); + stmt.setInt(2, policy.getId()); + stmt.addBatch(); + } + stmt.executeBatch(); + } catch (SQLException e) { + String msg = "Error occurred while adding the device ids with policy to database."; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, null); + } + return policy; + } + + @Override + public Policy addDatesToPolicy(Date startDate, Date endDate, Policy policy) throws PolicyManagerDAOException { + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String query = "INSERT INTO DM_DATE (START_DATE, END_DATE, POLICY_ID) VALUES (?, ?, ?)"; + stmt = conn.prepareStatement(query); + stmt.setDate(1, startDate); + stmt.setDate(2, endDate); + stmt.setInt(3, policy.getId()); stmt.executeUpdate(); - policy.getDeviceList().add(device); + } catch (SQLException e) { - String msg = "Error occurred while adding the device ids with policy to database."; + String msg = "Error occurred while adding the start date (" + startDate + ") and end date (" + + endDate + ") with policy to database."; log.error(msg, e); throw new PolicyManagerDAOException(msg, e); - } catch (DeviceManagementDAOException e) { - String msg = "Error occurred while reading the device data from the database."; + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, null); + } + return policy; + } + + @Override + public Policy addTimesToPolicy(int startTime, int endTime, Policy policy) throws PolicyManagerDAOException { + + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String query = "INSERT INTO DM_TIME (STARTING_TIME, ENDING_TIME, POLICY_ID) VALUES (?, ?, ?)"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, startTime); + stmt.setInt(2, endTime); + stmt.setInt(3, policy.getId()); + stmt.executeUpdate(); + + } catch (SQLException e) { + String msg = "Error occurred while adding the start time (" + startTime + ") and end time (" + + endTime + ") with policy to database."; log.error(msg, e); throw new PolicyManagerDAOException(msg, e); } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys); + PolicyManagementDAOUtil.cleanupResources(stmt, null); } + return policy; + } + @Override + public Policy addLocationToPolicy(String latitude, String longitude, Policy policy) throws PolicyManagerDAOException { + + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String query = "INSERT INTO DM_LOCATION (LATITUDE, LONGITUDE, POLICY_ID) VALUES (?, ?, ?)"; + stmt = conn.prepareStatement(query); + stmt.setString(1, latitude); + stmt.setString(2, longitude); + stmt.setInt(3, policy.getId()); + stmt.executeUpdate(); + + } catch (SQLException e) { + String msg = "Error occurred while adding the Location (" + latitude + ") (" + + longitude + ") with policy to database."; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, null); + } return policy; } @Override public Policy updatePolicy(Policy policy) throws PolicyManagerDAOException { - Connection conn = null; + Connection conn; PreparedStatement stmt = null; ResultSet generatedKeys = null; try { conn = this.getConnection(); - String query = "UPDATE DM_POLICY SET NAME= ?, TENANT_ID = ?, PROFILE_ID = ? WHERE ID = ?"; + String query = "UPDATE DM_POLICY SET NAME= ?, TENANT_ID = ?, PROFILE_ID = ?, PRIORITY = ? WHERE ID = ?"; stmt = conn.prepareStatement(query); stmt.setString(1, policy.getPolicyName()); stmt.setInt(2, policy.getTenantId()); stmt.setInt(3, policy.getProfile().getProfileId()); - stmt.setInt(4, policy.getId()); - + stmt.setInt(4, policy.getPriorityId()); + stmt.setInt(5, policy.getId()); stmt.executeUpdate(); + } catch (SQLException e) { - String msg = "Error occurred while updating policy " + policy.getPolicyName() + " (Policy Name) in database."; + String msg = "Error occurred while updating policy (" + policy.getPolicyName() + ") in database."; log.error(msg, e); throw new PolicyManagerDAOException(msg, e); } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys); + PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); } - return policy; } @Override public Policy getPolicy(int policyId) throws PolicyManagerDAOException { - Connection conn = null; + Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; Policy policy = new Policy(); @@ -180,38 +263,22 @@ public class PolicyDAOImpl implements PolicyDAO { String query = "SELECT * FROM DM_POLICY WHERE ID= ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, policyId); - resultSet = stmt.executeQuery(); while (resultSet.next()) { - Profile profile = profileDAO.getProfiles(resultSet.getInt("PROFILE_ID")); - List deviceList = getPolicyAppliedDevices(policyId); - List roleNames = getPolicyAppliedRoles(policyId); - policy.setId(policyId); policy.setPolicyName(resultSet.getString("NAME")); policy.setTenantId(resultSet.getInt("TENANT_ID")); - policy.setProfile(profile); - policy.setDeviceList(deviceList); - policy.setRoleList(roleNames); - - setDatesOfPolicy(policy); - setTimesOfPolicy(policy); - setLocationsOfPolicy(policy); } - return policy; + } catch (SQLException e) { String msg = "Error occurred while reading the policies from the database."; log.error(msg, e); throw new PolicyManagerDAOException(msg, e); - } catch (ProfileManagerDAOException e) { - String msg = "Error occurred while getting the profiles."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet); + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); } } @@ -219,7 +286,7 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public Policy getPolicyByProfileID(int profileId) throws PolicyManagerDAOException { - Connection conn = null; + Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; Policy policy = new Policy(); @@ -228,46 +295,30 @@ public class PolicyDAOImpl implements PolicyDAO { String query = "SELECT * FROM DM_POLICY WHERE PROFILE_ID= ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, profileId); - resultSet = stmt.executeQuery(); while (resultSet.next()) { - int policyId = resultSet.getInt("ID"); - Profile profile = profileDAO.getProfiles(profileId); - List deviceList = getPolicyAppliedDevices(policyId); - List roleNames = getPolicyAppliedRoles(policyId); - - policy.setId(policyId); + policy.setId(resultSet.getInt("ID")); policy.setPolicyName(resultSet.getString("NAME")); policy.setTenantId(resultSet.getInt("TENANT_ID")); - policy.setProfile(profile); - policy.setDeviceList(deviceList); - policy.setRoleList(roleNames); - - setDatesOfPolicy(policy); - setTimesOfPolicy(policy); - setLocationsOfPolicy(policy); } - return policy; + } catch (SQLException e) { String msg = "Error occurred while reading the policies from the database."; log.error(msg, e); throw new PolicyManagerDAOException(msg, e); - } catch (ProfileManagerDAOException e) { - String msg = "Error occurred while getting the profiles."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet); + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); } } @Override - public List getPolicy() throws PolicyManagerDAOException { + public List getAllPolicies() throws PolicyManagerDAOException { - Connection conn = null; + Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; List policies = new ArrayList(); @@ -275,150 +326,69 @@ public class PolicyDAOImpl implements PolicyDAO { conn = this.getConnection(); String query = "SELECT * FROM DM_POLICY"; stmt = conn.prepareStatement(query); - resultSet = stmt.executeQuery(); while (resultSet.next()) { Policy policy = new Policy(); - int policyId = resultSet.getInt("ID"); - Profile profile = profileDAO.getProfiles(resultSet.getInt("PROFILE_ID")); - List deviceList = getPolicyAppliedDevices(policyId); - List roleNames = getPolicyAppliedRoles(policyId); - - policy.setId(policyId); + policy.setId(resultSet.getInt("ID")); + policy.setProfileId(resultSet.getInt("PROFILE_ID")); policy.setPolicyName(resultSet.getString("NAME")); policy.setTenantId(resultSet.getInt("TENANT_ID")); - policy.setProfile(profile); - policy.setDeviceList(deviceList); - policy.setRoleList(roleNames); - - setDatesOfPolicy(policy); - setTimesOfPolicy(policy); - setLocationsOfPolicy(policy); - policies.add(policy); } return policies; + } catch (SQLException e) { String msg = "Error occurred while reading the policies from the database."; log.error(msg, e); throw new PolicyManagerDAOException(msg, e); - } catch (ProfileManagerDAOException e) { - String msg = "Error occurred while getting the profiles."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet); + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); } - } @Override - public List getPolicy(String deviceTypeName) throws PolicyManagerDAOException { - - Connection conn = null; - PreparedStatement stmt = null; - ResultSet resultSet = null; - List policies = new ArrayList(); - try { - DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName); - - // This gives the profiles related to given device types. - List profileList = profileDAO.getProfilesOfDeviceType(deviceTypeName); - - for (Profile profile : profileList) { - policies.add(getPolicyByProfileID(profile.getProfileId())); - } - - /* conn = this.getConnection(); - - // TODO : Change the query for device type - String query = "SELECT dp.ID PID, dp.NAME PNAME, dp.TENANT_ID PTD, dp.PROFILE_ID PPID FROM DM_POLICY dp " + - "INNER JOIN DM_PROFILE dpr ON dpr.ID = dp.PROFILE_ID WHERE dpr.ID = ?"; - stmt = conn.prepareStatement(query); - - resultSet = stmt.executeQuery(); - - //ID NAME TENANT_ID PROFILE_ID - while (resultSet.next()) { - Policy policy = new Policy(); - - int policyId = resultSet.getInt("PID"); - Profile profile = profileDAO.getProfiles(resultSet.getInt("PID")); - List deviceList = getPolicyAppliedDevices(policyId); - List roleNames = getPolicyAppliedRoles(policyId); - - policy.setId(policyId); - policy.setPolicyName(resultSet.getString("PNAME")); - policy.setTenantId(resultSet.getInt("PTD")); - policy.setProfile(profile); - policy.setDeviceList(deviceList); - policy.setRoleList(roleNames); - - setDatesOfPolicy(policy); - setTimesOfPolicy(policy); - setLocationsOfPolicy(policy); - - policies.add(policy); - }*/ - - return policies; - /* } catch (SQLException e) { - String msg = "Error occurred while reading the policies from the database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e);*/ - } catch (ProfileManagerDAOException e) { - String msg = "Error occurred while getting the profiles."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); - } catch (DeviceManagementDAOException e) { - String msg = "Error occurred while getting the device type."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); - } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet); - } - + public List getPolicyOfDeviceType(String deviceTypeName) throws PolicyManagerDAOException { + return null; } - // TODO : - private List getPolicyAppliedDevices(int policyId) throws PolicyManagerDAOException { + @Override + public List getPolicyAppliedDevicesIds(int policyId) throws PolicyManagerDAOException { - Connection conn = null; + Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; + List deviceIdList = new ArrayList(); - List deviceList = new ArrayList(); try { conn = this.getConnection(); String query = "SELECT * FROM DM_DEVICE_POLICY WHERE POLICY_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, policyId); - resultSet = stmt.executeQuery(); while (resultSet.next()) { - deviceList.add(deviceDAO.getDevice(resultSet.getInt("DEVICE_ID"))); + + deviceIdList.add(resultSet.getInt("DEVICE_ID")); } - return deviceList; + return deviceIdList; } catch (SQLException e) { String msg = "Error occurred while getting the device related to policies."; log.error(msg, e); throw new PolicyManagerDAOException(msg, e); - } catch (DeviceManagementDAOException e) { - String msg = "Error occurred while getting device data."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet); + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); } } - // TODO : - private List getPolicyAppliedRoles(int policyId) throws PolicyManagerDAOException { - Connection conn = null; + + public List getPolicyAppliedRoles(int policyId) throws PolicyManagerDAOException { + + Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; @@ -428,41 +398,46 @@ public class PolicyDAOImpl implements PolicyDAO { String query = "SELECT * FROM DM_ROLE_POLICY WHERE POLICY_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, policyId); - resultSet = stmt.executeQuery(); while (resultSet.next()) { + roleNames.add(resultSet.getString("ROLE_NAME")); } - return roleNames; + } catch (SQLException e) { String msg = "Error occurred while getting the roles related to policies."; log.error(msg, e); throw new PolicyManagerDAOException(msg, e); } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet); + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); } } - // TODO : - private void setTimesOfPolicy(Policy policy) throws PolicyManagerDAOException { - Connection conn = null; + public PolicyTimes getTimesOfPolicy(Policy policy) throws PolicyManagerDAOException { + + Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; + PolicyTimes times = new PolicyTimes(); try { conn = this.getConnection(); String query = "SELECT STARTING_TIME, ENDING_TIME FROM DM_TIME WHERE POLICY_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, policy.getId()); - resultSet = stmt.executeQuery(); while (resultSet.next()) { + //TODO: policy.setStartTime(resultSet.getInt("STARTING_TIME")); policy.setEndTime(resultSet.getInt("ENDING_TIME")); + + times.setStartTime(resultSet.getInt("STARTING_TIME")); + times.setEndTime(resultSet.getInt("ENDING_TIME")); } } catch (SQLException e) { @@ -470,28 +445,34 @@ public class PolicyDAOImpl implements PolicyDAO { log.error(msg, e); throw new PolicyManagerDAOException(msg, e); } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet); + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); } + return times; } - // TODO : - private void setDatesOfPolicy(Policy policy) throws PolicyManagerDAOException { - Connection conn = null; + public PolicyDates getDatesOfPolicy(Policy policy) throws PolicyManagerDAOException { + + Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; + PolicyDates dates = new PolicyDates(); try { conn = this.getConnection(); String query = "SELECT START_DATE, END_DATE FROM DM_DATE WHERE POLICY_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, policy.getId()); - resultSet = stmt.executeQuery(); while (resultSet.next()) { + //TODO: policy.setStartDate(resultSet.getDate("START_DATE")); policy.setEndDate(resultSet.getDate("END_DATE")); + + dates.setStartDate(resultSet.getDate("START_DATE")); + dates.setEndDate(resultSet.getDate("END_DATE")); } } catch (SQLException e) { @@ -499,28 +480,34 @@ public class PolicyDAOImpl implements PolicyDAO { log.error(msg, e); throw new PolicyManagerDAOException(msg, e); } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet); + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); } + return dates; } - // TODO: - private void setLocationsOfPolicy(Policy policy) throws PolicyManagerDAOException { - Connection conn = null; + public PolicyLocations getLocationsOfPolicy(Policy policy) throws PolicyManagerDAOException { + + Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; + PolicyLocations locations = new PolicyLocations(); try { conn = this.getConnection(); - String query = "SELECT LAT, LONG FROM DM_LOCATION WHERE POLICY_ID = ?"; + String query = "SELECT LATITUDE, LONGITUDE FROM DM_LOCATION WHERE POLICY_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, policy.getId()); - resultSet = stmt.executeQuery(); while (resultSet.next()) { - policy.setLatitude(resultSet.getString("LAT")); - policy.setLongitude(resultSet.getString("LONG")); + //TODO: + policy.setLatitude(resultSet.getString("LATITUDE")); + policy.setLongitude(resultSet.getString("LONGITUDE")); + + locations.setLatitude(resultSet.getString("LATITUDE")); + locations.setLongitude(resultSet.getString("LONGITUDE")); } } catch (SQLException e) { @@ -528,79 +515,108 @@ public class PolicyDAOImpl implements PolicyDAO { log.error(msg, e); throw new PolicyManagerDAOException(msg, e); } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet); + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); } + return locations; } @Override - public List getPolicy(DeviceIdentifier deviceIdentifier) throws PolicyManagerDAOException { + public List getPolicyIdsOfDevice(Device device) throws PolicyManagerDAOException { - Connection conn = null; + Connection conn; PreparedStatement stmt = null; - ResultSet generatedKeys = null; - List policies = new ArrayList(); + ResultSet resultSet = null; + List policyIds = new ArrayList(); + try { conn = this.getConnection(); String query = "SELECT * FROM DM_DEVICE_POLICY WHERE DEVICE_ID = ? "; stmt = conn.prepareStatement(query); - stmt.setInt(1, deviceDAO.getDevice(deviceIdentifier).getId().intValue()); - - generatedKeys = stmt.executeQuery(); + stmt.setInt(1, device.getId()); + resultSet = stmt.executeQuery(); - while (generatedKeys.next()){ - policies.add(getPolicy(generatedKeys.getInt("POLICY_ID"))); + while (resultSet.next()) { + policyIds.add(resultSet.getInt("POLICY_ID")); } - return policies; } catch (SQLException e) { String msg = "Error occurred while reading the device policy table."; log.error(msg, e); throw new PolicyManagerDAOException(msg, e); - } catch (DeviceManagementDAOException e) { - String msg = "Error occurred while getting device data from the device identifier."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys); + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); } - + return policyIds; } @Override - public List getPolicyOfRole(String roleName) throws PolicyManagerDAOException { + public List getPolicyOfRole(String roleName) throws PolicyManagerDAOException { - Connection conn = null; + Connection conn; PreparedStatement stmt = null; - ResultSet generatedKeys = null; - List policies = new ArrayList(); + ResultSet resultSet = null; + List policyIds = new ArrayList(); + try { conn = this.getConnection(); String query = "SELECT * FROM DM_ROLE_POLICY WHERE ROLE_NAME = ? "; stmt = conn.prepareStatement(query); stmt.setString(1, roleName); + resultSet = stmt.executeQuery(); - generatedKeys = stmt.executeQuery(); - - while (generatedKeys.next()){ - policies.add(getPolicy(generatedKeys.getInt("POLICY_ID"))); + while (resultSet.next()) { + policyIds.add(resultSet.getInt("POLICY_ID")); } - return policies; } catch (SQLException e) { String msg = "Error occurred while reading the role policy table."; log.error(msg, e); throw new PolicyManagerDAOException(msg, e); } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys); + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); } + return policyIds; } @Override - public void deletePolicy(Policy policy) throws PolicyManagerDAOException { + public List getPolicyOfUser(String username) throws PolicyManagerDAOException { - Connection conn = null; + Connection conn; PreparedStatement stmt = null; - ResultSet generatedKeys = null; + ResultSet resultSet = null; + List policyIds = new ArrayList(); + + try { + conn = this.getConnection(); + String query = "SELECT * FROM DM_USER_POLICY WHERE USERNAME = ? "; + stmt = conn.prepareStatement(query); + stmt.setString(1, username); + resultSet = stmt.executeQuery(); + + while (resultSet.next()) { + policyIds.add(resultSet.getInt("POLICY_ID")); + } + + } catch (SQLException e) { + String msg = "Error occurred while reading the user policy table."; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); + } + return policyIds; + } + + @Override + public boolean deletePolicy(Policy policy) throws PolicyManagerDAOException { + + Connection conn; + PreparedStatement stmt = null; + try { conn = this.getConnection(); String query = "DELETE FROM DM_POLICY WHERE ID = ?"; @@ -611,38 +627,96 @@ public class PolicyDAOImpl implements PolicyDAO { if (log.isDebugEnabled()) { log.debug("Policy (" + policy.getPolicyName() + ") delete from database."); } + return true; } catch (SQLException e) { String msg = "Unable to delete the policy (" + policy.getPolicyName() + ") from database."; log.error(msg); throw new PolicyManagerDAOException(msg, e); } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys); + PolicyManagementDAOUtil.cleanupResources(stmt, null); } + } + + @Override + public boolean deleteAllPolicyRelatedConfigs(int policyId) throws PolicyManagerDAOException { + + Connection conn; + PreparedStatement stmt = null; + + try { + conn = this.getConnection(); + + String userPolicy = "DELETE FROM DM_USER_POLICY WHERE POLICY_ID = ?"; + stmt = conn.prepareStatement(userPolicy); + stmt.setInt(1, policyId); + stmt.executeUpdate(); + + String rolePolicy = "DELETE FROM DM_ROLE_POLICY WHERE POLICY_ID = ?"; + stmt = conn.prepareStatement(rolePolicy); + stmt.setInt(1, policyId); + stmt.executeUpdate(); + + + String devicePolicy = "DELETE FROM DM_DEVICE_POLICY WHERE POLICY_ID = ?"; + stmt = conn.prepareStatement(devicePolicy); + stmt.setInt(1, policyId); + stmt.executeUpdate(); + + + String locationPolicy = "DELETE FROM DM_LOCATION WHERE POLICY_ID = ?"; + stmt = conn.prepareStatement(locationPolicy); + stmt.setInt(1, policyId); + stmt.executeUpdate(); + + String timePolicy = "DELETE FROM DM_TIME WHERE POLICY_ID = ?"; + stmt = conn.prepareStatement(timePolicy); + stmt.setInt(1, policyId); + stmt.executeUpdate(); + + + String datePolicy = "DELETE FROM DM_DATE WHERE POLICY_ID = ?"; + stmt = conn.prepareStatement(datePolicy); + stmt.setInt(1, policyId); + stmt.executeUpdate(); + + if (log.isDebugEnabled()) { + log.debug("Policy (" + policyId + ") related configs deleted from database."); + } + return true; + } catch (SQLException e) { + String msg = "Unable to delete the policy (" + policyId + ") related configs from database."; + log.error(msg); + throw new PolicyManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, null); + } } private Connection getConnection() throws PolicyManagerDAOException { + return PolicyManagementDAOFactory.getConnection(); + } + + private void closeConnection() { try { - return PolicyManagementDAOFactory.getDataSource().getConnection(); - } catch (SQLException e) { - throw new PolicyManagerDAOException("Error occurred while obtaining a connection from the policy " + - "management metadata repository datasource", e); + PolicyManagementDAOFactory.closeConnection(); + } catch (PolicyManagerDAOException e) { + log.warn("Unable to close the database connection."); } } private Policy persistPolicy(Policy policy) throws PolicyManagerDAOException { - Connection conn = null; + Connection conn; PreparedStatement stmt = null; ResultSet generatedKeys = null; - // TODO : find a way to get the tenant Id. - int tenantId = -1234; + int tenantId = MultitenantConstants.SUPER_TENANT_ID; try { conn = this.getConnection(); - String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY) VALUES (?, ?, ?)"; - stmt = conn.prepareStatement(query, stmt.RETURN_GENERATED_KEYS); + String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY) VALUES (?, ?, ?, ?)"; + stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); stmt.setString(1, policy.getPolicyName()); stmt.setInt(2, policy.getProfile().getProfileId()); @@ -665,33 +739,27 @@ public class PolicyDAOImpl implements PolicyDAO { throw new RuntimeException("No rows were inserted, policy id cannot be null."); } - profileDAO.addProfile(policy.getProfile()); - - return policy; } catch (SQLException e) { String msg = "Error occurred while adding policy to the database."; log.error(msg, e); throw new PolicyManagerDAOException(msg, e); - } catch (ProfileManagerDAOException e) { - String msg = "Error occurred while adding profile to the database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys); + PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); } + return policy; } /** * This method returns the device type id when supplied with device type name. * - * @param deviceType - * @return + * @param deviceType device type. + * @return integer value * @throws PolicyManagerDAOException */ private int getDeviceTypeId(String deviceType) throws PolicyManagerDAOException { - Connection conn = null; + Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; int deviceTypeId = -1; @@ -700,46 +768,52 @@ public class PolicyDAOImpl implements PolicyDAO { String query = "SELECT ID FROM DM_DEVICE_TYPE WHERE NAME = ?"; stmt = conn.prepareStatement(query); stmt.setString(1, deviceType); - resultSet = stmt.executeQuery(); while (resultSet.next()) { deviceTypeId = resultSet.getInt("ID"); } + } catch (SQLException e) { String msg = "Error occurred while selecting the device type id."; log.error(msg, e); throw new PolicyManagerDAOException(msg, e); } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet); + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); } return deviceTypeId; } - private int readHighestPriorityOfPolicies() throws PolicyManagerDAOException { + private int readHighestPriorityOfPolicies() throws PolicyManagerDAOException { - Connection conn = null; + Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; + int priority = 0; + try { conn = this.getConnection(); String query = "SELECT MAX(PRIORITY) PRIORITY FROM DM_POLICY;"; stmt = conn.prepareStatement(query); - resultSet = stmt.executeQuery(); while (resultSet.next()) { - return resultSet.getInt("PRIORITY"); + priority = resultSet.getInt("PRIORITY") + 1; } + if (log.isDebugEnabled()) { + log.debug("Priority of the new policy added is (" + priority + ")"); + } + } catch (SQLException e) { String msg = "Error occurred while reading the highest priority of the policies."; log.error(msg, e); throw new PolicyManagerDAOException(msg, e); } finally { - PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet); + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); } - return 0; + return priority; } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java index 36ac7a4b73..43a4e34df3 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java @@ -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."; + String msg = "Error occurred while updating the profile (" + profile.getProfileName() + ") 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 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 getAllProfiles() throws ProfileManagerDAOException { - Connection conn = null; + Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; List profileList = new ArrayList(); + 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 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 getProfilesOfDeviceType(String deviceTypeName) throws ProfileManagerDAOException { + public List getProfilesOfDeviceType(DeviceType deviceType) throws ProfileManagerDAOException { - Connection conn = null; + Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; List profileList = new ArrayList(); + 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 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."); + } + } + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/util/PolicyManagementDAOUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/util/PolicyManagementDAOUtil.java index f1079277a3..c23f6efc3f 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/util/PolicyManagementDAOUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/util/PolicyManagementDAOUtil.java @@ -69,4 +69,8 @@ public class PolicyManagementDAOUtil { } } + public static void cleanupResources(PreparedStatement stmt, ResultSet rs) { + cleanupResources(null, stmt, rs); + } + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java index 961a4b6365..fc2b6bb4d5 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java @@ -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 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 roleNames, Policy policy) throws PolicyManagementException { + return policyManager.addPolicyToRole(roleNames, policy); } @Override public List 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 getPoliciesOfDevice(String deviceId, String deviceType) - throws FeatureManagementException, PolicyManagementException { - return null; + public List getPoliciesOfDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { + return policyManager.getPoliciesOfDevice(deviceIdentifier); } @Override - public List 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 getPoliciesOfDeviceType(String deviceType) throws PolicyManagementException { + return policyManager.getPoliciesOfDeviceType(deviceType); } @Override - public List 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 getPoliciesOfRole(String roleName) throws PolicyManagementException { + return policyManager.getPoliciesOfRole(roleName); + } + + @Override + public List 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); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java index 905c096962..132679db9e 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java @@ -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> policies = new ArrayList>(); 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 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); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementDataHolder.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementDataHolder.java index 6269f62153..be2cb8a62f 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementDataHolder.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementDataHolder.java @@ -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; + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java index e0d2cea037..ccd6d42508 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java @@ -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); + } + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/FeatureManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/FeatureManager.java new file mode 100644 index 0000000000..529d9c0d1a --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/FeatureManager.java @@ -0,0 +1,56 @@ +/* +* Copyright (c) 2015 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you under the Apache License, +* Version 2.0 (the "License"); you may not use this file except +* in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +*/ + + +package org.wso2.carbon.policy.mgt.core.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 addFeatures(List 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 addProfileFeatures(List features, int profileId) throws FeatureManagementException; + + List updateProfileFeatures(List features, int profileId) throws FeatureManagementException; + + List getAllFeatures() throws FeatureManagementException; + + List getAllFeatures(String deviceType) throws FeatureManagementException; + + List getFeaturesForProfile(int profileId) throws FeatureManagementException; + + boolean deleteFeature(int featureId) throws FeatureManagementException; + + boolean deleteFeaturesOfProfile(Profile profile) throws FeatureManagementException; +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java new file mode 100644 index 0000000000..e11321d993 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java @@ -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 deviceIdentifierList, Policy policy) throws PolicyManagementException; + + Policy addPolicyToRole(List roleNames, Policy policy) throws PolicyManagementException; + + Policy addPolicyToUser(List usernameList, Policy policy) throws PolicyManagementException; + + Policy getPolicyByProfileID(int profileId) throws PolicyManagementException; + + Policy getPolicy(int policyId) throws PolicyManagementException; + + List getPolicies() throws PolicyManagementException; + + List getPoliciesOfDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; + + List getPoliciesOfDeviceType(String deviceType) throws PolicyManagementException; + + List getPoliciesOfRole(String roleName) throws PolicyManagementException; + + List getPoliciesOfUser(String username) throws PolicyManagementException; + + List getPolicyAppliedDevicesIds(int policyId) throws PolicyManagementException; +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/ProfileManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/ProfileManager.java new file mode 100644 index 0000000000..67757e546e --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/ProfileManager.java @@ -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 getAllProfiles() throws ProfileManagementException; + + List getProfilesOfDeviceType(String deviceType) throws ProfileManagementException; +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java new file mode 100644 index 0000000000..4d2e940178 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java @@ -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 addFeatures(List 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 addProfileFeatures(List 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 updateProfileFeatures(List 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 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 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 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; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java new file mode 100644 index 0000000000..b37c50ff9a --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -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 deviceIdentifierList, Policy policy) throws + PolicyManagementException { + + try { + PolicyManagementDAOFactory.beginTransaction(); + if (policy.getId() == 0) { + policyDAO.addPolicy(policy); + } + List deviceList = new ArrayList(); + 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 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 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 deviceList; + List 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 deviceList; + List 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 getPolicies() throws PolicyManagementException { + + List policyList; + + try { + policyList = policyDAO.getAllPolicies(); + List 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 getPoliciesOfDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { + + List policyIdList; + List policies = new ArrayList(); + try { + Device device = deviceDAO.getDevice(deviceIdentifier); + policyIdList = policyDAO.getPolicyIdsOfDevice(device); + List 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 getPoliciesOfDeviceType(String deviceTypeName) throws PolicyManagementException { + + List policies = new ArrayList(); + + try { + DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName); + List profileList = profileDAO.getProfilesOfDeviceType(deviceType); + List 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 getPoliciesOfRole(String roleName) throws PolicyManagementException { + + List policies = new ArrayList(); + List policyIdList; + + try { + policyIdList = policyDAO.getPolicyOfRole(roleName); + List 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 getPoliciesOfUser(String username) throws PolicyManagementException { + + List policies = new ArrayList(); + List policyIdList; + + try { + policyIdList = policyDAO.getPolicyOfUser(username); + List 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 getPolicyAppliedDevicesIds(int policyId) throws PolicyManagementException { + + List deviceList = new ArrayList(); + List 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; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java new file mode 100644 index 0000000000..de53ea3523 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java @@ -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 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 getAllProfiles() throws ProfileManagementException { + List profileList; + try { + profileList = profileDAO.getAllProfiles(); + List featureList = featureDAO.getAllProfileFeatures(); + List deviceTypes = deviceTypeDAO.getDeviceTypes(); + for (Profile profile : profileList) { + + List list = new ArrayList(); + 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 getProfilesOfDeviceType(String deviceTypeName) throws ProfileManagementException { + List profileList; + List featureList; + try { + DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName); + profileList = profileDAO.getProfilesOfDeviceType(deviceType); + featureList = featureDAO.getAllProfileFeatures(); + + for (Profile profile : profileList) { + List profileFeatureList = new ArrayList(); + 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; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java index d670d9a779..434c75b2d3 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java @@ -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 getPolicies(String deviceType) throws PolicyManagementException { - return policyManager.getPolicies(deviceType); + return policyManagerService.getPolicies(deviceType); } @Override public List 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(); } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java index 2c4d670cca..12312d12de 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java @@ -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 featureList; + private List profileFeatureList; + private Profile profile; + private Policy policy; + private List 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 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 roles = new ArrayList(); + 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 deviceIdentifierList = new ArrayList(); + 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 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 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 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 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()); + } + } + + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/DeviceCreator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/DeviceCreator.java new file mode 100644 index 0000000000..7f808bc83c --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/DeviceCreator.java @@ -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 deviceList = new ArrayList(); + + public static List 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); + } + + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/DeviceTypeCreator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/DeviceTypeCreator.java new file mode 100644 index 0000000000..e8e2188ddd --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/DeviceTypeCreator.java @@ -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; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/FeatureCreator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/FeatureCreator.java index ea06c80c87..439bb7f15a 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/FeatureCreator.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/FeatureCreator.java @@ -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 featureList = new ArrayList(); featureList.add(feature1); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/PolicyCreator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/PolicyCreator.java new file mode 100644 index 0000000000..112d00b5c9 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/PolicyCreator.java @@ -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 users = new ArrayList(); + 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 roles = new ArrayList(); + roles.add("Role_01"); + roles.add("Role_02"); + roles.add("Role_03"); + + policy.setRoleList(roles); + + List users = new ArrayList(); + 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; + } + + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileCreator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileCreator.java new file mode 100644 index 0000000000..0095f8d987 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileCreator.java @@ -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 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; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileFeatureCreator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileFeatureCreator.java new file mode 100644 index 0000000000..34a358d905 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileFeatureCreator.java @@ -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 getProfileFeature(List features) { + List profileFeatureList = new ArrayList(); + + 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; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql index 0fd55c652d..d6eb8f0a73 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql @@ -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); + 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) + ; + + diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql index a192fd4cce..e0959de557 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql @@ -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; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testdbconfig.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testdbconfig.xml index ee2d323f32..6241a0aefe 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testdbconfig.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testdbconfig.xml @@ -25,7 +25,7 @@ root - jdbc:h2:mem:cdm-test-db;DB_CLOSE_DELAY=-1 + jdbc:h2:mem:WSO2_TEST_DB;DB_CLOSE_ON_EXIT=FALSE;LOCK_TIMEOUT=60000 org.h2.Driver wso2carbon wso2carbon diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java index eb5b504b2e..8359768e5c 100644 --- a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java +++ b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java @@ -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 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) { diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/internal/PolicyDecisionPointDataHolder.java b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/internal/PolicyDecisionPointDataHolder.java index 30ba8466f2..7b5d104ea1 100644 --- a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/internal/PolicyDecisionPointDataHolder.java +++ b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/internal/PolicyDecisionPointDataHolder.java @@ -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; } } diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/internal/PolicyEvaluationServiceComponent.java b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/internal/PolicyEvaluationServiceComponent.java index 8d95706152..189534294f 100644 --- a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/internal/PolicyEvaluationServiceComponent.java +++ b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/internal/PolicyEvaluationServiceComponent.java @@ -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); } }