|
|
@ -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
|
|
|
|