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) @ApiModelProperty(name = "totalCost", value = "Total cost of all devices per tenant", required = false)
private double totalCost; 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") @ApiModelProperty(value = "List of devices returned")
@JsonProperty("devices") @JsonProperty("devices")
public List<Device> getList() { public List<Device> getList() {
@ -42,6 +48,22 @@ public class DeviceList extends BasePaginatedResult {
this.devices = devices; 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() { public double getTotalCost() {
return totalCost; return totalCost;
} }

@ -432,6 +432,83 @@ public interface DeviceManagementService {
@QueryParam("limit") @QueryParam("limit")
int 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 @GET
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@ApiOperation( @ApiOperation(

@ -124,24 +124,12 @@ import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
import javax.validation.Valid; import javax.validation.Valid;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
import javax.ws.rs.DELETE; import javax.ws.rs.*;
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.core.Response; import javax.ws.rs.core.Response;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Properties;
@Path("/devices") @Path("/devices")
public class DeviceManagementServiceImpl implements DeviceManagementService { public class DeviceManagementServiceImpl implements DeviceManagementService {
@ -385,11 +373,12 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
} }
devices.setList((List<Device>) result.getData()); devices.setList((List<Device>) result.getData());
devices.setBilledDateIsValid(result.isBilledDateIsValid());
devices.setMessage(result.getMessage());
devices.setCount(result.getRecordsTotal()); devices.setCount(result.getRecordsTotal());
devices.setTotalCost(result.getTotalCost()); devices.setTotalCost(result.getTotalCost());
return Response.status(Response.Status.OK).entity(devices).build(); return Response.status(Response.Status.OK).entity(devices).build();
} } catch (Exception e) {
catch (Exception e) {
String msg = "Error occurred while retrieving billing data"; String msg = "Error occurred while retrieving billing data";
log.error(msg, e); log.error(msg, e);
return Response.serverError().entity( 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 @GET
@Override @Override
@Path("/user-devices") @Path("/user-devices")
@ -1563,7 +1597,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
String msg = "Error occurred when updating the application subscription status of the operation. " + String msg = "Error occurred when updating the application subscription status of the operation. " +
"The device identifier is: " + deviceIdentifier; "The device identifier is: " + deviceIdentifier;
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
} }
@GET @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) @ApiModelProperty(name = "totalCost", value = "Total cost of all devices per tenant", required = false)
private double totalCost; 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() { public double getTotalCost() {
return totalCost; return totalCost;
} }

@ -44,6 +44,7 @@ public final class DeviceManagementConstants {
public static final String DEVICE_CACHE = "DEVICE_CACHE"; public static final String DEVICE_CACHE = "DEVICE_CACHE";
public static final String API_RESOURCE_PERMISSION_CACHE = "API_RESOURCE_CACHE_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 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 ENROLLMENT_NOTIFICATION_API_ENDPOINT = "/api/device-mgt/enrollment-notification";
public static final String URL_SEPERATOR = "/"; public static final String URL_SEPERATOR = "/";

@ -7,6 +7,26 @@ public class Billing {
private boolean isHideBillGenerationInSubTenant; private boolean isHideBillGenerationInSubTenant;
private boolean isHideTotalCalculationInSuperTenant; private boolean isHideTotalCalculationInSuperTenant;
private boolean isHideTotalCalculationInSubTenant; 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") @XmlElement(name = "HideBillGenerationInSuperTenant")
public boolean isHideBillGenerationInSuperTenant() { 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; 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. * This method is used to retrieve the devices of a given tenant without pagination.
*
* @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
* @param tenantId tenant id. * @param tenantId tenant id.
* @return rreturns paginated list of removed devices of tenant. * @return returns a list of devices of the tenant.
* @throws DeviceManagementDAOException * @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 * 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 javax.sql.DataSource;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.List; import java.util.List;
@ -124,6 +125,11 @@ public class DeviceManagementDAOFactory {
public static EnrollmentDAO getEnrollmentDAO() { public static EnrollmentDAO getEnrollmentDAO() {
return new EnrollmentDAOImpl(); return new EnrollmentDAOImpl();
} }
public static BillingDAO getBillingDAO() {
return new BillingDAOImpl();
}
public static DeviceStatusDAO getDeviceStatusDAO() { public static DeviceStatusDAO getDeviceStatusDAO() {
return new DeviceStatusDAOImpl(); 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 @Override
public List<DeviceBilling> getDeviceBillList(PaginationRequest request, int tenantId, Timestamp startDate, Timestamp endDate) public List<Device> getDeviceListWithoutPagination(int tenantId)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
List<DeviceBilling> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String sql = "SELECT DM_DEVICE.ID, DEVICE_IDENTIFICATION, DESCRIPTION, NAME AS DEVICE_NAME, DM_ENROLMENT.ID AS ENROLMENT_ID,\n" + 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" +
" DATE_OF_ENROLMENT,STATUS, LAST_BILLED_DATE, TIMESTAMPDIFF('DAY', DATE_OF_ENROLMENT, CURDATE()) as DAYS_SINCE_ENROLLED\n" + "DM_ENROLMENT.ID AS ENROLMENT_ID, DATE_OF_ENROLMENT,OWNER, OWNERSHIP,IS_TRANSFERRED, STATUS, DATE_OF_LAST_UPDATE, LAST_BILLED_DATE,\n" +
" FROM DM_DEVICE JOIN DM_ENROLMENT ON (DM_DEVICE.ID = DM_ENROLMENT.DEVICE_ID)\n" + "TIMESTAMPDIFF('DAY', DATE_OF_ENROLMENT, CURDATE()) as DAYS_SINCE_ENROLLED FROM DM_DEVICE JOIN DM_ENROLMENT\n" +
" WHERE DM_ENROLMENT.TENANT_ID=?"; "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 = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
ResultSet rs = stmt.executeQuery(); ResultSet rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {
DeviceBilling device = DeviceManagementDAOUtil.loadDeviceBilling(rs); Device device = DeviceManagementDAOUtil.loadDevice(rs);
devices.add(device); devices.add(device);
} }
} catch (SQLException e) { } catch (SQLException e) {
@ -212,35 +212,6 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
return devices; 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 @Override
public List<Device> getAllocatedDevices(PaginationRequest request, int tenantId, int activeServerCount, int serverIndex) public List<Device> getAllocatedDevices(PaginationRequest request, int tenantId, int activeServerCount, int serverIndex)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {

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

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

@ -190,12 +190,7 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
} }
@Override @Override
public List<DeviceBilling> getRemovedDeviceBillList(PaginationRequest request, int tenantId) throws DeviceManagementDAOException { public List<Device> getDeviceListWithoutPagination(int tenantId) throws DeviceManagementDAOException {
return null;
}
@Override
public List<DeviceBilling> getDeviceBillList(PaginationRequest request, int tenantId, Timestamp startDate, Timestamp endDate) throws DeviceManagementDAOException {
return null; 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; 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. * Returns the device of specified id.
* *

@ -138,6 +138,8 @@ import java.io.StringWriter;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -156,6 +158,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
private final EnrollmentDAO enrollmentDAO; private final EnrollmentDAO enrollmentDAO;
private final ApplicationDAO applicationDAO; private final ApplicationDAO applicationDAO;
private MetadataDAO metadataDAO; private MetadataDAO metadataDAO;
private final BillingDAO billingDAO;
private final DeviceStatusDAO deviceStatusDAO; private final DeviceStatusDAO deviceStatusDAO;
public DeviceManagementProviderServiceImpl() { public DeviceManagementProviderServiceImpl() {
@ -165,6 +168,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
this.enrollmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO(); this.enrollmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO();
this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO(); this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO();
this.billingDAO = DeviceManagementDAOFactory.getBillingDAO();
this.deviceStatusDAO = DeviceManagementDAOFactory.getDeviceStatusDAO(); this.deviceStatusDAO = DeviceManagementDAOFactory.getDeviceStatusDAO();
/* Registering a listener to retrieve events when some device management service plugin is installed after /* Registering a listener to retrieve events when some device management service plugin is installed after
@ -937,101 +941,153 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return this.getAllDevices(request, true); return this.getAllDevices(request, true);
} }
@Override public PaginationResult calculateCost(int tenantId, String tenantDomain, Timestamp startDate, Timestamp endDate, boolean generateBill, List<Device> allDevices) throws DeviceManagementException {
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(); PaginationResult paginationResult = new PaginationResult();
double totalCost = 0.0; double totalCost = 0.0;
List<Device> allDevices; boolean allDevicesBilledDateIsValid = true;
int count = 0; String lastBilledDates = "";
ArrayList<String> lastBilledDatesList = new ArrayList<>();
List<Billing> invalidDevices = new ArrayList<>();
List<Device> removeBillingPeriodInvalidDevices = new ArrayList<>() ;
List<Device> removeStatusUpdateInvalidDevices = new ArrayList<>() ;
try { 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(); MetadataManagementDAOFactory.beginTransaction();
Metadata metadata = metadataDAO.getMetadata(tenantId, metaKey); Metadata metadata = metadataDAO.getMetadata(-1234, DeviceManagementConstants.META_KEY);
Gson g = new Gson(); Gson g = new Gson();
Collection<Cost> costData = null; Collection<Cost> costData = null;
Type collectionType = new TypeToken<Collection<Cost>>() {}.getType(); Type collectionType = new TypeToken<Collection<Cost>>() {}.getType();
if (metadata != null) { if (metadata != null) {
costData = g.fromJson(metadata.getMetaValue(), collectionType); costData = g.fromJson(metadata.getMetaValue(), collectionType);
for (Cost tenantCost : costData) { for (Cost tenantCost : costData) {
if (tenantCost.getTenantDomain().equals(tenantDomain)) { if (tenantCost.getTenantDomain().equals(tenantDomain)) {
for (Device device : allDevices) { for (Device device : allDevices) {
device.setDeviceStatusInfo(getDeviceStatusHistory(device, startDate, endDate, true)); device.setDeviceStatusInfo(getDeviceStatusHistory(device, null, null, true));
long dateDiff = 0; long dateDiff = 0;
List<DeviceStatus> deviceStatus = device.getDeviceStatusInfo(); List<DeviceStatus> deviceStatus = device.getDeviceStatusInfo();
boolean lastBilledDate = false; 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);
}
}
}
if (!billDateIsInvalid) {
for (int i = 0; i < deviceStatus.size(); i++) { for (int i = 0; i < deviceStatus.size(); i++) {
if (deviceStatus.get(i).getStatus().toString().equals("ACTIVE")) { if (deviceStatus.get(i).getStatus().toString().equals("ACTIVE")) {
if (deviceStatus.size() > i + 1) { if (deviceStatus.size() > i + 1) {
if (!lastBilledDate) { if (!lastBilledDate) {
lastBilledDate = true; lastBilledDate = true;
if (device.getEnrolmentInfo().getLastBilledDate() == 0) { if (device.getEnrolmentInfo().getLastBilledDate() == 0) {
deviceStatusIsValid = true;
dateDiff = dateDiff + (deviceStatus.get(i + 1).getUpdateTime().getTime() - deviceStatus.get(i).getUpdateTime().getTime()); dateDiff = dateDiff + (deviceStatus.get(i + 1).getUpdateTime().getTime() - deviceStatus.get(i).getUpdateTime().getTime());
} else { } else {
if (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()); dateDiff = dateDiff + (deviceStatus.get(i + 1).getUpdateTime().getTime() - device.getEnrolmentInfo().getLastBilledDate());
} }
} }
} else { } else {
if (deviceStatus.get(i).getUpdateTime().getTime() >= device.getEnrolmentInfo().getLastBilledDate()) { if (deviceStatus.get(i).getUpdateTime().getTime() >= device.getEnrolmentInfo().getLastBilledDate()) {
deviceStatusIsValid = true;
dateDiff = dateDiff + (deviceStatus.get(i + 1).getUpdateTime().getTime() - deviceStatus.get(i).getUpdateTime().getTime()); 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) { if (!lastBilledDate) {
lastBilledDate = true; 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 (device.getEnrolmentInfo().getLastBilledDate() == 0) {
if (endDate.getTime() >= deviceStatus.get(i).getUpdateTime().getTime()) {
deviceStatusIsValid = true;
dateDiff = dateDiff + (endDate.getTime() - deviceStatus.get(i).getUpdateTime().getTime()); dateDiff = dateDiff + (endDate.getTime() - deviceStatus.get(i).getUpdateTime().getTime());
}
} else { } else {
if (endDate.getTime() >= device.getEnrolmentInfo().getLastBilledDate()) { if (endDate.getTime() >= device.getEnrolmentInfo().getLastBilledDate()) {
deviceStatusIsValid = true;
dateDiff = dateDiff + (endDate.getTime() - device.getEnrolmentInfo().getLastBilledDate()); dateDiff = dateDiff + (endDate.getTime() - device.getEnrolmentInfo().getLastBilledDate());
} }
} }
} else { } 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()); 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);; long dateInDays = TimeUnit.DAYS.convert(dateDiff, TimeUnit.MILLISECONDS);
double cost = (tenantCost.getCost() / 365) * dateInDays; double cost = (tenantCost.getCost() / 365) * dateInDays;
totalCost = cost + totalCost; totalCost = cost + totalCost;
device.setCost(Math.round(cost * 100.0) / 100.0); device.setCost(Math.round(cost * 100.0) / 100.0);
device.setDaysUsed((int) dateInDays); device.setDaysUsed((int) dateInDays);
if (generateBill) { if (generateBill) {
Timestamp timestamp = new Timestamp(System.currentTimeMillis()); enrollmentDAO.updateEnrollmentLastBilledDate(device.getEnrolmentInfo(), endDate, tenantId);
enrollmentDAO.updateEnrollmentLastBilledDate(device.getEnrolmentInfo(), timestamp, 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) { } catch (DeviceManagementDAOException e) {
String msg = "Error occurred while retrieving device list pertaining to the current tenant"; String msg = "Error occurred while retrieving device list pertaining to the current tenant";
log.error(msg, e); log.error(msg, e);
throw new DeviceManagementException(msg, e); throw new DeviceManagementException(msg, e);
} } catch (Exception e) {
catch (Exception e) {
String msg = "Error occurred in getAllDevices"; String msg = "Error occurred in getAllDevices";
log.error(msg, e); log.error(msg, e);
throw new DeviceManagementException(msg, e); throw new DeviceManagementException(msg, e);
@ -1039,13 +1095,83 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
MetadataManagementDAOFactory.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.setData(allDevices);
paginationResult.setTotalCost(Math.round(totalCost * 100.0) / 100.0); 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.setRecordsFiltered(count);
paginationResult.setRecordsTotal(count); paginationResult.setRecordsTotal(count);
return paginationResult; 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 @Override
public PaginationResult getAllDevices(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { public PaginationResult getAllDevices(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException {
if (request == null) { if (request == null) {
@ -1862,6 +1988,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
} }
} }
@Override @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()) { if (log.isDebugEnabled()) {

Loading…
Cancel
Save