diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java index 2f1170a77e..f6e39a7514 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java @@ -42,22 +42,42 @@ public interface SubscriptionDAO { void addOperationMapping (int operationId, List deviceSubscriptionId, int tenantId) throws ApplicationManagementDAOException; /** - * Adds a mapping between user and the application which the application is installed on. This mapping will be - * added when an enterprise installation triggered to the user. + * Adds a mapping between user and the application which the application is subscribed on. This mapping will be + * added when an app subscription triggered to the user. * * @param tenantId id of the tenant * @param subscribedBy username of the user who subscribe the application * @param users list of user names of the users whose devices are subscribed to the application * @param releaseId id of the {@link ApplicationReleaseDTO} - * @throws ApplicationManagementDAOException If unable to add a mapping between device and application + * @throws ApplicationManagementDAOException If unable to add a mapping between user and application */ - void addUserSubscriptions(int tenantId, String subscribedBy, List users, int releaseId) + void addUserSubscriptions(int tenantId, String subscribedBy, List users, int releaseId, String action) throws ApplicationManagementDAOException; - void addRoleSubscriptions(int tenantId, String subscribedBy, List roles, int releaseId) + /** + * Adds a mapping between role and the application which the application is subscribed on. This mapping will be + * added when an app subscription triggered to the role. + * + * @param tenantId id of the tenant + * @param subscribedBy username of the user who subscribe the application + * @param roles list of role names of the roles whose devices are subscribed to the application + * @param releaseId id of the {@link ApplicationReleaseDTO} + * @throws ApplicationManagementDAOException If unable to add a mapping between role and application + */ + void addRoleSubscriptions(int tenantId, String subscribedBy, List roles, int releaseId, String action) throws ApplicationManagementDAOException; - void addGroupSubscriptions(int tenantId, String subscribedBy, List groups, int releaseId) + /** + * Adds a mapping between group and the application which the application is subscribed on. This mapping will be + * added when an app subscription triggered to the user. + * + * @param tenantId id of the tenant + * @param subscribedBy username of the user who subscribe the application + * @param groups list of group names of the groups whose devices are subscribed to the application + * @param releaseId id of the {@link ApplicationReleaseDTO} + * @throws ApplicationManagementDAOException If unable to add a mapping between group and application + */ + void addGroupSubscriptions(int tenantId, String subscribedBy, List groups, int releaseId, String action) throws ApplicationManagementDAOException; List getDeviceSubscriptions(int appReleaseId, int tenantId) throws 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 38ab032ebd..b9814e62bd 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 @@ -188,27 +188,32 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public void addUserSubscriptions(int tenantId, String subscribedBy, List users, int releaseId) - throws ApplicationManagementDAOException { - String sql = "INSERT INTO " - + "AP_USER_SUBSCRIPTION(" - + "TENANT_ID, " - + "SUBSCRIBED_BY, " - + "SUBSCRIBED_TIMESTAMP, " - + "USER_NAME, " - + "AP_APP_RELEASE_ID) " - + "VALUES (?, ?, ?, ?, ?)"; + public void addUserSubscriptions(int tenantId, String subscribedBy, List users, int releaseId, + String action) throws ApplicationManagementDAOException { try { + boolean isUnsubscribed = false; + String sql = "INSERT INTO AP_USER_SUBSCRIPTION(TENANT_ID, "; + + if (SubAction.UNINSTALL.toString().equalsIgnoreCase(action)) { + sql += "UNSUBSCRIBED, UNSUBSCRIBED_BY, UNSUBSCRIBED_TIMESTAMP, "; + isUnsubscribed = true; + } else { + sql += "UNSUBSCRIBED, SUBSCRIBED_BY, SUBSCRIBED_TIMESTAMP, "; + } + + sql += "USER_NAME, AP_APP_RELEASE_ID) VALUES (?, ?, ?, ?, ?,?)"; + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { Calendar calendar = Calendar.getInstance(); Timestamp timestamp = new Timestamp(calendar.getTime().getTime()); for (String user : users) { stmt.setInt(1, tenantId); - stmt.setString(2, subscribedBy); - stmt.setTimestamp(3, timestamp); - stmt.setString(4, user); - stmt.setInt(5, releaseId); + stmt.setBoolean(2, isUnsubscribed); + stmt.setString(3, subscribedBy); + stmt.setTimestamp(4, timestamp); + stmt.setString(5, user); + stmt.setInt(6, releaseId); stmt.addBatch(); if (log.isDebugEnabled()) { log.debug("Adding an user subscription for user " + user + " and application release which " @@ -224,34 +229,39 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { String msg = "Error occurred while executing query to add user subscription. Subscribing user is " - + subscribedBy + " and executed query: " + sql; + + subscribedBy; log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); } } @Override - public void addRoleSubscriptions(int tenantId, String subscribedBy, List roles, int releaseId) + public void addRoleSubscriptions(int tenantId, String subscribedBy, List roles, int releaseId, String action) throws ApplicationManagementDAOException { - String sql = "INSERT INTO " - + "AP_ROLE_SUBSCRIPTION(" - + "TENANT_ID, " - + "SUBSCRIBED_BY, " - + "SUBSCRIBED_TIMESTAMP, " - + "ROLE_NAME, " - + "AP_APP_RELEASE_ID) " - + "VALUES (?, ?, ?, ?, ?)"; try { + boolean isUnsubscribed = false; + String sql = "INSERT INTO AP_ROLE_SUBSCRIPTION(TENANT_ID, "; + + if (SubAction.UNINSTALL.toString().equalsIgnoreCase(action)) { + sql += "UNSUBSCRIBED, UNSUBSCRIBED_BY, UNSUBSCRIBED_TIMESTAMP, "; + isUnsubscribed = true; + } else { + sql += "UNSUBSCRIBED, SUBSCRIBED_BY, SUBSCRIBED_TIMESTAMP, "; + } + + sql += "ROLE_NAME, AP_APP_RELEASE_ID) VALUES (?, ?, ?, ?, ?,?)"; + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { Calendar calendar = Calendar.getInstance(); Timestamp timestamp = new Timestamp(calendar.getTime().getTime()); for (String role : roles) { stmt.setInt(1, tenantId); - stmt.setString(2, subscribedBy); - stmt.setTimestamp(3, timestamp); - stmt.setString(4, role); - stmt.setInt(5, releaseId); + stmt.setBoolean(2, isUnsubscribed); + stmt.setString(3, subscribedBy); + stmt.setTimestamp(4, timestamp); + stmt.setString(5, role); + stmt.setInt(6, releaseId); stmt.addBatch(); if (log.isDebugEnabled()) { log.debug("Adding a role subscription for role " + role + " and application release which " @@ -267,34 +277,39 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { String msg = "Error occurred while executing query to add role subscription. Subscribing role is " - + subscribedBy + " and executed query: " + sql; + + subscribedBy; log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); } } @Override - public void addGroupSubscriptions(int tenantId, String subscribedBy, List groups, int releaseId) - throws ApplicationManagementDAOException { - String sql = "INSERT INTO " - + "AP_GROUP_SUBSCRIPTION(" - + "TENANT_ID, " - + "SUBSCRIBED_BY, " - + "SUBSCRIBED_TIMESTAMP, " - + "GROUP_NAME, " - + "AP_APP_RELEASE_ID) " - + "VALUES (?, ?, ?, ?, ?)"; + public void addGroupSubscriptions(int tenantId, String subscribedBy, List groups, int releaseId, + String action) throws ApplicationManagementDAOException { try { + boolean isUnsubscribed = false; + String sql = "INSERT INTO AP_GROUP_SUBSCRIPTION(TENANT_ID, "; + + if (SubAction.UNINSTALL.toString().equalsIgnoreCase(action)) { + sql += "UNSUBSCRIBED, UNSUBSCRIBED_BY, UNSUBSCRIBED_TIMESTAMP, "; + isUnsubscribed = true; + } else { + sql += "UNSUBSCRIBED, SUBSCRIBED_BY, SUBSCRIBED_TIMESTAMP, "; + } + + sql += "GROUP_NAME, AP_APP_RELEASE_ID) VALUES (?, ?, ?, ?, ?,?)"; + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { Calendar calendar = Calendar.getInstance(); Timestamp timestamp = new Timestamp(calendar.getTime().getTime()); for (String group : groups) { stmt.setInt(1, tenantId); - stmt.setString(2, subscribedBy); - stmt.setTimestamp(3, timestamp); - stmt.setString(4, group); - stmt.setInt(5, releaseId); + stmt.setBoolean(2, isUnsubscribed); + stmt.setString(3, subscribedBy); + stmt.setTimestamp(4, timestamp); + stmt.setString(5, group); + stmt.setInt(6, releaseId); stmt.addBatch(); if (log.isDebugEnabled()) { log.debug("Adding a group subscription for role " + group + " and application release which " @@ -310,7 +325,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { String msg = "Error occurred while executing query to add group subscription. Subscribing group is " - + subscribedBy + " and executed query: " + sql; + + subscribedBy; log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); } 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 b0ac6a5dbd..c9f4d0e4a1 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 @@ -471,24 +471,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); try { ConnectionManagerUtil.beginDBTransaction(); - List subscribedEntities = new ArrayList<>(); - if (SubscriptionType.USER.toString().equalsIgnoreCase(subType)) { - subscribedEntities = subscriptionDAO.getAppSubscribedUserNames(params, applicationReleaseId, tenantId); - } else if (SubscriptionType.ROLE.toString().equalsIgnoreCase(subType)) { - subscribedEntities = subscriptionDAO.getAppSubscribedRoleNames(params, applicationReleaseId, tenantId); - } else if (SubscriptionType.GROUP.toString().equalsIgnoreCase(subType)) { - subscribedEntities = subscriptionDAO.getAppSubscribedGroupNames(params, applicationReleaseId, tenantId); - } - - params.removeAll(subscribedEntities); - if (!params.isEmpty()) { - subscriptionDAO.addUserSubscriptions(tenantId, username, params, applicationReleaseId); - } - if (!subscribedEntities.isEmpty()) { - subscriptionDAO - .updateSubscriptions(tenantId, username, subscribedEntities, applicationReleaseId, subType, - action); - } + updateBulkSubscribers(applicationReleaseId, params, subType, action, tenantId, username); if (SubAction.INSTALL.toString().equalsIgnoreCase(action) && !appSubscribingDeviceIds.isEmpty()) { subscriptionDAO.addDeviceSubscription(username, appSubscribingDeviceIds, subType, @@ -760,35 +743,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); try { ConnectionManagerUtil.beginDBTransaction(); - if (SubscriptionType.USER.toString().equalsIgnoreCase(subType)) { - List subscribedEntities = subscriptionDAO - .getAppSubscribedUserNames(params, applicationReleaseId, tenantId); - if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { - params.removeAll(subscribedEntities); - subscriptionDAO.addUserSubscriptions(tenantId, username, params, applicationReleaseId); - } - subscriptionDAO.updateSubscriptions(tenantId, username, subscribedEntities, applicationReleaseId, subType, - action); - } else if (SubscriptionType.ROLE.toString().equalsIgnoreCase(subType)) { - List subscribedEntities = subscriptionDAO - .getAppSubscribedRoleNames(params, applicationReleaseId, tenantId); - if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { - params.removeAll(subscribedEntities); - subscriptionDAO.addRoleSubscriptions(tenantId, username, params, applicationReleaseId); - } - subscriptionDAO.updateSubscriptions(tenantId, username, subscribedEntities, applicationReleaseId, subType, - action); - } else if (SubscriptionType.GROUP.toString().equalsIgnoreCase(subType)) { - List subscribedEntities = subscriptionDAO - .getAppSubscribedGroupNames(params, applicationReleaseId, tenantId); - if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { - params.removeAll(subscribedEntities); - subscriptionDAO.addGroupSubscriptions(tenantId, username, params, applicationReleaseId); - } - subscriptionDAO.updateSubscriptions(tenantId, username, subscribedEntities, applicationReleaseId, subType, - action); - } - + updateBulkSubscribers(applicationReleaseId, params, subType, action, tenantId, username); for (Activity activity : activities) { int operationId = Integer.parseInt(activity.getActivityId().split("ACTIVITY_")[1]); List subUpdatingDeviceIds = new ArrayList<>(); @@ -840,6 +795,48 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } + /** + * This method is responsible to update bulk subscriber's data. i.e USER, ROLE, GROUP. Before invoke this method it + * is required to start DB transaction + * + * @param applicationReleaseId Application release Id + * @param params subscribers. If subscription is performed via user, group or role, params is a list of + * {@link String} + * @param subType Subscription type. i.e USER, GROUP, ROLE or DEVICE + * @param action performing action. ie INSTALL or UNINSTALL> + * @param tenantId Tenant Id + * @param username Username + * @throws ApplicationManagementDAOException if error occurred while updating or inserting subscriber entities + */ + private void updateBulkSubscribers(int applicationReleaseId, List params, String subType, String action, + int tenantId, String username) throws ApplicationManagementDAOException { + List subscribedEntities = new ArrayList<>(); + if (SubscriptionType.USER.toString().equalsIgnoreCase(subType)) { + subscribedEntities = subscriptionDAO.getAppSubscribedUserNames(params, applicationReleaseId, tenantId); + params.removeAll(subscribedEntities); + if (!params.isEmpty()) { + subscriptionDAO.addUserSubscriptions(tenantId, username, params, applicationReleaseId, action); + } + } else if (SubscriptionType.ROLE.toString().equalsIgnoreCase(subType)) { + subscribedEntities = subscriptionDAO.getAppSubscribedRoleNames(params, applicationReleaseId, tenantId); + params.removeAll(subscribedEntities); + if (!params.isEmpty()) { + subscriptionDAO.addRoleSubscriptions(tenantId, username, params, applicationReleaseId, action); + } + } else if (SubscriptionType.GROUP.toString().equalsIgnoreCase(subType)) { + subscribedEntities = subscriptionDAO.getAppSubscribedGroupNames(params, applicationReleaseId, tenantId); + params.removeAll(subscribedEntities); + if (!params.isEmpty()) { + subscriptionDAO.addGroupSubscriptions(tenantId, username, params, applicationReleaseId, action); + } + } + + if (!subscribedEntities.isEmpty()) { + subscriptionDAO + .updateSubscriptions(tenantId, username, subscribedEntities, applicationReleaseId, subType, action); + } + } + /** * This method is responsible to get device IDs thta operation has added. * diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/APIUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/APIUtil.java index 55a4f63909..5765ecebf5 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/APIUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/APIUtil.java @@ -423,13 +423,13 @@ public class APIUtil { } public static String getArtifactDownloadBaseURL() throws ApplicationManagementException { - String host = System.getProperty(Constants.IOT_HOST_PROPERTY); + String host = System.getProperty(Constants.IOT_CORE_HOST); MDMConfig mdmConfig = ConfigurationManager.getInstance().getConfiguration().getMdmConfig(); String port; if (Constants.HTTP_PROTOCOL.equals(mdmConfig.getArtifactDownloadProtocol())){ - port = System.getProperty(Constants.IOT_HTTP_PORT_PROPERTY); + port = System.getProperty(Constants.IOT_CORE_HTTP_PORT); } else if( Constants.HTTPS_PROTOCOL.equals(mdmConfig.getArtifactDownloadProtocol())){ - port = System.getProperty(Constants.IOT_HTTPS_PORT_PROPERTY); + port = System.getProperty(Constants.IOT_CORE_HTTPS_PORT); } else { String msg = "In order to download application artifacts invalid protocols are defined."; log.error(msg); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java index 0f58452f54..cbbe2229c5 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java @@ -36,9 +36,9 @@ public class Constants { public static final String PLIST_NAME = "Info.plist"; public static final String CF_BUNDLE_VERSION = "CFBundleVersion"; public static final String APP_EXTENSION = ".app"; - public static final String IOT_HOST_PROPERTY = "iot.core.host"; - public static final String IOT_HTTP_PORT_PROPERTY = "iot.core.http.port"; - public static final String IOT_HTTPS_PORT_PROPERTY = "iot.core.https.port"; + public static final String IOT_CORE_HOST = "iot.core.host"; + public static final String IOT_CORE_HTTP_PORT = "iot.core.http.port"; + public static final String IOT_CORE_HTTPS_PORT = "iot.core.https.port"; public static final String HTTPS_PROTOCOL = "https"; public static final String HTTP_PROTOCOL = "http"; 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 148ea46385..f16927301b 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 @@ -3,7 +3,7 @@ -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS AP_APP( ID INTEGER NOT NULL AUTO_INCREMENT, - NAME VARCHAR(45) NOT NULL, + NAME VARCHAR(350) NOT NULL, DESCRIPTION CLOB NULL, TYPE VARCHAR(200) NOT NULL, TENANT_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 4d45a828da..78e05c1371 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 @@ -3,7 +3,7 @@ -- ----------------------------------------------------- CREATE TABLE AP_APP( ID INTEGER NOT NULL IDENTITY, - NAME VARCHAR(45) NOT NULL, + NAME VARCHAR(350) NOT NULL, DESCRIPTION VARCHAR(max) NULL, TYPE VARCHAR(200) NOT NULL, TENANT_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 3ca9a0c985..1daa796858 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 @@ -3,7 +3,7 @@ -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS AP_APP( ID INTEGER NOT NULL AUTO_INCREMENT, - NAME VARCHAR(45) NOT NULL, + NAME VARCHAR(350) NOT NULL, DESCRIPTION VARCHAR(200) NOT NULL, TYPE VARCHAR(200) NOT NULL, TENANT_ID INTEGER NOT NULL,