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,12 +221,20 @@ 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();
Type listType = new TypeToken<List<AllowedDeviceStatus>>() {}.getType(); String metaValue;
List<AllowedDeviceStatus> statusList = gson.fromJson(metadata.getMetaValue(), listType); if (metadata != null) {
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)) {
@ -188,50 +243,37 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
} }
// Device type not found in metadata // Device type not found in metadata
return Collections.emptyList(); 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();
}
} }
@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) {
metaValue = metadata.getMetaValue();
} else {
List<DeviceStatusItem> statusList = getDefaultDeviceStatus();
metaValue = gson.toJson(statusList);
addDefaultDeviceStatusFilters(tenantId);
}
Type listType = new TypeToken<List<AllowedDeviceStatus>>() { Type listType = new TypeToken<List<AllowedDeviceStatus>>() {
}.getType(); }.getType();
List<AllowedDeviceStatus> statusList = gson.fromJson(metadata.getMetaValue(), listType); 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)) {
@ -241,17 +283,6 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
} }
return false; // Device type not found in metadata 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();
}
} }
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