@ -69,8 +69,10 @@ import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
import org.wso2.carbon.device.mgt.common.PaginationRequest ;
import org.wso2.carbon.device.mgt.common.PaginationRequest ;
import org.wso2.carbon.device.mgt.common.PaginationResult ;
import org.wso2.carbon.device.mgt.common.PaginationResult ;
import org.wso2.carbon.device.mgt.common.StartupOperationConfig ;
import org.wso2.carbon.device.mgt.common.StartupOperationConfig ;
import org.wso2.carbon.device.mgt.common.BillingResponse ;
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.app.mgt.ApplicationManagementException ;
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException ;
import org.wso2.carbon.device.mgt.common.app.mgt.MobileAppTypes ;
import org.wso2.carbon.device.mgt.common.configuration.mgt.AmbiguousConfigurationException ;
import org.wso2.carbon.device.mgt.common.configuration.mgt.AmbiguousConfigurationException ;
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.configuration.mgt.ConfigurationManagementException ;
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException ;
@ -119,11 +121,11 @@ import org.wso2.carbon.device.mgt.common.type.mgt.DeviceTypePlatformVersion;
import org.wso2.carbon.device.mgt.core.DeviceManagementConstants ;
import org.wso2.carbon.device.mgt.core.DeviceManagementConstants ;
import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository ;
import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository ;
import org.wso2.carbon.device.mgt.core.cache.DeviceCacheKey ;
import org.wso2.carbon.device.mgt.core.cache.DeviceCacheKey ;
import org.wso2.carbon.device.mgt.core.cache.impl.BillingCacheManagerImpl ;
import org.wso2.carbon.device.mgt.core.cache.impl.DeviceCacheManagerImpl ;
import org.wso2.carbon.device.mgt.core.cache.impl.DeviceCacheManagerImpl ;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager ;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager ;
import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig ;
import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig ;
import org.wso2.carbon.device.mgt.core.dao.ApplicationDAO ;
import org.wso2.carbon.device.mgt.core.dao.ApplicationDAO ;
import org.wso2.carbon.device.mgt.core.dao.BillingDAO ;
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO ;
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO ;
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.dao.DeviceManagementDAOFactory ;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory ;
@ -141,6 +143,7 @@ import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent ;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent ;
import org.wso2.carbon.device.mgt.core.internal.PluginInitializationListener ;
import org.wso2.carbon.device.mgt.core.internal.PluginInitializationListener ;
import org.wso2.carbon.device.mgt.core.metadata.mgt.dao.MetadataDAO ;
import org.wso2.carbon.device.mgt.core.metadata.mgt.dao.MetadataDAO ;
import org.wso2.carbon.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOException ;
import org.wso2.carbon.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOFactory ;
import org.wso2.carbon.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOFactory ;
import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation ;
import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation ;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil ;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil ;
@ -164,8 +167,7 @@ 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.sql.Timestamp ;
import java.text.Format ;
import java.time.LocalDateTime ;
import java.text.SimpleDateFormat ;
import java.util.ArrayList ;
import java.util.ArrayList ;
import java.util.Calendar ;
import java.util.Calendar ;
import java.util.Collection ;
import java.util.Collection ;
@ -193,7 +195,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
private final EnrollmentDAO enrollmentDAO ;
private final EnrollmentDAO enrollmentDAO ;
private final ApplicationDAO applicationDAO ;
private final ApplicationDAO applicationDAO ;
private MetadataDAO metadataDAO ;
private MetadataDAO metadataDAO ;
private final BillingDAO billingDAO ;
private final DeviceStatusDAO deviceStatusDAO ;
private final DeviceStatusDAO deviceStatusDAO ;
public DeviceManagementProviderServiceImpl ( ) {
public DeviceManagementProviderServiceImpl ( ) {
@ -203,7 +204,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
this . deviceTypeDAO = DeviceManagementDAOFactory . getDeviceTypeDAO ( ) ;
this . deviceTypeDAO = DeviceManagementDAOFactory . getDeviceTypeDAO ( ) ;
this . enrollmentDAO = DeviceManagementDAOFactory . getEnrollmentDAO ( ) ;
this . enrollmentDAO = DeviceManagementDAOFactory . getEnrollmentDAO ( ) ;
this . metadataDAO = MetadataManagementDAOFactory . getMetadataDAO ( ) ;
this . metadataDAO = MetadataManagementDAOFactory . getMetadataDAO ( ) ;
this . billingDAO = DeviceManagementDAOFactory . getBillingDAO ( ) ;
this . deviceStatusDAO = DeviceManagementDAOFactory . getDeviceStatusDAO ( ) ;
this . deviceStatusDAO = DeviceManagementDAOFactory . getDeviceStatusDAO ( ) ;
/ * Registering a listener to retrieve events when some device management service plugin is installed after
/ * Registering a listener to retrieve events when some device management service plugin is installed after
@ -1021,236 +1021,232 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return this . getAllDevices ( request , true ) ;
return this . getAllDevices ( request , true ) ;
}
}
public PaginationResult calculateCost ( int tenantId , String tenantDomain , Timestamp startDate , Timestamp endDate , boolean generateBill , List < Device > allDevices ) throws DeviceManagementException {
/ * *
* Calculate cost of a tenants Device List .
* Once the full device list of a tenant is sent here devices are looped for cost calculation
* Cost per tenant is retrieved from the Meta Table
* When looping the devices the most recent device status of that device is retrieved from status table
* If device is enrolled prior to start date now in removed state - - > time is calculated from - startDate to last updated time
* If device is enrolled prior to start date now in not removed - - > time is calculated from - startDate to endDate
* If device is enrolled after the start date now in removed state - - > time is calculated from - dateOfEnrollment to last updated time
* If device is enrolled after the start date now in not removed - - > time is calculated from - dateOfEnrollment to endDate
* Once time is calculated cost is set for each device
*
* @param tenantDomain Tenant domain cost id calculated for .
* @param startDate start date of usage period .
* @param endDate end date of usage period .
* @param allDevices device list of the tenant for the selected time - period .
* @return Whether status is changed or not
* @throws DeviceManagementException on errors while trying to calculate Cost
* /
public BillingResponse calculateCost ( String tenantDomain , Timestamp startDate , Timestamp endDate , List < Device > allDevices ) throws MetadataManagementDAOException , DeviceManagementException {
PaginationResult paginationResult = new PaginationResult ( ) ;
BillingResponse billingResponse = new BillingResponse ( ) ;
List < Device > deviceStatusNotAvailable = new ArrayList < > ( ) ;
double totalCost = 0.0 ;
double totalCost = 0.0 ;
boolean allDevicesBilledDateIsValid = true ;
ArrayList < String > lastBilledDatesList = new ArrayList < > ( ) ;
List < Billing > invalidDevices = new ArrayList < > ( ) ;
List < Device > removeBillingPeriodInvalidDevices = new ArrayList < > ( ) ;
List < Device > removeStatusUpdateInvalidDevices = new ArrayList < > ( ) ;
try {
try {
MetadataManagementDAOFactory . beginTransaction ( ) ;
MetadataManagementDAOFactory . openConnection ( ) ;
Metadata metadata = metadataDAO . getMetadata ( MultitenantConstants . SUPER_TENANT_ID , 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 ;
Type collectionType = new TypeToken < Collection < Cost > > ( ) {
Type collectionType = new TypeToken < Collection < Cost > > ( ) { } . getType ( ) ;
} . getType ( ) ;
if ( metadata ! = null ) {
if ( 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 ) {
for ( Device device : allDevices ) {
device . setDeviceStatusInfo ( getDeviceStatusHistory ( device , null , null , true ) ) ;
long dateDiff = 0 ;
long dateDiff = 0 ;
device . setDeviceStatusInfo ( getDeviceStatusHistory ( device , null , endDate , true ) ) ;
List < DeviceStatus > deviceStatus = device . getDeviceStatusInfo ( ) ;
List < DeviceStatus > deviceStatus = device . getDeviceStatusInfo ( ) ;
boolean firstDateBilled = false ;
if ( device . getEnrolmentInfo ( ) . getDateOfEnrolment ( ) < startDate . getTime ( ) ) {
boolean deviceStatusIsValid = false ;
if ( ! deviceStatus . isEmpty ( ) & & deviceStatus . get ( 0 ) . getStatus ( ) . equals ( "REMOVED" ) ) {
if ( deviceStatus . get ( 0 ) . getUpdateTime ( ) . getTime ( ) > = startDate . getTime ( ) ) {
List < Billing > deviceBilling = billingDAO . getBilling ( device . getId ( ) , startDate , endDate ) ;
dateDiff = deviceStatus . get ( 0 ) . getUpdateTime ( ) . getTime ( ) - startDate . getTime ( ) ;
boolean billDateIsInvalid = false ;
if ( deviceBilling . isEmpty ( ) ) {
billDateIsInvalid = false ;
}
if ( generateBill ) {
for ( Billing bill : deviceBilling ) {
if ( ( bill . getBillingStart ( ) < = startDate . getTime ( ) & & startDate . getTime ( ) < = bill . getBillingEnd ( ) ) | |
( bill . getBillingStart ( ) < = endDate . getTime ( ) & & endDate . getTime ( ) < = bill . getBillingEnd ( ) ) ) {
billDateIsInvalid = true ;
invalidDevices . add ( bill ) ;
}
}
}
if ( ! billDateIsInvalid ) {
for ( int i = 0 ; i < deviceStatus . size ( ) ; i + + ) {
if ( DeviceManagementConstants . ACTIVE_STATUS . equals ( deviceStatus . get ( i ) . getStatus ( ) . toString ( ) ) ) {
if ( deviceStatus . size ( ) > i + 1 ) {
if ( ! firstDateBilled ) {
firstDateBilled = true ;
if ( device . getEnrolmentInfo ( ) . getLastBilledDate ( ) = = 0 ) {
deviceStatusIsValid = true ;
dateDiff = dateDiff + ( deviceStatus . get ( i + 1 ) . getUpdateTime ( ) . getTime ( ) - deviceStatus . get ( i ) . getUpdateTime ( ) . getTime ( ) ) ;
} else {
if ( deviceStatus . get ( i + 1 ) . getUpdateTime ( ) . getTime ( ) > = device . getEnrolmentInfo ( ) . getLastBilledDate ( ) ) {
deviceStatusIsValid = true ;
dateDiff = dateDiff + ( deviceStatus . get ( i + 1 ) . getUpdateTime ( ) . getTime ( ) - device . getEnrolmentInfo ( ) . getLastBilledDate ( ) ) ;
}
}
} else {
if ( deviceStatus . get ( i ) . getUpdateTime ( ) . getTime ( ) > = device . getEnrolmentInfo ( ) . getLastBilledDate ( ) ) {
deviceStatusIsValid = true ;
dateDiff = dateDiff + ( deviceStatus . get ( i + 1 ) . getUpdateTime ( ) . getTime ( ) - deviceStatus . get ( i ) . getUpdateTime ( ) . getTime ( ) ) ;
}
}
} else {
// If only one status row is retrieved this block is executed
if ( ! firstDateBilled ) {
firstDateBilled = true ;
// Is executed if there is no lastBilled date and if the updates time is before the end date
if ( device . getEnrolmentInfo ( ) . getLastBilledDate ( ) = = 0 ) {
if ( endDate . getTime ( ) > = deviceStatus . get ( i ) . getUpdateTime ( ) . getTime ( ) ) {
deviceStatusIsValid = true ;
dateDiff = dateDiff + ( endDate . getTime ( ) - deviceStatus . get ( i ) . getUpdateTime ( ) . getTime ( ) ) ;
}
} else {
if ( endDate . getTime ( ) > = device . getEnrolmentInfo ( ) . getLastBilledDate ( ) ) {
deviceStatusIsValid = true ;
dateDiff = dateDiff + ( endDate . getTime ( ) - device . getEnrolmentInfo ( ) . getLastBilledDate ( ) ) ;
}
}
} else {
if ( device . getEnrolmentInfo ( ) . getLastBilledDate ( ) < = deviceStatus . get ( i ) . getUpdateTime ( ) . getTime ( )
& & endDate . getTime ( ) > = deviceStatus . get ( i ) . getUpdateTime ( ) . getTime ( ) ) {
deviceStatusIsValid = true ;
dateDiff = dateDiff + ( endDate . getTime ( ) - deviceStatus . get ( i ) . getUpdateTime ( ) . getTime ( ) ) ;
}
if ( device . getEnrolmentInfo ( ) . getLastBilledDate ( ) > = deviceStatus . get ( i ) . getUpdateTime ( ) . getTime ( )
& & endDate . getTime ( ) > = device . getEnrolmentInfo ( ) . getLastBilledDate ( ) ) {
deviceStatusIsValid = true ;
dateDiff = dateDiff + ( endDate . getTime ( ) - device . getEnrolmentInfo ( ) . getLastBilledDate ( ) ) ;
}
}
}
}
}
} else if ( ! deviceStatus . isEmpty ( ) & & ! deviceStatus . get ( 0 ) . getStatus ( ) . equals ( "REMOVED" ) ) {
dateDiff = endDate . getTime ( ) - startDate . getTime ( ) ;
}
}
long dateInDays = TimeUnit . DAYS . convert ( dateDiff , TimeUnit . MILLISECONDS ) ;
double cost = ( tenantCost . getCost ( ) / 365 ) * dateInDays ;
totalCost = cost + totalCost ;
device . setCost ( Math . round ( cost * 100.0 ) / 100.0 ) ;
device . setDaysUsed ( ( int ) dateInDays ) ;
if ( generateBill ) {
enrollmentDAO . updateEnrollmentLastBilledDate ( device . getEnrolmentInfo ( ) , endDate , tenantId ) ;
billingDAO . addBilling ( device . getId ( ) , tenantId , startDate , endDate ) ;
DeviceManagementDAOFactory . commitTransaction ( ) ;
}
} else {
} else {
if ( ! deviceStatus . isEmpty ( ) & & deviceStatus . get ( 0 ) . getStatus ( ) . equals ( "REMOVED" ) ) {
for ( int i = 0 ; i < deviceStatus . size ( ) ; i + + ) {
if ( deviceStatus . get ( 0 ) . getUpdateTime ( ) . getTime ( ) > = device . getEnrolmentInfo ( ) . getDateOfEnrolment ( ) ) {
if ( endDate . getTime ( ) > = deviceStatus . get ( i ) . getUpdateTime ( ) . getTime ( )
dateDiff = deviceStatus . get ( 0 ) . getUpdateTime ( ) . getTime ( ) - device . getEnrolmentInfo ( ) . getDateOfEnrolment ( ) ;
& & startDate . getTime ( ) < = deviceStatus . get ( i ) . getUpdateTime ( ) . getTime ( ) ) {
deviceStatusIsValid = true ;
}
}
if ( device . getEnrolmentInfo ( ) . getLastBilledDate ( ) ! = 0 ) {
Date date = new Date ( device . getEnrolmentInfo ( ) . getLastBilledDate ( ) ) ;
Format format = new SimpleDateFormat ( "yyyy MM dd" ) ;
for ( String lastBillDate : lastBilledDatesList ) {
if ( ! lastBillDate . equals ( format . format ( date ) ) ) {
lastBilledDatesList . add ( format . format ( date ) ) ;
}
}
}
} else if ( ! deviceStatus . isEmpty ( ) & & ! deviceStatus . get ( 0 ) . getStatus ( ) . equals ( "REMOVED" ) ) {
dateDiff = endDate . getTime ( ) - device . getEnrolmentInfo ( ) . getDateOfEnrolment ( ) ;
}
}
removeBillingPeriodInvalidDevices . add ( device ) ;
}
}
long dateInDays = TimeUnit . DAYS . convert ( dateDiff , TimeUnit . MILLISECONDS ) ;
if ( ! deviceStatusIsValid ) {
double cost = ( tenantCost . getCost ( ) / 365 ) * dateInDays ;
removeStatusUpdateInvalidDevices . add ( device ) ;
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 ) ;
}
}
}
}
}
}
}
}
}
}
} catch ( DeviceManagement DAO Exception e ) {
} catch ( DeviceManagementException e ) {
String msg = "Error occurred while retrieving device list pertaining to the current tenant ";
String msg = "Error occurred calculating cost of devices" ;
log . error ( msg , e ) ;
log . error ( msg , e ) ;
throw new DeviceManagementException ( msg , e ) ;
throw new DeviceManagementException ( msg , e ) ;
} catch ( Exception e ) {
} catch ( SQLException e ) {
String msg = "Error occurred in getAllDevices" ;
String msg = "Error when retrieving data" ;
log . error ( msg , e ) ;
throw new DeviceManagementException ( msg , e ) ;
} catch ( MetadataManagementDAOException e ) {
String msg = "Error when retrieving metadata of billing feature" ;
log . error ( msg , e ) ;
log . error ( msg , e ) ;
throw new DeviceManagementException ( msg , e ) ;
throw new DeviceManagementException ( msg , e ) ;
} finally {
} finally {
DeviceManagementDAOFactory . closeConnection ( ) ;
MetadataManagementDAOFactory . closeConnection ( ) ;
MetadataManagementDAOFactory . closeConnection ( ) ;
}
}
if ( ! removeBillingPeriodInvalidDevices . isEmpty ( ) & & removeBillingPeriodInvalidDevices . size ( ) = = allDevices . size ( ) ) {
if ( ! deviceStatusNotAvailable . isEmpty ( ) ) {
allDevicesBilledDateIsValid = false ;
allDevices . removeAll ( deviceStatusNotAvailable ) ;
paginationResult . setMessage ( "Invalid bill period." ) ;
}
}
if ( ! removeStatusUpdateInvalidDevices . isEmpty ( ) & & removeStatusUpdateInvalidDevices . size ( ) = = allDevices . size ( ) ) {
allDevicesBilledDateIsValid = false ;
if ( paginationResult . getMessage ( ) ! = null ) {
paginationResult . setMessage ( paginationResult . getMessage ( ) + " and no device updates within entered bill period." ) ;
} else {
paginationResult . setMessage ( "Devices have not been updated within the given period or entered end date comes before the " +
"last billed date." ) ;
}
}
Calendar calStart = Calendar . getInstance ( ) ;
allDevices . removeAll ( removeBillingPeriodInvalidDevices ) ;
Calendar calEnd = Calendar . getInstance ( ) ;
allDevices . removeAll ( removeStatusUpdateInvalidDevices ) ;
calStart . setTimeInMillis ( startDate . getTime ( ) ) ;
calEnd . setTimeInMillis ( endDate . getTime ( ) ) ;
paginationResult . setBilledDateIsValid ( allDevicesBilledDateIsValid ) ;
billingResponse . setDevice ( allDevices ) ;
paginationResult . setData ( allDevices ) ;
billingResponse . setYear ( String . valueOf ( calStart . get ( Calendar . YEAR ) ) ) ;
paginationResult . setTotalCost ( Math . round ( totalCost * 100.0 ) / 100.0 ) ;
billingResponse . setStartDate ( startDate . toString ( ) ) ;
return paginationResult ;
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
public PaginationResult getAllDevicesBillings ( PaginationRequest request , int tenantId , String tenantDomain , Timestamp startDate , Timestamp endDate , boolean generateBill ) throws DeviceManagementException {
public PaginationResult createBillingFile ( int tenantId , String tenantDomain , Timestamp startDate , Timestamp endDate ) throws DeviceManagementException {
if ( request = = null ) {
String msg = "Received incomplete pagination request for method getAllDeviceBillings" ;
log . error ( msg ) ;
throw new DeviceManagementException ( msg ) ;
}
DeviceManagerUtil . validateDeviceListPageSize ( request ) ;
PaginationResult paginationResult = new PaginationResult ( ) ;
PaginationResult paginationResult = new PaginationResult ( ) ;
List < Device > allDevices ;
List < Device > allDevices = new ArrayList < > ( ) ;
int count = 0 ;
List < BillingResponse > billingResponseList = new ArrayList < > ( ) ;
double totalCost = 0.0 ;
int deviceCount = 0 ;
Timestamp initialStartDate = startDate ;
boolean remainingDaysConsidered = false ;
try {
try {
DeviceManagementDAOFactory . beginTransaction ( ) ;
DeviceManagementDAOFactory . openConnection ( ) ;
allDevices = deviceDAO . getDevices ( request , tenantId ) ;
count = deviceDAO . getDeviceCount ( request , tenantId ) ;
paginationResult = calculateCost ( tenantId , tenantDomain , startDate , endDate , generateBill , allDevices ) ;
} catch ( DeviceManagementDAOException e ) {
// TODO Do the check of cache enabling here
String msg = "Error occurred while retrieving device bill list pertaining to the current tenant" ;
paginationResult = BillingCacheManagerImpl . getInstance ( ) . getBillingFromCache ( tenantDomain , startDate , endDate ) ;
log . error ( msg , e ) ;
if ( paginationResult = = null ) {
throw new DeviceManagementException ( msg , e ) ;
paginationResult = new PaginationResult ( ) ;
} catch ( Exception e ) {
long difference_In_Time = endDate . getTime ( ) - startDate . getTime ( ) ;
String msg = "Error occurred in getAllDevicesBillings" ;
log . error ( msg , e ) ;
long difference_In_Years = ( difference_In_Time / ( 1000L * 60 * 60 * 24 * 365 ) ) ;
throw new DeviceManagementException ( msg , e ) ;
}
long difference_In_Days = ( difference_In_Time / ( 1000 * 60 * 60 * 24 ) ) % 365 ;
paginationResult . setRecordsFiltered ( count ) ;
paginationResult . setRecordsTotal ( count ) ;
for ( int i = 1 ; i < = difference_In_Years ; i + + ) {
return paginationResult ;
List < Device > allDevicesPerYear = new ArrayList < > ( ) ;
}
LocalDateTime oneYearAfterStart = startDate . toLocalDateTime ( ) . plusYears ( 1 ) ;
Timestamp newStartDate ;
Timestamp newEndDate ;
if ( i = = difference_In_Years ) {
if ( difference_In_Days = = 0 | | Timestamp . valueOf ( oneYearAfterStart ) . getTime ( ) > = endDate . getTime ( ) ) {
remainingDaysConsidered = true ;
oneYearAfterStart = startDate . toLocalDateTime ( ) ;
newEndDate = endDate ;
} else if ( Timestamp . valueOf ( oneYearAfterStart ) . getTime ( ) > = endDate . getTime ( ) ) {
newEndDate = Timestamp . valueOf ( oneYearAfterStart ) ;
} else {
oneYearAfterStart = startDate . toLocalDateTime ( ) . plusYears ( 1 ) ;
newEndDate = Timestamp . valueOf ( oneYearAfterStart ) ;
}
} else {
oneYearAfterStart = startDate . toLocalDateTime ( ) . plusYears ( 1 ) ;
newEndDate = Timestamp . valueOf ( oneYearAfterStart ) ;
}
@Override
newStartDate = startDate ;
public PaginationResult createBillingFile ( int tenantId , String tenantDomain , Timestamp startDate , Timestamp endDate , boolean generateBill ) throws DeviceManagementException {
PaginationResult paginationResult = new PaginationResult ( ) ;
// The query returns devices which are enrolled in this year now in not removed state
List < Device > allDevices ;
allDevicesPerYear . addAll ( deviceDAO . getNonRemovedYearlyDeviceList ( tenantId , newStartDate , newEndDate ) ) ;
try {
DeviceManagementDAOFactory . beginTransaction ( ) ;
// The query returns devices which are enrolled in this year now in removed state
allDevices = deviceDAO . getDeviceListWithoutPagination ( tenantId ) ;
allDevicesPerYear . addAll ( deviceDAO . getRemovedYearlyDeviceList ( tenantId , newStartDate , newEndDate ) ) ;
paginationResult = calculateCost ( tenantId , tenantDomain , startDate , endDate , generateBill , allDevices ) ;
// The query returns devices which are enrolled prior this year now in not removed state
allDevicesPerYear . addAll ( deviceDAO . getNonRemovedPriorYearsDeviceList ( tenantId , newStartDate , newEndDate ) ) ;
// The query returns devices which are enrolled prior this year now in removed state
allDevicesPerYear . addAll ( deviceDAO . getRemovedPriorYearsDeviceList ( tenantId , newStartDate , newEndDate ) ) ;
BillingResponse billingResponse = calculateCost ( tenantDomain , newStartDate , newEndDate , allDevicesPerYear ) ;
billingResponseList . add ( billingResponse ) ;
allDevices . addAll ( billingResponse . getDevice ( ) ) ;
totalCost = totalCost + billingResponse . getTotalCostPerYear ( ) ;
deviceCount = deviceCount + billingResponse . getDeviceCount ( ) ;
LocalDateTime nextStartDate = oneYearAfterStart . plusDays ( 1 ) ;
startDate = Timestamp . valueOf ( nextStartDate ) ;
}
if ( difference_In_Days ! = 0 & & ! remainingDaysConsidered ) {
List < Device > allDevicesPerRemainingDays = new ArrayList < > ( ) ;
// The query returns devices which are enrolled in this year now in not removed state
allDevicesPerRemainingDays . addAll ( deviceDAO . getNonRemovedYearlyDeviceList ( tenantId , startDate , endDate ) ) ;
// The query returns devices which are enrolled in this year now in removed state
allDevicesPerRemainingDays . addAll ( deviceDAO . getRemovedYearlyDeviceList ( tenantId , startDate , endDate ) ) ;
// The query returns devices which are enrolled prior this year now in not removed state
allDevicesPerRemainingDays . addAll ( deviceDAO . getNonRemovedPriorYearsDeviceList ( tenantId , startDate , endDate ) ) ;
// The query returns devices which are enrolled prior this year now in removed state
allDevicesPerRemainingDays . addAll ( deviceDAO . getRemovedPriorYearsDeviceList ( tenantId , startDate , endDate ) ) ;
BillingResponse billingResponse = calculateCost ( tenantDomain , startDate , endDate , allDevicesPerRemainingDays ) ;
billingResponseList . add ( billingResponse ) ;
allDevices . addAll ( billingResponse . getDevice ( ) ) ;
totalCost = totalCost + billingResponse . getTotalCostPerYear ( ) ;
deviceCount = deviceCount + billingResponse . getDeviceCount ( ) ;
}
Calendar calStart = Calendar . getInstance ( ) ;
Calendar calEnd = Calendar . getInstance ( ) ;
calStart . setTimeInMillis ( initialStartDate . getTime ( ) ) ;
calEnd . setTimeInMillis ( endDate . getTime ( ) ) ;
BillingResponse billingResponse = new BillingResponse ( "all" , Math . round ( totalCost * 100.0 ) / 100.0 , allDevices , calStart . get ( Calendar . YEAR ) + " - " + calEnd . get ( Calendar . YEAR ) , initialStartDate . toString ( ) , endDate . toString ( ) , allDevices . size ( ) ) ;
billingResponseList . add ( billingResponse ) ;
paginationResult . setData ( billingResponseList ) ;
paginationResult . setTotalCost ( Math . round ( totalCost * 100.0 ) / 100.0 ) ;
paginationResult . setTotalDeviceCount ( deviceCount ) ;
BillingCacheManagerImpl . getInstance ( ) . addBillingToCache ( paginationResult , tenantDomain , initialStartDate , endDate ) ;
return paginationResult ;
}
} catch ( DeviceManagementDAOException e ) {
} catch ( DeviceManagementDAOException e ) {
String msg = "Error occurred while retrieving device bill list without pagination pertaining to the current tenant" ;
String msg = "Error occurred while retrieving device bill list related to the current tenant";
log . error ( msg , e ) ;
log . error ( msg , e ) ;
throw new DeviceManagementException ( msg , e ) ;
throw new DeviceManagementException ( msg , e ) ;
} catch ( Exception e ) {
} catch ( MetadataManagementDAO Exception e ) {
String msg = "Error occurred in createBillingFile" ;
String msg = "Error when retrieving metadata of billing featur e";
log . error ( msg , e ) ;
log . error ( msg , e ) ;
throw new DeviceManagementException ( msg , e ) ;
throw new DeviceManagementException ( msg , e ) ;
} catch ( SQLException e ) {
String msg = "Error occurred while opening a connection to the data source" ;
log . error ( msg , e ) ;
throw new DeviceManagementException ( msg , e ) ;
} finally {
DeviceManagementDAOFactory . closeConnection ( ) ;
}
}
return paginationResult ;
return paginationResult ;
}
}