Merge pull request #886 from Megala21/appm_new

Platform Management fixing issues and adding support for other database types
feature/appm-store/pbac
Megala Uthayakumar 7 years ago committed by GitHub
commit 1baca9524b

@ -60,8 +60,8 @@ import javax.ws.rs.core.Response;
} }
), ),
tags = { tags = {
@Tag(name = "application_management", description = "Platform Management APIS related with " @Tag(name = "device_management, application_management", description = "Platform Management APIS "
+ "Application Management") + "related with Application Management")
} }
) )
@Scopes ( @Scopes (
@ -279,13 +279,10 @@ public interface PlatformManagementAPI {
value = { value = {
@ApiResponse( @ApiResponse(
code = 200, code = 200,
message = "OK. \n Successfully updated the platform"), message = "OK. \n Successfully deleted the platform"),
@ApiResponse(
code = 400,
message = "Bad Request. \n Invalid request parameters passed."),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server Error. \n Error occurred while getting the platform list.", message = "Internal Server Error. \n Error occurred while deleting the platform.",
response = ErrorResponse.class) response = ErrorResponse.class)
}) })
Response removePlatform( Response removePlatform(
@ -297,4 +294,50 @@ public interface PlatformManagementAPI {
String identifier String identifier
); );
@PUT
@Path("update-status/{identifier}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(
consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON,
httpMethod = "PUT",
value = "Update Platform status",
notes = "This will update the platform status for the tenant space",
tags = "Platform Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = SCOPE, value = "perm:platform:update")
})
}
)
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "OK. \n Successfully updated the platform."),
@ApiResponse(
code = 404,
message = "Not found. \n Non-file based platform not found to update."),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Error occurred while getting the platform list.",
response = ErrorResponse.class)
})
Response updatePlatformStatus(
@ApiParam(
name = "identifier",
required = true)
@PathParam("identifier")
@Size(max = 45)
String identifier,
@ApiParam(name = "status", allowableValues = "ENABLED, DISABLED", value =
"Provide the status of platform for that tenant:\n"
+ "- ENABLED: The platforms that are currently enabled for the tenant\n"
+ "- DISABLED: The platforms that currently disabled "
+ "to be used for tenant\n", required = true)
@QueryParam("status")
String status
);
} }

@ -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 :"
@ -166,4 +170,24 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI {
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
} }
} }
@PUT
@Path("update-status/{identifier}")
@Override
public Response updatePlatformStatus(@PathParam("identifier") @Size(max = 45) String id, @QueryParam("status")
String status) {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
try {
APIUtil.getPlatformManager().updatePlatformStatus(tenantId, id, status);
return Response.status(Response.Status.OK).build();
} catch (PlatformManagementDAOException e) {
log.error("Platform Management Database Exception while trying to update the status of the platform with "
+ "the identifier : " + id + " for the tenant : " + tenantId, e);
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
} catch (PlatformManagementException e) {
log.error("Platform Management Exception while trying to update the status of the platform with the "
+ "identifier : " + id + " for the tenant : " + tenantId, e);
return APIUtil.getResponse(e, Response.Status.NOT_FOUND);
}
}
} }

@ -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()) {

@ -29,24 +29,108 @@ import java.util.List;
*/ */
public interface PlatformManager { public interface PlatformManager {
void initialize(int tenantId) throws PlatformManagementException; /**
* To initialize the shared platforms for the tenant during the tenant initialization time.
*
* @param tenantId ID of the tenant
* @throws PlatformManagementException Platform Management Exception
*/
public void initialize(int tenantId) throws PlatformManagementException;
List<Platform> getPlatforms(int tenantId) throws PlatformManagementException; /**
* To get platforms of the specific tenant.
*
* @param tenantId ID of the tenant
* @return List of platforms
* @throws PlatformManagementException Platform Management Exception
*/
public List<Platform> getPlatforms(int tenantId) throws PlatformManagementException;
Platform getPlatform(int tenantId, String platformIdentifier) throws PlatformManagementException; /**
* To get platform with the given platform identifier and tenant ID.
*
* @param tenantId ID of the tenant
* @param platformIdentifier Unique identifier of the platform.
* @return the Specific platform with the platform identifier and tenant
* @throws PlatformManagementException Platform Management Exception
*/
public Platform getPlatform(int tenantId, String platformIdentifier) throws PlatformManagementException;
void register(int tenantId, Platform platform) throws PlatformManagementException; /**
* To register a platform under particular tenant.
*
* @param tenantId ID of the tenant.
* @param platform Platform to be registered
* @throws PlatformManagementException Platform Management Exception
*/
public void register(int tenantId, Platform platform) throws PlatformManagementException;
void update(int tenantId, String oldPlatformIdentifier, Platform platform) /**
* To update a platform.
*
* @param tenantId ID of the tenant
* @param oldPlatformIdentifier Old platform Identifier
* @param platform Platform to be updated
* @throws PlatformManagementException Platform Management Exception
*/
public void update(int tenantId, String oldPlatformIdentifier, Platform platform)
throws PlatformManagementException; throws PlatformManagementException;
void unregister(int tenantId, String platformIdentifier, boolean isFileBased) /**
* To un-register the platform.
*
* @param tenantId ID of the tenant.
* @param platformIdentifier ID of the platform
* @param isFileBased To indicate whether a file based or not.
* @throws PlatformManagementException Platform Management Exception.
*/
public void unregister(int tenantId, String platformIdentifier, boolean isFileBased)
throws PlatformManagementException; throws PlatformManagementException;
void addMapping(int tenantId, List<String> platformIdentifiers) throws PlatformManagementException; /**
* To add mapping to platform identifiers with the tenant ID.
*
* @param tenantId ID of the tenant
* @param platformIdentifiers Platform Identifiers
* @throws PlatformManagementException Platform Management Exception
*/
public void addMapping(int tenantId, List<String> platformIdentifiers) throws PlatformManagementException;
/**
* To add mapping to a platform for a tenant.
*
* @param tenantId ID of the tenant.
* @param platformIdentifier ID of the platform, the mapping should be added.
* @throws PlatformManagementException Platform Management Exception.
*/
public void addMapping(int tenantId, String platformIdentifier) throws PlatformManagementException;
/**
* To remove a mapping of a platform to a tenant.
*
* @param tenantId ID of the tenant.
* @param platformIdentifier ID of the platform.
* @throws PlatformManagementException Platform Management Exception.
*/
public void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementException;
void addMapping(int tenantId, String platformIdentifier) throws PlatformManagementException; /**
* To update the platform status(ENABLED / DISABLED).
*
* @param tenantId Id of the tenant
* @param platformIdentifier ID of the platform
* @param status Status to be updated.
* @throws PlatformManagementException Platform Management Exception.
*/
public void updatePlatformStatus(int tenantId, String platformIdentifier, String status)
throws PlatformManagementException;
void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementException; /**
* To remove platforms that belongs to particular tenant.
*
* @param tenantId ID of the tenant.
* @throws PlatformManagementException Platform Management Exception.
*/
public void removePlatforms(int tenantId) throws PlatformManagementException;
} }

@ -46,4 +46,12 @@ public interface PlatformDAO {
Platform getPlatform(int tenantId, String identifier) throws PlatformManagementDAOException; Platform getPlatform(int tenantId, String identifier) 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;
} }

