mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
Merge pull request 'Add device deletion scenario for billing' (#226) from osh.silva/device-mgt-core:billing-delete-9713 into master
Reviewed-on: https://repository.entgra.net/community/device-mgt-core/pulls/226
This commit is contained in:
commit
50887c12f9
@ -32,7 +32,7 @@ public class EnrolmentInfo implements Serializable {
|
|||||||
public enum Status {
|
public enum Status {
|
||||||
CREATED, ACTIVE, INACTIVE, UNREACHABLE, UNCLAIMED, SUSPENDED, BLOCKED, REMOVED, DISENROLLMENT_REQUESTED,
|
CREATED, ACTIVE, INACTIVE, UNREACHABLE, UNCLAIMED, SUSPENDED, BLOCKED, REMOVED, DISENROLLMENT_REQUESTED,
|
||||||
CONFIGURED, READY_TO_CONNECT, RETURN_PENDING, RETURNED, DEFECTIVE, WARRANTY_PENDING, WARRANTY_SENT,
|
CONFIGURED, READY_TO_CONNECT, RETURN_PENDING, RETURNED, DEFECTIVE, WARRANTY_PENDING, WARRANTY_SENT,
|
||||||
WARRANTY_REPLACED, ASSIGNED
|
WARRANTY_REPLACED, ASSIGNED, DELETED
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum OwnerShip {
|
public enum OwnerShip {
|
||||||
|
|||||||
@ -634,7 +634,7 @@ public interface DeviceDAO {
|
|||||||
* @param enrollmentIds list of enrollment ids.
|
* @param enrollmentIds list of enrollment ids.
|
||||||
* @throws DeviceManagementDAOException when no enrolments are found for the given device.
|
* @throws DeviceManagementDAOException when no enrolments are found for the given device.
|
||||||
*/
|
*/
|
||||||
void deleteDevices(List<String> deviceIdentifiers, List<Integer> deviceIds, List<Integer> enrollmentIds) throws DeviceManagementDAOException;
|
void deleteDevices(List<String> deviceIdentifiers, List<Integer> deviceIds, List<Integer> enrollmentIds, List<Device> validDevices) throws DeviceManagementDAOException;
|
||||||
|
|
||||||
boolean transferDevice(String deviceType, String deviceId, String owner, int destinationTenantId)
|
boolean transferDevice(String deviceType, String deviceId, String owner, int destinationTenantId)
|
||||||
throws DeviceManagementDAOException, SQLException;
|
throws DeviceManagementDAOException, SQLException;
|
||||||
|
|||||||
@ -52,6 +52,7 @@ import java.util.Date;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.StringJoiner;
|
import java.util.StringJoiner;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
||||||
|
|
||||||
@ -2155,7 +2156,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deleteDevices(List<String> deviceIdentifiers, List<Integer> deviceIds, List<Integer> enrollmentIds)
|
public void deleteDevices(List<String> deviceIdentifiers, List<Integer> deviceIds, List<Integer> enrollmentIds, List<Device> validDevices)
|
||||||
throws DeviceManagementDAOException {
|
throws DeviceManagementDAOException {
|
||||||
Connection conn;
|
Connection conn;
|
||||||
try {
|
try {
|
||||||
@ -2210,7 +2211,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|||||||
"operation response, enrollment operation mapping data of " +
|
"operation response, enrollment operation mapping data of " +
|
||||||
"devices with identifiers: " + deviceIdentifiers);
|
"devices with identifiers: " + deviceIdentifiers);
|
||||||
}
|
}
|
||||||
removeDeviceEnrollment(conn, deviceIds);
|
refactorEnrolment(conn, deviceIds);
|
||||||
|
refactorDeviceStatus(conn, validDevices);
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.debug("Successfully removed device enrollment data of devices: " + deviceIdentifiers);
|
log.debug("Successfully removed device enrollment data of devices: " + deviceIdentifiers);
|
||||||
}
|
}
|
||||||
@ -2218,7 +2220,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.debug("Successfully removed device group mapping data of devices: " + deviceIdentifiers);
|
log.debug("Successfully removed device group mapping data of devices: " + deviceIdentifiers);
|
||||||
}
|
}
|
||||||
removeDevice(conn, deviceIds);
|
refactorDevice(conn, deviceIds);
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.debug("Successfully permanently deleted the device of devices: " + deviceIdentifiers);
|
log.debug("Successfully permanently deleted the device of devices: " + deviceIdentifiers);
|
||||||
}
|
}
|
||||||
@ -2749,30 +2751,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/***
|
|
||||||
* This method removes records of a given list of enrollments from the DM_ENROLMENT table
|
|
||||||
* @param conn Connection object
|
|
||||||
* @param enrollmentIds list of enrollment ids (primary keys)
|
|
||||||
* @throws DeviceManagementDAOException if deletion fails
|
|
||||||
*/
|
|
||||||
private void removeDeviceEnrollment(Connection conn, List<Integer> enrollmentIds)
|
|
||||||
throws DeviceManagementDAOException {
|
|
||||||
String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ?";
|
|
||||||
try {
|
|
||||||
if (!executeBatchOperation(conn, sql, enrollmentIds)) {
|
|
||||||
String msg = "Failed to remove enrollments of devices with enrollmentIds : " + enrollmentIds
|
|
||||||
+ " while executing batch operation";
|
|
||||||
log.error(msg);
|
|
||||||
throw new DeviceManagementDAOException(msg);
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
String msg = "SQL error occurred while removing enrollments of devices with enrollmentIds : "
|
|
||||||
+ enrollmentIds;
|
|
||||||
log.error(msg, e);
|
|
||||||
throw new DeviceManagementDAOException(msg, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* This method removes records of a given list of devices from the DM_DEVICE_GROUP_MAP table
|
* This method removes records of a given list of devices from the DM_DEVICE_GROUP_MAP table
|
||||||
* @param conn Connection object
|
* @param conn Connection object
|
||||||
@ -2797,26 +2775,115 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* This method removes records of a given list of devices from the DM_DEVICE table
|
* This method refactors some attributes of a given list of devices which are being deleted by the user
|
||||||
* @param conn Connection object
|
* @param conn Connection object
|
||||||
* @param deviceIds list of device ids (primary keys)
|
* @param deviceIds list of device ids (primary keys)
|
||||||
* @throws DeviceManagementDAOException if deletion fails
|
* @throws DeviceManagementDAOException if refactoring fails
|
||||||
*/
|
*/
|
||||||
private void removeDevice(Connection conn, List<Integer> deviceIds) throws DeviceManagementDAOException {
|
public void refactorDevice(Connection conn, List<Integer> deviceIds) throws DeviceManagementDAOException {
|
||||||
String sql = "DELETE FROM DM_DEVICE WHERE ID = ?";
|
String updateQuery = "UPDATE DM_DEVICE SET DEVICE_IDENTIFICATION = ?, NAME = ? WHERE ID = ?";
|
||||||
try {
|
|
||||||
if (!executeBatchOperation(conn, sql, deviceIds)) {
|
try (PreparedStatement preparedStatement = conn.prepareStatement(updateQuery)) {
|
||||||
String msg = "Failed to remove devices with deviceIds : " + deviceIds + " while executing batch operation";
|
for (int deviceId : deviceIds) {
|
||||||
log.error(msg);
|
|
||||||
throw new DeviceManagementDAOException(msg);
|
String randomIdentification = generateRandomString(10);
|
||||||
|
String randomName = generateRandomString(20);
|
||||||
|
|
||||||
|
preparedStatement.setString(1, randomIdentification);
|
||||||
|
preparedStatement.setString(2, randomName);
|
||||||
|
preparedStatement.setInt(3, deviceId);
|
||||||
|
|
||||||
|
preparedStatement.executeUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
String msg = "SQL error occurred while removing devices with deviceIds : " + deviceIds;
|
String msg = "SQL error occurred while refactoring device properties of deviceIds: " + deviceIds;
|
||||||
log.error(msg, e);
|
log.error(msg, e);
|
||||||
throw new DeviceManagementDAOException(msg, e);
|
throw new DeviceManagementDAOException(msg, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/***
|
||||||
|
* This method refactors some attributes of a given list of devices in the DM_ENROLMENT table
|
||||||
|
* @param conn Connection object
|
||||||
|
* @param deviceIds list of device ids (primary keys)
|
||||||
|
* @throws DeviceManagementDAOException if refactoring fails
|
||||||
|
*/
|
||||||
|
public void refactorEnrolment(Connection conn, List<Integer> deviceIds) throws DeviceManagementDAOException {
|
||||||
|
String updateQuery = "UPDATE DM_ENROLMENT SET OWNER = ?, OWNERSHIP = ?, STATUS = ? WHERE DEVICE_ID = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement preparedStatement = conn.prepareStatement(updateQuery)) {
|
||||||
|
for (int deviceId : deviceIds) {
|
||||||
|
|
||||||
|
String randomOwner = generateRandomString(4);
|
||||||
|
String randomOwnership = generateRandomString(6);
|
||||||
|
|
||||||
|
preparedStatement.setString(1, randomOwner);
|
||||||
|
preparedStatement.setString(2, randomOwnership);
|
||||||
|
preparedStatement.setString(3, String.valueOf(Status.DELETED));
|
||||||
|
preparedStatement.setInt(4, deviceId);
|
||||||
|
|
||||||
|
preparedStatement.executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "SQL error occurred while refactoring device enrolment properties of deviceIds: " + deviceIds;
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementDAOException(msg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* This method updates the status of a given list of devices to DELETED state in the DM_DEVICE_STATUS table
|
||||||
|
* @param conn Connection object
|
||||||
|
* @param validDevices list of devices
|
||||||
|
* @throws DeviceManagementDAOException if updating fails
|
||||||
|
*/
|
||||||
|
public void refactorDeviceStatus(Connection conn, List<Device> validDevices) throws DeviceManagementDAOException {
|
||||||
|
String updateQuery = "UPDATE DM_DEVICE_STATUS SET STATUS = ? WHERE ID = ?";
|
||||||
|
String selectLastMatchingRecordQuery = "SELECT ID FROM DM_DEVICE_STATUS WHERE ENROLMENT_ID = ? AND DEVICE_ID = ? ORDER BY ID DESC LIMIT 1";
|
||||||
|
|
||||||
|
try (PreparedStatement selectStatement = conn.prepareStatement(selectLastMatchingRecordQuery);
|
||||||
|
PreparedStatement updateStatement = conn.prepareStatement(updateQuery)) {
|
||||||
|
|
||||||
|
for (Device device : validDevices) {
|
||||||
|
|
||||||
|
selectStatement.setInt(1, device.getEnrolmentInfo().getId());
|
||||||
|
selectStatement.setInt(2, device.getId());
|
||||||
|
|
||||||
|
ResultSet resultSet = selectStatement.executeQuery();
|
||||||
|
int lastRecordId = 0;
|
||||||
|
if (resultSet.next()) {
|
||||||
|
lastRecordId = resultSet.getInt("ID");
|
||||||
|
}
|
||||||
|
|
||||||
|
updateStatement.setString(1, String.valueOf(Status.DELETED));
|
||||||
|
updateStatement.setInt(2, lastRecordId);
|
||||||
|
updateStatement.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "SQL error occurred while updating device status properties.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementDAOException(msg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String generateRandomString(int length) {
|
||||||
|
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||||
|
StringBuilder randomString = new StringBuilder();
|
||||||
|
|
||||||
|
Random random = new Random();
|
||||||
|
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
int index = random.nextInt(characters.length());
|
||||||
|
randomString.append(characters.charAt(index));
|
||||||
|
}
|
||||||
|
|
||||||
|
return randomString.toString();
|
||||||
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* This method executes batch operations for a given list of primary keys
|
* This method executes batch operations for a given list of primary keys
|
||||||
* where the statement only has one param of type int, following the given pattern:
|
* where the statement only has one param of type int, following the given pattern:
|
||||||
|
|||||||
@ -54,7 +54,7 @@ public class DeviceStatusDAOImpl implements DeviceStatusDAO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (billingStatus) {
|
if (billingStatus) {
|
||||||
sql += " ORDER BY UPDATE_TIME DESC";
|
sql += " ORDER BY ID DESC";
|
||||||
}
|
}
|
||||||
|
|
||||||
stmt = conn.prepareStatement(sql);
|
stmt = conn.prepareStatement(sql);
|
||||||
|
|||||||
@ -237,7 +237,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
|
|||||||
"WHERE " +
|
"WHERE " +
|
||||||
"e.TENANT_ID=? AND " +
|
"e.TENANT_ID=? AND " +
|
||||||
"d.ID=e.DEVICE_ID AND " +
|
"d.ID=e.DEVICE_ID AND " +
|
||||||
"STATUS !='REMOVED' AND " +
|
"STATUS NOT IN ('REMOVED', 'DELETED') AND " +
|
||||||
"(" +
|
"(" +
|
||||||
"DATE_OF_ENROLMENT BETWEEN ? AND ? " +
|
"DATE_OF_ENROLMENT BETWEEN ? AND ? " +
|
||||||
")";
|
")";
|
||||||
@ -276,7 +276,9 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
|
|||||||
"from DM_DEVICE d, DM_ENROLMENT e " +
|
"from DM_DEVICE d, DM_ENROLMENT e " +
|
||||||
"where " +
|
"where " +
|
||||||
"e.TENANT_ID=? and d.ID=e.DEVICE_ID and " +
|
"e.TENANT_ID=? and d.ID=e.DEVICE_ID and " +
|
||||||
"STATUS ='REMOVED' and " +
|
"(" +
|
||||||
|
"STATUS = 'REMOVED' OR STATUS = 'DELETED' " +
|
||||||
|
") and " +
|
||||||
"(" +
|
"(" +
|
||||||
"DATE_OF_ENROLMENT between ? and ? " +
|
"DATE_OF_ENROLMENT between ? and ? " +
|
||||||
") and " +
|
") and " +
|
||||||
@ -319,7 +321,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
|
|||||||
"where " +
|
"where " +
|
||||||
"e.TENANT_ID=? and " +
|
"e.TENANT_ID=? and " +
|
||||||
"d.ID=e.DEVICE_ID and " +
|
"d.ID=e.DEVICE_ID and " +
|
||||||
"STATUS !='REMOVED' and " +
|
"STATUS NOT IN ('REMOVED', 'DELETED') and " +
|
||||||
"(" +
|
"(" +
|
||||||
"DATE_OF_ENROLMENT < ? " +
|
"DATE_OF_ENROLMENT < ? " +
|
||||||
")";
|
")";
|
||||||
@ -358,7 +360,9 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
|
|||||||
"from DM_DEVICE d, DM_ENROLMENT e " +
|
"from DM_DEVICE d, DM_ENROLMENT e " +
|
||||||
"where " +
|
"where " +
|
||||||
"e.TENANT_ID=? and d.ID=e.DEVICE_ID and " +
|
"e.TENANT_ID=? and d.ID=e.DEVICE_ID and " +
|
||||||
"STATUS ='REMOVED' and " +
|
"(" +
|
||||||
|
"STATUS = 'REMOVED' OR STATUS = 'DELETED' " +
|
||||||
|
") and " +
|
||||||
"(" +
|
"(" +
|
||||||
"DATE_OF_ENROLMENT < ? " +
|
"DATE_OF_ENROLMENT < ? " +
|
||||||
") and " +
|
") and " +
|
||||||
|
|||||||
@ -652,6 +652,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|||||||
Map<String, DeviceManager> deviceManagerMap = new HashMap<>();
|
Map<String, DeviceManager> deviceManagerMap = new HashMap<>();
|
||||||
List<DeviceCacheKey> deviceCacheKeyList = new ArrayList<>();
|
List<DeviceCacheKey> deviceCacheKeyList = new ArrayList<>();
|
||||||
List<Device> existingDevices;
|
List<Device> existingDevices;
|
||||||
|
List<Device> validDevices = new ArrayList<>();;
|
||||||
int tenantId = this.getTenantId();
|
int tenantId = this.getTenantId();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -684,6 +685,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|||||||
deviceCacheKey.setDeviceType(device.getType());
|
deviceCacheKey.setDeviceType(device.getType());
|
||||||
deviceCacheKey.setTenantId(tenantId);
|
deviceCacheKey.setTenantId(tenantId);
|
||||||
deviceCacheKeyList.add(deviceCacheKey);
|
deviceCacheKeyList.add(deviceCacheKey);
|
||||||
|
validDevices.add(device);
|
||||||
deviceIds.add(device.getId());
|
deviceIds.add(device.getId());
|
||||||
validDeviceIdentifiers.add(device.getDeviceIdentifier());
|
validDeviceIdentifiers.add(device.getDeviceIdentifier());
|
||||||
enrollmentIds.add(device.getEnrolmentInfo().getId());
|
enrollmentIds.add(device.getEnrolmentInfo().getId());
|
||||||
@ -713,7 +715,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|||||||
try {
|
try {
|
||||||
DeviceManagementDAOFactory.beginTransaction();
|
DeviceManagementDAOFactory.beginTransaction();
|
||||||
//deleting device from the core
|
//deleting device from the core
|
||||||
deviceDAO.deleteDevices(validDeviceIdentifiers, new ArrayList<>(deviceIds), enrollmentIds);
|
deviceDAO.deleteDevices(validDeviceIdentifiers, new ArrayList<>(deviceIds), enrollmentIds, validDevices);
|
||||||
for (Map.Entry<String, DeviceManager> entry : deviceManagerMap.entrySet()) {
|
for (Map.Entry<String, DeviceManager> entry : deviceManagerMap.entrySet()) {
|
||||||
try {
|
try {
|
||||||
// deleting device from the plugin level
|
// deleting device from the plugin level
|
||||||
@ -1032,7 +1034,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|||||||
* @return Whether status is changed or not
|
* @return Whether status is changed or not
|
||||||
* @throws DeviceManagementException on errors while trying to calculate Cost
|
* @throws DeviceManagementException on errors while trying to calculate Cost
|
||||||
*/
|
*/
|
||||||
public BillingResponse calculateCost(String tenantDomain, Timestamp startDate, Timestamp endDate, List<Device> allDevices) throws MetadataManagementDAOException, DeviceManagementException {
|
public BillingResponse calculateUsage(String tenantDomain, Timestamp startDate, Timestamp endDate, List<Device> allDevices) throws MetadataManagementDAOException, DeviceManagementException {
|
||||||
|
|
||||||
BillingResponse billingResponse = new BillingResponse();
|
BillingResponse billingResponse = new BillingResponse();
|
||||||
List<Device> deviceStatusNotAvailable = new ArrayList<>();
|
List<Device> deviceStatusNotAvailable = new ArrayList<>();
|
||||||
@ -1045,55 +1047,19 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|||||||
|
|
||||||
Gson g = new Gson();
|
Gson g = new Gson();
|
||||||
Collection<Cost> costData = null;
|
Collection<Cost> costData = null;
|
||||||
|
int tenantIdContext = CarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||||
|
|
||||||
Type collectionType = new TypeToken<Collection<Cost>>() {
|
Type collectionType = new TypeToken<Collection<Cost>>() {
|
||||||
}.getType();
|
}.getType();
|
||||||
if (metadata != null) {
|
if (tenantIdContext == MultitenantConstants.SUPER_TENANT_ID && 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) {
|
totalCost = generateCost(allDevices, startDate, endDate, tenantCost, deviceStatusNotAvailable, totalCost);
|
||||||
long dateDiff = 0;
|
|
||||||
device.setDeviceStatusInfo(getDeviceStatusHistory(device, null, endDate, true));
|
|
||||||
List<DeviceStatus> deviceStatus = device.getDeviceStatusInfo();
|
|
||||||
if (device.getEnrolmentInfo().getDateOfEnrolment() < startDate.getTime()) {
|
|
||||||
if (!deviceStatus.isEmpty() && String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED")) {
|
|
||||||
if (deviceStatus.get(0).getUpdateTime().getTime() >= startDate.getTime()) {
|
|
||||||
dateDiff = deviceStatus.get(0).getUpdateTime().getTime() - startDate.getTime();
|
|
||||||
}
|
|
||||||
} else if (!deviceStatus.isEmpty() && !String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED")) {
|
|
||||||
dateDiff = endDate.getTime() - startDate.getTime();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!deviceStatus.isEmpty() && String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED")) {
|
|
||||||
if (deviceStatus.get(0).getUpdateTime().getTime() >= device.getEnrolmentInfo().getDateOfEnrolment()) {
|
|
||||||
dateDiff = deviceStatus.get(0).getUpdateTime().getTime() - device.getEnrolmentInfo().getDateOfEnrolment();
|
|
||||||
}
|
|
||||||
} else if (!deviceStatus.isEmpty() && !String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED")) {
|
|
||||||
dateDiff = endDate.getTime() - device.getEnrolmentInfo().getDateOfEnrolment();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert dateDiff to days as a decimal value
|
|
||||||
double dateDiffInDays = (double) dateDiff / (24 * 60 * 60 * 1000);
|
|
||||||
|
|
||||||
if (dateDiffInDays % 1 >= 0.9) {
|
|
||||||
dateDiffInDays = Math.ceil(dateDiffInDays);
|
|
||||||
}
|
|
||||||
|
|
||||||
long dateInDays = (long) dateDiffInDays;
|
|
||||||
double cost = (tenantCost.getCost() / 365) * dateInDays;
|
|
||||||
totalCost += cost;
|
|
||||||
device.setCost(Math.round(cost * 100.0) / 100.0);
|
|
||||||
long totalDays = dateInDays + device.getDaysUsed();
|
|
||||||
device.setDaysUsed((int) totalDays);
|
|
||||||
if (deviceStatus.isEmpty()) {
|
|
||||||
deviceStatusNotAvailable.add(device);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
totalCost = generateCost(allDevices, startDate, endDate, null, deviceStatusNotAvailable, totalCost);
|
||||||
}
|
}
|
||||||
} catch (DeviceManagementException e) {
|
} catch (DeviceManagementException e) {
|
||||||
String msg = "Error occurred calculating cost of devices";
|
String msg = "Error occurred calculating cost of devices";
|
||||||
@ -1125,6 +1091,56 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|||||||
return billingResponse;
|
return billingResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double generateCost(List<Device> allDevices, Timestamp startDate, Timestamp endDate, Cost tenantCost, List<Device> deviceStatusNotAvailable, double totalCost) throws DeviceManagementException {
|
||||||
|
for (Device device : allDevices) {
|
||||||
|
long dateDiff = 0;
|
||||||
|
device.setDeviceStatusInfo(getDeviceStatusHistory(device, null, endDate, true));
|
||||||
|
List<DeviceStatus> deviceStatus = device.getDeviceStatusInfo();
|
||||||
|
if (device.getEnrolmentInfo().getDateOfEnrolment() < startDate.getTime()) {
|
||||||
|
if (!deviceStatus.isEmpty() && (String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED")
|
||||||
|
|| String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) {
|
||||||
|
if (deviceStatus.get(0).getUpdateTime().getTime() >= startDate.getTime()) {
|
||||||
|
dateDiff = deviceStatus.get(0).getUpdateTime().getTime() - startDate.getTime();
|
||||||
|
}
|
||||||
|
} else if (!deviceStatus.isEmpty() && (!String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED")
|
||||||
|
&& !String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) {
|
||||||
|
dateDiff = endDate.getTime() - startDate.getTime();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!deviceStatus.isEmpty() && (String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED")
|
||||||
|
|| String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) {
|
||||||
|
if (deviceStatus.get(0).getUpdateTime().getTime() >= device.getEnrolmentInfo().getDateOfEnrolment()) {
|
||||||
|
dateDiff = deviceStatus.get(0).getUpdateTime().getTime() - device.getEnrolmentInfo().getDateOfEnrolment();
|
||||||
|
}
|
||||||
|
} else if (!deviceStatus.isEmpty() && (!String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED")
|
||||||
|
&& !String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) {
|
||||||
|
dateDiff = endDate.getTime() - device.getEnrolmentInfo().getDateOfEnrolment();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert dateDiff to days as a decimal value
|
||||||
|
double dateDiffInDays = (double) dateDiff / (24 * 60 * 60 * 1000);
|
||||||
|
|
||||||
|
if (dateDiffInDays % 1 >= 0.9) {
|
||||||
|
dateDiffInDays = Math.ceil(dateDiffInDays);
|
||||||
|
}
|
||||||
|
|
||||||
|
long dateInDays = (long) dateDiffInDays;
|
||||||
|
double cost = 0;
|
||||||
|
if (tenantCost != null) {
|
||||||
|
cost = (tenantCost.getCost() / 365) * dateInDays;
|
||||||
|
}
|
||||||
|
totalCost += cost;
|
||||||
|
device.setCost(Math.round(cost * 100.0) / 100.0);
|
||||||
|
long totalDays = dateInDays + device.getDaysUsed();
|
||||||
|
device.setDaysUsed((int) totalDays);
|
||||||
|
if (deviceStatus.isEmpty()) {
|
||||||
|
deviceStatusNotAvailable.add(device);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return totalCost;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PaginationResult createBillingFile(int tenantId, String tenantDomain, Timestamp startDate, Timestamp endDate) throws DeviceManagementException {
|
public PaginationResult createBillingFile(int tenantId, String tenantDomain, Timestamp startDate, Timestamp endDate) throws DeviceManagementException {
|
||||||
|
|
||||||
@ -1186,7 +1202,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|||||||
// The query returns devices which are enrolled prior this year now in removed state
|
// The query returns devices which are enrolled prior this year now in removed state
|
||||||
allDevicesPerYear.addAll(deviceDAO.getRemovedPriorYearsDeviceList(tenantId, newStartDate, newEndDate));
|
allDevicesPerYear.addAll(deviceDAO.getRemovedPriorYearsDeviceList(tenantId, newStartDate, newEndDate));
|
||||||
|
|
||||||
BillingResponse billingResponse = calculateCost(tenantDomain, newStartDate, newEndDate, allDevicesPerYear);
|
BillingResponse billingResponse = calculateUsage(tenantDomain, newStartDate, newEndDate, allDevicesPerYear);
|
||||||
billingResponseList.add(billingResponse);
|
billingResponseList.add(billingResponse);
|
||||||
allDevices.addAll(billingResponse.getDevice());
|
allDevices.addAll(billingResponse.getDevice());
|
||||||
totalCost = totalCost + billingResponse.getTotalCostPerYear();
|
totalCost = totalCost + billingResponse.getTotalCostPerYear();
|
||||||
@ -1210,7 +1226,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|||||||
// The query returns devices which are enrolled prior this year now in removed state
|
// The query returns devices which are enrolled prior this year now in removed state
|
||||||
allDevicesPerRemainingDays.addAll(deviceDAO.getRemovedPriorYearsDeviceList(tenantId, startDate, endDate));
|
allDevicesPerRemainingDays.addAll(deviceDAO.getRemovedPriorYearsDeviceList(tenantId, startDate, endDate));
|
||||||
|
|
||||||
BillingResponse billingResponse = calculateCost(tenantDomain, startDate, endDate, allDevicesPerRemainingDays);
|
BillingResponse billingResponse = calculateUsage(tenantDomain, startDate, endDate, allDevicesPerRemainingDays);
|
||||||
billingResponseList.add(billingResponse);
|
billingResponseList.add(billingResponse);
|
||||||
allDevices.addAll(billingResponse.getDevice());
|
allDevices.addAll(billingResponse.getDevice());
|
||||||
totalCost = totalCost + billingResponse.getTotalCostPerYear();
|
totalCost = totalCost + billingResponse.getTotalCostPerYear();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user