From eb6186ba280579013c56cf71705bd0b6035cf268 Mon Sep 17 00:00:00 2001 From: pramilaniroshan Date: Tue, 28 May 2024 13:25:54 +0530 Subject: [PATCH] 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 {