@ -29,6 +29,7 @@ import org.wso2.carbon.device.application.mgt.core.dao.impl.application.H2Applic
import org.wso2.carbon.device.application.mgt.core.dao.impl.application.MySQLApplicationDAOImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.application.MySQLApplicationDAOImpl;
import org.wso2.carbon.device.application.mgt.core.dao.impl.lifecyclestate.GenericLifecycleStateImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.lifecyclestate.GenericLifecycleStateImpl;
import org.wso2.carbon.device.application.mgt.core.dao.impl.platform.GenericPlatformDAOImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.platform.GenericPlatformDAOImpl;
import org.wso2.carbon.device.application.mgt.core.dao.impl.platform.OracleMsSQLPlatformDAOImpl;
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException;
import org.wso2.carbon.device.application.mgt.core.util.ApplicationMgtDatabaseCreator; import org.wso2.carbon.device.application.mgt.core.util.ApplicationMgtDatabaseCreator;
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
@ -72,7 +73,11 @@ public class DAOFactory {
switch (databaseEngine) { switch (databaseEngine) {
case Constants.DataBaseTypes.DB_TYPE_H2: case Constants.DataBaseTypes.DB_TYPE_H2:
case Constants.DataBaseTypes.DB_TYPE_MYSQL: case Constants.DataBaseTypes.DB_TYPE_MYSQL:
case Constants.DataBaseTypes.DB_TYPE_POSTGRESQL:
return new GenericPlatformDAOImpl(); return new GenericPlatformDAOImpl();
case Constants.DataBaseTypes.DB_TYPE_MSSQL:
case Constants.DataBaseTypes.DB_TYPE_ORACLE:
return new OracleMsSQLPlatformDAOImpl();
default: default:
throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine);
} }
@ -111,9 +116,9 @@ public class DAOFactory {
DatabaseCreator databaseCreator = new ApplicationMgtDatabaseCreator(dataSourceName); DatabaseCreator databaseCreator = new ApplicationMgtDatabaseCreator(dataSourceName);
if (!databaseCreator.isDatabaseStructureCreated(validationQuery)) { if (!databaseCreator.isDatabaseStructureCreated(validationQuery)) {
databaseCreator.createRegistryDatabase(); databaseCreator.createRegistryDatabase();
if (log.isDebugEnabled()) { log.info("Application Management tables are created in the database");
log.debug("Application Management tables are created in the database"); } else {
} log.info("Application Management Database structure already exists. Not creating the database.");
} }
} }
} catch (SQLException e) { } catch (SQLException e) {

@ -22,12 +22,10 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.application.mgt.common.Platform; import org.wso2.carbon.device.application.mgt.common.Platform;
import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException;
import org.wso2.carbon.device.application.mgt.common.exception.TransactionManagementException;
import org.wso2.carbon.device.application.mgt.core.dao.PlatformDAO; import org.wso2.carbon.device.application.mgt.core.dao.PlatformDAO;
import org.wso2.carbon.device.application.mgt.core.dao.common.Util; import org.wso2.carbon.device.application.mgt.core.dao.common.Util;
import org.wso2.carbon.device.application.mgt.core.dao.impl.AbstractDAOImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.AbstractDAOImpl;
import org.wso2.carbon.device.application.mgt.core.exception.PlatformManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.PlatformManagementDAOException;
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import java.sql.Connection; import java.sql.Connection;
@ -49,10 +47,11 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
try { try {
int platformId = getPlatformId(tenantId, platform.getIdentifier()); int platformId = getPlatformId(tenantId, platform.getIdentifier());
if (platformId == -1) { if (platformId == -1) {
Connection connection = this.getConnection(); 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);
@ -61,6 +60,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());
@ -80,11 +80,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) {
@ -112,8 +115,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
} }
@Override @Override
public void update(int tenantId, String oldPlatformIdentifier, Platform platform) throws public void update(int tenantId, String oldPlatformIdentifier, Platform platform)
PlatformManagementDAOException { throws PlatformManagementDAOException {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
try { try {
int platformId = getPlatformId(tenantId, oldPlatformIdentifier); int platformId = getPlatformId(tenantId, oldPlatformIdentifier);
@ -121,9 +124,10 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
boolean isNameNull = platform.getName() == null; boolean isNameNull = platform.getName() == null;
if (platformId != -1) { if (platformId != -1) {
Connection connection = this.getConnection(); 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 = ? ";
} }
@ -135,19 +139,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();
@ -171,10 +176,11 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
preparedStatement.execute(); preparedStatement.execute();
} }
} }
} else { } else if (!isIdentifierNull) {
String insertToPlatform = "UPDATE APPM_PLATFORM SET IDENTIFIER = ? WHERE ID = ?"; String insertToPlatform = "UPDATE APPM_PLATFORM SET IDENTIFIER = ? WHERE ID = ?";
preparedStatement = connection.prepareStatement(insertToPlatform); preparedStatement = connection.prepareStatement(insertToPlatform);
preparedStatement.setInt(1, platformId); preparedStatement.setString(1, platform.getIdentifier());
preparedStatement.setInt(2, platformId);
preparedStatement.execute(); preparedStatement.execute();
} }
} else { } else {
@ -196,10 +202,9 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
private int getPlatformId(int tenantId, String platformIdentifier) throws PlatformManagementDAOException { private int getPlatformId(int tenantId, String platformIdentifier) throws PlatformManagementDAOException {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
ResultSet resultSet = null; ResultSet resultSet = null;
String query = "SELECT ID FROM APPM_PLATFORM WHERE (TENANT_ID=? AND IDENTIFIER=?) OR (IS_SHARED = TRUE AND " String query = SQLQueries.queryToGetPlatformId;
+ "IDENTIFIER=?)";
try { try {
Connection connection = this.getConnection(); Connection connection = this.getDBConnection();
preparedStatement = connection.prepareStatement(query); preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, tenantId); preparedStatement.setInt(1, tenantId);
preparedStatement.setString(2, platformIdentifier); preparedStatement.setString(2, platformIdentifier);
@ -218,17 +223,16 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
} }
} }
@Override @Override
public void unregister(int tenantId, String platformIdenfier, boolean isFileBased) throws public void unregister(int tenantId, String platformIdenfier, boolean isFileBased)
PlatformManagementDAOException { throws PlatformManagementDAOException {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
try { try {
Platform platform = getPlatform(tenantId, platformIdenfier); Platform platform = getPlatform(tenantId, platformIdenfier);
if (platform != null) { if (platform != null) {
if (isFileBased == platform.isFileBased()) { if (isFileBased == platform.isFileBased()) {
Connection connection = this.getConnection(); Connection connection = this.getDBConnection();
String deletePlatform = "DELETE FROM APPM_PLATFORM WHERE ID = ?"; String deletePlatform = "DELETE FROM APPM_PLATFORM WHERE ID = ?";
preparedStatement = connection.prepareStatement(deletePlatform); preparedStatement = connection.prepareStatement(deletePlatform);
preparedStatement.setInt(1, platform.getId()); preparedStatement.setInt(1, platform.getId());
@ -264,7 +268,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
for (String platformIdentifier : platformIdentifiers) { for (String platformIdentifier : platformIdentifiers) {
if (getTenantPlatformMapping(tenantId, platformIdentifier) == -1) { if (getTenantPlatformMapping(tenantId, platformIdentifier) == -1) {
int platformId = getPlatformId(tenantId, platformIdentifier); int platformId = getPlatformId(tenantId, platformIdentifier);
Connection connection = this.getConnection(); Connection connection = this.getDBConnection();
preparedStatement = connection.prepareStatement(insertMapping); preparedStatement = connection.prepareStatement(insertMapping);
preparedStatement.setInt(1, tenantId); preparedStatement.setInt(1, tenantId);
preparedStatement.setInt(2, platformId); preparedStatement.setInt(2, platformId);
@ -285,15 +289,15 @@ 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 "
+ "WHERE TENANT_ID=?) MAPPING JOIN (SELECT ID FROM APPM_PLATFORM WHERE APPM_PLATFORM.IDENTIFIER=?) " + "WHERE TENANT_ID=?) MAPPING JOIN (SELECT ID FROM APPM_PLATFORM WHERE APPM_PLATFORM.IDENTIFIER=?) "
+ "PLATFORM ON MAPPING.PLATFORM_ID=PLATFORM.ID"; + "PLATFORM ON MAPPING.PLATFORM_ID=PLATFORM.ID";
try { try {
Connection connection = this.getConnection(); Connection connection = this.getDBConnection();
preparedStatement = connection.prepareStatement(getMapping); preparedStatement = connection.prepareStatement(getMapping);
preparedStatement.setInt(1, tenantId); preparedStatement.setInt(1, tenantId);
preparedStatement.setString(2, platformIdentifier); preparedStatement.setString(2, platformIdentifier);
@ -320,7 +324,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
try { try {
int mappingId = getTenantPlatformMapping(tenantId, platformIdentifier); int mappingId = getTenantPlatformMapping(tenantId, platformIdentifier);
if (mappingId != -1) { if (mappingId != -1) {
Connection connection = this.getConnection(); Connection connection = this.getDBConnection();
preparedStatement = connection.prepareStatement(deleteMapping); preparedStatement = connection.prepareStatement(deleteMapping);
preparedStatement.setInt(1, mappingId); preparedStatement.setInt(1, mappingId);
preparedStatement.execute(); preparedStatement.execute();
@ -345,7 +349,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
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=?";
try { try {
Connection connection = this.getConnection(); Connection connection = this.getDBConnection();
preparedStatement = connection.prepareStatement(getMapping); preparedStatement = connection.prepareStatement(getMapping);
preparedStatement.setInt(1, MultitenantConstants.SUPER_TENANT_ID); preparedStatement.setInt(1, MultitenantConstants.SUPER_TENANT_ID);
preparedStatement.setInt(2, platformId); preparedStatement.setInt(2, platformId);
@ -361,7 +365,6 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
} }
} }
@Override @Override
public List<Platform> getPlatforms(int tenantId) throws PlatformManagementDAOException { public List<Platform> getPlatforms(int tenantId) throws PlatformManagementDAOException {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
@ -370,22 +373,20 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
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 = SQLQueries.queryToGetPlatforms;
"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";
try { try {
Connection connection = this.getConnection(); 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()) {
log.debug("Platform retrieved for the tenant Id " + tenantId); log.debug("Platform retrieved for the tenant Id " + tenantId);
} }
while (resultSet.next()) { while (resultSet.next()) {
String identifier = resultSet.getString("PLATFORM.IDENTIFIER"); int mappingID = resultSet.getInt(1);
int mappingID = resultSet.getInt("MAPPING.ID"); String identifier = resultSet.getString(2);
Platform platform = getPlatform(tenantId, identifier); Platform platform = getPlatform(tenantId, identifier);
if (mappingID != 0) { if (mappingID != 0) {
platform.setEnabled(true); platform.setEnabled(true);
@ -398,8 +399,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
} }
} }
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Number of platforms available for the tenant ID - " + tenantId + " :" + platforms log.debug("Number of platforms available for the tenant ID - " + tenantId + " :" + platforms.size());
.size());
} }
return platforms; return platforms;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
@ -415,11 +415,11 @@ 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.getConnection(); Connection connection = this.getDBConnection();
preparedStatement = connection.prepareStatement(platformQuery); preparedStatement = connection.prepareStatement(platformQuery);
preparedStatement.setString(1, tenantDomain); preparedStatement.setString(1, tenantDomain);
preparedStatement.setString(2, platformIdentifier); preparedStatement.setString(2, platformIdentifier);
@ -459,6 +459,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
} }
} }
@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;
@ -466,37 +467,170 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
String sql = ""; String sql = "";
try { try {
conn = this.getConnection(); conn = this.getDBConnection();
sql = "SELECT * FROM APPM_PLATFORM WHERE IDENTIFIER = ? AND TENANT_ID = ?"; sql = SQLQueries.queryToGetPlatform;
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;
if (rs.next()) { if (rs.next()) {
platform = new Platform(); platform = new Platform();
platform.setFileBased(rs.getBoolean("FILE_BASED")); 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));
if (rs.getInt(1) != 0) {
platform.setEnabled(true);
} else {
platform.setEnabled(false);
}
}
}
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 obtaining the DB connection.", e);
} finally {
Util.cleanupResources(stmt, rs);
}
}
@Override
public void removePlatforms(int tenantId) throws PlatformManagementDAOException {
PreparedStatement preparedStatement = null;
String sql = "DELETE FROM APPM_PLATFORM WHERE TENANT_ID = ?";
try {
Connection connection = this.getDBConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, tenantId);
preparedStatement.executeUpdate();
} catch (DBConnectionException e) {
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);
} finally {
Util.cleanupResources(preparedStatement, null);
}
}
@Override
public int getSuperTenantAndOwnPlatforms(String platformIdentifier, int tenantId)
throws PlatformManagementDAOException {
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String sql = SQLQueries.queryToGetSupertenantAndOwnPlatforms;
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.setIdentifier(rs.getString("IDENTIFIER"));
platform.setShared(rs.getBoolean("IS_SHARED"));
platform.setDefaultTenantMapping(rs.getBoolean("IS_DEFAULT_TENANT_MAPPING"));
if (!platform.isFileBased()) { if (!platform.isFileBased()) {
platform.setId(rs.getInt("ID")); platform.setId(rs.getInt("ID"));
platform.setName(rs.getString("NAME")); platform.setName(rs.getString("NAME"));
platform.setDescription(rs.getString("DESCRIPTION")); platform.setDescription(rs.getString("DESCRIPTION"));
platform.setIconName(rs.getString("ICON_NAME")); platform.setIconName(rs.getString("ICON_NAME"));
platform.setShared(rs.getBoolean("IS_SHARED"));
} }
} }
return platform; return platform;
} catch (SQLException e) { } catch (SQLException e) {
throw new PlatformManagementDAOException("Error occurred while getting platform with the identifier " + throw new PlatformManagementDAOException("Error occurred while executing the query : " + sql + " for "
identifier + ", for the tenant : " + tenantId, e); + "getting platforms owned by tenant : " + tenantId, e);
} catch (DBConnectionException 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 for getting " + "platforms owned by tenant : "
+ tenantId, e);
} finally { } finally {
Util.cleanupResources(stmt, rs); 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);
}
}
} }

