From cd42a2c48fea2b15222c258ada529eccf0562a02 Mon Sep 17 00:00:00 2001 From: osh Date: Wed, 28 Jun 2023 20:03:16 +0530 Subject: [PATCH 1/2] Adding the DAO layer for vpp user management fixes https://roadmap.entgra.net/issues/10169 --- .../mgt/core/dao/VppApplicationDAO.java | 14 ++ .../ApplicationManagementDAOFactory.java | 36 ++++- .../vpp/GenericVppApplicationDAOImpl.java | 152 ++++++++++++++++++ .../impl/vpp/OracleVppApplicationDAOImpl.java | 64 ++++++++ .../impl/vpp/PostgreSQLVppApplicationDAO.java | 63 ++++++++ .../vpp/SQLServerVppApplicationDAOImpl.java | 4 + .../core/impl/VppApplicationManagerImpl.java | 80 +++++++-- .../application/mgt/core/util/DAOUtil.java | 38 +++++ 8 files changed, 435 insertions(+), 16 deletions(-) create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/VppApplicationDAO.java create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/GenericVppApplicationDAOImpl.java create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/OracleVppApplicationDAOImpl.java create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/PostgreSQLVppApplicationDAO.java create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/SQLServerVppApplicationDAOImpl.java diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/VppApplicationDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/VppApplicationDAO.java new file mode 100644 index 0000000000..ae2b75e515 --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/VppApplicationDAO.java @@ -0,0 +1,14 @@ +package io.entgra.device.mgt.core.application.mgt.core.dao; + +import io.entgra.device.mgt.core.application.mgt.common.dto.VppUserDTO; +import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException; + + +public interface VppApplicationDAO { + + int addVppUser(VppUserDTO userDTO) throws ApplicationManagementDAOException; + + VppUserDTO updateVppUser(VppUserDTO userDTO) throws ApplicationManagementDAOException; + + VppUserDTO getUserByDMUsername(String emmUsername) throws ApplicationManagementDAOException; +} diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/common/ApplicationManagementDAOFactory.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/common/ApplicationManagementDAOFactory.java index e7996ee804..1a29b33b2d 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/common/ApplicationManagementDAOFactory.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/common/ApplicationManagementDAOFactory.java @@ -17,7 +17,7 @@ */ package io.entgra.device.mgt.core.application.mgt.core.dao.common; -import io.entgra.device.mgt.core.application.mgt.core.dao.SPApplicationDAO; +import io.entgra.device.mgt.core.application.mgt.core.dao.*; import io.entgra.device.mgt.core.application.mgt.core.dao.impl.application.spapplication.GenericSPApplicationDAOImpl; import io.entgra.device.mgt.core.application.mgt.core.dao.impl.application.spapplication.OracleSPApplicationDAOImpl; import io.entgra.device.mgt.core.application.mgt.core.dao.impl.application.spapplication.PostgreSQLSPApplicationDAOImpl; @@ -26,16 +26,14 @@ import io.entgra.device.mgt.core.application.mgt.core.dao.impl.visibility.Generi import io.entgra.device.mgt.core.application.mgt.core.dao.impl.visibility.OracleVisibilityDAOImpl; import io.entgra.device.mgt.core.application.mgt.core.dao.impl.visibility.PostgreSQLVisibilityDAOImpl; import io.entgra.device.mgt.core.application.mgt.core.dao.impl.visibility.SQLServerVisibilityDAOImpl; +import io.entgra.device.mgt.core.application.mgt.core.dao.impl.vpp.GenericVppApplicationDAOImpl; +import io.entgra.device.mgt.core.application.mgt.core.dao.impl.vpp.OracleVppApplicationDAOImpl; +import io.entgra.device.mgt.core.application.mgt.core.dao.impl.vpp.PostgreSQLVppApplicationDAO; +import io.entgra.device.mgt.core.application.mgt.core.dao.impl.vpp.SQLServerVppApplicationDAOImpl; import io.entgra.device.mgt.core.application.mgt.core.util.Constants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import io.entgra.device.mgt.core.application.mgt.common.exception.UnsupportedDatabaseEngineException; -import io.entgra.device.mgt.core.application.mgt.core.dao.ApplicationDAO; -import io.entgra.device.mgt.core.application.mgt.core.dao.ApplicationReleaseDAO; -import io.entgra.device.mgt.core.application.mgt.core.dao.LifecycleStateDAO; -import io.entgra.device.mgt.core.application.mgt.core.dao.ReviewDAO; -import io.entgra.device.mgt.core.application.mgt.core.dao.SubscriptionDAO; -import io.entgra.device.mgt.core.application.mgt.core.dao.VisibilityDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.impl.application.PostgreSQLApplicationDAOImpl; import io.entgra.device.mgt.core.application.mgt.core.dao.impl.application.SQLServerApplicationDAOImpl; import io.entgra.device.mgt.core.application.mgt.core.dao.impl.application.release.OracleApplicationReleaseDAOImpl; @@ -232,4 +230,28 @@ public class ApplicationManagementDAOFactory { } throw new IllegalStateException("Database engine has not initialized properly."); } + + + /** + * To get the instance of VppApplicationImplementation of the particular database engine. + * @return specific VppApplicationImplementation + */ + public static VppApplicationDAO getVppApplicationDAO() { + if (databaseEngine != null) { + switch (databaseEngine) { + case Constants.DataBaseTypes.DB_TYPE_H2: + case Constants.DataBaseTypes.DB_TYPE_MYSQL: + return new GenericVppApplicationDAOImpl(); + case Constants.DataBaseTypes.DB_TYPE_POSTGRESQL: + return new PostgreSQLVppApplicationDAO(); + case Constants.DataBaseTypes.DB_TYPE_ORACLE: + return new OracleVppApplicationDAOImpl(); + case Constants.DataBaseTypes.DB_TYPE_MSSQL: + return new SQLServerVppApplicationDAOImpl(); + default: + throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); + } + } + throw new IllegalStateException("Database engine has not initialized properly."); + } } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/GenericVppApplicationDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/GenericVppApplicationDAOImpl.java new file mode 100644 index 0000000000..3dc356b764 --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/GenericVppApplicationDAOImpl.java @@ -0,0 +1,152 @@ +package io.entgra.device.mgt.core.application.mgt.core.dao.impl.vpp; + +import io.entgra.device.mgt.core.application.mgt.common.dto.VppUserDTO; +import io.entgra.device.mgt.core.application.mgt.common.exception.DBConnectionException; +import io.entgra.device.mgt.core.application.mgt.core.dao.VppApplicationDAO; +import io.entgra.device.mgt.core.application.mgt.core.dao.impl.AbstractDAOImpl; +import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException; +import io.entgra.device.mgt.core.application.mgt.core.exception.UnexpectedServerErrorException; +import io.entgra.device.mgt.core.application.mgt.core.util.DAOUtil; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.sql.*; + +public class GenericVppApplicationDAOImpl extends AbstractDAOImpl implements VppApplicationDAO { + private static final Log log = LogFactory.getLog(GenericVppApplicationDAOImpl.class); + + @Override + public int addVppUser(VppUserDTO userDTO) + throws ApplicationManagementDAOException { + int vppUserId = -1; + String sql = "INSERT INTO " + + "AP_VPP_USER(" + + "CLIENT_USER_ID, " + + "DM_USERNAME, " + + "TENANT_ID, " + + "EMAIL, " + + "INVITE_CODE, " + + "STATUS," + + "CREATED_TIME," + + "LAST_UPDATED_TIME," + + "MANAGED_ID," + + "TEMP_PASSWORD) " + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { + long currentTime = System.currentTimeMillis(); + stmt.setString(1, userDTO.getClientUserId()); + stmt.setString(2, userDTO.getDmUsername()); + stmt.setInt(3, userDTO.getTenantId()); + stmt.setString(4, userDTO.getEmail()); + stmt.setString(5, userDTO.getInviteCode()); + stmt.setString(6, userDTO.getStatus()); + stmt.setLong(7, currentTime); + stmt.setLong(8, currentTime); + stmt.setString(9, userDTO.getManagedId()); + stmt.setString(10, userDTO.getTmpPassword()); + stmt.executeUpdate(); + try (ResultSet rs = stmt.getGeneratedKeys()) { + if (rs.next()) { + vppUserId = rs.getInt(1); + } + } + return vppUserId; + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining database connection when adding the vpp user"; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred when processing SQL to add the vpp user."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + + public VppUserDTO updateVppUser(VppUserDTO userDTO) + throws ApplicationManagementDAOException { + + String sql = "UPDATE " + + "AP_VPP_USER " + + "SET " + + "CLIENT_USER_ID = ?," + + "DM_USERNAME = ?, " + + "TENANT_ID = ?, " + + "EMAIL = ?, " + + "INVITE_CODE = ?, " + + "STATUS = ?, " + + "LAST_UPDATED_TIME = ?, " + + "MANAGED_ID = ?, " + + "TEMP_PASSWORD = ? " + + "WHERE ID = ?"; + try { + Connection conn = this.getDBConnection(); + long updatedTime = System.currentTimeMillis(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, userDTO.getClientUserId()); + stmt.setString(2, userDTO.getDmUsername()); + stmt.setInt(3, userDTO.getTenantId()); + stmt.setString(4, userDTO.getEmail()); + stmt.setString(5, userDTO.getInviteCode()); + stmt.setString(6, userDTO.getStatus()); + stmt.setLong(7, updatedTime); + stmt.setString(8, userDTO.getManagedId()); + stmt.setString(9, userDTO.getTmpPassword()); + stmt.setInt(10, userDTO.getId()); + stmt.executeUpdate(); + if (stmt.executeUpdate() == 1) { + return userDTO; + } + return null; + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining database connection when updating the vpp user"; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred when processing SQL to updating the vpp user."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + + public VppUserDTO getUserByDMUsername(String emmUsername) + throws ApplicationManagementDAOException { + String sql = "SELECT " + + "ID, " + + "CLIENT_USER_ID, " + + "TENANT_ID, " + + "EMAIL, " + + "INVITE_CODE, " + + "STATUS, " + + "CREATED_TIME, " + + "LAST_UPDATED_TIME, " + + "MANAGED_ID, " + + "TEMP_PASSWORD " + + "FROM AP_VPP_USER " + + "WHERE DM_USERNAME = ?"; + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, emmUsername); + try (ResultSet rs = stmt.executeQuery()) { + return DAOUtil.loadVppUser(rs); + } + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining database connection when retrieving vpp user by EMM Username."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred when processing SQL to retrieve vpp user by EMM Username."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (UnexpectedServerErrorException e) { + String msg = "Found more than one user for: " + emmUsername; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } +} diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/OracleVppApplicationDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/OracleVppApplicationDAOImpl.java new file mode 100644 index 0000000000..fd5f5cc9d3 --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/OracleVppApplicationDAOImpl.java @@ -0,0 +1,64 @@ +package io.entgra.device.mgt.core.application.mgt.core.dao.impl.vpp; + +import io.entgra.device.mgt.core.application.mgt.common.dto.VppUserDTO; +import io.entgra.device.mgt.core.application.mgt.common.exception.DBConnectionException; +import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.sql.*; + +public class OracleVppApplicationDAOImpl extends GenericVppApplicationDAOImpl { + + private static final Log log = LogFactory.getLog(GenericVppApplicationDAOImpl.class); + + @Override + public int addVppUser(VppUserDTO userDTO) + throws ApplicationManagementDAOException { + int vppUserId = -1; + String sql = "INSERT INTO " + + "AP_VPP_USER(" + + "CLIENT_USER_ID, " + + "DM_USERNAME, " + + "TENANT_ID, " + + "EMAIL, " + + "INVITE_CODE, " + + "STATUS," + + "CREATED_TIME," + + "LAST_UPDATED_TIME," + + "MANAGED_ID," + + "TEMP_PASSWORD) " + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql, new String[] {"ID"})) { + long currentTime = System.currentTimeMillis(); + stmt.setString(1, userDTO.getClientUserId()); + stmt.setString(2, userDTO.getDmUsername()); + stmt.setInt(3, userDTO.getTenantId()); + stmt.setString(4, userDTO.getEmail()); + stmt.setString(5, userDTO.getInviteCode()); + stmt.setString(6, userDTO.getStatus()); + stmt.setLong(7, currentTime); + stmt.setLong(8, currentTime); + stmt.setString(9, userDTO.getManagedId()); + stmt.setString(10, userDTO.getTmpPassword()); + stmt.executeUpdate(); + try (ResultSet rs = stmt.getGeneratedKeys()) { + if (rs.next()) { + vppUserId = rs.getInt(1); + } + } + return vppUserId; + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining database connection when adding the vpp user"; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred when processing SQL to add the vpp user."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } +} diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/PostgreSQLVppApplicationDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/PostgreSQLVppApplicationDAO.java new file mode 100644 index 0000000000..7dd20ff6f4 --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/PostgreSQLVppApplicationDAO.java @@ -0,0 +1,63 @@ +package io.entgra.device.mgt.core.application.mgt.core.dao.impl.vpp; + +import io.entgra.device.mgt.core.application.mgt.common.dto.VppUserDTO; +import io.entgra.device.mgt.core.application.mgt.common.exception.DBConnectionException; +import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.sql.*; + +public class PostgreSQLVppApplicationDAO extends GenericVppApplicationDAOImpl { + + private static final Log log = LogFactory.getLog(GenericVppApplicationDAOImpl.class); + + public int addVppUser(VppUserDTO userDTO) + throws ApplicationManagementDAOException { + int vppUserId = -1; + String sql = "INSERT INTO " + + "AP_VPP_USER(" + + "CLIENT_USER_ID, " + + "DM_USERNAME, " + + "TENANT_ID, " + + "EMAIL, " + + "INVITE_CODE, " + + "STATUS," + + "CREATED_TIME," + + "LAST_UPDATED_TIME," + + "MANAGED_ID," + + "TEMP_PASSWORD) " + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql, new String[]{"ID"})) { + long currentTime = System.currentTimeMillis(); + stmt.setString(1, userDTO.getClientUserId()); + stmt.setString(2, userDTO.getDmUsername()); + stmt.setInt(3, userDTO.getTenantId()); + stmt.setString(4, userDTO.getEmail()); + stmt.setString(5, userDTO.getInviteCode()); + stmt.setString(6, userDTO.getStatus()); + stmt.setLong(7, currentTime); + stmt.setLong(8, currentTime); + stmt.setString(9, userDTO.getManagedId()); + stmt.setString(10, userDTO.getTmpPassword()); + stmt.executeUpdate(); + try (ResultSet rs = stmt.getGeneratedKeys()) { + if (rs.next()) { + vppUserId = rs.getInt(1); + } + } + return vppUserId; + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining database connection when adding the vpp user"; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred when processing SQL to add the vpp user."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } +} diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/SQLServerVppApplicationDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/SQLServerVppApplicationDAOImpl.java new file mode 100644 index 0000000000..dc110b03f4 --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/SQLServerVppApplicationDAOImpl.java @@ -0,0 +1,4 @@ +package io.entgra.device.mgt.core.application.mgt.core.dao.impl.vpp; + +public class SQLServerVppApplicationDAOImpl extends GenericVppApplicationDAOImpl { +} diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/VppApplicationManagerImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/VppApplicationManagerImpl.java index 6ec3023def..011a2e3868 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/VppApplicationManagerImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/VppApplicationManagerImpl.java @@ -24,6 +24,8 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import io.entgra.device.mgt.core.application.mgt.common.dto.ProxyResponse; import io.entgra.device.mgt.core.application.mgt.common.dto.VppItuneUserDTO; +import io.entgra.device.mgt.core.application.mgt.common.exception.DBConnectionException; +import io.entgra.device.mgt.core.application.mgt.common.exception.TransactionManagementException; import io.entgra.device.mgt.core.application.mgt.common.wrapper.VppItuneUserRequestWrapper; import io.entgra.device.mgt.core.application.mgt.common.dto.VppUserDTO; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; @@ -32,9 +34,12 @@ import io.entgra.device.mgt.core.application.mgt.common.wrapper.VppItuneUserResp import io.entgra.device.mgt.core.application.mgt.core.dao.ApplicationDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.SPApplicationDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.VisibilityDAO; +import io.entgra.device.mgt.core.application.mgt.core.dao.VppApplicationDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.common.ApplicationManagementDAOFactory; +import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException; import io.entgra.device.mgt.core.application.mgt.core.internal.DataHolder; import io.entgra.device.mgt.core.application.mgt.core.lifecycle.LifecycleStateManager; +import io.entgra.device.mgt.core.application.mgt.core.util.ConnectionManagerUtil; import io.entgra.device.mgt.core.application.mgt.core.util.Constants; import io.entgra.device.mgt.core.application.mgt.core.util.VppHttpUtil; import org.apache.commons.logging.Log; @@ -56,6 +61,7 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { private SPApplicationDAO spApplicationDAO; private VisibilityDAO visibilityDAO; private final LifecycleStateManager lifecycleStateManager; + private VppApplicationDAO vppApplicationDAO; public VppApplicationManagerImpl() { initDataAccessObjects(); @@ -66,6 +72,7 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { this.applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); this.visibilityDAO = ApplicationManagementDAOFactory.getVisibilityDAO(); this.spApplicationDAO = ApplicationManagementDAOFactory.getSPApplicationDAO(); + this.vppApplicationDAO = ApplicationManagementDAOFactory.getVppApplicationDAO(); } @Override @@ -96,10 +103,30 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { .getInviteCode()); userDTO.setStatus(vppItuneUserResponseWrapper.getUser().get(0).getStatus()); log.error("userDTO " + userDTO.toString()); - // TODO: Save the userDTO in the DAO - - - return userDTO; + try { + ConnectionManagerUtil.beginDBTransaction(); + if (vppApplicationDAO.addVppUser(userDTO) != -1) { + ConnectionManagerUtil.commitDBTransaction(); + return userDTO; + } + ConnectionManagerUtil.rollbackDBTransaction(); + return null; + } catch (ApplicationManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Error occurred while adding the Vpp User."; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while executing database transaction for adding Vpp User."; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (DBConnectionException e) { + String msg = "Error occurred while retrieving the database connection for adding Vpp User."; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } } } @@ -113,8 +140,20 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { @Override public VppUserDTO getUserByDMUsername(String emmUsername) throws ApplicationManagementException { - // TODO: Return from DAO in a tenanted manner - return null; + try { + ConnectionManagerUtil.openDBConnection(); + return vppApplicationDAO.getUserByDMUsername(emmUsername); + } catch (DBConnectionException e) { + String msg = "DB Connection error occurs while getting vpp User data related to EMM user " + emmUsername + "."; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred while getting vpp User data related to EMM user " + emmUsername + "."; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } } @Override @@ -131,12 +170,35 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { HttpStatus.SC_CREATED) && proxyResponse.getData().contains(Constants.VPP.EVENT_ID)) { log.error("userDTO " + userDTO.toString()); - // TODO: Save the userDTO in the DAO - + try { + ConnectionManagerUtil.beginDBTransaction(); + if (vppApplicationDAO.updateVppUser(userDTO) == null) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Unable to update the Vpp user " +userDTO.getId(); + log.error(msg); + throw new ApplicationManagementException(msg); + } + ConnectionManagerUtil.commitDBTransaction(); + } catch (ApplicationManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Error occurred while updating the Vpp User."; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while executing database transaction for Vpp User update."; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (DBConnectionException e) { + String msg = "Error occurred while retrieving the database connection for Vpp User update."; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } } } catch (IOException e) { - String msg = "Error while callng VPP backend to update"; + String msg = "Error while calling VPP backend to update"; log.error(msg, e); throw new ApplicationManagementException(msg, e); } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/DAOUtil.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/DAOUtil.java index 1960230759..89b5ac01ca 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/DAOUtil.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/DAOUtil.java @@ -32,6 +32,7 @@ import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDT import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ReviewDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ScheduledSubscriptionDTO; +import io.entgra.device.mgt.core.application.mgt.common.dto.VppUserDTO; import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; import java.sql.PreparedStatement; @@ -43,6 +44,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Date; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -344,6 +346,42 @@ public class DAOUtil { return subscriptionDTOS; } + public static VppUserDTO loadVppUser(ResultSet rs) throws SQLException, UnexpectedServerErrorException { + List vppUserDTOS = loadVppUsers(rs); + if (vppUserDTOS.isEmpty()) { + return null; + } + if (vppUserDTOS.size() > 1) { + String msg = "Internal server error. Found more than one vpp user for requested emmUsername"; + log.error(msg); + throw new UnexpectedServerErrorException(msg); + } + return vppUserDTOS.get(0); + } + + public static List loadVppUsers (ResultSet rs) throws SQLException { + List vppUserDTOS = new ArrayList<>(); + while (rs.next()) { + VppUserDTO vppUserDTO = new VppUserDTO(); + vppUserDTO.setId(rs.getInt("ID")); + vppUserDTO.setClientUserId(rs.getString("CLIENT_USER_ID")); + vppUserDTO.setTenantId(rs.getInt("TENANT_ID")); + vppUserDTO.setEmail(rs.getString("EMAIL")); + vppUserDTO.setInviteCode(rs.getString("INVITE_CODE")); + vppUserDTO.setStatus(rs.getString("STATUS")); + vppUserDTO.setManagedId(rs.getString("MANAGED_ID")); + vppUserDTO.setTmpPassword(rs.getString("TEMP_PASSWORD")); + if (rs.getLong("CREATED_TIME") != 0) { + vppUserDTO.setCreatedTime(new Date(rs.getLong(("CREATED_TIME")) * 1000).toString()); + } + if (rs.getLong("LAST_UPDATED_TIME") != 0) { + vppUserDTO.setLastUpdatedTime(new Date(rs.getLong(("LAST_UPDATED_TIME")) * 1000).toString()); + } + vppUserDTOS.add(vppUserDTO); + } + return vppUserDTOS; + } + /** * Cleans up the statement and resultset after executing the query * From 2e799a545feb33dfd4618f38e9d5ed31e53cd615 Mon Sep 17 00:00:00 2001 From: osh Date: Wed, 28 Jun 2023 21:43:06 +0530 Subject: [PATCH 2/2] Resolve comments --- .../mgt/core/dao/VppApplicationDAO.java | 24 ++++++++++++-- .../vpp/GenericVppApplicationDAOImpl.java | 31 +++++++++++++++---- .../impl/vpp/OracleVppApplicationDAOImpl.java | 22 +++++++++++-- .../impl/vpp/PostgreSQLVppApplicationDAO.java | 22 +++++++++++-- .../vpp/SQLServerVppApplicationDAOImpl.java | 18 +++++++++++ .../core/impl/VppApplicationManagerImpl.java | 12 +++++-- 6 files changed, 113 insertions(+), 16 deletions(-) diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/VppApplicationDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/VppApplicationDAO.java index ae2b75e515..6ebec3f0e7 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/VppApplicationDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/VppApplicationDAO.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package io.entgra.device.mgt.core.application.mgt.core.dao; import io.entgra.device.mgt.core.application.mgt.common.dto.VppUserDTO; @@ -6,9 +24,9 @@ import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManag public interface VppApplicationDAO { - int addVppUser(VppUserDTO userDTO) throws ApplicationManagementDAOException; + int addVppUser(VppUserDTO userDTO, int tenantId) throws ApplicationManagementDAOException; - VppUserDTO updateVppUser(VppUserDTO userDTO) throws ApplicationManagementDAOException; + VppUserDTO updateVppUser(VppUserDTO userDTO, int tenantId) throws ApplicationManagementDAOException; - VppUserDTO getUserByDMUsername(String emmUsername) throws ApplicationManagementDAOException; + VppUserDTO getUserByDMUsername(String emmUsername, int tenantId) throws ApplicationManagementDAOException; } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/GenericVppApplicationDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/GenericVppApplicationDAOImpl.java index 3dc356b764..2bafc338f3 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/GenericVppApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/GenericVppApplicationDAOImpl.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package io.entgra.device.mgt.core.application.mgt.core.dao.impl.vpp; import io.entgra.device.mgt.core.application.mgt.common.dto.VppUserDTO; @@ -16,7 +34,7 @@ public class GenericVppApplicationDAOImpl extends AbstractDAOImpl implements Vp private static final Log log = LogFactory.getLog(GenericVppApplicationDAOImpl.class); @Override - public int addVppUser(VppUserDTO userDTO) + public int addVppUser(VppUserDTO userDTO, int tenantId) throws ApplicationManagementDAOException { int vppUserId = -1; String sql = "INSERT INTO " @@ -38,7 +56,7 @@ public class GenericVppApplicationDAOImpl extends AbstractDAOImpl implements Vp long currentTime = System.currentTimeMillis(); stmt.setString(1, userDTO.getClientUserId()); stmt.setString(2, userDTO.getDmUsername()); - stmt.setInt(3, userDTO.getTenantId()); + stmt.setInt(3, tenantId); stmt.setString(4, userDTO.getEmail()); stmt.setString(5, userDTO.getInviteCode()); stmt.setString(6, userDTO.getStatus()); @@ -65,7 +83,7 @@ public class GenericVppApplicationDAOImpl extends AbstractDAOImpl implements Vp } } - public VppUserDTO updateVppUser(VppUserDTO userDTO) + public VppUserDTO updateVppUser(VppUserDTO userDTO, int tenantId) throws ApplicationManagementDAOException { String sql = "UPDATE " @@ -87,7 +105,7 @@ public class GenericVppApplicationDAOImpl extends AbstractDAOImpl implements Vp try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, userDTO.getClientUserId()); stmt.setString(2, userDTO.getDmUsername()); - stmt.setInt(3, userDTO.getTenantId()); + stmt.setInt(3, tenantId); stmt.setString(4, userDTO.getEmail()); stmt.setString(5, userDTO.getInviteCode()); stmt.setString(6, userDTO.getStatus()); @@ -112,7 +130,7 @@ public class GenericVppApplicationDAOImpl extends AbstractDAOImpl implements Vp } } - public VppUserDTO getUserByDMUsername(String emmUsername) + public VppUserDTO getUserByDMUsername(String emmUsername, int tenantId) throws ApplicationManagementDAOException { String sql = "SELECT " + "ID, " @@ -126,11 +144,12 @@ public class GenericVppApplicationDAOImpl extends AbstractDAOImpl implements Vp + "MANAGED_ID, " + "TEMP_PASSWORD " + "FROM AP_VPP_USER " - + "WHERE DM_USERNAME = ?"; + + "WHERE DM_USERNAME = ? AND TENANT_ID = ?"; try { Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, emmUsername); + stmt.setInt(2, tenantId); try (ResultSet rs = stmt.executeQuery()) { return DAOUtil.loadVppUser(rs); } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/OracleVppApplicationDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/OracleVppApplicationDAOImpl.java index fd5f5cc9d3..480f18006e 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/OracleVppApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/OracleVppApplicationDAOImpl.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package io.entgra.device.mgt.core.application.mgt.core.dao.impl.vpp; import io.entgra.device.mgt.core.application.mgt.common.dto.VppUserDTO; @@ -13,7 +31,7 @@ public class OracleVppApplicationDAOImpl extends GenericVppApplicationDAOImpl { private static final Log log = LogFactory.getLog(GenericVppApplicationDAOImpl.class); @Override - public int addVppUser(VppUserDTO userDTO) + public int addVppUser(VppUserDTO userDTO, int tenantId) throws ApplicationManagementDAOException { int vppUserId = -1; String sql = "INSERT INTO " @@ -35,7 +53,7 @@ public class OracleVppApplicationDAOImpl extends GenericVppApplicationDAOImpl { long currentTime = System.currentTimeMillis(); stmt.setString(1, userDTO.getClientUserId()); stmt.setString(2, userDTO.getDmUsername()); - stmt.setInt(3, userDTO.getTenantId()); + stmt.setInt(3, tenantId); stmt.setString(4, userDTO.getEmail()); stmt.setString(5, userDTO.getInviteCode()); stmt.setString(6, userDTO.getStatus()); diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/PostgreSQLVppApplicationDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/PostgreSQLVppApplicationDAO.java index 7dd20ff6f4..5d9fe7a8f6 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/PostgreSQLVppApplicationDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/PostgreSQLVppApplicationDAO.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package io.entgra.device.mgt.core.application.mgt.core.dao.impl.vpp; import io.entgra.device.mgt.core.application.mgt.common.dto.VppUserDTO; @@ -12,7 +30,7 @@ public class PostgreSQLVppApplicationDAO extends GenericVppApplicationDAOImpl { private static final Log log = LogFactory.getLog(GenericVppApplicationDAOImpl.class); - public int addVppUser(VppUserDTO userDTO) + public int addVppUser(VppUserDTO userDTO, int tenantId) throws ApplicationManagementDAOException { int vppUserId = -1; String sql = "INSERT INTO " @@ -34,7 +52,7 @@ public class PostgreSQLVppApplicationDAO extends GenericVppApplicationDAOImpl { long currentTime = System.currentTimeMillis(); stmt.setString(1, userDTO.getClientUserId()); stmt.setString(2, userDTO.getDmUsername()); - stmt.setInt(3, userDTO.getTenantId()); + stmt.setInt(3, tenantId); stmt.setString(4, userDTO.getEmail()); stmt.setString(5, userDTO.getInviteCode()); stmt.setString(6, userDTO.getStatus()); diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/SQLServerVppApplicationDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/SQLServerVppApplicationDAOImpl.java index dc110b03f4..da4e4828da 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/SQLServerVppApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/SQLServerVppApplicationDAOImpl.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package io.entgra.device.mgt.core.application.mgt.core.dao.impl.vpp; public class SQLServerVppApplicationDAOImpl extends GenericVppApplicationDAOImpl { diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/VppApplicationManagerImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/VppApplicationManagerImpl.java index 011a2e3868..468fc17304 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/VppApplicationManagerImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/VppApplicationManagerImpl.java @@ -45,6 +45,7 @@ import io.entgra.device.mgt.core.application.mgt.core.util.VppHttpUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.HttpStatus; +import org.wso2.carbon.context.PrivilegedCarbonContext; import java.io.IOException; @@ -77,6 +78,9 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { @Override public VppUserDTO addUser(VppUserDTO userDTO) throws ApplicationManagementException { + + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + // Call the API to add try { VppItuneUserDTO ituneUserDTO = userDTO; @@ -105,7 +109,7 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { log.error("userDTO " + userDTO.toString()); try { ConnectionManagerUtil.beginDBTransaction(); - if (vppApplicationDAO.addVppUser(userDTO) != -1) { + if (vppApplicationDAO.addVppUser(userDTO, tenantId) != -1) { ConnectionManagerUtil.commitDBTransaction(); return userDTO; } @@ -140,9 +144,10 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { @Override public VppUserDTO getUserByDMUsername(String emmUsername) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { ConnectionManagerUtil.openDBConnection(); - return vppApplicationDAO.getUserByDMUsername(emmUsername); + return vppApplicationDAO.getUserByDMUsername(emmUsername, tenantId); } catch (DBConnectionException e) { String msg = "DB Connection error occurs while getting vpp User data related to EMM user " + emmUsername + "."; log.error(msg, e); @@ -158,6 +163,7 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { @Override public void updateUser(VppUserDTO userDTO) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); VppItuneUserDTO ituneUserDTO = userDTO; VppItuneUserRequestWrapper wrapper = new VppItuneUserRequestWrapper(); wrapper.getUser().add(ituneUserDTO); @@ -173,7 +179,7 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { try { ConnectionManagerUtil.beginDBTransaction(); - if (vppApplicationDAO.updateVppUser(userDTO) == null) { + if (vppApplicationDAO.updateVppUser(userDTO, tenantId) == null) { ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Unable to update the Vpp user " +userDTO.getId(); log.error(msg);