From 008b24dc37629bf3528ae685cefd8eebd494925f Mon Sep 17 00:00:00 2001 From: amanda <74random.amanda@gmail.com> Date: Wed, 6 May 2020 06:50:38 +0530 Subject: [PATCH] Add VPP DAOs --- .../mgt/common/dto/ApplicationDTO.java | 14 +++- .../mgt/common/dto/LicenseDTO.java | 56 +++++++++++++ .../mgt/common/dto/VPPDeviceDTO.java | 46 ++++++++++ .../mgt/core/dao/ApplicationDAO.java | 8 +- .../GenericApplicationDAOImpl.java | 83 ++++++++++++++++++- .../mgt/core/impl/ApplicationManagerImpl.java | 9 ++ .../main/resources/conf/application-mgt.xml | 1 + .../dbscripts/cdm/application-mgt/mysql.sql | 32 +++---- 8 files changed, 232 insertions(+), 17 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/LicenseDTO.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/VPPDeviceDTO.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/ApplicationDTO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/ApplicationDTO.java index f924e2774fb..c2c617ac492 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/ApplicationDTO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/ApplicationDTO.java @@ -97,9 +97,13 @@ public class ApplicationDTO { private String packageName; @ApiModelProperty(name = "adamId", - value = "adamID is the asset ID given by iTunes") + value = "The unique identifier for a product in the iTunes Store") private String adamId; + @ApiModelProperty(name = "licenses", + value = "List of application licenses") + private List licenses; + public String getAdamId() { return adamId; } @@ -108,6 +112,14 @@ public class ApplicationDTO { this.adamId = adamId; } + public List getLicenses() { + return licenses; + } + + public void setLicenses(List licenses) { + this.licenses = licenses; + } + public String getPackageName() { return packageName; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/LicenseDTO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/LicenseDTO.java new file mode 100644 index 00000000000..7b35233f6ed --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/LicenseDTO.java @@ -0,0 +1,56 @@ +package org.wso2.carbon.device.application.mgt.common.dto; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +@ApiModel(value = "LicenseDTO", description = "LicenseDTO represents License details.") +public class LicenseDTO { + @ApiModelProperty(name = "id", + value = "The ID given to the license when it is stored in the APPM database") + private int id; + + @ApiModelProperty(name = "licenseId", + value = "The identifier of the assigned license") + private String licenseId; + + @ApiModelProperty(name = "adamId", + value = "The unique identifier for a product in the iTunes Store") + private String adamId; + + @ApiModelProperty(name = "status", + value = "The current state of the license", + example = "Available, Refunded") + private String status; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getLicenseId() { + return licenseId; + } + + public void setLicenseId(String licenseId) { + this.licenseId = licenseId; + } + + public String getAdamId() { + return adamId; + } + + public void setAdamId(String adamId) { + this.adamId = adamId; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/VPPDeviceDTO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/VPPDeviceDTO.java new file mode 100644 index 00000000000..9a74a9d1f3a --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/VPPDeviceDTO.java @@ -0,0 +1,46 @@ +package org.wso2.carbon.device.application.mgt.common.dto; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +@ApiModel(value = "VPPDeviceDTO", description = "VPPDeviceDTO represents VPP Device details.") +public class VPPDeviceDTO { + + @ApiModelProperty(name = "id", + value = "The ID given to the device when it is stored in the APPM database") + private int id; + + @ApiModelProperty(name = "serialNumber", + value = "serialNumber of the ios device", + required = true) + private String serialNumber; + + @ApiModelProperty(name = "userId", + value = "userId of the device owner", + required = true) + private String userId; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getSerialNumber() { + return serialNumber; + } + + public void setSerialNumber(String serialNumber) { + this.serialNumber = serialNumber; + } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java index 73784e48130..44c8600d7c4 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java @@ -20,7 +20,9 @@ package org.wso2.carbon.device.application.mgt.core.dao; import org.wso2.carbon.device.application.mgt.common.*; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; import org.wso2.carbon.device.application.mgt.common.dto.CategoryDTO; -import org.wso2.carbon.device.application.mgt.common.dto.TagDTO; +import org.wso2.carbon.device.application.mgt.common.dto.VPPDeviceDTO; +import org.wso2.carbon.device.application.mgt.common.dto.LicenseDTO; +import org.wso2.carbon.device.application.mgt.common.dto.TagDTO;; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import java.util.List; @@ -39,6 +41,10 @@ public interface ApplicationDAO { */ int createApplication(ApplicationDTO applicationDTO, int tenantId) throws ApplicationManagementDAOException; + int createVPPDevice(VPPDeviceDTO vppDeviceDTO, int tenantId) throws ApplicationManagementDAOException; + + void addLicenses(List licenses, int tenantId) throws ApplicationManagementDAOException; + /** * To add tags for a particular application. * diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index 04604d6ccf8..df79c04ccc8 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -23,6 +23,8 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.application.mgt.common.AppLifecycleState; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; import org.wso2.carbon.device.application.mgt.common.dto.CategoryDTO; +import org.wso2.carbon.device.application.mgt.common.dto.VPPDeviceDTO; +import org.wso2.carbon.device.application.mgt.common.dto.LicenseDTO; import org.wso2.carbon.device.application.mgt.common.Filter; import org.wso2.carbon.device.application.mgt.common.dto.TagDTO; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; @@ -61,7 +63,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic + "TYPE, " + "SUB_TYPE, " + "TENANT_ID, " - + "DEVICE_TYPE_ID) VALUES (?, ?, ?, ?, ?, ?)"; + + "DEVICE_TYPE_ID," + + "ADAM_ID) VALUES (?, ?, ?, ?, ?, ?, ?)"; int applicationId = -1; try { Connection conn = this.getDBConnection(); @@ -72,6 +75,11 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic stmt.setString(4, applicationDTO.getSubType()); stmt.setInt(5, tenantId); stmt.setInt(6, applicationDTO.getDeviceTypeId()); + if(!StringUtils.isEmpty(applicationDTO.getAdamId())){ + stmt.setString(7, applicationDTO.getAdamId()); + } else{ + stmt.setString(7, null); + } stmt.executeUpdate(); try (ResultSet rs = stmt.getGeneratedKeys()) { if (rs.next()) { @@ -93,6 +101,45 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } } + @Override + public int createVPPDevice(VPPDeviceDTO vppDeviceDTO, int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to create a VPP Device"); + log.debug("VPPDeviceDTO Details : "); + log.debug("Serial number : " + vppDeviceDTO.getSerialNumber() + " User ID : " + vppDeviceDTO.getUserId()); + } + String sql = "INSERT INTO AP_VPP_DEVICE " + + "(SERIAL_NUMBER, " + + "USER_ID, " + + "TENANT_ID) VALUES (?, ?, ?)"; + int deviceId = -1; + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { + stmt.setString(1, vppDeviceDTO.getSerialNumber()); + stmt.setString(2, vppDeviceDTO.getUserId()); + stmt.setInt(3, tenantId); + stmt.executeUpdate(); + try (ResultSet rs = stmt.getGeneratedKeys()) { + if (rs.next()) { + deviceId = rs.getInt(1); + } + return deviceId; + } + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to create a VPP Device which has " + + "Serial number : " + vppDeviceDTO.getSerialNumber(); + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while executing SQL to create a VPP Device which has Serial number : " + + vppDeviceDTO.getSerialNumber(); + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + @Override public List getApplications(Filter filter, int deviceTypeId, int tenantId) throws ApplicationManagementDAOException { @@ -748,6 +795,40 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } } + @Override + public void addLicenses(List licenses, int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to add Licenses"); + } + String sql = "INSERT INTO AP_LICENSE " + + "(LICENSE_ID, " + + "ADAM_ID, " + + "STATUS, " + + "TENANT_ID) " + + "VALUES (?, ?, ?, ?)"; + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + for (LicenseDTO license : licenses) { + stmt.setString(1, license.getLicenseId()); + stmt.setString(2, license.getAdamId()); + stmt.setString(3, license.getStatus()); + stmt.setInt(4, tenantId); + stmt.addBatch(); + } + stmt.executeBatch(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection when adding licenses"; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while adding licenses. Executed Query: " + sql; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + @Override public List getAllTags(int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index 55ddd7d3f36..bbcb1264093 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -898,6 +898,15 @@ public class ApplicationManagerImpl implements ApplicationManager { } } + //adding licenses + if (applicationDTO.getLicenses() != null && !applicationDTO.getLicenses().isEmpty()) { + this.applicationDAO.addLicenses(applicationDTO.getLicenses(), tenantId); + if (log.isDebugEnabled()) { + log.debug("Licenses added to AP_LICENSE table. App Id:" + appId + + " AdamId : " + applicationDTO.getAdamId()); + } + } + //adding application tags if (tags != null && !tags.isEmpty()) { List registeredTags = applicationDAO.getAllTags(tenantId); diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml index d0d02a058a3..39c32da8e3b 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml @@ -168,6 +168,7 @@ Video Players & Editors Weather GooglePlaySyncedApp + LicensedVPPAssets diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql index 4802f08e0af..869bd32fae2 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql @@ -17,26 +17,29 @@ CREATE TABLE IF NOT EXISTS AP_APP( ); -- ----------------------------------------------------- --- Table AP_VPP_LICENSE --- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS AP_VPP_LICENSE( - LICENSE_ID INTEGER NOT NULL AUTO_INCREMENT, - ADAM_ID VARCHAR(45) NOT NULL UNIQUE , - AVAILABILITY VARCHAR(350) NOT NULL, - PRIMARY KEY (LICENSE_ID), - CONSTRAINT fk_AP_VPP_LICENSE_AP_APP1 +-- Table AP_LICENSE +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS AP_LICENSE( + ID INTEGER NOT NULL AUTO_INCREMENT, + TENANT_ID INTEGER NOT NULL, + LICENSE_ID VARCHAR(45) NOT NULL UNIQUE , + ADAM_ID VARCHAR(45) NOT NULL , + STATUS VARCHAR(350) NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_AP_LICENSE_AP_APP1 FOREIGN KEY (ADAM_ID) REFERENCES AP_APP (ADAM_ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); -CREATE INDEX fk_AP_VPP_LICENSE_AP_APP1_idx ON AP_VPP_LICENSE (ADAM_ID ASC); +CREATE INDEX fk_AP_LICENSE_AP_APP1_idx ON AP_LICENSE (ADAM_ID ASC); -- ----------------------------------------------------- -- Table AP_VPP_DEVICE -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS AP_VPP_DEVICE( ID INTEGER NOT NULL AUTO_INCREMENT, - SERIAL_NUMBER INTEGER NOT NULL, - USER_ID INTEGER NOT NULL, + TENANT_ID INTEGER NOT NULL, + SERIAL_NUMBER VARCHAR(45) NOT NULL, + USER_ID VARCHAR(45) NOT NULL, PRIMARY KEY (ID) ); @@ -46,16 +49,17 @@ CREATE TABLE IF NOT EXISTS AP_VPP_DEVICE( CREATE TABLE IF NOT EXISTS AP_VPP_DEVICE_LICENSE_MAPPING( LICENSE_ID INTEGER NOT NULL, DEVICE_ID INTEGER NOT NULL, + TENANT_ID INTEGER NOT NULL, PRIMARY KEY (LICENSE_ID, DEVICE_ID), CONSTRAINT fk_AP_VPP_DEVICE_LICENSE_MAPPING_AP_VPP_DEVICE FOREIGN KEY (DEVICE_ID) REFERENCES AP_VPP_DEVICE (ID), - CONSTRAINT fk_AP_VPP_DEVICE_LICENSE_MAPPING_AP_VPP_LICENSE + CONSTRAINT fk_AP_VPP_DEVICE_LICENSE_MAPPING_AP_LICENSE FOREIGN KEY (LICENSE_ID) - REFERENCES AP_VPP_LICENSE (LICENSE_ID) + REFERENCES AP_LICENSE (ID) ); CREATE INDEX fk_AP_VPP_DEVICE_LICENSE_MAPPING_AP_VPP_DEVICE_idx ON AP_VPP_DEVICE (ID ASC); -CREATE INDEX fk_AP_VPP_DEVICE_LICENSE_MAPPING_AP_VPP_LICENSE_idx ON AP_VPP_LICENSE (LICENSE_ID ASC); +CREATE INDEX fk_AP_VPP_DEVICE_LICENSE_MAPPING_AP_LICENSE_idx ON AP_LICENSE (LICENSE_ID ASC); -- ----------------------------------------------------- -- Table AP_APP_RELEASE