From d1e6aab5cdbee1dd7cc524634f283afd73cd66e8 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Sat, 19 Oct 2019 02:29:27 +0530 Subject: [PATCH] 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 6a86f753b7..aaafcec75c 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 ea4ff20962..7900c36acb 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); + } + } }