From c755dc018a53acbfb8fff0523e5c6797c40a43b8 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Wed, 28 Mar 2018 00:08:42 +0530 Subject: [PATCH] Add Application updating functionality --- .../application/mgt/common/Application.java | 16 ++-- .../mgt/core/dao/ApplicationDAO.java | 5 +- .../GenericApplicationDAOImpl.java | 78 +++++++++++-------- .../visibility/GenericVisibilityDAOImpl.java | 2 +- .../mgt/core/impl/ApplicationManagerImpl.java | 66 +++++++++++++--- .../impl/UnrestrictedRoleManagerImpl.java | 1 + 6 files changed, 115 insertions(+), 53 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java index 646d03affd1..d4f5e6a5f8d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java @@ -21,6 +21,7 @@ package org.wso2.carbon.device.application.mgt.common; import org.wso2.carbon.device.application.mgt.common.jaxrs.Exclude; import org.wso2.carbon.device.mgt.core.dto.DeviceType; + import java.util.List; /** @@ -95,13 +96,6 @@ public class Application { public ImageArtifact iconOfLatestRelease; - public List getUnrestrictedRoles() { - return unrestrictedRoles; - } - - public void setUnrestrictedRoles(List unrestrictedRoles) { - this.unrestrictedRoles = unrestrictedRoles; - } public String getType() { return type; @@ -166,4 +160,12 @@ public class Application { public void setApplicationReleases(List applicationReleases) { this.applicationReleases = applicationReleases; } + + public List getUnrestrictedRoles() { + return unrestrictedRoles; + } + + public void setUnrestrictedRoles(List unrestrictedRoles) { + this.unrestrictedRoles = unrestrictedRoles; + } } 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 5130ad29596..d7b6f45e0d1 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 @@ -143,14 +143,15 @@ public interface ApplicationDAO { */ int getApplicationCount(Filter filter, int tenantId) throws ApplicationManagementDAOException; - /** * To delete the tags of a application. * + * @param tags Tags which are going to delete. * @param applicationId ID of the application to delete the tags. + * @param tenantId Tenant Id * @throws ApplicationManagementDAOException Application Management DAO Exception. */ - void deleteTags(int applicationId) throws ApplicationManagementDAOException; + void deleteTags(List tags, int applicationId, int tenantId) throws ApplicationManagementDAOException; /** * To get an {@link Application} associated with the given release 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 caa3f666d4d..a431dcbf29a 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 @@ -93,36 +93,6 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } } - @Override - public void addTags(List tags, int applicationId, int tenantId) throws ApplicationManagementDAOException { - if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to add tags"); - } - Connection conn; - PreparedStatement stmt = null; - String sql = "INSERT INTO AP_APP_TAG (TAG, TENANT_ID, AP_APP_ID) VALUES (?, ?, ?)"; - try { - conn = this.getDBConnection(); - conn.setAutoCommit(false); - stmt = conn.prepareStatement(sql); - for (Tag tag : tags) { - stmt.setString(1, tag.getTagName()); - stmt.setInt(2, tenantId); - stmt.setInt(3, applicationId); - stmt.addBatch(); - } - stmt.executeBatch(); - - } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Error occurred while obtaining the DB connection when adding tags", e); - } catch (SQLException e) { - throw new ApplicationManagementDAOException("Error occurred while adding tags", e); - } finally { - Util.cleanupResources(stmt, null); - } - } - @Override public int isExistApplication(String appName, String type, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { @@ -531,15 +501,55 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } @Override - public void deleteTags(int applicationId) throws ApplicationManagementDAOException { + public void addTags(List tags, int applicationId, int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to add tags"); + } Connection conn; PreparedStatement stmt = null; + String sql = "INSERT INTO AP_APP_TAG (TAG, TENANT_ID, AP_APP_ID) VALUES (?, ?, ?)"; try { conn = this.getDBConnection(); - String sql = "DELETE FROM AP_APP_TAG WHERE ID = ?"; + conn.setAutoCommit(false); stmt = conn.prepareStatement(sql); - stmt.setInt(1, applicationId); - stmt.executeUpdate(); + for (Tag tag : tags) { + stmt.setString(1, tag.getTagName()); + stmt.setInt(2, tenantId); + stmt.setInt(3, applicationId); + stmt.addBatch(); + } + stmt.executeBatch(); + + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException( + "Error occurred while obtaining the DB connection when adding tags", e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException("Error occurred while adding tags", e); + } finally { + Util.cleanupResources(stmt, null); + } + } + + @Override + public void deleteTags(List tags, int applicationId, int tenantId) throws ApplicationManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + + String sql = "DELETE FROM AP_APP_TAG WHERE ID = ? AND AP_APP_ID = ? AND TENANT_ID = ?;"; + try{ + conn = this.getDBConnection(); + conn.setAutoCommit(false); + stmt = conn.prepareStatement(sql); + + for (Tag tag : tags) { + stmt.setInt(1, tag.getId()); + stmt.setInt(2, applicationId); + stmt.setInt(3, tenantId); + stmt.addBatch(); + } + stmt.executeBatch(); + + } catch (DBConnectionException e) { throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); 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/visibility/GenericVisibilityDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java index 0722b36a0b4..9ae0719cd3f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java @@ -80,7 +80,7 @@ public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements Visibil PreparedStatement stmt = null; ResultSet rs = null; List unrestrictedRoles = new ArrayList<>(); - UnrestrictedRole unrestrictedRole = null; + UnrestrictedRole unrestrictedRole; String sql = "SELECT ID, ROLE FROM AP_UNRESTRICTED_ROLES WHERE AP_APP_ID = ? AND TENANT_ID = ?;"; try{ conn = this.getDBConnection(); 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 221d310c65c..1f80ac0fb21 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 @@ -31,6 +31,7 @@ import org.wso2.carbon.device.application.mgt.common.ApplicationType; import org.wso2.carbon.device.application.mgt.common.Filter; import org.wso2.carbon.device.application.mgt.common.LifecycleState; import org.wso2.carbon.device.application.mgt.common.SortingOrder; +import org.wso2.carbon.device.application.mgt.common.Tag; import org.wso2.carbon.device.application.mgt.common.UnrestrictedRole; import org.wso2.carbon.device.application.mgt.common.User; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; @@ -56,6 +57,7 @@ import org.wso2.carbon.utils.multitenancy.MultitenantUtils; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.sql.Timestamp; import java.util.Date; @@ -223,7 +225,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } - private boolean isRoleExists(List unrestrictedRoleList, String userName) + private boolean isRoleExists(Collection unrestrictedRoleList, String userName) throws UserStoreException { String[] roleList; roleList = getRolesOfUser(userName); @@ -811,7 +813,17 @@ public class ApplicationManagerImpl implements ApplicationManager { @Override public Application updateApplication(Application application) throws ApplicationManagementException { + + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); Application existingApplication = validateApplication(application.getId()); + ApplicationDAO applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); + VisibilityDAO visibilityDAO = ApplicationManagementDAOFactory.getVisibilityDAO(); + List addingRoleList; + List removingRoleList; + List addingTags; + List removingTags; + + if (existingApplication == null) { throw new NotFoundException("Tried to update Application which is not in the publisher, " + "Please verify application details"); @@ -837,25 +849,51 @@ public class ApplicationManagerImpl implements ApplicationManager { } } } - //todo get restricted roles and assign for application if (existingApplication.getIsRestricted() != application.getIsRestricted()) { - if (existingApplication.getIsRestricted() == 1) { + if (existingApplication.getIsRestricted() == 0 && existingApplication.getUnrestrictedRoles() == null) { if (application.getUnrestrictedRoles() == null || application.getUnrestrictedRoles().isEmpty()) { throw new ApplicationManagementException("If you are going to add role restriction for non role " + "restricted Application, Unrestricted role list " + "won't be empty or null"); } - } else if (existingApplication.getIsRestricted() == 0) { + visibilityDAO.addUnrestrictedRoles(application.getUnrestrictedRoles(), application.getId(), tenantId); + } else if (existingApplication.getIsRestricted() == 1 && existingApplication.getUnrestrictedRoles() != + null) { if (application.getUnrestrictedRoles() != null || !application.getUnrestrictedRoles().isEmpty()) { throw new ApplicationManagementException("If you are going to remove role restriction from role " + - "restricted Application, Unrestricted role list should be empty or null"); + "restricted Application, Unrestricted role list " + + "should be empty or null"); + } + visibilityDAO.deleteUnrestrictedRoles(existingApplication.getUnrestrictedRoles(), application.getId(), + tenantId); + } + } else if (existingApplication.getIsRestricted() == application.getIsRestricted()) { + if (existingApplication.getIsRestricted() == 1) { + addingRoleList = getDifference(application.getUnrestrictedRoles(), existingApplication + .getUnrestrictedRoles()); + removingRoleList = getDifference(existingApplication + .getUnrestrictedRoles(), application.getUnrestrictedRoles()); + if (!addingRoleList.isEmpty()) { + visibilityDAO.addUnrestrictedRoles(addingRoleList, application.getId(), tenantId); + } + if (!removingRoleList.isEmpty()) { + visibilityDAO.deleteUnrestrictedRoles(removingRoleList, application.getId(), tenantId); + + } + } - //todo update role restriction } - //todo get tags and assign for application verify - //todo update application - return application; + addingTags = getDifference(existingApplication.getTags(), application.getTags()); + removingTags = getDifference(application.getTags(), existingApplication.getTags()); + if (!addingTags.isEmpty()) { + applicationDAO.addTags(addingTags, application.getId(), tenantId); + } + if (!removingTags.isEmpty()) { + applicationDAO.deleteTags(removingTags, application.getId(), tenantId); + } + + return applicationDAO.editApplication(application, tenantId); } private Filter validateFilter(Filter filter) { @@ -879,4 +917,14 @@ public class ApplicationManagerImpl implements ApplicationManager { } return filter; } + + private List getDifference(List list1, Collection list2) { + List list = new ArrayList<>(); + for (T t : list1) { + if(!list2.contains(t)) { + list.add(t); + } + } + return list; + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/UnrestrictedRoleManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/UnrestrictedRoleManagerImpl.java index c4ab85ee8fa..861246ec539 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/UnrestrictedRoleManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/UnrestrictedRoleManagerImpl.java @@ -26,6 +26,7 @@ import org.wso2.carbon.device.application.mgt.core.dao.VisibilityDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; +import java.util.Collection; import java.util.List; //todo need to work on business logic