@ -0,0 +1,87 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.wso2.carbon.device.application.mgt.core.dao.impl.platform;
import org.wso2.carbon.device.application.mgt.common.Platform;
import org.wso2.carbon.device.application.mgt.core.exception.PlatformManagementDAOException;
import java.util.List;
/**
* Oracle and MsSQL specific implementation for Platform DAO.
*/
public class OracleMsSQLPlatformDAOImpl extends GenericPlatformDAOImpl {
@Override
public int getSuperTenantAndOwnPlatforms(String platformIdentifier, int tenantId)
throws PlatformManagementDAOException {
SQLQueries.queryToGetSupertenantAndOwnPlatforms = "SELECT ID from APPM_PLATFORM where IDENTIFIER "
+ "= ? AND (TENANT_ID = ? OR (TENANT_ID = ? AND IS_SHARED = 1))";
return super.getSuperTenantAndOwnPlatforms(platformIdentifier, tenantId);
}
@Override
public int register(int tenantId, Platform platform) throws PlatformManagementDAOException {
SQLQueries.queryToGetPlatformId =
"SELECT ID FROM APPM_PLATFORM WHERE (TENANT_ID=? AND IDENTIFIER=?) OR (IS_SHARED = 1 AND "
+ "IDENTIFIER=?)";
return super.register(tenantId, platform);
}
@Override
public Platform getPlatform(int tenantId, String identifier) throws PlatformManagementDAOException {
SQLQueries.queryToGetPlatform =
"SELECT MAPPING.ID, PLATFORM.IDENTIFIER, PLATFORM.FILE_BASED, PLATFORM.ID, PLATFORM.NAME, "
+ "PLATFORM.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 = 1)) PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING "
+ "MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID AND MAPPING.TENANT_ID = ?";
return super.getPlatform(tenantId, identifier);
}
public void removeMappingTenants(String platformIdentifier) throws PlatformManagementDAOException {
SQLQueries.queryToGetPlatformId =
"SELECT ID FROM APPM_PLATFORM WHERE (TENANT_ID=? AND IDENTIFIER=?) OR (IS_SHARED = 1 AND "
+ "IDENTIFIER=?)";
super.removeMappingTenants(platformIdentifier);
}
public void update(int tenantId, String oldPlatformIdentifier, Platform platform)
throws PlatformManagementDAOException {
SQLQueries.queryToGetPlatformId =
"SELECT ID FROM APPM_PLATFORM WHERE (TENANT_ID=? AND IDENTIFIER=?) OR (IS_SHARED = 1 AND "
+ "IDENTIFIER=?)";
super.update(tenantId, oldPlatformIdentifier, platform);
}
public void addMapping(int tenantId, List<String> platformIdentifiers) throws PlatformManagementDAOException {
SQLQueries.queryToGetPlatformId =
"SELECT ID FROM APPM_PLATFORM WHERE (TENANT_ID=? AND IDENTIFIER=?) OR (IS_SHARED = 1 AND "
+ "IDENTIFIER=?)";
super.addMapping(tenantId, platformIdentifiers);
}
public List<Platform> getPlatforms(int tenantId) throws PlatformManagementDAOException {
SQLQueries.queryToGetPlatforms = "SELECT MAPPING.ID, PLATFORM.IDENTIFIER FROM (SELECT * FROM APPM_PLATFORM "
+ "WHERE TENANT_ID=? OR IS_SHARED = 1) PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING "
+ "MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID AND MAPPING.TENANT_ID = ?";
return super.getPlatforms(tenantId);
}
}

