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 {