Add billing data export api

fixes https://gitlab.com/entgra/product-iots/-/issues/1226
feature/traccar-sync
osh 3 years ago
parent a4b2a325b1
commit 5ad9e3e6b4

@ -32,6 +32,12 @@ public class DeviceList extends BasePaginatedResult {
@ApiModelProperty(name = "totalCost", value = "Total cost of all devices per tenant", required = false)
private double totalCost;
@ApiModelProperty(name = "message", value = "Send information text to the billing UI", required = false)
private String message;
@ApiModelProperty(name = "billedDateIsValid", value = "Check if user entered date is valid", required = false)
private boolean billedDateIsValid;
@ApiModelProperty(value = "List of devices returned")
@JsonProperty("devices")
public List<Device> getList() {
@ -42,6 +48,22 @@ public class DeviceList extends BasePaginatedResult {
this.devices = devices;
}
public boolean isBilledDateIsValid() {
return billedDateIsValid;
}
public void setBilledDateIsValid(boolean billedDateIsValid) {
this.billedDateIsValid = billedDateIsValid;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public double getTotalCost() {
return totalCost;
}

@ -432,6 +432,83 @@ public interface DeviceManagementService {
@QueryParam("limit")
int limit);
@GET
@Path("/billing/file")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = "GET",
value = "Getting Cost details of devices in a tenant",
notes = "Provides individual cost and total cost of all devices per tenant.",
tags = "Device Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:view")
})
}
)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK. \n Successfully fetched the list of devices.",
response = DeviceList.class,
responseHeaders = {
@ResponseHeader(
name = "Content-Type",
description = "The content type of the body"),
@ResponseHeader(
name = "Content-Disposition",
description = "The content disposition 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. \n Empty body because the client already has the latest version of " +
"the requested resource.\n"),
@ApiResponse(
code = 400,
message = "The incoming request has more than one selection criteria defined via the query parameters.",
response = ErrorResponse.class),
@ApiResponse(
code = 404,
message = "The search criteria did not match any device registered with the server.",
response = ErrorResponse.class),
@ApiResponse(
code = 406,
message = "Not Acceptable.\n The requested media type is not supported."),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Server error occurred while fetching the device list.",
response = ErrorResponse.class)
})
Response exportBilling(
@ApiParam(
name = "tenantDomain",
value = "The tenant domain.",
required = false)
String tenantDomain,
@ApiParam(
name = "startDate",
value = "The start date.",
required = false)
Timestamp startDate,
@ApiParam(
name = "endDate",
value = "The end date.",
required = false)
Timestamp endDate,
@ApiParam(
name = "generateBill",
value = "The generate bill boolean.",
required = false)
boolean generateBill);
@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(

@ -124,24 +124,12 @@ import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
import javax.validation.Valid;
import javax.validation.constraints.Size;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Properties;
import java.util.*;
@Path("/devices")
public class DeviceManagementServiceImpl implements DeviceManagementService {
@ -217,8 +205,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
}
if (status != null && !status.isEmpty()) {
boolean isStatusEmpty = true;
for (String statusString : status){
if (StringUtils.isNotBlank(statusString)){
for (String statusString : status) {
if (StringUtils.isNotBlank(statusString)) {
isStatusEmpty = false;
break;
}
@ -348,10 +336,10 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
@Override
@Path("/billing")
public Response getDevicesBilling(
@QueryParam ("tenantDomain") String tenantDomain,
@QueryParam ("startDate") Timestamp startDate,
@QueryParam ("endDate") Timestamp endDate,
@QueryParam ("generateBill") boolean generateBill,
@QueryParam("tenantDomain") String tenantDomain,
@QueryParam("startDate") Timestamp startDate,
@QueryParam("endDate") Timestamp endDate,
@QueryParam("generateBill") boolean generateBill,
@QueryParam("offset") int offset,
@DefaultValue("10")
@QueryParam("limit") int limit) {
@ -385,11 +373,12 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
}
devices.setList((List<Device>) result.getData());
devices.setBilledDateIsValid(result.isBilledDateIsValid());
devices.setMessage(result.getMessage());
devices.setCount(result.getRecordsTotal());
devices.setTotalCost(result.getTotalCost());
return Response.status(Response.Status.OK).entity(devices).build();
}
catch (Exception e) {
} catch (Exception e) {
String msg = "Error occurred while retrieving billing data";
log.error(msg, e);
return Response.serverError().entity(
@ -397,6 +386,51 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
}
}
@GET
@Override
@Path("/billing/file")
public Response exportBilling(
@QueryParam("tenantDomain") String tenantDomain,
@QueryParam("startDate") Timestamp startDate,
@QueryParam("endDate") Timestamp endDate,
@QueryParam("generateBill") boolean generateBill) {
try {
DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
PaginationResult result;
int tenantId = 0;
RealmService realmService = DeviceMgtAPIUtils.getRealmService();
DeviceList devices = new DeviceList();
if (!tenantDomain.isEmpty()) {
tenantId = realmService.getTenantManager().getTenantId(tenantDomain);
} else {
tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
}
try {
result = dms.createBillingFile(tenantId, tenantDomain, startDate, endDate, generateBill);
} catch (Exception exception) {
String msg = "Error occurred when trying to retrieve billing data without pagination";
log.error(msg, exception);
return null;
}
devices.setList((List<Device>) result.getData());
devices.setBilledDateIsValid(result.isBilledDateIsValid());
devices.setMessage(result.getMessage());
devices.setCount(result.getRecordsTotal());
devices.setTotalCost(result.getTotalCost());
return Response.status(Response.Status.OK).entity(devices).build();
} catch (Exception e) {
String msg = "Error occurred while retrieving billing data without pagination";
log.error(msg, e);
return null;
}
}
@GET
@Override
@Path("/user-devices")
@ -463,7 +497,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
@QueryParam("to") long to,
@QueryParam("type") String type,
@DefaultValue("0") @QueryParam("offset") int offset,
@DefaultValue("100") @QueryParam("limit") int limit){
@DefaultValue("100") @QueryParam("limit") int limit) {
try {
RequestValidationUtil.validatePaginationParameters(offset, limit);
DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
@ -488,7 +522,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
String msg = "Error occurred while getting the data using '" + groupId + "'";
log.error(msg);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} catch (UserStoreException e){
} catch (UserStoreException e) {
String msg = "Error occurred while retrieving role list of user '" + authorizedUser + "'";
log.error(msg);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
@ -496,7 +530,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
PaginationResult result = dms.getAllDevices(request, false);
if(!result.getData().isEmpty()){
if (!result.getData().isEmpty()) {
devices.setList((List<Device>) result.getData());
for (Device device : devices.getList()) {
@ -604,7 +638,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
DeviceData deviceData = new DeviceData();
deviceData.setDeviceIdentifier(deviceIdentifier);
if (!StringUtils.isBlank(ifModifiedSince)){
if (!StringUtils.isBlank(ifModifiedSince)) {
SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
try {
sinceDate = format.parse(ifModifiedSince);
@ -617,10 +651,10 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
}
}
if (!StringUtils.isBlank(owner)){
if (!StringUtils.isBlank(owner)) {
deviceData.setDeviceOwner(owner);
}
if (!StringUtils.isBlank(ownership)){
if (!StringUtils.isBlank(ownership)) {
deviceData.setDeviceOwnership(ownership);
}
device = dms.getDevice(deviceData, true);
@ -893,7 +927,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
List<Device> devices;
DeviceList deviceList = new DeviceList();
try {
if(map.getProperties().isEmpty()){
if (map.getProperties().isEmpty()) {
if (log.isDebugEnabled()) {
log.debug("No search criteria defined when querying devices.");
}
@ -901,7 +935,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
}
DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
devices = dms.getDevicesBasedOnProperties(map.getProperties());
if(devices == null || devices.isEmpty()){
if (devices == null || devices.isEmpty()) {
if (log.isDebugEnabled()) {
log.debug("No Devices Found for criteria : " + map);
}
@ -1544,7 +1578,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
if (MDMAppConstants.AndroidConstants.OPCODE_INSTALL_APPLICATION.equals(operation.getCode()) ||
MDMAppConstants.AndroidConstants.OPCODE_UNINSTALL_APPLICATION.equals(operation.getCode())) {
ApplicationManager applicationManager = DeviceMgtAPIUtils.getApplicationManager();
applicationManager.updateSubsStatus(device.getId(), operation.getId(),operation.getStatus().toString());
applicationManager.updateSubsStatus(device.getId(), operation.getId(), operation.getStatus().toString());
}
return Response.status(Response.Status.OK).entity("OperationStatus updated successfully.").build();
} catch (BadRequestException e) {
@ -1563,7 +1597,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
String msg = "Error occurred when updating the application subscription status of the operation. " +
"The device identifier is: " + deviceIdentifier;
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();
}
}
@GET

@ -0,0 +1,118 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. 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;
import com.google.gson.Gson;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
@ApiModel(value = "Billing", description = "This class carries all information related to a device billing.")
public class Billing implements Serializable {
private static final long serialVersionUID = 1998101711L;
@ApiModelProperty(name = "invoiceId", value = "ID of the billing.",
required = false)
private int invoiceId;
@ApiModelProperty(name = "deviceId", value = "Device id of the billing.",
required = false)
private int deviceId;
@ApiModelProperty(name = "tenantId", value = "Tenant of the device.",
required = false)
private int tenantId;
@ApiModelProperty(name = "billingStart", value = "Start date of the billing period.", required = false)
private Long billingStart;
@ApiModelProperty(name = "billingEnd", value = "End date of the billing period.", required = false)
private Long billingEnd;
@ApiModelProperty(name = "deviceCount", value = "Device count for a billing period",
required = false)
private int deviceCount;
public Billing() {
}
public Billing(int invoiceId, int deviceId, int tenantId, Long billingStart, Long billingEnd) {
this.invoiceId = invoiceId;
this.deviceId = deviceId;
this.tenantId = tenantId;
this.billingStart = billingStart;
this.billingEnd = billingEnd;
}
public int getInvoiceId() {
return invoiceId;
}
public void setInvoiceId(int invoiceId) {
this.invoiceId = invoiceId;
}
public int getDeviceId() {
return deviceId;
}
public void setDeviceId(int deviceId) {
this.deviceId = deviceId;
}
public int getTenantId() {
return tenantId;
}
public void setTenantId(int tenantId) {
this.tenantId = tenantId;
}
public Long getBillingStart() {
return billingStart;
}
public void setBillingStart(Long billingStart) {
this.billingStart = billingStart;
}
public Long getBillingEnd() {
return billingEnd;
}
public void setBillingEnd(Long billingEnd) {
this.billingEnd = billingEnd;
}
public int getDeviceCount() {
return deviceCount;
}
public void setDeviceCount(int deviceCount) {
this.deviceCount = deviceCount;
}
@Override
public String toString() {
return new Gson().toJson(this);
}
}

@ -47,6 +47,28 @@ public class PaginationResult implements Serializable {
@ApiModelProperty(name = "totalCost", value = "Total cost of all devices per tenant", required = false)
private double totalCost;
@ApiModelProperty(name = "billedDateIsValid", value = "Check if user entered date is valid", required = false)
private boolean billedDateIsValid;
@ApiModelProperty(name = "message", value = "Send information text to the billing UI", required = false)
private String message;
public boolean isBilledDateIsValid() {
return billedDateIsValid;
}
public void setBilledDateIsValid(boolean billedDateIsValid) {
this.billedDateIsValid = billedDateIsValid;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public double getTotalCost() {
return totalCost;
}

@ -44,6 +44,7 @@ public final class DeviceManagementConstants {
public static final String DEVICE_CACHE = "DEVICE_CACHE";
public static final String API_RESOURCE_PERMISSION_CACHE = "API_RESOURCE_CACHE_CACHE";
public static final String GEOFENCE_CACHE = "GEOFENCE_CACHE";
public static final String META_KEY = "PER_DEVICE_COST";
public static final String ENROLLMENT_NOTIFICATION_API_ENDPOINT = "/api/device-mgt/enrollment-notification";
public static final String URL_SEPERATOR = "/";

@ -7,6 +7,26 @@ public class Billing {
private boolean isHideBillGenerationInSubTenant;
private boolean isHideTotalCalculationInSuperTenant;
private boolean isHideTotalCalculationInSubTenant;
private boolean isHideDomainSelectionInSuperTenant;
private boolean isHideDomainSelectionInSubTenant;
@XmlElement(name = "HideDomainSelectionInSuperTenant")
public boolean isHideDomainSelectionInSuperTenant() {
return isHideDomainSelectionInSuperTenant;
}
public void setHideDomainSelectionInSuperTenant(boolean hideDomainSelectionInSuperTenant) {
isHideDomainSelectionInSuperTenant = hideDomainSelectionInSuperTenant;
}
@XmlElement(name = "HideDomainSelectionInSubTenant")
public boolean isHideDomainSelectionInSubTenant() {
return isHideDomainSelectionInSubTenant;
}
public void setHideDomainSelectionInSubTenant(boolean hideDomainSelectionInSubTenant) {
isHideDomainSelectionInSubTenant = hideDomainSelectionInSubTenant;
}
@XmlElement(name = "HideBillGenerationInSuperTenant")
public boolean isHideBillGenerationInSuperTenant() {

@ -0,0 +1,13 @@
package org.wso2.carbon.device.mgt.core.dao;
import org.wso2.carbon.device.mgt.common.Billing;
import java.sql.Timestamp;
import java.util.List;
public interface BillingDAO {
void addBilling(int deviceId, int tenantId, Timestamp billingStart, Timestamp billingEnd) throws DeviceManagementDAOException;
List<Billing> getBilling(int deviceId, Timestamp billingStart, Timestamp billingEnd) throws DeviceManagementDAOException;
}

@ -294,24 +294,12 @@ public interface DeviceDAO {
List<Device> getDevices(PaginationRequest request, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to retrieve the not removed devices of a given tenant as a paginated result.
*
* @param request PaginationRequest object holding the data for pagination
* @param tenantId tenant id.
* @return returns paginated list of not removed devices of tenant.
* @throws DeviceManagementDAOException
*/
List<DeviceBilling> getDeviceBillList(PaginationRequest request, int tenantId, Timestamp startDate, Timestamp endDate) throws DeviceManagementDAOException;
/**
* This method is used to retrieve the removed devices of a given tenant as a paginated result.
*
* @param request PaginationRequest object holding the data for pagination
* This method is used to retrieve the devices of a given tenant without pagination.
* @param tenantId tenant id.
* @return rreturns paginated list of removed devices of tenant.
* @return returns a list of devices of the tenant.
* @throws DeviceManagementDAOException
*/
List<DeviceBilling> getRemovedDeviceBillList(PaginationRequest request, int tenantId) throws DeviceManagementDAOException;
List<Device> getDeviceListWithoutPagination(int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to retrieve the devices of a given tenant as a paginated result, along the lines of

@ -42,6 +42,7 @@ import org.wso2.carbon.device.mgt.core.privacy.dao.impl.PrivacyComplianceDAOImpl
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Hashtable;
import java.util.List;
@ -124,6 +125,11 @@ public class DeviceManagementDAOFactory {
public static EnrollmentDAO getEnrollmentDAO() {
return new EnrollmentDAOImpl();
}
public static BillingDAO getBillingDAO() {
return new BillingDAOImpl();
}
public static DeviceStatusDAO getDeviceStatusDAO() {
return new DeviceStatusDAOImpl();
}

@ -0,0 +1,71 @@
package org.wso2.carbon.device.mgt.core.dao.impl;
import org.wso2.carbon.device.mgt.common.Billing;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.core.dao.BillingDAO;
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.util.DeviceManagementDAOUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class BillingDAOImpl implements BillingDAO {
@Override
public void addBilling(int deviceId, int tenantId, Timestamp billingStart, Timestamp billingEnd) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = this.getConnection();
String sql = "INSERT INTO DM_BILLING(DEVICE_ID, TENANT_ID, BILLING_START, BILLING_END) VALUES(?, ?, ?, ?)";
stmt = conn.prepareStatement(sql, new String[] {"invoice_id"});
stmt.setInt(1, deviceId);
stmt.setInt(2,tenantId);
stmt.setTimestamp(3, billingStart);
stmt.setTimestamp(4, billingEnd);
stmt.execute();
} catch (SQLException e) {
e.printStackTrace();
throw new DeviceManagementDAOException("Error occurred while adding billing period", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public List<Billing> getBilling(int deviceId, Timestamp billingStart, Timestamp billingEnd) throws DeviceManagementDAOException {
List<Billing> billings = new ArrayList<>();
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
EnrolmentInfo.Status status = null;
try {
conn = this.getConnection();
String sql;
sql = "SELECT * FROM DM_BILLING WHERE DEVICE_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
rs = stmt.executeQuery();
while (rs.next()) {
Billing bill = new Billing(rs.getInt("INVOICE_ID"), rs.getInt("DEVICE_ID"),rs.getInt("TENANT_ID"),
(rs.getTimestamp("BILLING_START").getTime()), (rs.getTimestamp("BILLING_END").getTime()));
billings.add(bill);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred getting billing periods", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return billings;
}
private Connection getConnection() throws SQLException {
return DeviceManagementDAOFactory.getConnection();
}
}

@ -185,23 +185,23 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<DeviceBilling> getDeviceBillList(PaginationRequest request, int tenantId, Timestamp startDate, Timestamp endDate)
public List<Device> getDeviceListWithoutPagination(int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
List<DeviceBilling> devices = new ArrayList<>();
List<Device> devices = new ArrayList<>();
try {
conn = this.getConnection();
String sql = "SELECT DM_DEVICE.ID, DEVICE_IDENTIFICATION, DESCRIPTION, NAME AS DEVICE_NAME, DM_ENROLMENT.ID AS ENROLMENT_ID,\n" +
" DATE_OF_ENROLMENT,STATUS, LAST_BILLED_DATE, TIMESTAMPDIFF('DAY', DATE_OF_ENROLMENT, CURDATE()) as DAYS_SINCE_ENROLLED\n" +
" FROM DM_DEVICE JOIN DM_ENROLMENT ON (DM_DEVICE.ID = DM_ENROLMENT.DEVICE_ID)\n" +
" WHERE DM_ENROLMENT.TENANT_ID=?";
String sql = "SELECT DM_DEVICE.ID AS DEVICE_ID, DEVICE_IDENTIFICATION, DESCRIPTION, DM_DEVICE.NAME AS DEVICE_NAME, DM_DEVICE_TYPE.NAME AS DEVICE_TYPE,\n" +
"DM_ENROLMENT.ID AS ENROLMENT_ID, DATE_OF_ENROLMENT,OWNER, OWNERSHIP,IS_TRANSFERRED, STATUS, DATE_OF_LAST_UPDATE, LAST_BILLED_DATE,\n" +
"TIMESTAMPDIFF('DAY', DATE_OF_ENROLMENT, CURDATE()) as DAYS_SINCE_ENROLLED FROM DM_DEVICE JOIN DM_ENROLMENT\n" +
"ON (DM_DEVICE.ID = DM_ENROLMENT.DEVICE_ID) JOIN DM_DEVICE_TYPE ON (DM_DEVICE.DEVICE_TYPE_ID = DM_DEVICE_TYPE.ID) WHERE DM_ENROLMENT.TENANT_ID=?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
DeviceBilling device = DeviceManagementDAOUtil.loadDeviceBilling(rs);
Device device = DeviceManagementDAOUtil.loadDevice(rs);
devices.add(device);
}
} catch (SQLException e) {
@ -212,35 +212,6 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
return devices;
}
@Override
public List<DeviceBilling> getRemovedDeviceBillList(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
List<DeviceBilling> devices = new ArrayList<>();
try {
conn = this.getConnection();
String sql = "select DEVICE_IDENTIFICATION, DESCRIPTION, NAME AS DEVICE_NAME, DATE_OF_ENROLMENT, LAST_BILLED_DATE, DATE_OF_LAST_UPDATE, STATUS, " +
"TIMESTAMPDIFF('DAY', DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE) AS DAYS_USED from DM_DEVICE d, DM_ENROLMENT e" +
" where e.TENANT_ID=? and d.ID=e.DEVICE_ID and STATUS ='REMOVED'\n";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
// DeviceBilling device = DeviceManagementDAOUtil.loadDeviceBilling(rs, true);
DeviceBilling device = DeviceManagementDAOUtil.loadDeviceBilling(rs);
devices.add(device);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices belongs to '" +
request.getOwner() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return devices;
}
@Override
public List<Device> getAllocatedDevices(PaginationRequest request, int tenantId, int activeServerCount, int serverIndex)
throws DeviceManagementDAOException {

@ -189,12 +189,7 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<DeviceBilling> getRemovedDeviceBillList(PaginationRequest request, int tenantId) throws DeviceManagementDAOException {
return null;
}
@Override
public List<DeviceBilling> getDeviceBillList(PaginationRequest request, int tenantId , Timestamp startDate, Timestamp endDate) throws DeviceManagementDAOException {
public List<Device> getDeviceListWithoutPagination(int tenantId) throws DeviceManagementDAOException {
return null;
}

@ -180,12 +180,7 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<DeviceBilling> getRemovedDeviceBillList(PaginationRequest request, int tenantId) throws DeviceManagementDAOException {
return null;
}
@Override
public List<DeviceBilling> getDeviceBillList(PaginationRequest request, int tenantId, Timestamp startDate, Timestamp endDate) throws DeviceManagementDAOException {
public List<Device> getDeviceListWithoutPagination(int tenantId) throws DeviceManagementDAOException {
return null;
}

@ -190,12 +190,7 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<DeviceBilling> getRemovedDeviceBillList(PaginationRequest request, int tenantId) throws DeviceManagementDAOException {
return null;
}
@Override
public List<DeviceBilling> getDeviceBillList(PaginationRequest request, int tenantId, Timestamp startDate, Timestamp endDate) throws DeviceManagementDAOException {
public List<Device> getDeviceListWithoutPagination(int tenantId) throws DeviceManagementDAOException {
return null;
}

@ -209,6 +209,17 @@ public interface DeviceManagementProviderService {
*/
PaginationResult getAllDevicesBillings(PaginationRequest request, int tenantId, String tenantDomain, Timestamp startDate, Timestamp endDate, boolean generateBill) throws DeviceManagementException;
/**
* Method to retrieve all the devices with pagination support.
*
* @return PaginationResult - Result including the device bill list without pagination.
* @throws DeviceManagementException If some unusual behaviour is observed while fetching billing of
* devices.
*/
PaginationResult createBillingFile(int tenantId, String tenantDomain, Timestamp startDate, Timestamp endDate, boolean generateBill) throws DeviceManagementException;
/**
* Returns the device of specified id.
*

@ -138,6 +138,8 @@ import java.io.StringWriter;
import java.lang.reflect.Type;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@ -156,6 +158,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
private final EnrollmentDAO enrollmentDAO;
private final ApplicationDAO applicationDAO;
private MetadataDAO metadataDAO;
private final BillingDAO billingDAO;
private final DeviceStatusDAO deviceStatusDAO;
public DeviceManagementProviderServiceImpl() {
@ -165,6 +168,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
this.enrollmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO();
this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO();
this.billingDAO = DeviceManagementDAOFactory.getBillingDAO();
this.deviceStatusDAO = DeviceManagementDAOFactory.getDeviceStatusDAO();
/* Registering a listener to retrieve events when some device management service plugin is installed after
@ -768,7 +772,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
throw new DeviceManagementException(msg);
}
if (log.isDebugEnabled()) {
log.debug("Getting allocated Devices for Server with index "+ serverIndex + " and" +
log.debug("Getting allocated Devices for Server with index " + serverIndex + " and" +
" type '" + deviceType);
}
List<Device> allocatedDevices;
@ -937,101 +941,153 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return this.getAllDevices(request, true);
}
@Override
public PaginationResult getAllDevicesBillings(PaginationRequest request, int tenantId, String tenantDomain, Timestamp startDate, Timestamp endDate, boolean generateBill) throws DeviceManagementException {
if (request == null) {
String msg = "Received incomplete pagination request for method getAllDeviceBillings";
log.error(msg);
throw new DeviceManagementException(msg);
}
public PaginationResult calculateCost(int tenantId, String tenantDomain, Timestamp startDate, Timestamp endDate, boolean generateBill, List<Device> allDevices) throws DeviceManagementException {
DeviceManagerUtil.validateDeviceListPageSize(request);
PaginationResult paginationResult = new PaginationResult();
double totalCost = 0.0;
List<Device> allDevices;
int count = 0;
boolean allDevicesBilledDateIsValid = true;
String lastBilledDates = "";
ArrayList<String> lastBilledDatesList = new ArrayList<>();
List<Billing> invalidDevices = new ArrayList<>();
List<Device> removeBillingPeriodInvalidDevices = new ArrayList<>() ;
List<Device> removeStatusUpdateInvalidDevices = new ArrayList<>() ;
try {
DeviceManagementDAOFactory.beginTransaction();
// allDevices = deviceDAO.getDeviceBillList(request, tenantId, startDate, endDate);
allDevices = deviceDAO.getDevices(request, tenantId);
count = deviceDAO.getDeviceCount(request, tenantId);
String metaKey = "PER_DEVICE_COST";
MetadataManagementDAOFactory.beginTransaction();
Metadata metadata = metadataDAO.getMetadata(tenantId, metaKey);
Metadata metadata = metadataDAO.getMetadata(-1234, DeviceManagementConstants.META_KEY);
Gson g = new Gson();
Collection<Cost> costData = null;
Type collectionType = new TypeToken<Collection<Cost>>(){}.getType();
Type collectionType = new TypeToken<Collection<Cost>>() {}.getType();
if (metadata != null) {
costData = g.fromJson(metadata.getMetaValue(), collectionType);
for (Cost tenantCost: costData) {
for (Cost tenantCost : costData) {
if (tenantCost.getTenantDomain().equals(tenantDomain)) {
for (Device device: allDevices) {
device.setDeviceStatusInfo(getDeviceStatusHistory(device, startDate, endDate, true));
for (Device device : allDevices) {
device.setDeviceStatusInfo(getDeviceStatusHistory(device, null, null, true));
long dateDiff = 0;
List<DeviceStatus> deviceStatus = device.getDeviceStatusInfo();
boolean lastBilledDate = false;
boolean deviceStatusIsValid = false;
List<Billing> deviceBilling = billingDAO.getBilling(device.getId(), startDate, endDate);
boolean billDateIsInvalid = false;
if (deviceBilling.isEmpty()) {
billDateIsInvalid = false;
}
if (generateBill) {
for (Billing bill : deviceBilling) {
if ((bill.getBillingStart() <= startDate.getTime() && startDate.getTime() <= bill.getBillingEnd()) ||
(bill.getBillingStart() <= endDate.getTime() && endDate.getTime() <= bill.getBillingEnd())) {
billDateIsInvalid = true;
invalidDevices.add(bill);
}
}
}
for (int i=0; i<deviceStatus.size(); i++) {
if(deviceStatus.get(i).getStatus().toString().equals("ACTIVE")) {
if (deviceStatus.size()> i+1) {
if (!billDateIsInvalid) {
for (int i = 0; i < deviceStatus.size(); i++) {
if (deviceStatus.get(i).getStatus().toString().equals("ACTIVE")) {
if (deviceStatus.size() > i + 1) {
if (!lastBilledDate) {
lastBilledDate = true;
if (device.getEnrolmentInfo().getLastBilledDate() == 0) {
dateDiff = dateDiff + (deviceStatus.get(i+1).getUpdateTime().getTime() - deviceStatus.get(i).getUpdateTime().getTime());
deviceStatusIsValid = true;
dateDiff = dateDiff + (deviceStatus.get(i + 1).getUpdateTime().getTime() - deviceStatus.get(i).getUpdateTime().getTime());
} else {
if (deviceStatus.get(i+1).getUpdateTime().getTime() >= device.getEnrolmentInfo().getLastBilledDate()) {
dateDiff = dateDiff + (deviceStatus.get(i+1).getUpdateTime().getTime() - device.getEnrolmentInfo().getLastBilledDate());
if (deviceStatus.get(i + 1).getUpdateTime().getTime() >= device.getEnrolmentInfo().getLastBilledDate()) {
deviceStatusIsValid = true;
dateDiff = dateDiff + (deviceStatus.get(i + 1).getUpdateTime().getTime() - device.getEnrolmentInfo().getLastBilledDate());
}
}
} else {
if ( deviceStatus.get(i).getUpdateTime().getTime() >= device.getEnrolmentInfo().getLastBilledDate()) {
dateDiff = dateDiff + (deviceStatus.get(i+1).getUpdateTime().getTime() - deviceStatus.get(i).getUpdateTime().getTime());
if (deviceStatus.get(i).getUpdateTime().getTime() >= device.getEnrolmentInfo().getLastBilledDate()) {
deviceStatusIsValid = true;
dateDiff = dateDiff + (deviceStatus.get(i + 1).getUpdateTime().getTime() - deviceStatus.get(i).getUpdateTime().getTime());
}
}
} else {
} else { // The last status update calculation is done in this block
// If only one status row is retrieved this block is executed
if (!lastBilledDate) {
lastBilledDate = true;
// Is executed if there is no lastBilled date and if the updates time is before the enddate
if (device.getEnrolmentInfo().getLastBilledDate() == 0) {
if (endDate.getTime() >= deviceStatus.get(i).getUpdateTime().getTime()) {
deviceStatusIsValid = true;
dateDiff = dateDiff + (endDate.getTime() - deviceStatus.get(i).getUpdateTime().getTime());
}
} else {
if (endDate.getTime() >= device.getEnrolmentInfo().getLastBilledDate()) {
dateDiff = dateDiff +(endDate.getTime() - device.getEnrolmentInfo().getLastBilledDate());
deviceStatusIsValid = true;
dateDiff = dateDiff + (endDate.getTime() - device.getEnrolmentInfo().getLastBilledDate());
}
}
} else {
if (device.getEnrolmentInfo().getLastBilledDate() <= deviceStatus.get(i).getUpdateTime().getTime()
&& endDate.getTime() >= deviceStatus.get(i).getUpdateTime().getTime()) {
deviceStatusIsValid = true;
dateDiff = dateDiff + (endDate.getTime() - deviceStatus.get(i).getUpdateTime().getTime());
}
if (device.getEnrolmentInfo().getLastBilledDate() >= deviceStatus.get(i).getUpdateTime().getTime()
&& endDate.getTime() >= device.getEnrolmentInfo().getLastBilledDate()) {
deviceStatusIsValid = true;
dateDiff = dateDiff + (endDate.getTime() - device.getEnrolmentInfo().getLastBilledDate());
}
}
}
}
}
long dateInDays = TimeUnit.DAYS.convert(dateDiff, TimeUnit.MILLISECONDS);;
double cost = (tenantCost.getCost()/365)*dateInDays;
long dateInDays = TimeUnit.DAYS.convert(dateDiff, TimeUnit.MILLISECONDS);
double cost = (tenantCost.getCost() / 365) * dateInDays;
totalCost = cost + totalCost;
device.setCost(Math.round(cost * 100.0) / 100.0);
device.setDaysUsed((int) dateInDays);
if (generateBill) {
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
enrollmentDAO.updateEnrollmentLastBilledDate(device.getEnrolmentInfo(), timestamp, tenantId);
enrollmentDAO.updateEnrollmentLastBilledDate(device.getEnrolmentInfo(), endDate, tenantId);
billingDAO.addBilling(device.getId(), tenantId, startDate, endDate);
DeviceManagementDAOFactory.commitTransaction();
}
} else {
for (int i = 0; i < deviceStatus.size(); i++) {
if (endDate.getTime() >= deviceStatus.get(i).getUpdateTime().getTime()
&& startDate.getTime() <= deviceStatus.get(i).getUpdateTime().getTime()) {
deviceStatusIsValid = true;
}
}
if (device.getEnrolmentInfo().getLastBilledDate() != 0) {
Date date = new Date(device.getEnrolmentInfo().getLastBilledDate());
Format format = new SimpleDateFormat("yyyy MM dd");
if (!lastBilledDatesList.contains(lastBilledDatesList.add(format.format(date)))) {
lastBilledDatesList.add(format.format(date));
}
lastBilledDates = lastBilledDates + ' ' + lastBilledDatesList;
}
removeBillingPeriodInvalidDevices.add(device);
}
if (!deviceStatusIsValid) {
removeStatusUpdateInvalidDevices.add(device);
}
}
}
}
}
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while retrieving device list pertaining to the current tenant";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
catch (Exception e) {
} catch (Exception e) {
String msg = "Error occurred in getAllDevices";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
@ -1039,13 +1095,83 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
DeviceManagementDAOFactory.closeConnection();
MetadataManagementDAOFactory.closeConnection();
}
if(!removeBillingPeriodInvalidDevices.isEmpty() && removeBillingPeriodInvalidDevices.size() == allDevices.size()) {
allDevicesBilledDateIsValid = false;
paginationResult.setMessage("Invalid bill period last billed dates of devices are " +lastBilledDates);
}
if(!removeStatusUpdateInvalidDevices.isEmpty() && removeStatusUpdateInvalidDevices.size() == allDevices.size()) {
allDevicesBilledDateIsValid = false;
if (paginationResult.getMessage() != null){
paginationResult.setMessage(paginationResult.getMessage() + " and no device updates within entered bill period.");
} else {
paginationResult.setMessage("Devices have not been updated within the given period or entered end date comes before the last billed date");
}
}
allDevices.removeAll(removeBillingPeriodInvalidDevices);
allDevices.removeAll(removeStatusUpdateInvalidDevices);
paginationResult.setBilledDateIsValid(allDevicesBilledDateIsValid);
paginationResult.setData(allDevices);
paginationResult.setTotalCost(Math.round(totalCost * 100.0) / 100.0);
return paginationResult;
}
@Override
public PaginationResult getAllDevicesBillings(PaginationRequest request, int tenantId, String tenantDomain, Timestamp startDate, Timestamp endDate, boolean generateBill) throws DeviceManagementException {
if (request == null) {
String msg = "Received incomplete pagination request for method getAllDeviceBillings";
log.error(msg);
throw new DeviceManagementException(msg);
}
DeviceManagerUtil.validateDeviceListPageSize(request);
PaginationResult paginationResult = new PaginationResult();
List<Device> allDevices;
int count = 0;
try {
DeviceManagementDAOFactory.beginTransaction();
allDevices = deviceDAO.getDevices(request, tenantId);
count = deviceDAO.getDeviceCount(request, tenantId);
paginationResult = calculateCost(tenantId, tenantDomain, startDate, endDate, generateBill, allDevices);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while retrieving device bill list pertaining to the current tenant";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getAllDevicesBillings";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
paginationResult.setRecordsFiltered(count);
paginationResult.setRecordsTotal(count);
return paginationResult;
}
@Override
public PaginationResult createBillingFile(int tenantId, String tenantDomain, Timestamp startDate, Timestamp endDate, boolean generateBill) throws DeviceManagementException {
PaginationResult paginationResult = new PaginationResult();
List<Device> allDevices;
try {
DeviceManagementDAOFactory.beginTransaction();
allDevices = deviceDAO.getDeviceListWithoutPagination(tenantId);
paginationResult = calculateCost(tenantId, tenantDomain, startDate, endDate, generateBill, allDevices);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while retrieving device bill list without pagination pertaining to the current tenant";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in createBillingFile";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
return paginationResult;
}
@Override
public PaginationResult getAllDevices(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException {
if (request == null) {
@ -1862,8 +1988,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
DeviceManagementDAOFactory.closeConnection();
}
}
@Override
public List<DeviceStatus> getDeviceStatusHistory(Device device, Date fromDate, Date toDate, boolean billingStatus) throws DeviceManagementException{
public List<DeviceStatus> getDeviceStatusHistory(Device device, Date fromDate, Date toDate, boolean billingStatus) throws DeviceManagementException {
if (log.isDebugEnabled()) {
log.debug("get status history of device: " + device.getDeviceIdentifier());
}
@ -1883,7 +2010,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
}
@Override
public List<DeviceStatus> getDeviceCurrentEnrolmentStatusHistory(Device device, Date fromDate, Date toDate) throws DeviceManagementException{
public List<DeviceStatus> getDeviceCurrentEnrolmentStatusHistory(Device device, Date fromDate, Date toDate) throws DeviceManagementException {
if (log.isDebugEnabled()) {
log.debug("get status history of device: " + device.getDeviceIdentifier());
}
@ -1912,12 +2039,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
}
@Override
public List<DeviceStatus> getDeviceStatusHistory(Device device) throws DeviceManagementException{
public List<DeviceStatus> getDeviceStatusHistory(Device device) throws DeviceManagementException {
return getDeviceStatusHistory(device, null, null, false);
}
@Override
public List<DeviceStatus> getDeviceCurrentEnrolmentStatusHistory(Device device) throws DeviceManagementException{
public List<DeviceStatus> getDeviceCurrentEnrolmentStatusHistory(Device device) throws DeviceManagementException {
return getDeviceCurrentEnrolmentStatusHistory(device, null, null);
}
@ -4570,7 +4697,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
}
@Override
public License getLicenseConfig (String deviceTypeName) throws DeviceManagementException {
public License getLicenseConfig(String deviceTypeName) throws DeviceManagementException {
DeviceManagementService deviceManagementService =
pluginRepository.getDeviceManagementService(deviceTypeName,
this.getTenantId());

Loading…
Cancel
Save