From b5a143370992914b60714275ebcc2e7e7a4c9415 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Fri, 18 Oct 2019 16:49:37 +0530 Subject: [PATCH 1/3] Improve SQL queries in APPM --- .../core/impl/SubscriptionManagerImpl.java | 18 ++++++---- .../carbon/device/mgt/common/app/mgt/App.java | 10 ------ .../DeviceManagementProviderServiceImpl.java | 8 ++--- .../core/util/MDMAndroidOperationUtil.java | 4 +-- .../dbscripts/cdm/application-mgt/h2.sql | 10 +++--- .../dbscripts/cdm/application-mgt/mssql.sql | 10 +++--- .../dbscripts/cdm/application-mgt/mysql.sql | 34 +++++++++---------- 7 files changed, 45 insertions(+), 49 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index 83ce469ba33..8c6c69f4175 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -692,15 +692,13 @@ public class SubscriptionManagerImpl implements SubscriptionManager { App app = new App(); MobileAppTypes mobileAppType = MobileAppTypes.valueOf(application.getType()); if (DeviceTypes.ANDROID.toString().equalsIgnoreCase(deviceType)) { + app.setType(mobileAppType); + app.setLocation(application.getApplicationReleases().get(0).getInstallerPath()); + app.setIdentifier(application.getPackageName()); + app.setName(application.getName()); if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { - app.setType(mobileAppType); - app.setLocation(application.getApplicationReleases().get(0).getInstallerPath()); - app.setIdentifier(application.getPackageName()); - app.setName(application.getName()); return MDMAndroidOperationUtil.createInstallAppOperation(app); } else if (SubAction.UNINSTALL.toString().equalsIgnoreCase(action)) { - app.setType(mobileAppType); - app.setAppIdentifier(application.getPackageName()); return MDMAndroidOperationUtil.createAppUninstallOperation(app); } else { String msg = "Invalid Action is found. Action: " + action; @@ -817,6 +815,14 @@ public class SubscriptionManagerImpl implements SubscriptionManager { for (DeviceSubscriptionDTO deviceIds : deviceSubscriptionDTOS) { deviceIdList.add(deviceIds.getDeviceId()); } + + if (deviceIdList.isEmpty()){ + PaginationResult paginationResult = new PaginationResult(); + paginationResult.setData(deviceIdList); + paginationResult.setRecordsFiltered(0); + paginationResult.setRecordsTotal(0); + return paginationResult; + } //pass the device id list to device manager service method try { PaginationResult deviceDetails = deviceManagementProviderService diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/App.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/App.java index a7a9d95c6b2..7b3b34ded4d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/App.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/App.java @@ -59,8 +59,6 @@ public class App { "tab and you will see the package name under the respective " + "application..", required = true) private String packageName; - @ApiModelProperty(name = "appIdentifier", value = "The package name of the application.", required = true) - private String appIdentifier; private String location; @ApiModelProperty(name = "properties", value = "List of meta data.", required = true) private Properties properties; @@ -129,14 +127,6 @@ public class App { this.packageName = packageName; } - public String getAppIdentifier() { - return appIdentifier; - } - - public void setAppIdentifier(String appIdentifier) { - this.appIdentifier = appIdentifier; - } - public String getLocation() { return location; } 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 d4732d172da..b700a53bdb4 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 @@ -3734,12 +3734,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } PaginationResult paginationResult = new PaginationResult(); int count; - List SubscribedDeviceDetails; + List subscribedDeviceDetails; try { DeviceManagementDAOFactory.openConnection(); - SubscribedDeviceDetails = deviceDAO + subscribedDeviceDetails = deviceDAO .getSubscribedDevices(offsetValue, limitValue, devicesIds, tenantId, status); - count = SubscribedDeviceDetails.size(); + count = subscribedDeviceDetails.size(); } catch (DeviceManagementDAOException e) { String msg = "Error occurred while retrieving device list for device ids " + devicesIds; @@ -3752,7 +3752,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } finally { DeviceManagementDAOFactory.closeConnection(); } - paginationResult.setData(getAllDeviceInfo(SubscribedDeviceDetails)); + paginationResult.setData(getAllDeviceInfo(subscribedDeviceDetails)); paginationResult.setRecordsFiltered(count); paginationResult.setRecordsTotal(count); return paginationResult; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/MDMAndroidOperationUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/MDMAndroidOperationUtil.java index 20d7fda28cf..f9c90d6d1ae 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/MDMAndroidOperationUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/MDMAndroidOperationUtil.java @@ -91,14 +91,14 @@ public class MDMAndroidOperationUtil { EnterpriseApplication enterpriseApplication = new EnterpriseApplication(); enterpriseApplication.setType(application.getType().toString()); - enterpriseApplication.setAppIdentifier(application.getAppIdentifier()); + enterpriseApplication.setAppIdentifier(application.getIdentifier()); operation.setPayLoad(enterpriseApplication.toJSON()); break; case PUBLIC: AppStoreApplication appStoreApplication = new AppStoreApplication(); appStoreApplication.setType(application.getType().toString()); - appStoreApplication.setAppIdentifier(application.getAppIdentifier()); + appStoreApplication.setAppIdentifier(application.getIdentifier()); operation.setPayLoad(appStoreApplication.toJSON()); break; case WEBAPP: diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql index 4dd5d3af245..148ea46385a 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql @@ -25,7 +25,7 @@ CREATE TABLE IF NOT EXISTS AP_APP_RELEASE( TENANT_ID INTEGER NOT NULL, UUID VARCHAR(200) NOT NULL, RELEASE_TYPE VARCHAR(45) NOT NULL, - PACKAGE_NAME VARCHAR(45) NOT NULL, + PACKAGE_NAME VARCHAR(150) NOT NULL, APP_PRICE DECIMAL(6, 2) NULL DEFAULT NULL, INSTALLER_LOCATION VARCHAR(100) NOT NULL, ICON_LOCATION VARCHAR(100) NOT NULL, @@ -107,7 +107,7 @@ CREATE TABLE IF NOT EXISTS AP_DEVICE_SUBSCRIPTION( TENANT_ID INTEGER NOT NULL, SUBSCRIBED_BY VARCHAR(100) NOT NULL, SUBSCRIBED_TIMESTAMP TIMESTAMP NOT NULL, - UNSUBSCRIBED BOOLEAN NULL DEFAULT NULL, + UNSUBSCRIBED BOOLEAN NOT NULL DEFAULT false, UNSUBSCRIBED_BY VARCHAR(100) NULL DEFAULT NULL, UNSUBSCRIBED_TIMESTAMP TIMESTAMP NULL DEFAULT NULL, ACTION_TRIGGERED_FROM VARCHAR(45) NOT NULL, @@ -129,7 +129,7 @@ CREATE TABLE IF NOT EXISTS AP_GROUP_SUBSCRIPTION( TENANT_ID INTEGER NOT NULL, SUBSCRIBED_BY VARCHAR(100) NOT NULL, SUBSCRIBED_TIMESTAMP TIMESTAMP NOT NULL, - UNSUBSCRIBED BOOLEAN NULL DEFAULT NULL, + UNSUBSCRIBED BOOLEAN NOT NULL DEFAULT false, UNSUBSCRIBED_BY VARCHAR(100) NULL DEFAULT NULL, UNSUBSCRIBED_TIMESTAMP TIMESTAMP NULL DEFAULT NULL, GROUP_NAME VARCHAR(100) NOT NULL, @@ -150,7 +150,7 @@ CREATE TABLE IF NOT EXISTS AP_ROLE_SUBSCRIPTION( ROLE_NAME VARCHAR(100) NOT NULL, SUBSCRIBED_BY VARCHAR(100) NOT NULL, SUBSCRIBED_TIMESTAMP TIMESTAMP NOT NULL, - UNSUBSCRIBED BOOLEAN NULL DEFAULT NULL, + UNSUBSCRIBED BOOLEAN NOT NULL DEFAULT false, UNSUBSCRIBED_BY VARCHAR(100) NULL DEFAULT NULL, UNSUBSCRIBED_TIMESTAMP TIMESTAMP NULL DEFAULT NULL, AP_APP_RELEASE_ID INTEGER NOT NULL, @@ -185,7 +185,7 @@ CREATE TABLE IF NOT EXISTS AP_USER_SUBSCRIPTION( USER_NAME VARCHAR(100) NOT NULL, SUBSCRIBED_BY VARCHAR(100) NOT NULL, SUBSCRIBED_TIMESTAMP TIMESTAMP NOT NULL, - UNSUBSCRIBED BOOLEAN NULL DEFAULT NULL, + UNSUBSCRIBED BOOLEAN NOT NULL DEFAULT false, UNSUBSCRIBED_BY VARCHAR(100) NULL DEFAULT NULL, UNSUBSCRIBED_TIMESTAMP TIMESTAMP NULL DEFAULT NULL, AP_APP_RELEASE_ID INTEGER NOT NULL, diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mssql.sql b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mssql.sql index 99330c303b6..dbaffe47333 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mssql.sql +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mssql.sql @@ -25,7 +25,7 @@ CREATE TABLE AP_APP_RELEASE( TENANT_ID INTEGER NOT NULL, UUID VARCHAR(200) NOT NULL, RELEASE_TYPE VARCHAR(45) NOT NULL, - PACKAGE_NAME VARCHAR(45) NOT NULL, + PACKAGE_NAME VARCHAR(150) NOT NULL, APP_PRICE DECIMAL(6, 2) NULL DEFAULT NULL, INSTALLER_LOCATION VARCHAR(100) NOT NULL, ICON_LOCATION VARCHAR(100) NOT NULL, @@ -108,7 +108,7 @@ CREATE TABLE AP_DEVICE_SUBSCRIPTION( TENANT_ID INTEGER NOT NULL, SUBSCRIBED_BY VARCHAR(100) NOT NULL, SUBSCRIBED_TIMESTAMP DATETIME2(0) NOT NULL, - UNSUBSCRIBED BIT NULL DEFAULT NULL, + UNSUBSCRIBED BIT NOT NULL DEFAULT 'false', UNSUBSCRIBED_BY VARCHAR(100) NULL DEFAULT NULL, UNSUBSCRIBED_TIMESTAMP DATETIME2(0) NULL DEFAULT NULL, ACTION_TRIGGERED_FROM VARCHAR(45) NOT NULL, @@ -130,7 +130,7 @@ CREATE TABLE AP_GROUP_SUBSCRIPTION( TENANT_ID INTEGER NOT NULL, SUBSCRIBED_BY VARCHAR(100) NOT NULL, SUBSCRIBED_TIMESTAMP DATETIME2(0) NOT NULL, - UNSUBSCRIBED BIT NULL DEFAULT NULL, + UNSUBSCRIBED BIT NOT NULL DEFAULT 'false', UNSUBSCRIBED_BY VARCHAR(100) NULL DEFAULT NULL, UNSUBSCRIBED_TIMESTAMP DATETIME2(0) NULL DEFAULT NULL, GROUP_NAME VARCHAR(100) NOT NULL, @@ -151,7 +151,7 @@ CREATE TABLE AP_ROLE_SUBSCRIPTION( ROLE_NAME VARCHAR(100) NOT NULL, SUBSCRIBED_BY VARCHAR(100) NOT NULL, SUBSCRIBED_TIMESTAMP DATETIME2(0) NOT NULL, - UNSUBSCRIBED BIT NULL DEFAULT NULL, + UNSUBSCRIBED BIT NOT NULL DEFAULT 'false', UNSUBSCRIBED_BY VARCHAR(100) NULL DEFAULT NULL, UNSUBSCRIBED_TIMESTAMP DATETIME2(0) NULL DEFAULT NULL, AP_APP_RELEASE_ID INTEGER NOT NULL, @@ -186,7 +186,7 @@ CREATE TABLE AP_USER_SUBSCRIPTION( USER_NAME VARCHAR(100) NOT NULL, SUBSCRIBED_BY VARCHAR(100) NOT NULL, SUBSCRIBED_TIMESTAMP DATETIME2(0) NOT NULL, - UNSUBSCRIBED BIT NULL DEFAULT NULL, + UNSUBSCRIBED BIT NOT NULL DEFAULT 'false', UNSUBSCRIBED_BY VARCHAR(100) NULL DEFAULT NULL, UNSUBSCRIBED_TIMESTAMP DATETIME2(0) NULL DEFAULT NULL, AP_APP_RELEASE_ID INTEGER NOT NULL, 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 52d880cb026..383210ef9fe 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 @@ -25,7 +25,7 @@ CREATE TABLE IF NOT EXISTS AP_APP_RELEASE( TENANT_ID INTEGER NOT NULL, UUID VARCHAR(200) NOT NULL, RELEASE_TYPE VARCHAR(45) NOT NULL, - PACKAGE_NAME VARCHAR(45) NOT NULL, + PACKAGE_NAME VARCHAR(150) NOT NULL, APP_PRICE DECIMAL(6, 2) NULL DEFAULT NULL, INSTALLER_LOCATION VARCHAR(100) NOT NULL, ICON_LOCATION VARCHAR(100) NOT NULL, @@ -107,7 +107,7 @@ CREATE TABLE IF NOT EXISTS AP_DEVICE_SUBSCRIPTION( TENANT_ID INTEGER NOT NULL, SUBSCRIBED_BY VARCHAR(100) NOT NULL, SUBSCRIBED_TIMESTAMP TIMESTAMP NOT NULL, - UNSUBSCRIBED BOOLEAN NULL DEFAULT NULL, + UNSUBSCRIBED BOOLEAN NOT NULL DEFAULT false, UNSUBSCRIBED_BY VARCHAR(100) NULL DEFAULT NULL, UNSUBSCRIBED_TIMESTAMP TIMESTAMP NULL DEFAULT NULL, ACTION_TRIGGERED_FROM VARCHAR(45) NOT NULL, @@ -129,7 +129,7 @@ CREATE TABLE IF NOT EXISTS AP_GROUP_SUBSCRIPTION( TENANT_ID INTEGER NOT NULL, SUBSCRIBED_BY VARCHAR(100) NOT NULL, SUBSCRIBED_TIMESTAMP TIMESTAMP NOT NULL, - UNSUBSCRIBED BOOLEAN NULL DEFAULT NULL, + UNSUBSCRIBED BOOLEAN NOT NULL DEFAULT false, UNSUBSCRIBED_BY VARCHAR(100) NULL DEFAULT NULL, UNSUBSCRIBED_TIMESTAMP TIMESTAMP NULL DEFAULT NULL, GROUP_NAME VARCHAR(100) NOT NULL, @@ -145,19 +145,19 @@ CREATE INDEX fk_AP_GROUP_SUBSCRIPTION_AP_APP_RELEASE1_idx ON AP_GROUP_SUBSCRIPTI -- Table AP_ROLE_SUBSCRIPTION -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS AP_ROLE_SUBSCRIPTION( -ID INTEGER NOT NULL AUTO_INCREMENT, -TENANT_ID INTEGER NOT NULL, -ROLE_NAME VARCHAR(100) NOT NULL, -SUBSCRIBED_BY VARCHAR(100) NOT NULL, -SUBSCRIBED_TIMESTAMP TIMESTAMP NOT NULL, -UNSUBSCRIBED BOOLEAN NULL DEFAULT NULL, -UNSUBSCRIBED_BY VARCHAR(100) NULL DEFAULT NULL, -UNSUBSCRIBED_TIMESTAMP TIMESTAMP NULL DEFAULT NULL, -AP_APP_RELEASE_ID INTEGER NOT NULL, -PRIMARY KEY (ID), -CONSTRAINT fk_AP_ROLE_SUBSCRIPTION_AP_APP_RELEASE1 - FOREIGN KEY (AP_APP_RELEASE_ID) - REFERENCES AP_APP_RELEASE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION + ID INTEGER NOT NULL AUTO_INCREMENT, + TENANT_ID INTEGER NOT NULL, + ROLE_NAME VARCHAR(100) NOT NULL, + SUBSCRIBED_BY VARCHAR(100) NOT NULL, + SUBSCRIBED_TIMESTAMP TIMESTAMP NOT NULL, + UNSUBSCRIBED BOOLEAN NOT NULL DEFAULT false, + UNSUBSCRIBED_BY VARCHAR(100) NULL DEFAULT NULL, + UNSUBSCRIBED_TIMESTAMP TIMESTAMP NULL DEFAULT NULL, + AP_APP_RELEASE_ID INTEGER NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_AP_ROLE_SUBSCRIPTION_AP_APP_RELEASE1 + FOREIGN KEY (AP_APP_RELEASE_ID) + REFERENCES AP_APP_RELEASE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); CREATE INDEX fk_AP_ROLE_SUBSCRIPTION_AP_APP_RELEASE1_idx ON AP_ROLE_SUBSCRIPTION (AP_APP_RELEASE_ID ASC); @@ -185,7 +185,7 @@ CREATE TABLE IF NOT EXISTS AP_USER_SUBSCRIPTION( USER_NAME VARCHAR(100) NOT NULL, SUBSCRIBED_BY VARCHAR(100) NOT NULL, SUBSCRIBED_TIMESTAMP TIMESTAMP NOT NULL, - UNSUBSCRIBED BOOLEAN NULL DEFAULT NULL, + UNSUBSCRIBED BOOLEAN NOT NULL DEFAULT false, UNSUBSCRIBED_BY VARCHAR(100) NULL DEFAULT NULL, UNSUBSCRIBED_TIMESTAMP TIMESTAMP NULL DEFAULT NULL, AP_APP_RELEASE_ID INTEGER NOT NULL, From 0dbd4bf2dbb408bf99caf55a3d4fc8ca8aecee84 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Fri, 18 Oct 2019 17:41:00 +0530 Subject: [PATCH 2/3] fix compilation error --- .../mgt/core/impl/SubscriptionManagerImpl.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index 8c6c69f4175..c59407b1baf 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -642,6 +642,16 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } + /** + * This method is responsible to add operation on given devices. + * + * @param applicationDTO application. + * @param deviceIdentifierList list of device identifiers. + * @param deviceType device type + * @param action action e.g :- INSTALL, UNINSTALL + * @return {@link Activity} + * @throws ApplicationManagementException if found an invalid device. + */ private Activity addAppOperationOnDevices(ApplicationDTO applicationDTO, List deviceIdentifierList, String deviceType, String action) throws ApplicationManagementException { @@ -664,7 +674,6 @@ public class SubscriptionManagerImpl implements SubscriptionManager { private Operation generateOperationPayloadByDeviceType(String deviceType, Application application, String action) throws ApplicationManagementException { try { - //todo rethink and modify the {@link App} usage if (ApplicationType.CUSTOM.toString().equalsIgnoreCase(application.getType())) { ProfileOperation operation = new ProfileOperation(); if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { @@ -720,7 +729,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { return MDMIOSOperationUtil.createInstallAppOperation(app); } else if (SubAction.UNINSTALL.toString().equalsIgnoreCase(action)) { app.setType(mobileAppType); - app.setAppIdentifier(application.getPackageName()); + app.setIdentifier(application.getPackageName()); return MDMIOSOperationUtil.createAppUninstallOperation(app); } else { String msg = "Invalid Action is found. Action: " + action; From d1e6aab5cdbee1dd7cc524634f283afd73cd66e8 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Sat, 19 Oct 2019 02:29:27 +0530 Subject: [PATCH 3/3] Improve APPM DAO layer to support MsSQL --- .../GenericSubscriptionDAOImpl.java | 2 +- .../SQLServerSubscriptionDAOImpl.java | 111 ++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) 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/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index 6a86f753b7c..aaafcec75c2 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -140,7 +140,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc try (ResultSet rs = stmt.getGeneratedKeys()){ List updatedDeviceSubIds = new ArrayList<>(); while (rs.next()) { - updatedDeviceSubIds.add(rs.getInt("ID")); + updatedDeviceSubIds.add(rs.getInt(1)); } return updatedDeviceSubIds; } 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/subscription/SQLServerSubscriptionDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/SQLServerSubscriptionDAOImpl.java index ea4ff20962b..7900c36acbb 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/SQLServerSubscriptionDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/SQLServerSubscriptionDAOImpl.java @@ -26,7 +26,9 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Timestamp; import java.util.ArrayList; +import java.util.Calendar; import java.util.List; /** @@ -156,4 +158,113 @@ public class SQLServerSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { throw new ApplicationManagementDAOException(msg, e); } } + + @Override + public List updateDeviceSubscription(String updateBy, List deviceIds, + boolean isUnsubscribed, String actionTriggeredFrom, String installStatus, int releaseId, int tenantId) + throws ApplicationManagementDAOException { + try { + String sql = "UPDATE AP_DEVICE_SUBSCRIPTION SET "; + + if (isUnsubscribed) { + sql += "UNSUBSCRIBED = true, UNSUBSCRIBED_BY = ?, UNSUBSCRIBED_TIMESTAMP = ?, "; + } else { + sql += "SUBSCRIBED_BY = ?, SUBSCRIBED_TIMESTAMP = ?, "; + } + sql += "ACTION_TRIGGERED_FROM = ?, " + + "STATUS = ? " + + "WHERE " + + "DM_DEVICE_ID = ? AND " + + "AP_APP_RELEASE_ID = ? AND " + + "TENANT_ID = ?"; + + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + Calendar calendar = Calendar.getInstance(); + Timestamp timestamp = new Timestamp(calendar.getTime().getTime()); + List updatedDeviceSubIds = new ArrayList<>(); + for (Integer deviceId : deviceIds) { + stmt.setString(1, updateBy); + stmt.setTimestamp(2, timestamp); + stmt.setString(3, actionTriggeredFrom); + stmt.setString(4, installStatus); + stmt.setInt(5, deviceId); + stmt.setInt(6, releaseId); + stmt.setInt(7, tenantId); + stmt.executeUpdate(); + try (ResultSet rs = stmt.getGeneratedKeys()) { + if (rs.next()) { + updatedDeviceSubIds.add(rs.getInt(1)); + } + } + } + return updatedDeviceSubIds; + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to update device subscriptions of " + + "application. Updated by: " + updateBy + " and updating action triggered from " + + actionTriggeredFrom; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while executing SQL to update the device subscriptions of application. " + + "Updated by: " + updateBy + " and updating action triggered from " + actionTriggeredFrom; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + + @Override + public List addDeviceSubscription(String subscribedBy, List deviceIds, + String subscribedFrom, String installStatus, int releaseId, int tenantId) + throws ApplicationManagementDAOException { + String sql = "INSERT INTO " + + "AP_DEVICE_SUBSCRIPTION(" + + "SUBSCRIBED_BY, " + + "SUBSCRIBED_TIMESTAMP, " + + "ACTION_TRIGGERED_FROM, " + + "STATUS, " + + "DM_DEVICE_ID, " + + "AP_APP_RELEASE_ID," + + "TENANT_ID) " + + "VALUES (?, ?, ?, ?, ?, ?, ?)"; + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + Calendar calendar = Calendar.getInstance(); + Timestamp timestamp = new Timestamp(calendar.getTime().getTime()); + List deviceSubIds = new ArrayList<>(); + for (Integer deviceId : deviceIds) { + stmt.setString(1, subscribedBy); + stmt.setTimestamp(2, timestamp); + stmt.setString(3, subscribedFrom); + stmt.setString(4, installStatus); + stmt.setInt(5, deviceId); + stmt.setInt(6, releaseId); + stmt.setInt(7, tenantId); + if (log.isDebugEnabled()) { + log.debug("Adding a device subscription for device id " + deviceId + " and application " + + "release which has release id" + releaseId); + } + stmt.executeUpdate(); + try (ResultSet rs = stmt.getGeneratedKeys()) { + if (rs.next()) { + deviceSubIds.add(rs.getInt(1)); + } + } + } + return deviceSubIds; + } + } catch (DBConnectionException e) { + String msg = "Error occured while obtaining database connection to add device subscription for application " + + "release which has release Id" + releaseId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occured when processing SQL to add device subscription for application release which" + + " has release Id " + releaseId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } }