From 2fab3029ffc05a997ef2d0e9e2d4284e8b4ddad7 Mon Sep 17 00:00:00 2001 From: Yohan Avishke Date: Wed, 4 Mar 2020 13:54:54 +0530 Subject: [PATCH] Add equation explaining text to the comments and fix fomratting issues Text will explain equation step by step by using. Added missing comments, formatted code lines, formatted license and added more comments. --- .../exceptions/BadRequestException.java | 3 +- .../report/mgt/ReportManagementService.java | 1 + .../dao/impl/device/GenericDeviceDAOImpl.java | 9 ++++-- .../dao/impl/device/OracleDeviceDAOImpl.java | 5 ++-- .../impl/device/PostgreSQLDeviceDAOImpl.java | 5 ++-- .../impl/device/SQLServerDeviceDAOImpl.java | 5 ++-- .../device/mgt/core/report/mgt/Constants.java | 4 +-- .../mgt/core/util/DeviceManagerUtil.java | 29 +++++++++++++++++-- 8 files changed, 41 insertions(+), 20 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/exceptions/BadRequestException.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/exceptions/BadRequestException.java index 5b1b26e92e..b7d99da0e0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/exceptions/BadRequestException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/exceptions/BadRequestException.java @@ -1,5 +1,4 @@ /* - * * Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved. * * Entgra (pvt) Ltd. licenses this file to you under the Apache License, @@ -42,5 +41,5 @@ public class BadRequestException extends Exception { public BadRequestException(String message, Throwable cause) { super(message, cause); } - } + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/report/mgt/ReportManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/report/mgt/ReportManagementService.java index 6216c3afb2..ef5806d798 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/report/mgt/ReportManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/report/mgt/ReportManagementService.java @@ -57,6 +57,7 @@ public interface ReportManagementService { * @param request {@link PaginationRequest} * @return {@link PaginationResult} * @throws ReportManagementException Might occur during the business logic or building database query + * @throws BadRequestException Might occur if the given os version or the device type doesn't match */ PaginationResult getDevicesExpiredByOSVersion(PaginationRequest request) throws ReportManagementException, BadRequestException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java index 01de7ca32b..e2a35770c2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java @@ -867,6 +867,8 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { try { Long osValue = (Long) request.getProperty(Constants.OS_VALUE); Connection conn = getConnection(); + /* following variable is used to identify the datasource type.This is due to a + convert function performed in the query which will depend on the datasource */ String dataSourceType = conn.getMetaData().getDatabaseProductName(); String sql="SELECT " + "d1.DEVICE_TYPE, " + @@ -938,6 +940,8 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { throws DeviceManagementDAOException { try { Connection conn = getConnection(); + /* following variable is used to identify the datasource type.This is due to a + convert function performed in the query which will depend on the datasource */ String dataSourceType = conn.getMetaData().getDatabaseProductName(); String sql = "SELECT " + "COUNT(ddi.DEVICE_ID) AS DEVICE_COUNT " + @@ -964,11 +968,10 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { ps.setLong(4, osValue); try (ResultSet rs = ps.executeQuery()) { - int deviceCount = 0; if (rs.next()) { - deviceCount = rs.getInt("DEVICE_COUNT"); + return rs.getInt("DEVICE_COUNT"); } - return deviceCount; + return 0; } } } catch (SQLException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java index 695f1b9ce8..2ef6697453 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java @@ -925,11 +925,10 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { ps.setLong(4, osValue); try (ResultSet rs = ps.executeQuery()) { - int deviceCount = 0; if (rs.next()) { - deviceCount = rs.getInt("DEVICE_COUNT"); + return rs.getInt("DEVICE_COUNT"); } - return deviceCount; + return 0; } } } catch (SQLException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java index 8275c7691f..fc137897bb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java @@ -909,11 +909,10 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl { ps.setLong(4, osValue); try (ResultSet rs = ps.executeQuery()) { - int deviceCount = 0; if (rs.next()) { - deviceCount = rs.getInt("DEVICE_COUNT"); + return rs.getInt("DEVICE_COUNT"); } - return deviceCount; + return 0; } } } catch (SQLException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java index 657e8d7a5c..ecda3ef66d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java @@ -725,11 +725,10 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { ps.setLong(4, osValue); try (ResultSet rs = ps.executeQuery()) { - int deviceCount = 0; if (rs.next()) { - deviceCount = rs.getInt("DEVICE_COUNT"); + return rs.getInt("DEVICE_COUNT"); } - return deviceCount; + return 0; } } } catch (SQLException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/report/mgt/Constants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/report/mgt/Constants.java index 4f5406e560..2be36d0acf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/report/mgt/Constants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/report/mgt/Constants.java @@ -1,5 +1,4 @@ /* - * * Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved. * * Entgra (pvt) Ltd. licenses this file to you under the Apache License, @@ -15,10 +14,8 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. - * */ - package org.wso2.carbon.device.mgt.core.report.mgt; public class Constants { @@ -33,3 +30,4 @@ public class Constants { public static final int NUM_OF_OS_VERSION_DIGITS= 5; public static final int NUM_OF_OS_VERSION_POSITIONS = 3; } + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index ff1cd594d4..0c0308d6b1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -731,7 +731,18 @@ public final class DeviceManagerUtil { int osVersionsLength = osVersions.length; /* - *

Following loop will work for the value generation

+ *

Equation explanation

+ * + *

+ Eg: {@code osVersion == "5.1.1"} will generate an array of {@code ["5","1","1"]} + Following loop for the above result can be broken down as below + * Iteration 1 : {@code Math.pow} result = 5 00000 00000, {@code sum} = 5 00000 00000 + * Iteration 2 : {@code Math.pow} result = 1 00000, {@code sum} = 5 00001 00000 + * Iteration 3 : {@code Math.pow} result = 1, {@code sum} = 5 00001 00001 + To generate the above results I have multiplied the array values with powers of 10. + The constraints used to generate the power of 10 is explained below, + *

+ * *

{@code Constants.NUM_OF_OS_VERSION_POSITIONS - (i + 1)} was done in-order to identify which position of the OS version is been used for generation process, so correct number @@ -742,7 +753,7 @@ public final class DeviceManagerUtil { {@code Constants.NUM_OF_OS_VERSION_DIGITS} this multiplication will make sure that the values generated will reduce in following order main OS version, minor OS version, Revision. *

- */ + */ return IntStream .range(0, osVersionsLength) .mapToLong(i -> (long) (Math.pow(10, (Constants.NUM_OF_OS_VERSION_POSITIONS - (i + 1)) @@ -762,7 +773,19 @@ public final class DeviceManagerUtil { StringJoiner joiner = new StringJoiner("."); /* - *

Following loop will break the generated value into parts and will recreate the OS version

+ *

Equation explanation

+ * + *

+ Eg: {@code osVersionValue == "5 00001 00001"} + Following loop will divide to break down the above number to regenerate the os version + * Iteration 1 : {@code osVersion} = 5 , {@code osVersionValue} = 00001 00001 + * Iteration 2 : {@code osVersion} = 1 , {@code osVersionValue} = 00001 + * Iteration 3 : {@code osVersion} = 1 , {@code osVersionValue} = 0 + Final array = {@code ["5","1","1"]} + To generate the above results I have divided the generated value with powers of 10. + The constraints used to generate the power of 10 is explained below, + *

+ * *

{@code 10, (i - 1) * Constants.NUM_OF_OS_VERSION_DIGITS} this will break the generated value creating each OS version position in following order main OS version, minor OS version,