Add re-enrollment device cost calculation
feature/traccar-sync
osh 3 years ago
parent 8872d1baeb
commit c135105889

@ -81,6 +81,7 @@ import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.sql.Timestamp;
import java.util.List; import java.util.List;
/** /**
@ -399,9 +400,23 @@ public interface DeviceManagementService {
@ApiParam( @ApiParam(
name = "tenantDomain", name = "tenantDomain",
value = "The tenant domain.", value = "The tenant domain.",
required = false, required = false)
defaultValue = "nita")
String tenantDomain, 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,
@ApiParam( @ApiParam(
name = "offset", name = "offset",
value = "The starting pagination index for the complete list of qualified items.", value = "The starting pagination index for the complete list of qualified items.",

@ -133,6 +133,7 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.DefaultValue; import javax.ws.rs.DefaultValue;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.sql.Timestamp;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
@ -345,24 +346,24 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
@Override @Override
@Path("/billing") @Path("/billing")
public Response getDevicesBilling( public Response getDevicesBilling(
@DefaultValue("nita")
@QueryParam ("tenantDomain") String tenantDomain, @QueryParam ("tenantDomain") String tenantDomain,
@QueryParam ("startDate") Timestamp startDate,
@QueryParam ("endDate") Timestamp endDate,
@QueryParam ("generateBill") boolean generateBill,
@QueryParam("offset") int offset, @QueryParam("offset") int offset,
@DefaultValue("10") @DefaultValue("10")
@QueryParam("limit") int limit) { @QueryParam("limit") int limit) {
try { try {
RequestValidationUtil.validatePaginationParameters(offset, limit); RequestValidationUtil.validatePaginationParameters(offset, limit);
DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
// DeviceAccessAuthorizationService deviceAccessAuthorizationService =
// DeviceMgtAPIUtils.getDeviceAccessAuthorizationService();
PaginationRequest request = new PaginationRequest(offset, limit); PaginationRequest request = new PaginationRequest(offset, limit);
PaginationResult result; PaginationResult result;
DeviceList devices = new DeviceList(); DeviceList devices = new DeviceList();
try { try {
result = dms.getAllDevicesBillings(request, true, tenantDomain); result = dms.getAllDevicesBillings(request, tenantDomain, startDate, endDate, generateBill);
} catch (Exception exception) { } catch (Exception exception) {
String msg = "------------------TEST ERROR-----------------------"; String msg = "Error occurred when trying to retrieve billing data";
log.error(msg, exception); log.error(msg, exception);
return Response.serverError().entity( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
@ -379,7 +380,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
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 fetching all enrolled devices"; String msg = "Error occurred while retrieving billing data";
log.error(msg, e); log.error(msg, e);
return Response.serverError().entity( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();

@ -24,8 +24,10 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.app.mgt.Application;
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistorySnapshotWrapper; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistorySnapshotWrapper;
import org.wso2.carbon.device.mgt.common.type.mgt.DeviceStatus;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@ApiModel(value = "Device", description = "This class carries all information related to a managed device.") @ApiModel(value = "Device", description = "This class carries all information related to a managed device.")
@ -72,6 +74,17 @@ public class Device implements Serializable {
required = false) required = false)
private List<Application> applications; private List<Application> applications;
@ApiModelProperty(name = "cost", value = "Cost charged per device.", required = false)
private Double cost;
@ApiModelProperty(name = "daysUsed", value = "Number of days gone since device enrollment.",
required = false)
private int daysUsed;
@ApiModelProperty(name = "deviceStatusInfo", value = "This defines the device status details. " +
"It is mandatory to define this information.", required = false)
private List<DeviceStatus> deviceStatusInfo = new ArrayList<>();
@ApiModelProperty( @ApiModelProperty(
name = "historySnapshot", name = "historySnapshot",
value = "device history snapshots") value = "device history snapshots")
@ -92,6 +105,30 @@ public class Device implements Serializable {
this.properties = properties; this.properties = properties;
} }
public Double getCost() {
return cost;
}
public void setCost(Double cost) {
this.cost = cost;
}
public int getDaysUsed() {
return daysUsed;
}
public void setDaysUsed(int daysUsed) {
this.daysUsed = daysUsed;
}
public List<DeviceStatus> getDeviceStatusInfo() {
return deviceStatusInfo;
}
public void setDeviceStatusInfo(List<DeviceStatus> deviceStatusInfo) {
this.deviceStatusInfo = deviceStatusInfo;
}
public int getId() { public int getId() {
return id; return id;
} }

@ -17,18 +17,17 @@
*/ */
package org.wso2.carbon.device.mgt.common; package org.wso2.carbon.device.mgt.common;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.Gson; import com.google.gson.Gson;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistorySnapshotWrapper; import org.wso2.carbon.device.mgt.common.type.mgt.DeviceStatus;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@ApiModel(value = "DeviceBilling", description = "This class carries all information related to a managed device.") @ApiModel(value = "DeviceBilling", description = "This class carries all information related to a device billing.")
public class DeviceBilling implements Serializable { public class DeviceBilling implements Serializable {
private static final long serialVersionUID = 1998101711L; private static final long serialVersionUID = 1998101711L;
@ -40,9 +39,6 @@ public class DeviceBilling implements Serializable {
@ApiModelProperty(name = "name", value = "The device name that can be set on the device by the device user.", @ApiModelProperty(name = "name", value = "The device name that can be set on the device by the device user.",
required = true) required = true)
private String name; private String name;
//
// @ApiModelProperty(name = "type", value = "The OS type of the device.", required = true)
// private String type;
@ApiModelProperty(name = "description", value = "Additional information on the device.", required = false) @ApiModelProperty(name = "description", value = "Additional information on the device.", required = false)
private String description; private String description;
@ -58,7 +54,7 @@ public class DeviceBilling implements Serializable {
required = false) required = false)
private String deviceIdentifier; private String deviceIdentifier;
@ApiModelProperty(name = "daysSinceEnrolled", value = "Number of days gone since device enrollment date to current date.", @ApiModelProperty(name = "daysSinceEnrolled", value = "Number of days gone since device enrollment.",
required = false) required = false)
private int daysSinceEnrolled; private int daysSinceEnrolled;
@ -70,39 +66,23 @@ public class DeviceBilling implements Serializable {
"It is mandatory to define this information.", required = false) "It is mandatory to define this information.", required = false)
private EnrolmentInfo enrolmentInfo; private EnrolmentInfo enrolmentInfo;
// @ApiModelProperty(name = "features", value = "List of features.", required = false) @ApiModelProperty(name = "deviceStatusInfo", value = "This defines the device status details. " +
// private List<Feature> features; "It is mandatory to define this information.", required = false)
private List<DeviceStatus> deviceStatusInfo = new ArrayList<>();
// private List<DeviceBilling.Property> properties;
@ApiModelProperty(name = "advanceInfo", value = "This defines the device registration related information. " + @ApiModelProperty(name = "advanceInfo", value = "This defines the device registration related information. " +
"It is mandatory to define this information.", required = false) "It is mandatory to define this information.", required = false)
private DeviceInfo deviceInfo; private DeviceInfo deviceInfo;
// @ApiModelProperty(name = "applications", value = "This represents the application list installed into the device",
// required = false)
// private List<Application> applications;
// @ApiModelProperty(
// name = "historySnapshot",
// value = "device history snapshots")
// @JsonProperty(value = "historySnapshot")
// private DeviceLocationHistorySnapshotWrapper historySnapshot;
public DeviceBilling() { public DeviceBilling() {
} }
public DeviceBilling(String name, String description, EnrolmentInfo enrolmentInfo) { public List<DeviceStatus> getDeviceStatusInfo() {
this.name = name; return deviceStatusInfo;
this.description = description;
this.enrolmentInfo = enrolmentInfo;
} }
public DeviceBilling(String name, String description, String deviceId, EnrolmentInfo enrolmentInfo) { public void setDeviceStatusInfo(List<DeviceStatus> deviceStatusInfo) {
this.name = name; this.deviceStatusInfo = deviceStatusInfo;
this.description = description;
this.deviceIdentifier = deviceId;
this.enrolmentInfo = enrolmentInfo;
} }
public int getDaysUsed() { public int getDaysUsed() {
@ -177,46 +157,11 @@ public class DeviceBilling implements Serializable {
this.deviceInfo = deviceInfo; this.deviceInfo = deviceInfo;
} }
public static class Property {
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
@Override @Override
public String toString() { public String toString() {
return new Gson().toJson(this); return new Gson().toJson(this);
} }
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof DeviceBilling))
return false;
DeviceBilling device = (DeviceBilling) o;
return getDeviceIdentifier().equals(device.getDeviceIdentifier());
}
@Override @Override
public int hashCode() { public int hashCode() {
return getDeviceIdentifier().hashCode(); return getDeviceIdentifier().hashCode();

@ -49,7 +49,7 @@ public class EnrolmentInfo implements Serializable {
private Long dateOfEnrolment; private Long dateOfEnrolment;
@ApiModelProperty(name = "dateOfLastUpdate", value = "Date of the device's last update. This value is not necessary.", required = false ) @ApiModelProperty(name = "dateOfLastUpdate", value = "Date of the device's last update. This value is not necessary.", required = false )
private Long dateOfLastUpdate; private Long dateOfLastUpdate;
@ApiModelProperty(name = "lastBilledDate", value = "Date of the device's last update. This value is not necessary.", required = false ) @ApiModelProperty(name = "lastBilledDate", value = "Date of the device's last billed date", required = false )
private Long lastBilledDate; private Long lastBilledDate;
@ApiModelProperty(name = "ownership", value = "Defines the ownership details. The ownership type can be any of the" + @ApiModelProperty(name = "ownership", value = "Defines the ownership details. The ownership type can be any of the" +
" following values.\n" + " following values.\n" +
@ -95,14 +95,6 @@ public class EnrolmentInfo implements Serializable {
this.dateOfEnrolment = dateOfEnrolment; this.dateOfEnrolment = dateOfEnrolment;
} }
public Long getDateOfLastUpdate() {
return dateOfLastUpdate;
}
public void setDateOfLastUpdate(Long dateOfLastUpdate) {
this.dateOfLastUpdate = dateOfLastUpdate;
}
public Long getLastBilledDate() { public Long getLastBilledDate() {
return lastBilledDate; return lastBilledDate;
} }
@ -111,6 +103,14 @@ public class EnrolmentInfo implements Serializable {
this.lastBilledDate = lastBilledDate; this.lastBilledDate = lastBilledDate;
} }
public Long getDateOfLastUpdate() {
return dateOfLastUpdate;
}
public void setDateOfLastUpdate(Long dateOfLastUpdate) {
this.dateOfLastUpdate = dateOfLastUpdate;
}
public OwnerShip getOwnership() { public OwnerShip getOwnership() {
return ownership; return ownership;
} }

@ -22,11 +22,9 @@ import com.google.gson.Gson;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import java.sql.Timestamp;
import java.util.Date;
@XmlRootElement(name = "Cost") @XmlRootElement(name = "Cost")
public class Costdata { public class Cost {
private String tenantDomain; private String tenantDomain;
private Double cost; private Double cost;

@ -47,6 +47,7 @@ import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.common.geo.service.GeoCluster; import org.wso2.carbon.device.mgt.common.geo.service.GeoCluster;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -300,7 +301,7 @@ public interface DeviceDAO {
* @return returns paginated list of not removed devices of tenant. * @return returns paginated list of not removed devices of tenant.
* @throws DeviceManagementDAOException * @throws DeviceManagementDAOException
*/ */
List<DeviceBilling> getDeviceBillList(PaginationRequest request, int tenantId) 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. * This method is used to retrieve the removed devices of a given tenant as a paginated result.

@ -34,7 +34,7 @@ public interface DeviceStatusDAO {
List<DeviceStatus> getStatus(int deviceId, int tenantId) throws DeviceManagementDAOException; List<DeviceStatus> getStatus(int deviceId, int tenantId) throws DeviceManagementDAOException;
List<DeviceStatus> getStatus(int deviceId, int tenantId, Date fromDate, Date toDate) throws DeviceManagementDAOException; List<DeviceStatus> getStatus(int deviceId, int tenantId, Date fromDate, Date toDate, boolean billingStatus) throws DeviceManagementDAOException;
List<DeviceStatus> getStatus(int enrolmentId) throws DeviceManagementDAOException; List<DeviceStatus> getStatus(int enrolmentId) throws DeviceManagementDAOException;

@ -22,6 +22,7 @@ import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status; import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status;
import java.sql.Timestamp;
import java.util.List; import java.util.List;
public interface EnrollmentDAO { public interface EnrollmentDAO {
@ -32,6 +33,8 @@ public interface EnrollmentDAO {
boolean updateEnrollmentStatus(List<EnrolmentInfo> enrolmentInfos) throws DeviceManagementDAOException; boolean updateEnrollmentStatus(List<EnrolmentInfo> enrolmentInfos) throws DeviceManagementDAOException;
boolean updateEnrollmentLastBilledDate(EnrolmentInfo enrolmentInfos, Timestamp lastBilledDate, int tenantId) throws DeviceManagementDAOException;
int removeEnrollment(int deviceId, String currentOwner, int tenantId) throws DeviceManagementDAOException; int removeEnrollment(int deviceId, String currentOwner, int tenantId) throws DeviceManagementDAOException;
@Deprecated @Deprecated

@ -15,7 +15,7 @@ import java.util.List;
public class DeviceStatusDAOImpl implements DeviceStatusDAO { public class DeviceStatusDAOImpl implements DeviceStatusDAO {
private List<DeviceStatus> getStatus(int id, Date fromDate, Date toDate, boolean isDeviceId) throws DeviceManagementDAOException { private List<DeviceStatus> getStatus(int id, Date fromDate, Date toDate, boolean isDeviceId, boolean billingStatus) throws DeviceManagementDAOException {
List<DeviceStatus> result = new ArrayList<>(); List<DeviceStatus> result = new ArrayList<>();
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -25,7 +25,14 @@ public class DeviceStatusDAOImpl implements DeviceStatusDAO {
conn = this.getConnection(); conn = this.getConnection();
// either we list all status values for the device using the device id or only get status values for the given enrolment id // either we list all status values for the device using the device id or only get status values for the given enrolment id
String idType = isDeviceId ? "DEVICE_ID" : "ENROLMENT_ID"; String idType = isDeviceId ? "DEVICE_ID" : "ENROLMENT_ID";
String sql = "SELECT ENROLMENT_ID, DEVICE_ID, UPDATE_TIME, STATUS, CHANGED_BY FROM DM_DEVICE_STATUS WHERE " + idType + " = ?"; String sql;
if (billingStatus) {
sql = "SELECT ENROLMENT_ID, DEVICE_ID, UPDATE_TIME, STATUS, CHANGED_BY FROM DM_DEVICE_STATUS WHERE STATUS IN ('ACTIVE','REMOVED') AND " + idType + " = ?";
} else {
sql = "SELECT ENROLMENT_ID, DEVICE_ID, UPDATE_TIME, STATUS, CHANGED_BY FROM DM_DEVICE_STATUS WHERE " + idType + " = ?";
}
// filter the data based on a date range if specified // filter the data based on a date range if specified
if (fromDate != null){ if (fromDate != null){
sql += " AND UPDATE_TIME >= ?"; sql += " AND UPDATE_TIME >= ?";
@ -64,17 +71,17 @@ public class DeviceStatusDAOImpl implements DeviceStatusDAO {
@Override @Override
public List<DeviceStatus> getStatus(int enrolmentId, Date fromDate, Date toDate) throws DeviceManagementDAOException { public List<DeviceStatus> getStatus(int enrolmentId, Date fromDate, Date toDate) throws DeviceManagementDAOException {
return getStatus(enrolmentId, fromDate, toDate, false); return getStatus(enrolmentId, fromDate, toDate, false, false);
} }
@Override @Override
public List<DeviceStatus> getStatus(int deviceId, int tenantId) throws DeviceManagementDAOException { public List<DeviceStatus> getStatus(int deviceId, int tenantId) throws DeviceManagementDAOException {
return getStatus(deviceId, tenantId, null, null); return getStatus(deviceId, tenantId, null, null, false);
} }
@Override @Override
public List<DeviceStatus> getStatus(int deviceId, int tenantId, Date fromDate, Date toDate) throws DeviceManagementDAOException { public List<DeviceStatus> getStatus(int deviceId, int tenantId, Date fromDate, Date toDate, boolean billingStatus) throws DeviceManagementDAOException {
return getStatus(deviceId, fromDate, toDate, true); return getStatus(deviceId, fromDate, toDate, true, billingStatus);
} }
@Override @Override

@ -144,6 +144,27 @@ public class EnrollmentDAOImpl implements EnrollmentDAO {
return status; return status;
} }
@Override
public boolean updateEnrollmentLastBilledDate(EnrolmentInfo enrolmentInfo, Timestamp lastBilledDate, int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = this.getConnection();
String sql = "UPDATE DM_ENROLMENT SET LAST_BILLED_DATE = ? WHERE ID = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setLong(1, lastBilledDate.getTime());
stmt.setInt(2, enrolmentInfo.getId());
stmt.setInt(3, tenantId);
stmt.executeUpdate();
return true;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while updating enrolment last billed date.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override @Override
public int removeEnrollment(int deviceId, String currentOwner, public int removeEnrollment(int deviceId, String currentOwner,

@ -80,6 +80,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
"e.IS_TRANSFERRED, " + "e.IS_TRANSFERRED, " +
"e.DATE_OF_LAST_UPDATE, " + "e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT, " + "e.DATE_OF_ENROLMENT, " +
"e.LAST_BILLED_DATE, " +
"e.ID AS ENROLMENT_ID " + "e.ID AS ENROLMENT_ID " +
"FROM DM_ENROLMENT e, " + "FROM DM_ENROLMENT e, " +
"(SELECT d.ID, " + "(SELECT d.ID, " +
@ -184,29 +185,27 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
} }
@Override @Override
public List<DeviceBilling> getDeviceBillList(PaginationRequest request, int tenantId) public List<DeviceBilling> getDeviceBillList(PaginationRequest request, int tenantId, Timestamp startDate, Timestamp endDate)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
List<DeviceBilling> devices = new ArrayList<>(); List<DeviceBilling> devices = new ArrayList<>();
try { try {
conn = this.getConnection(); conn = this.getConnection();
// String sql ="select DEVICE_IDENTIFICATION, DESCRIPTION, NAME AS DEVICE_NAME, DATE_OF_ENROLMENT, LAST_BILLED_DATE AS BILLED_DATE,STATUS,\n" + String sql = "SELECT DM_DEVICE.ID, DEVICE_IDENTIFICATION, DESCRIPTION, NAME AS DEVICE_NAME, DM_ENROLMENT.ID AS ENROLMENT_ID,\n" +
// "TIMESTAMPDIFF('DAY', CURDATE(), DATE_OF_ENROLMENT) as DAYS_SINCE_ENROLLED from DM_DEVICE d, DM_ENROLMENT e\n" + " DATE_OF_ENROLMENT,STATUS, LAST_BILLED_DATE, TIMESTAMPDIFF('DAY', DATE_OF_ENROLMENT, CURDATE()) as DAYS_SINCE_ENROLLED\n" +
// "where e.TENANT_ID= ? and d.ID=e.DEVICE_ID and STATUS !='REMOVED' LIMIT 10"; " FROM DM_DEVICE JOIN DM_ENROLMENT ON (DM_DEVICE.ID = DM_ENROLMENT.DEVICE_ID)\n" +
String sql ="select DEVICE_IDENTIFICATION, DESCRIPTION, NAME AS DEVICE_NAME, DATE_OF_ENROLMENT, LAST_BILLED_DATE,STATUS,\n" + " WHERE DM_ENROLMENT.TENANT_ID=?";
"TIMESTAMPDIFF('DAY', DATE_OF_ENROLMENT, CURDATE()) as DAYS_SINCE_ENROLLED from DM_DEVICE d, DM_ENROLMENT e\n" +
"where e.TENANT_ID= ? and d.ID=e.DEVICE_ID and STATUS !='REMOVED' LIMIT 10";
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, false); DeviceBilling device = DeviceManagementDAOUtil.loadDeviceBilling(rs);
devices.add(device); devices.add(device);
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices belongs to '" + throw new DeviceManagementDAOException("Error occurred while fetching the list of devices '" +
request.getOwner() + "'", e); request.getOwner() + "'", e);
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null); DeviceManagementDAOUtil.cleanupResources(stmt, null);
@ -230,7 +229,8 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
ResultSet rs = stmt.executeQuery(); ResultSet rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {
DeviceBilling device = DeviceManagementDAOUtil.loadDeviceBilling(rs, true); // DeviceBilling device = DeviceManagementDAOUtil.loadDeviceBilling(rs, true);
DeviceBilling device = DeviceManagementDAOUtil.loadDeviceBilling(rs);
devices.add(device); devices.add(device);
} }
} catch (SQLException e) { } catch (SQLException e) {

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

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

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

@ -21,7 +21,9 @@ import java.sql.*;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.util.*;
import java.util.Date; import java.util.Date;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.CarbonContext;
@ -32,6 +34,7 @@ import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistorySnapshot; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistorySnapshot;
import org.wso2.carbon.device.mgt.common.device.details.DeviceMonitoringData; import org.wso2.carbon.device.mgt.common.device.details.DeviceMonitoringData;
import org.wso2.carbon.device.mgt.common.type.mgt.DeviceStatus;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
@ -41,9 +44,6 @@ import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import javax.naming.InitialContext; import javax.naming.InitialContext;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
public final class DeviceManagementDAOUtil { public final class DeviceManagementDAOUtil {
@ -150,16 +150,17 @@ public final class DeviceManagementDAOUtil {
enrolmentInfo.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime()); enrolmentInfo.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime());
enrolmentInfo.setDateOfLastUpdate(rs.getTimestamp("DATE_OF_LAST_UPDATE").getTime()); enrolmentInfo.setDateOfLastUpdate(rs.getTimestamp("DATE_OF_LAST_UPDATE").getTime());
enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(rs.getString("STATUS"))); enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(rs.getString("STATUS")));
enrolmentInfo.setLastBilledDate(rs.getLong("LAST_BILLED_DATE"));
return enrolmentInfo; return enrolmentInfo;
} }
public static EnrolmentInfo loadEnrolmentBilling(ResultSet rs, Boolean removedDevices) throws SQLException { public static EnrolmentInfo loadEnrolmentBilling(ResultSet rs) throws SQLException {
System.out.println("-----------------DAOO 222------------------------------");
EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
enrolmentInfo.setId(rs.getInt("ENROLMENT_ID"));
enrolmentInfo.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime()); enrolmentInfo.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime());
enrolmentInfo.setLastBilledDate(rs.getLong("LAST_BILLED_DATE")); enrolmentInfo.setLastBilledDate(rs.getLong("LAST_BILLED_DATE"));
enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(rs.getString("STATUS"))); enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(rs.getString("STATUS")));
if (removedDevices) { if (EnrolmentInfo.Status.valueOf(rs.getString("STATUS")).equals("REMOVED")) {
enrolmentInfo.setDateOfLastUpdate(rs.getTimestamp("DATE_OF_LAST_UPDATE").getTime()); enrolmentInfo.setDateOfLastUpdate(rs.getTimestamp("DATE_OF_LAST_UPDATE").getTime());
} }
return enrolmentInfo; return enrolmentInfo;
@ -210,18 +211,19 @@ public final class DeviceManagementDAOUtil {
return device; return device;
} }
public static DeviceBilling loadDeviceBilling(ResultSet rs, Boolean removedDevices) throws SQLException { public static DeviceBilling loadDeviceBilling(ResultSet rs) throws SQLException {
System.out.println("-----------------DAOO 111------------------------------");
DeviceBilling device = new DeviceBilling(); DeviceBilling device = new DeviceBilling();
device.setId(rs.getInt("ID"));
device.setName(rs.getString("DEVICE_NAME")); device.setName(rs.getString("DEVICE_NAME"));
device.setDescription(rs.getString("DESCRIPTION")); device.setDescription(rs.getString("DESCRIPTION"));
device.setDeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION")); device.setDeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION"));
if (removedDevices) { device.setDaysUsed((int) rs.getLong("DAYS_SINCE_ENROLLED"));
device.setDaysUsed((int) rs.getLong("DAYS_USED")); // if (removedDevices) {
} else { // device.setDaysUsed((int) rs.getLong("DAYS_USED"));
device.setDaysSinceEnrolled((int) rs.getLong("DAYS_SINCE_ENROLLED")); // } else {
} // device.setDaysSinceEnrolled((int) rs.getLong("DAYS_SINCE_ENROLLED"));
device.setEnrolmentInfo(loadEnrolmentBilling(rs, removedDevices)); // }
device.setEnrolmentInfo(loadEnrolmentBilling(rs));
return device; return device;
} }

@ -42,7 +42,6 @@ import org.wso2.carbon.device.mgt.common.configuration.mgt.AmbiguousConfiguratio
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException; import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException;
import org.wso2.carbon.device.mgt.common.configuration.mgt.DeviceConfiguration; import org.wso2.carbon.device.mgt.common.configuration.mgt.DeviceConfiguration;
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration;
import org.wso2.carbon.device.mgt.common.cost.mgt.Costdata;
import org.wso2.carbon.device.mgt.common.device.details.DeviceData; import org.wso2.carbon.device.mgt.common.device.details.DeviceData;
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry;
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistorySnapshot; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistorySnapshot;
@ -70,6 +69,7 @@ import org.wso2.carbon.device.mgt.common.geo.service.GeoCluster;
import org.wso2.carbon.device.mgt.common.geo.service.GeoCoordinate; import org.wso2.carbon.device.mgt.common.geo.service.GeoCoordinate;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -203,22 +203,11 @@ public interface DeviceManagementProviderService {
* Method to retrieve all the devices with pagination support. * Method to retrieve all the devices with pagination support.
* *
* @param request PaginationRequest object holding the data for pagination * @param request PaginationRequest object holding the data for pagination
* @return List<DeviceBilling> - Result includes the cost of removed device list.
* @throws DeviceManagementException If some unusual behaviour is observed while fetching the
* devices.
*/
List<DeviceBilling> getRemovedDeviceListWithCost(PaginationResult paginationResult, PaginationRequest request, Collection<Costdata> costdata, String tenantDomain, Double totalCost) throws DeviceManagementException;
/**
* Method to retrieve all the devices with pagination support.
*
* @param request PaginationRequest object holding the data for pagination
* @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required
* along with the device data.
* @return PaginationResult - Result including the required parameters necessary to do pagination. * @return PaginationResult - Result including the required parameters necessary to do pagination.
* @throws DeviceManagementException If some unusual behaviour is observed while fetching the * @throws DeviceManagementException If some unusual behaviour is observed while fetching billing of
* devices. * devices.
*/ */
PaginationResult getAllDevicesBillings(PaginationRequest request, boolean requireDeviceInfo, String tenantDomain) throws DeviceManagementException; PaginationResult getAllDevicesBillings(PaginationRequest request, String tenantDomain, Timestamp startDate, Timestamp endDate, boolean generateBill) throws DeviceManagementException;
/** /**
* Returns the device of specified id. * Returns the device of specified id.
@ -705,7 +694,7 @@ public interface DeviceManagementProviderService {
List<DeviceStatus> getDeviceStatusHistory(Device device) throws DeviceManagementException; List<DeviceStatus> getDeviceStatusHistory(Device device) throws DeviceManagementException;
List<DeviceStatus> getDeviceStatusHistory(Device device, Date fromDate, Date toDate) throws DeviceManagementException; List<DeviceStatus> getDeviceStatusHistory(Device device, Date fromDate, Date toDate, boolean billingStatus) throws DeviceManagementException;
List<DeviceStatus> getDeviceCurrentEnrolmentStatusHistory(Device device) throws DeviceManagementException; List<DeviceStatus> getDeviceCurrentEnrolmentStatusHistory(Device device) throws DeviceManagementException;

@ -61,7 +61,7 @@ import org.wso2.carbon.device.mgt.common.configuration.mgt.DeviceConfiguration;
import org.wso2.carbon.device.mgt.common.configuration.mgt.DevicePropertyInfo; import org.wso2.carbon.device.mgt.common.configuration.mgt.DevicePropertyInfo;
import org.wso2.carbon.device.mgt.common.configuration.mgt.EnrollmentConfiguration; import org.wso2.carbon.device.mgt.common.configuration.mgt.EnrollmentConfiguration;
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration;
import org.wso2.carbon.device.mgt.common.cost.mgt.Costdata; import org.wso2.carbon.device.mgt.common.cost.mgt.Cost;
import org.wso2.carbon.device.mgt.common.device.details.DeviceData; import org.wso2.carbon.device.mgt.common.device.details.DeviceData;
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation;
@ -137,6 +137,7 @@ import java.io.IOException;
import java.io.StringWriter; 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.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -936,112 +937,156 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
@Override @Override
public List<DeviceBilling> getRemovedDeviceListWithCost(PaginationResult paginationResult, PaginationRequest request, Collection<Costdata> costdata, String tenantDomain, Double totalCost) throws DeviceManagementException { public PaginationResult getAllDevicesBillings(PaginationRequest request, String tenantDomain, Timestamp startDate, Timestamp endDate, boolean generateBill) throws DeviceManagementException {
List<DeviceBilling> allRemovedDevices;
int tenantId = this.getTenantId();
try {
allRemovedDevices = deviceDAO.getRemovedDeviceBillList(request,tenantId);
for (Costdata test: costdata) {
if (test.getTenantDomain().equals(tenantDomain)) {
for (DeviceBilling device: allRemovedDevices) {
long dateDiff = device.getEnrolmentInfo().getDateOfLastUpdate()-device.getEnrolmentInfo().getDateOfEnrolment();
long dateInDays = dateDiff / (1000*60*60*24);
double cost = (test.getCost()/365)*dateInDays;
totalCost = cost + totalCost;
device.setCost(cost);
}
}
}
paginationResult.setTotalCost(totalCost);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while retrieving device list pertaining to the current tenant";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
catch (Exception e) {
String msg = "Error occurred get devices";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
return allRemovedDevices;
}
@Override
public PaginationResult getAllDevicesBillings(PaginationRequest request, boolean requireDeviceInfo, String tenantDomain) throws DeviceManagementException {
if (request == null) { if (request == null) {
String msg = "Received incomplete pagination request for method getAllDeviceBillings"; String msg = "Received incomplete pagination request for method getAllDeviceBillings";
log.error(msg); log.error(msg);
throw new DeviceManagementException(msg); throw new DeviceManagementException(msg);
} }
if (log.isDebugEnabled()) {
log.debug("Get devices with pagination " + request.toString() + " and requiredDeviceInfo: " + requireDeviceInfo);
}
System.out.println("--------------------COREEEE LAYERR-------------------");
PaginationResult paginationResult = new PaginationResult(); PaginationResult paginationResult = new PaginationResult();
Double totalCost = 0.0; Double totalCost = 0.0;
List<DeviceBilling> allDevices; // List<DeviceBilling> allDevices;
List<DeviceBilling> allRemovedDevices; List<Device> allDevices;
int count = 0; int count = 0;
int tenantId = this.getTenantId(); int tenantId = this.getTenantId();
DeviceManagerUtil.validateDeviceListPageSize(request);
try { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
allDevices = deviceDAO.getDeviceBillList(request, tenantId); // allDevices = deviceDAO.getDeviceBillList(request, tenantId, startDate, endDate);
allRemovedDevices = deviceDAO.getRemovedDeviceBillList(request,tenantId); allDevices = deviceDAO.getDevices(request, tenantId);
count = deviceDAO.getDeviceCount(request, tenantId); count = deviceDAO.getDeviceCount(request, tenantId);
System.out.println("-----------------HERE------------------------------");
String metaKey = "PER_DEVICE_COST"; String metaKey = "PER_DEVICE_COST";
MetadataManagementDAOFactory.openConnection(); MetadataManagementDAOFactory.openConnection();
Metadata metadata = metadataDAO.getMetadata(tenantId, metaKey); Metadata metadata = metadataDAO.getMetadata(tenantId, metaKey);
Gson g = new Gson(); Gson g = new Gson();
Type collectionType = new TypeToken<Collection<Costdata>>(){}.getType(); Type collectionType = new TypeToken<Collection<Cost>>(){}.getType();
Collection<Costdata> costdata = g.fromJson(metadata.getMetaValue(), collectionType); // change name Collection<Cost> costData = g.fromJson(metadata.getMetaValue(), collectionType);
for (Costdata test: costdata) { for (Cost tenantCost: costData) {
if (test.getTenantDomain().equals(tenantDomain)) { if (tenantCost.getTenantDomain().equals(tenantDomain)) {
for (DeviceBilling device: allDevices) { for (Device device: allDevices) {
long dateDiff; device.setDeviceStatusInfo(getDeviceStatusHistory(device, startDate, endDate, true));
if (device.getEnrolmentInfo().getLastBilledDate() == 0) { long dateDiff = 0;
dateDiff = test.getSubscriptionEnd()-device.getEnrolmentInfo().getDateOfEnrolment();
} else { List<DeviceStatus> deviceStatus = device.getDeviceStatusInfo();
dateDiff = test.getSubscriptionEnd()-device.getEnrolmentInfo().getLastBilledDate(); boolean lastBilledDate = false;
for (int i=0; i<deviceStatus.size(); i++) {
if(deviceStatus.get(i).getStatus().toString().equals("ACTIVE")) {
if (deviceStatus.size()> i+1) {
if (lastBilledDate == false) {
lastBilledDate = true;
if (device.getEnrolmentInfo().getLastBilledDate() == 0) {
dateDiff = dateDiff + (deviceStatus.get(i+1).getUpdateTime().getTime() - deviceStatus.get(i).getUpdateTime().getTime());
} else {
if (deviceStatus.get(i+1).getUpdateTime().getTime() >= device.getEnrolmentInfo().getLastBilledDate()) {
dateDiff = dateDiff + (deviceStatus.get(i+1).getUpdateTime().getTime() - device.getEnrolmentInfo().getLastBilledDate());
}
}
} else {
if ( deviceStatus.get(i).getUpdateTime().getTime() >= device.getEnrolmentInfo().getLastBilledDate()) {
dateDiff = dateDiff + (deviceStatus.get(i+1).getUpdateTime().getTime() - deviceStatus.get(i).getUpdateTime().getTime());
}
}
} else {
if (lastBilledDate == false) {
lastBilledDate = true;
if (device.getEnrolmentInfo().getLastBilledDate() == 0) {
dateDiff = dateDiff + (endDate.getTime() - deviceStatus.get(i).getUpdateTime().getTime());
} else {
if (endDate.getTime() >= device.getEnrolmentInfo().getLastBilledDate()) {
dateDiff = dateDiff +(endDate.getTime() - device.getEnrolmentInfo().getLastBilledDate());
}
}
} else {
dateDiff = dateDiff + (endDate.getTime() - deviceStatus.get(i).getUpdateTime().getTime());
}
}
}
} }
// dateDiff = test.getSubscriptionEnd().getTime()-device.getEnrolmentInfo().getDateOfEnrolment();
long dateInDays = dateDiff / (1000*60*60*24); long dateInDays = dateDiff / (1000*60*60*24);
double cost = (test.getCost()/365)*dateInDays; double cost = (tenantCost.getCost()/365)*dateInDays;
totalCost = cost + totalCost; totalCost = cost + totalCost;
device.setCost(cost); device.setCost(cost);
} device.setDaysUsed((int) dateInDays);
}
}
for (Costdata test: costdata) { if (generateBill) {
if (test.getTenantDomain().equals(tenantDomain)) { Timestamp timestamp = new Timestamp(System.currentTimeMillis());
for (DeviceBilling device: allRemovedDevices) { enrollmentDAO.updateEnrollmentLastBilledDate(device.getEnrolmentInfo(), timestamp, tenantId);
long dateDiff;
// long dateDiff = device.getEnrolmentInfo().getDateOfLastUpdate()-device.getEnrolmentInfo().getDateOfEnrolment();
if (device.getEnrolmentInfo().getLastBilledDate() == 0) {
dateDiff = test.getSubscriptionEnd()-device.getEnrolmentInfo().getDateOfEnrolment();
} else {
dateDiff = test.getSubscriptionEnd()-device.getEnrolmentInfo().getLastBilledDate();
} }
long dateInDays = dateDiff / (1000*60*60*24);
double cost = (test.getCost()/365)*dateInDays;
totalCost = cost + totalCost;
device.setCost(cost);
} }
// for (DeviceBilling device: allDevices) {
// device.setDeviceStatusInfo(getDeviceStatusHistory(device, startDate, endDate, true));
// long dateDiff = 0;
//
// List<DeviceStatus> deviceStatus = device.getDeviceStatusInfo();
// boolean lastBilledDate = false;
//// int startIndex = deviceStatus.indexOf("ACTIVE");
// for (int i=0; i<deviceStatus.size(); i++) {
// if(deviceStatus.get(i).getStatus().toString().equals("ACTIVE")) {
// if (deviceStatus.size()> i+1) {
// if (lastBilledDate == false) {
// lastBilledDate = true;
// if (device.getEnrolmentInfo().getLastBilledDate() == 0) {
// dateDiff = dateDiff + (deviceStatus.get(i+1).getUpdateTime().getTime() - deviceStatus.get(i).getUpdateTime().getTime());
// } else {
// if (deviceStatus.get(i+1).getUpdateTime().getTime() >= device.getEnrolmentInfo().getLastBilledDate()) {
// dateDiff = dateDiff + (deviceStatus.get(i+1).getUpdateTime().getTime() - device.getEnrolmentInfo().getLastBilledDate());
// }
//// dateDiff = dateDiff + (deviceStatus.get(i+1).getUpdateTime().getTime() - device.getEnrolmentInfo().getLastBilledDate());
// }
// } else {
// if ( deviceStatus.get(i).getUpdateTime().getTime() >= device.getEnrolmentInfo().getLastBilledDate()) {
// dateDiff = dateDiff + (deviceStatus.get(i+1).getUpdateTime().getTime() - deviceStatus.get(i).getUpdateTime().getTime());
// }
//// dateDiff = dateDiff + (deviceStatus.get(i+1).getUpdateTime().getTime() - deviceStatus.get(i).getUpdateTime().getTime());
// }
//// dateDiff = deviceStatus.get(i+1).getUpdateTime().getTime() - deviceStatus.get(i).getUpdateTime().getTime();
// } else {
// if (lastBilledDate == false) {
// lastBilledDate = true;
// if (device.getEnrolmentInfo().getLastBilledDate() == 0) {
// dateDiff = dateDiff + (endDate.getTime() - deviceStatus.get(i).getUpdateTime().getTime());
// } else {
// if (endDate.getTime() >= device.getEnrolmentInfo().getLastBilledDate()) {
// dateDiff = dateDiff +(endDate.getTime() - device.getEnrolmentInfo().getLastBilledDate());
// }
//// dateDiff = dateDiff +(endDate.getTime() - device.getEnrolmentInfo().getLastBilledDate());
// }
// } else {
// dateDiff = dateDiff + (endDate.getTime() - deviceStatus.get(i).getUpdateTime().getTime());
// }
//// dateDiff = endDate.getTime() - deviceStatus.get(i).getUpdateTime().getTime();
// }
// }
// }
//
// long dateInDays = dateDiff / (1000*60*60*24);
// double cost = (test.getCost()/365)*dateInDays;
// totalCost = cost + totalCost;
// device.setCost(cost);
// device.setDaysUsed((int) dateInDays);
//
// if (generateBill) {
// Timestamp timestamp = new Timestamp(System.currentTimeMillis());
// enrollmentDAO.updateEnrollmentLastBilledDate(device.getEnrolmentInfo(), timestamp, tenantId);
// }
//
//// if (cost == 0) {
//// allDevices.remove(device);
//// }
//
// }
} }
} }
// allRemovedDevices = this.getRemovedDeviceListWithCost(paginationResult, request, costdata, tenantDomain, totalCost);
allDevices.addAll(allRemovedDevices);
} 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);
@ -1881,13 +1926,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
} }
@Override @Override
public List<DeviceStatus> getDeviceStatusHistory(Device device, Date fromDate, Date toDate) 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());
} }
try { try {
int tenantId = this.getTenantId(); int tenantId = this.getTenantId();
return deviceStatusDAO.getStatus(device.getId(), tenantId, fromDate, toDate); return deviceStatusDAO.getStatus(device.getId(), tenantId, fromDate, toDate, billingStatus);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction(); DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while retrieving status history"; String msg = "Error occurred while retrieving status history";
@ -1931,7 +1976,7 @@ 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); return getDeviceStatusHistory(device, null, null, false);
} }
@Override @Override

Loading…
Cancel
Save