Adding platform removal

feature/appm-store/pbac
megala21 8 years ago
parent f16b03ffee
commit eda5b5909c

@ -34,6 +34,7 @@ import org.wso2.carbon.device.application.mgt.common.Platform;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.PUT; import javax.ws.rs.PUT;
@ -82,6 +83,12 @@ import javax.ws.rs.core.Response;
description = "Update a platform", description = "Update a platform",
key = "perm:platform:update", key = "perm:platform:update",
permissions = {"/device-mgt/platform/update"} permissions = {"/device-mgt/platform/update"}
),
@org.wso2.carbon.apimgt.annotations.api.Scope(
name = "Remove a platform",
description = "Remove a platform",
key = "perm:platform:remove",
permissions = {"/device-mgt/platform/remove"}
) )
} }
) )
@ -251,4 +258,43 @@ public interface PlatformManagementAPI {
String identifier String identifier
); );
@DELETE
@Path("/{identifier}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(
consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON,
httpMethod = "DELETE",
value = "Remove Platform",
notes = "This will remove the relevant platform.",
tags = "Platform Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = SCOPE, value = "perm:platform:remove")
})
}
)
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "OK. \n Successfully updated the platform"),
@ApiResponse(
code = 400,
message = "Bad Request. \n Invalid request parameters passed."),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Error occurred while getting the platform list.",
response = ErrorResponse.class)
})
Response removePlatform(
@ApiParam(
name = "identifier",
required = true)
@PathParam("identifier")
@Size(max = 45)
String identifier
);
} }

@ -29,6 +29,7 @@ import org.wso2.carbon.device.application.mgt.core.exception.PlatformManagementD
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.PUT; import javax.ws.rs.PUT;
@ -102,8 +103,12 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI {
Platform platform = APIUtil.getPlatformManager().getPlatform(tenantId, id); Platform platform = APIUtil.getPlatformManager().getPlatform(tenantId, id);
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 :"
+ tenantId, e);
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
} catch (PlatformManagementException e) { } catch (PlatformManagementException e) {
log.error("Error while trying the get the platform with the identifier : " + id + " for the tenant :"
+ tenantId, e);
return APIUtil.getResponse(e, Response.Status.NOT_FOUND); return APIUtil.getResponse(e, Response.Status.NOT_FOUND);
} }
} }
@ -118,7 +123,8 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI {
APIUtil.getPlatformManager().register(tenantId, platform); APIUtil.getPlatformManager().register(tenantId, platform);
return Response.status(Response.Status.CREATED).build(); return Response.status(Response.Status.CREATED).build();
} else { } else {
return APIUtil.getResponse("Invxalid payload! Platform ID and names are mandatory fields!", return APIUtil
.getResponse("Invalid payload! Platform 'identifier' and 'name' are mandatory fields!",
Response.Status.BAD_REQUEST); Response.Status.BAD_REQUEST);
} }
} else { } else {
@ -126,6 +132,8 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI {
Response.Status.BAD_REQUEST); Response.Status.BAD_REQUEST);
} }
} catch (PlatformManagementException e) { } catch (PlatformManagementException e) {
log.error("Platform Management Exception while trying to add the platform with identifier : " + platform
.getIdentifier() + " for the tenant : " + tenantId, e);
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
} }
} }
@ -143,4 +151,19 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI {
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
} }
} }
@DELETE
@Path("/{identifier}")
@Override
public Response removePlatform(@PathParam("identifier") @Size(max = 45) String id) {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
try {
APIUtil.getPlatformManager().unregister(tenantId, id, false);
return Response.status(Response.Status.OK).build();
} catch (PlatformManagementException e) {
log.error("Platform Management Exception while trying to un-register the platform with the identifier : "
+ id + " for the tenant : " + tenantId, e);
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
}
}
} }

