|
|
@ -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,31 +1047,73 @@ 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)) {
|
|
|
|
|
|
|
|
totalCost = generateCost(allDevices, startDate, endDate, tenantCost, deviceStatusNotAvailable, totalCost);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
totalCost = generateCost(allDevices, startDate, endDate, null, deviceStatusNotAvailable, totalCost);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
|
|
|
String msg = "Error occurred calculating cost of devices";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new DeviceManagementException(msg, e);
|
|
|
|
|
|
|
|
} catch (MetadataManagementException e) {
|
|
|
|
|
|
|
|
String msg = "Error when retrieving metadata of billing feature";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new DeviceManagementException(msg, e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!deviceStatusNotAvailable.isEmpty()) {
|
|
|
|
|
|
|
|
allDevices.removeAll(deviceStatusNotAvailable);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Calendar calStart = Calendar.getInstance();
|
|
|
|
|
|
|
|
Calendar calEnd = Calendar.getInstance();
|
|
|
|
|
|
|
|
calStart.setTimeInMillis(startDate.getTime());
|
|
|
|
|
|
|
|
calEnd.setTimeInMillis(endDate.getTime());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
billingResponse.setDevice(allDevices);
|
|
|
|
|
|
|
|
billingResponse.setYear(String.valueOf(calStart.get(Calendar.YEAR)));
|
|
|
|
|
|
|
|
billingResponse.setStartDate(startDate.toString());
|
|
|
|
|
|
|
|
billingResponse.setEndDate(endDate.toString());
|
|
|
|
|
|
|
|
billingResponse.setBillPeriod(calStart.get(Calendar.YEAR) + " - " + calEnd.get(Calendar.YEAR));
|
|
|
|
|
|
|
|
billingResponse.setTotalCostPerYear(Math.round(totalCost * 100.0) / 100.0);
|
|
|
|
|
|
|
|
billingResponse.setDeviceCount(allDevices.size());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
for (Device device : allDevices) {
|
|
|
|
long dateDiff = 0;
|
|
|
|
long dateDiff = 0;
|
|
|
|
device.setDeviceStatusInfo(getDeviceStatusHistory(device, null, endDate, true));
|
|
|
|
device.setDeviceStatusInfo(getDeviceStatusHistory(device, null, endDate, true));
|
|
|
|
List<DeviceStatus> deviceStatus = device.getDeviceStatusInfo();
|
|
|
|
List<DeviceStatus> deviceStatus = device.getDeviceStatusInfo();
|
|
|
|
if (device.getEnrolmentInfo().getDateOfEnrolment() < startDate.getTime()) {
|
|
|
|
if (device.getEnrolmentInfo().getDateOfEnrolment() < startDate.getTime()) {
|
|
|
|
if (!deviceStatus.isEmpty() && String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED")) {
|
|
|
|
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()) {
|
|
|
|
if (deviceStatus.get(0).getUpdateTime().getTime() >= startDate.getTime()) {
|
|
|
|
dateDiff = 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")) {
|
|
|
|
} 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();
|
|
|
|
dateDiff = endDate.getTime() - startDate.getTime();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
if (!deviceStatus.isEmpty() && String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED")) {
|
|
|
|
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()) {
|
|
|
|
if (deviceStatus.get(0).getUpdateTime().getTime() >= device.getEnrolmentInfo().getDateOfEnrolment()) {
|
|
|
|
dateDiff = 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")) {
|
|
|
|
} 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();
|
|
|
|
dateDiff = endDate.getTime() - device.getEnrolmentInfo().getDateOfEnrolment();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -1082,7 +1126,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
long dateInDays = (long) dateDiffInDays;
|
|
|
|
long dateInDays = (long) dateDiffInDays;
|
|
|
|
double cost = (tenantCost.getCost() / 365) * dateInDays;
|
|
|
|
double cost = 0;
|
|
|
|
|
|
|
|
if (tenantCost != null) {
|
|
|
|
|
|
|
|
cost = (tenantCost.getCost() / 365) * dateInDays;
|
|
|
|
|
|
|
|
}
|
|
|
|
totalCost += cost;
|
|
|
|
totalCost += cost;
|
|
|
|
device.setCost(Math.round(cost * 100.0) / 100.0);
|
|
|
|
device.setCost(Math.round(cost * 100.0) / 100.0);
|
|
|
|
long totalDays = dateInDays + device.getDaysUsed();
|
|
|
|
long totalDays = dateInDays + device.getDaysUsed();
|
|
|
@ -1091,38 +1138,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|
|
|
deviceStatusNotAvailable.add(device);
|
|
|
|
deviceStatusNotAvailable.add(device);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalCost;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
|
|
|
String msg = "Error occurred calculating cost of devices";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new DeviceManagementException(msg, e);
|
|
|
|
|
|
|
|
} catch (MetadataManagementException e) {
|
|
|
|
|
|
|
|
String msg = "Error when retrieving metadata of billing feature";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new DeviceManagementException(msg, e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!deviceStatusNotAvailable.isEmpty()) {
|
|
|
|
|
|
|
|
allDevices.removeAll(deviceStatusNotAvailable);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Calendar calStart = Calendar.getInstance();
|
|
|
|
|
|
|
|
Calendar calEnd = Calendar.getInstance();
|
|
|
|
|
|
|
|
calStart.setTimeInMillis(startDate.getTime());
|
|
|
|
|
|
|
|
calEnd.setTimeInMillis(endDate.getTime());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
billingResponse.setDevice(allDevices);
|
|
|
|
|
|
|
|
billingResponse.setYear(String.valueOf(calStart.get(Calendar.YEAR)));
|
|
|
|
|
|
|
|
billingResponse.setStartDate(startDate.toString());
|
|
|
|
|
|
|
|
billingResponse.setEndDate(endDate.toString());
|
|
|
|
|
|
|
|
billingResponse.setBillPeriod(calStart.get(Calendar.YEAR) + " - " + calEnd.get(Calendar.YEAR));
|
|
|
|
|
|
|
|
billingResponse.setTotalCostPerYear(Math.round(totalCost * 100.0) / 100.0);
|
|
|
|
|
|
|
|
billingResponse.setDeviceCount(allDevices.size());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return billingResponse;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -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();
|
|
|
|