Merge branch 'reporting' into 'reporting'

Reporting

See merge request entgra/carbon-device-mgt!470
feature/appm-store/pbac
Inosh Perara 5 years ago
commit 453b6b8596

@ -123,6 +123,12 @@ class App extends React.Component {
)
.then(res => {
config.deviceTypes = JSON.parse(res.data.data);
config.supportedOStypes = [];
config.deviceTypes.forEach(type => {
if (['ios', 'android'].includes(type.name)) {
config.supportedOStypes.push(type);
}
});
this.setState({
config: config,
loading: false,

@ -31,6 +31,7 @@ import DeviceStatusReport from './scenes/Home/scenes/Reports/scenes/DeviceStatus
import AppNotInstalledDevicesReport from './scenes/Home/scenes/Reports/scenes/AppNotInstalledDevices';
import Geo from './scenes/Home/scenes/Geo';
import EncryptionStatus from './scenes/Home/scenes/Reports/scenes/EncryptionStatus';
import OutdatedOSversionReport from './scenes/Home/scenes/Reports/scenes/OutdatedOSVersion';
const routes = [
{
@ -113,6 +114,11 @@ const routes = [
component: EnrollmentsVsUnenrollmentsReport,
exact: true,
},
{
path: '/entgra/reports/expired-devices',
component: OutdatedOSversionReport,
exact: true,
},
{
path: '/entgra/reports/enrollment-type',
component: EnrollmentTypeReport,

@ -116,6 +116,12 @@ const columns = [
},
// todo add filtering options
},
{
title: 'OS Version',
dataIndex: 'deviceInfo',
key: 'osVersion',
render: deviceInfo => deviceInfo.osVersion,
},
];
const getTimeAgo = time => {

@ -157,6 +157,35 @@ class Reports extends React.Component {
</Link>
</Col>
<Col span={8}>
<Link
to={{
// Path to respective report page
pathname: '/entgra/reports/expired-devices',
data: {
name: 'expired_devices_report',
},
}}
>
<Card
bordered={true}
hoverable={true}
style={{ borderRadius: 10, marginBottom: 16 }}
>
<div align="center">
<Icon
type="desktop"
style={{ fontSize: '25px', color: '#08c' }}
/>
<h2>
<b>Expired Devices Report</b>
</h2>
<p>Report based on device OS version</p>
</div>
</Card>
</Link>
</Col>
<Col span={8}>
<Link
to={{

@ -0,0 +1,179 @@
/*
* 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.
*/
import React from 'react';
import axios from 'axios';
import {
PageHeader,
Breadcrumb,
Icon,
Button,
Select,
message,
notification,
} from 'antd';
import { Link } from 'react-router-dom';
import { withConfigContext } from '../../../../../../components/ConfigContext';
import DeviceTable from '../../components/DevicesTable';
let config = null;
const { Option } = Select;
let { typeSelected, versionSelected } = false;
class OutdatedOSversionReport extends React.Component {
routes;
constructor(props) {
super(props);
this.routes = props.routes;
config = this.props.context;
this.state = {
deviceType: null,
osVersion: null,
apiURL: null,
visible: false,
supportedOsVersions: [],
};
}
onDeviceTypeChange = value => {
typeSelected = true;
this.setState({ deviceType: value });
this.getSupportedOsVersions(value);
};
onOSVersionChange = value => {
versionSelected = true;
this.setState({ osVersion: value });
};
onClickGenerateButton = () => {
const { osVersion, deviceType } = this.state;
if (osVersion && deviceType != null) {
let apiURL =
window.location.origin +
config.serverConfig.invoker.uri +
config.serverConfig.invoker.deviceMgt +
`/reports/expired-devices/${deviceType}?osVersion=${osVersion}&`;
this.setState({ apiURL });
}
};
getSupportedOsVersions = deviceType => {
const config = this.props.context;
axios
.get(
window.location.origin +
config.serverConfig.invoker.uri +
config.serverConfig.invoker.deviceMgt +
`/admin/device-types/${deviceType}/versions`,
)
.then(res => {
if (res.status === 200) {
let supportedOsVersions = JSON.parse(res.data.data);
this.setState({
supportedOsVersions,
});
}
})
.catch(error => {
if (error.hasOwnProperty('response') && error.response.status === 401) {
message.error('You are not logged in');
window.location.href = window.location.origin + '/entgra/login';
} else {
notification.error({
message: 'There was a problem',
duration: 0,
description: 'Error occurred while trying to load OS Versions.',
});
}
});
};
render() {
const { apiURL, supportedOsVersions } = this.state;
return (
<div>
<PageHeader style={{ paddingTop: 0 }}>
<Breadcrumb style={{ paddingBottom: 16 }}>
<Breadcrumb.Item>
<Link to="/entgra">
<Icon type="home" /> Home
</Link>
</Breadcrumb.Item>
<Breadcrumb.Item>
<Link to="/entgra/reports">Reports</Link>
</Breadcrumb.Item>
<Breadcrumb.Item>Expired Devices</Breadcrumb.Item>
</Breadcrumb>
<div className="wrap" style={{ marginBottom: '10px' }}>
<h3>Expired Devices Report</h3>
<div className="wrap" style={{ marginBottom: '10px' }}>
<Select
showSearch
style={{ width: 200, marginLeft: '10px' }}
placeholder="Select a Device type"
onChange={this.onDeviceTypeChange}
>
{config.supportedOStypes.map(data => (
<Option value={data.name} key={data.id}>
{data.name}
</Option>
))}
</Select>
<Select
style={{ width: 200, marginLeft: '10px' }}
placeholder="
Select an OS Version"
onChange={this.onOSVersionChange}
>
{supportedOsVersions.map(data => {
return (
<Option key={data.versionName}>{data.versionName}</Option>
);
})}
</Select>
{typeSelected && versionSelected ? (
<Button
type="primary"
onClick={this.onClickGenerateButton}
style={{ marginLeft: '10px' }}
>
Generate Report
</Button>
) : null}
</div>
<div
id="table"
style={{ backgroundColor: '#ffffff', borderRadius: 5 }}
>
<DeviceTable apiUrl={apiURL} />
</div>
</div>
</PageHeader>
<div
style={{ background: '#f0f2f5', padding: 24, minHeight: 720 }}
></div>
</div>
);
}
}
export default withConfigContext(OutdatedOSversionReport);

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

@ -49,6 +49,7 @@ import org.apache.axis2.transport.http.HTTPConstants;
import org.wso2.carbon.apimgt.annotations.api.Scope;
import org.wso2.carbon.apimgt.annotations.api.Scopes;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.general.TenantDetail;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
@ -100,6 +101,12 @@ import java.util.List;
description = "Permanently Delete the device specified by device id",
key = "perm:devices:permanent-delete",
permissions = {"/device-mgt/admin/devices/permanent-delete"}
),
@Scope(
name = "Getting Details of Device tenants",
description = "Getting Details of Device tenants",
key = "perm:admin:tenant:view",
permissions = {"/device-mgt/devices/tenants/view"}
)
}
)
@ -353,4 +360,54 @@ public interface DeviceManagementAdminService {
value = "List of device identifiers.",
required = true)
List<String> deviceIdentifiers);
@GET
@Path("/tenants")
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = "GET",
value = "Getting Details of tenants",
notes = "Get the details of tenants.",
response = TenantDetail.class,
responseContainer = "List",
tags = "Device Management Administrative Service",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = Constants.SCOPE, value =
"perm:admin:tenant:view")
})
}
)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK. \n Successfully fetched the list of tenants.",
response = TenantDetail.class,
responseContainer = "List",
responseHeaders = {
@ResponseHeader(
name = "Content-Type",
description = "The content type of the body"),
@ResponseHeader(
name = "ETag",
description = "Entity Tag of the response resource.\n" +
"Used by caches, or in conditional requests."),
@ResponseHeader(
name = "Last-Modified",
description = "Date and time the resource was last modified.\n" +
"Used by caches, or in conditional requests."),
}),
@ApiResponse(
code = 304,
message = "Not Modified. Empty body because the client already has the latest version of the " +
"requested resource.\n"),
@ApiResponse(
code = 401,
message = "Unauthorized.\n The unauthorized access to the requested resource.",
response = ErrorResponse.class),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Server error occurred while fetching the" +
" tenant list.",
response = ErrorResponse.class)
})
Response getTenants();
}

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

