From ec0be0183e2d07fccf316d62f21640bd54c76857 Mon Sep 17 00:00:00 2001 From: megala21 Date: Thu, 3 Aug 2017 22:15:02 +0530 Subject: [PATCH] Re-structuring and adding changes after testing --- .../impl/PlatformManagementAPIImpl.java | 4 + .../application/mgt/common/Platform.java | 2 + .../application/mgt/core/dao/PlatformDAO.java | 6 + .../impl/platform/GenericPlatformDAOImpl.java | 209 ++++++++++++----- .../mgt/core/impl/PlatformManagerImpl.java | 219 ++++++++++++------ .../dbscripts/cdm/application-mgt/h2.sql | 1 + .../dbscripts/cdm/application-mgt/mysql.sql | 1 + 7 files changed, 322 insertions(+), 120 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/PlatformManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/PlatformManagementAPIImpl.java index 7d342b55a2c..de6d0829ddc 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/PlatformManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/PlatformManagementAPIImpl.java @@ -101,6 +101,10 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { Platform platform = APIUtil.getPlatformManager().getPlatform(tenantId, id); + + if (platform == null) { + return Response.status(Response.Status.NOT_FOUND).entity("Platform not found").build(); + } return Response.status(Response.Status.OK).entity(platform).build(); } catch (PlatformManagementDAOException e) { log.error("Error while trying the get the platform with the identifier : " + id + " for the tenant :" diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Platform.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Platform.java index af7dc7133e8..3ac3ef3ead1 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Platform.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Platform.java @@ -68,6 +68,8 @@ public class Platform { this.iconName = platform.getIconName(); this.fileBased = platform.isFileBased(); this.shared = platform.isShared(); + this.enabled = platform.isEnabled(); + this.defaultTenantMapping = platform.isDefaultTenantMapping(); if (platform.getProperties() != null) { this.properties = new ArrayList<>(); for (Property property : platform.getProperties()) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/PlatformDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/PlatformDAO.java index 770f4cd7fec..c67971a7f14 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/PlatformDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/PlatformDAO.java @@ -48,4 +48,10 @@ public interface PlatformDAO { void removePlatforms(int tenantId) throws PlatformManagementDAOException; + int getSuperTenantAndOwnPlatforms(String platformIdentifier, int tenantId) throws PlatformManagementDAOException; + + Platform getTenantOwnedPlatform(int tenantId, String platformIdentifier) throws PlatformManagementDAOException; + + int getMultiTenantPlatforms(String identifier) throws PlatformManagementDAOException; + } 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/platform/GenericPlatformDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/platform/GenericPlatformDAOImpl.java index 954d203294e..cb98c1fb297 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/platform/GenericPlatformDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/platform/GenericPlatformDAOImpl.java @@ -41,8 +41,7 @@ import java.util.List; public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformDAO { private static Log log = LogFactory.getLog(GenericPlatformDAOImpl.class); - @Override - public int register(int tenantId, Platform platform) throws PlatformManagementDAOException { + @Override public int register(int tenantId, Platform platform) throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; try { int platformId = getPlatformId(tenantId, platform.getIdentifier()); @@ -50,7 +49,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD Connection connection = this.getDBConnection(); if (!platform.isFileBased()) { String insertToPlatform = "INSERT INTO APPM_PLATFORM (IDENTIFIER, TENANT_ID, NAME, FILE_BASED, " - + "DESCRIPTION, IS_SHARED, ICON_NAME)" + " VALUES (?, ?, ?, ?, ?, ?, ?)"; + + "DESCRIPTION, IS_SHARED, ICON_NAME, IS_DEFAULT_TENANT_MAPPING)" + " VALUES (?, ?, ?, ?, " + + "?, ?, ?, ?)"; preparedStatement = connection.prepareStatement(insertToPlatform); preparedStatement.setString(1, platform.getIdentifier()); preparedStatement.setInt(2, tenantId); @@ -59,6 +59,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD preparedStatement.setString(5, platform.getDescription()); preparedStatement.setBoolean(6, platform.isShared()); preparedStatement.setString(7, platform.getIconName()); + preparedStatement.setBoolean(8, platform.isDefaultTenantMapping()); preparedStatement.execute(); platformId = getPlatformId(tenantId, platform.getIdentifier()); @@ -78,11 +79,14 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } else { String insertToPlatform = - "INSERT INTO APPM_PLATFORM (IDENTIFIER, TENANT_ID, FILE_BASED)" + " VALUES (?, ?, ?)"; + "INSERT INTO APPM_PLATFORM (IDENTIFIER, TENANT_ID, FILE_BASED, IS_SHARED, " + + "IS_DEFAULT_TENANT_MAPPING) VALUES (?, ?, ?, ?, ?)"; preparedStatement = connection.prepareStatement(insertToPlatform); preparedStatement.setString(1, platform.getIdentifier()); preparedStatement.setInt(2, tenantId); preparedStatement.setBoolean(3, true); + preparedStatement.setBoolean(4, platform.isShared()); + preparedStatement.setBoolean(5, platform.isDefaultTenantMapping()); preparedStatement.execute(); } if (platformId == -1) { @@ -109,9 +113,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - @Override - public void update(int tenantId, String oldPlatformIdentifier, Platform platform) throws - PlatformManagementDAOException { + @Override public void update(int tenantId, String oldPlatformIdentifier, Platform platform) + throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; try { int platformId = getPlatformId(tenantId, oldPlatformIdentifier); @@ -121,7 +124,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD if (platformId != -1) { Connection connection = this.getDBConnection(); if (!platform.isFileBased()) { - String insertToPlatform = "UPDATE APPM_PLATFORM SET DESCRIPTION=?, IS_SHARED=?, ICON_NAME=?"; + String insertToPlatform = "UPDATE APPM_PLATFORM SET DESCRIPTION=?, IS_SHARED=?, ICON_NAME=?, " + + "IS_DEFAULT_TENANT_MAPPING=?"; if (!isIdentifierNull) { insertToPlatform += ",IDENTIFIER = ? "; } @@ -133,19 +137,20 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD preparedStatement.setString(1, platform.getDescription()); preparedStatement.setBoolean(2, platform.isShared()); preparedStatement.setString(3, platform.getIconName()); + preparedStatement.setBoolean(4, platform.isDefaultTenantMapping()); if (!isIdentifierNull && !isNameNull) { - preparedStatement.setString(4, platform.getIdentifier()); + preparedStatement.setString(5, platform.getIdentifier()); + preparedStatement.setString(6, platform.getName()); + preparedStatement.setInt(7, platformId); + } else if (isIdentifierNull && !isNameNull) { preparedStatement.setString(5, platform.getName()); preparedStatement.setInt(6, platformId); - } else if (isIdentifierNull && !isNameNull) { - preparedStatement.setString(4, platform.getName()); - preparedStatement.setInt(5, platformId); } else if (!isIdentifierNull) { - preparedStatement.setString(4, platform.getIdentifier()); - preparedStatement.setInt(5, platformId); + preparedStatement.setString(5, platform.getIdentifier()); + preparedStatement.setInt(6, platformId); } else { - preparedStatement.setInt(4, platformId); + preparedStatement.setInt(5, platformId); } preparedStatement.execute(); @@ -217,10 +222,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - - @Override - public void unregister(int tenantId, String platformIdenfier, boolean isFileBased) throws - PlatformManagementDAOException { + @Override public void unregister(int tenantId, String platformIdenfier, boolean isFileBased) + throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; try { Platform platform = getPlatform(tenantId, platformIdenfier); @@ -284,8 +287,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - private int getTenantPlatformMapping(int tenantId, String platformIdentifier) throws - PlatformManagementDAOException { + private int getTenantPlatformMapping(int tenantId, String platformIdentifier) + throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; ResultSet resultSet = null; String getMapping = "SELECT MAPPING.ID as ID FROM (SELECT ID, PLATFORM_ID FROM APPM_PLATFORM_TENANT_MAPPING " @@ -312,8 +315,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - @Override - public void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementDAOException { + @Override public void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementDAOException { String deleteMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE ID = ?"; PreparedStatement preparedStatement = null; try { @@ -338,8 +340,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - @Override - public void removeMappingTenants(String platformIdentifier) throws PlatformManagementDAOException { + @Override public void removeMappingTenants(String platformIdentifier) throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; int platformId = getPlatformId(MultitenantConstants.SUPER_TENANT_ID, platformIdentifier); String getMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE TENANT_ID != ? AND PLATFORM_ID=?"; @@ -360,23 +361,21 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - - @Override - public List getPlatforms(int tenantId) throws PlatformManagementDAOException { + @Override public List getPlatforms(int tenantId) throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; ResultSet resultSet = null; if (log.isDebugEnabled()) { log.debug("GetPlaforms request received for the tenant ID " + tenantId); } - String selectQuery = - "SELECT MAPPING.ID, PLATFORM.IDENTIFIER FROM (SELECT * FROM APPM_PLATFORM WHERE TENANT_ID=? OR " - + "IS_SHARED = TRUE AND FILE_BASED = FALSE) PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING " - + "MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID"; + String selectQuery = "SELECT MAPPING.ID, PLATFORM.IDENTIFIER FROM (SELECT * FROM APPM_PLATFORM " + + "WHERE TENANT_ID=? OR IS_SHARED = TRUE ) PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING " + + "MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID AND MAPPING.TENANT_ID = ?"; try { Connection connection = this.getDBConnection(); preparedStatement = connection.prepareStatement(selectQuery); preparedStatement.setInt(1, tenantId); + preparedStatement.setInt(2, tenantId); resultSet = preparedStatement.executeQuery(); List platforms = new ArrayList<>(); if (log.isDebugEnabled()) { @@ -413,9 +412,9 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD public Platform getPlatform(String tenantDomain, String platformIdentifier) throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; ResultSet resultSet = null; - String platformQuery = "SELECT * FROM (SELECT * FROM APPM_PLATFORM WHERE (TENANT_DOMAIN=? AND IDENTIFIER=?) " + - "OR (IS_SHARED = TRUE AND IDENTIFIER=?) AND FILE_BASED = FALSE ) PLATFORM " + - "LEFT JOIN APPM_PLATFORM_PROPERTIES PROPS ON PLATFORM.ID = PROPS.PLATFORM_ID"; + String platformQuery = "SELECT * FROM (SELECT * FROM APPM_PLATFORM WHERE (TENANT_DOMAIN=? AND IDENTIFIER=?) " + + "OR (IS_SHARED = TRUE AND IDENTIFIER=?) AND FILE_BASED = FALSE ) PLATFORM " + + "LEFT JOIN APPM_PLATFORM_PROPERTIES PROPS ON PLATFORM.ID = PROPS.PLATFORM_ID"; try { Connection connection = this.getDBConnection(); preparedStatement = connection.prepareStatement(platformQuery); @@ -457,8 +456,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - @Override - public Platform getPlatform(int tenantId, String identifier) throws PlatformManagementDAOException { + @Override public Platform getPlatform(int tenantId, String identifier) throws PlatformManagementDAOException { Connection conn; PreparedStatement stmt = null; ResultSet rs = null; @@ -467,13 +465,15 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD try { conn = this.getDBConnection(); sql = "SELECT MAPPING.ID, PLATFORM.IDENTIFIER, PLATFORM.FILE_BASED, PLATFORM.ID, PLATFORM.NAME, PLATFORM" - + ".DESCRIPTION, PLATFORM.ICON_NAME, PLATFORM.IS_SHARED FROM (SELECT * FROM APPM_PLATFORM WHERE " - + "IDENTIFIER= ? AND (TENANT_ID=? OR IS_SHARED = TRUE AND FILE_BASED = FALSE)) PLATFORM LEFT JOIN " - + "APPM_PLATFORM_TENANT_MAPPING MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID"; + + ".DESCRIPTION, PLATFORM.ICON_NAME, PLATFORM.IS_SHARED, PLATFORM.IS_DEFAULT_TENANT_MAPPING FROM " + + "(SELECT * FROM APPM_PLATFORM WHERE IDENTIFIER= ? AND (TENANT_ID=? OR IS_SHARED = TRUE)) " + + "PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID " + + "AND MAPPING.TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, identifier); stmt.setInt(2, tenantId); + stmt.setInt(3, tenantId); rs = stmt.executeQuery(); Platform platform = null; @@ -481,15 +481,15 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD if (rs.next()) { platform = new Platform(); platform.setFileBased(rs.getBoolean(3)); - platform.setIdentifier(rs.getString(2)); + platform.setShared(rs.getBoolean(8)); + platform.setDefaultTenantMapping(rs.getBoolean(9)); + if (!platform.isFileBased()) { platform.setId(rs.getInt(4)); platform.setName(rs.getString(5)); platform.setDescription(rs.getString(6)); platform.setIconName(rs.getString(7)); - platform.setShared(rs.getBoolean(8)); - if (rs.getInt(1) != 0) { platform.setEnabled(true); } else { @@ -499,17 +499,17 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } return platform; } catch (SQLException e) { - throw new PlatformManagementDAOException("Error occurred while getting platform with the identifier " + - identifier + ", for the tenant : " + tenantId, e); - } catch (DBConnectionException e) { + throw new PlatformManagementDAOException( + "Error occurred while getting platform with the identifier " + identifier + ", for the tenant : " + + tenantId, e); + } catch (DBConnectionException e) { throw new PlatformManagementDAOException("Error occurred while obtaining the DB connection.", e); } finally { Util.cleanupResources(stmt, rs); } } - @Override - public void removePlatforms(int tenantId) throws PlatformManagementDAOException { + @Override public void removePlatforms(int tenantId) throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; String sql = "DELETE FROM APPM_PLATFORM WHERE TENANT_ID = ?"; @@ -519,13 +519,118 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD preparedStatement.setInt(1, tenantId); preparedStatement.executeUpdate(); } catch (DBConnectionException e) { - throw new PlatformManagementDAOException("Database connection error while removing the platforms for the " - + "tenant - " + tenantId); + throw new PlatformManagementDAOException( + "Database connection error while removing the platforms for the " + "tenant - " + tenantId); } catch (SQLException e) { - throw new PlatformManagementDAOException("SQL exception while executing the query " + sql + " for " - + "the tenant : " + tenantId); + throw new PlatformManagementDAOException( + "SQL exception while executing the query " + sql + " for " + "the tenant : " + tenantId); } finally { Util.cleanupResources(preparedStatement, null); } } + + @Override public int getSuperTenantAndOwnPlatforms(String platformIdentifier, int tenantId) + throws PlatformManagementDAOException { + PreparedStatement preparedStatement = null; + ResultSet resultSet = null; + String sql = "SELECT ID from APPM_PLATFORM where IDENTIFIER = ? AND (TENANT_ID = ? OR (TENANT_ID = ? AND " + + "IS_SHARED = true)"; + + try { + Connection connection = this.getDBConnection(); + preparedStatement = connection.prepareStatement(sql); + preparedStatement.setString(1, platformIdentifier); + preparedStatement.setInt(2, tenantId); + preparedStatement.setInt(3, MultitenantConstants.SUPER_TENANT_ID); + resultSet = preparedStatement.executeQuery(); + + if (resultSet.next()) { + return resultSet.getInt(1); + } else { + return -1; + } + } catch (DBConnectionException e) { + throw new PlatformManagementDAOException( + "Database connection error while removing the platfor for the " + "tenant - " + tenantId); + } catch (SQLException e) { + throw new PlatformManagementDAOException( + "SQL exception while executing the query " + sql + " for " + "the tenant : " + tenantId); + } finally { + Util.cleanupResources(preparedStatement, resultSet); + } + + } + + @Override public Platform getTenantOwnedPlatform(int tenantId, String platformIdentifier) + throws PlatformManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + String sql = ""; + + try { + conn = this.getDBConnection(); + sql = "SELECT * from APPM_PLATFORM WHERE TENANT_ID = ? AND IDENTIFIER = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, tenantId); + stmt.setString(2, platformIdentifier); + rs = stmt.executeQuery(); + + Platform platform = null; + + if (rs.next()) { + platform = new Platform(); + platform.setFileBased(rs.getBoolean("FILE_BASED")); + platform.setIdentifier(rs.getString("IDENTIFIER")); + platform.setShared(rs.getBoolean("IS_SHARED")); + platform.setDefaultTenantMapping(rs.getBoolean("IS_DEFAULT_TENANT_MAPPING")); + if (!platform.isFileBased()) { + platform.setId(rs.getInt("ID")); + platform.setName(rs.getString("NAME")); + platform.setDescription(rs.getString("DESCRIPTION")); + platform.setIconName(rs.getString("ICON_NAME")); + } + } + return platform; + } catch (SQLException e) { + throw new PlatformManagementDAOException("Error occurred while executing the query : " + sql + " for " + + "getting platforms owned by tenant : " + tenantId, e); + } catch (DBConnectionException e) { + throw new PlatformManagementDAOException( + "Error occurred while obtaining the DB connection for getting " + "platforms owned by tenant : " + + tenantId, e); + } finally { + Util.cleanupResources(stmt, rs); + } + } + + @Override + public int getMultiTenantPlatforms(String identifier) throws PlatformManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + String sql = ""; + + try { + conn = this.getDBConnection(); + sql = "SELECT ID from APPM_PLATFORM WHERE TENANT_ID != ? AND IDENTIFIER=?"; + + stmt = conn.prepareStatement(sql); + stmt.setInt(1, MultitenantConstants.SUPER_TENANT_ID); + stmt.setString(2, identifier); + + rs = stmt.executeQuery(); + + if (rs.next()) { + return rs.getInt(1); + } + return -1; + } catch (DBConnectionException e) { + throw new PlatformManagementDAOException("Database Connection exception while trying to get the tenants " + + "which has the platforms with the platform identifier : " + identifier, e); + } catch (SQLException e) { + throw new PlatformManagementDAOException("SQL exception while executing the query " + sql + " to get the" + + " tenants which has the platform with the platform identifier : " + identifier, 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/PlatformManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformManagerImpl.java index 31ab5552b34..c9d7546a93f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformManagerImpl.java @@ -156,8 +156,7 @@ public class PlatformManagerImpl implements PlatformManager { } else { return new Platform(platform); } - throw new PlatformManagementException( - "No platform was found for tenant - " + tenantId + " with platform identifier - " + identifier); + return null; } private Platform getPlatformFromInMemory(int tenantId, String identifier) { @@ -182,11 +181,7 @@ public class PlatformManagerImpl implements PlatformManager { @Override public synchronized void register(int tenantId, Platform platform) throws PlatformManagementException { - if (platform.isShared() && tenantId != MultitenantConstants.SUPER_TENANT_ID) { - throw new PlatformManagementException( - "Platform sharing is a restricted operation, therefore Platform - " + platform.getIdentifier() - + " cannot be shared by the tenant domain - " + tenantId); - } + validateBeforeRegister(tenantId, platform); try { ConnectionManagerUtil.beginDBTransaction(); int platformId = DAOFactory.getPlatformDAO().register(tenantId, platform); @@ -208,12 +203,7 @@ public class PlatformManagerImpl implements PlatformManager { if (platform.isDefaultTenantMapping()) { try { if (platform.isShared()) { - TenantManager tenantManager = DataHolder.getInstance().getRealmService().getTenantManager(); - Tenant[] tenants = tenantManager.getAllTenants(); - for (Tenant tenant : tenants) { - DAOFactory.getPlatformDAO() - .addMapping(tenant.getId(), getListOfString(platform.getIdentifier())); - } + sharePlatformWithOtherTenants(platform.getIdentifier()); } DAOFactory.getPlatformDAO().addMapping(tenantId, getListOfString(platform.getIdentifier())); } catch (UserStoreException e) { @@ -244,70 +234,38 @@ public class PlatformManagerImpl implements PlatformManager { @Override public void update(int tenantId, String oldPlatformIdentifier, Platform platform) throws PlatformManagementException { - if (platform.isShared() && tenantId != MultitenantConstants.SUPER_TENANT_ID) { - throw new PlatformManagementException( - "Platform sharing is a restricted operation, therefore Platform - " + platform.getIdentifier() - + " cannot be shared by the tenant domain - " + tenantId); - } + Platform oldPlatform = validateBeforeUpdate(tenantId, oldPlatformIdentifier, platform); try { ConnectionManagerUtil.beginDBTransaction(); - Platform oldPlatform = DAOFactory.getPlatformDAO().getPlatform(tenantId, oldPlatformIdentifier); - - if (oldPlatform == null) { - ConnectionManagerUtil.commitDBTransaction(); - throw new PlatformManagementException( - "Cannot update platform. Platform with identifier : " + oldPlatformIdentifier - + " does not exist."); - } - if (platform.getIdentifier() != null && !platform.getIdentifier().equals(oldPlatformIdentifier)) { - Platform existingPlatform = DAOFactory.getPlatformDAO().getPlatform(tenantId, platform.getIdentifier()); - - if (existingPlatform != null) { - ConnectionManagerUtil.commitDBTransaction(); - throw new PlatformManagementException( - "Cannot update the identifier of the platform from '" + oldPlatformIdentifier + "' to '" - + platform.getIdentifier() + "'. Another platform exists " - + "already with the identifier '" + platform.getIdentifier() + "' for the tenant : " - + tenantId); - } - } - if (platform.isFileBased()) { Map tenantPlatforms = this.inMemoryStore.get(tenantId); - // File based configurations will be updated in the server start-up as well.So in that case, cache, // will be empty. - if (tenantPlatforms == null) { + if (tenantPlatforms != null) { + if (tenantPlatforms.get(oldPlatformIdentifier) == null) { + throw new PlatformManagementException( + "Cannot update platform with identifier " + oldPlatformIdentifier + " as it is not " + + " existing already for the tenant " + tenantId); + } + } else { tenantPlatforms = new HashMap<>(); this.inMemoryStore.put(tenantId, tenantPlatforms); } - if (tenantPlatforms.get(platform.getIdentifier()) == null) { - tenantPlatforms.put(platform.getIdentifier(), platform); - } DAOFactory.getPlatformDAO().update(tenantId, oldPlatformIdentifier, platform); platform.setId(oldPlatform.getId()); tenantPlatforms.put(platform.getIdentifier(), platform); - } else { DAOFactory.getPlatformDAO().update(tenantId, oldPlatformIdentifier, platform); } - if (platform.isDefaultTenantMapping() && !oldPlatform.isDefaultTenantMapping()) { - try { - if (platform.isShared() && !oldPlatform.isShared()) { - TenantManager tenantManager = DataHolder.getInstance().getRealmService().getTenantManager(); - Tenant[] tenants = tenantManager.getAllTenants(); - for (Tenant tenant : tenants) { - DAOFactory.getPlatformDAO() - .addMapping(tenant.getId(), getListOfString(platform.getIdentifier())); - } - } - DAOFactory.getPlatformDAO().addMapping(tenantId, getListOfString(platform.getIdentifier())); - } catch (UserStoreException e) { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!", - e); + try { + if (platform.isShared() && !oldPlatform.isShared()) { + sharePlatformWithOtherTenants(platform.getIdentifier()); } + } catch (UserStoreException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!", + e); } if (!platform.isShared() && oldPlatform.isShared()) { DAOFactory.getPlatformDAO().removeMappingTenants(platform.getIdentifier()); @@ -331,12 +289,6 @@ public class PlatformManagerImpl implements PlatformManager { } } - private List getListOfString(String platformIdentifier) { - List identifiers = new ArrayList<>(); - identifiers.add(platformIdentifier); - return identifiers; - } - @Override public void unregister(int tenantId, String identifier, boolean isFileBased) throws PlatformManagementException { try { @@ -440,7 +392,6 @@ public class PlatformManagerImpl implements PlatformManager { } else { DAOFactory.getPlatformDAO().removeMapping(tenantId, platform.getIdentifier()); } - if (log.isDebugEnabled()) { log.debug("Platform with identifier : " + platformIdentifier + " successfully " + (isEnabledNewStatus ? "Enabled" : "Disabled")); @@ -458,7 +409,6 @@ public class PlatformManagerImpl implements PlatformManager { } finally { ConnectionManagerUtil.closeDBConnection(); } - } @Override @@ -479,4 +429,137 @@ public class PlatformManagerImpl implements PlatformManager { } } + /** + * To share the super-tenant platform with other tenants + * @param platformIdentifier Identifier of the platform + * @throws UserStoreException User Store Exception + * @throws PlatformManagementDAOException Platform Management DAO Exception + */ + private void sharePlatformWithOtherTenants(String platformIdentifier) + throws UserStoreException, PlatformManagementDAOException { + TenantManager tenantManager = DataHolder.getInstance().getRealmService().getTenantManager(); + Tenant[] tenants = tenantManager.getAllTenants(); + for (Tenant tenant : tenants) { + DAOFactory.getPlatformDAO() + .addMapping(tenant.getId(), getListOfString(platformIdentifier)); + } + } + + /** + * Validation need to be done before registering the platform + * + * @param tenantId ID of the tenant which the platform need to registered to + * @param platform Platform that need to be registered + * @throws PlatformManagementException Platform Management Exception + */ + private void validateBeforeRegister(int tenantId, Platform platform) throws PlatformManagementException { + validatePlatformSharing(tenantId, platform); + try { + ConnectionManagerUtil.beginDBTransaction(); + int existingPlatformId = DAOFactory.getPlatformDAO() + .getSuperTenantAndOwnPlatforms(platform.getIdentifier(), tenantId); + ConnectionManagerUtil.commitDBTransaction(); + if (existingPlatformId != -1) { + throw new PlatformManagementException( + "Another platform exists with the identifier " + platform.getIdentifier() + " in the tenant " + + tenantId + " or super-tenant. Please choose a " + + "different identifier for your platform"); + } + } catch (TransactionManagementException | DBConnectionException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new PlatformManagementException( + "Error while checking pre-conditions before registering" + " platform identifier '" + platform + .getIdentifier() + "' for the tenant :" + tenantId); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + /** + * Validations that need to be done before updating the platform + * + * @param tenantId ID of the tenant + * @param oldPlatformIdentifier Identifier of the old platform + * @param platform Updated platform + * @return Old platform if all the validation succeeds + * @throws PlatformManagementException Platform ManagementException + */ + private Platform validateBeforeUpdate(int tenantId, String oldPlatformIdentifier, Platform platform) throws + PlatformManagementException { + validatePlatformSharing(tenantId, platform); + try { + ConnectionManagerUtil.beginDBTransaction(); + Platform oldPlatform = DAOFactory.getPlatformDAO().getTenantOwnedPlatform(tenantId, oldPlatformIdentifier); + if (oldPlatform == null) { + ConnectionManagerUtil.commitDBTransaction(); + throw new PlatformManagementException( + "Cannot update platform. Platform with identifier : " + oldPlatformIdentifier + + " does not exist for the tenant : " + tenantId); + } + if (platform.getIdentifier() != null && !platform.getIdentifier().equals(oldPlatformIdentifier)) { + int existingPlatformID = DAOFactory.getPlatformDAO() + .getSuperTenantAndOwnPlatforms(platform.getIdentifier(), tenantId); + ConnectionManagerUtil.commitDBTransaction(); + if (existingPlatformID == -1) { + throw new PlatformManagementException( + "Cannot update the identifier of the platform from '" + oldPlatformIdentifier + "' to '" + + platform.getIdentifier() + "'. Another platform exists " + + "already with the identifier '" + platform.getIdentifier() + "' for the tenant : " + + tenantId + " or in super-tenant"); + } + } + return oldPlatform; + } catch (TransactionManagementException | DBConnectionException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new PlatformManagementException( + "Database error while validating the platform update with the " + "platform identifier: " + + oldPlatformIdentifier + " for the tenant :" + tenantId); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + /** + * To validate whether this platform can be shared or not before registering and updating the platform + * + * @param tenantId ID of the tenant + * @param platform Platform to be validated for sharing + */ + private void validatePlatformSharing(int tenantId, Platform platform) throws PlatformManagementException { + if (platform.isShared() && tenantId != MultitenantConstants.SUPER_TENANT_ID) { + throw new PlatformManagementException( + "Platform sharing is a restricted operation, therefore Platform - " + platform.getIdentifier() + + " cannot be shared by the tenant domain - " + tenantId); + } + try { + ConnectionManagerUtil.beginDBTransaction(); + if (platform.isShared()) { + int sharedPlatform = DAOFactory.getPlatformDAO().getMultiTenantPlatforms(platform.getIdentifier()); + ConnectionManagerUtil.commitDBTransaction(); + if (sharedPlatform != -1) { + throw new PlatformManagementException( + "Platform '" + platform.getIdentifier() + "' cannot be shared as some other tenants have " + + "platforms with the same identifier."); + } + } + } catch (TransactionManagementException | DBConnectionException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new PlatformManagementException( + "Error while checking platform sharing conditions for " + " platform identifier '" + platform + .getIdentifier() + "' for the tenant :" + tenantId); + } finally { + ConnectionManagerUtil.rollbackDBTransaction(); + } + } + + /** + * To get the list of the given platform Identifier + * @param platformIdentifier Identifier of the Platform + * @return Platform Identifier as a list + */ + private List getListOfString(String platformIdentifier) { + List identifiers = new ArrayList<>(); + identifiers.add(platformIdentifier); + return identifiers; + } } 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 c882ce90b3a..ce2e3df6fad 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 @@ -13,6 +13,7 @@ NAME VARCHAR (255), FILE_BASED BOOLEAN, DESCRIPTION LONGVARCHAR, IS_SHARED BOOLEAN, +IS_DEFAULT_TENANT_MAPPING BOOLEAN, ICON_NAME VARCHAR (100), PRIMARY KEY (IDENTIFIER, TENANT_ID) ); 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 6809473a8a5..b52ddee6574 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 @@ -22,6 +22,7 @@ NAME VARCHAR (255), FILE_BASED BOOLEAN, DESCRIPTION VARCHAR (2048), IS_SHARED BOOLEAN, +IS_DEFAULT_TENANT_MAPPING BOOLEAN, ICON_NAME VARCHAR (100), PRIMARY KEY (IDENTIFIER, TENANT_ID) );