@ -0,0 +1,40 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.wso2.carbon.device.application.mgt.core.dao.impl.platform;
/**
* SQL Queries specific to Platform.
*/
public class SQLQueries {
static String queryToGetSupertenantAndOwnPlatforms = "SELECT ID from APPM_PLATFORM where IDENTIFIER "
+ "= ? AND (TENANT_ID = ? OR (TENANT_ID = ? AND IS_SHARED = true))";
static String queryToGetPlatformId =
"SELECT ID FROM APPM_PLATFORM WHERE (TENANT_ID=? AND IDENTIFIER=?) OR (IS_SHARED = TRUE AND "
+ "IDENTIFIER=?)";
static String queryToGetPlatform =
"SELECT MAPPING.ID, PLATFORM.IDENTIFIER, PLATFORM.FILE_BASED, PLATFORM.ID, PLATFORM.NAME, PLATFORM"
+ ".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 = ?";
static String queryToGetPlatforms = "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 = ?";
}

@ -24,7 +24,9 @@ import org.apache.axis2.deployment.repository.util.DeploymentFileData;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.application.mgt.common.exception.PlatformManagementException; import org.wso2.carbon.device.application.mgt.common.exception.PlatformManagementException;
import org.wso2.carbon.device.application.mgt.common.services.PlatformManager;
import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.application.mgt.core.internal.DataHolder;
import org.wso2.carbon.device.application.mgt.core.util.Constants; import org.wso2.carbon.device.application.mgt.core.util.Constants;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils; import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
@ -66,9 +68,18 @@ public class PlatformDeployer extends AbstractDeployer {
Platform platformConf = (Platform) unmarshaller.unmarshal(deploymentFile); Platform platformConf = (Platform) unmarshaller.unmarshal(deploymentFile);
if (platformConf.getId().contentEquals(getPlatformID(deploymentFile.getName()))) { if (platformConf.getId().contentEquals(getPlatformID(deploymentFile.getName()))) {
org.wso2.carbon.device.application.mgt.common.Platform platform = convert(platformConf); org.wso2.carbon.device.application.mgt.common.Platform platform = convert(platformConf);
DataHolder.getInstance().getPlatformManager() PlatformManager platformManager = DataHolder.getInstance().getPlatformManager();
.register(CarbonContext.getThreadLocalCarbonContext().getTenantId(), platform); int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
org.wso2.carbon.device.application.mgt.common.Platform existingPlatform = platformManager
.getPlatform(tenantID, platform.getIdentifier());
if (existingPlatform != null && existingPlatform.isFileBased()) {
platformManager.update(tenantID, platformConf.getId(), platform);
log.info("Platform configuration : " + deploymentFile.getName() + " updated successfully");
} else {
platformManager.register(CarbonContext.getThreadLocalCarbonContext().getTenantId(), platform);
log.info("Platform configuration : " + deploymentFile.getName() + " deployed successfully"); log.info("Platform configuration : " + deploymentFile.getName() + " deployed successfully");
}
} else { } else {
log.error("Unable to deploy the platform - " + deploymentFile.getAbsolutePath() log.error("Unable to deploy the platform - " + deploymentFile.getAbsolutePath()
+ "!. Platform config file name - " + deploymentFile.getName() + "!. Platform config file name - " + deploymentFile.getName()
@ -89,7 +100,7 @@ public class PlatformDeployer extends AbstractDeployer {
.unregister(CarbonContext.getThreadLocalCarbonContext().getTenantId(), platformId, true); .unregister(CarbonContext.getThreadLocalCarbonContext().getTenantId(), platformId, true);
log.info("Platform configuration : " + fileName + " un-deployed successfully"); log.info("Platform configuration : " + fileName + " un-deployed successfully");
} catch (PlatformManagementException e) { } catch (PlatformManagementException e) {
log.error("Error occurred while un-deploying the platform - " + fileName); log.error("Error occurred while un-deploying the platform - " + fileName, e);
} }
} }

@ -52,7 +52,7 @@ public class PlatformManagerImpl implements PlatformManager {
@Override @Override
public void initialize(int tenantId) throws PlatformManagementException { public void initialize(int tenantId) throws PlatformManagementException {
try { try {
ConnectionManagerUtil.beginTransaction(); ConnectionManagerUtil.beginDBTransaction();
List<Platform> platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantId); List<Platform> platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantId);
List<String> platformIdentifiers = new ArrayList<>(); List<String> platformIdentifiers = new ArrayList<>();
for (Platform platform : platforms) { for (Platform platform : platforms) {
@ -61,19 +61,22 @@ public class PlatformManagerImpl implements PlatformManager {
} }
} }
DAOFactory.getPlatformDAO().addMapping(tenantId, platformIdentifiers); DAOFactory.getPlatformDAO().addMapping(tenantId, platformIdentifiers);
ConnectionManagerUtil.commitTransaction(); ConnectionManagerUtil.commitDBTransaction();
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Transaction Management Exception while initializing the " + "platforms for the tenant : " "Transaction Management Exception while initializing the " + "platforms for the tenant : "
+ tenantId, e); + tenantId, e);
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Database Connection Exception while initializing the " + "platforms for the tenant : " + tenantId, "Database Connection Exception while initializing the " + "platforms for the tenant : " + tenantId,
e); e);
} catch (PlatformManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw e;
} finally { } finally {
ConnectionManagerUtil.closeConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} }
@ -86,13 +89,18 @@ public class PlatformManagerImpl implements PlatformManager {
+ "PlatformManager level"); + "PlatformManager level");
} }
try { try {
ConnectionManagerUtil.openConnection(); ConnectionManagerUtil.beginDBTransaction();
platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantId); platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantId);
} catch (DBConnectionException e) { ConnectionManagerUtil.commitDBTransaction();
} catch (DBConnectionException | TransactionManagementException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Database Connection Exception while getting the platforms for the tenant : " + tenantId, e); "Database Connection Exception while getting the platforms for the tenant : " + tenantId, e);
} catch (PlatformManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw e;
} finally { } finally {
ConnectionManagerUtil.closeConnection(); ConnectionManagerUtil.closeDBConnection();
} }
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Number of platforms received from DAO layer is " + platforms.size() + " for the tenant " log.debug("Number of platforms received from DAO layer is " + platforms.size() + " for the tenant "
@ -128,23 +136,27 @@ public class PlatformManagerImpl implements PlatformManager {
Platform platform = getPlatformFromInMemory(tenantId, identifier); Platform platform = getPlatformFromInMemory(tenantId, identifier);
if (platform == null) { if (platform == null) {
try { try {
ConnectionManagerUtil.openConnection(); ConnectionManagerUtil.beginDBTransaction();
platform = DAOFactory.getPlatformDAO().getPlatform(tenantId, identifier); platform = DAOFactory.getPlatformDAO().getPlatform(tenantId, identifier);
ConnectionManagerUtil.commitDBTransaction();
if (platform != null) { if (platform != null) {
return platform; return platform;
} }
} catch (DBConnectionException e) { } catch (DBConnectionException | TransactionManagementException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Database Connection Exception while trying to get the " + "platform with the id :" + identifier "Database Connection Exception while trying to get the " + "platform with the id :" + identifier
+ " for the tenant : " + tenantId, e); + " for the tenant : " + tenantId, e);
} catch (PlatformManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw e;
} finally { } finally {
ConnectionManagerUtil.closeConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} 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) {
@ -169,13 +181,9 @@ 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.beginTransaction(); ConnectionManagerUtil.beginDBTransaction();
int platformId = DAOFactory.getPlatformDAO().register(tenantId, platform); int platformId = DAOFactory.getPlatformDAO().register(tenantId, platform);
if (platform.isFileBased()) { if (platform.isFileBased()) {
platform.setId(platformId); platform.setId(platformId);
@ -187,7 +195,7 @@ public class PlatformManagerImpl implements PlatformManager {
if (tenantPlatforms.get(platform.getIdentifier()) == null) { if (tenantPlatforms.get(platform.getIdentifier()) == null) {
tenantPlatforms.put(platform.getIdentifier(), platform); tenantPlatforms.put(platform.getIdentifier(), platform);
} else { } else {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementException( throw new PlatformManagementException(
"Platform - " + platform.getIdentifier() + " is already registered!"); "Platform - " + platform.getIdentifier() + " is already registered!");
} }
@ -195,136 +203,96 @@ 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) {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!", throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!",
e); e);
} }
} }
ConnectionManagerUtil.commitTransaction(); ConnectionManagerUtil.commitDBTransaction();
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Transaction Management Exception while trying to register a " + "platform with id " + platform "Transaction Management Exception while trying to register a " + "platform with id " + platform
.getIdentifier() + " for tenant " + tenantId); .getIdentifier() + " for tenant " + tenantId);
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Database Connection Exception while trying to register a " + "platform with id " + platform "Database Connection Exception while trying to register a " + "platform with id " + platform
.getIdentifier() + " for tenant " + tenantId); .getIdentifier() + " for tenant " + tenantId);
} catch (PlatformManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw e;
} finally { } finally {
ConnectionManagerUtil.closeConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} }
@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);
}
Platform oldPlatform;
if (platform.getIdentifier() != null && !platform.getIdentifier().equals(oldPlatformIdentifier)) {
try {
ConnectionManagerUtil.openConnection();
Platform existingPlatform = DAOFactory.getPlatformDAO().getPlatform(tenantId, platform.getIdentifier());
if (existingPlatform != null) {
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);
}
} catch (DBConnectionException e) {
throw new PlatformManagementException(
"Database Connection Exception while trying to update the " + "platform for the tenant : "
+ tenantId, e);
} finally {
ConnectionManagerUtil.closeConnection();
}
}
try { try {
ConnectionManagerUtil.beginDBTransaction();
if (platform.isFileBased()) { if (platform.isFileBased()) {
Map<String, Platform> tenantPlatforms = this.inMemoryStore.get(tenantId); Map<String, Platform> tenantPlatforms = this.inMemoryStore.get(tenantId);
if (tenantPlatforms == null) { // 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.get(oldPlatformIdentifier) == null) {
throw new PlatformManagementException( throw new PlatformManagementException(
"No platforms registered for the tenant - " + tenantId + " with platform identifier - " "Cannot update platform with identifier " + oldPlatformIdentifier + " as it is not "
+ platform.getIdentifier()); + " existing already for the tenant " + tenantId);
} }
oldPlatform = tenantPlatforms.get(oldPlatformIdentifier);
if (oldPlatform == null) {
throw new PlatformManagementException(
"No platforms registered for the tenant - " + tenantId + " with platform identifier - "
+ platform.getIdentifier());
} else { } else {
ConnectionManagerUtil.beginTransaction(); tenantPlatforms = new HashMap<>();
this.inMemoryStore.put(tenantId, tenantPlatforms);
}
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 {
ConnectionManagerUtil.beginTransaction();
oldPlatform = DAOFactory.getPlatformDAO().getPlatform(tenantId, oldPlatformIdentifier);
DAOFactory.getPlatformDAO().update(tenantId, oldPlatformIdentifier, platform); DAOFactory.getPlatformDAO().update(tenantId, oldPlatformIdentifier, platform);
} }
if (platform.isDefaultTenantMapping() && !oldPlatform.isDefaultTenantMapping()) {
try { try {
if (platform.isShared() && !oldPlatform.isShared()) { if (platform.isShared() && !oldPlatform.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()));
} catch (UserStoreException e) { } catch (UserStoreException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!", throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!",
e); e);
} }
}
if (!platform.isShared() && oldPlatform.isShared()) { if (!platform.isShared() && oldPlatform.isShared()) {
DAOFactory.getPlatformDAO().removeMappingTenants(platform.getIdentifier()); DAOFactory.getPlatformDAO().removeMappingTenants(platform.getIdentifier());
} }
ConnectionManagerUtil.commitTransaction(); ConnectionManagerUtil.commitDBTransaction();
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Transaction Management Exception while trying to update " + "platform : " + oldPlatformIdentifier "Transaction Management Exception while trying to update " + "platform : " + oldPlatformIdentifier
+ " of tenant :" + tenantId); + " of tenant :" + tenantId);
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Database Connection Exception while trying to update " + "platform : " + oldPlatformIdentifier "Database Connection Exception while trying to update " + "platform : " + oldPlatformIdentifier
+ " of tenant :" + tenantId); + " of tenant :" + tenantId);
} catch (PlatformManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw e;
} finally { } finally {
ConnectionManagerUtil.closeConnection(); ConnectionManagerUtil.closeDBConnection();
}
} }
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 {
ConnectionManagerUtil.beginTransaction(); ConnectionManagerUtil.beginDBTransaction();
DAOFactory.getPlatformDAO().unregister(tenantId, identifier, isFileBased); DAOFactory.getPlatformDAO().unregister(tenantId, identifier, isFileBased);
if (isFileBased) { if (isFileBased) {
@ -333,33 +301,41 @@ public class PlatformManagerImpl implements PlatformManager {
tenantPlatforms.remove(identifier); tenantPlatforms.remove(identifier);
} }
} }
ConnectionManagerUtil.commitTransaction(); ConnectionManagerUtil.commitDBTransaction();
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Transaction Management Exception while trying to un-register " + "the platform with identifier : " "Transaction Management Exception while trying to un-register " + "the platform with identifier : "
+ identifier + " tenant :" + tenantId, e); + identifier + " tenant :" + tenantId, e);
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Database Connection Exception while trying to un-register " + "the platform with identifier : " "Database Connection Exception while trying to un-register " + "the platform with identifier : "
+ identifier + " tenant :" + tenantId, e); + identifier + " tenant :" + tenantId, e);
} catch (PlatformManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw e;
} finally { } finally {
ConnectionManagerUtil.closeConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} }
@Override @Override
public void addMapping(int tenantId, List<String> platformIdentifiers) throws PlatformManagementException { public void addMapping(int tenantId, List<String> platformIdentifiers) throws PlatformManagementException {
try { try {
ConnectionManagerUtil.openConnection(); ConnectionManagerUtil.beginDBTransaction();
DAOFactory.getPlatformDAO().addMapping(tenantId, platformIdentifiers); DAOFactory.getPlatformDAO().addMapping(tenantId, platformIdentifiers);
} catch (DBConnectionException e) { ConnectionManagerUtil.commitDBTransaction();
} catch (DBConnectionException | TransactionManagementException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Database Connection Exception while trying to add tenant " + "mapping for tenant ID : " "Database Connection Exception while trying to add tenant " + "mapping for tenant ID : "
+ tenantId); + tenantId);
} catch (PlatformManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw e;
} finally { } finally {
ConnectionManagerUtil.closeConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} }
@ -373,13 +349,217 @@ public class PlatformManagerImpl implements PlatformManager {
@Override @Override
public void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementException { public void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementException {
try { try {
ConnectionManagerUtil.openConnection(); ConnectionManagerUtil.beginDBTransaction();
DAOFactory.getPlatformDAO().removeMapping(tenantId, platformIdentifier); DAOFactory.getPlatformDAO().removeMapping(tenantId, platformIdentifier);
} catch (DBConnectionException e) { ConnectionManagerUtil.commitDBTransaction();
} catch (DBConnectionException | TransactionManagementException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Database Connection Exception while trying to remove tenant mapping for tenant ID : " + tenantId); "Database Connection Exception while trying to remove tenant mapping for tenant ID : " + tenantId);
} catch (PlatformManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw e;
} finally {
ConnectionManagerUtil.closeDBConnection();
}
}
@Override
public void updatePlatformStatus(int tenantId, String platformIdentifier, String status)
throws PlatformManagementException {
try {
ConnectionManagerUtil.beginDBTransaction();
Platform platform = DAOFactory.getPlatformDAO().getPlatform(tenantId, platformIdentifier);
if (platform == null) {
ConnectionManagerUtil.commitDBTransaction();
throw new PlatformManagementException("Platform with identifier : " + platformIdentifier + " does not"
+ " exist for the tenant with id " + tenantId);
} else {
boolean isEnabledNewStatus = status.equalsIgnoreCase("ENABLED");
// If the platform is already in the same status. No need to enable the platform again
if (isEnabledNewStatus == platform.isEnabled()) {
if (log.isDebugEnabled()) {
log.debug("Platform with identifier : " + platformIdentifier + " is already in " +
(isEnabledNewStatus ? "Enabled" : "Disabled") + " status. No need to update.");
}
ConnectionManagerUtil.commitDBTransaction();
return;
} else {
if (isEnabledNewStatus) {
DAOFactory.getPlatformDAO().addMapping(tenantId, getListOfString(platform.getIdentifier()));
} else {
DAOFactory.getPlatformDAO().removeMapping(tenantId, platform.getIdentifier());
}
if (log.isDebugEnabled()) {
log.debug("Platform with identifier : " + platformIdentifier + " successfully " +
(isEnabledNewStatus ? "Enabled" : "Disabled"));
}
}
}
ConnectionManagerUtil.commitDBTransaction();
} catch (TransactionManagementException | DBConnectionException ex) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException("Database exception while trying to update the status of platform "
+ "with identifier '" + platformIdentifier + "' for the tenant" + tenantId);
} catch (PlatformManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw e;
} finally {
ConnectionManagerUtil.closeDBConnection();
}
}
@Override
public void removePlatforms(int tenantId) throws PlatformManagementException {
try {
ConnectionManagerUtil.beginDBTransaction();
DAOFactory.getPlatformDAO().removePlatforms(tenantId);
ConnectionManagerUtil.commitDBTransaction();
} catch (TransactionManagementException | DBConnectionException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException("Database exception while trying to remove all the platforms for"
+ " the tenant " + tenantId);
} catch (PlatformManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw e;
} finally {
ConnectionManagerUtil.closeDBConnection();
}
}
/**
* 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 { } finally {
ConnectionManagerUtil.closeConnection(); ConnectionManagerUtil.closeDBConnection();
}
} }
/**
* 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;
} }
} }

@ -0,0 +1,63 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.wso2.carbon.device.application.mgt.core.internal;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.application.mgt.common.exception.PlatformManagementException;
import org.wso2.carbon.utils.AbstractAxis2ConfigurationContextObserver;
/**
* PlatformManagementAxis2ConfigurationObserverImpl is responsible for adding relevant platform mapping of shared
* platforms during the tenant creation time.
*/
public class PlatformManagementAxis2ConfigurationObserverImpl extends AbstractAxis2ConfigurationContextObserver {
private static Log log = LogFactory.getLog(PlatformManagementAxis2ConfigurationObserverImpl.class);
/**
* Whenever a new tenant creation happens, shared platforms need to be added for the relevant tenant.
* @param tenantId Id of the tenant that is being created
*/
@Override
public void creatingConfigurationContext(int tenantId) {
try {
DataHolder.getInstance().getPlatformManager().initialize(tenantId);
} catch (PlatformManagementException e) {
log.error("Error while trying add platforms to the newly created tenant " + tenantId, e);
}
}
/**
* Whenever terminating a tenant,the platforms added by the tenant need to be removed.
* @param configContext Configuration context.
*/
@Override
public void terminatingConfigurationContext(ConfigurationContext configContext) {
PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
int tenantId = carbonContext.getTenantId();
try {
DataHolder.getInstance().getPlatformManager().removePlatforms(tenantId);
} catch (PlatformManagementException e) {
log.error("Error while removing shared platforms while removing the tenant: " + tenantId, e);
}
}
}

@ -40,6 +40,7 @@ import org.wso2.carbon.device.application.mgt.core.util.ApplicationManagementUti
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.ndatasource.core.DataSourceService; import org.wso2.carbon.ndatasource.core.DataSourceService;
import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.utils.Axis2ConfigurationContextObserver;
import javax.naming.NamingException; import javax.naming.NamingException;
@ -117,6 +118,9 @@ public class ServiceComponent {
DataHolder.getInstance().setApplicationUploadManager(uploadManager); DataHolder.getInstance().setApplicationUploadManager(uploadManager);
bundleContext.registerService(ApplicationUploadManager.class.getName(), uploadManager, null); bundleContext.registerService(ApplicationUploadManager.class.getName(), uploadManager, null);
bundleContext.registerService(Axis2ConfigurationContextObserver.class.getName(),
new PlatformManagementAxis2ConfigurationObserverImpl(), null);
DAOFactory.init(datasourceName); DAOFactory.init(datasourceName);
DAOFactory.initDatabases(); DAOFactory.initDatabases();
log.info("ApplicationManagement core bundle has been successfully initialized"); log.info("ApplicationManagement core bundle has been successfully initialized");
@ -154,12 +158,12 @@ public class ServiceComponent {
} }
protected void setDataSourceService(DataSourceService dataSourceService) { protected void setDataSourceService(DataSourceService dataSourceService) {
//Not implemented. Not needed but to make sure the datasource service are registered, as it is needed create /*Not implemented. Not needed but to make sure the datasource service are registered, as it is needed create
// databases. databases. */
} }
protected void unsetDataSourceService(DataSourceService dataSourceService) { protected void unsetDataSourceService(DataSourceService dataSourceService) {
//Not implemented. Not needed but to make sure the datasource service are registered, as it is needed to create /*Not implemented. Not needed but to make sure the datasource service are registered, as it is needed to create
// databases. databases.*/
} }
} }

