From be287ebb769ced694f5df513ae05c7d236b43437 Mon Sep 17 00:00:00 2001 From: pramilaniroshan Date: Wed, 24 Apr 2024 10:16:02 +0530 Subject: [PATCH 1/7] Refactor device status filter management service --- .../DeviceStatusManagementServiceImpl.java | 157 +++++++++++------- 1 file changed, 94 insertions(+), 63 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java index 1daea9fd24..e5cfe0f230 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java @@ -26,6 +26,7 @@ import io.entgra.device.mgt.core.device.mgt.common.exceptions.TransactionManagem import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.AllowedDeviceStatus; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata; +import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService; import io.entgra.device.mgt.core.device.mgt.core.config.ui.DeviceStatusConfigurations; import io.entgra.device.mgt.core.device.mgt.core.config.ui.DeviceStatusItem; import io.entgra.device.mgt.core.device.mgt.core.config.ui.UIConfigurationManager; @@ -79,6 +80,52 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement } } + private void addDefaultDeviceStatusFilters(int tenantId) throws MetadataManagementException { + try { + MetadataManagementDAOFactory.beginTransaction(); + if (!metadataDAO.isExist(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY)) { + Metadata defaultDeviceStatusMetadata = constructDeviceStatusMetadata(getDefaultDeviceStatus()); + // Add default device status metadata entries + addMetadataEntry(tenantId, defaultDeviceStatusMetadata, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); + } + MetadataManagementDAOFactory.commitTransaction(); + } catch (MetadataManagementDAOException e) { + MetadataManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while inserting default device status metadata entry."; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } finally { + MetadataManagementDAOFactory.closeConnection(); + } + } + + private void addDefaultDeviceStatusCheck(int tenantId) throws MetadataManagementException { + try { + MetadataManagementDAOFactory.beginTransaction(); + if (!metadataDAO.isExist(tenantId, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY)) { + Metadata defaultDeviceStatusCheckMetadata = constructDeviceStatusCheckMetadata(getDefaultDeviceStatusCheck()); + // Add default device status check metadata entries + addMetadataEntry(tenantId, defaultDeviceStatusCheckMetadata, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY); + } + MetadataManagementDAOFactory.commitTransaction(); + } catch (MetadataManagementDAOException e) { + MetadataManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while inserting default device status metadata entry."; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } finally { + MetadataManagementDAOFactory.closeConnection(); + } + } + @Override public void resetToDefaultDeviceStausFilter() throws MetadataManagementException { @@ -174,84 +221,68 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement } public List getDeviceStatusFilters(String deviceType, int tenantId) throws MetadataManagementException { - try { - MetadataManagementDAOFactory.openConnection(); - Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); - Gson gson = new Gson(); - Type listType = new TypeToken>() {}.getType(); - List statusList = gson.fromJson(metadata.getMetaValue(), listType); + MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl(); + Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); + Gson gson = new Gson(); + String metaValue; + if (metadata != null) { + metaValue = metadata.getMetaValue(); + } else { + List statusList = getDefaultDeviceStatus(); + metaValue = gson.toJson(statusList); + addDefaultDeviceStatusFilters(tenantId); + } + Type listType = new TypeToken>() { + }.getType(); + List statusList = gson.fromJson(metaValue, listType); - for (AllowedDeviceStatus status : statusList) { - if (status.getType().equalsIgnoreCase(deviceType)) { - return status.getStatus(); - } + for (AllowedDeviceStatus status : statusList) { + if (status.getType().equalsIgnoreCase(deviceType)) { + return status.getStatus(); } - // Device type not found in metadata - return Collections.emptyList(); - } catch (MetadataManagementDAOException e) { - String msg = "Error occurred while retrieving device status meta data for tenant: " + tenantId; - log.error(msg, e); - throw new MetadataManagementException(msg, e); - } catch (SQLException e) { - String msg = "Error occurred while opening a connection to the data source"; - log.error(msg, e); - throw new MetadataManagementException(msg, e); - } finally { - MetadataManagementDAOFactory.closeConnection(); } + // Device type not found in metadata + return Collections.emptyList(); } @Override public boolean getDeviceStatusCheck(int tenantId) throws MetadataManagementException { - try { - MetadataManagementDAOFactory.openConnection(); - Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY); - String metaValue = metadata.getMetaValue(); - return Boolean.parseBoolean(metaValue); - } catch (MetadataManagementDAOException e) { - String msg = "Error occurred while retrieving device status check meta data for tenant:" + tenantId; - log.error(msg, e); - throw new MetadataManagementException(msg, e); - } catch (SQLException e) { - String msg = "Error occurred while opening a connection to the data source"; - log.error(msg, e); - throw new MetadataManagementException(msg, e); - } finally { - MetadataManagementDAOFactory.closeConnection(); + MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl(); + Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY); + if (metadata != null) { + return Boolean.parseBoolean(metadata.getMetaValue()); + } else { + addDefaultDeviceStatusCheck(tenantId); + return getDefaultDeviceStatusCheck(); } } @Override public boolean isDeviceStatusValid(String deviceType, String deviceStatus, int tenantId) throws MetadataManagementException { - try { - MetadataManagementDAOFactory.openConnection(); - Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); - - Gson gson = new Gson(); - Type listType = new TypeToken>() { - }.getType(); - List statusList = gson.fromJson(metadata.getMetaValue(), listType); - - for (AllowedDeviceStatus status : statusList) { - if (status.getType().equalsIgnoreCase(deviceType)) { - List allowedStatus = status.getStatus(); - return allowedStatus.contains(deviceStatus); - } + MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl(); + Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); + String metaValue; + Gson gson = new Gson(); + if (metadata != null) { + metaValue = metadata.getMetaValue(); + } else { + List statusList = getDefaultDeviceStatus(); + metaValue = gson.toJson(statusList); + addDefaultDeviceStatusFilters(tenantId); + } + Type listType = new TypeToken>() { + }.getType(); + List statusList = gson.fromJson(metaValue, listType); + + for (AllowedDeviceStatus status : statusList) { + if (status.getType().equalsIgnoreCase(deviceType)) { + List allowedStatus = status.getStatus(); + return allowedStatus.contains(deviceStatus); } - - return false; // Device type not found in metadata - } catch (MetadataManagementDAOException e) { - String msg = "Error occurred while retrieving device status meta data for tenant: " + tenantId; - log.error(msg, e); - throw new MetadataManagementException(msg, e); - } catch (SQLException e) { - String msg = "Error occurred while opening a connection to the data source"; - log.error(msg, e); - throw new MetadataManagementException(msg, e); - } finally { - MetadataManagementDAOFactory.closeConnection(); } + + return false; // Device type not found in metadata } private void addMetadataEntry(int tenantId, Metadata metadata, String key) throws MetadataManagementDAOException { -- 2.36.3 From eb6186ba280579013c56cf71705bd0b6035cf268 Mon Sep 17 00:00:00 2001 From: pramilaniroshan Date: Tue, 28 May 2024 13:25:54 +0530 Subject: [PATCH 2/7] Extract common metadata retrieval and parsing logic to a separate method - Removed code duplication by creating `retrieveAndParseMetadata` method. - Improved memory optimization by declaring Gson as a class-level variable. --- .../DeviceStatusManagementServiceImpl.java | 54 +++++++------------ 1 file changed, 19 insertions(+), 35 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java index e5cfe0f230..761d2b98fc 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java @@ -50,8 +50,11 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement private final MetadataDAO metadataDAO; + private final Gson gson; + public DeviceStatusManagementServiceImpl() { this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO(); + this.gson = new Gson(); } @Override @@ -138,7 +141,6 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement // Retrieve the current device status metadata Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); if (metadata != null) { - Gson gson = new Gson(); Type listType = new TypeToken>() { }.getType(); List currentStatusList = gson.fromJson(metadata.getMetaValue(), listType); @@ -198,54 +200,37 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement @Override public List getDeviceStatusFilters(int tenantId) throws MetadataManagementException { - try { - MetadataManagementDAOFactory.openConnection(); - Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); - Gson gson = new Gson(); - Type listType = new TypeToken>() {}.getType(); - List statusList = gson.fromJson(metadata.getMetaValue(), listType); + return retrieveAndParseMetadata(tenantId); + } - return statusList; - } catch (MetadataManagementDAOException e) { - String msg = "Error occurred while retrieving device status meta data for tenant:" + tenantId; - log.error(msg, e); - throw new MetadataManagementException(msg, e); - } catch (SQLException e) { - String msg = "Error occurred while opening a connection to the data source"; - log.error(msg, e); - throw new MetadataManagementException(msg, e); - } finally { - MetadataManagementDAOFactory.closeConnection(); + public List getDeviceStatusFilters(String deviceType, int tenantId) throws MetadataManagementException { + List statusList = retrieveAndParseMetadata(tenantId); + for (AllowedDeviceStatus status : statusList) { + if (status.getType().equalsIgnoreCase(deviceType)) { + return status.getStatus(); + } } - + // Device type not found in metadata + return Collections.emptyList(); } - public List getDeviceStatusFilters(String deviceType, int tenantId) throws MetadataManagementException { + private List retrieveAndParseMetadata(int tenantId) throws MetadataManagementException { MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl(); Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); - Gson gson = new Gson(); String metaValue; if (metadata != null) { metaValue = metadata.getMetaValue(); } else { - List statusList = getDefaultDeviceStatus(); - metaValue = gson.toJson(statusList); + List defaultStatusList = getDefaultDeviceStatus(); + metaValue = gson.toJson(defaultStatusList); addDefaultDeviceStatusFilters(tenantId); } - Type listType = new TypeToken>() { - }.getType(); - List statusList = gson.fromJson(metaValue, listType); - - for (AllowedDeviceStatus status : statusList) { - if (status.getType().equalsIgnoreCase(deviceType)) { - return status.getStatus(); - } - } - // Device type not found in metadata - return Collections.emptyList(); + Type listType = new TypeToken>() {}.getType(); + return gson.fromJson(metaValue, listType); } + @Override public boolean getDeviceStatusCheck(int tenantId) throws MetadataManagementException { MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl(); @@ -263,7 +248,6 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl(); Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); String metaValue; - Gson gson = new Gson(); if (metadata != null) { metaValue = metadata.getMetaValue(); } else { -- 2.36.3 From 8de7a00f39deac13d7e1c0fe80e71025851d94db Mon Sep 17 00:00:00 2001 From: pramilaniroshan Date: Mon, 1 Jul 2024 08:34:40 +0530 Subject: [PATCH 3/7] Add Java Doc comments --- .../DeviceStatusManagementServiceImpl.java | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java index 761d2b98fc..3d4d557a79 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java @@ -34,11 +34,11 @@ import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.MetadataDAO; import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOException; import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOFactory; import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.util.MetadataConstants; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.lang.reflect.Type; -import java.sql.SQLException; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -83,6 +83,12 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement } } + /** + * Adds default device status filters for a given tenant. + * + * @param tenantId the tenant ID for which to add the device status filters. + * @throws MetadataManagementException if an error occurs while adding the metadata or managing the transaction. + */ private void addDefaultDeviceStatusFilters(int tenantId) throws MetadataManagementException { try { MetadataManagementDAOFactory.beginTransaction(); @@ -106,6 +112,12 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement } } + /** + * Adds default device status filter enable or not for a given tenant. + * + * @param tenantId the tenant ID for which to add the device status filters. + * @throws MetadataManagementException if an error occurs while adding the metadata or managing the transaction. + */ private void addDefaultDeviceStatusCheck(int tenantId) throws MetadataManagementException { try { MetadataManagementDAOFactory.beginTransaction(); @@ -137,6 +149,9 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement @Override public void updateDefaultDeviceStatusFilters(int tenantId, String deviceType, List deviceStatus) throws MetadataManagementException { try { + if (StringUtils.isEmpty(deviceType)) { + throw new IllegalArgumentException("Device type must not be empty or null"); + } MetadataManagementDAOFactory.beginTransaction(); // Retrieve the current device status metadata Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); @@ -147,7 +162,7 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement // Find the status for the specified deviceType for (AllowedDeviceStatus status : currentStatusList) { - if (status.getType().equalsIgnoreCase(deviceType)) { + if (deviceType.equalsIgnoreCase(status.getType())) { // Update the status list for the specified deviceType status.setStatus(deviceStatus); break; @@ -206,7 +221,7 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement public List getDeviceStatusFilters(String deviceType, int tenantId) throws MetadataManagementException { List statusList = retrieveAndParseMetadata(tenantId); for (AllowedDeviceStatus status : statusList) { - if (status.getType().equalsIgnoreCase(deviceType)) { + if (deviceType.equalsIgnoreCase(status.getType())) { return status.getStatus(); } } @@ -214,6 +229,12 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement return Collections.emptyList(); } + /** + * Retrieves and parses the allowed device status metadata for the specified tenant. + * @param tenantId the tenant ID for which to retrieve and parse the metadata. + * @return a list of {@link AllowedDeviceStatus} objects parsed from the metadata. + * @throws MetadataManagementException if an error occurs while retrieving or parsing the metadata. + */ private List retrieveAndParseMetadata(int tenantId) throws MetadataManagementException { MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl(); Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); @@ -245,6 +266,9 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement @Override public boolean isDeviceStatusValid(String deviceType, String deviceStatus, int tenantId) throws MetadataManagementException { + if (StringUtils.isEmpty(deviceType)) { + throw new IllegalArgumentException("Device type must not be empty or null"); + } MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl(); Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); String metaValue; @@ -260,7 +284,7 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement List statusList = gson.fromJson(metaValue, listType); for (AllowedDeviceStatus status : statusList) { - if (status.getType().equalsIgnoreCase(deviceType)) { + if (deviceType.equalsIgnoreCase(status.getType())) { List allowedStatus = status.getStatus(); return allowedStatus.contains(deviceStatus); } @@ -290,7 +314,6 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement Metadata metadata = new Metadata(); metadata.setMetaKey(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); metadata.setMetaValue(deviceStatusItemsJsonString); - return metadata; } @@ -300,7 +323,6 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement Metadata metadata = new Metadata(); metadata.setMetaKey(MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY); metadata.setMetaValue(String.valueOf(deviceStatusCheck)); - return metadata; } @@ -316,7 +338,6 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement log.debug("DeviceStatusConfigurations is null."); } } - return deviceStatusItems; } -- 2.36.3 From 195c48c85ebc9c6976a8efaf9fc908f310ebba06 Mon Sep 17 00:00:00 2001 From: pramilaniroshan Date: Mon, 1 Jul 2024 09:21:24 +0530 Subject: [PATCH 4/7] Resolve comments --- .../metadata/mgt/DeviceStatusManagementServiceImpl.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java index 3d4d557a79..956e65d851 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java @@ -150,7 +150,9 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement public void updateDefaultDeviceStatusFilters(int tenantId, String deviceType, List deviceStatus) throws MetadataManagementException { try { if (StringUtils.isEmpty(deviceType)) { - throw new IllegalArgumentException("Device type must not be empty or null"); + String msg = "Device type must not be empty or null"; + log.error(msg); + throw new IllegalArgumentException(msg); } MetadataManagementDAOFactory.beginTransaction(); // Retrieve the current device status metadata @@ -267,7 +269,9 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement @Override public boolean isDeviceStatusValid(String deviceType, String deviceStatus, int tenantId) throws MetadataManagementException { if (StringUtils.isEmpty(deviceType)) { - throw new IllegalArgumentException("Device type must not be empty or null"); + String msg = "Device type must not be empty or null"; + log.error(msg); + throw new IllegalArgumentException(msg); } MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl(); Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); -- 2.36.3 From 3bf10445a9a0799ff6260709a352505539e1990e Mon Sep 17 00:00:00 2001 From: pramilaniroshan Date: Wed, 3 Jul 2024 13:27:08 +0530 Subject: [PATCH 5/7] Implement Singleton Pattern for DeviceStatusManagementServiceImpl --- .../internal/DeviceManagementServiceComponent.java | 2 +- .../mgt/DeviceStatusManagementServiceImpl.java | 11 ++++++++++- .../core/device/mgt/core/search/DeviceDetails.java | 2 +- .../mgt/core/internal/TenantMgtServiceComponent.java | 2 +- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java index b2e1529f40..6bdaa5fee5 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -383,7 +383,7 @@ public class DeviceManagementServiceComponent { bundleContext.registerService(WhiteLabelManagementService.class.getName(), whiteLabelManagementService, null); /* Registering DeviceState Filter Service */ - DeviceStatusManagementService deviceStatusManagemntService = new DeviceStatusManagementServiceImpl(); + DeviceStatusManagementService deviceStatusManagemntService = DeviceStatusManagementServiceImpl.getInstance(); DeviceManagementDataHolder.getInstance().setDeviceStatusManagementService(deviceStatusManagemntService); try { deviceStatusManagemntService.addDefaultDeviceStatusFilterIfNotExist(tenantId); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java index 956e65d851..10ffe750f5 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java @@ -48,15 +48,24 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement private static final Log log = LogFactory.getLog(DeviceStatusManagementServiceImpl.class); + private static DeviceStatusManagementServiceImpl instance; + private final MetadataDAO metadataDAO; private final Gson gson; - public DeviceStatusManagementServiceImpl() { + private DeviceStatusManagementServiceImpl() { this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO(); this.gson = new Gson(); } + public static synchronized DeviceStatusManagementServiceImpl getInstance() { + if (instance == null) { + instance = new DeviceStatusManagementServiceImpl(); + } + return instance; + } + @Override public void addDefaultDeviceStatusFilterIfNotExist(int tenantId) throws MetadataManagementException { try { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/search/DeviceDetails.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/search/DeviceDetails.java index 8d6a4a5673..a7d3a06eb8 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/search/DeviceDetails.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/search/DeviceDetails.java @@ -43,7 +43,7 @@ public class DeviceDetails extends BaseDeviceManagementTest { DeviceManagementProviderService deviceManagementProviderService = new DeviceManagementProviderServiceImpl(); DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceManagementProviderService); - DeviceStatusManagementService deviceStatusManagementService = new DeviceStatusManagementServiceImpl(); + DeviceStatusManagementService deviceStatusManagementService = DeviceStatusManagementServiceImpl.getInstance(); DeviceManagementDataHolder.getInstance().setDeviceStatusManagementService(deviceStatusManagementService); } diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java index fa2f26c972..10d02e699a 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java @@ -66,7 +66,7 @@ public class TenantMgtServiceComponent { componentContext.getBundleContext().registerService(WhiteLabelManagementServiceImpl.class.getName(), whiteLabelManagementService, null); TenantMgtDataHolder.getInstance().setWhiteLabelManagementService(whiteLabelManagementService); - DeviceStatusManagementService deviceStatusManagementService = new DeviceStatusManagementServiceImpl(); + DeviceStatusManagementService deviceStatusManagementService = DeviceStatusManagementServiceImpl.getInstance(); componentContext.getBundleContext().registerService(DeviceStatusManagementService.class.getName(), deviceStatusManagementService, null); TenantMgtDataHolder.getInstance().setDeviceStatusManagementService(deviceStatusManagementService); -- 2.36.3 From 9e364ddb972b18faab228cd9a2a48bc14eeb2423 Mon Sep 17 00:00:00 2001 From: pramilaniroshan Date: Wed, 3 Jul 2024 13:39:09 +0530 Subject: [PATCH 6/7] Add caching layer for DeviceStatusManagementServiceImpl --- .../DeviceStatusManagementServiceImpl.java | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java index 10ffe750f5..b8b92e0f26 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java @@ -18,6 +18,8 @@ package io.entgra.device.mgt.core.device.mgt.core.metadata.mgt; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; @@ -42,6 +44,7 @@ import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.concurrent.TimeUnit; public class DeviceStatusManagementServiceImpl implements DeviceStatusManagementService { @@ -54,6 +57,10 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement private final Gson gson; + private static final Cache> cache = CacheBuilder.newBuilder() + .expireAfterWrite(5, TimeUnit.HOURS) + .build(); + private DeviceStatusManagementServiceImpl() { this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO(); this.gson = new Gson(); @@ -282,27 +289,29 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement log.error(msg); throw new IllegalArgumentException(msg); } - MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl(); - Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); - String metaValue; - if (metadata != null) { - metaValue = metadata.getMetaValue(); - } else { - List statusList = getDefaultDeviceStatus(); - metaValue = gson.toJson(statusList); - addDefaultDeviceStatusFilters(tenantId); + List allowedDeviceStatus = cache.getIfPresent(Integer.toString(tenantId)); + if (allowedDeviceStatus == null) { + MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl(); + Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); + String metaValue; + if (metadata != null) { + metaValue = metadata.getMetaValue(); + } else { + List statusList = getDefaultDeviceStatus(); + metaValue = gson.toJson(statusList); + addDefaultDeviceStatusFilters(tenantId); + } + Type listType = new TypeToken>() { + }.getType(); + allowedDeviceStatus = gson.fromJson(metaValue, listType); + cache.put(Integer.toString(tenantId), allowedDeviceStatus); } - Type listType = new TypeToken>() { - }.getType(); - List statusList = gson.fromJson(metaValue, listType); - - for (AllowedDeviceStatus status : statusList) { + for (AllowedDeviceStatus status : allowedDeviceStatus) { if (deviceType.equalsIgnoreCase(status.getType())) { List allowedStatus = status.getStatus(); return allowedStatus.contains(deviceStatus); } } - return false; // Device type not found in metadata } -- 2.36.3 From b991701d508d37be8f3274deb5932cd65f8ef845 Mon Sep 17 00:00:00 2001 From: pramilaniroshan Date: Mon, 8 Jul 2024 06:31:02 +0530 Subject: [PATCH 7/7] Resolve comments --- .../impl/DeviceStatusFilterServiceImpl.java | 6 +- .../mgt/DeviceStatusManagementService.java | 8 +- .../DeviceManagementServiceComponent.java | 2 +- .../DeviceStatusManagementServiceImpl.java | 213 ++++++------------ .../device/mgt/core/search/DeviceDetails.java | 5 + .../DeviceManagementProviderServiceTest.java | 2 +- .../mgt/core/impl/TenantManagerImpl.java | 2 +- 7 files changed, 82 insertions(+), 156 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceStatusFilterServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceStatusFilterServiceImpl.java index 7e5fd01437..1af284c039 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceStatusFilterServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceStatusFilterServiceImpl.java @@ -43,10 +43,9 @@ public class DeviceStatusFilterServiceImpl implements DeviceStatusFilterService @Path("/{deviceType}") public Response getDeviceStatusFilters(@PathParam("deviceType") String deviceType) { List result; - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); try { DeviceStatusManagementService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceStatusManagmentService(); - result = deviceManagementProviderService.getDeviceStatusFilters(deviceType, tenantId); + result = deviceManagementProviderService.getDeviceStatusFilters(deviceType); if (result != null) { return Response.status(Response.Status.OK).entity(result).build(); } @@ -82,10 +81,9 @@ public class DeviceStatusFilterServiceImpl implements DeviceStatusFilterService @QueryParam("isEnabled") boolean isEnabled) { boolean result; - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); try { DeviceStatusManagementService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceStatusManagmentService(); - result = deviceManagementProviderService.updateDefaultDeviceStatusCheck(tenantId, isEnabled); + result = deviceManagementProviderService.updateDefaultDeviceStatusCheck(isEnabled); if (result) { return Response.status(Response.Status.OK).entity("Successfully updated device status check.").build(); } else { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/metadata/mgt/DeviceStatusManagementService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/metadata/mgt/DeviceStatusManagementService.java index 5ee7c8fce0..d4b140c9c0 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/metadata/mgt/DeviceStatusManagementService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/metadata/mgt/DeviceStatusManagementService.java @@ -31,7 +31,7 @@ public interface DeviceStatusManagementService { * * @throws MetadataManagementException if error while adding default device status */ - void addDefaultDeviceStatusFilterIfNotExist(int tenantId) throws MetadataManagementException; + void addDefaultDeviceStatusFilterIfNotExist() throws MetadataManagementException; /** * This method is useful to reset existing device status to default values in xml @@ -53,21 +53,21 @@ public interface DeviceStatusManagementService { * * @throws MetadataManagementException if error while updating existing device status */ - boolean updateDefaultDeviceStatusCheck(int tenantId, boolean isChecked) + boolean updateDefaultDeviceStatusCheck(boolean isChecked) throws MetadataManagementException; /** * This method is useful to get existing device status filters * * @throws MetadataManagementException if error while getting existing device status */ - List getDeviceStatusFilters(int tenantId) throws MetadataManagementException; + List getDeviceStatusFilters() throws MetadataManagementException; /** * This method is useful to get existing device status filters by device type and tenant id * * @throws MetadataManagementException if error while getting existing device status */ - List getDeviceStatusFilters(String deviceType, int tenantId) throws MetadataManagementException; + List getDeviceStatusFilters(String deviceType) throws MetadataManagementException; /** * This method is useful to get existing device status filters diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java index 6bdaa5fee5..6eee433fb0 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -386,7 +386,7 @@ public class DeviceManagementServiceComponent { DeviceStatusManagementService deviceStatusManagemntService = DeviceStatusManagementServiceImpl.getInstance(); DeviceManagementDataHolder.getInstance().setDeviceStatusManagementService(deviceStatusManagemntService); try { - deviceStatusManagemntService.addDefaultDeviceStatusFilterIfNotExist(tenantId); + deviceStatusManagemntService.addDefaultDeviceStatusFilterIfNotExist(); } catch (Throwable e) { log.error("Error occurred while adding default tenant device status", e); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java index b8b92e0f26..4e9cde85c8 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java @@ -23,8 +23,8 @@ import com.google.common.cache.CacheBuilder; import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataKeyAlreadyExistsException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException; -import io.entgra.device.mgt.core.device.mgt.common.exceptions.TransactionManagementException; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.AllowedDeviceStatus; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata; @@ -32,9 +32,7 @@ import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManageme import io.entgra.device.mgt.core.device.mgt.core.config.ui.DeviceStatusConfigurations; import io.entgra.device.mgt.core.device.mgt.core.config.ui.DeviceStatusItem; import io.entgra.device.mgt.core.device.mgt.core.config.ui.UIConfigurationManager; -import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.MetadataDAO; -import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOException; -import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOFactory; +import io.entgra.device.mgt.core.device.mgt.core.internal.DeviceManagementDataHolder; import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.util.MetadataConstants; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; @@ -51,9 +49,9 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement private static final Log log = LogFactory.getLog(DeviceStatusManagementServiceImpl.class); - private static DeviceStatusManagementServiceImpl instance; + private static volatile DeviceStatusManagementServiceImpl instance; - private final MetadataDAO metadataDAO; + private static final MetadataManagementService metadataManagementService = DeviceManagementDataHolder.getInstance().getMetadataManagementService(); private final Gson gson; @@ -62,7 +60,6 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement .build(); private DeviceStatusManagementServiceImpl() { - this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO(); this.gson = new Gson(); } @@ -74,57 +71,30 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement } @Override - public void addDefaultDeviceStatusFilterIfNotExist(int tenantId) throws MetadataManagementException { + public void addDefaultDeviceStatusFilterIfNotExist() throws MetadataManagementException { try { - MetadataManagementDAOFactory.beginTransaction(); - if (!metadataDAO.isExist(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY) && !metadataDAO.isExist(tenantId, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY)) { Metadata defaultDeviceStatusMetadata = constructDeviceStatusMetadata(getDefaultDeviceStatus()); Metadata defaultDeviceStatusCheckMetadata = constructDeviceStatusCheckMetadata(getDefaultDeviceStatusCheck()); // Add default device status and device status check metadata entries - addMetadataEntry(tenantId, defaultDeviceStatusMetadata, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); - addMetadataEntry(tenantId, defaultDeviceStatusCheckMetadata, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY); - } - MetadataManagementDAOFactory.commitTransaction(); - } catch (MetadataManagementDAOException e) { - MetadataManagementDAOFactory.rollbackTransaction(); - String msg = "Error occurred while inserting default device status metadata entry."; - log.error(msg, e); - throw new MetadataManagementException(msg, e); - } catch (TransactionManagementException e) { - String msg = "Error occurred while opening a connection to the data source"; - log.error(msg, e); - throw new MetadataManagementException(msg, e); - } finally { - MetadataManagementDAOFactory.closeConnection(); + addMetadataEntry(defaultDeviceStatusMetadata, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); + addMetadataEntry(defaultDeviceStatusCheckMetadata, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY); + } catch (MetadataKeyAlreadyExistsException e) { + log.error("Metadata is already exist.", e); } } /** * Adds default device status filters for a given tenant. * - * @param tenantId the tenant ID for which to add the device status filters. * @throws MetadataManagementException if an error occurs while adding the metadata or managing the transaction. */ - private void addDefaultDeviceStatusFilters(int tenantId) throws MetadataManagementException { + private void addDefaultDeviceStatusFilters() throws MetadataManagementException { try { - MetadataManagementDAOFactory.beginTransaction(); - if (!metadataDAO.isExist(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY)) { Metadata defaultDeviceStatusMetadata = constructDeviceStatusMetadata(getDefaultDeviceStatus()); // Add default device status metadata entries - addMetadataEntry(tenantId, defaultDeviceStatusMetadata, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); - } - MetadataManagementDAOFactory.commitTransaction(); - } catch (MetadataManagementDAOException e) { - MetadataManagementDAOFactory.rollbackTransaction(); - String msg = "Error occurred while inserting default device status metadata entry."; - log.error(msg, e); - throw new MetadataManagementException(msg, e); - } catch (TransactionManagementException e) { - String msg = "Error occurred while opening a connection to the data source"; - log.error(msg, e); - throw new MetadataManagementException(msg, e); - } finally { - MetadataManagementDAOFactory.closeConnection(); + addMetadataEntry(defaultDeviceStatusMetadata, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); + } catch (MetadataKeyAlreadyExistsException e) { + log.error("Metadata is already exist.", e); } } @@ -136,24 +106,11 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement */ private void addDefaultDeviceStatusCheck(int tenantId) throws MetadataManagementException { try { - MetadataManagementDAOFactory.beginTransaction(); - if (!metadataDAO.isExist(tenantId, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY)) { Metadata defaultDeviceStatusCheckMetadata = constructDeviceStatusCheckMetadata(getDefaultDeviceStatusCheck()); // Add default device status check metadata entries - addMetadataEntry(tenantId, defaultDeviceStatusCheckMetadata, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY); - } - MetadataManagementDAOFactory.commitTransaction(); - } catch (MetadataManagementDAOException e) { - MetadataManagementDAOFactory.rollbackTransaction(); - String msg = "Error occurred while inserting default device status metadata entry."; - log.error(msg, e); - throw new MetadataManagementException(msg, e); - } catch (TransactionManagementException e) { - String msg = "Error occurred while opening a connection to the data source"; - log.error(msg, e); - throw new MetadataManagementException(msg, e); - } finally { - MetadataManagementDAOFactory.closeConnection(); + addMetadataEntry(defaultDeviceStatusCheckMetadata, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY); + } catch (MetadataKeyAlreadyExistsException e) { + log.error("Metadata is already exist.", e); } } @@ -164,80 +121,46 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement @Override public void updateDefaultDeviceStatusFilters(int tenantId, String deviceType, List deviceStatus) throws MetadataManagementException { - try { - if (StringUtils.isEmpty(deviceType)) { - String msg = "Device type must not be empty or null"; - log.error(msg); - throw new IllegalArgumentException(msg); - } - MetadataManagementDAOFactory.beginTransaction(); - // Retrieve the current device status metadata - Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); - if (metadata != null) { - Type listType = new TypeToken>() { - }.getType(); - List currentStatusList = gson.fromJson(metadata.getMetaValue(), listType); - - // Find the status for the specified deviceType - for (AllowedDeviceStatus status : currentStatusList) { - if (deviceType.equalsIgnoreCase(status.getType())) { - // Update the status list for the specified deviceType - status.setStatus(deviceStatus); - break; - } + if (StringUtils.isEmpty(deviceType)) { + String msg = "Device type must not be empty or null"; + log.error(msg); + throw new IllegalArgumentException(msg); + } + // Retrieve the current device status metadata + Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); + if (metadata != null) { + Type listType = new TypeToken>() { + }.getType(); + List currentStatusList = gson.fromJson(metadata.getMetaValue(), listType); + + // Find the status for the specified deviceType + for (AllowedDeviceStatus status : currentStatusList) { + if (deviceType.equalsIgnoreCase(status.getType())) { + // Update the status list for the specified deviceType + status.setStatus(deviceStatus); + break; } - metadata.setMetaValue(gson.toJson(currentStatusList)); - updateMetadataEntry(tenantId, metadata, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); } - MetadataManagementDAOFactory.commitTransaction(); - } catch (MetadataManagementDAOException e) { - MetadataManagementDAOFactory.rollbackTransaction(); - String msg = "Error occurred while updating device status metadata entry."; - log.error(msg, e); - throw new MetadataManagementException(msg, e); - } catch (TransactionManagementException e) { - String msg = "Error occurred while opening a connection to the data source"; - log.error(msg, e); - throw new MetadataManagementException(msg, e); - } finally { - MetadataManagementDAOFactory.closeConnection(); + metadata.setMetaValue(gson.toJson(currentStatusList)); + updateMetadataEntry(metadata, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); } } @Override - public boolean updateDefaultDeviceStatusCheck(int tenantId, boolean isChecked) throws MetadataManagementException { - boolean success = false; - try { - MetadataManagementDAOFactory.beginTransaction(); - if (metadataDAO.isExist(tenantId, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY)) { - Metadata isDeviceStatusChecked = constructDeviceStatusCheckMetadata(isChecked); - // Add default device status check metadata entries - updateMetadataEntry(tenantId, isDeviceStatusChecked, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY); - success = true; - } - MetadataManagementDAOFactory.commitTransaction(); - } catch (MetadataManagementDAOException e) { - MetadataManagementDAOFactory.rollbackTransaction(); - String msg = "Error occurred while updating device status check metadata entry."; - log.error(msg, e); - throw new MetadataManagementException(msg, e); - } catch (TransactionManagementException e) { - String msg = "Error occurred while opening a connection to the data source"; - log.error(msg, e); - throw new MetadataManagementException(msg, e); - } finally { - MetadataManagementDAOFactory.closeConnection(); - } - return success; + public boolean updateDefaultDeviceStatusCheck(boolean isChecked) throws MetadataManagementException { + Metadata isDeviceStatusChecked = constructDeviceStatusCheckMetadata(isChecked); + // Add default device status check metadata entries + updateMetadataEntry(isDeviceStatusChecked, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY); + return true; } @Override - public List getDeviceStatusFilters(int tenantId) throws MetadataManagementException { - return retrieveAndParseMetadata(tenantId); + public List getDeviceStatusFilters() throws MetadataManagementException { + return retrieveAndParseMetadata(); } - public List getDeviceStatusFilters(String deviceType, int tenantId) throws MetadataManagementException { - List statusList = retrieveAndParseMetadata(tenantId); + public List getDeviceStatusFilters(String deviceType) throws MetadataManagementException { + List statusList = retrieveAndParseMetadata(); for (AllowedDeviceStatus status : statusList) { if (deviceType.equalsIgnoreCase(status.getType())) { return status.getStatus(); @@ -249,12 +172,10 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement /** * Retrieves and parses the allowed device status metadata for the specified tenant. - * @param tenantId the tenant ID for which to retrieve and parse the metadata. * @return a list of {@link AllowedDeviceStatus} objects parsed from the metadata. * @throws MetadataManagementException if an error occurs while retrieving or parsing the metadata. */ - private List retrieveAndParseMetadata(int tenantId) throws MetadataManagementException { - MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl(); + private List retrieveAndParseMetadata() throws MetadataManagementException { Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); String metaValue; if (metadata != null) { @@ -262,7 +183,7 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement } else { List defaultStatusList = getDefaultDeviceStatus(); metaValue = gson.toJson(defaultStatusList); - addDefaultDeviceStatusFilters(tenantId); + addDefaultDeviceStatusFilters(); } Type listType = new TypeToken>() {}.getType(); return gson.fromJson(metaValue, listType); @@ -272,7 +193,6 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement @Override public boolean getDeviceStatusCheck(int tenantId) throws MetadataManagementException { - MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl(); Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY); if (metadata != null) { return Boolean.parseBoolean(metadata.getMetaValue()); @@ -291,19 +211,7 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement } List allowedDeviceStatus = cache.getIfPresent(Integer.toString(tenantId)); if (allowedDeviceStatus == null) { - MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl(); - Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); - String metaValue; - if (metadata != null) { - metaValue = metadata.getMetaValue(); - } else { - List statusList = getDefaultDeviceStatus(); - metaValue = gson.toJson(statusList); - addDefaultDeviceStatusFilters(tenantId); - } - Type listType = new TypeToken>() { - }.getType(); - allowedDeviceStatus = gson.fromJson(metaValue, listType); + allowedDeviceStatus = retrieveAndParseMetadata(); cache.put(Integer.toString(tenantId), allowedDeviceStatus); } for (AllowedDeviceStatus status : allowedDeviceStatus) { @@ -315,22 +223,23 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement return false; // Device type not found in metadata } - private void addMetadataEntry(int tenantId, Metadata metadata, String key) throws MetadataManagementDAOException { - metadataDAO.addMetadata(tenantId, metadata); + private void addMetadataEntry(Metadata metadata, String key) throws MetadataManagementException, MetadataKeyAlreadyExistsException { + metadata.setMetaKey(key); + metadataManagementService.createMetadata(metadata); if (log.isDebugEnabled()) { log.debug(key + " metadata entry has been inserted successfully"); } } - private void updateMetadataEntry(int tenantId, Metadata metadata, String key) throws MetadataManagementDAOException { - metadataDAO.updateMetadata(tenantId, metadata); + private void updateMetadataEntry(Metadata metadata, String key) throws MetadataManagementException { + metadata.setMetaKey(key); + metadataManagementService.updateMetadata(metadata); if (log.isDebugEnabled()) { log.debug(key + " metadata entry has been updated successfully"); } } private Metadata constructDeviceStatusMetadata(List deviceStatusItems) { - Gson gson = new Gson(); String deviceStatusItemsJsonString = gson.toJson(deviceStatusItems); Metadata metadata = new Metadata(); @@ -348,6 +257,13 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement return metadata; } + /** + * Retrieves the default device status items. + * This method fetches the device status configurations from the UI configuration manager. + * If the configurations are available, it returns the list of device status items. + * + * @return a list of {@code DeviceStatusItem} objects representing the default device statuses. + */ private List getDefaultDeviceStatus() { DeviceStatusConfigurations deviceStatusConfigurations = UIConfigurationManager.getInstance().getUIConfig().getDeviceStatusConfigurations(); List deviceStatusItems = new ArrayList<>(); @@ -363,6 +279,13 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement return deviceStatusItems; } + /** + * Retrieves the default device status check flag. + * This method fetches the device status configurations from the UI configuration manager. + * If the configurations are available, it returns the device status check flag. + * + * @return a boolean value indicating the default device status check flag. + */ private boolean getDefaultDeviceStatusCheck() { DeviceStatusConfigurations deviceStatusConfigurations = UIConfigurationManager.getInstance().getUIConfig().getDeviceStatusConfigurations(); boolean deviceStatusCheck = false; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/search/DeviceDetails.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/search/DeviceDetails.java index a7d3a06eb8..609fe4f7aa 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/search/DeviceDetails.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/search/DeviceDetails.java @@ -20,7 +20,9 @@ package io.entgra.device.mgt.core.device.mgt.core.search; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; +import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService; import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.DeviceStatusManagementServiceImpl; +import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.MetadataManagementServiceImpl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.testng.annotations.BeforeClass; @@ -43,6 +45,9 @@ public class DeviceDetails extends BaseDeviceManagementTest { DeviceManagementProviderService deviceManagementProviderService = new DeviceManagementProviderServiceImpl(); DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceManagementProviderService); + MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl(); + DeviceManagementDataHolder.getInstance().setMetadataManagementService(metadataManagementService); + DeviceStatusManagementService deviceStatusManagementService = DeviceStatusManagementServiceImpl.getInstance(); DeviceManagementDataHolder.getInstance().setDeviceStatusManagementService(deviceStatusManagementService); } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceTest.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceTest.java index 942cb2c8b0..032f11c7c4 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceTest.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceTest.java @@ -215,7 +215,7 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes if (!isMock()) { DeviceStatusManagementService deviceStatusManagementService = DeviceManagementDataHolder .getInstance().getDeviceStatusManagementService(); - deviceStatusManagementService.addDefaultDeviceStatusFilterIfNotExist(MultitenantConstants.SUPER_TENANT_ID); + deviceStatusManagementService.addDefaultDeviceStatusFilterIfNotExist(); Device device = TestDataHolder.generateDummyDeviceData(new DeviceIdentifier(DEVICE_ID, DEVICE_TYPE)); boolean enrollment = deviceMgtService.enrollDevice(device); Assert.assertTrue(enrollment); diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java index 00cdc801be..888036df0b 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java @@ -113,7 +113,7 @@ public class TenantManagerImpl implements TenantManager { initTenantFlow(tenantInfoBean); try { TenantMgtDataHolder.getInstance().getDeviceStatusManagementService(). - addDefaultDeviceStatusFilterIfNotExist(tenantInfoBean.getTenantId()); + addDefaultDeviceStatusFilterIfNotExist(); } catch (MetadataManagementException e) { String msg = "Error occurred while adding default device status filter"; log.error(msg, e); -- 2.36.3