From 0e5478fb5e0235c5282aeedfa852f1a78468ccdc Mon Sep 17 00:00:00 2001 From: osh Date: Wed, 5 Jul 2023 20:49:46 +0530 Subject: [PATCH 1/3] Add association DAO and fix meta issue fixes https://roadmap.entgra.net/issues/10186 --- .../services/VPPApplicationManager.java | 4 + .../mgt/core/config/Extension.java | 3 +- .../mgt/core/dao/VppApplicationDAO.java | 7 + .../vpp/GenericVppApplicationDAOImpl.java | 122 ++++++++++++++ .../core/impl/VppApplicationManagerImpl.java | 156 ++++++++++++++---- .../application/mgt/core/util/APIUtil.java | 25 +++ .../core/util/ApplicationManagementUtil.java | 9 + .../application/mgt/core/util/DAOUtil.java | 31 ++++ 8 files changed, 323 insertions(+), 34 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..05f30909494 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 getAssociation(int assetId, int userId) 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/config/Extension.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/config/Extension.java index 553ee671723..9ded8d3608e 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/config/Extension.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/config/Extension.java @@ -88,7 +88,8 @@ public class Extension { SubscriptionManager, VisibilityManager, ApplicationStorageManager, - PlatformStorageManager + PlatformStorageManager, + MetadataManagementService } } 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 df84ed1e270..02aec95174f 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 @@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.application.mgt.core.dao; 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.core.exception.ApplicationManagementDAOException; @@ -36,4 +37,10 @@ public interface VppApplicationDAO { int addAsset(VppAssetDTO vppAssetDTO, int tenantId) throws ApplicationManagementDAOException; VppAssetDTO updateAsset(VppAssetDTO vppAssetDTO, int tenantId) throws ApplicationManagementDAOException; + + VppAssociationDTO getAssociation(int assetId, int userId, int tenantId) throws ApplicationManagementDAOException; + + int addAssociation(VppAssociationDTO vppAssociationDTO, int tenantId) throws ApplicationManagementDAOException; + + VppAssociationDTO updateAssociation(VppAssociationDTO vppAssociationDTO, 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 13c26d75f60..212a6d4baf1 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 @@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.application.mgt.core.dao.impl.vpp; 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.DBConnectionException; import io.entgra.device.mgt.core.application.mgt.core.dao.VppApplicationDAO; @@ -324,4 +325,125 @@ public class GenericVppApplicationDAOImpl extends AbstractDAOImpl implements Vp throw new ApplicationManagementDAOException(msg, e); } } + + @Override + public VppAssociationDTO getAssociation(int assetId, int userId, int tenantId) + throws ApplicationManagementDAOException { + String sql = "SELECT " + + "ID, " + + "ASSOCIATION_TYPE, " + + "CREATED_TIME, " + + "LAST_UPDATED_TIME, " + + "PRICING_PARAMS " + + "FROM AP_VPP_ASSOCIATION " + + "WHERE ASSET_ID = ? AND USER_ID = ? AND TENANT_ID = ?"; + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, assetId); + stmt.setInt(2, userId); + stmt.setInt(3, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + return DAOUtil.loadAssignment(rs); + } + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining database connection when retrieving assignment data of user with id "+ userId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred when processing SQL to retrieve assignment by asset id and user id."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (UnexpectedServerErrorException e) { + String msg = "Found more than one assignment for user id: " + userId + " and asset id: " + assetId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + + @Override + public int addAssociation(VppAssociationDTO vppAssociationDTO, int tenantId) + throws ApplicationManagementDAOException { + int associationId = -1; + String sql = "INSERT INTO " + + "AP_VPP_ASSOCIATION(" + + "ASSET_ID, " + + "USER_ID, " + + "TENANT_ID, " + + "ASSOCIATION_TYPE," + + "CREATED_TIME," + + "LAST_UPDATED_TIME," + + "PRICING_PARAMS) " + + "VALUES (?, ?, ?, ?, ?)"; + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { + long currentTime = System.currentTimeMillis(); + stmt.setInt(1, vppAssociationDTO.getAssetId()); + stmt.setInt(2, vppAssociationDTO.getClientId()); + stmt.setInt(3, tenantId); + stmt.setString(4, vppAssociationDTO.getAssociationType()); + stmt.setLong(5, currentTime); + stmt.setLong(6, currentTime); + stmt.setString(7, vppAssociationDTO.getPricingParam()); + stmt.executeUpdate(); + try (ResultSet rs = stmt.getGeneratedKeys()) { + if (rs.next()) { + associationId = rs.getInt(1); + } + } + return associationId; + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining database connection when adding the asset."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred when processing SQL to add the asset."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + + @Override + public VppAssociationDTO updateAssociation(VppAssociationDTO vppAssociationDTO, int tenantId) + throws ApplicationManagementDAOException { + + String sql = "UPDATE " + + "AP_VPP_ASSOCIATION " + + "SET " + + "ASSET_ID = ?," + + "USER_ID = ?, " + + "ASSOCIATION_TYPE = ?, " + + "LAST_UPDATED_TIME = ?, " + + "PRICING_PARAMS = ? " + + "WHERE ID = ? AND TENANT_ID = ?"; + try { + Connection conn = this.getDBConnection(); + long updatedTime = System.currentTimeMillis(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, vppAssociationDTO.getAssetId()); + stmt.setInt(2, vppAssociationDTO.getClientId()); + stmt.setString(3, vppAssociationDTO.getAssociationType()); + stmt.setLong(4, updatedTime); + stmt.setString(5, vppAssociationDTO.getPricingParam()); + stmt.setInt(6, vppAssociationDTO.getId()); + stmt.setLong(7, tenantId); + stmt.executeUpdate(); + if (stmt.executeUpdate() == 1) { + return vppAssociationDTO; + } + 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); + } + } } 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..d1a0af7cf4f 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 @@ -50,6 +50,7 @@ import io.entgra.device.mgt.core.application.mgt.core.util.ApplicationManagement 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 io.entgra.device.mgt.core.application.mgt.core.util.APIUtil; import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService; @@ -71,6 +72,7 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { private static final String USER_UPDATE = APP_API + "/users/update"; private static final String USER_GET = APP_API + "/users"; private static final String ASSIGNMENTS_POST = APP_API + "/assets/associate"; + private static final String ASSIGNMENTS_GET = APP_API + "/assignments"; private static final String TOKEN = ""; private static final String LOOKUP_API = "https://uclient-api.itunes.apple" + ".com/WebObjects/MZStorePlatform.woa/wa/lookup?version=2&id="; @@ -285,10 +287,10 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { VppAssetDTO vppAssetDTOs = getAssetByAppId(application.getId()); if (vppAssetDTOs == null) { vppAssetDTOs = new VppAssetDTO(); - vppAssetDTOs.setAppId(application.getId()); + vppAssetDTO.setAppId(application.getId()); try { ConnectionManagerUtil.beginDBTransaction(); - if (vppApplicationDAO.addAsset(vppAssetDTOs, tenantId) != -1) { + if (vppApplicationDAO.addAsset(vppAssetDTO, tenantId) != -1) { ConnectionManagerUtil.commitDBTransaction(); } ConnectionManagerUtil.rollbackDBTransaction(); @@ -413,7 +415,7 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { return null; } - + @Override public VppAssetDTO getAssetByAppId(int appId) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { @@ -433,40 +435,12 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { } @Override - public ProxyResponse callVPPBackend(String url, - String payload, - String accessToken, - String method) throws IOException { - return VppHttpUtil.execute(url, payload, accessToken, method); - } - - public String getVppToken() throws ApplicationManagementException { - String token = ""; - MetadataManagementService meta = DeviceManagementDataHolder - .getInstance().getMetadataManagementService(); - Metadata metadata = null; - try { - metadata = meta.retrieveMetadata("DEP_META_KEY"); - if (metadata != null) { - - Gson g = new Gson(); - DepConfig depConfigs = g.fromJson(metadata.getMetaValue(), DepConfig.class); - token = depConfigs.getAccessToken(); - return token; - } - }catch (MetadataManagementException e) { - String msg = "Error when retrieving metadata of vpp feature"; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } - return token; - } - public boolean addAssociation(VppAssetDTO asset, List vppUsers) throws ApplicationManagementException { List associations = new ArrayList<>(); // To save to UEM DBs List clientUserIds = new ArrayList<>(); // Need this to send to vpp backend. + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); if (asset != null) { for (VppUserDTO vppUserDTO : vppUsers) { VppAssociationDTO associationDTO = VppHttpUtil.getAssociation(vppUserDTO, asset); @@ -494,7 +468,75 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { ProxyResponse proxyResponse = callVPPBackend(ASSIGNMENTS_POST, payload, TOKEN, Constants.VPP.POST); - return true; + 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); + if ((getAssignmentResponse.getCode() == HttpStatus.SC_OK || getAssignmentResponse.getCode() == + HttpStatus.SC_CREATED) && getAssignmentResponse.getData().contains(Constants.VPP.TOTAL_PAGES)) { +// VppAssociateResponseWrapper vppAssociateResponseWrapper = gson.fromJson +// (getAssignmentResponse.getData(), VppAssociateResponseWrapper.class); + for (VppAssociationDTO association : associations) { + + VppAssociationDTO vppAssociation = getAssociation(association.getAssetId(), association.getClientId()); + + if (vppAssociation == null) { + try { + ConnectionManagerUtil.beginDBTransaction(); + if (vppApplicationDAO.addAssociation(association, tenantId) != -1) { + ConnectionManagerUtil.commitDBTransaction(); + return true; + } + ConnectionManagerUtil.rollbackDBTransaction(); + return false; + } catch (ApplicationManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Error occurred while adding the Assignment."; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while executing database transaction for adding Assignment."; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (DBConnectionException e) { + String msg = "Error occurred while retrieving the database connection for adding Assignment."; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } else { + try { + ConnectionManagerUtil.beginDBTransaction(); + if (vppApplicationDAO.updateAssociation(association, tenantId) == null) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Unable to update the assignment: " +association.getAssetId(); + log.error(msg); + throw new ApplicationManagementException(msg); + } + ConnectionManagerUtil.commitDBTransaction(); + return true; + } catch (ApplicationManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Error occurred while updating the Asset."; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while executing database transaction for Asset update."; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (DBConnectionException e) { + String msg = "Error occurred while retrieving the database connection for Asset update."; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + } + } + + } } catch (IOException e) { String msg = "Error while adding associations"; @@ -507,4 +549,52 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { return false; } + + @Override + public VppAssociationDTO getAssociation(int assetId, int userId) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + try { + ConnectionManagerUtil.openDBConnection(); + return vppApplicationDAO.getAssociation(assetId, userId, tenantId); + } catch (DBConnectionException e) { + String msg = "DB Connection error occurs while getting assignment related to user of id " + userId + "."; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred while getting assignment data related to user of id " + userId + "."; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @Override + public ProxyResponse callVPPBackend(String url, + String payload, + String accessToken, + String method) throws IOException { + return VppHttpUtil.execute(url, payload, accessToken, method); + } + + public String getVppToken() throws ApplicationManagementException { + String token = ""; + MetadataManagementService meta = APIUtil.getMetadataManager(); + Metadata metadata = null; + try { + metadata = meta.retrieveMetadata("DEP_META_KEY"); + if (metadata != null) { + + Gson g = new Gson(); + DepConfig depConfigs = g.fromJson(metadata.getMetaValue(), DepConfig.class); + token = depConfigs.getAccessToken(); + return token; + } + }catch (MetadataManagementException e) { + String msg = "Error when retrieving metadata of vpp feature"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } + return token; + } } 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/APIUtil.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/APIUtil.java index 5a769742888..2e8536feb87 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/APIUtil.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/APIUtil.java @@ -26,6 +26,8 @@ import io.entgra.device.mgt.core.application.mgt.core.config.IdentityServiceProv import io.entgra.device.mgt.core.application.mgt.core.serviceprovider.ISServiceProviderApplicationService; import io.entgra.device.mgt.core.application.mgt.core.exception.BadRequestException; import io.entgra.device.mgt.core.application.mgt.core.exception.UnexpectedServerErrorException; +import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService; +import io.entgra.device.mgt.core.device.mgt.core.internal.DeviceManagementDataHolder; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -72,6 +74,7 @@ public class APIUtil { private static volatile ReviewManager reviewManager; private static volatile AppmDataHandler appmDataHandler; private static volatile VPPApplicationManager vppApplicationManager; + private static volatile MetadataManagementService metadataManagementService; public static SPApplicationManager getSPApplicationManager() { if (SPApplicationManager == null) { @@ -113,6 +116,28 @@ public class APIUtil { return applicationManager; } + public static MetadataManagementService getMetadataManager() { + try { + if (metadataManagementService == null) { + synchronized (APIUtil.class) { + if (metadataManagementService == null) { + metadataManagementService = ApplicationManagementUtil.getDeviceManagerInstance(); + if (metadataManagementService == null) { + String msg = "MetadataManagement Service service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + } + } + } + } catch (Exception e) { + String msg = "Error occurred while getting the vpp manager"; + log.error(msg); + throw new IllegalStateException(msg); + } + return metadataManagementService; + } + /** * To get the ApplicationDTO Storage Manager from the osgi context. * @return ApplicationStoreManager instance in the current osgi context. 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/ApplicationManagementUtil.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/ApplicationManagementUtil.java index 3ae13bae1f4..d739f0f68a8 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/ApplicationManagementUtil.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/ApplicationManagementUtil.java @@ -49,7 +49,9 @@ import io.entgra.device.mgt.core.application.mgt.core.impl.VppApplicationManager import io.entgra.device.mgt.core.application.mgt.core.lifecycle.LifecycleStateManager; import io.entgra.device.mgt.core.device.mgt.common.Base64File; import io.entgra.device.mgt.core.device.mgt.common.DeviceManagementConstants; +import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService; import io.entgra.device.mgt.core.device.mgt.core.common.util.FileUtil; +import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.MetadataManagementServiceImpl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -209,6 +211,13 @@ public class ApplicationManagementUtil { return new VppApplicationManagerImpl(); } + public static MetadataManagementService getDeviceManagerInstance() throws InvalidConfigurationException { + ConfigurationManager configurationManager = ConfigurationManager.getInstance(); + Extension extension = configurationManager.getExtension(Extension.Name.MetadataManagementService); + return getInstance(extension, MetadataManagementService.class); + } + + /** * This is useful to delete application artifacts if any error occurred while creating release/application * after uploading the artifacts 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..73df7fbe53b 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 @@ -422,6 +422,37 @@ public class DAOUtil { return vppAssetDTOS; } + public static VppAssociationDTO loadAssignment(ResultSet rs) throws SQLException, UnexpectedServerErrorException { + List vppAssociationDTOS = loadAssignments(rs); + if (vppAssociationDTOS.isEmpty()) { + return null; + } + if (vppAssociationDTOS.size() > 1) { + String msg = "Internal server error. Found more than one asset for given app id."; + log.error(msg); + throw new UnexpectedServerErrorException(msg); + } + return vppAssociationDTOS.get(0); + } + + public static List loadAssignments (ResultSet rs) throws SQLException { + List vppAssociationDTOS = new ArrayList<>(); + while (rs.next()) { + VppAssociationDTO vppAssociationDTO = new VppAssociationDTO(); + 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()); + } + if (rs.getLong("LAST_UPDATED_TIME") != 0) { + vppAssociationDTO.setLastUpdatedTime(new Date(rs.getLong(("LAST_UPDATED_TIME")) * 1000).toString()); + } + vppAssociationDTO.setPricingParam(rs.getString("PRICING_PARAMS")); + vppAssociationDTOS.add(vppAssociationDTO); + } + return vppAssociationDTOS; + } + /** * Cleans up the statement and resultset after executing the query * From dae8c0976155f4069c3e28824092e0dce409a936 Mon Sep 17 00:00:00 2001 From: osh Date: Wed, 5 Jul 2023 22:38:10 +0530 Subject: [PATCH 2/3] Resolve comments --- .../mgt/core/config/Extension.java | 3 +-- .../vpp/GenericVppApplicationDAOImpl.java | 1 - .../application/mgt/core/util/APIUtil.java | 23 ++++++++----------- .../core/util/ApplicationManagementUtil.java | 8 ------- 4 files changed, 10 insertions(+), 25 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/config/Extension.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/config/Extension.java index 9ded8d3608e..553ee671723 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/config/Extension.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/config/Extension.java @@ -88,8 +88,7 @@ public class Extension { SubscriptionManager, VisibilityManager, ApplicationStorageManager, - PlatformStorageManager, - MetadataManagementService + PlatformStorageManager } } 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 1e935d35de4..936e8da7e3d 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 @@ -458,7 +458,6 @@ public class GenericVppApplicationDAOImpl extends AbstractDAOImpl implements Vp stmt.setString(5, vppAssociationDTO.getPricingParam()); stmt.setInt(6, vppAssociationDTO.getId()); stmt.setLong(7, tenantId); - stmt.executeUpdate(); if (stmt.executeUpdate() == 1) { return vppAssociationDTO; } 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/APIUtil.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/APIUtil.java index 2e8536feb87..d37a85dee6a 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/APIUtil.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/APIUtil.java @@ -27,7 +27,6 @@ import io.entgra.device.mgt.core.application.mgt.core.serviceprovider.ISServiceP import io.entgra.device.mgt.core.application.mgt.core.exception.BadRequestException; import io.entgra.device.mgt.core.application.mgt.core.exception.UnexpectedServerErrorException; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService; -import io.entgra.device.mgt.core.device.mgt.core.internal.DeviceManagementDataHolder; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -117,23 +116,19 @@ public class APIUtil { } public static MetadataManagementService getMetadataManager() { - try { - if (metadataManagementService == null) { - synchronized (APIUtil.class) { + if (metadataManagementService == null) { + synchronized (APIUtil.class) { + if (metadataManagementService == null) { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + metadataManagementService = + (MetadataManagementService) ctx.getOSGiService(MetadataManagementService.class, null); if (metadataManagementService == null) { - metadataManagementService = ApplicationManagementUtil.getDeviceManagerInstance(); - if (metadataManagementService == null) { - String msg = "MetadataManagement Service service has not initialized."; - log.error(msg); - throw new IllegalStateException(msg); - } + String msg = "MetadataManagement Manager service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); } } } - } catch (Exception e) { - String msg = "Error occurred while getting the vpp manager"; - log.error(msg); - throw new IllegalStateException(msg); } return metadataManagementService; } 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/ApplicationManagementUtil.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/ApplicationManagementUtil.java index d739f0f68a8..36920d013c1 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/ApplicationManagementUtil.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/ApplicationManagementUtil.java @@ -210,14 +210,6 @@ public class ApplicationManagementUtil { // TODO: implement as an extension return new VppApplicationManagerImpl(); } - - public static MetadataManagementService getDeviceManagerInstance() throws InvalidConfigurationException { - ConfigurationManager configurationManager = ConfigurationManager.getInstance(); - Extension extension = configurationManager.getExtension(Extension.Name.MetadataManagementService); - return getInstance(extension, MetadataManagementService.class); - } - - /** * This is useful to delete application artifacts if any error occurred while creating release/application * after uploading the artifacts From 81fd5687ef74fedfc9b6b1e88678d82b7ce23283 Mon Sep 17 00:00:00 2001 From: osh Date: Sun, 9 Jul 2023 00:50:57 +0530 Subject: [PATCH 3/3] 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);