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.
pull/384/head
Pramila Niroshan 6 months ago
parent be287ebb76
commit eb6186ba28

@ -50,8 +50,11 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
private final MetadataDAO metadataDAO; private final MetadataDAO metadataDAO;
private final Gson gson;
public DeviceStatusManagementServiceImpl() { public DeviceStatusManagementServiceImpl() {
this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO(); this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO();
this.gson = new Gson();
} }
@Override @Override
@ -138,7 +141,6 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
// Retrieve the current device status metadata // Retrieve the current device status metadata
Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
if (metadata != null) { if (metadata != null) {
Gson gson = new Gson();
Type listType = new TypeToken<List<AllowedDeviceStatus>>() { Type listType = new TypeToken<List<AllowedDeviceStatus>>() {
}.getType(); }.getType();
List<AllowedDeviceStatus> currentStatusList = gson.fromJson(metadata.getMetaValue(), listType); List<AllowedDeviceStatus> currentStatusList = gson.fromJson(metadata.getMetaValue(), listType);
@ -198,54 +200,37 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
@Override @Override
public List<AllowedDeviceStatus> getDeviceStatusFilters(int tenantId) throws MetadataManagementException { public List<AllowedDeviceStatus> getDeviceStatusFilters(int tenantId) throws MetadataManagementException {
try { return retrieveAndParseMetadata(tenantId);
MetadataManagementDAOFactory.openConnection(); }
Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
Gson gson = new Gson();
Type listType = new TypeToken<List<AllowedDeviceStatus>>() {}.getType();
List<AllowedDeviceStatus> statusList = gson.fromJson(metadata.getMetaValue(), listType);
return statusList; public List<String> getDeviceStatusFilters(String deviceType, int tenantId) throws MetadataManagementException {
} catch (MetadataManagementDAOException e) { List<AllowedDeviceStatus> statusList = retrieveAndParseMetadata(tenantId);
String msg = "Error occurred while retrieving device status meta data for tenant:" + tenantId; for (AllowedDeviceStatus status : statusList) {
log.error(msg, e); if (status.getType().equalsIgnoreCase(deviceType)) {
throw new MetadataManagementException(msg, e); return status.getStatus();
} 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();
} }
public List<String> getDeviceStatusFilters(String deviceType, int tenantId) throws MetadataManagementException { private List<AllowedDeviceStatus> retrieveAndParseMetadata(int tenantId) throws MetadataManagementException {
MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl(); MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl();
Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
Gson gson = new Gson();
String metaValue; String metaValue;
if (metadata != null) { if (metadata != null) {
metaValue = metadata.getMetaValue(); metaValue = metadata.getMetaValue();
} else { } else {
List<DeviceStatusItem> statusList = getDefaultDeviceStatus(); List<DeviceStatusItem> defaultStatusList = getDefaultDeviceStatus();
metaValue = gson.toJson(statusList); metaValue = gson.toJson(defaultStatusList);
addDefaultDeviceStatusFilters(tenantId); addDefaultDeviceStatusFilters(tenantId);
} }
Type listType = new TypeToken<List<AllowedDeviceStatus>>() { Type listType = new TypeToken<List<AllowedDeviceStatus>>() {}.getType();
}.getType(); return gson.fromJson(metaValue, listType);
List<AllowedDeviceStatus> 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();
} }
@Override @Override
public boolean getDeviceStatusCheck(int tenantId) throws MetadataManagementException { public boolean getDeviceStatusCheck(int tenantId) throws MetadataManagementException {
MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl(); MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl();
@ -263,7 +248,6 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl(); MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl();
Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
String metaValue; String metaValue;
Gson gson = new Gson();
if (metadata != null) { if (metadata != null) {
metaValue = metadata.getMetaValue(); metaValue = metadata.getMetaValue();
} else { } else {

Loading…
Cancel
Save