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 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<List<AllowedDeviceStatus>>() {
}.getType();
List<AllowedDeviceStatus> currentStatusList = gson.fromJson(metadata.getMetaValue(), listType);
@ -198,54 +200,37 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
@Override
public List<AllowedDeviceStatus> 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<List<AllowedDeviceStatus>>() {}.getType();
List<AllowedDeviceStatus> statusList = gson.fromJson(metadata.getMetaValue(), listType);
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();
return retrieveAndParseMetadata(tenantId);
}
public List<String> getDeviceStatusFilters(String deviceType, int tenantId) throws MetadataManagementException {
List<AllowedDeviceStatus> 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<String> getDeviceStatusFilters(String deviceType, int tenantId) throws MetadataManagementException {
private List<AllowedDeviceStatus> 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<DeviceStatusItem> statusList = getDefaultDeviceStatus();
metaValue = gson.toJson(statusList);
List<DeviceStatusItem> defaultStatusList = getDefaultDeviceStatus();
metaValue = gson.toJson(defaultStatusList);
addDefaultDeviceStatusFilters(tenantId);
}
Type listType = new TypeToken<List<AllowedDeviceStatus>>() {
}.getType();
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();
Type listType = new TypeToken<List<AllowedDeviceStatus>>() {}.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 {

Loading…
Cancel
Save