@ -63,6 +63,10 @@
<url>/application-mgt/platforms/*</url> <url>/application-mgt/platforms/*</url>
<method>PUT</method> <method>PUT</method>
</Permission> </Permission>
<Permission>
<name>Remove Platform</name>
<path>/device-mgt/platform/remove</path>
<url>/application-mgt/platforms/*</url>
<method>DELETE</method>
</Permission>
</PermissionConfiguration> </PermissionConfiguration>

@ -32,7 +32,7 @@ public interface PlatformDAO {
void update(int tenantId, String oldPlatformIdentifier, Platform platform) throws PlatformManagementDAOException; void update(int tenantId, String oldPlatformIdentifier, Platform platform) throws PlatformManagementDAOException;
void unregister(int tenantId, String platformIdentifier) throws PlatformManagementDAOException; void unregister(int tenantId, String platformIdentifier, boolean isFileBased) throws PlatformManagementDAOException;
void addMapping(int tenantId, List<String> platformIdentifiers) throws PlatformManagementDAOException; void addMapping(int tenantId, List<String> platformIdentifiers) throws PlatformManagementDAOException;

@ -20,17 +20,19 @@ package org.wso2.carbon.device.application.mgt.core.dao.common;
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.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.wso2.carbon.device.application.mgt.common.Application; import org.wso2.carbon.device.application.mgt.common.Application;
import org.wso2.carbon.device.application.mgt.common.Platform;
import org.wso2.carbon.device.application.mgt.common.Category; import org.wso2.carbon.device.application.mgt.common.Category;
import org.wso2.carbon.device.application.mgt.common.Platform;
import org.wso2.carbon.device.application.mgt.core.util.JSONUtil; import org.wso2.carbon.device.application.mgt.core.util.JSONUtil;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.*; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Util { public class Util {
@ -93,6 +95,4 @@ public class Util {
} }
} }
} }
} }

@ -24,6 +24,7 @@ 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.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.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.device.application.mgt.core.util.ConnectionManagerUtil;
@ -44,15 +45,15 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
@Override @Override
public int register(int tenantId, Platform platform) throws PlatformManagementDAOException { public int register(int tenantId, Platform platform) throws PlatformManagementDAOException {
PreparedStatement preparedStatement = null;
try { try {
ConnectionManagerUtil.beginTransaction();
int platformId = getPlatformId(tenantId, platform.getIdentifier()); int platformId = getPlatformId(tenantId, platform.getIdentifier());
if (platformId == -1) { if (platformId == -1) {
Connection connection = ConnectionManagerUtil.getConnection(); Connection connection = this.getConnection();
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)" + " VALUES (?, ?, ?, ?, ?, ?, ?)";
PreparedStatement 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.setString(3, platform.getName()); preparedStatement.setString(3, platform.getName());
@ -80,7 +81,7 @@ 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)" + " VALUES (?, ?, ?)";
PreparedStatement 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);
@ -89,35 +90,24 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
if (platformId == -1) { if (platformId == -1) {
platformId = getPlatformId(tenantId, platform.getIdentifier()); platformId = getPlatformId(tenantId, platform.getIdentifier());
} }
ConnectionManagerUtil.commitTransaction();
return platformId; return platformId;
} else { } else {
if (!platform.isFileBased()) { if (!platform.isFileBased()) {
ConnectionManagerUtil.rollbackTransaction(); throw new PlatformManagementDAOException(
throw new PlatformManagementDAOException("Platform - " + platform.getIdentifier() "Platform - " + platform.getIdentifier() + " is already registered for tenant - "
+ " is already registered for tenant - " + tenantId); + tenantId);
} else { } else {
return platformId; return platformId;
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException("Error while executing the SQL query. ", e); throw new PlatformManagementDAOException("Error while executing the SQL query. ", e);
} catch (PlatformManagementDAOException ex) {
ConnectionManagerUtil.rollbackTransaction();
throw ex;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Unable to obtain the connection while trying to register the platform - " + platform "Unable to obtain the connection while trying to register the platform - " + platform
.getIdentifier() + " for tenant - " + tenantId, e); .getIdentifier() + " for tenant - " + tenantId, e);
} catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException(
"Error occurred while performing the transaction on the database " + "for adding the platform - "
+ platform.getIdentifier() + " , tenant domain - " + tenantId);
} finally { } finally {
ConnectionManagerUtil.closeConnection(); Util.cleanupResources(preparedStatement, null);
} }
} }
@ -126,19 +116,39 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
PlatformManagementDAOException { PlatformManagementDAOException {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
try { try {
ConnectionManagerUtil.beginTransaction();
int platformId = getPlatformId(tenantId, oldPlatformIdentifier); int platformId = getPlatformId(tenantId, oldPlatformIdentifier);
boolean isIdentifierNull = platform.getIdentifier() == null;
boolean isNameNull = platform.getName() == null;
if (platformId != -1) { if (platformId != -1) {
Connection connection = ConnectionManagerUtil.getConnection(); Connection connection = this.getConnection();
if (!platform.isFileBased()) { if (!platform.isFileBased()) {
String insertToPlatform = "UPDATE APPM_PLATFORM SET IDENTIFIER = ?, NAME =?, DESCRIPTION=?, " String insertToPlatform = "UPDATE APPM_PLATFORM SET DESCRIPTION=?, IS_SHARED=?, ICON_NAME=?";
+ "IS_SHARED=?, ICON_NAME=? WHERE ID = ?"; if (!isIdentifierNull) {
insertToPlatform += ",IDENTIFIER = ? ";
}
if (!isNameNull) {
insertToPlatform += ", NAME =?";
}
insertToPlatform += " WHERE ID = ?";
preparedStatement = connection.prepareStatement(insertToPlatform); preparedStatement = connection.prepareStatement(insertToPlatform);
preparedStatement.setString(1, platform.getIdentifier()); preparedStatement.setString(1, platform.getDescription());
preparedStatement.setString(2, platform.getName()); preparedStatement.setBoolean(2, platform.isShared());
preparedStatement.setString(3, platform.getDescription()); preparedStatement.setString(3, platform.getIconName());
preparedStatement.setBoolean(4, platform.isShared());
preparedStatement.setString(5, platform.getIconName()); if (!isIdentifierNull && !isNameNull) {
preparedStatement.setString(4, platform.getIdentifier());
preparedStatement.setString(5, platform.getName());
preparedStatement.setInt(6, platformId);
} else if (isIdentifierNull && !isNameNull) {
preparedStatement.setString(4, platform.getName());
preparedStatement.setInt(5, platformId);
} else if (!isIdentifierNull) {
preparedStatement.setString(4, platform.getIdentifier());
preparedStatement.setInt(5, platformId);
} else {
preparedStatement.setInt(4, platformId);
}
preparedStatement.execute(); preparedStatement.execute();
platformId = getPlatformId(tenantId, platform.getIdentifier()); platformId = getPlatformId(tenantId, platform.getIdentifier());
@ -150,6 +160,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
String insertPlatformProps = String insertPlatformProps =
"INSERT INTO APPM_PLATFORM_PROPERTIES (PLATFORM_ID, PROP_NAME, OPTIONAL," "INSERT INTO APPM_PLATFORM_PROPERTIES (PLATFORM_ID, PROP_NAME, OPTIONAL,"
+ " DEFAULT_VALUE) VALUES ( ? , ?, ? , ?)"; + " DEFAULT_VALUE) VALUES ( ? , ?, ? , ?)";
if (platform.getProperties() != null) {
for (Platform.Property property : platform.getProperties()) { for (Platform.Property property : platform.getProperties()) {
preparedStatement = connection.prepareStatement(insertPlatformProps); preparedStatement = connection.prepareStatement(insertPlatformProps);
preparedStatement.setInt(1, platformId); preparedStatement.setInt(1, platformId);
@ -158,37 +170,26 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
preparedStatement.setString(4, property.getDefaultValue()); preparedStatement.setString(4, property.getDefaultValue());
preparedStatement.execute(); preparedStatement.execute();
} }
}
} else { } else {
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.setInt(1, platformId);
preparedStatement.execute(); preparedStatement.execute();
} }
ConnectionManagerUtil.commitTransaction();
} else { } else {
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Cannot find any platform that was registered with identifier - " + platform.getIdentifier() "Cannot find any platform that was registered with identifier - " + platform.getIdentifier()
+ " for tenant - " + tenantId); + " for tenant - " + tenantId);
} }
} catch (SQLException e) { } catch (SQLException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException("Error while executing the SQL query. ", e); throw new PlatformManagementDAOException("Error while executing the SQL query. ", e);
} catch (PlatformManagementDAOException ex) {
ConnectionManagerUtil.rollbackTransaction();
throw ex;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Unable to obtain the connection while trying to register the platform - " + platform "Unable to obtain the connection while trying to register the platform - " + platform
.getIdentifier() + " for tenant - " + tenantId, e); .getIdentifier() + " for tenant - " + tenantId, e);
} catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException(
"Error occurred while performing the transaction on the database " + "for adding the platform - "
+ platform.getIdentifier() + " , tenant domain - " + tenantId);
} finally { } finally {
ConnectionManagerUtil.cleanupResources(preparedStatement, null); Util.cleanupResources(preparedStatement, null);
ConnectionManagerUtil.closeConnection();
} }
} }
@ -198,7 +199,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
String query = "SELECT ID FROM APPM_PLATFORM WHERE (TENANT_ID=? AND IDENTIFIER=?) OR (IS_SHARED = TRUE AND " String query = "SELECT ID FROM APPM_PLATFORM WHERE (TENANT_ID=? AND IDENTIFIER=?) OR (IS_SHARED = TRUE AND "
+ "IDENTIFIER=?)"; + "IDENTIFIER=?)";
try { try {
Connection connection = ConnectionManagerUtil.getConnection(); Connection connection = this.getConnection();
preparedStatement = connection.prepareStatement(query); preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, tenantId); preparedStatement.setInt(1, tenantId);
preparedStatement.setString(2, platformIdentifier); preparedStatement.setString(2, platformIdentifier);
@ -213,47 +214,46 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
} catch (SQLException e) { } catch (SQLException e) {
throw new PlatformManagementDAOException("Error in executing the query - " + query, e); throw new PlatformManagementDAOException("Error in executing the query - " + query, e);
} finally { } finally {
ConnectionManagerUtil.cleanupResources(preparedStatement, resultSet); Util.cleanupResources(preparedStatement, resultSet);
} }
} }
@Override @Override
public void unregister(int tenantId, String platformIdenfier) throws PlatformManagementDAOException { public void unregister(int tenantId, String platformIdenfier, boolean isFileBased) throws
PlatformManagementDAOException {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
try { try {
ConnectionManagerUtil.beginTransaction(); Platform platform = getPlatform(tenantId, platformIdenfier);
int platformId = getPlatformId(tenantId, platformIdenfier);
if (platformId != -1) { if (platform != null) {
Connection connection = ConnectionManagerUtil.getConnection(); if (isFileBased == platform.isFileBased()) {
Connection connection = this.getConnection();
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, platformId); preparedStatement.setInt(1, platform.getId());
preparedStatement.execute(); preparedStatement.execute();
ConnectionManagerUtil.commitTransaction();
} else { } else {
throw new PlatformManagementDAOException("Platform identifier - " + platformIdenfier if (isFileBased) {
+ " is already unregistered registered for tenant - " + tenantId); throw new PlatformManagementDAOException("Platform with identifier - " + platformIdenfier
+ " is not a file based platform. Try to remove that using PlatformManagement APIs");
} else {
throw new PlatformManagementDAOException("Platform with identifier - " + platformIdenfier
+ " is a file based platform. Try to remove that by un-deploying the relevant file.");
}
} }
} catch (TransactionManagementException e) { } else {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Unable to start the transaction while trying to register the platform - " + platformIdenfier "Platform identifier - " + platformIdenfier + " is not registered for tenant - " + tenantId);
+ " for tenant - " + tenantId, e); }
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Unable to obtain the connection while trying to register the platform - " + platformIdenfier "Unable to obtain the connection while trying to register the platform - " + platformIdenfier
+ " for tenant - " + tenantId, e); + " for tenant - " + tenantId, e);
} catch (SQLException e) { } catch (SQLException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException("Error while executing the SQL query. ", e); throw new PlatformManagementDAOException("Error while executing the SQL query. ", e);
} catch (PlatformManagementDAOException ex) {
ConnectionManagerUtil.rollbackTransaction();
throw ex;
} finally { } finally {
ConnectionManagerUtil.cleanupResources(preparedStatement, null); Util.cleanupResources(preparedStatement, null);
ConnectionManagerUtil.closeConnection();
} }
} }
@ -261,11 +261,10 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
String insertMapping = "INSERT INTO APPM_PLATFORM_TENANT_MAPPING(TENANT_ID, PLATFORM_ID) VALUES (?, ?)"; String insertMapping = "INSERT INTO APPM_PLATFORM_TENANT_MAPPING(TENANT_ID, PLATFORM_ID) VALUES (?, ?)";
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
try { try {
ConnectionManagerUtil.beginTransaction();
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 = ConnectionManagerUtil.getConnection(); Connection connection = this.getConnection();
preparedStatement = connection.prepareStatement(insertMapping); preparedStatement = connection.prepareStatement(insertMapping);
preparedStatement.setInt(1, tenantId); preparedStatement.setInt(1, tenantId);
preparedStatement.setInt(2, platformId); preparedStatement.setInt(2, platformId);
@ -275,40 +274,30 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
+ " - " + tenantId); + " - " + tenantId);
} }
} }
ConnectionManagerUtil.commitTransaction();
} catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException(
"Error occured while trying to add the mapping of platform - " + platformIdentifiers.toString()
+ " for tenant - " + tenantId, e);
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException("Error occurred when getting the connection for the database. ", throw new PlatformManagementDAOException("Error occurred when getting the connection for the database. ",
e); e);
} catch (SQLException e) { } catch (SQLException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException("Error occured while executing the SQL query - " + insertMapping, throw new PlatformManagementDAOException("Error occured while executing the SQL query - " + insertMapping,
e); e);
} catch (PlatformManagementDAOException ex) {
ConnectionManagerUtil.rollbackTransaction();
throw ex;
} finally { } finally {
ConnectionManagerUtil.cleanupResources(preparedStatement, null); Util.cleanupResources(preparedStatement, null);
ConnectionManagerUtil.closeConnection();
} }
} }
private int getTenantPlatformMapping(int tenantId, String platformIdentifier) throws private int getTenantPlatformMapping(int tenantId, String platformIdentifier) throws
PlatformManagementDAOException { PlatformManagementDAOException {
PreparedStatement preparedStatement = 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 = ConnectionManagerUtil.getConnection(); Connection connection = this.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(getMapping); preparedStatement = connection.prepareStatement(getMapping);
preparedStatement.setInt(1, tenantId); preparedStatement.setInt(1, tenantId);
preparedStatement.setString(2, platformIdentifier); preparedStatement.setString(2, platformIdentifier);
ResultSet resultSet = preparedStatement.executeQuery(); resultSet = preparedStatement.executeQuery();
if (resultSet.next()) { if (resultSet.next()) {
return resultSet.getInt("ID"); return resultSet.getInt("ID");
} }
@ -319,6 +308,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
e); e);
} catch (SQLException e) { } catch (SQLException e) {
throw new PlatformManagementDAOException("Error occured while executing the SQL query - " + getMapping, e); throw new PlatformManagementDAOException("Error occured while executing the SQL query - " + getMapping, e);
} finally {
Util.cleanupResources(preparedStatement, resultSet);
} }
} }
@ -327,32 +318,24 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
String deleteMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE ID = ?"; String deleteMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE ID = ?";
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
try { try {
ConnectionManagerUtil.beginTransaction();
int mappingId = getTenantPlatformMapping(tenantId, platformIdentifier); int mappingId = getTenantPlatformMapping(tenantId, platformIdentifier);
if (mappingId != -1) { if (mappingId != -1) {
Connection connection = ConnectionManagerUtil.getConnection(); Connection connection = this.getConnection();
preparedStatement = connection.prepareStatement(deleteMapping); preparedStatement = connection.prepareStatement(deleteMapping);
preparedStatement.setInt(1, mappingId); preparedStatement.setInt(1, mappingId);
preparedStatement.execute(); preparedStatement.execute();
ConnectionManagerUtil.commitTransaction();
} else { } else {
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Platform - " + platformIdentifier + " is already unassigned for tenant - " + tenantId); "Platform - " + platformIdentifier + " is already unassigned for tenant - " + tenantId);
} }
} catch (TransactionManagementException | DBConnectionException e) { } catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Error occurred while unassigning the platform - " + platformIdentifier + " for tenant - " "Error occurred while unassigning the platform - " + platformIdentifier + " for tenant - "
+ tenantId); + tenantId);
} catch (SQLException e) { } catch (SQLException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException("Error occurred while executing the query - " + deleteMapping); throw new PlatformManagementDAOException("Error occurred while executing the query - " + deleteMapping);
} catch (PlatformManagementDAOException ex) {
ConnectionManagerUtil.rollbackTransaction();
throw ex;
} finally { } finally {
ConnectionManagerUtil.cleanupResources(preparedStatement, null); Util.cleanupResources(preparedStatement, null);
ConnectionManagerUtil.closeConnection();
} }
} }
@ -362,21 +345,19 @@ 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 {
ConnectionManagerUtil.openConnection(); Connection connection = this.getConnection();
Connection connection = ConnectionManagerUtil.getConnection();
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);
preparedStatement.execute(); preparedStatement.execute();
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Error occurred while obtaining the connection to get the existing " + "Tenant - Platform Mapping.", "Error occurred while obtaining the connection to remove existing " + "Tenant - Platform Mapping"
e); + " for the platform : " + platformIdentifier, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new PlatformManagementDAOException("Error occurred while executing the SQL query - " + getMapping, e); throw new PlatformManagementDAOException("Error occurred while executing the SQL query - " + getMapping, e);
} finally { } finally {
ConnectionManagerUtil.cleanupResources(preparedStatement, null); Util.cleanupResources(preparedStatement, null);
ConnectionManagerUtil.closeConnection();
} }
} }
@ -394,7 +375,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
+ "IS_SHARED = TRUE AND FILE_BASED = FALSE) PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING " + "IS_SHARED = TRUE AND FILE_BASED = FALSE) PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING "
+ "MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID"; + "MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID";
try { try {
Connection connection = ConnectionManagerUtil.openConnection(); Connection connection = this.getConnection();
preparedStatement = connection.prepareStatement(selectQuery); preparedStatement = connection.prepareStatement(selectQuery);
preparedStatement.setInt(1, tenantId); preparedStatement.setInt(1, tenantId);
resultSet = preparedStatement.executeQuery(); resultSet = preparedStatement.executeQuery();
@ -427,8 +408,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
} catch (SQLException e) { } catch (SQLException e) {
throw new PlatformManagementDAOException("Error occurred when executing query - " + selectQuery, e); throw new PlatformManagementDAOException("Error occurred when executing query - " + selectQuery, e);
} finally { } finally {
ConnectionManagerUtil.cleanupResources(preparedStatement,resultSet); Util.cleanupResources(preparedStatement,resultSet);
ConnectionManagerUtil.closeConnection();
} }
} }
@ -439,8 +419,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
"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 {
ConnectionManagerUtil.openConnection(); Connection connection = this.getConnection();
Connection connection = ConnectionManagerUtil.getConnection();
preparedStatement = connection.prepareStatement(platformQuery); preparedStatement = connection.prepareStatement(platformQuery);
preparedStatement.setString(1, tenantDomain); preparedStatement.setString(1, tenantDomain);
preparedStatement.setString(2, platformIdentifier); preparedStatement.setString(2, platformIdentifier);
@ -476,8 +455,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
} catch (SQLException e) { } catch (SQLException e) {
throw new PlatformManagementDAOException("Error in executing the query - " + platformQuery, e); throw new PlatformManagementDAOException("Error in executing the query - " + platformQuery, e);
} finally { } finally {
ConnectionManagerUtil.cleanupResources(preparedStatement, resultSet); Util.cleanupResources(preparedStatement, resultSet);
ConnectionManagerUtil.closeConnection();
} }
} }
@ -518,7 +496,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
} 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.", e);
} finally { } finally {
ConnectionManagerUtil.cleanupResources(stmt, rs); Util.cleanupResources(stmt, rs);
} }
} }
} }

@ -20,10 +20,14 @@ package org.wso2.carbon.device.application.mgt.core.impl;
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.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.PlatformManagementException; import org.wso2.carbon.device.application.mgt.common.exception.PlatformManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.TransactionManagementException;
import org.wso2.carbon.device.application.mgt.common.services.PlatformManager; import org.wso2.carbon.device.application.mgt.common.services.PlatformManager;
import org.wso2.carbon.device.application.mgt.core.dao.common.DAOFactory; import org.wso2.carbon.device.application.mgt.core.dao.common.DAOFactory;
import org.wso2.carbon.device.application.mgt.core.exception.PlatformManagementDAOException;
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.ConnectionManagerUtil;
import org.wso2.carbon.user.api.Tenant; import org.wso2.carbon.user.api.Tenant;
import org.wso2.carbon.user.api.TenantManager; import org.wso2.carbon.user.api.TenantManager;
import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreException;
@ -47,6 +51,8 @@ public class PlatformManagerImpl implements PlatformManager {
@Override @Override
public void initialize(int tenantId) throws PlatformManagementException { public void initialize(int tenantId) throws PlatformManagementException {
try {
ConnectionManagerUtil.beginTransaction();
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) {
@ -54,18 +60,40 @@ public class PlatformManagerImpl implements PlatformManager {
platformIdentifiers.add(platform.getIdentifier()); platformIdentifiers.add(platform.getIdentifier());
} }
} }
addMapping(tenantId, platformIdentifiers); DAOFactory.getPlatformDAO().addMapping(tenantId, platformIdentifiers);
ConnectionManagerUtil.commitTransaction();
} catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException(
"Transaction Management Exception while initializing the " + "platforms for the tenant : "
+ tenantId, e);
} catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException(
"Database Connection Exception while initializing the " + "platforms for the tenant : " + tenantId,
e);
} finally {
ConnectionManagerUtil.closeConnection();
}
} }
@Override @Override
public List<Platform> getPlatforms(int tenantId) throws PlatformManagementException { public List<Platform> getPlatforms(int tenantId) throws PlatformManagementException {
int platformIndex = 0;
List<Platform> platforms;
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request for getting platforms received for the tenant ID " + tenantId + " at " log.debug("Request for getting platforms received for the tenant ID " + tenantId + " at "
+ "PlatformManager level"); + "PlatformManager level");
} }
List<Platform> platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantId); try {
int platformIndex = 0; ConnectionManagerUtil.openConnection();
platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantId);
} catch (DBConnectionException e) {
throw new PlatformManagementDAOException(
"Database Connection Exception while getting the platforms for the tenant : " + tenantId, e);
} finally {
ConnectionManagerUtil.closeConnection();
}
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 "
+ tenantId); + tenantId);
@ -90,8 +118,7 @@ public class PlatformManagerImpl implements PlatformManager {
platformIndex++; platformIndex++;
} }
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Number of effective platforms for the tenant " + tenantId log.debug("Number of effective platforms for the tenant " + tenantId + " : " + platforms.size());
+ " : " + platforms.size());
} }
return platforms; return platforms;
} }
@ -100,15 +127,24 @@ public class PlatformManagerImpl implements PlatformManager {
public Platform getPlatform(int tenantId, String identifier) throws PlatformManagementException { public Platform getPlatform(int tenantId, String identifier) throws PlatformManagementException {
Platform platform = getPlatformFromInMemory(tenantId, identifier); Platform platform = getPlatformFromInMemory(tenantId, identifier);
if (platform == null) { if (platform == null) {
try {
ConnectionManagerUtil.openConnection();
platform = DAOFactory.getPlatformDAO().getPlatform(tenantId, identifier); platform = DAOFactory.getPlatformDAO().getPlatform(tenantId, identifier);
if (platform != null) { if (platform != null) {
return platform; return platform;
} }
} catch (DBConnectionException e) {
throw new PlatformManagementDAOException(
"Database Connection Exception while trying to get the " + "platform with the id :" + identifier
+ " for the tenant : " + tenantId, e);
} finally {
ConnectionManagerUtil.closeConnection();
}
} else { } else {
return new Platform(platform); return new Platform(platform);
} }
throw new PlatformManagementException("No platform was found for tenant - " + tenantId + throw new PlatformManagementException(
" with Platform identifier - " + identifier); "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) {
@ -138,6 +174,8 @@ public class PlatformManagerImpl implements PlatformManager {
"Platform sharing is a restricted operation, therefore Platform - " + platform.getIdentifier() "Platform sharing is a restricted operation, therefore Platform - " + platform.getIdentifier()
+ " cannot be shared by the tenant domain - " + tenantId); + " cannot be shared by the tenant domain - " + tenantId);
} }
try {
ConnectionManagerUtil.beginTransaction();
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);
@ -149,6 +187,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();
throw new PlatformManagementException( throw new PlatformManagementException(
"Platform - " + platform.getIdentifier() + " is already registered!"); "Platform - " + platform.getIdentifier() + " is already registered!");
} }
@ -159,25 +198,64 @@ public class PlatformManagerImpl implements PlatformManager {
TenantManager tenantManager = DataHolder.getInstance().getRealmService().getTenantManager(); TenantManager tenantManager = DataHolder.getInstance().getRealmService().getTenantManager();
Tenant[] tenants = tenantManager.getAllTenants(); Tenant[] tenants = tenantManager.getAllTenants();
for (Tenant tenant : tenants) { for (Tenant tenant : tenants) {
addMapping(tenant.getId(), platform.getIdentifier()); DAOFactory.getPlatformDAO()
.addMapping(tenant.getId(), getListOfString(platform.getIdentifier()));
} }
} }
addMapping(tenantId, platform.getIdentifier()); DAOFactory.getPlatformDAO().addMapping(tenantId, getListOfString(platform.getIdentifier()));
} catch (UserStoreException e) { } catch (UserStoreException e) {
throw new PlatformManagementException("Error occured while assigning the platforms for tenants!", e); ConnectionManagerUtil.rollbackTransaction();
} throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!",
e);
}
}
ConnectionManagerUtil.commitTransaction();
} catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException(
"Transaction Management Exception while trying to register a " + "platform with id " + platform
.getIdentifier() + " for tenant " + tenantId);
} catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException(
"Database Connection Exception while trying to register a " + "platform with id " + platform
.getIdentifier() + " for tenant " + tenantId);
} finally {
ConnectionManagerUtil.closeConnection();
} }
} }
@Override @Override
public void update(int tenantId, String oldPlatformIdentifier, Platform platform) public void update(int tenantId, String oldPlatformIdentifier, Platform platform) throws
throws PlatformManagementException { PlatformManagementException {
if (platform.isShared() && tenantId != MultitenantConstants.SUPER_TENANT_ID) { if (platform.isShared() && tenantId != MultitenantConstants.SUPER_TENANT_ID) {
throw new PlatformManagementException( throw new PlatformManagementException(
"Platform sharing is a restricted operation, therefore Platform - " + platform.getIdentifier() "Platform sharing is a restricted operation, therefore Platform - " + platform.getIdentifier()
+ " cannot be shared by the tenant domain - " + tenantId); + " cannot be shared by the tenant domain - " + tenantId);
} }
Platform oldPlatform; 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 {
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) { if (tenantPlatforms == null) {
@ -191,11 +269,13 @@ public class PlatformManagerImpl implements PlatformManager {
"No platforms registered for the tenant - " + tenantId + " with platform identifier - " "No platforms registered for the tenant - " + tenantId + " with platform identifier - "
+ platform.getIdentifier()); + platform.getIdentifier());
} else { } else {
ConnectionManagerUtil.beginTransaction();
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); oldPlatform = DAOFactory.getPlatformDAO().getPlatform(tenantId, oldPlatformIdentifier);
DAOFactory.getPlatformDAO().update(tenantId, oldPlatformIdentifier, platform); DAOFactory.getPlatformDAO().update(tenantId, oldPlatformIdentifier, platform);
} }
@ -205,45 +285,101 @@ public class PlatformManagerImpl implements PlatformManager {
TenantManager tenantManager = DataHolder.getInstance().getRealmService().getTenantManager(); TenantManager tenantManager = DataHolder.getInstance().getRealmService().getTenantManager();
Tenant[] tenants = tenantManager.getAllTenants(); Tenant[] tenants = tenantManager.getAllTenants();
for (Tenant tenant : tenants) { for (Tenant tenant : tenants) {
addMapping(tenant.getId(), platform.getIdentifier()); DAOFactory.getPlatformDAO()
.addMapping(tenant.getId(), getListOfString(platform.getIdentifier()));
} }
} }
addMapping(tenantId, platform.getIdentifier()); DAOFactory.getPlatformDAO().addMapping(tenantId, getListOfString(platform.getIdentifier()));
} catch (UserStoreException e) { } catch (UserStoreException e) {
throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!", e); throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!",
e);
} }
} }
if (!platform.isShared() && oldPlatform.isShared()) { if (!platform.isShared() && oldPlatform.isShared()) {
DAOFactory.getPlatformDAO().removeMappingTenants(platform.getIdentifier()); DAOFactory.getPlatformDAO().removeMappingTenants(platform.getIdentifier());
} }
ConnectionManagerUtil.commitTransaction();
} catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException(
"Transaction Management Exception while trying to update " + "platform : " + oldPlatformIdentifier
+ " of tenant :" + tenantId);
} catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException(
"Database Connection Exception while trying to update " + "platform : " + oldPlatformIdentifier
+ " of tenant :" + tenantId);
} finally {
ConnectionManagerUtil.closeConnection();
}
}
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 public void unregister(int tenantId, String identifier, boolean isFileBased) throws PlatformManagementException {
PlatformManagementException { try {
ConnectionManagerUtil.beginTransaction();
DAOFactory.getPlatformDAO().unregister(tenantId, identifier, isFileBased);
if (isFileBased) { if (isFileBased) {
Map<String, Platform> tenantPlatforms = this.inMemoryStore.get(tenantId); Map<String, Platform> tenantPlatforms = this.inMemoryStore.get(tenantId);
if (tenantPlatforms != null) { if (tenantPlatforms != null) {
this.inMemoryStore.remove(identifier); tenantPlatforms.remove(identifier);
}
} }
ConnectionManagerUtil.commitTransaction();
} catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException(
"Transaction Management Exception while trying to un-register " + "the platform with identifier : "
+ identifier + " tenant :" + tenantId, e);
} catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException(
"Database Connection Exception while trying to un-register " + "the platform with identifier : "
+ identifier + " tenant :" + tenantId, e);
} finally {
ConnectionManagerUtil.closeConnection();
} }
DAOFactory.getPlatformDAO().unregister(tenantId, identifier);
} }
@Override @Override
public void addMapping(int tenantId, List<String> platformIdentifiers) throws PlatformManagementException { public void addMapping(int tenantId, List<String> platformIdentifiers) throws PlatformManagementException {
try {
ConnectionManagerUtil.openConnection();
DAOFactory.getPlatformDAO().addMapping(tenantId, platformIdentifiers); DAOFactory.getPlatformDAO().addMapping(tenantId, platformIdentifiers);
} catch (DBConnectionException e) {
throw new PlatformManagementDAOException(
"Database Connection Exception while trying to add tenant " + "mapping for tenant ID : "
+ tenantId);
} finally {
ConnectionManagerUtil.closeConnection();
}
} }
@Override @Override
public void addMapping(int tenantId, String platformIdentifier) throws PlatformManagementException { public void addMapping(int tenantId, String platformIdentifier) throws PlatformManagementException {
List<String> identifiers = new ArrayList<>(); List<String> identifiers = new ArrayList<>();
identifiers.add(platformIdentifier); identifiers.add(platformIdentifier);
DAOFactory.getPlatformDAO().addMapping(tenantId, identifiers); addMapping(tenantId, identifiers);
} }
@Override @Override
public void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementException { public void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementException {
try {
ConnectionManagerUtil.openConnection();
DAOFactory.getPlatformDAO().removeMapping(tenantId, platformIdentifier); DAOFactory.getPlatformDAO().removeMapping(tenantId, platformIdentifier);
} catch (DBConnectionException e) {
throw new PlatformManagementDAOException(
"Database Connection Exception while trying to remove tenant mapping for tenant ID : " + tenantId);
} finally {
ConnectionManagerUtil.closeConnection();
}
} }
} }

@ -20,11 +20,12 @@ package org.wso2.carbon.device.application.mgt.core.util;
import java.io.File; import java.io.File;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
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.
*/ */

