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