Adding the update platform API.

feature/appm-store/pbac
sinthuja 8 years ago
parent 52b2739bee
commit 59f43886cf

@ -76,7 +76,7 @@ public interface PlatformManagementAPI {
);
@GET
@Path("/{code}")
@Path("/{identifier}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(
@ -105,11 +105,11 @@ public interface PlatformManagementAPI {
})
Response getPlatform(
@ApiParam(
name = "code",
name = "identifier",
required = true)
@PathParam("code")
@PathParam("identifier")
@Size(max = 45)
String code
String identifier
);
@POST
@ -149,4 +149,48 @@ public interface PlatformManagementAPI {
Platform platform
);
@PUT
@Path("/{identifier}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(
consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON,
httpMethod = "PUT",
value = "Update Platform",
notes = "This will update the platform configuration for the tenant space",
tags = "Platform Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = SCOPE, value = "perm:update-platform")
})
}
)
@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 updatePlatform(
@ApiParam(
name = "platform",
value = "The payload of the platform",
required = true)
Platform platform,
@ApiParam(
name = "identifier",
required = true)
@PathParam("identifier")
@Size(max = 45)
String identifier
);
}

@ -17,7 +17,6 @@
*/
package org.wso2.carbon.device.application.mgt.api.services.impl;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
@ -28,10 +27,7 @@ import org.wso2.carbon.device.application.mgt.common.exception.PlatformManagemen
import org.wso2.carbon.device.application.mgt.core.exception.PlatformManagementDAOException;
import javax.validation.constraints.Size;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;
@ -44,7 +40,6 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI {
private static Log log = LogFactory.getLog(PlatformManagementAPIImpl.class);
@Override
public Response getPlatforms(@QueryParam("status") String status) {
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true);
@ -83,11 +78,11 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI {
@GET
@Override
@Path("/{code}")
public Response getPlatform(@PathParam("code") String code) {
@Path("/{identifier}")
public Response getPlatform(@PathParam("identifier") String id) {
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true);
try {
Platform platform = APIUtil.getPlatformManager().getPlatform(tenantDomain, code);
Platform platform = APIUtil.getPlatformManager().getPlatform(tenantDomain, id);
return Response.status(Response.Status.OK).entity(platform).build();
} catch (PlatformManagementDAOException e) {
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
@ -105,7 +100,7 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI {
APIUtil.getPlatformManager().register(tenantDomain, platform);
return Response.status(Response.Status.CREATED).build();
} else {
return APIUtil.getResponse("Invalid payload! Platform code and names are mandatory fields!", Response.Status.BAD_REQUEST);
return APIUtil.getResponse("Invalid payload! Platform ID and names are mandatory fields!", Response.Status.BAD_REQUEST);
}
} else {
return APIUtil.getResponse("Invalid payload! Platform needs to be passed as payload!", Response.Status.BAD_REQUEST);
@ -114,4 +109,18 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI {
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
}
}
@PUT
@Path("/{identifier}")
@Override
public Response updatePlatform(Platform platform, @PathParam("identifier") @Size(max = 45) String id) {
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true);
try {
APIUtil.getPlatformManager().update(tenantDomain, id, platform);
return Response.status(Response.Status.OK).build();
} catch (PlatformManagementException e) {
log.error("Error while updating the platform - " + id + " for tenant domain - " + tenantDomain, e);
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
}
}
}

@ -23,15 +23,24 @@ import org.wso2.carbon.device.application.mgt.common.jaxrs.Exclude;
import java.util.ArrayList;
import java.util.List;
public class Platform implements Cloneable {
public class Platform {
/**
* Unique id reference that is used in the database.
*/
@Exclude
private int id;
/**
* The name of the platform. It can contain spaces,etc.
*/
private String name;
private String description;
/**
* Unique human readable identifier used for the platform.
*/
private String identifier;
private String iconName;

@ -33,16 +33,18 @@ public interface PlatformManager {
List<Platform> getPlatforms(String tenantDomain) throws PlatformManagementException;
Platform getPlatform(String tenantDomain, String code) throws PlatformManagementException;
Platform getPlatform(String tenantDomain, String platformIdentifier) throws PlatformManagementException;
void register(String tenantDomain, Platform platform) throws PlatformManagementException;
void unregister(String tenantDomain, String platformCode, boolean isFileBased) throws PlatformManagementException;
void update(String tenantDomain, String oldPlatformIdentifier, Platform platform) throws PlatformManagementException;
void addMapping(String tenantDomain, List<String> platformCode) throws PlatformManagementException;
void unregister(String tenantDomain, String platformIdentifier, boolean isFileBased) throws PlatformManagementException;
void addMapping(String tenantDomain, String platformCode) throws PlatformManagementException;
void addMapping(String tenantDomain, List<String> platformIdentifiers) throws PlatformManagementException;
void removeMapping(String tenantDomain, String platformCode) throws PlatformManagementException;
void addMapping(String tenantDomain, String platformIdentifier) throws PlatformManagementException;
void removeMapping(String tenantDomain, String platformIdentifier) throws PlatformManagementException;
}

@ -26,18 +26,20 @@ import java.util.List;
public interface PlatformDAO {
public Platform getPlatformByIdentifier(String identifier) throws ApplicationManagementDAOException;
int register(String tenantDomain, Platform platform) throws PlatformManagementDAOException;
void register(String tenantDomain, Platform platform) throws PlatformManagementDAOException;
void update(String tenantDomain, String oldPlatformIdentifier, Platform platform) throws PlatformManagementDAOException;
void unregister(String tenantDomain, String platformCode) throws PlatformManagementDAOException;
void unregister(String tenantDomain, String platformIdentifier) throws PlatformManagementDAOException;
void addMapping(String tenantDomain, List<String> platformCode) throws PlatformManagementDAOException;
void addMapping(String tenantDomain, List<String> platformIdentifiers) throws PlatformManagementDAOException;
void removeMapping(String tenantDomain, String platformCode) throws PlatformManagementDAOException;
void removeMapping(String tenantDomain, String platformIdentifier) throws PlatformManagementDAOException;
void removeMappingTenants(String platformIdentifier) throws PlatformManagementDAOException;
List<Platform> getPlatforms(String tenantDomain) throws PlatformManagementDAOException;
Platform getPlatform(String tenantDomain, String platformCode) throws PlatformManagementDAOException;
Platform getPlatform(String tenantDomain, String platformIdentifier) throws PlatformManagementDAOException;
}

@ -22,11 +22,10 @@ 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.TransactionManagementException;
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.exception.ApplicationManagementDAOException;
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 java.sql.Connection;
import java.sql.PreparedStatement;
@ -38,109 +37,150 @@ import java.util.List;
public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformDAO {
@Override
public Platform getPlatformByIdentifier(String identifier) throws ApplicationManagementDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
String sql = "";
try {
conn = this.getConnection();
sql += "SELECT * ";
sql += "FROM APPM_PLATFORM ";
sql += "WHERE IDENTIFIER = ? ";
stmt = conn.prepareStatement(sql);
stmt.setString(1, identifier);
rs = stmt.executeQuery();
Platform platform = null;
if (rs.next()) {
platform = new Platform();
platform.setId(rs.getInt("ID"));
platform.setName(rs.getString("NAME"));
platform.setIdentifier(rs.getString("IDENTIFIER"));
platform.setPublished(rs.getBoolean("PUBLISHED"));
}
return platform;
} catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while getting application List", e);
} catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
} finally {
Util.cleanupResources(stmt, rs);
}
}
@Override
public void register(String tenantDomain, Platform platform) throws PlatformManagementDAOException {
public int register(String tenantDomain, Platform platform) throws PlatformManagementDAOException {
try {
ConnectionManagerUtil.beginTransaction();
if (getPlatformId(tenantDomain, platform.getIdentifier()) == -1) {
int platformId = getPlatformId(tenantDomain, platform.getIdentifier());
if (platformId == -1) {
Connection connection = ConnectionManagerUtil.getConnection();
String insertToPlatform = "INSERT INTO APPM_PLATFORM (CODE, TENANT_DOMAIN, NAME, DESCRIPTION, IS_SHARED, ICON_NAME)" +
" VALUES (?, ?, ?, ?, ?, ?)";
if (!platform.isFileBased()) {
String insertToPlatform = "INSERT INTO APPM_PLATFORM (IDENTIFIER, TENANT_DOMAIN, NAME, FILE_BASED, DESCRIPTION, IS_SHARED, ICON_NAME)" +
" VALUES (?, ?, ?, ?, ?, ?, ?)";
PreparedStatement preparedStatement = connection.prepareStatement(insertToPlatform);
preparedStatement.setString(1, platform.getIdentifier());
preparedStatement.setString(2, tenantDomain);
preparedStatement.setString(3, platform.getName());
preparedStatement.setString(4, platform.getDescription());
preparedStatement.setBoolean(5, platform.isShared());
preparedStatement.setString(6, platform.getIconName());
preparedStatement.setBoolean(4, false);
preparedStatement.setString(5, platform.getDescription());
preparedStatement.setBoolean(6, platform.isShared());
preparedStatement.setString(7, platform.getIconName());
preparedStatement.execute();
int platformID = getPlatformId(tenantDomain, platform.getIdentifier());
platformId = getPlatformId(tenantDomain, platform.getIdentifier());
String insertPlatformProps = "INSERT INTO APPM_PLATFORM_PROPERTIES (PLATFORM_ID, PROP_NAME, OPTIONAL, DEFAULT_VALUE) VALUES " +
"( ? , ?, ? , ?)";
for (Platform.Property property : platform.getProperties()) {
preparedStatement = connection.prepareStatement(insertPlatformProps);
preparedStatement.setInt(1, platformID);
preparedStatement.setInt(1, platformId);
preparedStatement.setString(2, property.getName());
preparedStatement.setBoolean(3, property.isOptional());
preparedStatement.setString(4, property.getDefaultValue());
preparedStatement.execute();
}
} else {
String insertToPlatform = "INSERT INTO APPM_PLATFORM (IDENTIFIER, TENANT_DOMAIN, FILE_BASED)" +
" VALUES (?, ?, ?)";
PreparedStatement preparedStatement = connection.prepareStatement(insertToPlatform);
preparedStatement.setString(1, platform.getIdentifier());
preparedStatement.setString(2, tenantDomain);
preparedStatement.setBoolean(3, true);
preparedStatement.execute();
}
if (platformId == -1) {
platformId = getPlatformId(tenantDomain, platform.getIdentifier());
}
ConnectionManagerUtil.commitTransaction();
return platformId;
} else {
if (!platform.isFileBased()) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException("Platform - " + platform.getIdentifier()
+ " is already registered for tenant - " + tenantDomain);
} else {
return platformId;
}
} catch (TransactionManagementException e) {
}
} catch (SQLException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException("Unable to start the transaction while trying to register the platform - "
+ platform.getIdentifier() + " for tenant - " + tenantDomain, e);
throw new PlatformManagementDAOException("Error while executing the SQL query. ", e);
} catch (PlatformManagementDAOException ex) {
ConnectionManagerUtil.rollbackTransaction();
throw ex;
} catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException("Unable to obtain the connection while trying to register the platform - "
+ platform.getIdentifier() + " for tenant - " + tenantDomain, 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 - " + tenantDomain);
} finally {
ConnectionManagerUtil.closeConnection();
}
}
@Override
public void update(String tenantDomain, String oldPlatformIdentifier, Platform platform) throws PlatformManagementDAOException {
try {
ConnectionManagerUtil.beginTransaction();
int platformId = getPlatformId(tenantDomain, oldPlatformIdentifier);
if (platformId != -1) {
Connection connection = ConnectionManagerUtil.getConnection();
if (!platform.isFileBased()) {
String insertToPlatform = "UPDATE APPM_PLATFORM SET IDENTIFIER = ?, NAME =?, DESCRIPTION=?, " +
"IS_SHARED=?, ICON_NAME=? WHERE ID = ?";
PreparedStatement preparedStatement = connection.prepareStatement(insertToPlatform);
preparedStatement.setString(1, platform.getIdentifier());
preparedStatement.setString(2, platform.getName());
preparedStatement.setString(3, platform.getDescription());
preparedStatement.setBoolean(4, platform.isShared());
preparedStatement.setString(5, platform.getIconName());
preparedStatement.execute();
platformId = getPlatformId(tenantDomain, platform.getIdentifier());
String deletePlatformProps = "DELETE FROM APPM_PLATFORM_PROPERTIES WHERE PLATFORM_ID=?";
preparedStatement = connection.prepareStatement(deletePlatformProps);
preparedStatement.setInt(1, platformId);
preparedStatement.execute();
String insertPlatformProps = "INSERT INTO APPM_PLATFORM_PROPERTIES (PLATFORM_ID, PROP_NAME, OPTIONAL," +
" DEFAULT_VALUE) VALUES ( ? , ?, ? , ?)";
for (Platform.Property property : platform.getProperties()) {
preparedStatement = connection.prepareStatement(insertPlatformProps);
preparedStatement.setInt(1, platformId);
preparedStatement.setString(2, property.getName());
preparedStatement.setBoolean(3, property.isOptional());
preparedStatement.setString(4, property.getDefaultValue());
preparedStatement.execute();
}
} else {
String insertToPlatform = "UPDATE APPM_PLATFORM SET IDENTIFIER = ? WHERE ID = ?";
PreparedStatement preparedStatement = connection.prepareStatement(insertToPlatform);
preparedStatement.setInt(1, platformId);
preparedStatement.execute();
}
ConnectionManagerUtil.commitTransaction();
} else {
throw new PlatformManagementDAOException("Cannot find any platform that was registered with identifier - "
+ platform.getIdentifier() + " for tenant - " + tenantDomain);
}
} catch (SQLException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException("Error while executing the SQL query. ", e);
} catch (PlatformManagementDAOException ex) {
ConnectionManagerUtil.rollbackTransaction();
throw ex;
} catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException("Unable to obtain the connection while trying to register the platform - "
+ platform.getIdentifier() + " for tenant - " + tenantDomain, 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 - " + tenantDomain);
} finally {
ConnectionManagerUtil.closeConnection();
}
}
private int getPlatformId(String tenantDomain, String platformCode) throws PlatformManagementDAOException {
String query = "SELECT ID FROM APPM_PLATFORM WHERE (TENANT_DOMAIN=? AND CODE=?) OR (IS_SHARED = TRUE AND CODE=?)";
private int getPlatformId(String tenantDomain, String platformIdentifier) throws PlatformManagementDAOException {
String query = "SELECT ID FROM APPM_PLATFORM WHERE (TENANT_DOMAIN=? AND IDENTIFIER=?) OR (IS_SHARED = TRUE AND IDENTIFIER=?)";
try {
Connection connection = ConnectionManagerUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, tenantDomain);
preparedStatement.setString(2, platformCode);
preparedStatement.setString(3, platformCode);
preparedStatement.setString(2, platformIdentifier);
preparedStatement.setString(3, platformIdentifier);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
return resultSet.getInt("ID");
@ -155,31 +195,29 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
@Override
public void unregister(String tenantDomain, String platformCode) throws PlatformManagementDAOException {
public void unregister(String tenantDomain, String platformIdenfier) throws PlatformManagementDAOException {
try {
ConnectionManagerUtil.beginTransaction();
int platformId = getPlatformId(tenantDomain, platformCode);
int platformId = getPlatformId(tenantDomain, platformIdenfier);
if (platformId != -1) {
Connection connection = ConnectionManagerUtil.getConnection();
String deletePlatform = "DELETE FROM APPM_PLATFORM WHERE ID = ?";
PreparedStatement preparedStatement = connection.prepareStatement(deletePlatform);
preparedStatement.setInt(1, platformId);
preparedStatement.execute();
ConnectionManagerUtil.commitTransaction();
} else {
throw new PlatformManagementDAOException("Platform - " + platformCode
throw new PlatformManagementDAOException("Platform identifier - " + platformIdenfier
+ " is already unregistered registered for tenant - " + tenantDomain);
}
} catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException("Unable to start the transaction while trying to register the platform - "
+ platformCode + " for tenant - " + tenantDomain, e);
+ platformIdenfier + " for tenant - " + tenantDomain, e);
} catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException("Unable to obtain the connection while trying to register the platform - "
+ platformCode + " for tenant - " + tenantDomain, e);
+ platformIdenfier + " for tenant - " + tenantDomain, e);
} catch (SQLException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException("Error while executing the SQL query. ", e);
@ -191,26 +229,27 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
}
}
public void addMapping(String tenantDomain, List<String> platformCodes) throws PlatformManagementDAOException {
String insertMapping = "INSERT INTO APPM_PLATFORM_TENANT_MAPPING(TENANT_DOMAIN, PLATFORM_CODE) VALUES (?, ?)";
public void addMapping(String tenantDomain, List<String> platformIdentifiers) throws PlatformManagementDAOException {
String insertMapping = "INSERT INTO APPM_PLATFORM_TENANT_MAPPING(TENANT_DOMAIN, PLATFORM_ID) VALUES (?, ?)";
try {
ConnectionManagerUtil.beginTransaction();
for (String platformCode : platformCodes) {
if (getTenantPlatformMapping(tenantDomain, platformCode) != -1) {
for (String platformIdentifier : platformIdentifiers) {
if (getTenantPlatformMapping(tenantDomain, platformIdentifier) != -1) {
int platformId = getPlatformId(tenantDomain, platformIdentifier);
Connection connection = ConnectionManagerUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(insertMapping);
preparedStatement.setString(1, tenantDomain);
preparedStatement.setString(2, platformCode);
preparedStatement.setInt(2, platformId);
preparedStatement.execute();
} else {
throw new PlatformManagementDAOException("Platform - " + platformCode + " is already assigned to tenant domain - " + tenantDomain);
throw new PlatformManagementDAOException("Platform identifier - " + platformIdentifier + " is already assigned to tenant domain - " + tenantDomain);
}
}
ConnectionManagerUtil.commitTransaction();
} catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException("Error occured while trying to add the mapping of platform - "
+ platformCodes.toString() + " for tenant - " + tenantDomain, e);
+ platformIdentifiers.toString() + " for tenant - " + tenantDomain, e);
} catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException("Error occurred when getting the connection for the database. ", e);
@ -225,13 +264,14 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
}
}
private int getTenantPlatformMapping(String tenantDomain, String platformCode) throws PlatformManagementDAOException {
String getMapping = "SELECT ID FROM APPM_PLATFORM_TENANT_MAPPING WHERE TENANT_DOMAIN=? AND PLATFORM_CODE=?";
private int getTenantPlatformMapping(String tenantDomain, String platformIdentifier) throws PlatformManagementDAOException {
String getMapping = "SELECT MAPPING.ID as ID FROM (SELECT ID FROM APPM_PLATFORM_TENANT_MAPPING WHERE TENANT_DOMAIN=?) MAPPING JOIN " +
"(SELECT ID FROM APPM_PLATFORM WHERE APPM_PLATFORM.IDENTIFIER=?) PLATFORM ON MAPPING.PLATFORM_ID=PLATFORM.ID";
try {
Connection connection = ConnectionManagerUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(getMapping);
preparedStatement.setString(1, tenantDomain);
preparedStatement.setString(2, platformCode);
preparedStatement.setString(2, platformIdentifier);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
return resultSet.getInt("ID");
@ -245,30 +285,28 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
}
}
public void removeMapping(String tenantDomain, String platformCode) throws PlatformManagementDAOException {
public void removeMapping(String tenantDomain, String platformIdentifier) throws PlatformManagementDAOException {
String deleteMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE ID = ?";
try {
ConnectionManagerUtil.beginTransaction();
int mappingId = getTenantPlatformMapping(tenantDomain, platformCode);
int mappingId = getTenantPlatformMapping(tenantDomain, platformIdentifier);
if (mappingId != -1) {
Connection connection = ConnectionManagerUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(deleteMapping);
preparedStatement.setInt(1, mappingId);
preparedStatement.execute();
ConnectionManagerUtil.commitTransaction();
} else {
throw new PlatformManagementDAOException("Platform - " + platformCode
throw new PlatformManagementDAOException("Platform - " + platformIdentifier
+ " is already unassigned for tenant - " + tenantDomain);
}
} catch (TransactionManagementException | DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException("Error occurred while unassigning the platform - " + platformCode
throw new PlatformManagementDAOException("Error occurred while unassigning the platform - " + platformIdentifier
+ " for tenant - " + tenantDomain);
} catch (SQLException e) {
ConnectionManagerUtil.rollbackTransaction();
throw new PlatformManagementDAOException("Error occured while executing the query - " + deleteMapping);
throw new PlatformManagementDAOException("Error occurred while executing the query - " + deleteMapping);
} catch (PlatformManagementDAOException ex) {
ConnectionManagerUtil.rollbackTransaction();
throw ex;
@ -277,19 +315,42 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
}
}
@Override
public void removeMappingTenants(String platformIdentifier) throws PlatformManagementDAOException {
int platformId = getPlatformId(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, platformIdentifier);
String getMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE TENANT_DOMAIN != ? AND PLATFORM_ID=?";
try {
ConnectionManagerUtil.openConnection();
Connection connection = ConnectionManagerUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(getMapping);
preparedStatement.setString(1, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
preparedStatement.setInt(2, platformId);
preparedStatement.execute();
} catch (DBConnectionException e) {
throw new PlatformManagementDAOException("Error occured while obtaining the connection to get the existing " +
"Tenant - Platform Mapping.", e);
} catch (SQLException e) {
throw new PlatformManagementDAOException("Error occured while executing the SQL query - " + getMapping, e);
} finally {
ConnectionManagerUtil.closeConnection();
}
}
@Override
public List<Platform> getPlatforms(String tenantDomain) throws PlatformManagementDAOException {
String selectQuery = "SELECT * FROM (SELECT * FROM APPM_PLATFORM WHERE TENANT_DOMAIN=? OR IS_SHARED = TRUE) PLATFORM " +
"LEFT JOIN APPM_PLATFORM_TENANT_MAPPING MAPPING ON PLATFORM.CODE = MAPPING.PLATFORM_CODE";
String selectQuery = "SELECT MAPPING.ID, PLATFORM.IDENTIFIER FROM (SELECT * FROM APPM_PLATFORM WHERE TENANT_DOMAIN=? OR IS_SHARED = TRUE AND FILE_BASED = FALSE) PLATFORM " +
"LEFT JOIN APPM_PLATFORM_TENANT_MAPPING MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID";
try {
Connection connection = ConnectionManagerUtil.openConnection();
PreparedStatement preparedStatement = connection.prepareStatement(selectQuery);
preparedStatement.setString(1, tenantDomain);
ResultSet resultSet = preparedStatement.executeQuery();
List<Platform> platforms = new ArrayList<>();
while (resultSet.next()) {
String platformCode = resultSet.getString("PLATFORM.CODE");
String identifier = resultSet.getString("PLATFORM.IDENTIFIER");
int mappingID = resultSet.getInt("MAPPING.ID");
Platform platform = getPlatform(tenantDomain, platformCode);
Platform platform = getPlatform(tenantDomain, identifier);
if (mappingID != 0) {
platform.setEnabled(true);
} else {
@ -307,20 +368,22 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
}
}
public Platform getPlatform(String tenantDomain, String platformCode) throws PlatformManagementDAOException {
String platformQuery = "SELECT * FROM (SELECT * FROM APPM_PLATFORM WHERE (TENANT_DOMAIN=? AND CODE=?) OR (IS_SHARED = TRUE AND CODE=?)) PLATFORM " +
public Platform getPlatform(String tenantDomain, String platformIdenfier) throws PlatformManagementDAOException {
String platformQuery = "SELECT * FROM (SELECT * FROM APPM_PLATFORM WHERE (TENANT_DOMAIN=? AND IDENTIFIER=?) " +
"OR (IS_SHARED = TRUE AND IDENTIFIER=?) AND FILE_BASED = FALSE ) PLATFORM " +
"LEFT JOIN APPM_PLATFORM_PROPERTIES PROPS ON PLATFORM.ID = PROPS.PLATFORM_ID";
try {
ConnectionManagerUtil.openConnection();
Connection connection = ConnectionManagerUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(platformQuery);
preparedStatement.setString(1, tenantDomain);
preparedStatement.setString(2, platformCode);
preparedStatement.setString(3, platformCode);
preparedStatement.setString(2, platformIdenfier);
preparedStatement.setString(3, platformIdenfier);
ResultSet resultSet = preparedStatement.executeQuery();
Platform platform = new Platform();
if (resultSet.next()) {
platform.setId(resultSet.getInt("PLATFORM.ID"));
platform.setIdentifier(platformCode);
platform.setIdentifier(platformIdenfier);
platform.setName(resultSet.getString("PLATFORM.NAME"));
platform.setIconName(resultSet.getString("PLATFORM.DESCRIPTION"));
platform.setIconName(resultSet.getString("PLATFORM.ICON_NAME"));
@ -338,12 +401,12 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
} while (resultSet.next());
platform.setProperties(properties);
} else {
platform.setIdentifier(platformCode);
platform.setIdentifier(platformIdenfier);
platform.setFileBased(true);
}
return platform;
} catch (DBConnectionException e) {
throw new PlatformManagementDAOException("Error when loading the platform - " + platformCode, e);
throw new PlatformManagementDAOException("Error when loading the platform - " + platformIdenfier, e);
} catch (SQLException e) {
throw new PlatformManagementDAOException("Error in executing the query - " + platformQuery, e);
}

@ -42,13 +42,13 @@ public class PlatformManagerImpl implements PlatformManager {
@Override
public void initialize(String tenantDomain) throws PlatformManagementException {
List<Platform> platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantDomain);
List<String> platformCodes = new ArrayList<>();
List<String> platformIdentifiers = new ArrayList<>();
for (Platform platform : platforms) {
if (!platform.isEnabled() & platform.isDefaultTenantMapping()) {
platformCodes.add(platform.getIdentifier());
platformIdentifiers.add(platform.getIdentifier());
}
}
addMapping(tenantDomain, platformCodes);
addMapping(tenantDomain, platformIdentifiers);
}
@Override
@ -71,21 +71,24 @@ public class PlatformManagerImpl implements PlatformManager {
}
@Override
public Platform getPlatform(String tenantDomain, String code) throws PlatformManagementException {
Platform platform = getPlatformFromInMemory(tenantDomain, code);
public Platform getPlatform(String tenantDomain, String identifier) throws PlatformManagementException {
Platform platform = getPlatformFromInMemory(tenantDomain, identifier);
if (platform == null) {
platform = DAOFactory.getPlatformDAO().getPlatform(tenantDomain, code);
}
platform = DAOFactory.getPlatformDAO().getPlatform(tenantDomain, identifier);
if (platform != null) {
return platform;
}
} else {
return new Platform(platform);
}
throw new PlatformManagementException("No platform was found for tenant - "+ tenantDomain+" with Platform code - "+ code);
throw new PlatformManagementException("No platform was found for tenant - " + tenantDomain +
" with Platform identifier - " + identifier);
}
private Platform getPlatformFromInMemory(String tenantDomain, String code) {
private Platform getPlatformFromInMemory(String tenantDomain, String identifier) {
Map<String, Platform> platformMap = this.inMemoryStore.get(tenantDomain);
if (platformMap != null) {
Platform platform = platformMap.get(code);
Platform platform = platformMap.get(identifier);
if (platform != null) {
return platform;
}
@ -93,7 +96,7 @@ public class PlatformManagerImpl implements PlatformManager {
if (!tenantDomain.equalsIgnoreCase(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) {
platformMap = this.inMemoryStore.get(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
if (platformMap != null) {
Platform platform = platformMap.get(code);
Platform platform = platformMap.get(identifier);
if (platform != null && platform.isShared()) {
return platform;
}
@ -108,7 +111,9 @@ public class PlatformManagerImpl implements PlatformManager {
throw new PlatformManagementException("Platform sharing is a restricted operation, therefore Platform - "
+ platform.getIdentifier() + " cannot be shared by the tenant domain - " + tenantDomain);
}
int platformId = DAOFactory.getPlatformDAO().register(tenantDomain, platform);
if (platform.isFileBased()) {
platform.setId(platformId);
Map<String, Platform> tenantPlatforms = this.inMemoryStore.get(tenantDomain);
if (tenantPlatforms == null) {
tenantPlatforms = new HashMap<>();
@ -119,8 +124,6 @@ public class PlatformManagerImpl implements PlatformManager {
} else {
throw new PlatformManagementException("Platform - " + platform.getIdentifier() + " is already registered!");
}
} else {
DAOFactory.getPlatformDAO().register(tenantDomain, platform);
}
if (platform.isDefaultTenantMapping()) {
try {
@ -139,31 +142,77 @@ public class PlatformManagerImpl implements PlatformManager {
}
@Override
public void unregister(String tenantDomain, String platformCode, boolean isFileBased) throws PlatformManagementException {
public void update(String tenantDomain, String oldPlatformIdentifier, Platform platform)
throws PlatformManagementException {
if (platform.isShared() && !tenantDomain.equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) {
throw new PlatformManagementException("Platform sharing is a restricted operation, therefore Platform - "
+ platform.getIdentifier() + " cannot be shared by the tenant domain - " + tenantDomain);
}
Platform oldPlatform;
if (platform.isFileBased()) {
Map<String, Platform> tenantPlatforms = this.inMemoryStore.get(tenantDomain);
if (tenantPlatforms == null) {
throw new PlatformManagementException("No platforms registered for the tenant - " + tenantDomain +
" with platform identifier - " + platform.getIdentifier());
}
oldPlatform = tenantPlatforms.get(oldPlatformIdentifier);
if (oldPlatform == null) {
throw new PlatformManagementException("No platforms registered for the tenant - " + tenantDomain +
" with platform identifier - " + platform.getIdentifier());
} else {
DAOFactory.getPlatformDAO().update(tenantDomain, oldPlatformIdentifier, platform);
platform.setId(oldPlatform.getId());
tenantPlatforms.put(platform.getIdentifier(), platform);
}
} else {
oldPlatform = DAOFactory.getPlatformDAO().getPlatform(tenantDomain, oldPlatformIdentifier);
DAOFactory.getPlatformDAO().update(tenantDomain, oldPlatformIdentifier, platform);
}
if (platform.isDefaultTenantMapping() && !oldPlatform.isDefaultTenantMapping()) {
try {
if (platform.isShared() && !oldPlatform.isShared()) {
TenantManager tenantManager = DataHolder.getInstance().getRealmService().getTenantManager();
Tenant[] tenants = tenantManager.getAllTenants();
for (Tenant tenant : tenants) {
addMapping(tenant.getDomain(), platform.getIdentifier());
}
}
addMapping(tenantDomain, platform.getIdentifier());
} catch (UserStoreException e) {
throw new PlatformManagementException("Error occured while assigning the platforms for tenants!", e);
}
}
if (!platform.isShared() && oldPlatform.isShared()) {
DAOFactory.getPlatformDAO().removeMappingTenants(platform.getIdentifier());
}
}
@Override
public void unregister(String tenantDomain, String identifier, boolean isFileBased) throws PlatformManagementException {
if (isFileBased) {
Map<String, Platform> tenantPlatforms = this.inMemoryStore.get(tenantDomain);
if (tenantPlatforms != null) {
this.inMemoryStore.remove(platformCode);
this.inMemoryStore.remove(identifier);
}
} else {
DAOFactory.getPlatformDAO().unregister(tenantDomain, platformCode);
DAOFactory.getPlatformDAO().unregister(tenantDomain, identifier);
}
}
@Override
public void addMapping(String tenantDomain, List<String> platformCode) throws PlatformManagementException {
DAOFactory.getPlatformDAO().addMapping(tenantDomain, platformCode);
public void addMapping(String tenantDomain, List<String> platformIdentifiers) throws PlatformManagementException {
DAOFactory.getPlatformDAO().addMapping(tenantDomain, platformIdentifiers);
}
@Override
public void addMapping(String tenantDomain, String platformCode) throws PlatformManagementException {
List<String> codes = new ArrayList<>();
codes.add(platformCode);
DAOFactory.getPlatformDAO().addMapping(tenantDomain, codes);
public void addMapping(String tenantDomain, String platformIdentifier) throws PlatformManagementException {
List<String> identifiers = new ArrayList<>();
identifiers.add(platformIdentifier);
DAOFactory.getPlatformDAO().addMapping(tenantDomain, identifiers);
}
@Override
public void removeMapping(String tenantDomain, String platformCode) throws PlatformManagementException {
DAOFactory.getPlatformDAO().removeMapping(tenantDomain, platformCode);
public void removeMapping(String tenantDomain, String platformIdentifier) throws PlatformManagementException {
DAOFactory.getPlatformDAO().removeMapping(tenantDomain, platformIdentifier);
}
}

@ -1,12 +1,13 @@
CREATE TABLE APPM_PLATFORM (
ID INT NOT NULL AUTO_INCREMENT,
CODE VARCHAR (100) NOT NULL,
IDENTIFIER VARCHAR (100) NOT NULL,
TENANT_DOMAIN VARCHAR (100) NOT NULL ,
NAME VARCHAR (255) NOT NULL,
NAME VARCHAR (255),
FILE_BASED BOOLEAN,
DESCRIPTION LONGVARCHAR,
IS_SHARED BOOLEAN,
ICON_NAME VARCHAR (100),
PRIMARY KEY (ID, CODE, TENANT_DOMAIN)
PRIMARY KEY (ID, IDENTIFIER, TENANT_DOMAIN)
);
CREATE TABLE APPM_PLATFORM_PROPERTIES (
@ -22,8 +23,8 @@ PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME)
CREATE TABLE APPM_PLATFORM_TENANT_MAPPING (
ID INT NOT NULL AUTO_INCREMENT,
TENANT_DOMAIN VARCHAR (100) NOT NULL ,
PLATFORM_CODE VARCHAR (100) NOT NULL,
FOREIGN KEY(PLATFORM_CODE) REFERENCES APPM_PLATFORM(CODE) ON DELETE CASCADE,
PRIMARY KEY (ID, TENANT_DOMAIN, PLATFORM_CODE)
PLATFORM_ID VARCHAR (100) NOT NULL,
FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(ID) ON DELETE CASCADE,
PRIMARY KEY (ID, TENANT_DOMAIN, PLATFORM_ID)
)

Loading…
Cancel
Save