Change API to filter devices using a given OS version

When syncing devices a value will be generated for each device version.
At the API call same method is used to generate a value for the passed
OS version then thedevices are filtered using those 2 values.

This will still create a similar value but now will use
mathematical operations(pow operations) to generate
feature/appm-store/pbac
Yohan Avishke 5 years ago
parent 2340147a40
commit e8a260dbc4

@ -313,12 +313,12 @@ public interface ReportManagementService {
int limit) throws ReportManagementException; int limit) throws ReportManagementException;
@GET @GET
@Path("expired-devices/{deviceType}") @Path("/expired-devices/{deviceType}")
@ApiOperation( @ApiOperation(
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
httpMethod = "GET", httpMethod = "GET",
value = "Getting Details of Registered Devices filtered by OS version build date", value = "Getting Details of Registered Devices filtered by OS version",
notes = "Provides details of devices that have a version build date older than the provided date.", notes = "Provides details of devices that have a OS version older than the provided version.",
tags = "Device Management", tags = "Device Management",
extensions = { extensions = {
@Extension(properties = { @Extension(properties = {
@ -337,9 +337,9 @@ public interface ReportManagementService {
name = "Content-Type", name = "Content-Type",
description = "The content type of the body")}), description = "The content type of the body")}),
@ApiResponse( @ApiResponse(
code = 404, code = 400,
message = "Not Found. " + message = "Bad Request. " +
"\n Device type does not exist", "\n Contents of the request are invalid",
response = ErrorResponse.class), response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
@ -354,10 +354,10 @@ public interface ReportManagementService {
required = true) required = true)
@PathParam("deviceType") String deviceType, @PathParam("deviceType") String deviceType,
@ApiParam( @ApiParam(
name = "osBuildDate", name = "osVersion",
value = "Minimum OS version build date which is used to filter the devices.", value = "Minimum OS version which is used to filter the devices.",
required = true) required = true)
@QueryParam("osBuildDate") Long osBuildDate, @QueryParam("osVersion") String osVersion,
@ApiParam( @ApiParam(
name = "offset", name = "offset",
value = "The starting pagination index for the list of filtered devices.", value = "The starting pagination index for the list of filtered devices.",

@ -25,6 +25,7 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.exceptions.BadRequestException;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceTypeNotFoundException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceTypeNotFoundException;
import org.wso2.carbon.device.mgt.common.exceptions.ReportManagementException; import org.wso2.carbon.device.mgt.common.exceptions.ReportManagementException;
import org.wso2.carbon.device.mgt.core.report.mgt.Constants; import org.wso2.carbon.device.mgt.core.report.mgt.Constants;
@ -169,10 +170,10 @@ public class ReportManagementServiceImpl implements ReportManagementService {
} }
@GET @GET
@Path("expired-devices/{deviceType}") @Path("/expired-devices/{deviceType}")
@Override @Override
public Response getExpiredDevicesByOSVersion(@PathParam("deviceType") String deviceType, public Response getExpiredDevicesByOSVersion(@PathParam("deviceType") String deviceType,
@QueryParam("osBuildDate") Long osBuildDate, @QueryParam("osVersion") String osVersion,
@DefaultValue("0") @DefaultValue("0")
@QueryParam("offset") int offset, @QueryParam("offset") int offset,
@DefaultValue("5") @DefaultValue("5")
@ -180,20 +181,23 @@ public class ReportManagementServiceImpl implements ReportManagementService {
try { try {
PaginationRequest request = new PaginationRequest(offset, limit); PaginationRequest request = new PaginationRequest(offset, limit);
request.setDeviceType(deviceType); request.setDeviceType(deviceType);
request.setProperty(Constants.OS_BUILD_DATE, osBuildDate); request.setProperty(Constants.OS_VERSION, osVersion);
PaginationResult paginationResult = DeviceMgtAPIUtils PaginationResult paginationResult = DeviceMgtAPIUtils
.getReportManagementService() .getReportManagementService()
.getDevicesExpiredByOSVersion(request); .getDevicesExpiredByOSVersion(request);
return Response.status(Response.Status.OK).entity(paginationResult).build(); DeviceList devices = new DeviceList();
} catch (DeviceTypeNotFoundException e) { devices.setList((List<Device>) paginationResult.getData());
String msg = "Error occurred while retrieving devices list. Device type: " + deviceType + devices.setCount(paginationResult.getRecordsTotal());
"is not valid";
log.error(msg); return Response.status(Response.Status.OK).entity(devices).build();
return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); } catch (BadRequestException e) {
String msg = "Error occurred while validating device type or the OS version.";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
} catch (ReportManagementException e) { } catch (ReportManagementException e) {
String msg = "Error occurred while retrieving devices list with out-dated OS build versions"; String msg = "Error occurred while retrieving list of devices with out-dated OS versions.";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} }

@ -0,0 +1,46 @@
/*
*
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
*
* Entgra (pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* 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.common.exceptions;
public class BadRequestException extends Exception {
private static final long serialVersionUID = 2304023531260840549L;
public BadRequestException() {
super();
}
public BadRequestException(String msg) {
super(msg);
}
public BadRequestException(Throwable cause) {
super(cause);
}
public BadRequestException(String msg, Exception nestedEx) {
super(msg, nestedEx);
}
public BadRequestException(String message, Throwable cause) {
super(message, cause);
}
}

@ -21,6 +21,7 @@ import com.google.gson.JsonObject;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.BadRequestException;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceTypeNotFoundException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceTypeNotFoundException;
import org.wso2.carbon.device.mgt.common.exceptions.ReportManagementException; import org.wso2.carbon.device.mgt.common.exceptions.ReportManagementException;
@ -56,10 +57,9 @@ public interface ReportManagementService {
* @param request {@link PaginationRequest} * @param request {@link PaginationRequest}
* @return {@link PaginationResult} * @return {@link PaginationResult}
* @throws ReportManagementException Might occur during the business logic or building database query * @throws ReportManagementException Might occur during the business logic or building database query
* @throws DeviceTypeNotFoundException Might occur while validating the device type
*/ */
PaginationResult getDevicesExpiredByOSVersion(PaginationRequest request) PaginationResult getDevicesExpiredByOSVersion(PaginationRequest request)
throws ReportManagementException, DeviceTypeNotFoundException; throws ReportManagementException, BadRequestException;
/** /**
* Get a paginated list of devices which is filtered by given encryption status * Get a paginated list of devices which is filtered by given encryption status

@ -637,12 +637,12 @@ public interface DeviceDAO {
* Count the number of devices older than the given OS version of a device type * Count the number of devices older than the given OS version of a device type
* *
* @param deviceType Device type name * @param deviceType Device type name
* @param osBuildDate BUild date off the current OS version * @param osValue Generated value for the OS version
* @param tenantId Id of the current tenant. * @param tenantId Id of the current tenant.
* @return {@link Integer} * @return {@link Integer}
* @throws DeviceManagementDAOException Thrown if error occurs while database transactions * @throws DeviceManagementDAOException Thrown if error occurs while database transactions
*/ */
int getCountOfDeviceExpiredByOSVersion(String deviceType, long osBuildDate, int tenantId) int getCountOfDeviceExpiredByOSVersion(String deviceType, Long osValue, int tenantId)
throws DeviceManagementDAOException; throws DeviceManagementDAOException;
/** /**

@ -1915,108 +1915,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
throw new DeviceManagementDAOException(msg, e); throw new DeviceManagementDAOException(msg, e);
} }
} }
@Override
public List<Device> getDevicesExpiredByOSVersion(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
try {
Long osBuildDate = (Long) request.getProperty(Constants.OS_BUILD_DATE);
Connection conn = getConnection();
String sql = "SELECT " +
"dt.NAME AS DEVICE_TYPE, " +
"d.ID AS DEVICE_ID, " +
"d.NAME AS DEVICE_NAME, " +
"d.DESCRIPTION, " +
"d.DEVICE_IDENTIFICATION, " +
"dd.OS_VERSION, " +
"dd.OS_BUILD_DATE, " +
"e.ID AS ENROLMENT_ID, " +
"e.OWNER, " +
"e.OWNERSHIP, " +
"e.STATUS, " +
"e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT " +
"FROM DM_DEVICE d, " +
"DM_DEVICE_DETAIL dd, " +
"DM_ENROLMENT e, " +
"(SELECT ID, NAME " +
"FROM DM_DEVICE_TYPE " +
"WHERE NAME = ? " +
"AND PROVIDER_TENANT_ID = ?) dt " +
"WHERE dt.ID = d.DEVICE_TYPE_ID " +
"AND d.ID = e.DEVICE_ID " +
"AND d.ID = dd.DEVICE_ID " +
"AND dd.OS_BUILD_DATE < ? " +
"LIMIT ? OFFSET ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
int paramIDx = 1;
ps.setString(paramIDx++, request.getDeviceType());
ps.setInt(paramIDx++, tenantId);
ps.setLong(paramIDx++, osBuildDate);
ps.setInt(paramIDx++, request.getRowCount());
ps.setInt(paramIDx, request.getStartIndex());
try (ResultSet rs = ps.executeQuery()) {
List<Device> devices = new ArrayList<>();
DeviceInfo deviceInfo = new DeviceInfo();
while (rs.next()) {
Device device = DeviceManagementDAOUtil.loadDevice(rs);
deviceInfo.setOsVersion(rs.getString(Constants.OS_VERSION));
deviceInfo.setOsBuildDate(rs.getString(Constants.OS_BUILD_DATE));
device.setDeviceInfo(deviceInfo);
devices.add(device);
}
return devices;
}
}
} catch (SQLException e) {
String msg = "Error occurred while building or executing queries to retrieve information " +
"of devices with an older OS build date";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
@Override
public int getCountOfDeviceExpiredByOSVersion(String deviceType, long osBuildDate, int tenantId)
throws DeviceManagementDAOException {
try {
Connection conn = getConnection();
String sql = "SELECT " +
"COUNT(dd.DEVICE_ID) AS DEVICE_COUNT " +
"FROM DM_DEVICE d, " +
"DM_DEVICE_DETAIL dd, " +
"(SELECT ID " +
"FROM DM_DEVICE_TYPE " +
"WHERE NAME = ? " +
"AND PROVIDER_TENANT_ID = ?) dt " +
"WHERE d.DEVICE_TYPE_ID = dt.ID " +
"AND d.ID = dd.DEVICE_ID " +
"AND dd.OS_BUILD_DATE < ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
int paramIdx = 1;
ps.setString(paramIdx++, deviceType);
ps.setInt(paramIdx++, tenantId);
ps.setLong(paramIdx, osBuildDate);
try (ResultSet rs = ps.executeQuery()) {
int deviceCount = 0;
if (rs.next()) {
deviceCount = rs.getInt("DEVICE_COUNT");
}
return deviceCount;
}
}
} catch (SQLException e) {
String msg = "Error occurred while building or executing queries to retrieve the count " +
"of devices with an older OS build date";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
@Override @Override
public List<Device> getAppNotInstalledDevices( public List<Device> getAppNotInstalledDevices(
PaginationRequest request, int tenantId, String packageName, String version) PaginationRequest request, int tenantId, String packageName, String version)

@ -23,11 +23,14 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Count; import org.wso2.carbon.device.mgt.common.Count;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.impl.AbstractDeviceDAOImpl; import org.wso2.carbon.device.mgt.core.dao.impl.AbstractDeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.report.mgt.Constants;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -858,6 +861,124 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
} }
} }
@Override
public List<Device> getDevicesExpiredByOSVersion(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
try {
Long osValue = (Long) request.getProperty(Constants.OS_VALUE);
Connection conn = getConnection();
String dataSourceType = conn.getMetaData().getDatabaseProductName();
String sql="SELECT " +
"d1.DEVICE_TYPE, " +
"d1.DEVICE_ID, " +
"d1.DEVICE_NAME, " +
"d1.DESCRIPTION, " +
"d1.DEVICE_IDENTIFICATION, " +
"ddd.OS_VERSION, " +
"e.ID AS ENROLMENT_ID, " +
"e.OWNER, " +
"e.OWNERSHIP, " +
"e.STATUS, " +
"e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT " +
"FROM DM_DEVICE_INFO ddi," +
"DM_DEVICE_DETAIL ddd, " +
"DM_ENROLMENT e, " +
"(SELECT dt.NAME AS DEVICE_TYPE, " +
"d.ID AS DEVICE_ID, " +
"d.NAME AS DEVICE_NAME, " +
"DESCRIPTION, " +
"DEVICE_IDENTIFICATION " +
"FROM DM_DEVICE_TYPE dt, " +
"DM_DEVICE d " +
"WHERE dt.NAME = ? " +
"AND PROVIDER_TENANT_ID = ? " +
"AND dt.ID = d.DEVICE_TYPE_ID " +
") d1 " +
"WHERE d1.DEVICE_ID = e.DEVICE_ID " +
"AND d1.DEVICE_ID = ddi.DEVICE_ID " +
"AND ddi.KEY_FIELD = ? ";
if (dataSourceType.contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2)) {
sql += "AND CAST( ddi.VALUE_FIELD AS BIGINT ) < ? ";
} else if (dataSourceType.contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL)) {
sql += "AND CAST( ddi.VALUE_FIELD AS UNSIGNED ) < ? ";
}
sql += "LIMIT ? OFFSET ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, request.getDeviceType());
ps.setInt(2, tenantId);
ps.setString(3, Constants.OS_VALUE);
ps.setLong(4, osValue);
ps.setInt(5, request.getRowCount());
ps.setInt(6, request.getStartIndex());
try (ResultSet rs = ps.executeQuery()) {
List<Device> devices = new ArrayList<>();
while (rs.next()) {
Device device = DeviceManagementDAOUtil.loadDevice(rs);
DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.setOsVersion(rs.getString("OS_VERSION"));
device.setDeviceInfo(deviceInfo);
devices.add(device);
}
return devices;
}
}
} catch (SQLException e) {
String msg = "Error occurred while building or executing queries to retrieve information " +
"of devices with an older OS build date";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
@Override
public int getCountOfDeviceExpiredByOSVersion(String deviceType, Long osValue, int tenantId)
throws DeviceManagementDAOException {
try {
Connection conn = getConnection();
String dataSourceType = conn.getMetaData().getDatabaseProductName();
String sql = "SELECT " +
"COUNT(ddi.DEVICE_ID) AS DEVICE_COUNT " +
"FROM DM_DEVICE_INFO ddi, " +
"(SELECT d.ID AS DEVICE_ID " +
"FROM DM_DEVICE_TYPE dt, " +
"DM_DEVICE d " +
"WHERE dt.NAME = ? " +
"AND PROVIDER_TENANT_ID = ? " +
"AND dt.ID = d.DEVICE_TYPE_ID " +
") d1 " +
"WHERE d1.DEVICE_ID = ddi.DEVICE_ID " +
"AND ddi.KEY_FIELD = ? ";
if (dataSourceType.contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2)) {
sql += "AND CAST( ddi.VALUE_FIELD AS BIGINT ) < ? ";
} else if (dataSourceType.contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL)) {
sql += "AND CAST( ddi.VALUE_FIELD AS UNSIGNED ) < ? ";
}
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, deviceType);
ps.setInt(2, tenantId);
ps.setString(3, Constants.OS_VALUE);
ps.setLong(4, osValue);
try (ResultSet rs = ps.executeQuery()) {
int deviceCount = 0;
if (rs.next()) {
deviceCount = rs.getInt("DEVICE_COUNT");
}
return deviceCount;
}
}
} catch (SQLException e) {
String msg = "Error occurred while building or executing queries to retrieve the count " +
"of devices with an older OS build date";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
private Connection getConnection() throws SQLException { private Connection getConnection() throws SQLException {
return DeviceManagementDAOFactory.getConnection(); return DeviceManagementDAOFactory.getConnection();
} }

@ -834,52 +834,57 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
public List<Device> getDevicesExpiredByOSVersion(PaginationRequest request, int tenantId) public List<Device> getDevicesExpiredByOSVersion(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
try { try {
Long osBuildDate = (Long) request.getProperty(Constants.OS_BUILD_DATE); Long osValue = (Long) request.getProperty(Constants.OS_VALUE);
Connection conn = getConnection(); Connection conn = getConnection();
String sql = "SELECT " + String sql="SELECT " +
"dt.NAME AS DEVICE_TYPE, " + "d1.DEVICE_TYPE, " +
"d.ID AS DEVICE_ID, " + "d1.DEVICE_ID, " +
"d.NAME AS DEVICE_NAME, " + "d1.DEVICE_NAME, " +
"d.DESCRIPTION, " + "d1.DESCRIPTION, " +
"d.DEVICE_IDENTIFICATION, " + "d1.DEVICE_IDENTIFICATION, " +
"dd.OS_VERSION, " + "ddd.OS_VERSION, " +
"dd.OS_BUILD_DATE, " + "e.ID AS ENROLMENT_ID, " +
"e.ID AS ENROLMENT_ID, " + "e.OWNER, " +
"e.OWNER, " + "e.OWNERSHIP, " +
"e.OWNERSHIP, " + "e.STATUS, " +
"e.STATUS, " + "e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_LAST_UPDATE, " + "e.DATE_OF_ENROLMENT " +
"e.DATE_OF_ENROLMENT " + "FROM DM_DEVICE_INFO ddi, " +
"FROM DM_DEVICE d, " + "DM_DEVICE_DETAIL ddd, " +
"DM_DEVICE_DETAIL dd, " + "DM_ENROLMENT e, " +
"DM_ENROLMENT e, " + "(SELECT dt.NAME AS DEVICE_TYPE, " +
"(SELECT ID, NAME " + "d.ID AS DEVICE_ID, " +
"FROM DM_DEVICE_TYPE " + "d.NAME AS DEVICE_NAME, " +
"WHERE NAME = ? " + "DESCRIPTION, " +
"AND PROVIDER_TENANT_ID = ?) dt " + "DEVICE_IDENTIFICATION " +
"WHERE dt.ID = d.DEVICE_TYPE_ID " + " FROM DM_DEVICE_TYPE dt, " +
"AND d.ID = e.DEVICE_ID " + "DM_DEVICE d " +
"AND d.ID = dd.DEVICE_ID " + "WHERE dt.NAME = ? " +
"AND dd.OS_BUILD_DATE < ? " + "AND PROVIDER_TENANT_ID = ? " +
"ORDER BY ENROLMENT_ID " + "AND dt.ID = d.DEVICE_TYPE_ID " +
"OFFSET ? ROWS " + ") d1 " +
"FETCH NEXT ? ROWS ONLY"; "WHERE d1.DEVICE_ID = e.DEVICE_ID " +
"AND d1.DEVICE_ID = ddi.DEVICE_ID " +
"AND ddi.KEY_FIELD = ? " +
"AND CAST( ddi.VALUE_FIELD AS BIGINT ) < ? " +
"ORDER BY ENROLMENT_ID " +
"OFFSET ? ROWS " +
"FETCH NEXT ? ROWS ONLY";
try (PreparedStatement ps = conn.prepareStatement(sql)) { try (PreparedStatement ps = conn.prepareStatement(sql)) {
int paramIDx = 1; ps.setString(1, request.getDeviceType());
ps.setString(paramIDx++, request.getDeviceType()); ps.setInt(2, tenantId);
ps.setInt(paramIDx++, tenantId); ps.setString(3, Constants.OS_VALUE);
ps.setLong(paramIDx++, osBuildDate); ps.setLong(4, osValue);
ps.setInt(paramIDx++, request.getStartIndex()); ps.setInt(5, request.getStartIndex());
ps.setInt(paramIDx, request.getRowCount()); ps.setInt(6, request.getRowCount());
try (ResultSet rs = ps.executeQuery()) { try (ResultSet rs = ps.executeQuery()) {
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();
DeviceInfo deviceInfo = new DeviceInfo();
while (rs.next()) { while (rs.next()) {
Device device = DeviceManagementDAOUtil.loadDevice(rs); Device device = DeviceManagementDAOUtil.loadDevice(rs);
deviceInfo.setOsVersion(rs.getString(Constants.OS_VERSION)); DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.setOsBuildDate(rs.getString(Constants.OS_BUILD_DATE)); deviceInfo.setOsVersion(rs.getString("OS_VERSION"));
device.setDeviceInfo(deviceInfo); device.setDeviceInfo(deviceInfo);
devices.add(device); devices.add(device);
} }
@ -887,8 +892,49 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while retrieving information of devices with an older OS date " + String msg = "Error occurred while building or executing queries to retrieve information " +
"than the minimum date"; "of devices with an older OS build date";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
@Override
public int getCountOfDeviceExpiredByOSVersion(String deviceType, Long osValue, int tenantId)
throws DeviceManagementDAOException {
try {
Connection conn = getConnection();
String sql = "SELECT " +
"COUNT(ddi.DEVICE_ID) AS DEVICE_COUNT " +
"FROM DM_DEVICE_INFO ddi, " +
"(SELECT d.ID AS DEVICE_ID " +
"FROM DM_DEVICE_TYPE dt, " +
"DM_DEVICE d " +
"WHERE dt.NAME = ? " +
"AND PROVIDER_TENANT_ID = ? " +
"AND dt.ID = d.DEVICE_TYPE_ID " +
") d1 " +
"WHERE d1.DEVICE_ID = ddi.DEVICE_ID " +
"AND ddi.KEY_FIELD = ? " +
"AND CAST( ddi.VALUE_FIELD AS BIGINT ) < ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, deviceType);
ps.setInt(2, tenantId);
ps.setString(3, Constants.OS_VALUE);
ps.setLong(4, osValue);
try (ResultSet rs = ps.executeQuery()) {
int deviceCount = 0;
if (rs.next()) {
deviceCount = rs.getInt("DEVICE_COUNT");
}
return deviceCount;
}
}
} catch (SQLException e) {
String msg = "Error occurred while building or executing queries to retrieve the count " +
"of devices with an older OS build date";
log.error(msg, e); log.error(msg, e);
throw new DeviceManagementDAOException(msg, e); throw new DeviceManagementDAOException(msg, e);
} }

@ -24,10 +24,12 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Count; import org.wso2.carbon.device.mgt.common.Count;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.impl.AbstractDeviceDAOImpl; import org.wso2.carbon.device.mgt.core.dao.impl.AbstractDeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.report.mgt.Constants;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -814,6 +816,114 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
} }
} }
@Override
public List<Device> getDevicesExpiredByOSVersion(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
try {
Long osValue = (Long) request.getProperty(Constants.OS_VALUE);
Connection conn = getConnection();
String sql="SELECT " +
"d1.DEVICE_TYPE, " +
"d1.DEVICE_ID, " +
"d1.DEVICE_NAME, " +
"d1.DESCRIPTION, " +
"d1.DEVICE_IDENTIFICATION, " +
"ddd.OS_VERSION, " +
"e.ID AS ENROLMENT_ID, " +
"e.OWNER, " +
"e.OWNERSHIP, " +
"e.STATUS, " +
"e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT " +
"FROM DM_DEVICE_INFO ddi, " +
"DM_DEVICE_DETAIL ddd, " +
"DM_ENROLMENT e, " +
"(SELECT dt.NAME AS DEVICE_TYPE, " +
"d.ID AS DEVICE_ID, " +
"d.NAME AS DEVICE_NAME, " +
"DESCRIPTION, " +
"DEVICE_IDENTIFICATION " +
" FROM DM_DEVICE_TYPE dt, " +
"DM_DEVICE d " +
"WHERE dt.NAME = ? " +
"AND PROVIDER_TENANT_ID = ? " +
"AND dt.ID = d.DEVICE_TYPE_ID " +
") d1 " +
"WHERE d1.DEVICE_ID = e.DEVICE_ID " +
"AND d1.DEVICE_ID = ddi.DEVICE_ID " +
"AND ddi.KEY_FIELD = ? " +
"AND CAST( ddi.VALUE_FIELD AS BIGINT ) < ? " +
"LIMIT ? OFFSET ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, request.getDeviceType());
ps.setInt(2, tenantId);
ps.setString(3, Constants.OS_VALUE);
ps.setLong(4, osValue);
ps.setInt(5, request.getRowCount());
ps.setInt(6, request.getStartIndex());
try (ResultSet rs = ps.executeQuery()) {
List<Device> devices = new ArrayList<>();
while (rs.next()) {
Device device = DeviceManagementDAOUtil.loadDevice(rs);
DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.setOsVersion(rs.getString("OS_VERSION"));
device.setDeviceInfo(deviceInfo);
devices.add(device);
}
return devices;
}
}
} catch (SQLException e) {
String msg = "Error occurred while building or executing queries to retrieve information " +
"of devices with an older OS build date";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
@Override
public int getCountOfDeviceExpiredByOSVersion(String deviceType, Long osValue, int tenantId)
throws DeviceManagementDAOException {
try {
Connection conn = getConnection();
String sql = "SELECT " +
"COUNT(ddi.DEVICE_ID) AS DEVICE_COUNT " +
"FROM DM_DEVICE_INFO ddi, " +
"(SELECT d.ID AS DEVICE_ID " +
"FROM DM_DEVICE_TYPE dt, " +
"DM_DEVICE d " +
"WHERE dt.NAME = ? " +
"AND PROVIDER_TENANT_ID = ? " +
"AND dt.ID = d.DEVICE_TYPE_ID " +
") d1 " +
"WHERE d1.DEVICE_ID = ddi.DEVICE_ID " +
"AND ddi.KEY_FIELD = ? " +
"AND CAST( ddi.VALUE_FIELD AS BIGINT ) < ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, deviceType);
ps.setInt(2, tenantId);
ps.setString(3, Constants.OS_VALUE);
ps.setLong(4, osValue);
try (ResultSet rs = ps.executeQuery()) {
int deviceCount = 0;
if (rs.next()) {
deviceCount = rs.getInt("DEVICE_COUNT");
}
return deviceCount;
}
}
} catch (SQLException e) {
String msg = "Error occurred while building or executing queries to retrieve the count " +
"of devices with an older OS build date";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
private Connection getConnection() throws SQLException { private Connection getConnection() throws SQLException {
return DeviceManagementDAOFactory.getConnection(); return DeviceManagementDAOFactory.getConnection();
} }

@ -24,12 +24,14 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Count; import org.wso2.carbon.device.mgt.common.Count;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.impl.AbstractDeviceDAOImpl; import org.wso2.carbon.device.mgt.core.dao.impl.AbstractDeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.geo.GeoCluster; import org.wso2.carbon.device.mgt.core.geo.GeoCluster;
import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate; import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate;
import org.wso2.carbon.device.mgt.core.report.mgt.Constants;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -630,6 +632,114 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
} }
} }
@Override
public List<Device> getDevicesExpiredByOSVersion(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
try {
Long osValue = (Long) request.getProperty(Constants.OS_VALUE);
Connection conn = getConnection();
String sql="SELECT " +
"d1.DEVICE_TYPE, " +
"d1.DEVICE_ID, " +
"d1.DEVICE_NAME, " +
"d1.DESCRIPTION, " +
"d1.DEVICE_IDENTIFICATION, " +
"ddd.OS_VERSION, " +
"e.ID AS ENROLMENT_ID, " +
"e.OWNER, " +
"e.OWNERSHIP, " +
"e.STATUS, " +
"e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT " +
"FROM DM_DEVICE_INFO ddi, " +
"DM_DEVICE_DETAIL ddd, " +
"DM_ENROLMENT e, " +
"(SELECT dt.NAME AS DEVICE_TYPE, " +
"d.ID AS DEVICE_ID, " +
"d.NAME AS DEVICE_NAME, " +
"DESCRIPTION, " +
"DEVICE_IDENTIFICATION " +
" FROM DM_DEVICE_TYPE dt, " +
"DM_DEVICE d " +
"WHERE dt.NAME = ? " +
"AND PROVIDER_TENANT_ID = ? " +
"AND dt.ID = d.DEVICE_TYPE_ID " +
") d1 " +
"WHERE d1.DEVICE_ID = e.DEVICE_ID " +
"AND d1.DEVICE_ID = ddi.DEVICE_ID " +
"AND ddi.KEY_FIELD = ? " +
"AND CAST( ddi.VALUE_FIELD AS BIGINT ) < ? " +
"LIMIT ? OFFSET ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, request.getDeviceType());
ps.setInt(2, tenantId);
ps.setString(3, Constants.OS_VALUE);
ps.setLong(4, osValue);
ps.setInt(5, request.getRowCount());
ps.setInt(6, request.getStartIndex());
try (ResultSet rs = ps.executeQuery()) {
List<Device> devices = new ArrayList<>();
while (rs.next()) {
Device device = DeviceManagementDAOUtil.loadDevice(rs);
DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.setOsVersion(rs.getString("OS_VERSION"));
device.setDeviceInfo(deviceInfo);
devices.add(device);
}
return devices;
}
}
} catch (SQLException e) {
String msg = "Error occurred while building or executing queries to retrieve information " +
"of devices with an older OS build date";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
@Override
public int getCountOfDeviceExpiredByOSVersion(String deviceType, Long osValue, int tenantId)
throws DeviceManagementDAOException {
try {
Connection conn = getConnection();
String sql = "SELECT " +
"COUNT(ddi.DEVICE_ID) AS DEVICE_COUNT " +
"FROM DM_DEVICE_INFO ddi, " +
"(SELECT d.ID AS DEVICE_ID " +
"FROM DM_DEVICE_TYPE dt, " +
"DM_DEVICE d " +
"WHERE dt.NAME = ? " +
"AND PROVIDER_TENANT_ID = ? " +
"AND dt.ID = d.DEVICE_TYPE_ID " +
") d1 " +
"WHERE d1.DEVICE_ID = ddi.DEVICE_ID " +
"AND ddi.KEY_FIELD = ? " +
"AND CAST( ddi.VALUE_FIELD AS BIGINT ) < ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, deviceType);
ps.setInt(2, tenantId);
ps.setString(3, Constants.OS_VALUE);
ps.setLong(4, osValue);
try (ResultSet rs = ps.executeQuery()) {
int deviceCount = 0;
if (rs.next()) {
deviceCount = rs.getInt("DEVICE_COUNT");
}
return deviceCount;
}
}
} catch (SQLException e) {
String msg = "Error occurred while building or executing queries to retrieve the count " +
"of devices with an older OS build date";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
private Connection getConnection() throws SQLException { private Connection getConnection() throws SQLException {
return DeviceManagementDAOFactory.getConnection(); return DeviceManagementDAOFactory.getConnection();
} }

@ -18,6 +18,7 @@
package org.wso2.carbon.device.mgt.core.device.details.mgt.impl; package org.wso2.carbon.device.mgt.core.device.details.mgt.impl;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.CarbonContext;
@ -39,6 +40,7 @@ import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManag
import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO;
import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDAOException; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDAOException;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.report.mgt.Constants;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import org.wso2.carbon.device.mgt.core.util.HttpReportingUtil; import org.wso2.carbon.device.mgt.core.util.HttpReportingUtil;
@ -98,6 +100,8 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
} else { } else {
Map<String, String> updatableProps = new HashMap<>(); Map<String, String> updatableProps = new HashMap<>();
Map<String, String> injectableProps = new HashMap<>(); Map<String, String> injectableProps = new HashMap<>();
// generate a default value depending on the devices OS version
addOSVersionValue(device, newDeviceInfo);
for (String key : newDeviceInfo.getDeviceDetailsMap().keySet()) { for (String key : newDeviceInfo.getDeviceDetailsMap().keySet()) {
if (previousDeviceProperties.containsKey(key)) { if (previousDeviceProperties.containsKey(key)) {
updatableProps.put(key, newDeviceInfo.getDeviceDetailsMap().get(key)); updatableProps.put(key, newDeviceInfo.getDeviceDetailsMap().get(key));
@ -420,5 +424,39 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
return newDeviceInfo; return newDeviceInfo;
} }
/**
* Generate and add a value depending on the device's OS version included in device info
*
* @param device device data
* @param newDeviceInfo device info data
*/
private void addOSVersionValue(Device device, DeviceInfo newDeviceInfo) {
String deviceTypeName = device.getType();
if (StringUtils.isBlank(device.getType())) {
if (log.isDebugEnabled()) {
log.debug("Unable to generate a OS version value for device type: " +
deviceTypeName + ". Device type cannot be null or empty");
}
} else {
if (!deviceTypeName.equals("android") && !deviceTypeName.equals("ios")) {
if (log.isDebugEnabled()) {
log.debug("Unable to generate a OS version value for device type: " +
deviceTypeName + ". OS version value is only generatable for " +
"android and ios");
}
} else {
String osVersion = newDeviceInfo.getOsVersion();
String osValue = String.valueOf(DeviceManagerUtil.generateOSVersionValue(osVersion));
if (StringUtils.isBlank(osValue)) {
if (log.isDebugEnabled()) {
log.debug("Unable to generate a OS version value for OS version: " +
osVersion + " for device type: " + deviceTypeName);
}
} else {
newDeviceInfo.getDeviceDetailsMap().put(Constants.OS_VALUE, osValue);
}
}
}
}
} }

@ -1,27 +1,35 @@
/* /*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* *
* WSO2 Inc. licenses this file to you under the Apache License, * Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * Entgra (pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* *
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* 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; package org.wso2.carbon.device.mgt.core.report.mgt;
public class Constants { public class Constants {
// device types
// device properties public static final String ANDROID = "android";
public static final String IOS = "ios";
// device properties
public static final String OS_BUILD_DATE = "OS_BUILD_DATE"; public static final String OS_BUILD_DATE = "OS_BUILD_DATE";
public static final String OS_VERSION = "OS_VERSION"; public static final String OS_VERSION = "OS_VERSION";
public static final String OS_VALUE = "OS_VALUE";
// OS version value generating properties
public static final int NUM_OF_OS_VERSION_DIGITS= 5;
public static final int NUM_OF_OS_VERSION_POSITIONS = 3;
} }

@ -23,6 +23,9 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Count; import org.wso2.carbon.device.mgt.common.Count;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.BadRequestException;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.PaginationResult;
@ -157,46 +160,55 @@ public class ReportManagementServiceImpl implements ReportManagementService {
@Override @Override
public PaginationResult getDevicesExpiredByOSVersion(PaginationRequest request) public PaginationResult getDevicesExpiredByOSVersion(PaginationRequest request)
throws ReportManagementException, DeviceTypeNotFoundException { throws ReportManagementException, BadRequestException {
if (request == null || if (request == null ||
StringUtils.isBlank(request.getDeviceType()) || StringUtils.isBlank(request.getDeviceType()) ||
!request.getProperties().containsKey(Constants.OS_BUILD_DATE) || request.getProperties() == null ||
(Long) request.getProperty(Constants.OS_BUILD_DATE) == 0) { !request.getProperties().containsKey(Constants.OS_VERSION) ||
StringUtils.isBlank((String) request.getProperty(Constants.OS_VERSION))) {
String msg = "Error Invalid data received from the request.\n" + String msg = "Error Invalid data received from the request.\n" +
"osBuildDate cannot be null or 0 and device type cannot be null or empty"; "osVersion and device type cannot be null or empty.";
log.error(msg); log.error(msg);
throw new ReportManagementException(msg); throw new BadRequestException(msg);
} }
String deviceType = request.getDeviceType();
if (!deviceType.equals(Constants.ANDROID) && !deviceType.equals(Constants.IOS)) {
String msg = "Error Invalid device type:" + deviceType + " received. Valid device types " +
"are android and ios.";
log.error(msg);
throw new BadRequestException(msg);
}
try { try {
int tenantId = DeviceManagementDAOUtil.getTenantId(); int tenantId = DeviceManagementDAOUtil.getTenantId();
String deviceType = request.getDeviceType();
PaginationResult paginationResult = new PaginationResult(); PaginationResult paginationResult = new PaginationResult();
DeviceManagerUtil.validateDeviceListPageSize(request); DeviceManagerUtil.validateDeviceListPageSize(request);
DeviceType deviceTypeObj = DeviceManagerUtil.getDeviceType( String osVersion = (String) request.getProperty(Constants.OS_VERSION);
deviceType, tenantId); Long osVersionValue = DeviceManagerUtil.generateOSVersionValue(osVersion);
if (deviceTypeObj == null) { if (osVersionValue == null){
String msg = "Error, device of type: " + deviceType + " does not exist"; String msg = "Failed to generate OS value, received OS version: " + osVersion +
" is in incorrect format([0-9]+([.][0-9]+)*) or version is invalid.";
log.error(msg); log.error(msg);
throw new DeviceTypeNotFoundException(msg); throw new BadRequestException(msg);
} }
request.setProperty(Constants.OS_VALUE, osVersionValue);
try { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
List<Device> devices = deviceDAO.getDevicesExpiredByOSVersion(request, tenantId);
List<Device> devices = deviceDAO.getDevicesExpiredByOSVersion(
request, tenantId);
int deviceCount = deviceDAO.getCountOfDeviceExpiredByOSVersion( int deviceCount = deviceDAO.getCountOfDeviceExpiredByOSVersion(
deviceType, deviceType, osVersionValue, tenantId);
(Long) request.getProperty(Constants.OS_BUILD_DATE),
tenantId);
paginationResult.setData(devices); paginationResult.setData(devices);
paginationResult.setRecordsFiltered(devices.size()); paginationResult.setRecordsFiltered(devices.size());
paginationResult.setRecordsTotal(deviceCount); paginationResult.setRecordsTotal(deviceCount);
return paginationResult; return paginationResult;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while opening a connection to the data source"; String msg = "Error occurred while opening a connection to the data source.";
log.error(msg, e); log.error(msg, e);
throw new ReportManagementException(msg, e); throw new ReportManagementException(msg, e);
} finally { } finally {
@ -204,12 +216,12 @@ public class ReportManagementServiceImpl implements ReportManagementService {
} }
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
String msg = "Error occurred while retrieving expired devices by a OS build date " + String msg = "Error occurred while retrieving expired devices by a OS version " +
"for the tenant"; "for the tenant.";
log.error(msg, e); log.error(msg, e);
throw new ReportManagementException(msg, e); throw new ReportManagementException(msg, e);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "Error occurred while validating the request"; String msg = "Error occurred while validating the request.";
log.error(msg, e); log.error(msg, e);
throw new ReportManagementException(msg, e); throw new ReportManagementException(msg, e);
} }

@ -60,6 +60,7 @@ import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.operation.mgt.util.DeviceIDHolder; import org.wso2.carbon.device.mgt.core.operation.mgt.util.DeviceIDHolder;
import org.wso2.carbon.device.mgt.core.report.mgt.Constants;
import org.wso2.carbon.identity.jwt.client.extension.JWTClient; import org.wso2.carbon.identity.jwt.client.extension.JWTClient;
import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo; import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo;
import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException;
@ -89,7 +90,11 @@ import java.util.HashMap;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.StringJoiner;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
public final class DeviceManagerUtil { public final class DeviceManagerUtil {
@ -694,4 +699,86 @@ public final class DeviceManagerUtil {
JWTClient jwtClient = jwtClientManagerService.getJWTClient(); JWTClient jwtClient = jwtClientManagerService.getJWTClient();
return jwtClient.getAccessToken(clientId, clientSecret, deviceOwner, scopes); return jwtClient.getAccessToken(clientId, clientSecret, deviceOwner, scopes);
} }
/**
* <h1>Generate a value for the passed os version</h1>
*
* <p>Value is generated by isolating each position of OS version then adding Zeros up until
* Minor version and Revision have a constant number of digits.
* <i>Eg: 5.1.1 will be 50000100001, 9 will be 90000000000</i>
* </p>
*
* <p>Above conversion is done in order to fail proof in situations where 6.0.0 can be
* smaller than 5.12.1.
* <i>Eg: 5.12.1 will be 5121 and 6.0.0 will be 600 and 5121 > 600(this statement is incorrect)</i>
* </p>
*
* @param osVersion os version(eg: 5.1.1)
* @return {@link Long} generated value
*/
public static Long generateOSVersionValue(String osVersion) {
Matcher osMatcher = Pattern.compile("[0-9]+([.][0-9]+)*").matcher(osVersion);
if (!osMatcher.find()) {
if (log.isDebugEnabled()) {
log.debug("Unable to read OS version. OS version: " + osVersion + "is invalid. " +
"Please follow the following convention [0-9]+([.][0-9]+)*");
}
return null;
}
osVersion = osMatcher.group();
String[] osVersions = osVersion.split("[.]");
int osVersionsLength = osVersions.length;
/*
* <h1>Following loop will work for the value generation</h1>
* <p>
{@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
of Zeros can be generated.
* </p>
*
* <p>
{@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.
* </p>
*/
return IntStream
.range(0, osVersionsLength)
.mapToLong(i -> (long) (Math.pow(10, (Constants.NUM_OF_OS_VERSION_POSITIONS - (i + 1))
* Constants.NUM_OF_OS_VERSION_DIGITS)
* Integer.parseInt(osVersions[i]))).sum();
}
/**
* Revert a generated value back to a OS version
*
* @param osVersionValue value that should be reverted
* @return {@link String} OS version
*/
/* Following method is unused but was still included in case a requirement occurs to revert the
* generated values in DM_DEVICE_INFO back to a OS versions */
public static String reverseOSVersionValue(Long osVersionValue) {
StringJoiner joiner = new StringJoiner(".");
/*
* <h1>Following loop will break the generated value into parts and will recreate the OS version</h1>
* <p>
{@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,
Revision.
* </p>
*
*/
for (int i = Constants.NUM_OF_OS_VERSION_POSITIONS; i > 0; i--) {
long osVersion = Double.valueOf(
osVersionValue / Math.pow(10, (i - 1) * Constants.NUM_OF_OS_VERSION_DIGITS))
.longValue();
osVersionValue = Double.valueOf(
osVersionValue % Math.pow(10, (i - 1) * Constants.NUM_OF_OS_VERSION_DIGITS))
.longValue();
joiner.add(String.valueOf(osVersion));
}
return joiner.toString();
}
} }

Loading…
Cancel
Save