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 {
@ -217,8 +205,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
} }
if (status != null && !status.isEmpty()) { if (status != null && !status.isEmpty()) {
boolean isStatusEmpty = true; boolean isStatusEmpty = true;
for (String statusString : status){ for (String statusString : status) {
if (StringUtils.isNotBlank(statusString)){ if (StringUtils.isNotBlank(statusString)) {
isStatusEmpty = false; isStatusEmpty = false;
break; break;
} }
@ -348,10 +336,10 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
@Override @Override
@Path("/billing") @Path("/billing")
public Response getDevicesBilling( public Response getDevicesBilling(
@QueryParam ("tenantDomain") String tenantDomain, @QueryParam("tenantDomain") String tenantDomain,
@QueryParam ("startDate") Timestamp startDate, @QueryParam("startDate") Timestamp startDate,
@QueryParam ("endDate") Timestamp endDate, @QueryParam("endDate") Timestamp endDate,
@QueryParam ("generateBill") boolean generateBill, @QueryParam("generateBill") boolean generateBill,
@QueryParam("offset") int offset, @QueryParam("offset") int offset,
@DefaultValue("10") @DefaultValue("10")
@QueryParam("limit") int limit) { @QueryParam("limit") int limit) {
@ -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")
@ -433,8 +467,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
* Validate group Id and group Id greater than 0 and exist. * Validate group Id and group Id greater than 0 and exist.
* *
* @param groupId Group ID of the group * @param groupId Group ID of the group
* @param from time to start getting DeviceLocationHistorySnapshotWrapper in milliseconds * @param from time to start getting DeviceLocationHistorySnapshotWrapper in milliseconds
* @param to time to end getting DeviceLocationHistorySnapshotWrapper in milliseconds * @param to time to end getting DeviceLocationHistorySnapshotWrapper in milliseconds
*/ */
private static void validateGroupId(int groupId, long from, long to) throws GroupManagementException, BadRequestException { private static void validateGroupId(int groupId, long from, long to) throws GroupManagementException, BadRequestException {
if (from == 0 || to == 0) { if (from == 0 || to == 0) {
@ -463,7 +497,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
@QueryParam("to") long to, @QueryParam("to") long to,
@QueryParam("type") String type, @QueryParam("type") String type,
@DefaultValue("0") @QueryParam("offset") int offset, @DefaultValue("0") @QueryParam("offset") int offset,
@DefaultValue("100") @QueryParam("limit") int limit){ @DefaultValue("100") @QueryParam("limit") int limit) {
try { try {
RequestValidationUtil.validatePaginationParameters(offset, limit); RequestValidationUtil.validatePaginationParameters(offset, limit);
DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
@ -472,31 +506,31 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
// this is the user who initiates the request // this is the user who initiates the request
String authorizedUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); String authorizedUser = CarbonContext.getThreadLocalCarbonContext().getUsername();
try { try {
validateGroupId(groupId, from, to); validateGroupId(groupId, from, to);
boolean isPermitted = DeviceMgtAPIUtils.checkPermission(groupId, authorizedUser); boolean isPermitted = DeviceMgtAPIUtils.checkPermission(groupId, authorizedUser);
if (isPermitted) { if (isPermitted) {
request.setGroupId(groupId); request.setGroupId(groupId);
} else { } else {
String msg = "Current user '" + authorizedUser String msg = "Current user '" + authorizedUser
+ "' doesn't have enough privileges to list devices of group '" + "' doesn't have enough privileges to list devices of group '"
+ groupId + "'"; + groupId + "'";
log.error(msg);
return Response.status(Response.Status.FORBIDDEN).entity(msg).build();
}
} catch (GroupManagementException e) {
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){
String msg = "Error occurred while retrieving role list of user '" + authorizedUser + "'";
log.error(msg); log.error(msg);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); return Response.status(Response.Status.FORBIDDEN).entity(msg).build();
} }
} catch (GroupManagementException e) {
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) {
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();
}
PaginationResult result = dms.getAllDevices(request, false); PaginationResult result = dms.getAllDevices(request, false);
if(!result.getData().isEmpty()){ if (!result.getData().isEmpty()) {
devices.setList((List<Device>) result.getData()); devices.setList((List<Device>) result.getData());
for (Device device : devices.getList()) { for (Device device : devices.getList()) {
@ -604,7 +638,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
DeviceData deviceData = new DeviceData(); DeviceData deviceData = new DeviceData();
deviceData.setDeviceIdentifier(deviceIdentifier); deviceData.setDeviceIdentifier(deviceIdentifier);
if (!StringUtils.isBlank(ifModifiedSince)){ if (!StringUtils.isBlank(ifModifiedSince)) {
SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
try { try {
sinceDate = format.parse(ifModifiedSince); sinceDate = format.parse(ifModifiedSince);
@ -617,10 +651,10 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
} }
} }
if (!StringUtils.isBlank(owner)){ if (!StringUtils.isBlank(owner)) {
deviceData.setDeviceOwner(owner); deviceData.setDeviceOwner(owner);
} }
if (!StringUtils.isBlank(ownership)){ if (!StringUtils.isBlank(ownership)) {
deviceData.setDeviceOwnership(ownership); deviceData.setDeviceOwnership(ownership);
} }
device = dms.getDevice(deviceData, true); device = dms.getDevice(deviceData, true);
@ -710,7 +744,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
sinceDate = format.parse(ifModifiedSince); sinceDate = format.parse(ifModifiedSince);
} catch (ParseException e) { } catch (ParseException e) {
String message = "Error occurred while parse the since date.Invalid date string is provided in " + String message = "Error occurred while parse the since date.Invalid date string is provided in " +
"'If-Modified-Since' header"; "'If-Modified-Since' header";
log.error(message, e); log.error(message, e);
return Response.status(Response.Status.BAD_REQUEST).entity( return Response.status(Response.Status.BAD_REQUEST).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage("Invalid date " + new ErrorResponse.ErrorResponseBuilder().setMessage("Invalid date " +
@ -723,7 +757,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
String message = "No device is modified after the timestamp provided in 'If-Modified-Since' header"; String message = "No device is modified after the timestamp provided in 'If-Modified-Since' header";
log.error(message); log.error(message);
return Response.status(Response.Status.NOT_MODIFIED).entity("No device is modified " + return Response.status(Response.Status.NOT_MODIFIED).entity("No device is modified " +
"after the timestamp provided in 'If-Modified-Since' header").build(); "after the timestamp provided in 'If-Modified-Since' header").build();
} }
} else { } else {
device = dms.getDevice(id, requireDeviceInfo); device = dms.getDevice(id, requireDeviceInfo);
@ -738,7 +772,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
// check whether the user is authorized // check whether the user is authorized
if (!deviceAccessAuthorizationService.isUserAuthorized(deviceIdentifier, authorizedUser)) { if (!deviceAccessAuthorizationService.isUserAuthorized(deviceIdentifier, authorizedUser)) {
String message = "User '" + authorizedUser + "' is not authorized to retrieve the given " + String message = "User '" + authorizedUser + "' is not authorized to retrieve the given " +
"device id '" + id + "'"; "device id '" + id + "'";
log.error(message); log.error(message);
return Response.status(Response.Status.UNAUTHORIZED).entity( return Response.status(Response.Status.UNAUTHORIZED).entity(
new ErrorResponse.ErrorResponseBuilder().setCode(401l).setMessage(message).build()).build(); new ErrorResponse.ErrorResponseBuilder().setCode(401l).setMessage(message).build()).build();
@ -893,7 +927,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
List<Device> devices; List<Device> devices;
DeviceList deviceList = new DeviceList(); DeviceList deviceList = new DeviceList();
try { try {
if(map.getProperties().isEmpty()){ if (map.getProperties().isEmpty()) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("No search criteria defined when querying devices."); log.debug("No search criteria defined when querying devices.");
} }
@ -901,7 +935,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
} }
DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
devices = dms.getDevicesBasedOnProperties(map.getProperties()); devices = dms.getDevicesBasedOnProperties(map.getProperties());
if(devices == null || devices.isEmpty()){ if (devices == null || devices.isEmpty()) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("No Devices Found for criteria : " + map); log.debug("No Devices Found for criteria : " + map);
} }
@ -1201,14 +1235,14 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
/** /**
* List device status history * List device status history
* *
* @param type Device type * @param type Device type
* @param id Device id * @param id Device id
* @return {@link Response} object * @return {@link Response} object
*/ */
@GET @GET
@Path("/{type}/{id}/getstatushistory") @Path("/{type}/{id}/getstatushistory")
public Response getDeviceStatusHistory(@PathParam("type") @Size(max = 45) String type, public Response getDeviceStatusHistory(@PathParam("type") @Size(max = 45) String type,
@PathParam("id") @Size(max = 45) String id) { @PathParam("id") @Size(max = 45) String id) {
//TODO check authorization for this //TODO check authorization for this
RequestValidationUtil.validateDeviceIdentifier(type, id); RequestValidationUtil.validateDeviceIdentifier(type, id);
DeviceManagementProviderService deviceManagementProviderService = DeviceManagementProviderService deviceManagementProviderService =
@ -1233,14 +1267,14 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
/** /**
* List device status history for the current enrolment * List device status history for the current enrolment
* *
* @param type Device type * @param type Device type
* @param id Device id * @param id Device id
* @return {@link Response} object * @return {@link Response} object
*/ */
@GET @GET
@Path("/{type}/{id}/getenrolmentstatushistory") @Path("/{type}/{id}/getenrolmentstatushistory")
public Response getCurrentEnrolmentDeviceStatusHistory(@PathParam("type") @Size(max = 45) String type, public Response getCurrentEnrolmentDeviceStatusHistory(@PathParam("type") @Size(max = 45) String type,
@PathParam("id") @Size(max = 45) String id) { @PathParam("id") @Size(max = 45) String id) {
//TODO check authorization for this or current enrolment should be based on for the enrolment associated with the user //TODO check authorization for this or current enrolment should be based on for the enrolment associated with the user
RequestValidationUtil.validateDeviceIdentifier(type, id); RequestValidationUtil.validateDeviceIdentifier(type, id);
DeviceManagementProviderService deviceManagementProviderService = DeviceManagementProviderService deviceManagementProviderService =
@ -1544,7 +1578,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
if (MDMAppConstants.AndroidConstants.OPCODE_INSTALL_APPLICATION.equals(operation.getCode()) || if (MDMAppConstants.AndroidConstants.OPCODE_INSTALL_APPLICATION.equals(operation.getCode()) ||
MDMAppConstants.AndroidConstants.OPCODE_UNINSTALL_APPLICATION.equals(operation.getCode())) { MDMAppConstants.AndroidConstants.OPCODE_UNINSTALL_APPLICATION.equals(operation.getCode())) {
ApplicationManager applicationManager = DeviceMgtAPIUtils.getApplicationManager(); 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(); return Response.status(Response.Status.OK).entity("OperationStatus updated successfully.").build();
} catch (BadRequestException e) { } 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. " + 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
@ -768,8 +772,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
throw new DeviceManagementException(msg); throw new DeviceManagementException(msg);
} }
if (log.isDebugEnabled()) { 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); " type '" + deviceType);
} }
List<Device> allocatedDevices; List<Device> allocatedDevices;
try { try {
@ -783,7 +787,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
String msg = "Error occurred while retrieving all devices of type '" + String msg = "Error occurred while retrieving all devices of type '" +
deviceType + "' that are being managed within the scope of current tenant"; deviceType + "' that are being managed within the scope of current tenant";
log.error(msg, e); log.error(msg, e);
throw new DeviceManagementException(msg, e); throw new DeviceManagementException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
@ -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);
}
}
}
for (int i=0; i<deviceStatus.size(); i++) { if (!billDateIsInvalid) {
if(deviceStatus.get(i).getStatus().toString().equals("ACTIVE")) { for (int i = 0; i < deviceStatus.size(); i++) {
if (deviceStatus.size()> i+1) { if (deviceStatus.get(i).getStatus().toString().equals("ACTIVE")) {
if (!lastBilledDate) { if (deviceStatus.size() > i + 1) {
lastBilledDate = true; if (!lastBilledDate) {
if (device.getEnrolmentInfo().getLastBilledDate() == 0) { lastBilledDate = true;
dateDiff = dateDiff + (deviceStatus.get(i+1).getUpdateTime().getTime() - deviceStatus.get(i).getUpdateTime().getTime()); if (device.getEnrolmentInfo().getLastBilledDate() == 0) {
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()) {
deviceStatusIsValid = true;
dateDiff = dateDiff + (deviceStatus.get(i + 1).getUpdateTime().getTime() - device.getEnrolmentInfo().getLastBilledDate());
}
}
} else { } else {
if (deviceStatus.get(i+1).getUpdateTime().getTime() >= device.getEnrolmentInfo().getLastBilledDate()) { if (deviceStatus.get(i).getUpdateTime().getTime() >= device.getEnrolmentInfo().getLastBilledDate()) {
dateDiff = dateDiff + (deviceStatus.get(i+1).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 ( deviceStatus.get(i).getUpdateTime().getTime() >= device.getEnrolmentInfo().getLastBilledDate()) { // If only one status row is retrieved this block is executed
dateDiff = dateDiff + (deviceStatus.get(i+1).getUpdateTime().getTime() - deviceStatus.get(i).getUpdateTime().getTime()); if (!lastBilledDate) {
} lastBilledDate = true;
} // Is executed if there is no lastBilled date and if the updates time is before the enddate
} else { if (device.getEnrolmentInfo().getLastBilledDate() == 0) {
if (!lastBilledDate) { if (endDate.getTime() >= deviceStatus.get(i).getUpdateTime().getTime()) {
lastBilledDate = true; deviceStatusIsValid = true;
if (device.getEnrolmentInfo().getLastBilledDate() == 0) { dateDiff = dateDiff + (endDate.getTime() - deviceStatus.get(i).getUpdateTime().getTime());
dateDiff = dateDiff + (endDate.getTime() - deviceStatus.get(i).getUpdateTime().getTime()); }
} else {
if (endDate.getTime() >= device.getEnrolmentInfo().getLastBilledDate()) {
deviceStatusIsValid = true;
dateDiff = dateDiff + (endDate.getTime() - device.getEnrolmentInfo().getLastBilledDate());
}
}
} else { } else {
if (endDate.getTime() >= device.getEnrolmentInfo().getLastBilledDate()) { if (device.getEnrolmentInfo().getLastBilledDate() <= deviceStatus.get(i).getUpdateTime().getTime()
dateDiff = dateDiff +(endDate.getTime() - device.getEnrolmentInfo().getLastBilledDate()); && 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());
} }
} }
} else {
dateDiff = dateDiff + (endDate.getTime() - deviceStatus.get(i).getUpdateTime().getTime());
} }
} }
} }
}
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,8 +1988,9 @@ 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()) {
log.debug("get status history of device: " + device.getDeviceIdentifier()); log.debug("get status history of device: " + device.getDeviceIdentifier());
} }
@ -1883,7 +2010,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
@Override @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()) { if (log.isDebugEnabled()) {
log.debug("get status history of device: " + device.getDeviceIdentifier()); log.debug("get status history of device: " + device.getDeviceIdentifier());
} }
@ -1912,12 +2039,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
@Override @Override
public List<DeviceStatus> getDeviceStatusHistory(Device device) throws DeviceManagementException{ public List<DeviceStatus> getDeviceStatusHistory(Device device) throws DeviceManagementException {
return getDeviceStatusHistory(device, null, null, false); return getDeviceStatusHistory(device, null, null, false);
} }
@Override @Override
public List<DeviceStatus> getDeviceCurrentEnrolmentStatusHistory(Device device) throws DeviceManagementException{ public List<DeviceStatus> getDeviceCurrentEnrolmentStatusHistory(Device device) throws DeviceManagementException {
return getDeviceCurrentEnrolmentStatusHistory(device, null, null); return getDeviceCurrentEnrolmentStatusHistory(device, null, null);
} }
@ -3334,7 +3461,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override @Override
public List<DeviceLocationHistorySnapshot> getDeviceLocationInfo(DeviceIdentifier deviceIdentifier, long from, public List<DeviceLocationHistorySnapshot> getDeviceLocationInfo(DeviceIdentifier deviceIdentifier, long from,
long to) throws DeviceManagementException { long to) throws DeviceManagementException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Get device location information"); log.debug("Get device location information");
} }
@ -4489,7 +4616,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override @Override
public void triggerCorrectiveActions(String deviceIdentifier, String featureCode, List<String> actions, public void triggerCorrectiveActions(String deviceIdentifier, String featureCode, List<String> actions,
List<ConfigurationEntry> configList) throws DeviceManagementException, DeviceNotFoundException { List<ConfigurationEntry> configList) throws DeviceManagementException, DeviceNotFoundException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Triggering Corrective action. Device Identifier: " + deviceIdentifier); log.debug("Triggering Corrective action. Device Identifier: " + deviceIdentifier);
} }
@ -4570,10 +4697,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
@Override @Override
public License getLicenseConfig (String deviceTypeName) throws DeviceManagementException { public License getLicenseConfig(String deviceTypeName) throws DeviceManagementException {
DeviceManagementService deviceManagementService = DeviceManagementService deviceManagementService =
pluginRepository.getDeviceManagementService(deviceTypeName, pluginRepository.getDeviceManagementService(deviceTypeName,
this.getTenantId()); this.getTenantId());
if (deviceManagementService == null) { if (deviceManagementService == null) {
String msg = "Device management service loading is failed for the device type: " + deviceTypeName; String msg = "Device management service loading is failed for the device type: " + deviceTypeName;
log.error(msg); log.error(msg);

Loading…
Cancel
Save