@ -27,8 +27,6 @@ import org.wso2.carbon.device.application.mgt.common.exception.TransactionManage
import javax.naming.InitialContext; import javax.naming.InitialContext;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
public class ConnectionManagerUtil { public class ConnectionManagerUtil {
@ -187,27 +185,4 @@ public class ConnectionManagerUtil {
return null; return null;
} }
/**
* Cleanup resources used to transaction
*
* @param stmt Prepared statement used
* @param rs Obtained results set
*/
public static void cleanupResources(PreparedStatement stmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
log.warn("Error occurred while closing result set", e);
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
log.warn("Error occurred while closing prepared statement", e);
}
}
}
} }

@ -19,13 +19,12 @@ PRIMARY KEY (IDENTIFIER, TENANT_ID)
CREATE TABLE IF NOT EXISTS APPM_PLATFORM_PROPERTIES ( CREATE TABLE IF NOT EXISTS APPM_PLATFORM_PROPERTIES (
ID INT NOT NULL AUTO_INCREMENT, ID INT NOT NULL AUTO_INCREMENT,
PLATFORM_ID VARCHAR (100) NOT NULL, PLATFORM_ID INT NOT NULL,
PROP_NAME VARCHAR (100) NOT NULL, PROP_NAME VARCHAR (100) NOT NULL,
OPTIONAL BOOLEAN, OPTIONAL BOOLEAN,
DEFAUL_VALUE VARCHAR (255), DEFAUL_VALUE VARCHAR (255),
TENANT_ID INT NOT NULL , FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(ID) ON DELETE CASCADE,
FOREIGN KEY(PLATFORM_ID, TENANT_ID) REFERENCES APPM_PLATFORM(IDENTIFIER, TENANT_ID) ON DELETE CASCADE, PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME)
PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME, TENANT_ID)
); );
CREATE TABLE IF NOT EXISTS APPM_PLATFORM_TENANT_MAPPING ( CREATE TABLE IF NOT EXISTS APPM_PLATFORM_TENANT_MAPPING (

@ -20,21 +20,20 @@ IDENTIFIER VARCHAR (100) NOT NULL,
TENANT_ID INT NOT NULL , TENANT_ID INT NOT NULL ,
NAME VARCHAR (255), NAME VARCHAR (255),
FILE_BASED BOOLEAN, FILE_BASED BOOLEAN,
DESCRIPTION VARCHAR(2048), DESCRIPTION VARCHAR (2048),
IS_SHARED BOOLEAN, IS_SHARED BOOLEAN,
ICON_NAME VARCHAR (100), ICON_NAME VARCHAR (100),
PRIMARY KEY (IDENTIFIER, TENANT_ID) PRIMARY KEY (IDENTIFIER, TENANT_ID)
); );
CREATE TABLE IF NOT EXISTS APPM_PLATFORM_PROPERTIES ( CREATE TABLE IF NOT EXISTS APPM_PLATFORM_PROPERTIES (
ID INT NOT NULL AUTO_INCREMENT UNIQUE, ID INT NOT NULL AUTO_INCREMENT,
PLATFORM_ID VARCHAR (100) NOT NULL, PLATFORM_ID INT NOT NULL,
TENANT_ID INT NOT NULL ,
PROP_NAME VARCHAR (100) NOT NULL, PROP_NAME VARCHAR (100) NOT NULL,
OPTIONAL BOOLEAN, OPTIONAL BOOLEAN,
DEFAUL_VALUE VARCHAR (255), DEFAUL_VALUE VARCHAR (255),
PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME, TENANT_ID), FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(ID) ON DELETE CASCADE,
FOREIGN KEY(PLATFORM_ID, TENANT_ID) REFERENCES APPM_PLATFORM(IDENTIFIER, TENANT_ID) ON DELETE CASCADE PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME)
); );

Loading…
Cancel
Save