osh 3 years ago
parent 7ba8311c93
commit f86617a264

@ -45,6 +45,7 @@ public final class DeviceManagementConstants {
public static final String API_RESOURCE_PERMISSION_CACHE = "API_RESOURCE_CACHE_CACHE"; public static final String API_RESOURCE_PERMISSION_CACHE = "API_RESOURCE_CACHE_CACHE";
public static final String GEOFENCE_CACHE = "GEOFENCE_CACHE"; public static final String GEOFENCE_CACHE = "GEOFENCE_CACHE";
public static final String META_KEY = "PER_DEVICE_COST"; public static final String META_KEY = "PER_DEVICE_COST";
public static final String ACTIVE_STATUS = "ACTIVE";
public static final String ENROLLMENT_NOTIFICATION_API_ENDPOINT = "/api/device-mgt/enrollment-notification"; public static final String ENROLLMENT_NOTIFICATION_API_ENDPOINT = "/api/device-mgt/enrollment-notification";
public static final String URL_SEPERATOR = "/"; public static final String URL_SEPERATOR = "/";

@ -129,6 +129,7 @@ import org.wso2.carbon.email.sender.core.service.EmailSenderService;
import org.wso2.carbon.stratos.common.beans.TenantInfoBean; import org.wso2.carbon.stratos.common.beans.TenantInfoBean;
import org.wso2.carbon.tenant.mgt.services.TenantMgtAdminService; import org.wso2.carbon.tenant.mgt.services.TenantMgtAdminService;
import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
@ -946,7 +947,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
PaginationResult paginationResult = new PaginationResult(); PaginationResult paginationResult = new PaginationResult();
double totalCost = 0.0; double totalCost = 0.0;
boolean allDevicesBilledDateIsValid = true; boolean allDevicesBilledDateIsValid = true;
String lastBilledDates = "";
ArrayList<String> lastBilledDatesList = new ArrayList<>(); ArrayList<String> lastBilledDatesList = new ArrayList<>();
List<Billing> invalidDevices = new ArrayList<>(); List<Billing> invalidDevices = new ArrayList<>();
List<Device> removeBillingPeriodInvalidDevices = new ArrayList<>() ; List<Device> removeBillingPeriodInvalidDevices = new ArrayList<>() ;
@ -954,7 +954,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
try { try {
MetadataManagementDAOFactory.beginTransaction(); MetadataManagementDAOFactory.beginTransaction();
Metadata metadata = metadataDAO.getMetadata(-1234, DeviceManagementConstants.META_KEY); Metadata metadata = metadataDAO.getMetadata(MultitenantConstants.SUPER_TENANT_ID, DeviceManagementConstants.META_KEY);
Gson g = new Gson(); Gson g = new Gson();
Collection<Cost> costData = null; Collection<Cost> costData = null;
@ -970,7 +970,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
long dateDiff = 0; long dateDiff = 0;
List<DeviceStatus> deviceStatus = device.getDeviceStatusInfo(); List<DeviceStatus> deviceStatus = device.getDeviceStatusInfo();
boolean lastBilledDate = false; boolean firstDateBilled = false;
boolean deviceStatusIsValid = false; boolean deviceStatusIsValid = false;
List<Billing> deviceBilling = billingDAO.getBilling(device.getId(), startDate, endDate); List<Billing> deviceBilling = billingDAO.getBilling(device.getId(), startDate, endDate);
@ -992,10 +992,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
if (!billDateIsInvalid) { if (!billDateIsInvalid) {
for (int i = 0; i < deviceStatus.size(); i++) { for (int i = 0; i < deviceStatus.size(); i++) {
if (deviceStatus.get(i).getStatus().toString().equals("ACTIVE")) { if (DeviceManagementConstants.ACTIVE_STATUS.equals(deviceStatus.get(i).getStatus().toString())) {
if (deviceStatus.size() > i + 1) { if (deviceStatus.size() > i + 1) {
if (!lastBilledDate) { if (!firstDateBilled) {
lastBilledDate = true; firstDateBilled = true;
if (device.getEnrolmentInfo().getLastBilledDate() == 0) { if (device.getEnrolmentInfo().getLastBilledDate() == 0) {
deviceStatusIsValid = true; deviceStatusIsValid = true;
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());
@ -1011,11 +1011,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
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());
} }
} }
} else { // The last status update calculation is done in this block } else {
// If only one status row is retrieved this block is executed // If only one status row is retrieved this block is executed
if (!lastBilledDate) { if (!firstDateBilled) {
lastBilledDate = true; firstDateBilled = true;
// Is executed if there is no lastBilled date and if the updates time is before the enddate
// Is executed if there is no lastBilled date and if the updates time is before the end date
if (device.getEnrolmentInfo().getLastBilledDate() == 0) { if (device.getEnrolmentInfo().getLastBilledDate() == 0) {
if (endDate.getTime() >= deviceStatus.get(i).getUpdateTime().getTime()) { if (endDate.getTime() >= deviceStatus.get(i).getUpdateTime().getTime()) {
deviceStatusIsValid = true; deviceStatusIsValid = true;
@ -1066,11 +1068,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
if (device.getEnrolmentInfo().getLastBilledDate() != 0) { if (device.getEnrolmentInfo().getLastBilledDate() != 0) {
Date date = new Date(device.getEnrolmentInfo().getLastBilledDate()); Date date = new Date(device.getEnrolmentInfo().getLastBilledDate());
Format format = new SimpleDateFormat("yyyy MM dd"); Format format = new SimpleDateFormat("yyyy MM dd");
if (!lastBilledDatesList.contains(lastBilledDatesList.add(format.format(date)))) {
for (String lastBillDate : lastBilledDatesList) {
if (!lastBillDate.equals(format.format(date))) {
lastBilledDatesList.add(format.format(date)); lastBilledDatesList.add(format.format(date));
} }
}
lastBilledDates = lastBilledDates + ' ' + lastBilledDatesList;
} }
removeBillingPeriodInvalidDevices.add(device); removeBillingPeriodInvalidDevices.add(device);
} }
@ -1098,14 +1101,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
if(!removeBillingPeriodInvalidDevices.isEmpty() && removeBillingPeriodInvalidDevices.size() == allDevices.size()) { if(!removeBillingPeriodInvalidDevices.isEmpty() && removeBillingPeriodInvalidDevices.size() == allDevices.size()) {
allDevicesBilledDateIsValid = false; allDevicesBilledDateIsValid = false;
paginationResult.setMessage("Invalid bill period last billed dates of devices are " +lastBilledDates); paginationResult.setMessage("Invalid bill period.");
} }
if(!removeStatusUpdateInvalidDevices.isEmpty() && removeStatusUpdateInvalidDevices.size() == allDevices.size()) { if(!removeStatusUpdateInvalidDevices.isEmpty() && removeStatusUpdateInvalidDevices.size() == allDevices.size()) {
allDevicesBilledDateIsValid = false; allDevicesBilledDateIsValid = false;
if (paginationResult.getMessage() != null){ if (paginationResult.getMessage() != null){
paginationResult.setMessage(paginationResult.getMessage() + " and no device updates within entered bill period."); paginationResult.setMessage(paginationResult.getMessage() + " and no device updates within entered bill period.");
} else { } else {
paginationResult.setMessage("Devices have not been updated within the given period or entered end date comes before the last billed date"); paginationResult.setMessage("Devices have not been updated within the given period or entered end date comes before the " +
"last billed date.");
} }
} }

@ -25,10 +25,12 @@
<!-- maximum number of login cache entries --> <!-- maximum number of login cache entries -->
<LoginCacheCapacity>10000</LoginCacheCapacity> <LoginCacheCapacity>10000</LoginCacheCapacity>
<Billing> <Billing>
<HideBillGenerationInSuperTenant>true</HideBillGenerationInSuperTenant> <HideBillGenerationInSuperTenant>false</HideBillGenerationInSuperTenant>
<HideBillGenerationInSubTenant>true</HideBillGenerationInSubTenant> <HideBillGenerationInSubTenant>false</HideBillGenerationInSubTenant>
<HideTotalCalculationInSuperTenant>true</HideTotalCalculationInSuperTenant> <HideTotalCalculationInSuperTenant>false</HideTotalCalculationInSuperTenant>
<HideTotalCalculationInSubTenant>true</HideTotalCalculationInSubTenant> <HideTotalCalculationInSubTenant>false</HideTotalCalculationInSubTenant>
<HideDomainSelectionInSuperTenant>false</HideDomainSelectionInSuperTenant>
<HideDomainSelectionInSubTenant>true</HideDomainSelectionInSubTenant>
</Billing> </Billing>
<AppRegistration> <AppRegistration>
<Tags> <Tags>

@ -52,6 +52,17 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE (
CONSTRAINT uk_DM_DEVICE UNIQUE (NAME, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, TENANT_ID) CONSTRAINT uk_DM_DEVICE UNIQUE (NAME, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, TENANT_ID)
); );
CREATE TABLE IF NOT EXISTS DM_BILLING (
INVOICE_ID INTEGER auto_increment NOT NULL,
TENANT_ID INTEGER default 0,
DEVICE_ID INT default NULL,
BILLING_START TIMESTAMP not null,
BILLING_END TIMESTAMP not null,
PRIMARY KEY (INVOICE_ID),
CONSTRAINT FK_DM_BILLING_DM_DEVICE
FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID)
);
CREATE TABLE IF NOT EXISTS DM_DEVICE_PROPERTIES ( CREATE TABLE IF NOT EXISTS DM_DEVICE_PROPERTIES (
DEVICE_TYPE_NAME VARCHAR(300) NOT NULL, DEVICE_TYPE_NAME VARCHAR(300) NOT NULL,
DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL,
@ -103,6 +114,7 @@ CREATE TABLE IF NOT EXISTS DM_ENROLMENT (
DATE_OF_ENROLMENT TIMESTAMP DEFAULT NULL, DATE_OF_ENROLMENT TIMESTAMP DEFAULT NULL,
DATE_OF_LAST_UPDATE TIMESTAMP DEFAULT NULL, DATE_OF_LAST_UPDATE TIMESTAMP DEFAULT NULL,
TENANT_ID INT NOT NULL, TENANT_ID INT NOT NULL,
LAST_BILLED_DATE BIGINT DEFAULT 0,
PRIMARY KEY (ID), PRIMARY KEY (ID),
CONSTRAINT fk_dm_device_enrolment FOREIGN KEY (DEVICE_ID) REFERENCES CONSTRAINT fk_dm_device_enrolment FOREIGN KEY (DEVICE_ID) REFERENCES
DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,

@ -66,6 +66,18 @@ CREATE TABLE DM_DEVICE (
REFERENCES DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION REFERENCES DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
); );
IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_BILLING]') AND TYPE IN (N'U'))
CREATE TABLE DM_BILLING (
INVOICE_ID INTEGER IDENTITY(1,1) NOT NULL,
TENANT_ID INTEGER DEFAULT 0,
DEVICE_ID INTEGER DEFAULT NULL,
BILLING_START DATETIME2 NOT NULL,
BILLING_END DATETIME2 NOT NULL,
PRIMARY KEY (INVOICE_ID),
CONSTRAINT FK_DM_BILLING_DM_DEVICE2 FOREIGN KEY (DEVICE_ID)
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_PROPERTIES]') AND TYPE IN (N'U')) IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_PROPERTIES]') AND TYPE IN (N'U'))
CREATE TABLE DM_DEVICE_PROPERTIES ( CREATE TABLE DM_DEVICE_PROPERTIES (
DEVICE_TYPE_NAME VARCHAR(300) NOT NULL, DEVICE_TYPE_NAME VARCHAR(300) NOT NULL,
@ -146,6 +158,7 @@ CREATE TABLE DM_ENROLMENT (
DATE_OF_ENROLMENT DATETIME2 DEFAULT NULL, DATE_OF_ENROLMENT DATETIME2 DEFAULT NULL,
DATE_OF_LAST_UPDATE DATETIME2 DEFAULT NULL, DATE_OF_LAST_UPDATE DATETIME2 DEFAULT NULL,
TENANT_ID INTEGER NOT NULL, TENANT_ID INTEGER NOT NULL,
LAST_BILLED_DATE BIGINT DEFAULT 0,
PRIMARY KEY (ID), PRIMARY KEY (ID),
CONSTRAINT FK_DM_DEVICE_ENROLMENT FOREIGN KEY (DEVICE_ID) REFERENCES CONSTRAINT FK_DM_DEVICE_ENROLMENT FOREIGN KEY (DEVICE_ID) REFERENCES
DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION

@ -59,6 +59,17 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE (
REFERENCES DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION REFERENCES DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
)ENGINE = InnoDB; )ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS DM_BILLING (
INVOICE_ID INTEGER AUTO_INCREMENT NOT NULL,
TENANT_ID INTEGER DEFAULT 0,
DEVICE_ID INTEGER DEFAULT NULL,
BILLING_START TIMESTAMP NOT NULL,
BILLING_END TIMESTAMP NOT NULL,
PRIMARY KEY (INVOICE_ID),
CONSTRAINT fk_DM_BILLING_DM_DEVICE2 FOREIGN KEY (DEVICE_ID)
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
)ENGINE = InnoDB;
CREATE INDEX IDX_DM_DEVICE ON DM_DEVICE(TENANT_ID, DEVICE_TYPE_ID); CREATE INDEX IDX_DM_DEVICE ON DM_DEVICE(TENANT_ID, DEVICE_TYPE_ID);
CREATE INDEX IDX_DM_DEVICE_TYPE_ID_DEVICE_IDENTIFICATION ON DM_DEVICE(TENANT_ID, DEVICE_TYPE_ID,DEVICE_IDENTIFICATION); CREATE INDEX IDX_DM_DEVICE_TYPE_ID_DEVICE_IDENTIFICATION ON DM_DEVICE(TENANT_ID, DEVICE_TYPE_ID,DEVICE_IDENTIFICATION);
@ -118,6 +129,7 @@ CREATE TABLE IF NOT EXISTS DM_ENROLMENT (
DATE_OF_ENROLMENT TIMESTAMP NULL DEFAULT NULL, DATE_OF_ENROLMENT TIMESTAMP NULL DEFAULT NULL,
DATE_OF_LAST_UPDATE TIMESTAMP NULL DEFAULT NULL, DATE_OF_LAST_UPDATE TIMESTAMP NULL DEFAULT NULL,
TENANT_ID INT NOT NULL, TENANT_ID INT NOT NULL,
LAST_BILLED_DATE BIGINT DEFAULT 0,
PRIMARY KEY (ID), PRIMARY KEY (ID),
CONSTRAINT FK_DM_DEVICE_ENROLMENT FOREIGN KEY (DEVICE_ID) REFERENCES CONSTRAINT FK_DM_DEVICE_ENROLMENT FOREIGN KEY (DEVICE_ID) REFERENCES
DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION

@ -124,6 +124,19 @@ WHEN (NEW.ID IS NULL)
END; END;
/ /
CREATE TABLE DM_BILLING (
INVOICE_ID NUMBER(10) NOT NULL,
TENANT_ID NUMBER(10) DEFAULT 0,
DEVICE_ID NUMBER(10) DEFAULT NULL,
BILLING_START TIMESTAMP NOT NULL,
BILLING_END TIMESTAMP NOT NULL,
CONSTRAINT PK_DM_BILLING PRIMARY KEY (INVOICE_ID),
CONSTRAINT fk_DM_BILLING_DM_DEVICE2
FOREIGN KEY (DEVICE_ID)
REFERENCES DM_DEVICE (ID)
)
/
CREATE TABLE DM_DEVICE_PROPERTIES ( CREATE TABLE DM_DEVICE_PROPERTIES (
DEVICE_TYPE_NAME VARCHAR2(300) NOT NULL, DEVICE_TYPE_NAME VARCHAR2(300) NOT NULL,
DEVICE_IDENTIFICATION VARCHAR2(300) NOT NULL, DEVICE_IDENTIFICATION VARCHAR2(300) NOT NULL,
@ -208,6 +221,7 @@ CREATE TABLE DM_ENROLMENT (
DATE_OF_ENROLMENT TIMESTAMP(0) DEFAULT NULL, DATE_OF_ENROLMENT TIMESTAMP(0) DEFAULT NULL,
DATE_OF_LAST_UPDATE TIMESTAMP(0) DEFAULT NULL, DATE_OF_LAST_UPDATE TIMESTAMP(0) DEFAULT NULL,
TENANT_ID NUMBER(10) NOT NULL, TENANT_ID NUMBER(10) NOT NULL,
LAST_BILLED_DATE BIGINT DEFAULT 0,
CONSTRAINT PK_DM_ENROLMENT PRIMARY KEY (ID), CONSTRAINT PK_DM_ENROLMENT PRIMARY KEY (ID),
CONSTRAINT FK_DM_DEVICE_ENROLMENT FOREIGN KEY (DEVICE_ID) REFERENCES CONSTRAINT FK_DM_DEVICE_ENROLMENT FOREIGN KEY (DEVICE_ID) REFERENCES
DM_DEVICE (ID) DM_DEVICE (ID)

@ -57,6 +57,17 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE (
REFERENCES DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION REFERENCES DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
); );
CREATE TABLE IF NOT EXISTS DM_BILLING (
INVOICE_ID INTEGER DEFAULT NEXTVAL ('DM_BILLING_seq') NOT NULL,
TENANT_ID INTEGER DEFAULT 0,
DEVICE_ID INTEGER DEFAULT NULL,
BILLING_START TIMESTAMP(0) NOT NULL,
BILLING_END TIMESTAMP(0) NOT NULL,
PRIMARY KEY (INVOICE_ID),
CONSTRAINT fk_DM_BILLING_DM_DEVICE2 FOREIGN KEY (DEVICE_ID)
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX IDX_DM_DEVICE ON DM_DEVICE(TENANT_ID, DEVICE_TYPE_ID); CREATE INDEX IDX_DM_DEVICE ON DM_DEVICE(TENANT_ID, DEVICE_TYPE_ID);
CREATE INDEX IDX_DM_DEVICE_TYPE_ID_DEVICE_IDENTIFICATION ON DM_DEVICE(TENANT_ID, DEVICE_TYPE_ID,DEVICE_IDENTIFICATION); CREATE INDEX IDX_DM_DEVICE_TYPE_ID_DEVICE_IDENTIFICATION ON DM_DEVICE(TENANT_ID, DEVICE_TYPE_ID,DEVICE_IDENTIFICATION);
@ -116,6 +127,7 @@ CREATE TABLE IF NOT EXISTS DM_ENROLMENT (
DATE_OF_ENROLMENT TIMESTAMP(0) NULL DEFAULT NULL, DATE_OF_ENROLMENT TIMESTAMP(0) NULL DEFAULT NULL,
DATE_OF_LAST_UPDATE TIMESTAMP(0) NULL DEFAULT NULL, DATE_OF_LAST_UPDATE TIMESTAMP(0) NULL DEFAULT NULL,
TENANT_ID INTEGER NOT NULL, TENANT_ID INTEGER NOT NULL,
LAST_BILLED_DATE BIGINT DEFAULT 0,
PRIMARY KEY (ID), PRIMARY KEY (ID),
CONSTRAINT FK_DM_DEVICE_ENROLMENT FOREIGN KEY (DEVICE_ID) REFERENCES CONSTRAINT FK_DM_DEVICE_ENROLMENT FOREIGN KEY (DEVICE_ID) REFERENCES
DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION

Loading…
Cancel
Save