Re-structuring and adding changes after testing

feature/appm-store/pbac
megala21 7 years ago
parent 3ec1bb1e97
commit ec0be0183e

@ -101,6 +101,10 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
try { try {
Platform platform = APIUtil.getPlatformManager().getPlatform(tenantId, id); 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(); return Response.status(Response.Status.OK).entity(platform).build();
} catch (PlatformManagementDAOException e) { } catch (PlatformManagementDAOException e) {
log.error("Error while trying the get the platform with the identifier : " + id + " for the tenant :" log.error("Error while trying the get the platform with the identifier : " + id + " for the tenant :"

@ -68,6 +68,8 @@ public class Platform {
this.iconName = platform.getIconName(); this.iconName = platform.getIconName();
this.fileBased = platform.isFileBased(); this.fileBased = platform.isFileBased();
this.shared = platform.isShared(); this.shared = platform.isShared();
this.enabled = platform.isEnabled();
this.defaultTenantMapping = platform.isDefaultTenantMapping();
if (platform.getProperties() != null) { if (platform.getProperties() != null) {
this.properties = new ArrayList<>(); this.properties = new ArrayList<>();
for (Property property : platform.getProperties()) { for (Property property : platform.getProperties()) {

@ -48,4 +48,10 @@ public interface PlatformDAO {
void removePlatforms(int tenantId) throws PlatformManagementDAOException; 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;
} }

@ -41,8 +41,7 @@ import java.util.List;
public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformDAO { public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformDAO {
private static Log log = LogFactory.getLog(GenericPlatformDAOImpl.class); private static Log log = LogFactory.getLog(GenericPlatformDAOImpl.class);
@Override @Override public int register(int tenantId, Platform platform) throws PlatformManagementDAOException {
public int register(int tenantId, Platform platform) throws PlatformManagementDAOException {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
try { try {
int platformId = getPlatformId(tenantId, platform.getIdentifier()); int platformId = getPlatformId(tenantId, platform.getIdentifier());
@ -50,7 +49,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
Connection connection = this.getDBConnection(); Connection connection = this.getDBConnection();
if (!platform.isFileBased()) { if (!platform.isFileBased()) {
String insertToPlatform = "INSERT INTO APPM_PLATFORM (IDENTIFIER, TENANT_ID, NAME, FILE_BASED, " 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 = connection.prepareStatement(insertToPlatform);
preparedStatement.setString(1, platform.getIdentifier()); preparedStatement.setString(1, platform.getIdentifier());
preparedStatement.setInt(2, tenantId); preparedStatement.setInt(2, tenantId);
@ -59,6 +59,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
preparedStatement.setString(5, platform.getDescription()); preparedStatement.setString(5, platform.getDescription());
preparedStatement.setBoolean(6, platform.isShared()); preparedStatement.setBoolean(6, platform.isShared());
preparedStatement.setString(7, platform.getIconName()); preparedStatement.setString(7, platform.getIconName());
preparedStatement.setBoolean(8, platform.isDefaultTenantMapping());
preparedStatement.execute(); preparedStatement.execute();
platformId = getPlatformId(tenantId, platform.getIdentifier()); platformId = getPlatformId(tenantId, platform.getIdentifier());
@ -78,11 +79,14 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
} }
} else { } else {
String insertToPlatform = 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 = connection.prepareStatement(insertToPlatform);
preparedStatement.setString(1, platform.getIdentifier()); preparedStatement.setString(1, platform.getIdentifier());
preparedStatement.setInt(2, tenantId); preparedStatement.setInt(2, tenantId);
preparedStatement.setBoolean(3, true); preparedStatement.setBoolean(3, true);
preparedStatement.setBoolean(4, platform.isShared());
preparedStatement.setBoolean(5, platform.isDefaultTenantMapping());
preparedStatement.execute(); preparedStatement.execute();
} }
if (platformId == -1) { if (platformId == -1) {
@ -109,9 +113,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
} }
} }
@Override @Override public void update(int tenantId, String oldPlatformIdentifier, Platform platform)
public void update(int tenantId, String oldPlatformIdentifier, Platform platform) throws throws PlatformManagementDAOException {
PlatformManagementDAOException {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
try { try {
int platformId = getPlatformId(tenantId, oldPlatformIdentifier); int platformId = getPlatformId(tenantId, oldPlatformIdentifier);
@ -121,7 +124,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
if (platformId != -1) { if (platformId != -1) {
Connection connection = this.getDBConnection(); Connection connection = this.getDBConnection();
if (!platform.isFileBased()) { 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) { if (!isIdentifierNull) {
insertToPlatform += ",IDENTIFIER = ? "; insertToPlatform += ",IDENTIFIER = ? ";
} }
@ -133,19 +137,20 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
preparedStatement.setString(1, platform.getDescription()); preparedStatement.setString(1, platform.getDescription());
preparedStatement.setBoolean(2, platform.isShared()); preparedStatement.setBoolean(2, platform.isShared());
preparedStatement.setString(3, platform.getIconName()); preparedStatement.setString(3, platform.getIconName());
preparedStatement.setBoolean(4, platform.isDefaultTenantMapping());
if (!isIdentifierNull && !isNameNull) { 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.setString(5, platform.getName());
preparedStatement.setInt(6, platformId); preparedStatement.setInt(6, platformId);
} else if (isIdentifierNull && !isNameNull) {
preparedStatement.setString(4, platform.getName());
preparedStatement.setInt(5, platformId);
} else if (!isIdentifierNull) { } else if (!isIdentifierNull) {
preparedStatement.setString(4, platform.getIdentifier()); preparedStatement.setString(5, platform.getIdentifier());
preparedStatement.setInt(5, platformId); preparedStatement.setInt(6, platformId);
} else { } else {
preparedStatement.setInt(4, platformId); preparedStatement.setInt(5, platformId);
} }
preparedStatement.execute(); preparedStatement.execute();
@ -217,10 +222,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
} }
} }
@Override public void unregister(int tenantId, String platformIdenfier, boolean isFileBased)
@Override throws PlatformManagementDAOException {
public void unregister(int tenantId, String platformIdenfier, boolean isFileBased) throws
PlatformManagementDAOException {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
try { try {
Platform platform = getPlatform(tenantId, platformIdenfier); Platform platform = getPlatform(tenantId, platformIdenfier);
@ -284,8 +287,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
} }
} }
private int getTenantPlatformMapping(int tenantId, String platformIdentifier) throws private int getTenantPlatformMapping(int tenantId, String platformIdentifier)
PlatformManagementDAOException { throws PlatformManagementDAOException {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
ResultSet resultSet = null; ResultSet resultSet = null;
String getMapping = "SELECT MAPPING.ID as ID FROM (SELECT ID, PLATFORM_ID FROM APPM_PLATFORM_TENANT_MAPPING " 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 @Override public void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementDAOException {
public void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementDAOException {
String deleteMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE ID = ?"; String deleteMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE ID = ?";
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
try { try {
@ -338,8 +340,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
} }
} }
@Override @Override public void removeMappingTenants(String platformIdentifier) throws PlatformManagementDAOException {
public void removeMappingTenants(String platformIdentifier) throws PlatformManagementDAOException {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
int platformId = getPlatformId(MultitenantConstants.SUPER_TENANT_ID, platformIdentifier); int platformId = getPlatformId(MultitenantConstants.SUPER_TENANT_ID, platformIdentifier);
String getMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE TENANT_ID != ? AND PLATFORM_ID=?"; 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<Platform> getPlatforms(int tenantId) throws PlatformManagementDAOException {
@Override
public List<Platform> getPlatforms(int tenantId) throws PlatformManagementDAOException {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
ResultSet resultSet = null; ResultSet resultSet = null;
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("GetPlaforms request received for the tenant ID " + tenantId); log.debug("GetPlaforms request received for the tenant ID " + tenantId);
} }
String selectQuery = String selectQuery = "SELECT MAPPING.ID, PLATFORM.IDENTIFIER FROM (SELECT * FROM APPM_PLATFORM "
"SELECT MAPPING.ID, PLATFORM.IDENTIFIER FROM (SELECT * FROM APPM_PLATFORM WHERE TENANT_ID=? OR " + "WHERE TENANT_ID=? OR IS_SHARED = TRUE ) PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING "
+ "IS_SHARED = TRUE AND FILE_BASED = FALSE) PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING " + "MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID AND MAPPING.TENANT_ID = ?";
+ "MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID";
try { try {
Connection connection = this.getDBConnection(); Connection connection = this.getDBConnection();
preparedStatement = connection.prepareStatement(selectQuery); preparedStatement = connection.prepareStatement(selectQuery);
preparedStatement.setInt(1, tenantId); preparedStatement.setInt(1, tenantId);
preparedStatement.setInt(2, tenantId);
resultSet = preparedStatement.executeQuery(); resultSet = preparedStatement.executeQuery();
List<Platform> platforms = new ArrayList<>(); List<Platform> platforms = new ArrayList<>();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
@ -413,9 +412,9 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
public Platform getPlatform(String tenantDomain, String platformIdentifier) throws PlatformManagementDAOException { public Platform getPlatform(String tenantDomain, String platformIdentifier) throws PlatformManagementDAOException {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
ResultSet resultSet = null; ResultSet resultSet = null;
String platformQuery = "SELECT * FROM (SELECT * FROM APPM_PLATFORM WHERE (TENANT_DOMAIN=? AND IDENTIFIER=?) " + String platformQuery = "SELECT * FROM (SELECT * FROM APPM_PLATFORM WHERE (TENANT_DOMAIN=? AND IDENTIFIER=?) "
"OR (IS_SHARED = TRUE AND IDENTIFIER=?) AND FILE_BASED = FALSE ) PLATFORM " + + "OR (IS_SHARED = TRUE AND IDENTIFIER=?) AND FILE_BASED = FALSE ) PLATFORM "
"LEFT JOIN APPM_PLATFORM_PROPERTIES PROPS ON PLATFORM.ID = PROPS.PLATFORM_ID"; + "LEFT JOIN APPM_PLATFORM_PROPERTIES PROPS ON PLATFORM.ID = PROPS.PLATFORM_ID";
try { try {
Connection connection = this.getDBConnection(); Connection connection = this.getDBConnection();
preparedStatement = connection.prepareStatement(platformQuery); preparedStatement = connection.prepareStatement(platformQuery);
@ -457,8 +456,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
} }
} }
@Override @Override public Platform getPlatform(int tenantId, String identifier) throws PlatformManagementDAOException {
public Platform getPlatform(int tenantId, String identifier) throws PlatformManagementDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
@ -467,13 +465,15 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
try { try {
conn = this.getDBConnection(); conn = this.getDBConnection();
sql = "SELECT MAPPING.ID, PLATFORM.IDENTIFIER, PLATFORM.FILE_BASED, PLATFORM.ID, PLATFORM.NAME, PLATFORM" 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 " + ".DESCRIPTION, PLATFORM.ICON_NAME, PLATFORM.IS_SHARED, PLATFORM.IS_DEFAULT_TENANT_MAPPING FROM "
+ "IDENTIFIER= ? AND (TENANT_ID=? OR IS_SHARED = TRUE AND FILE_BASED = FALSE)) PLATFORM LEFT JOIN " + "(SELECT * FROM APPM_PLATFORM WHERE IDENTIFIER= ? AND (TENANT_ID=? OR IS_SHARED = TRUE)) "
+ "APPM_PLATFORM_TENANT_MAPPING MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID"; + "PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID "
+ "AND MAPPING.TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, identifier); stmt.setString(1, identifier);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
Platform platform = null; Platform platform = null;
@ -481,15 +481,15 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
if (rs.next()) { if (rs.next()) {
platform = new Platform(); platform = new Platform();
platform.setFileBased(rs.getBoolean(3)); platform.setFileBased(rs.getBoolean(3));
platform.setIdentifier(rs.getString(2)); platform.setIdentifier(rs.getString(2));
platform.setShared(rs.getBoolean(8));
platform.setDefaultTenantMapping(rs.getBoolean(9));
if (!platform.isFileBased()) { if (!platform.isFileBased()) {
platform.setId(rs.getInt(4)); platform.setId(rs.getInt(4));
platform.setName(rs.getString(5)); platform.setName(rs.getString(5));
platform.setDescription(rs.getString(6)); platform.setDescription(rs.getString(6));
platform.setIconName(rs.getString(7)); platform.setIconName(rs.getString(7));
platform.setShared(rs.getBoolean(8));
if (rs.getInt(1) != 0) { if (rs.getInt(1) != 0) {
platform.setEnabled(true); platform.setEnabled(true);
} else { } else {
@ -499,17 +499,17 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
} }
return platform; return platform;
} catch (SQLException e) { } catch (SQLException e) {
throw new PlatformManagementDAOException("Error occurred while getting platform with the identifier " + throw new PlatformManagementDAOException(
identifier + ", for the tenant : " + tenantId, e); "Error occurred while getting platform with the identifier " + identifier + ", for the tenant : "
} catch (DBConnectionException e) { + tenantId, e);
} catch (DBConnectionException e) {
throw new PlatformManagementDAOException("Error occurred while obtaining the DB connection.", e); throw new PlatformManagementDAOException("Error occurred while obtaining the DB connection.", e);
} finally { } finally {
Util.cleanupResources(stmt, rs); Util.cleanupResources(stmt, rs);
} }
} }
@Override @Override public void removePlatforms(int tenantId) throws PlatformManagementDAOException {
public void removePlatforms(int tenantId) throws PlatformManagementDAOException {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
String sql = "DELETE FROM APPM_PLATFORM WHERE TENANT_ID = ?"; 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.setInt(1, tenantId);
preparedStatement.executeUpdate(); preparedStatement.executeUpdate();
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new PlatformManagementDAOException("Database connection error while removing the platforms for the " throw new PlatformManagementDAOException(
+ "tenant - " + tenantId); "Database connection error while removing the platforms for the " + "tenant - " + tenantId);
} catch (SQLException e) { } catch (SQLException e) {
throw new PlatformManagementDAOException("SQL exception while executing the query " + sql + " for " throw new PlatformManagementDAOException(
+ "the tenant : " + tenantId); "SQL exception while executing the query " + sql + " for " + "the tenant : " + tenantId);
} finally { } finally {
Util.cleanupResources(preparedStatement, null); 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);
}
}
} }

@ -156,8 +156,7 @@ public class PlatformManagerImpl implements PlatformManager {
} else { } else {
return new Platform(platform); return new Platform(platform);
} }
throw new PlatformManagementException( return null;
"No platform was found for tenant - " + tenantId + " with platform identifier - " + identifier);
} }
private Platform getPlatformFromInMemory(int tenantId, String identifier) { private Platform getPlatformFromInMemory(int tenantId, String identifier) {
@ -182,11 +181,7 @@ public class PlatformManagerImpl implements PlatformManager {
@Override @Override
public synchronized void register(int tenantId, Platform platform) throws PlatformManagementException { public synchronized void register(int tenantId, Platform platform) throws PlatformManagementException {
if (platform.isShared() && tenantId != MultitenantConstants.SUPER_TENANT_ID) { validateBeforeRegister(tenantId, platform);
throw new PlatformManagementException(
"Platform sharing is a restricted operation, therefore Platform - " + platform.getIdentifier()
+ " cannot be shared by the tenant domain - " + tenantId);
}
try { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
int platformId = DAOFactory.getPlatformDAO().register(tenantId, platform); int platformId = DAOFactory.getPlatformDAO().register(tenantId, platform);
@ -208,12 +203,7 @@ public class PlatformManagerImpl implements PlatformManager {
if (platform.isDefaultTenantMapping()) { if (platform.isDefaultTenantMapping()) {
try { try {
if (platform.isShared()) { if (platform.isShared()) {
TenantManager tenantManager = DataHolder.getInstance().getRealmService().getTenantManager(); sharePlatformWithOtherTenants(platform.getIdentifier());
Tenant[] tenants = tenantManager.getAllTenants();
for (Tenant tenant : tenants) {
DAOFactory.getPlatformDAO()
.addMapping(tenant.getId(), getListOfString(platform.getIdentifier()));
}
} }
DAOFactory.getPlatformDAO().addMapping(tenantId, getListOfString(platform.getIdentifier())); DAOFactory.getPlatformDAO().addMapping(tenantId, getListOfString(platform.getIdentifier()));
} catch (UserStoreException e) { } catch (UserStoreException e) {
@ -244,70 +234,38 @@ public class PlatformManagerImpl implements PlatformManager {
@Override @Override
public void update(int tenantId, String oldPlatformIdentifier, Platform platform) throws public void update(int tenantId, String oldPlatformIdentifier, Platform platform) throws
PlatformManagementException { PlatformManagementException {
if (platform.isShared() && tenantId != MultitenantConstants.SUPER_TENANT_ID) { Platform oldPlatform = validateBeforeUpdate(tenantId, oldPlatformIdentifier, platform);
throw new PlatformManagementException(
"Platform sharing is a restricted operation, therefore Platform - " + platform.getIdentifier()
+ " cannot be shared by the tenant domain - " + tenantId);
}
try { try {
ConnectionManagerUtil.beginDBTransaction(); 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()) { if (platform.isFileBased()) {
Map<String, Platform> tenantPlatforms = this.inMemoryStore.get(tenantId); Map<String, Platform> tenantPlatforms = this.inMemoryStore.get(tenantId);
// File based configurations will be updated in the server start-up as well.So in that case, cache, // File based configurations will be updated in the server start-up as well.So in that case, cache,
// will be empty. // 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<>(); tenantPlatforms = new HashMap<>();
this.inMemoryStore.put(tenantId, tenantPlatforms); this.inMemoryStore.put(tenantId, tenantPlatforms);
} }
if (tenantPlatforms.get(platform.getIdentifier()) == null) {
tenantPlatforms.put(platform.getIdentifier(), platform);
}
DAOFactory.getPlatformDAO().update(tenantId, oldPlatformIdentifier, platform); DAOFactory.getPlatformDAO().update(tenantId, oldPlatformIdentifier, platform);
platform.setId(oldPlatform.getId()); platform.setId(oldPlatform.getId());
tenantPlatforms.put(platform.getIdentifier(), platform); tenantPlatforms.put(platform.getIdentifier(), platform);
} else { } else {
DAOFactory.getPlatformDAO().update(tenantId, oldPlatformIdentifier, platform); 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()));
} try {
} if (platform.isShared() && !oldPlatform.isShared()) {
DAOFactory.getPlatformDAO().addMapping(tenantId, getListOfString(platform.getIdentifier())); sharePlatformWithOtherTenants(platform.getIdentifier());
} catch (UserStoreException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!",
e);
} }
} catch (UserStoreException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!",
e);
} }
if (!platform.isShared() && oldPlatform.isShared()) { if (!platform.isShared() && oldPlatform.isShared()) {
DAOFactory.getPlatformDAO().removeMappingTenants(platform.getIdentifier()); DAOFactory.getPlatformDAO().removeMappingTenants(platform.getIdentifier());
@ -331,12 +289,6 @@ public class PlatformManagerImpl implements PlatformManager {
} }
} }
private List<String> getListOfString(String platformIdentifier) {
List<String> identifiers = new ArrayList<>();
identifiers.add(platformIdentifier);
return identifiers;
}
@Override @Override
public void unregister(int tenantId, String identifier, boolean isFileBased) throws PlatformManagementException { public void unregister(int tenantId, String identifier, boolean isFileBased) throws PlatformManagementException {
try { try {
@ -440,7 +392,6 @@ public class PlatformManagerImpl implements PlatformManager {
} else { } else {
DAOFactory.getPlatformDAO().removeMapping(tenantId, platform.getIdentifier()); DAOFactory.getPlatformDAO().removeMapping(tenantId, platform.getIdentifier());
} }
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Platform with identifier : " + platformIdentifier + " successfully " + log.debug("Platform with identifier : " + platformIdentifier + " successfully " +
(isEnabledNewStatus ? "Enabled" : "Disabled")); (isEnabledNewStatus ? "Enabled" : "Disabled"));
@ -458,7 +409,6 @@ public class PlatformManagerImpl implements PlatformManager {
} finally { } finally {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} }
@Override @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<String> getListOfString(String platformIdentifier) {
List<String> identifiers = new ArrayList<>();
identifiers.add(platformIdentifier);
return identifiers;
}
} }

@ -13,6 +13,7 @@ NAME VARCHAR (255),
FILE_BASED BOOLEAN, FILE_BASED BOOLEAN,
DESCRIPTION LONGVARCHAR, DESCRIPTION LONGVARCHAR,
IS_SHARED BOOLEAN, IS_SHARED BOOLEAN,
IS_DEFAULT_TENANT_MAPPING BOOLEAN,
ICON_NAME VARCHAR (100), ICON_NAME VARCHAR (100),
PRIMARY KEY (IDENTIFIER, TENANT_ID) PRIMARY KEY (IDENTIFIER, TENANT_ID)
); );

@ -22,6 +22,7 @@ NAME VARCHAR (255),
FILE_BASED BOOLEAN, FILE_BASED BOOLEAN,
DESCRIPTION VARCHAR (2048), DESCRIPTION VARCHAR (2048),
IS_SHARED BOOLEAN, IS_SHARED BOOLEAN,
IS_DEFAULT_TENANT_MAPPING BOOLEAN,
ICON_NAME VARCHAR (100), ICON_NAME VARCHAR (100),
PRIMARY KEY (IDENTIFIER, TENANT_ID) PRIMARY KEY (IDENTIFIER, TENANT_ID)
); );

Loading…
Cancel
Save