@ -19,13 +19,11 @@
package org.wso2.carbon.device.application.mgt.core.util; package org.wso2.carbon.device.application.mgt.core.util;
import java.io.File; import java.io.File;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.CarbonUtils;
import org.wso2.carbon.utils.dbcreator.DatabaseCreator; import org.wso2.carbon.utils.dbcreator.DatabaseCreator;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
/** /**
* ApplicationMgtDatabaseCreator is responsible for creating the Application Management related tables. * ApplicationMgtDatabaseCreator is responsible for creating the Application Management related tables.
*/ */

@ -24,11 +24,14 @@ import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionExcep
import org.wso2.carbon.device.application.mgt.common.exception.IllegalTransactionStateException; import org.wso2.carbon.device.application.mgt.common.exception.IllegalTransactionStateException;
import org.wso2.carbon.device.application.mgt.common.exception.TransactionManagementException; import org.wso2.carbon.device.application.mgt.common.exception.TransactionManagementException;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.sql.DataSource;
/**
* ConnectionManagerUtil is responsible for handling all the datasource connections utilities.
*/
public class ConnectionManagerUtil { public class ConnectionManagerUtil {
private static final Log log = LogFactory.getLog(ConnectionManagerUtil.class); private static final Log log = LogFactory.getLog(ConnectionManagerUtil.class);
@ -58,9 +61,7 @@ public class ConnectionManagerUtil {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();
if (conn == null) { if (conn == null) {
conn = getDBConnection(); conn = getDBConnection();
} } else if (inTransaction(conn)) {
if (inTransaction(conn)) {
throw new IllegalTransactionStateException("Transaction has already been started."); throw new IllegalTransactionStateException("Transaction has already been started.");
} }
@ -259,9 +260,9 @@ public class ConnectionManagerUtil {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();
if (conn == null) { if (conn == null) {
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " + throw new IllegalTransactionStateException("No connection is associated with the current transaction. "
"This might have ideally been caused by not properly initiating the transaction via " + + "This might have ideally been caused by not properly initiating the transaction via "
"'beginTransaction'/'openConnection' methods"); + "'beginTransaction'/'openConnection' methods");
} }
try { try {
conn.close(); conn.close();
@ -298,4 +299,18 @@ public class ConnectionManagerUtil {
return null; return null;
} }
/**
* To check whether particular database that is used for application management supports batch query execution.
*
* @return true if batch query is supported, otherwise false.
*/
public static boolean isBatchQuerySupported() {
try {
return dataSource.getConnection().getMetaData().supportsBatchUpdates();
} catch (SQLException e) {
log.error("Error occurred while checking whether database supports batch updates", e);
}
return false;
}
} }

@ -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)
); );
@ -30,8 +31,8 @@ PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME)
CREATE TABLE IF NOT EXISTS APPM_PLATFORM_TENANT_MAPPING ( CREATE TABLE IF NOT EXISTS APPM_PLATFORM_TENANT_MAPPING (
ID INT NOT NULL AUTO_INCREMENT, ID INT NOT NULL AUTO_INCREMENT,
TENANT_ID INT NOT NULL , TENANT_ID INT NOT NULL ,
PLATFORM_ID VARCHAR (100) NOT NULL, PLATFORM_ID INT NOT NULL,
FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(IDENTIFIER) ON DELETE CASCADE, FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(ID) ON DELETE CASCADE,
PRIMARY KEY (ID, TENANT_ID, PLATFORM_ID) PRIMARY KEY (ID, TENANT_ID, PLATFORM_ID)
); );

