From 81fd5687ef74fedfc9b6b1e88678d82b7ce23283 Mon Sep 17 00:00:00 2001 From: osh Date: Sun, 9 Jul 2023 00:50:57 +0530 Subject: [PATCH] Update user service and token retrieval --- .../core/impl/VppApplicationManagerImpl.java | 70 ++++++++++--------- .../application/mgt/core/util/DAOUtil.java | 31 ++++++-- 2 files changed, 61 insertions(+), 40 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/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 9ecc31ee468..f250af7a6bb 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 @@ -114,12 +114,12 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { Gson gson = new Gson(); String userPayload = gson.toJson(wrapper); - ProxyResponse proxyResponse = callVPPBackend(USER_CREATE, userPayload, TOKEN, Constants.VPP.POST); + ProxyResponse proxyResponse = callVPPBackend(USER_CREATE, userPayload, getVppToken(), Constants.VPP.POST); if ((proxyResponse.getCode() == HttpStatus.SC_OK || proxyResponse.getCode() == HttpStatus.SC_CREATED) && proxyResponse.getData().contains(Constants.VPP.EVENT_ID)) { // Create user does not return any useful data. Its needed to call the backend again ProxyResponse getUserResponse = callVPPBackend(USER_GET + Constants.VPP.CLIENT_USER_ID_PARAM + - userDTO.getClientUserId(), userPayload, TOKEN, Constants.VPP.GET); + userDTO.getClientUserId(), userPayload, getVppToken(), Constants.VPP.GET); if ((getUserResponse.getCode() == HttpStatus.SC_OK || getUserResponse.getCode() == HttpStatus.SC_CREATED) && getUserResponse.getData().contains(Constants.VPP.TOTAL_PAGES)) { VppItuneUserResponseWrapper vppItuneUserResponseWrapper = gson.fromJson @@ -196,38 +196,40 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { Gson gson = new Gson(); String userPayload = gson.toJson(wrapper); try { - ProxyResponse proxyResponse = callVPPBackend(USER_UPDATE, userPayload, TOKEN, Constants.VPP.POST); + ProxyResponse proxyResponse = callVPPBackend(USER_UPDATE, userPayload, getVppToken(), Constants.VPP.POST); if ((proxyResponse.getCode() == HttpStatus.SC_OK || proxyResponse.getCode() == HttpStatus.SC_CREATED) && proxyResponse.getData().contains(Constants.VPP.EVENT_ID)) { - - log.error("userDTO " + userDTO.toString()); - - try { - ConnectionManagerUtil.beginDBTransaction(); - if (vppApplicationDAO.updateVppUser(userDTO, tenantId) == null) { + VppUserDTO currentUserDTO = getUserByDMUsername(userDTO.getDmUsername()); + if (currentUserDTO != null) { + userDTO.setId(currentUserDTO.getId()); + } + try { + ConnectionManagerUtil.beginDBTransaction(); + if (vppApplicationDAO.updateVppUser(userDTO, tenantId) == 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 = "Unable to update the Vpp user " +userDTO.getId(); - log.error(msg); - throw new ApplicationManagementException(msg); + 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(); } - 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 calling VPP backend to update"; log.error(msg, e); @@ -239,7 +241,7 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { public void syncUsers(String clientId) throws ApplicationManagementException { ProxyResponse proxyResponse = null; try { - proxyResponse = callVPPBackend(USER_GET, null, TOKEN, Constants + proxyResponse = callVPPBackend(USER_GET, null, getVppToken(), Constants .VPP.GET); if ((proxyResponse.getCode() == HttpStatus.SC_OK || proxyResponse.getCode() == HttpStatus.SC_CREATED) && proxyResponse.getData().contains(Constants.VPP.TOTAL_PAGES)) { @@ -266,7 +268,7 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { if (nextPageIndex > 0) { // Not the first page url += "?pageIndex=" + nextPageIndex; } - proxyResponse = callVPPBackend(url, null, TOKEN, Constants.VPP.GET); + proxyResponse = callVPPBackend(url, null, getVppToken(), Constants.VPP.GET); if ((proxyResponse.getCode() == HttpStatus.SC_OK || proxyResponse.getCode() == HttpStatus.SC_CREATED) && proxyResponse.getData().contains(Constants.VPP.TOTAL_PAGES)) { Gson gson = new Gson(); @@ -360,7 +362,7 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { private ItuneAppDTO lookupAsset(String packageName) throws ApplicationManagementException { String lookupURL = LOOKUP_API + packageName + LOOKUP_API_PREFIX; try { - ProxyResponse proxyResponse = callVPPBackend(lookupURL, null, TOKEN, Constants.VPP.GET); + ProxyResponse proxyResponse = callVPPBackend(lookupURL, null, getVppToken(), Constants.VPP.GET); if ((proxyResponse.getCode() == HttpStatus.SC_OK || proxyResponse.getCode() == HttpStatus.SC_CREATED) && proxyResponse.getData().contains(Constants.VPP.GET_APP_DATA_RESPONSE_START)) { String responseData = proxyResponse.getData(); @@ -467,12 +469,12 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { Gson gson = new Gson(); String payload = gson.toJson(vppAssociate); - ProxyResponse proxyResponse = callVPPBackend(ASSIGNMENTS_POST, payload, TOKEN, + ProxyResponse proxyResponse = callVPPBackend(ASSIGNMENTS_POST, payload, getVppToken(), Constants.VPP.POST); if ((proxyResponse.getCode() == HttpStatus.SC_OK || proxyResponse.getCode() == HttpStatus.SC_CREATED) && proxyResponse.getData().contains(Constants.VPP.EVENT_ID)) { // Create assignment does not return any useful data. Its needed to call the backend again - ProxyResponse getAssignmentResponse = callVPPBackend(ASSIGNMENTS_GET, null, TOKEN, Constants.VPP.GET); + ProxyResponse getAssignmentResponse = callVPPBackend(ASSIGNMENTS_GET, null, getVppToken(), Constants.VPP.GET); if ((getAssignmentResponse.getCode() == HttpStatus.SC_OK || getAssignmentResponse.getCode() == HttpStatus.SC_CREATED) && getAssignmentResponse.getData().contains(Constants.VPP.TOTAL_PAGES)) { // VppAssociateResponseWrapper vppAssociateResponseWrapper = gson.fromJson 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 256a76bb0ce..1f497ea5ec7 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 @@ -33,6 +33,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; +import java.text.SimpleDateFormat; import java.time.Instant; import java.util.ArrayList; import java.util.HashMap; @@ -375,10 +376,16 @@ public class DAOUtil { vppUserDTO.setDmUsername(rs.getString("DM_USERNAME")); } if (rs.getLong("CREATED_TIME") != 0) { - vppUserDTO.setCreatedTime(new Date(rs.getLong(("CREATED_TIME")) * 1000).toString()); + Date date = new Date(rs.getLong("CREATED_TIME")); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String dateString = dateFormat.format(date); + vppUserDTO.setCreatedTime(dateString); } if (rs.getLong("LAST_UPDATED_TIME") != 0) { - vppUserDTO.setLastUpdatedTime(new Date(rs.getLong(("LAST_UPDATED_TIME")) * 1000).toString()); + Date date = new Date(rs.getLong("LAST_UPDATED_TIME")); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String dateString = dateFormat.format(date); + vppUserDTO.setLastUpdatedTime(dateString); } vppUserDTOS.add(vppUserDTO); } @@ -416,10 +423,16 @@ public class DAOUtil { vppAssetDTO.setAppId(rs.getInt("APP_ID")); vppAssetDTO.setTenantId(rs.getInt("TENANT_ID")); if (rs.getLong("CREATED_TIME") != 0) { - vppAssetDTO.setCreatedTime(new Date(rs.getLong(("CREATED_TIME")) * 1000).toString()); + Date date = new Date(rs.getLong("CREATED_TIME")); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String dateString = dateFormat.format(date); + vppAssetDTO.setCreatedTime(dateString); } if (rs.getLong("LAST_UPDATED_TIME") != 0) { - vppAssetDTO.setLastUpdatedTime(new Date(rs.getLong(("LAST_UPDATED_TIME")) * 1000).toString()); + Date date = new Date(rs.getLong("LAST_UPDATED_TIME")); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String dateString = dateFormat.format(date); + vppAssetDTO.setLastUpdatedTime(dateString); } vppAssetDTO.setAdamId(rs.getString("ADAM_ID")); vppAssetDTO.setAssignedCount(rs.getString("ASSIGNED_COUNT")); @@ -461,10 +474,16 @@ public class DAOUtil { vppAssociationDTO.setId(rs.getInt("ID")); vppAssociationDTO.setAssociationType(rs.getString("ASSOCIATION_TYPE")); if (rs.getLong("CREATED_TIME") != 0) { - vppAssociationDTO.setCreatedTime(new Date(rs.getLong(("CREATED_TIME")) * 1000).toString()); + Date date = new Date(rs.getLong("CREATED_TIME")); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String dateString = dateFormat.format(date); + vppAssociationDTO.setCreatedTime(dateString); } if (rs.getLong("LAST_UPDATED_TIME") != 0) { - vppAssociationDTO.setLastUpdatedTime(new Date(rs.getLong(("LAST_UPDATED_TIME")) * 1000).toString()); + Date date = new Date(rs.getLong("LAST_UPDATED_TIME")); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String dateString = dateFormat.format(date); + vppAssociationDTO.setLastUpdatedTime(dateString); } vppAssociationDTO.setPricingParam(rs.getString("PRICING_PARAMS")); vppAssociationDTOS.add(vppAssociationDTO);