From cd42a2c48fea2b15222c258ada529eccf0562a02 Mon Sep 17 00:00:00 2001 From: osh Date: Wed, 28 Jun 2023 20:03:16 +0530 Subject: [PATCH] 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 *