@ -1,44 +1,35 @@
CREATE TABLE IF NOT EXISTS APPM_APPLICATION_TYPE ( IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[APPM_PLATFORM]') AND TYPE IN (N'U'))
ID INTEGER AUTO_INCREMENT, CREATE TABLE APPM_PLATFORM (
ID INT IDENTITY(1,1) NOT NULL UNIQUE,
IDENTIFIER VARCHAR (100) NOT NULL,
TENANT_ID INT NOT NULL ,
NAME VARCHAR (255), NAME VARCHAR (255),
DESCRIPTION TEXT, FILE_BASED BIT,
CODE VARCHAR (255), DESCRIPTION VARCHAR(2048),
PARAMTERS LONGTEXT, IS_SHARED BIT,
PRIMARY KEY (ID) IS_DEFAULT_TENANT_MAPPING BIT,
)ENGINE INNODB; ICON_NAME VARCHAR (100),
PRIMARY KEY (IDENTIFIER, TENANT_ID)
);
CREATE TABLE IF NOT EXISTS APPM_APPLICATION_CATEGORY ( IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[APPM_PLATFORM_PROPERTIES]') AND TYPE IN (N'U'))
ID INTEGER AUTO_INCREMENT, CREATE TABLE APPM_PLATFORM_PROPERTIES (
NAME VARCHAR (255), ID INT IDENTITY(1,1) NOT NULL,
DESCRIPTION TEXT, PLATFORM_ID INT NOT NULL,
PRIMARY KEY (ID) PROP_NAME VARCHAR (100) NOT NULL,
)ENGINE INNODB; OPTIONAL BIT,
DEFAUL_VALUE VARCHAR (255),
FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(ID) ON DELETE CASCADE,
CREATE TABLE IF NOT EXISTS APPM_APPLICATION ( PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME)
ID INTEGER AUTO_INCREMENT, );
NAME VARCHAR (255),
UUID VARCHAR (255),
DESCRIPTION MEDIUMTEXT,
ICON_NAME VARCHAR (255),
BANNER_NAME VARCHAR (255),
VIDEO_NAME VARCHAR (255),
SCREENSHOTS TEXT,
TAGS TEXT,
APPLICATION_TYPE_ID INTEGER,
CATEGORY_ID INTEGER,
CREATED_AT DATETIME,
MODIFIED_AT DATETIME,
PRIMARY KEY (ID),
FOREIGN KEY (CATEGORY_ID) REFERENCES APPM_APPLICATION_CATEGORY(ID),
FOREIGN KEY (APPLICATION_TYPE_ID) REFERENCES APPM_APPLICATION_TYPE(ID)
)ENGINE INNODB;
IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[APPM_PLATFORM_TENANT_MAPPING]') AND TYPE IN (N'U'))
CREATE TABLE APPM_PLATFORM_TENANT_MAPPING (
ID INT IDENTITY(1,1) NOT NULL,
TENANT_ID INT NOT NULL ,
PLATFORM_ID INT NOT NULL,
FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(ID) ON DELETE CASCADE,
PRIMARY KEY (ID, TENANT_ID, PLATFORM_ID)
);
CREATE TABLE IF NOT EXISTS APPM_APPLICATION_PROPERTIES ( CREATE INDEX FK_PLATFROM_TENANT_MAPPING_PLATFORM ON APPM_PLATFORM_TENANT_MAPPING(PLATFORM_ID ASC);
PROP_KEY VARCHAR (255),
PROP_VAL MEDIUMTEXT,
APPLICATION_ID INTEGER,
PRIMARY KEY (APPLICATION_ID, PROP_KEY),
FOREIGN KEY (APPLICATION_ID) REFERENCES APPM_APPLICATION(ID)
)ENGINE INNODB;

