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); + } + } }