From e98674f9b95b89c3a37f1c1157e12d9c0347d32f Mon Sep 17 00:00:00 2001 From: inoshperera Date: Fri, 10 May 2019 07:57:39 +0530 Subject: [PATCH 1/6] init commit for MacOS --- .../jaxrs/api/impl/CertificateManagementAdminServiceImpl.java | 1 + 1 file changed, 1 insertion(+) diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/src/main/java/org/wso2/carbon/certificate/mgt/cert/jaxrs/api/impl/CertificateManagementAdminServiceImpl.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/src/main/java/org/wso2/carbon/certificate/mgt/cert/jaxrs/api/impl/CertificateManagementAdminServiceImpl.java index 5957b1b8cf..c1cb5c7197 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/src/main/java/org/wso2/carbon/certificate/mgt/cert/jaxrs/api/impl/CertificateManagementAdminServiceImpl.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/src/main/java/org/wso2/carbon/certificate/mgt/cert/jaxrs/api/impl/CertificateManagementAdminServiceImpl.java @@ -242,6 +242,7 @@ public class CertificateManagementAdminServiceImpl implements CertificateManagem String challengeToken = certMgtService.extractChallengeToken(cert); if (challengeToken != null) { + challengeToken = challengeToken.replace("\u0004&\u0013$",""); challengeToken = challengeToken.substring(challengeToken.indexOf("(") + 1).trim(); SCEPManager scepManager = CertificateMgtAPIUtils.getSCEPManagerService(); From 2da0a6aa1921bbf9f52119bd834ffec2b8e254c3 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Mon, 20 May 2019 11:54:13 +0530 Subject: [PATCH 2/6] Fix macos enrollment error due to invalid challenge token --- .../api/impl/CertificateManagementAdminServiceImpl.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/src/main/java/org/wso2/carbon/certificate/mgt/cert/jaxrs/api/impl/CertificateManagementAdminServiceImpl.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/src/main/java/org/wso2/carbon/certificate/mgt/cert/jaxrs/api/impl/CertificateManagementAdminServiceImpl.java index c1cb5c7197..697aadc1a8 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/src/main/java/org/wso2/carbon/certificate/mgt/cert/jaxrs/api/impl/CertificateManagementAdminServiceImpl.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/src/main/java/org/wso2/carbon/certificate/mgt/cert/jaxrs/api/impl/CertificateManagementAdminServiceImpl.java @@ -49,6 +49,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; @Path("/admin/certificates") public class CertificateManagementAdminServiceImpl implements CertificateManagementAdminService { @@ -242,7 +244,11 @@ public class CertificateManagementAdminServiceImpl implements CertificateManagem String challengeToken = certMgtService.extractChallengeToken(cert); if (challengeToken != null) { - challengeToken = challengeToken.replace("\u0004&\u0013$",""); + Pattern regexPattern = Pattern.compile("[a-zA-Z0-9][a-zA-Z0-9-]+$"); + Matcher regexMatcher = regexPattern.matcher(challengeToken); + if (regexMatcher.find()) { + challengeToken = regexMatcher.group(); + } challengeToken = challengeToken.substring(challengeToken.indexOf("(") + 1).trim(); SCEPManager scepManager = CertificateMgtAPIUtils.getSCEPManagerService(); From 650f02bf0341226f04527c63b990e81577e2276d Mon Sep 17 00:00:00 2001 From: Milan Perera Date: Sat, 8 Jun 2019 10:01:49 +0530 Subject: [PATCH 3/6] Add user created date and modified date --- .../device/mgt/jaxrs/beans/BasicUserInfo.java | 20 ++++++++++++++++++ .../impl/UserManagementServiceImpl.java | 21 ++++++++++++------- .../device/mgt/jaxrs/util/Constants.java | 2 ++ 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfo.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfo.java index d7c82a9edc..07e58252ca 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfo.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfo.java @@ -32,6 +32,10 @@ public class BasicUserInfo { private String lastname; @ApiModelProperty(name = "emailAddress", value = "The email address of the user.", required = true ) private String emailAddress; + @ApiModelProperty(name = "createdDate", value = "User creation date." ) + private String createdDate; + @ApiModelProperty(name = "modifiedDate", value = "User modifiedDate date." ) + private String modifiedDate; public String getUsername() { return username; @@ -65,4 +69,20 @@ public class BasicUserInfo { this.emailAddress = emailAddress; } + public String getCreatedDate() { + return createdDate; + } + + public void setCreatedDate(String createdDate) { + this.createdDate = createdDate; + } + + public String getModifiedDate() { + return modifiedDate; + } + + public void setModifiedDate(String modifiedDate) { + this.modifiedDate = modifiedDate; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java index 8bcb3d934b..090563af7b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java @@ -94,6 +94,7 @@ import java.net.URISyntaxException; import java.security.SecureRandom; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.Instant; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; @@ -152,7 +153,7 @@ public class UserManagementServiceImpl implements UserManagementService { Map defaultUserClaims = this.buildDefaultUserClaims(userInfo.getFirstname(), userInfo.getLastname(), - userInfo.getEmailAddress()); + userInfo.getEmailAddress(), true); // calling addUser method of carbon user api List tmpRoles = new ArrayList<>(); String[] userInfoRoles = userInfo.getRoles(); @@ -279,7 +280,7 @@ public class UserManagementServiceImpl implements UserManagementService { Map defaultUserClaims = this.buildDefaultUserClaims(userInfo.getFirstname(), userInfo.getLastname(), - userInfo.getEmailAddress()); + userInfo.getEmailAddress(), false); if (StringUtils.isNotEmpty(userInfo.getPassword())) { // Decoding Base64 encoded password userStoreManager.updateCredentialByAdmin(username, @@ -427,11 +428,7 @@ public class UserManagementServiceImpl implements UserManagementService { userList = new ArrayList<>(users.length); BasicUserInfo user; for (String username : users) { - user = new BasicUserInfo(); - user.setUsername(username); - user.setEmailAddress(getClaimValue(username, Constants.USER_CLAIM_EMAIL_ADDRESS)); - user.setFirstname(getClaimValue(username, Constants.USER_CLAIM_FIRST_NAME)); - user.setLastname(getClaimValue(username, Constants.USER_CLAIM_LAST_NAME)); + user = getBasicUserInfo(username); userList.add(user); } @@ -899,11 +896,17 @@ public class UserManagementServiceImpl implements UserManagementService { } } - private Map buildDefaultUserClaims(String firstName, String lastName, String emailAddress) { + private Map buildDefaultUserClaims(String firstName, String lastName, String emailAddress, + boolean isFresh) { Map defaultUserClaims = new HashMap<>(); defaultUserClaims.put(Constants.USER_CLAIM_FIRST_NAME, firstName); defaultUserClaims.put(Constants.USER_CLAIM_LAST_NAME, lastName); defaultUserClaims.put(Constants.USER_CLAIM_EMAIL_ADDRESS, emailAddress); + if (isFresh) { + defaultUserClaims.put(Constants.USER_CLAIM_CREATED, String.valueOf(Instant.now().getEpochSecond())); + } else { + defaultUserClaims.put(Constants.USER_CLAIM_MODIFIED, String.valueOf(Instant.now().getEpochSecond())); + } if (log.isDebugEnabled()) { log.debug("Default claim map is created for new user: " + defaultUserClaims.toString()); } @@ -936,6 +939,8 @@ public class UserManagementServiceImpl implements UserManagementService { userInfo.setEmailAddress(getClaimValue(username, Constants.USER_CLAIM_EMAIL_ADDRESS)); userInfo.setFirstname(getClaimValue(username, Constants.USER_CLAIM_FIRST_NAME)); userInfo.setLastname(getClaimValue(username, Constants.USER_CLAIM_LAST_NAME)); + userInfo.setCreatedDate(getClaimValue(username, Constants.USER_CLAIM_CREATED)); + userInfo.setModifiedDate(getClaimValue(username, Constants.USER_CLAIM_MODIFIED)); return userInfo; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/Constants.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/Constants.java index 4525cdd38d..29a9e5da25 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/Constants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/Constants.java @@ -26,6 +26,8 @@ public class Constants { public static final String USER_CLAIM_EMAIL_ADDRESS = "http://wso2.org/claims/emailaddress"; public static final String USER_CLAIM_FIRST_NAME = "http://wso2.org/claims/givenname"; public static final String USER_CLAIM_LAST_NAME = "http://wso2.org/claims/lastname"; + public static final String USER_CLAIM_CREATED = "http://wso2.org/claims/created"; + public static final String USER_CLAIM_MODIFIED = "http://wso2.org/claims/modified"; public static final String PRIMARY_USER_STORE = "PRIMARY"; public static final String DEFAULT_STREAM_VERSION = "1.0.0"; public static final String SCOPE = "scope"; From e26b50742be3f8f33dfef9c9e5e3ab3ee19cf5ba Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 13 Jun 2019 13:28:06 +0530 Subject: [PATCH 4/6] Increase length of VALUE_FIELD column in DM_DEVICE_INFO table --- .../src/test/resources/sql/h2.sql | 2 +- .../src/test/resources/sql-files/h2.sql | 2 +- .../src/test/resources/sql/CreateH2TestDB.sql | 2 +- .../src/main/resources/dbscripts/cdm/h2.sql | 2 +- .../src/main/resources/dbscripts/cdm/mssql.sql | 2 +- .../src/main/resources/dbscripts/cdm/mysql.sql | 2 +- .../src/main/resources/dbscripts/cdm/oracle.sql | 2 +- .../src/main/resources/dbscripts/cdm/postgresql.sql | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql index 232123d86f..6a7ff48814 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql @@ -416,7 +416,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO ( DEVICE_ID INT NULL, ENROLMENT_ID INT NOT NULL, KEY_FIELD VARCHAR(45) NULL, - VALUE_FIELD VARCHAR(100) NULL, + VALUE_FIELD VARCHAR(1000) NULL, PRIMARY KEY (ID), CONSTRAINT DM_DEVICE_INFO_DEVICE FOREIGN KEY (DEVICE_ID) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/sql-files/h2.sql b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/sql-files/h2.sql index e2d3356c69..c6a935cfc4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/sql-files/h2.sql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/sql-files/h2.sql @@ -400,7 +400,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INT NULL, KEY_FIELD VARCHAR(45) NULL, - VALUE_FIELD VARCHAR(100) NULL, + VALUE_FIELD VARCHAR(1000) NULL, PRIMARY KEY (ID), CONSTRAINT DM_DEVICE_INFO_DEVICE FOREIGN KEY (DEVICE_ID) 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 289ac3ec3e..e236efe7ff 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 @@ -467,7 +467,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO ( DEVICE_ID INT NULL, ENROLMENT_ID INT NOT NULL, KEY_FIELD VARCHAR(45) NULL, - VALUE_FIELD VARCHAR(500) NULL, + VALUE_FIELD VARCHAR(1000) NULL, PRIMARY KEY (ID), CONSTRAINT DM_DEVICE_INFO_DEVICE FOREIGN KEY (DEVICE_ID) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index 6e4989721a..abfdcad952 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -413,7 +413,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO ( DEVICE_ID INT NULL, ENROLMENT_ID INT NOT NULL, KEY_FIELD VARCHAR(45) NULL, - VALUE_FIELD VARCHAR(500) NULL, + VALUE_FIELD VARCHAR(1000) NULL, PRIMARY KEY (ID), CONSTRAINT DM_DEVICE_INFO_DEVICE FOREIGN KEY (DEVICE_ID) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index 8d6bb0fb7d..0cb27a0ebc 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -457,7 +457,7 @@ CREATE TABLE DM_DEVICE_INFO ( DEVICE_ID INTEGER NULL, ENROLMENT_ID INTEGER NOT NULL, KEY_FIELD VARCHAR(45) NULL, - VALUE_FIELD VARCHAR(500) NULL, + VALUE_FIELD VARCHAR(1000) NULL, PRIMARY KEY (ID), INDEX DM_DEVICE_INFO_DEVICE_idx (DEVICE_ID ASC), INDEX DM_DEVICE_INFO_DEVICE_ENROLLMENT_idx (ENROLMENT_ID ASC) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index 0ff005b6b9..65a5d235b4 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -472,7 +472,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO ( DEVICE_ID INT NULL, ENROLMENT_ID INT NOT NULL, KEY_FIELD VARCHAR(45) NULL, - VALUE_FIELD VARCHAR(500) NULL, + VALUE_FIELD VARCHAR(1000) NULL, PRIMARY KEY (ID), INDEX DM_DEVICE_INFO_DEVICE_idx (DEVICE_ID ASC), INDEX DM_DEVICE_INFO_DEVICE_ENROLLMENT_idx (ENROLMENT_ID ASC), diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index 9ebdcf5615..2382fe2b47 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -791,7 +791,7 @@ CREATE TABLE DM_DEVICE_INFO ( DEVICE_ID NUMBER(10) NOT NULL, ENROLMENT_ID NUMBER(10) NOT NULL, KEY_FIELD VARCHAR2(45) NULL, - VALUE_FIELD VARCHAR2(500) NULL, + VALUE_FIELD VARCHAR2(1000) NULL, PRIMARY KEY (ID), CONSTRAINT DM_DEVICE_INFO_DEVICE FOREIGN KEY (DEVICE_ID) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql index 4ef06be29c..26e7800a8f 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -417,7 +417,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO ( DEVICE_ID INT NOT NULL, ENROLMENT_ID INT NOT NULL, KEY_FIELD VARCHAR(45) NULL, - VALUE_FIELD VARCHAR(500) NULL, + VALUE_FIELD VARCHAR(1000) NULL, CONSTRAINT DM_DEVICE_INFO_DEVICE FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) From b30ea9a3f385514db8e8e7b44e4d93fe34e5f244 Mon Sep 17 00:00:00 2001 From: charitha Date: Mon, 17 Jun 2019 09:36:54 +0530 Subject: [PATCH 5/6] Fix https://gitlab.com/entgra/product-iots/issues/109 --- .../src/main/resources/dbscripts/cdm/h2.sql | 4 ++-- .../src/main/resources/dbscripts/cdm/mssql.sql | 4 ++-- .../src/main/resources/dbscripts/cdm/mysql.sql | 4 ++-- .../src/main/resources/dbscripts/cdm/oracle.sql | 4 ++-- .../src/main/resources/dbscripts/cdm/postgresql.sql | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index abfdcad952..736e6a1adb 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -12,7 +12,7 @@ CREATE TABLE IF NOT EXISTS DM_GROUP ( ID INTEGER AUTO_INCREMENT NOT NULL, GROUP_NAME VARCHAR(100) DEFAULT NULL, DESCRIPTION TEXT DEFAULT NULL, - OWNER VARCHAR(45) DEFAULT NULL, + OWNER VARCHAR(255) DEFAULT NULL, TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID) ); @@ -110,7 +110,7 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION ( CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, - OWNER VARCHAR(50) NOT NULL, + OWNER VARCHAR(255) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, DATE_OF_ENROLMENT TIMESTAMP DEFAULT NULL, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index 0cb27a0ebc..30e680a4c8 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -20,7 +20,7 @@ IF NOT EXISTS(SELECT * ID INTEGER IDENTITY (1, 1) NOT NULL, GROUP_NAME VARCHAR(100) DEFAULT NULL, DESCRIPTION VARCHAR(MAX) DEFAULT NULL, - OWNER VARCHAR(45) DEFAULT NULL, + OWNER VARCHAR(255) DEFAULT NULL, TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID) ); @@ -140,7 +140,7 @@ IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[D CREATE TABLE DM_ENROLMENT ( ID INTEGER IDENTITY(1,1) NOT NULL, DEVICE_ID INTEGER NOT NULL, - OWNER VARCHAR(50) NOT NULL, + OWNER VARCHAR(255) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, DATE_OF_ENROLMENT DATETIME2 DEFAULT NULL, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index 65a5d235b4..b8ef339105 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -16,7 +16,7 @@ CREATE TABLE IF NOT EXISTS DM_GROUP ( ID INTEGER AUTO_INCREMENT NOT NULL, GROUP_NAME VARCHAR(100) DEFAULT NULL, DESCRIPTION TEXT DEFAULT NULL, - OWNER VARCHAR(45) DEFAULT NULL, + OWNER VARCHAR(255) DEFAULT NULL, TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID) ) @@ -125,7 +125,7 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION ( CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, - OWNER VARCHAR(50) NOT NULL, + OWNER VARCHAR(255) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, DATE_OF_ENROLMENT TIMESTAMP NULL DEFAULT NULL, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index 2382fe2b47..77b51da66a 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -27,7 +27,7 @@ CREATE TABLE DM_GROUP ( ID NUMBER(10) NOT NULL, DESCRIPTION CLOB DEFAULT NULL, GROUP_NAME VARCHAR2(100) DEFAULT NULL, - OWNER VARCHAR2(45) DEFAULT NULL, + OWNER VARCHAR2(255) DEFAULT NULL, TENANT_ID NUMBER(10) DEFAULT 0, CONSTRAINT PK_DM_GROUP PRIMARY KEY (ID) ) @@ -201,7 +201,7 @@ CREATE TABLE DM_PROFILE_OPERATION ( CREATE TABLE DM_ENROLMENT ( ID NUMBER(10) NOT NULL, DEVICE_ID NUMBER(10) NOT NULL, - OWNER VARCHAR2(50) NOT NULL, + OWNER VARCHAR2(255) NOT NULL, OWNERSHIP VARCHAR2(45) DEFAULT NULL, STATUS VARCHAR2(50) NULL, DATE_OF_ENROLMENT TIMESTAMP(0) DEFAULT NULL, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql index 26e7800a8f..f1a05feabd 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -12,7 +12,7 @@ CREATE TABLE IF NOT EXISTS DM_GROUP ( ID BIGSERIAL NOT NULL PRIMARY KEY, GROUP_NAME VARCHAR(100) DEFAULT NULL, DESCRIPTION TEXT DEFAULT NULL, - OWNER VARCHAR(45) DEFAULT NULL, + OWNER VARCHAR(255) DEFAULT NULL, TENANT_ID INTEGER DEFAULT 0 ); @@ -108,7 +108,7 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION ( CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID BIGSERIAL NOT NULL PRIMARY KEY, DEVICE_ID INTEGER NOT NULL, - OWNER VARCHAR(50) NOT NULL, + OWNER VARCHAR(255) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, DATE_OF_ENROLMENT TIMESTAMP NULL DEFAULT NULL, From 58a31d0031bcb8ceb82ddb995b89b326210b29f1 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Tue, 25 Jun 2019 19:15:24 +0000 Subject: [PATCH 6/6] Plugin level support for deleting device permanently --- .../device/mgt/common/DeviceManager.java | 26 ++++ .../core/dao/impl/AbstractDeviceDAOImpl.java | 136 ++++++++---------- .../DeviceManagementProviderServiceImpl.java | 36 +++-- .../device/mgt/core/TestDeviceManager.java | 5 + .../type/template/DeviceTypeManager.java | 47 ++++++ .../template/dao/DeviceTypePluginDAOImpl.java | 48 +++++++ .../device/type/template/dao/PluginDAO.java | 19 +++ .../dao/PropertyBasedPluginDAOImpl.java | 38 +++++ .../mgt/core/mock/TypeXDeviceManager.java | 22 +++ 9 files changed, 290 insertions(+), 87 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManager.java index ebb225ae4f..5a2259fe40 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManager.java @@ -14,6 +14,23 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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.device.mgt.common; @@ -79,6 +96,15 @@ public interface DeviceManager { */ boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException; + /** + * Method to delete a particular device from CDM. + * + * @param deviceId Fully qualified device identifier + * @return A boolean indicating the status of the operation. + * @throws DeviceManagementException If some unusual behaviour is observed while deleting a device + */ + boolean deleteDevice(DeviceIdentifier deviceId, Device device) throws DeviceManagementException; + /** * Method to retrieve the status of the registration process of a particular device. * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index aa07aba8b4..866b230ed2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -1409,91 +1409,97 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } public void deleteDevice(DeviceIdentifier deviceIdentifier, int tenantId) throws DeviceManagementDAOException { + String deviceIdentifierId = deviceIdentifier.getId(); + String deviceType = deviceIdentifier.getType(); Connection conn; try { conn = this.getConnection(); int deviceId = getDeviceId(conn, deviceIdentifier, tenantId); if (deviceId == -1) { - String msg = "Device " + deviceIdentifier.getId() + " of type " + deviceIdentifier.getType() + - " is not found"; + String msg = "Device " + deviceIdentifierId + " of type " + deviceType + " is not found"; log.error(msg); throw new DeviceManagementDAOException(msg); } else { - int enrollmentId = getEnrollmentId(conn, deviceId, tenantId); - if (enrollmentId == -1) { - String msg = "Enrollment not found for the device " + deviceIdentifier.getId() + " of type " + - deviceIdentifier.getType(); + List enrollmentIds = getEnrollmentIds(conn, deviceId, tenantId); + if (enrollmentIds == null || enrollmentIds.isEmpty()) { + String msg = "Enrollments not found for the device " + deviceIdentifierId + " of type " + + deviceType; log.error(msg); throw new DeviceManagementDAOException(msg); } else { removeDeviceDetail(conn, deviceId); if (log.isDebugEnabled()) { - log.debug("Successfully removed device detail data"); + log.debug("Successfully removed device detail data of device " + deviceIdentifierId + + " of type " + deviceType); } removeDeviceLocation(conn, deviceId); if (log.isDebugEnabled()) { - log.debug("Successfully removed device location data"); + log.debug("Successfully removed device location data of device " + deviceIdentifierId + + " of type " + deviceType); } removeDeviceInfo(conn, deviceId); if (log.isDebugEnabled()) { - log.debug("Successfully removed device info data"); + log.debug("Successfully removed device info data of device " + deviceIdentifierId + + " of type " + deviceType); } removeDeviceNotification(conn, deviceId); if (log.isDebugEnabled()) { - log.debug("Successfully removed device notification data"); + log.debug("Successfully removed device notification data of device " + deviceIdentifierId + + " of type " + deviceType); } removeDeviceApplicationMapping(conn, deviceId); if (log.isDebugEnabled()) { - log.debug("Successfully removed device application mapping data"); + log.debug("Successfully removed device application mapping data of device " + + deviceIdentifierId + " of type " + deviceType); } removeDevicePolicyApplied(conn, deviceId); if (log.isDebugEnabled()) { - log.debug("Successfully removed device applied policy data"); + log.debug("Successfully removed device applied policy data of device " + deviceIdentifierId + + " of type " + deviceType); } removeDevicePolicy(conn, deviceId); if (log.isDebugEnabled()) { - log.debug("Successfully removed device policy data"); + log.debug("Successfully removed device policy data of device " + deviceIdentifierId + + " of type " + deviceType); } - removeEnrollmentDeviceDetail(conn, enrollmentId); if (log.isDebugEnabled()) { - log.debug("Successfully removed enrollment device detail data"); + log.debug("Starting to remove " + enrollmentIds.size() + " enrollment data of device " + + deviceIdentifierId + " of type " + deviceType); } - removeEnrollmentDeviceLocation(conn, enrollmentId); - if (log.isDebugEnabled()) { - log.debug("Successfully removed enrollment device location data"); - } - removeEnrollmentDeviceInfo(conn, enrollmentId); - if (log.isDebugEnabled()) { - log.debug("Successfully removed enrollment device info data"); - } - removeEnrollmentDeviceApplicationMapping(conn, enrollmentId); - if (log.isDebugEnabled()) { - log.debug("Successfully removed enrollment device application mapping data"); - } - removeDeviceOperationResponse(conn, enrollmentId); - if (log.isDebugEnabled()) { - log.debug("Successfully removed device operation response data"); + for (Integer enrollmentId : enrollmentIds) { + removeEnrollmentDeviceDetail(conn, enrollmentId); + removeEnrollmentDeviceLocation(conn, enrollmentId); + removeEnrollmentDeviceInfo(conn, enrollmentId); + removeEnrollmentDeviceApplicationMapping(conn, enrollmentId); + removeDeviceOperationResponse(conn, enrollmentId); + removeEnrollmentOperationMapping(conn, enrollmentId); } - removeEnrollmentOperationMapping(conn, enrollmentId); if (log.isDebugEnabled()) { - log.debug("Successfully removed enrollment operation mapping data"); + log.debug("Successfully removed enrollment device details, enrollment device location, " + + "enrollment device info, enrollment device application mapping, " + + "enrollment device operation response, enrollment operation mapping data of device " + + deviceIdentifierId + " of type " + deviceType); } removeDeviceEnrollment(conn, deviceId); if (log.isDebugEnabled()) { - log.debug("Successfully removed device enrollment data"); + log.debug("Successfully removed device enrollment data of device " + deviceIdentifierId + + " of type " + deviceType); } removeDeviceGroupMapping(conn, deviceId); if (log.isDebugEnabled()) { - log.debug("Successfully removed device group mapping data"); + log.debug("Successfully removed device group mapping data of device " + deviceIdentifierId + + " of type " + deviceType); } removeDevice(conn, deviceId); if (log.isDebugEnabled()) { - log.debug("Successfully permanently deleted the device"); + log.debug("Successfully permanently deleted the device of device " + deviceIdentifierId + + " of type " + deviceType); } } } } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while deleting the device", e); + throw new DeviceManagementDAOException("Error occurred while deleting the device " + deviceIdentifierId + + " of type " + deviceType, e); } } @@ -1519,30 +1525,29 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { return deviceId; } - private int getEnrollmentId(Connection conn, int deviceId, int tenantId) throws DeviceManagementDAOException { + private List getEnrollmentIds(Connection conn, int deviceId, int tenantId) throws DeviceManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; - int enrollmentId = -1; + List enrollmentIds = new ArrayList<>(); try { String sql = "SELECT ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, deviceId); stmt.setInt(2, tenantId); rs = stmt.executeQuery(); - if (rs.next()) { - enrollmentId = rs.getInt("ID"); + while (rs.next()) { + enrollmentIds.add(rs.getInt("ID")); } } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while retrieving enrollment id of the device", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } - return enrollmentId; + return enrollmentIds; } private void removeDeviceDetail(Connection conn, int deviceId) throws DeviceManagementDAOException { PreparedStatement stmt = null; - ResultSet rs = null; try { String sql = "DELETE FROM DM_DEVICE_DETAIL WHERE DEVICE_ID = ?"; stmt = conn.prepareStatement(sql); @@ -1551,13 +1556,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while removing device detail", e); } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + DeviceManagementDAOUtil.cleanupResources(stmt, null); } } private void removeDeviceLocation(Connection conn, int deviceId) throws DeviceManagementDAOException { PreparedStatement stmt = null; - ResultSet rs = null; try { String sql = "DELETE FROM DM_DEVICE_LOCATION WHERE DEVICE_ID = ?"; stmt = conn.prepareStatement(sql); @@ -1566,13 +1570,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while removing device location", e); } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + DeviceManagementDAOUtil.cleanupResources(stmt, null); } } private void removeDeviceInfo(Connection conn, int deviceId) throws DeviceManagementDAOException { PreparedStatement stmt = null; - ResultSet rs = null; try { String sql = "DELETE FROM DM_DEVICE_INFO WHERE DEVICE_ID = ?"; stmt = conn.prepareStatement(sql); @@ -1581,13 +1584,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while removing device info", e); } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + DeviceManagementDAOUtil.cleanupResources(stmt, null); } } private void removeDeviceNotification(Connection conn, int deviceId) throws DeviceManagementDAOException { PreparedStatement stmt = null; - ResultSet rs = null; try { String sql = "DELETE FROM DM_NOTIFICATION WHERE DEVICE_ID = ?"; stmt = conn.prepareStatement(sql); @@ -1596,13 +1598,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while removing device notification", e); } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + DeviceManagementDAOUtil.cleanupResources(stmt, null); } } private void removeDeviceApplicationMapping(Connection conn, int deviceId) throws DeviceManagementDAOException { PreparedStatement stmt = null; - ResultSet rs = null; try { String sql = "DELETE FROM DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ?"; stmt = conn.prepareStatement(sql); @@ -1611,13 +1612,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while removing device application mapping", e); } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + DeviceManagementDAOUtil.cleanupResources(stmt, null); } } private void removeDevicePolicyApplied(Connection conn, int deviceId) throws DeviceManagementDAOException { PreparedStatement stmt = null; - ResultSet rs = null; try { String sql = "DELETE FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ?"; stmt = conn.prepareStatement(sql); @@ -1626,13 +1626,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while removing device policy applied", e); } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + DeviceManagementDAOUtil.cleanupResources(stmt, null); } } private void removeDevicePolicy(Connection conn, int deviceId) throws DeviceManagementDAOException { PreparedStatement stmt = null; - ResultSet rs = null; try { String sql = "DELETE FROM DM_DEVICE_POLICY WHERE DEVICE_ID = ?"; stmt = conn.prepareStatement(sql); @@ -1641,13 +1640,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while removing device policy", e); } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + DeviceManagementDAOUtil.cleanupResources(stmt, null); } } private void removeEnrollmentDeviceDetail(Connection conn, int enrollmentId) throws DeviceManagementDAOException { PreparedStatement stmt = null; - ResultSet rs = null; try { String sql = "DELETE FROM DM_DEVICE_DETAIL WHERE ENROLMENT_ID = ?"; stmt = conn.prepareStatement(sql); @@ -1656,13 +1654,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while removing enrollment device detail", e); } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + DeviceManagementDAOUtil.cleanupResources(stmt, null); } } private void removeEnrollmentDeviceLocation(Connection conn, int enrollmentId) throws DeviceManagementDAOException { PreparedStatement stmt = null; - ResultSet rs = null; try { String sql = "DELETE FROM DM_DEVICE_LOCATION WHERE ENROLMENT_ID = ?"; stmt = conn.prepareStatement(sql); @@ -1671,13 +1668,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while removing enrollment device location", e); } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + DeviceManagementDAOUtil.cleanupResources(stmt, null); } } private void removeEnrollmentDeviceInfo(Connection conn, int enrollmentId) throws DeviceManagementDAOException { PreparedStatement stmt = null; - ResultSet rs = null; try { String sql = "DELETE FROM DM_DEVICE_INFO WHERE ENROLMENT_ID = ?"; stmt = conn.prepareStatement(sql); @@ -1686,14 +1682,13 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while removing enrollment device info", e); } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + DeviceManagementDAOUtil.cleanupResources(stmt, null); } } private void removeEnrollmentDeviceApplicationMapping(Connection conn, int enrollmentId) throws DeviceManagementDAOException { PreparedStatement stmt = null; - ResultSet rs = null; try { String sql = "DELETE FROM DM_DEVICE_APPLICATION_MAPPING WHERE ENROLMENT_ID = ?"; stmt = conn.prepareStatement(sql); @@ -1703,13 +1698,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { throw new DeviceManagementDAOException("Error occurred while removing enrollment device application " + "mapping", e); } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + DeviceManagementDAOUtil.cleanupResources(stmt, null); } } private void removeDeviceOperationResponse(Connection conn, int enrollmentId) throws DeviceManagementDAOException { PreparedStatement stmt = null; - ResultSet rs = null; try { String sql = "DELETE FROM DM_DEVICE_OPERATION_RESPONSE WHERE ENROLMENT_ID = ?"; stmt = conn.prepareStatement(sql); @@ -1718,14 +1712,13 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while removing device operation response", e); } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + DeviceManagementDAOUtil.cleanupResources(stmt, null); } } private void removeEnrollmentOperationMapping(Connection conn, int enrollmentId) throws DeviceManagementDAOException { PreparedStatement stmt = null; - ResultSet rs = null; try { String sql = "DELETE FROM DM_ENROLMENT_OP_MAPPING WHERE ENROLMENT_ID = ?"; stmt = conn.prepareStatement(sql); @@ -1734,13 +1727,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while removing enrollment operation mapping", e); } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + DeviceManagementDAOUtil.cleanupResources(stmt, null); } } private void removeDeviceEnrollment(Connection conn, int deviceId) throws DeviceManagementDAOException { PreparedStatement stmt = null; - ResultSet rs = null; try { String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ?"; stmt = conn.prepareStatement(sql); @@ -1749,13 +1741,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while removing device enrollment", e); } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + DeviceManagementDAOUtil.cleanupResources(stmt, null); } } private void removeDeviceGroupMapping(Connection conn, int deviceId) throws DeviceManagementDAOException { PreparedStatement stmt = null; - ResultSet rs = null; try { String sql = "DELETE FROM DM_DEVICE_GROUP_MAP WHERE DEVICE_ID = ?"; stmt = conn.prepareStatement(sql); @@ -1764,13 +1755,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while removing device group mapping", e); } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + DeviceManagementDAOUtil.cleanupResources(stmt, null); } } private void removeDevice(Connection conn, int deviceId) throws DeviceManagementDAOException { PreparedStatement stmt = null; - ResultSet rs = null; try { String sql = "DELETE FROM DM_DEVICE WHERE ID = ?"; stmt = conn.prepareStatement(sql); @@ -1779,7 +1769,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while removing device", e); } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + DeviceManagementDAOUtil.cleanupResources(stmt, null); } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 621cc966e3..76e86420d3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -14,23 +14,23 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. - */ -/* - * Copyright (c) 2019, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved. * - * Entgra (pvt) Ltd. 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 + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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. + * 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.device.mgt.core.service; @@ -550,6 +550,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { DeviceManagementDAOFactory.beginTransaction(); deviceDAO.deleteDevice(deviceId, tenantId); + try { + deviceManager.deleteDevice(deviceId, device); + } catch (DeviceManagementException e) { + String msg = "Error occurred while permanently deleting '" + deviceId.getType() + + "' device with the identifier '" + deviceId.getId() + "' in plugin."; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } DeviceManagementDAOFactory.commitTransaction(); this.removeDeviceFromCache(deviceId); } catch (DeviceManagementDAOException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java index 47a01c9e59..8675858f8b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java @@ -59,6 +59,11 @@ public class TestDeviceManager implements DeviceManager { return true; } + @Override + public boolean deleteDevice(DeviceIdentifier deviceId, Device device) throws DeviceManagementException { + return true; + } + @Override public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { return true; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManager.java index b84d165814..948e5a5ff6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManager.java @@ -15,6 +15,22 @@ * specific language governing permissions and limitations * under the License. * + * + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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.device.mgt.extensions.device.type.template; @@ -568,4 +584,35 @@ public class DeviceTypeManager implements DeviceManager { return null; } + @Override + public boolean deleteDevice(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException { + if (propertiesExist) { + boolean status; + Device existingDevice = this.getDevice(deviceIdentifier); + if (existingDevice == null) { + return false; + } + try { + if (log.isDebugEnabled()) { + log.debug("Deleting the details of " + deviceType + " device : " + device.getDeviceIdentifier()); + } + deviceTypePluginDAOManager.getDeviceTypeDAOHandler().beginTransaction(); + status = deviceTypePluginDAOManager.getDeviceDAO().deleteDevice(existingDevice); + deviceTypePluginDAOManager.getDeviceTypeDAOHandler().commitTransaction(); + } catch (DeviceTypeMgtPluginException e) { + try { + deviceTypePluginDAOManager.getDeviceTypeDAOHandler().rollbackTransaction(); + } catch (DeviceTypeMgtPluginException e1) { + log.warn("Error occurred while roll back the delete device info transaction : '" + + device.toString() + "'", e1); + } + throw new DeviceManagementException( + "Error occurred while deleting the " + deviceType + " device: '" + + device.getDeviceIdentifier() + "'", e); + } + return status; + } + return true; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypePluginDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypePluginDAOImpl.java index 755cb0397b..00c9e4ff40 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypePluginDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypePluginDAOImpl.java @@ -14,6 +14,23 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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.device.mgt.extensions.device.type.template.dao; @@ -45,6 +62,7 @@ public class DeviceTypePluginDAOImpl implements PluginDAO { private String createDBqueryForAddDevice; private String updateDBQueryForUpdateDevice; private String selectDBQueryToGetAllDevice; + private String deleteDBQueryForDeleteDevice; public DeviceTypePluginDAOImpl(DeviceDAODefinition deviceDAODefinition, DeviceTypeDAOHandler deviceTypeDAOHandler) { @@ -196,6 +214,33 @@ public class DeviceTypePluginDAOImpl implements PluginDAO { } } + @Override + public boolean deleteDevice(Device device) throws DeviceTypeMgtPluginException { + boolean status = false; + Connection conn; + PreparedStatement stmt = null; + try { + conn = deviceTypeDAOHandler.getConnection(); + stmt = conn.prepareStatement(deleteDBQueryForDeleteDevice); + stmt.setString(1, device.getDeviceIdentifier()); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + if (log.isDebugEnabled()) { + log.debug("Device " + device.getDeviceIdentifier() + " data has been deleted."); + } + } + } catch (SQLException e) { + String msg = "Error occurred while deleting the device '" + device.getDeviceIdentifier() + "' data in " + + deviceDAODefinition.getDeviceTableName(); + log.error(msg, e); + throw new DeviceTypeMgtPluginException(msg, e); + } finally { + DeviceTypeUtils.cleanupResources(stmt, null); + } + return status; + } + private String getDeviceTableColumnNames() { return StringUtils.join(deviceDAODefinition.getColumnNames(), ", "); } @@ -239,5 +284,8 @@ public class DeviceTypePluginDAOImpl implements PluginDAO { selectDBQueryToGetAllDevice = "SELECT " + getDeviceTableColumnNames() + "," + deviceDAODefinition.getPrimaryKey() + " FROM " + deviceDAODefinition.getDeviceTableName(); + + deleteDBQueryForDeleteDevice = "DELETE FROM " + deviceDAODefinition.getDeviceTableName() + " WHERE " + + deviceDAODefinition.getPrimaryKey() + " = ?"; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PluginDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PluginDAO.java index 47606fe6a6..bd1747d454 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PluginDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PluginDAO.java @@ -14,6 +14,23 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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.device.mgt.extensions.device.type.template.dao; @@ -31,4 +48,6 @@ public interface PluginDAO { boolean updateDevice(Device device) throws DeviceTypeMgtPluginException; List getAllDevices() throws DeviceTypeMgtPluginException; + + boolean deleteDevice(Device device) throws DeviceTypeMgtPluginException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PropertyBasedPluginDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PropertyBasedPluginDAOImpl.java index ac90fb1052..1c38884ca7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PropertyBasedPluginDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PropertyBasedPluginDAOImpl.java @@ -14,6 +14,23 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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.device.mgt.extensions.device.type.template.dao; @@ -201,6 +218,27 @@ public class PropertyBasedPluginDAOImpl implements PluginDAO { } } + @Override + public boolean deleteDevice(Device device) throws DeviceTypeMgtPluginException { + Connection conn; + PreparedStatement stmt = null; + try { + conn = deviceTypeDAOHandler.getConnection(); + stmt = conn.prepareStatement("DELETE FROM DM_DEVICE_PROPERTIES WHERE DEVICE_IDENTIFICATION = ?"); + stmt.setString(1, device.getDeviceIdentifier()); + stmt.executeUpdate(); + return true; + } catch (SQLException e) { + String msg = "Error occurred while deleting the device '" + device.getDeviceIdentifier() + "' data on" + + deviceType; + log.error(msg, e); + throw new DeviceTypeMgtPluginException(msg, e); + } finally { + DeviceTypeUtils.cleanupResources(stmt, null); + } + } + + private String getPropertyValue(List properties, String propertyName) { for (Device.Property property : properties) { if (property.getName() != null && property.getName().equals(propertyName)) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mock/TypeXDeviceManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mock/TypeXDeviceManager.java index d9477b5119..f303b15c7f 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mock/TypeXDeviceManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mock/TypeXDeviceManager.java @@ -14,6 +14,23 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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.mock; @@ -61,6 +78,11 @@ public class TypeXDeviceManager implements DeviceManager { return false; } + @Override + public boolean deleteDevice(DeviceIdentifier deviceId, Device device) throws DeviceManagementException { + return false; + } + @Override public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { return false;