From 026986cd93e1ba3c0fe41be5d18c1d94f2b879f3 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Wed, 5 Jul 2023 17:16:38 +0530 Subject: [PATCH] Add DAO improvements for user APIs Fixes https://roadmap.entgra.net/issues/10169 --- .../services/VPPApplicationManager.java | 4 + .../vpp/GenericVppApplicationDAOImpl.java | 76 +++++++++++++------ .../core/impl/VppApplicationManagerImpl.java | 16 +++- .../application/mgt/core/util/Constants.java | 2 +- .../application/mgt/core/util/DAOUtil.java | 25 +++++- .../main/resources/conf/application-mgt.xml | 1 + .../repository/conf/application-mgt.xml.j2 | 1 + 7 files changed, 93 insertions(+), 32 deletions(-) diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/VPPApplicationManager.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/VPPApplicationManager.java index f537764c62d..443aed228f8 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/VPPApplicationManager.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/VPPApplicationManager.java @@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.application.mgt.common.services; import io.entgra.device.mgt.core.application.mgt.common.dto.ProxyResponse; import io.entgra.device.mgt.core.application.mgt.common.dto.VppAssetDTO; +import io.entgra.device.mgt.core.application.mgt.common.dto.VppAssociationDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.VppUserDTO; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; @@ -44,4 +45,7 @@ public interface VPPApplicationManager { boolean addAssociation(VppAssetDTO asset, List vppUsers) throws ApplicationManagementException; + + VppAssociationDTO getUserAssociation(String adamId, String emmUsername) throws + ApplicationManagementException; } 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 13c26d75f60..3ec54872615 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 @@ -89,34 +89,61 @@ public class GenericVppApplicationDAOImpl extends AbstractDAOImpl implements Vp public VppUserDTO updateVppUser(VppUserDTO userDTO, int tenantId) 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 = ?"; + String sql = "UPDATE AP_VPP_USER SET "; + if (userDTO.getClientUserId() != null && !userDTO.getClientUserId().isEmpty()) { + sql += "CLIENT_USER_ID = ?,"; + } + if (userDTO.getDmUsername() != null && !userDTO.getDmUsername().isEmpty()) { + sql += "DM_USERNAME = ?,"; + } + if (userDTO.getEmail() != null && !userDTO.getEmail().isEmpty()) { + sql += "EMAIL = ?,"; + } + if (userDTO.getInviteCode() != null && !userDTO.getInviteCode().isEmpty()) { + sql += "INVITE_CODE = ?,"; + } + if (userDTO.getStatus() != null && !userDTO.getStatus().isEmpty()) { + sql += "STATUS = ?,"; + } + if (userDTO.getManagedId() != null && !userDTO.getManagedId().isEmpty()) { + sql += "MANAGED_ID = ?,"; + } + if (userDTO.getTmpPassword() != null && !userDTO.getTmpPassword().isEmpty()) { + sql += "TEMP_PASSWORD = ?,"; + } + + sql += " TENANT_ID = ?, LAST_UPDATED_TIME = ? 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, tenantId); - 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(); + int x = 0; + + if (userDTO.getClientUserId() != null && !userDTO.getClientUserId().isEmpty()) { + stmt.setString(++x, userDTO.getClientUserId()); + } + if (userDTO.getDmUsername() != null && !userDTO.getDmUsername().isEmpty()) { + stmt.setString(++x, userDTO.getDmUsername()); + } + if (userDTO.getEmail() != null && !userDTO.getEmail().isEmpty()) { + stmt.setString(++x, userDTO.getEmail()); + } + if (userDTO.getInviteCode() != null && !userDTO.getInviteCode().isEmpty()) { + stmt.setString(++x, userDTO.getInviteCode()); + } + if (userDTO.getStatus() != null && !userDTO.getStatus().isEmpty()) { + stmt.setString(++x, userDTO.getStatus()); + } + if (userDTO.getManagedId() != null && !userDTO.getManagedId().isEmpty()) { + stmt.setString(++x, userDTO.getManagedId()); + } + if (userDTO.getTmpPassword() != null && !userDTO.getTmpPassword().isEmpty()) { + stmt.setString(++x, userDTO.getTmpPassword()); + } + stmt.setInt(++x, tenantId); + stmt.setLong(++x, updatedTime); + stmt.setInt(++x, userDTO.getId()); if (stmt.executeUpdate() == 1) { return userDTO; } @@ -147,6 +174,7 @@ public class GenericVppApplicationDAOImpl extends AbstractDAOImpl implements Vp + "LAST_UPDATED_TIME, " + "MANAGED_ID, " + "TEMP_PASSWORD " + + "DM_USERNAME " + "FROM AP_VPP_USER " + "WHERE DM_USERNAME = ? AND TENANT_ID = ?"; try { 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 b86ef9d2a3a..f6ab0d92a88 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 @@ -110,9 +110,6 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { wrapper.getUser().add(ituneUserDTO); Gson gson = new Gson(); -// Gson gson = new GsonBuilder() -// .setExclusionStrategies(new NullEmptyExclusionStrategy()) -// .create(); String userPayload = gson.toJson(wrapper); ProxyResponse proxyResponse = callVPPBackend(USER_CREATE, userPayload, TOKEN, Constants.VPP.POST); @@ -131,8 +128,12 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { log.error("userDTO " + userDTO.toString()); try { ConnectionManagerUtil.beginDBTransaction(); - if (vppApplicationDAO.addVppUser(userDTO, tenantId) != -1) { + int id = vppApplicationDAO.addVppUser(userDTO, tenantId); + if (id != -1) { ConnectionManagerUtil.commitDBTransaction(); + userDTO.setId(id); + userDTO.setTenantId(PrivilegedCarbonContext + .getThreadLocalCarbonContext().getTenantId()); return userDTO; } ConnectionManagerUtil.rollbackDBTransaction(); @@ -507,4 +508,11 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { return false; } + + @Override + public VppAssociationDTO getUserAssociation(String adamId, String emmUsername) throws ApplicationManagementException { + // Todo: Join the 3 tables and find a matching association + return null; + } + } 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/Constants.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/Constants.java index f689d568284..016fe414fa6 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/Constants.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/Constants.java @@ -196,7 +196,7 @@ public class Constants { public static final String PAID_SUB_METHOD = "PAID"; public static final String TYPE = "type";; public static final String PACKAGE_NAME = "packageName"; - public static final String APPLE_STORE_SYNCED_APP_CATEGORY = "APPLE_STORE_SYNCED_APP"; + public static final String APPLE_STORE_SYNCED_APP_CATEGORY = "AppleStoreSyncedApp"; public static final String RESULTS = "results"; public static final String ARTWORK = "artwork"; 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 f152d27f4f0..c5d9f259a63 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 @@ -362,9 +362,18 @@ public class DAOUtil { 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 (columnExist(rs,"STATUS")) { + vppUserDTO.setStatus(rs.getString("STATUS")); + } + if (columnExist(rs,"MANAGED_ID")) { + vppUserDTO.setManagedId(rs.getString("MANAGED_ID")); + } + if (columnExist(rs,"TEMP_PASSWORD")) { + vppUserDTO.setTmpPassword(rs.getString("TEMP_PASSWORD")); + } + if (columnExist(rs,"DM_USERNAME")) { + vppUserDTO.setDmUsername(rs.getString("DM_USERNAME")); + } if (rs.getLong("CREATED_TIME") != 0) { vppUserDTO.setCreatedTime(new Date(rs.getLong(("CREATED_TIME")) * 1000).toString()); } @@ -376,6 +385,16 @@ public class DAOUtil { return vppUserDTOS; } + private static boolean columnExist(ResultSet rs, String column){ + try{ + rs.findColumn(column); + return true; + } catch (SQLException sqlex){ + } + + return false; + } + public static VppAssetDTO loadAsset(ResultSet rs) throws SQLException, UnexpectedServerErrorException { List vppAssetDTOS = loadAssets(rs); if (vppAssetDTOS.isEmpty()) { diff --git a/features/application-mgt/io.entgra.device.mgt.core.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml b/features/application-mgt/io.entgra.device.mgt.core.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml index 8913afa2b9b..bb1e6144acb 100644 --- a/features/application-mgt/io.entgra.device.mgt.core.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml +++ b/features/application-mgt/io.entgra.device.mgt.core.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml @@ -171,6 +171,7 @@ Video Players & Editors Weather GooglePlaySyncedApp + AppleStoreSyncedApp diff --git a/features/application-mgt/io.entgra.device.mgt.core.application.mgt.server.feature/src/main/resources/conf_templates/templates/repository/conf/application-mgt.xml.j2 b/features/application-mgt/io.entgra.device.mgt.core.application.mgt.server.feature/src/main/resources/conf_templates/templates/repository/conf/application-mgt.xml.j2 index cbd8c6f0c9b..bc4d34b0481 100644 --- a/features/application-mgt/io.entgra.device.mgt.core.application.mgt.server.feature/src/main/resources/conf_templates/templates/repository/conf/application-mgt.xml.j2 +++ b/features/application-mgt/io.entgra.device.mgt.core.application.mgt.server.feature/src/main/resources/conf_templates/templates/repository/conf/application-mgt.xml.j2 @@ -202,6 +202,7 @@ Video Players & Editors Weather GooglePlaySyncedApp + AppleStoreSyncedApp {% if application_mgt_conf.app_categories is defined %} {%- for app_category in application_mgt_conf.app_categories -%} {{app_category}}