From 748c28d36898455d9f1e1afea5c8624ab809d041 Mon Sep 17 00:00:00 2001 From: "osh.silva" Date: Mon, 29 Jul 2024 16:21:01 +0530 Subject: [PATCH 1/3] Fix ambigous column error --- .../dao/impl/device/GenericDeviceDAOImpl.java | 20 +++++++++---------- .../DeviceManagementProviderServiceImpl.java | 2 -- 2 files changed, 10 insertions(+), 12 deletions(-) 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/dao/impl/device/GenericDeviceDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java index 5710e0181f..85b4b5553d 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java @@ -225,11 +225,11 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { try { Connection conn = getConnection(); String sql = "SELECT d.ID AS DEVICE_ID, " + - "DEVICE_IDENTIFICATION, " + + "d.DEVICE_IDENTIFICATION, " + "DESCRIPTION, " + "NAME, " + "DATE_OF_ENROLMENT, " + - "LAST_UPDATED_TIMESTAMP, " + + "d.LAST_UPDATED_TIMESTAMP, " + "STATUS, " + "DATE_OF_LAST_UPDATE, " + "TIMESTAMPDIFF(DAY, ?, DATE_OF_ENROLMENT) as DAYS_SINCE_ENROLLED " + @@ -266,12 +266,12 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { try { conn = this.getConnection(); String sql = "select d.ID AS DEVICE_ID, " + - "DEVICE_IDENTIFICATION, " + + "d.DEVICE_IDENTIFICATION, " + "DESCRIPTION, " + "NAME, " + "DATE_OF_ENROLMENT, " + "DATE_OF_LAST_UPDATE, " + - "d1.LAST_UPDATED_TIMESTAMP, " + + "d.LAST_UPDATED_TIMESTAMP, " + "STATUS, " + "TIMESTAMPDIFF(DAY, DATE_OF_LAST_UPDATE, DATE_OF_ENROLMENT) AS DAYS_USED " + "from DM_DEVICE d, DM_ENROLMENT e " + @@ -311,11 +311,11 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { try { conn = this.getConnection(); String sql = "select d.ID AS DEVICE_ID, " + - "DEVICE_IDENTIFICATION, " + + "d.DEVICE_IDENTIFICATION, " + "DESCRIPTION, " + "NAME, " + "DATE_OF_ENROLMENT, " + - "LAST_UPDATED_TIMESTAMP, " + + "d.LAST_UPDATED_TIMESTAMP, " + "STATUS, " + "DATE_OF_LAST_UPDATE, " + "TIMESTAMPDIFF(DAY, ?, ?) as DAYS_SINCE_ENROLLED " + @@ -352,12 +352,12 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { try { conn = this.getConnection(); String sql = "select d.ID AS DEVICE_ID, " + - "DEVICE_IDENTIFICATION, " + + "d.DEVICE_IDENTIFICATION, " + "DESCRIPTION, " + "NAME, " + "DATE_OF_ENROLMENT, " + "DATE_OF_LAST_UPDATE, " + - "LAST_UPDATED_TIMESTAMP, " + + "d.LAST_UPDATED_TIMESTAMP, " + "STATUS, " + "TIMESTAMPDIFF(DAY, DATE_OF_LAST_UPDATE, ?) AS DAYS_USED " + "from DM_DEVICE d, DM_ENROLMENT e " + @@ -400,9 +400,9 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { conn = this.getConnection(); String sql = "SELECT " + "DM_DEVICE.ID AS DEVICE_ID, " + - "DEVICE_IDENTIFICATION, " + + "d.DEVICE_IDENTIFICATION, " + "DESCRIPTION, " + - "LAST_UPDATED_TIMESTAMP, " + + "d.LAST_UPDATED_TIMESTAMP, " + "DM_DEVICE.NAME AS DEVICE_NAME, " + "DEVICE_TYPE, " + "DM_ENROLMENT.ID AS ENROLMENT_ID, " + 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/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 851153de16..918ffb062c 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -2233,8 +2233,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv String msg = "Error occurred while opening a connection to the data source"; log.info(msg, e); throw new DeviceManagementException(msg, e); - } finally { - DeviceManagementDAOFactory.closeConnection(); } } -- 2.36.3 From a472f73adc66ef675365ea38b537d71a73042c8d Mon Sep 17 00:00:00 2001 From: "osh.silva" Date: Mon, 29 Jul 2024 23:44:18 +0530 Subject: [PATCH 2/3] Fix status connection issue --- .../DeviceManagementProviderServiceImpl.java | 111 ++++++++---------- 1 file changed, 49 insertions(+), 62 deletions(-) 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/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 918ffb062c..737a14177b 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -1154,50 +1154,62 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv public double generateCost(List allDevices, Timestamp startDate, Timestamp endDate, Cost tenantCost, List deviceStatusNotAvailable, double totalCost) throws DeviceManagementException { List deviceStatus; - for (Device device : allDevices) { - long dateDiff = 0; - deviceStatus = getDeviceStatusHistoryInsideTransaction(device, null, endDate, true); - if (device.getEnrolmentInfo().getDateOfEnrolment() < startDate.getTime()) { - if (!deviceStatus.isEmpty() && (String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED") - || String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) { - if (deviceStatus.get(0).getUpdateTime().getTime() >= startDate.getTime()) { - dateDiff = deviceStatus.get(0).getUpdateTime().getTime() - startDate.getTime(); + try { + DeviceManagementDAOFactory.getConnection(); + for (Device device : allDevices) { + long dateDiff = 0; + int tenantId = this.getTenantId(); + deviceStatus = deviceStatusDAO.getStatus(device.getId(), tenantId, null, endDate, true); + if (device.getEnrolmentInfo().getDateOfEnrolment() < startDate.getTime()) { + if (!deviceStatus.isEmpty() && (String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED") + || String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) { + if (deviceStatus.get(0).getUpdateTime().getTime() >= startDate.getTime()) { + dateDiff = deviceStatus.get(0).getUpdateTime().getTime() - startDate.getTime(); + } + } else if (!deviceStatus.isEmpty() && (!String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED") + && !String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) { + dateDiff = endDate.getTime() - startDate.getTime(); } - } else if (!deviceStatus.isEmpty() && (!String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED") - && !String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) { - dateDiff = endDate.getTime() - startDate.getTime(); - } - } else { - if (!deviceStatus.isEmpty() && (String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED") - || String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) { - if (deviceStatus.get(0).getUpdateTime().getTime() >= device.getEnrolmentInfo().getDateOfEnrolment()) { - dateDiff = deviceStatus.get(0).getUpdateTime().getTime() - device.getEnrolmentInfo().getDateOfEnrolment(); + } else { + if (!deviceStatus.isEmpty() && (String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED") + || String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) { + if (deviceStatus.get(0).getUpdateTime().getTime() >= device.getEnrolmentInfo().getDateOfEnrolment()) { + dateDiff = deviceStatus.get(0).getUpdateTime().getTime() - device.getEnrolmentInfo().getDateOfEnrolment(); + } + } else if (!deviceStatus.isEmpty() && (!String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED") + && !String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) { + dateDiff = endDate.getTime() - device.getEnrolmentInfo().getDateOfEnrolment(); } - } else if (!deviceStatus.isEmpty() && (!String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED") - && !String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) { - dateDiff = endDate.getTime() - device.getEnrolmentInfo().getDateOfEnrolment(); } - } - // Convert dateDiff to days as a decimal value - double dateDiffInDays = (double) dateDiff / (24 * 60 * 60 * 1000); + // Convert dateDiff to days as a decimal value + double dateDiffInDays = (double) dateDiff / (24 * 60 * 60 * 1000); - if (dateDiffInDays % 1 >= 0.9) { - dateDiffInDays = Math.ceil(dateDiffInDays); - } + if (dateDiffInDays % 1 >= 0.9) { + dateDiffInDays = Math.ceil(dateDiffInDays); + } - long dateInDays = (long) dateDiffInDays; - double cost = 0; - if (tenantCost != null) { - cost = (tenantCost.getCost() / 365) * dateInDays; - } - totalCost += cost; - device.setCost(Math.round(cost * 100.0) / 100.0); - long totalDays = dateInDays + device.getDaysUsed(); - device.setDaysUsed((int) totalDays); - if (deviceStatus.isEmpty()) { - deviceStatusNotAvailable.add(device); + long dateInDays = (long) dateDiffInDays; + double cost = 0; + if (tenantCost != null) { + cost = (tenantCost.getCost() / 365) * dateInDays; + } + totalCost += cost; + device.setCost(Math.round(cost * 100.0) / 100.0); + long totalDays = dateInDays + device.getDaysUsed(); + device.setDaysUsed((int) totalDays); + if (deviceStatus.isEmpty()) { + deviceStatusNotAvailable.add(device); + } } + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred in retrieving status history for a device in billing."; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.info(msg, e); + throw new DeviceManagementException(msg, e); } return totalCost; } @@ -2211,31 +2223,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } - /* - This is just to avoid breaking the billing functionality as it required to call getDeviceStatusHistory method - without transaction handling. - */ - private List getDeviceStatusHistoryInsideTransaction( - Device device, Date fromDate, Date toDate, boolean billingStatus) - throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("get status history of device: " + device.getDeviceIdentifier()); - } - try { - DeviceManagementDAOFactory.getConnection(); - int tenantId = this.getTenantId(); - return deviceStatusDAO.getStatus(device.getId(), tenantId, fromDate, toDate, billingStatus); - } catch (DeviceManagementDAOException e) { - String msg = "Error occurred in retrieving status history for device :" + device.getDeviceIdentifier(); - log.error(msg, e); - throw new DeviceManagementException(msg, e); - } catch (SQLException e) { - String msg = "Error occurred while opening a connection to the data source"; - log.info(msg, e); - throw new DeviceManagementException(msg, e); - } - } - @Override public List getDeviceStatusHistory(Device device, Date fromDate, Date toDate, boolean billingStatus) throws DeviceManagementException { if (log.isDebugEnabled()) { -- 2.36.3 From a2b8b8bda231e1585bd518191c4dfdc49d22d5ae Mon Sep 17 00:00:00 2001 From: "osh.silva" Date: Thu, 19 Sep 2024 14:17:26 +0530 Subject: [PATCH 3/3] Remove unwanted connection --- .../core/service/DeviceManagementProviderServiceImpl.java | 5 ----- 1 file changed, 5 deletions(-) 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/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 9949cc24b1..df57af8444 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -1177,7 +1177,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv public double generateCost(List allDevices, Timestamp startDate, Timestamp endDate, Cost tenantCost, List deviceStatusNotAvailable, double totalCost) throws DeviceManagementException { List deviceStatus; try { - DeviceManagementDAOFactory.getConnection(); for (Device device : allDevices) { long dateDiff = 0; int tenantId = this.getTenantId(); @@ -1228,10 +1227,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv String msg = "Error occurred in retrieving status history for a device in billing."; log.error(msg, e); throw new DeviceManagementException(msg, e); - } catch (SQLException e) { - String msg = "Error occurred while opening a connection to the data source"; - log.info(msg, e); - throw new DeviceManagementException(msg, e); } return totalCost; } -- 2.36.3