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 {