Refactor device status filter management service

pull/384/head
Pramila Niroshan 7 months ago
parent 62722718f2
commit be287ebb76

@ -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.AllowedDeviceStatus;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; 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.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.DeviceStatusConfigurations;
import io.entgra.device.mgt.core.device.mgt.core.config.ui.DeviceStatusItem; 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.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 @Override
public void resetToDefaultDeviceStausFilter() throws MetadataManagementException { public void resetToDefaultDeviceStausFilter() throws MetadataManagementException {
@ -174,84 +221,68 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
} }
public List<String> getDeviceStatusFilters(String deviceType, int tenantId) throws MetadataManagementException { public List<String> getDeviceStatusFilters(String deviceType, int tenantId) throws MetadataManagementException {
try { MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl();
MetadataManagementDAOFactory.openConnection(); Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); Gson gson = new Gson();
Gson gson = new Gson(); String metaValue;
Type listType = new TypeToken<List<AllowedDeviceStatus>>() {}.getType(); if (metadata != null) {
List<AllowedDeviceStatus> statusList = gson.fromJson(metadata.getMetaValue(), listType); metaValue = metadata.getMetaValue();
} else {
List<DeviceStatusItem> statusList = getDefaultDeviceStatus();
metaValue = gson.toJson(statusList);
addDefaultDeviceStatusFilters(tenantId);
}
Type listType = new TypeToken<List<AllowedDeviceStatus>>() {
}.getType();
List<AllowedDeviceStatus> statusList = gson.fromJson(metaValue, listType);
for (AllowedDeviceStatus status : statusList) { for (AllowedDeviceStatus status : statusList) {
if (status.getType().equalsIgnoreCase(deviceType)) { if (status.getType().equalsIgnoreCase(deviceType)) {
return status.getStatus(); 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 @Override
public boolean getDeviceStatusCheck(int tenantId) throws MetadataManagementException { public boolean getDeviceStatusCheck(int tenantId) throws MetadataManagementException {
try { MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl();
MetadataManagementDAOFactory.openConnection(); Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY);
Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY); if (metadata != null) {
String metaValue = metadata.getMetaValue(); return Boolean.parseBoolean(metadata.getMetaValue());
return Boolean.parseBoolean(metaValue); } else {
} catch (MetadataManagementDAOException e) { addDefaultDeviceStatusCheck(tenantId);
String msg = "Error occurred while retrieving device status check meta data for tenant:" + tenantId; return getDefaultDeviceStatusCheck();
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();
} }
} }
@Override @Override
public boolean isDeviceStatusValid(String deviceType, String deviceStatus, int tenantId) throws MetadataManagementException { public boolean isDeviceStatusValid(String deviceType, String deviceStatus, int tenantId) throws MetadataManagementException {
try { MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl();
MetadataManagementDAOFactory.openConnection(); Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); String metaValue;
Gson gson = new Gson();
Gson gson = new Gson(); if (metadata != null) {
Type listType = new TypeToken<List<AllowedDeviceStatus>>() { metaValue = metadata.getMetaValue();
}.getType(); } else {
List<AllowedDeviceStatus> statusList = gson.fromJson(metadata.getMetaValue(), listType); List<DeviceStatusItem> statusList = getDefaultDeviceStatus();
metaValue = gson.toJson(statusList);
for (AllowedDeviceStatus status : statusList) { addDefaultDeviceStatusFilters(tenantId);
if (status.getType().equalsIgnoreCase(deviceType)) { }
List<String> allowedStatus = status.getStatus(); Type listType = new TypeToken<List<AllowedDeviceStatus>>() {
return allowedStatus.contains(deviceStatus); }.getType();
} List<AllowedDeviceStatus> statusList = gson.fromJson(metaValue, listType);
for (AllowedDeviceStatus status : statusList) {
if (status.getType().equalsIgnoreCase(deviceType)) {
List<String> 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 { private void addMetadataEntry(int tenantId, Metadata metadata, String key) throws MetadataManagementDAOException {

Loading…
Cancel
Save