@ -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)
); );

@ -1,44 +1,80 @@
CREATE TABLE IF NOT EXISTS APPM_APPLICATION_TYPE ( -- -----------------------------------------------------
ID INTEGER AUTO_INCREMENT, -- Schema WSO2DM_APPM_DB
NAME VARCHAR (255), -- -----------------------------------------------------
DESCRIPTION TEXT,
CODE VARCHAR (255), -- -----------------------------------------------------
PARAMTERS LONGTEXT, -- Table APPM_PLATFORM
PRIMARY KEY (ID) -- -----------------------------------------------------
)ENGINE INNODB; CREATE TABLE APPM_PLATFORM (
ID INT UNIQUE,
CREATE TABLE IF NOT EXISTS APPM_APPLICATION_CATEGORY ( IDENTIFIER VARCHAR (100) NOT NULL,
ID INTEGER AUTO_INCREMENT, TENANT_ID INT NOT NULL ,
NAME VARCHAR (255), NAME VARCHAR (255),
DESCRIPTION TEXT, FILE_BASED NUMBER (1),
PRIMARY KEY (ID) DESCRIPTION VARCHAR (2048),
)ENGINE INNODB; IS_SHARED NUMBER (1),
IS_DEFAULT_TENANT_MAPPING NUMBER (1),
ICON_NAME VARCHAR (100),
PRIMARY KEY (IDENTIFIER, TENANT_ID)
)
/
CREATE SEQUENCE APPM_PLATFORM_SEQ START WITH 1 INCREMENT BY 1 NOCACHE
/
CREATE OR REPLACE TRIGGER APPM_PLATFORM_TRIG
BEFORE INSERT
ON APPM_PLATFORM
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT APPM_PLATFORM_SEQ.nextval INTO :NEW.ID FROM dual;
END;
/
CREATE TABLE IF NOT EXISTS APPM_APPLICATION (
ID INTEGER AUTO_INCREMENT, CREATE TABLE APPM_PLATFORM_PROPERTIES (
NAME VARCHAR (255), ID INT,
UUID VARCHAR (255), PLATFORM_ID INT NOT NULL,
DESCRIPTION MEDIUMTEXT, PROP_NAME VARCHAR (100) NOT NULL,
ICON_NAME VARCHAR (255), OPTIONAL NUMBER (1),
BANNER_NAME VARCHAR (255), DEFAUL_VALUE VARCHAR (255),
VIDEO_NAME VARCHAR (255), FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(ID) ON DELETE CASCADE,
SCREENSHOTS TEXT, PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME)
TAGS TEXT, )
APPLICATION_TYPE_ID INTEGER, /
CATEGORY_ID INTEGER,
CREATED_AT DATETIME, CREATE SEQUENCE APPM_PLATFORM_PROPERTIES_SEQ START WITH 1 INCREMENT BY 1 NOCACHE
MODIFIED_AT DATETIME, /
PRIMARY KEY (ID), CREATE OR REPLACE TRIGGER APPM_PLATFORM_PROPERTIES_TRIG
FOREIGN KEY (CATEGORY_ID) REFERENCES APPM_APPLICATION_CATEGORY(ID), BEFORE INSERT
FOREIGN KEY (APPLICATION_TYPE_ID) REFERENCES APPM_APPLICATION_TYPE(ID) ON APPM_PLATFORM_PROPERTIES
)ENGINE INNODB; REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
CREATE TABLE IF NOT EXISTS APPM_APPLICATION_PROPERTIES ( SELECT APPM_PLATFORM_PROPERTIES_SEQ.nextval INTO :NEW.ID FROM dual;
PROP_KEY VARCHAR (255), END;
PROP_VAL MEDIUMTEXT, /
APPLICATION_ID INTEGER,
PRIMARY KEY (APPLICATION_ID, PROP_KEY), CREATE TABLE APPM_PLATFORM_TENANT_MAPPING (
FOREIGN KEY (APPLICATION_ID) REFERENCES APPM_APPLICATION(ID) ID INT,
)ENGINE INNODB; TENANT_ID INT NOT NULL ,
PLATFORM_ID INT NOT NULL,
FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(ID) ON DELETE CASCADE,
PRIMARY KEY (ID, TENANT_ID, PLATFORM_ID)
)
/
CREATE SEQUENCE APPM_TENANT_MAPPING_SEQ START WITH 1 INCREMENT BY 1 NOCACHE
/
CREATE OR REPLACE TRIGGER APPM_TENANT_MAPPING_TRIG
BEFORE INSERT
ON APPM_PLATFORM_TENANT_MAPPING
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT APPM_TENANT_MAPPING_SEQ.nextval INTO :NEW.ID FROM dual;
END;
/
CREATE INDEX FK_PLATFROM_TENANT_MAPPING ON APPM_PLATFORM_TENANT_MAPPING(PLATFORM_ID ASC)
/