@ -45,13 +45,19 @@ import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException;
import org.wso2.carbon.device.mgt.common.exceptions.UserNotFoundException;
import org.wso2.carbon.device.mgt.common.general.TenantDetail;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.DeviceManagementAdminService;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
import org.wso2.carbon.user.api.Tenant;
import org.wso2.carbon.user.api.TenantManager;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.core.service.RealmService;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Size;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
@ -62,6 +68,7 @@ import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;
@Path("/admin/devices")
@ -189,4 +196,51 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
}
@Override
@Path("/tenants")
@GET
public Response getTenants() {
List<TenantDetail> tenantDetails;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
if (tenantId == MultitenantConstants.SUPER_TENANT_ID) {
RealmService realmService = (RealmService) PrivilegedCarbonContext
.getThreadLocalCarbonContext().getOSGiService(RealmService.class, null);
if (realmService == null) {
String msg = "RealmService is not initialized";
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
try {
Tenant[] tenants = realmService.getTenantManager().getAllTenants();
tenantDetails = new ArrayList<>();
if (tenants != null && tenants.length > 0) {
for (Tenant tenant : tenants) {
TenantDetail tenantDetail = new TenantDetail();
tenantDetail.setId(tenant.getId());
tenantDetail.setAdminFirstName(tenant.getAdminFirstName());
tenantDetail.setAdminFullName(tenant.getAdminFullName());
tenantDetail.setAdminLastName(tenant.getAdminLastName());
tenantDetail.setAdminName(tenant.getAdminName());
tenantDetail.setDomain(tenant.getDomain());
tenantDetail.setEmail(tenant.getEmail());
tenantDetails.add(tenantDetail);
}
return Response.status(Response.Status.OK).entity(tenantDetails).build();
} else {
return Response.status(Response.Status.NOT_FOUND).entity("No tenants found")
.build();
}
} catch (UserStoreException e) {
String msg = "Error occurred while fetching tenant list";
log.error(msg, e);
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
} else {
return Response.status(Response.Status.BAD_REQUEST).entity("This API is available " +
"for super tenant admin only.").build();
}
}
}

@ -0,0 +1,45 @@
/*
* 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);
}
}

@ -0,0 +1,88 @@
/*
* Copyright (c) 2019, Entgra (pvt) Ltd. (https://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.general;
public class TenantDetail {
private int id;
private String domain;
private String adminName;
private String adminFullName;
private String adminFirstName;
private String adminLastName;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getAdminName() {
return adminName;
}
public void setAdminName(String adminName) {
this.adminName = adminName;
}
public String getAdminFullName() {
return adminFullName;
}
public void setAdminFullName(String adminFullName) {
this.adminFullName = adminFullName;
}
public String getAdminFirstName() {
return adminFirstName;
}
public void setAdminFirstName(String adminFirstName) {
this.adminFirstName = adminFirstName;
}
public String getAdminLastName() {
return adminLastName;
}
public void setAdminLastName(String adminLastName) {
this.adminLastName = adminLastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}

@ -21,6 +21,7 @@ import com.google.gson.JsonObject;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
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.exceptions.BadRequestException;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceTypeNotFoundException;
import org.wso2.carbon.device.mgt.common.exceptions.ReportManagementException;
@ -56,10 +57,10 @@ public interface ReportManagementService {
* @param request {@link PaginationRequest}
* @return {@link PaginationResult}
* @throws ReportManagementException Might occur during the business logic or building database query
* @throws DeviceTypeNotFoundException Might occur while validating the device type
* @throws BadRequestException Might occur if the given os version or the device type doesn't match
*/
PaginationResult getDevicesExpiredByOSVersion(PaginationRequest request)
throws ReportManagementException, DeviceTypeNotFoundException;
throws ReportManagementException, BadRequestException;
/**
* 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
*
* @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.
* @return {@link Integer}
* @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;
/**

@ -1916,107 +1916,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
}
}
@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
public List<Device> getAppNotInstalledDevices(
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.wso2.carbon.device.mgt.common.Count;
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.device.details.DeviceInfo;
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.impl.AbstractDeviceDAOImpl;
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.PreparedStatement;
@ -858,6 +861,128 @@ 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();
/* 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, " +
"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 d1.DEVICE_ID = ddd.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();
/* 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 " +
"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()) {
if (rs.next()) {
return rs.getInt("DEVICE_COUNT");
}
return 0;
}
}
} 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 {
return DeviceManagementDAOFactory.getConnection();
}

@ -834,52 +834,58 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
public List<Device> getDevicesExpiredByOSVersion(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
try {
Long osBuildDate = (Long) request.getProperty(Constants.OS_BUILD_DATE);
Long osValue = (Long) request.getProperty(Constants.OS_VALUE);
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, " +
"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 d, " +
"DM_DEVICE_DETAIL dd, " +
"FROM DM_DEVICE_INFO ddi, " +
"DM_DEVICE_DETAIL ddd, " +
"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 < ? " +
"(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 d1.DEVICE_ID = ddd.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)) {
int paramIDx = 1;
ps.setString(paramIDx++, request.getDeviceType());
ps.setInt(paramIDx++, tenantId);
ps.setLong(paramIDx++, osBuildDate);
ps.setInt(paramIDx++, request.getStartIndex());
ps.setInt(paramIDx, request.getRowCount());
ps.setString(1, request.getDeviceType());
ps.setInt(2, tenantId);
ps.setString(3, Constants.OS_VALUE);
ps.setLong(4, osValue);
ps.setInt(5, request.getStartIndex());
ps.setInt(6, request.getRowCount());
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));
DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.setOsVersion(rs.getString("OS_VERSION"));
device.setDeviceInfo(deviceInfo);
devices.add(device);
}
@ -887,8 +893,48 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving information of devices with an older OS date " +
"than the minimum date";
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()) {
if (rs.next()) {
return rs.getInt("DEVICE_COUNT");
}
return 0;
}
}
} 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);
}

@ -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.Device;
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.DeviceManagementDAOFactory;
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.report.mgt.Constants;
import java.sql.Connection;
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 d1.DEVICE_ID = ddd.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()) {
if (rs.next()) {
return rs.getInt("DEVICE_COUNT");
}
return 0;
}
}
} 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 {
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.Device;
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.DeviceManagementDAOFactory;
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.geo.GeoCluster;
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.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 d1.DEVICE_ID = ddd.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()) {
if (rs.next()) {
return rs.getInt("DEVICE_COUNT");
}
return 0;
}
}
} 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 {
return DeviceManagementDAOFactory.getConnection();
}

@ -45,7 +45,9 @@ 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.DeviceDetailsMgtDAOException;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
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.HttpReportingUtil;
import org.wso2.carbon.user.api.UserRealm;
@ -110,6 +112,8 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
} else {
Map<String, String> updatableProps = 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()) {
if (previousDeviceProperties.containsKey(key)) {
updatableProps.put(key, newDeviceInfo.getDeviceDetailsMap().get(key));
@ -464,6 +468,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
return newDeviceInfo;
}
private String[] getRolesOfUser(String userName) throws UserStoreException {
UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm();
String[] roleList;
@ -478,5 +483,40 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
return roleList;
}
/**
* 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,7 +1,7 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* 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
@ -16,12 +16,18 @@
* under the License.
*/
package org.wso2.carbon.device.mgt.core.report.mgt;
public class Constants {
// device types
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_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.wso2.carbon.device.mgt.common.Count;
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.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult;
@ -157,46 +160,55 @@ public class ReportManagementServiceImpl implements ReportManagementService {
@Override
public PaginationResult getDevicesExpiredByOSVersion(PaginationRequest request)
throws ReportManagementException, DeviceTypeNotFoundException {
throws ReportManagementException, BadRequestException {
if (request == null ||
StringUtils.isBlank(request.getDeviceType()) ||
!request.getProperties().containsKey(Constants.OS_BUILD_DATE) ||
(Long) request.getProperty(Constants.OS_BUILD_DATE) == 0) {
request.getProperties() == null ||
!request.getProperties().containsKey(Constants.OS_VERSION) ||
StringUtils.isBlank((String) request.getProperty(Constants.OS_VERSION))) {
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);
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 {
int tenantId = DeviceManagementDAOUtil.getTenantId();
String deviceType = request.getDeviceType();
PaginationResult paginationResult = new PaginationResult();
DeviceManagerUtil.validateDeviceListPageSize(request);
DeviceType deviceTypeObj = DeviceManagerUtil.getDeviceType(
deviceType, tenantId);
if (deviceTypeObj == null) {
String msg = "Error, device of type: " + deviceType + " does not exist";
String osVersion = (String) request.getProperty(Constants.OS_VERSION);
Long osVersionValue = DeviceManagerUtil.generateOSVersionValue(osVersion);
if (osVersionValue == null){
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);
throw new DeviceTypeNotFoundException(msg);
throw new BadRequestException(msg);
}
request.setProperty(Constants.OS_VALUE, osVersionValue);
try {
DeviceManagementDAOFactory.openConnection();
List<Device> devices = deviceDAO.getDevicesExpiredByOSVersion(request, tenantId);
List<Device> devices = deviceDAO.getDevicesExpiredByOSVersion(
request, tenantId);
int deviceCount = deviceDAO.getCountOfDeviceExpiredByOSVersion(
deviceType,
(Long) request.getProperty(Constants.OS_BUILD_DATE),
tenantId);
deviceType, osVersionValue, tenantId);
paginationResult.setData(devices);
paginationResult.setRecordsFiltered(devices.size());
paginationResult.setRecordsTotal(deviceCount);
return paginationResult;
} 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);
throw new ReportManagementException(msg, e);
} finally {
@ -204,12 +216,12 @@ public class ReportManagementServiceImpl implements ReportManagementService {
}
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while retrieving expired devices by a OS build date " +
"for the tenant";
String msg = "Error occurred while retrieving expired devices by a OS version " +
"for the tenant.";
log.error(msg, e);
throw new ReportManagementException(msg, e);
} catch (DeviceManagementException e) {
String msg = "Error occurred while validating the request";
String msg = "Error occurred while validating the request.";
log.error(msg, e);
throw new ReportManagementException(msg, e);
}

@ -3427,7 +3427,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
deviceLocation.setDistance(Double.parseDouble(distance));
deviceLocation.setSpeed(Float.parseFloat(speed));
deviceLocation.setBearing(Float.parseFloat(bearing));
deviceInformationManager.addDeviceLocation(device, deviceLocation);
deviceInformationManager.addDeviceLocation(deviceLocation);
} catch (Exception e) {
//We are not failing the execution since this is not critical for the functionality. But logging as
// a warning for reference.

@ -66,6 +66,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.internal.DeviceManagementDataHolder;
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.dto.AccessTokenInfo;
import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException;
@ -95,7 +96,11 @@ import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
public final class DeviceManagerUtil {
@ -730,4 +735,109 @@ public final class DeviceManagerUtil {
}
return null;
}
/**
* <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>Equation explanation</h1>
*
* <p>
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,
* </p>
*
* <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>Equation explanation</h1>
*
* <p>
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,
* </p>
*
* <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();
}
}

@ -27,6 +27,7 @@ import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
@ -53,7 +54,7 @@ public class HandlerUtil {
* @throws IOException IO exception returns if error occurs when executing the httpMethod
*/
public static ProxyResponse execute(HttpRequestBase httpRequest) throws IOException {
try (CloseableHttpClient client = HttpClients.createDefault()) {
try (CloseableHttpClient client = getHttpClient()) {
HttpResponse response = client.execute(httpRequest);
ProxyResponse proxyResponse = new ProxyResponse();
@ -217,4 +218,18 @@ public class HandlerUtil {
return gatewayPort;
}
/**
* Retrieve Http client based on hostname verification.
* @return {@link CloseableHttpClient} http client
*/
public static CloseableHttpClient getHttpClient() {
boolean isIgnoreHostnameVerification = Boolean.parseBoolean(System.
getProperty("org.wso2.ignoreHostnameVerification"));
if (isIgnoreHostnameVerification) {
return HttpClients.custom().setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build();
} else {
return HttpClients.createDefault();
}
}
}

@ -258,6 +258,8 @@
<Scope>perm:geo-service:alerts-manage</Scope>
<Scope>appm:read</Scope>
<Scope>perm:devices:permanent-delete</Scope>
<Scope>perm:android:manage-configuration</Scope>
<Scope>perm:android:view-configuration</Scope>
</Scopes>
<SSOConfiguration>
<Issuer>app-mgt</Issuer>

Loading…
Cancel
Save