@ -1,44 +1,44 @@
CREATE TABLE IF NOT EXISTS APPM_APPLICATION_TYPE ( DROP TABLE IF EXISTS APPM_PLATFORM;
ID INTEGER AUTO_INCREMENT, DROP SEQUENCE IF EXISTS APPM_PLATFORM_PK_SEQ;
NAME VARCHAR (255), CREATE SEQUENCE APPM_PLATFORM_PK_SEQ;
DESCRIPTION TEXT,
CODE VARCHAR (255),
PARAMTERS LONGTEXT,
PRIMARY KEY (ID)
)ENGINE INNODB;
CREATE TABLE IF NOT EXISTS APPM_APPLICATION_CATEGORY ( CREATE TABLE APPM_PLATFORM (
ID INTEGER AUTO_INCREMENT, ID INT DEFAULT NEXTVAL('APPM_PLATFORM_PK_SEQ') UNIQUE,
IDENTIFIER VARCHAR (100) NOT NULL,
TENANT_ID INT NOT NULL ,
NAME VARCHAR (255), NAME VARCHAR (255),
DESCRIPTION TEXT, FILE_BASED BOOLEAN,
PRIMARY KEY (ID) DESCRIPTION VARCHAR(2048),
)ENGINE INNODB; IS_SHARED BOOLEAN,
IS_DEFAULT_TENANT_MAPPING BOOLEAN,
ICON_NAME VARCHAR (100),
PRIMARY KEY (IDENTIFIER, TENANT_ID)
);
DROP TABLE IF EXISTS APPM_PLATFORM_PROPERTIES;
DROP SEQUENCE IF EXISTS APPM_PLATFORM_PROPERTIES_PK_SEQ;
CREATE SEQUENCE APPM_PLATFORM_PROPERTIES_PK_SEQ;
CREATE TABLE IF NOT EXISTS APPM_APPLICATION ( CREATE TABLE APPM_PLATFORM_PROPERTIES (
ID INTEGER AUTO_INCREMENT, ID INT DEFAULT NEXTVAL('APPM_PLATFORM_PROPERTIES_PK_SEQ'),
NAME VARCHAR (255), PLATFORM_ID INT NOT NULL,
UUID VARCHAR (255), PROP_NAME VARCHAR (100) NOT NULL,
DESCRIPTION MEDIUMTEXT, OPTIONAL BOOLEAN,
ICON_NAME VARCHAR (255), DEFAUL_VALUE VARCHAR (255),
BANNER_NAME VARCHAR (255), FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(ID) ON DELETE CASCADE,
VIDEO_NAME VARCHAR (255), PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME)
SCREENSHOTS TEXT, );
TAGS TEXT,
APPLICATION_TYPE_ID INTEGER, DROP TABLE IF EXISTS APPM_PLATFORM_TENANT_MAPPING;
CATEGORY_ID INTEGER, DROP SEQUENCE IF EXISTS APPM_PLATFORM_TENANT_MAPPING_PK_SEQ;
CREATED_AT DATETIME, CREATE SEQUENCE APPM_PLATFORM_TENANT_MAPPING_PK_SEQ;
MODIFIED_AT DATETIME,
PRIMARY KEY (ID),
FOREIGN KEY (CATEGORY_ID) REFERENCES APPM_APPLICATION_CATEGORY(ID),
FOREIGN KEY (APPLICATION_TYPE_ID) REFERENCES APPM_APPLICATION_TYPE(ID)
)ENGINE INNODB;
CREATE TABLE APPM_PLATFORM_TENANT_MAPPING (
ID INT DEFAULT NEXTVAL('APPM_PLATFORM_TENANT_MAPPING_PK_SEQ'),
TENANT_ID INT NOT NULL ,
PLATFORM_ID INT NOT NULL,
FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(ID) ON DELETE CASCADE,
PRIMARY KEY (ID, TENANT_ID, PLATFORM_ID)
);
CREATE TABLE IF NOT EXISTS APPM_APPLICATION_PROPERTIES ( CREATE INDEX FK_PLATFROM_TENANT_MAPPING_PLATFORM ON APPM_PLATFORM_TENANT_MAPPING(PLATFORM_ID ASC);
PROP_KEY VARCHAR (255),
PROP_VAL MEDIUMTEXT,
APPLICATION_ID INTEGER,
PRIMARY KEY (APPLICATION_ID, PROP_KEY),
FOREIGN KEY (APPLICATION_ID) REFERENCES APPM_APPLICATION(ID)
)ENGINE INNODB;
Loading…
Cancel
Save