@ -48,9 +48,6 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
@Override
@Override
public List < Device > getDevices ( PaginationRequest request , int tenantId )
public List < Device > getDevices ( PaginationRequest request , int tenantId )
throws DeviceManagementDAOException {
throws DeviceManagementDAOException {
Connection conn ;
PreparedStatement stmt = null ;
ResultSet rs = null ;
List < Device > devices ;
List < Device > devices ;
String deviceType = request . getDeviceType ( ) ;
String deviceType = request . getDeviceType ( ) ;
boolean isDeviceTypeProvided = false ;
boolean isDeviceTypeProvided = false ;
@ -62,33 +59,42 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
boolean isOwnerPatternProvided = false ;
boolean isOwnerPatternProvided = false ;
String ownership = request . getOwnership ( ) ;
String ownership = request . getOwnership ( ) ;
boolean isOwnershipProvided = false ;
boolean isOwnershipProvided = false ;
String status = request . getStatus ( ) ;
List< String> status List = request . getStatus List ( ) ;
boolean isStatusProvided = false ;
boolean isStatusProvided = false ;
String excludeStatus = request . getExcludeStatus ( ) ;
boolean isExcludeStatusProvided = false ;
Date since = request . getSince ( ) ;
Date since = request . getSince ( ) ;
boolean isSinceProvided = false ;
boolean isSinceProvided = false ;
try {
conn = this . getConnection ( ) ;
String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, " +
"d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE " +
"FROM DM_DEVICE d, DM_DEVICE_TYPE t " ;
try {
Connection conn = getConnection ( ) ;
String sql = "SELECT d1.ID AS DEVICE_ID, " +
"d1.DESCRIPTION, " +
"d1.NAME AS DEVICE_NAME, " +
"d1.DEVICE_TYPE, " +
"d1.DEVICE_IDENTIFICATION, " +
"e.OWNER, " +
"e.OWNERSHIP, " +
"e.STATUS, " +
"e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT, " +
"e.ID AS ENROLMENT_ID " +
"FROM DM_ENROLMENT e, " +
"(SELECT d.ID, " +
"d.DESCRIPTION, " +
"d.NAME, " +
"d.DEVICE_IDENTIFICATION, " +
"t.NAME AS DEVICE_TYPE " +
"FROM DM_DEVICE d, " +
"DM_DEVICE_TYPE t " ;
//Add the query to filter active devices on timestamp
//Add the query to filter active devices on timestamp
if ( since ! = null ) {
if ( since ! = null ) {
sql = sql + ", DM_DEVICE_DETAIL dt" ;
sql = sql + ", DM_DEVICE_DETAIL dt" ;
isSinceProvided = true ;
isSinceProvided = true ;
}
}
sql = sql + " WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?" ;
sql = sql + " WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?" ;
//Add query for last updated timestamp
//Add query for last updated timestamp
if ( isSinceProvided ) {
if ( isSinceProvided ) {
sql = sql + " AND dt.DEVICE_ID = d.ID AND dt.UPDATE_TIMESTAMP > ?" ;
sql = sql + " AND dt.DEVICE_ID = d.ID AND dt.UPDATE_TIMESTAMP > ?" ;
}
}
//Add the query for device-type
//Add the query for device-type
if ( deviceType ! = null & & ! deviceType . isEmpty ( ) ) {
if ( deviceType ! = null & & ! deviceType . isEmpty ( ) ) {
sql = sql + " AND t.NAME = ?" ;
sql = sql + " AND t.NAME = ?" ;
@ -99,9 +105,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
sql = sql + " AND d.NAME LIKE ?" ;
sql = sql + " AND d.NAME LIKE ?" ;
isDeviceNameProvided = true ;
isDeviceNameProvided = true ;
}
}
sql = sql + ") d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?" ;
sql = sql + ") d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?" ;
//Add the query for ownership
//Add the query for ownership
if ( ownership ! = null & & ! ownership . isEmpty ( ) ) {
if ( ownership ! = null & & ! ownership . isEmpty ( ) ) {
sql = sql + " AND e.OWNERSHIP = ?" ;
sql = sql + " AND e.OWNERSHIP = ?" ;
@ -115,73 +119,62 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
sql = sql + " AND e.OWNER LIKE ?" ;
sql = sql + " AND e.OWNER LIKE ?" ;
isOwnerPatternProvided = true ;
isOwnerPatternProvided = true ;
}
}
//Add the query for status
if ( statusList ! = null & & ! statusList . isEmpty ( ) ) {
if ( status ! = null & & ! status . isEmpty ( ) ) {
sql + = buildStatusQuery ( statusList ) ;
sql = sql + " AND e.STATUS = ?" ;
isStatusProvided = true ;
isStatusProvided = true ;
}
}
//Add the query for exclude status
if ( excludeStatus ! = null & & ! excludeStatus . isEmpty ( ) ) {
sql = sql + " AND e.STATUS != ?" ;
isExcludeStatusProvided = true ;
}
sql = sql + " LIMIT ?,?" ;
sql = sql + " LIMIT ?,?" ;
stmt = conn . prepareStatement ( sql ) ;
try ( PreparedStatement stmt = conn . prepareStatement ( sql ) ) {
stmt . setInt ( 1 , tenantId ) ;
int paramIdx = 1 ;
int paramIdx = 2 ;
stmt . setInt ( paramIdx + + , tenantId ) ;
if ( isSinceProvided ) {
if ( isSinceProvided ) {
stmt . setLong ( paramIdx + + , since . getTime ( ) ) ;
stmt . setLong ( paramIdx + + , since . getTime ( ) ) ;
}
}
if ( isDeviceTypeProvided ) {
if ( isDeviceTypeProvided ) {
stmt . setString ( paramIdx + + , deviceType ) ;
stmt . setString ( paramIdx + + , deviceType ) ;
}
}
if ( isDeviceNameProvided ) {
if ( isDeviceNameProvided ) {
stmt . setString ( paramIdx + + , deviceName + "%" ) ;
stmt . setString ( paramIdx + + , deviceName + "%" ) ;
}
}
stmt . setInt ( paramIdx + + , tenantId ) ;
if ( isOwnershipProvided ) {
stmt . setString ( paramIdx + + , ownership ) ;
}
if ( isOwnerProvided ) {
stmt . setString ( paramIdx + + , owner ) ;
} else if ( isOwnerPatternProvided ) {
stmt . setString ( paramIdx + + , ownerPattern + "%" ) ;
}
if ( isStatusProvided ) {
for ( String status : statusList ) {
stmt . setString ( paramIdx + + , status ) ;
}
}
stmt . setInt ( paramIdx + + , request . getStartIndex ( ) ) ;
stmt . setInt ( paramIdx , request . getRowCount ( ) ) ;
stmt . setInt ( paramIdx + + , tenantId ) ;
try ( ResultSet rs = stmt . executeQuery ( ) ) {
if ( isOwnershipProvided ) {
devices = new ArrayList < > ( ) ;
stmt . setString ( paramIdx + + , ownership ) ;
while ( rs . next ( ) ) {
}
Device device = DeviceManagementDAOUtil . loadDevice ( rs ) ;
if ( isOwnerProvided ) {
devices . add ( device ) ;
stmt . setString ( paramIdx + + , owner ) ;
}
} else if ( isOwnerPatternProvided ) {
return devices ;
stmt . setString ( paramIdx + + , ownerPattern + "%" ) ;
}
}
if ( isStatusProvided ) {
stmt . setString ( paramIdx + + , status ) ;
}
if ( isExcludeStatusProvided ) {
stmt . setString ( paramIdx + + , excludeStatus ) ;
}
stmt . setInt ( paramIdx + + , request . getStartIndex ( ) ) ;
stmt . setInt ( paramIdx , request . getRowCount ( ) ) ;
rs = stmt . executeQuery ( ) ;
devices = new ArrayList < > ( ) ;
while ( rs . next ( ) ) {
Device device = DeviceManagementDAOUtil . loadDevice ( rs ) ;
devices . add ( device ) ;
}
}
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw new DeviceManagementDAOException ( "Error occurred while retrieving information of all " +
String msg = "Error occurred while retrieving information of all " +
"registered devices" , e ) ;
"registered devices" ;
} finally {
log . error ( msg , e ) ;
DeviceManagementDAOUtil . cleanupResources ( stmt , rs ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
}
}
return devices ;
}
}
@Override
@Override
public List < Device > searchDevicesInGroup ( PaginationRequest request , int tenantId )
public List < Device > searchDevicesInGroup ( PaginationRequest request , int tenantId )
throws DeviceManagementDAOException {
throws DeviceManagementDAOException {
Connection conn ;
PreparedStatement stmt = null ;
ResultSet rs = null ;
List < Device > devices = null ;
List < Device > devices = null ;
int groupId = request . getGroupId ( ) ;
int groupId = request . getGroupId ( ) ;
String deviceType = request . getDeviceType ( ) ;
String deviceType = request . getDeviceType ( ) ;
boolean isDeviceTypeProvided = false ;
boolean isDeviceTypeProvided = false ;
@ -193,52 +186,63 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
boolean isOwnerPatternProvided = false ;
boolean isOwnerPatternProvided = false ;
String ownership = request . getOwnership ( ) ;
String ownership = request . getOwnership ( ) ;
boolean isOwnershipProvided = false ;
boolean isOwnershipProvided = false ;
String status = request . getStatus ( ) ;
List< String> status List = request . getStatus List ( ) ;
boolean isStatusProvided = false ;
boolean isStatusProvided = false ;
String excludeStatus = request . getExcludeStatus ( ) ;
boolean isExcludeStatusProvided = false ;
Date since = request . getSince ( ) ;
Date since = request . getSince ( ) ;
boolean isSinceProvided = false ;
boolean isSinceProvided = false ;
try {
try {
conn = this . getConnection ( ) ;
Connection conn = getConnection ( ) ;
String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
String sql = "SELECT d1.DEVICE_ID, " +
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
"d1.DESCRIPTION, " +
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, " +
"d1.NAME AS DEVICE_NAME, " +
"(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE " +
"d1.DEVICE_TYPE, " +
"FROM (SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID " +
"d1.DEVICE_IDENTIFICATION, " +
"FROM DM_DEVICE d, (SELECT dgm.DEVICE_ID FROM DM_DEVICE_GROUP_MAP dgm WHERE dgm.GROUP_ID = ?) dgm1 WHERE" +
"e.OWNER, " +
" d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?" ;
"e.OWNERSHIP, " +
"e.STATUS, " +
"e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT, " +
"e.ID AS ENROLMENT_ID " +
"FROM DM_ENROLMENT e, " +
"(SELECT gd.DEVICE_ID, " +
"gd.DESCRIPTION, " +
"gd.NAME, " +
"gd.DEVICE_IDENTIFICATION, " +
"t.NAME AS DEVICE_TYPE " +
"FROM " +
"(SELECT d.ID AS DEVICE_ID, " +
"d.DESCRIPTION, " +
"d.NAME, " +
"d.DEVICE_IDENTIFICATION, " +
"d.DEVICE_TYPE_ID " +
"FROM DM_DEVICE d, " +
"(SELECT dgm.DEVICE_ID " +
"FROM DM_DEVICE_GROUP_MAP dgm " +
"WHERE dgm.GROUP_ID = ?) dgm1 " +
"WHERE d.ID = dgm1.DEVICE_ID " +
"AND d.TENANT_ID = ?" ;
//Add the query for device-name
//Add the query for device-name
if ( deviceName ! = null & & ! deviceName . isEmpty ( ) ) {
if ( deviceName ! = null & & ! deviceName . isEmpty ( ) ) {
sql = sql + " AND d.NAME LIKE ?" ;
sql = sql + " AND d.NAME LIKE ?" ;
isDeviceNameProvided = true ;
isDeviceNameProvided = true ;
}
}
sql = sql + ") gd, DM_DEVICE_TYPE t" ;
sql = sql + ") gd, DM_DEVICE_TYPE t" ;
if ( since ! = null ) {
if ( since ! = null ) {
sql = sql + ", DM_DEVICE_DETAIL dt" ;
sql = sql + ", DM_DEVICE_DETAIL dt" ;
isSinceProvided = true ;
isSinceProvided = true ;
}
}
sql = sql + " WHERE gd.DEVICE_TYPE_ID = t.ID" ;
sql = sql + " WHERE gd.DEVICE_TYPE_ID = t.ID" ;
//Add query for last updated timestamp
//Add query for last updated timestamp
if ( isSinceProvided ) {
if ( isSinceProvided ) {
sql = sql + " AND dt.DEVICE_ID = gd.DEVICE_ID AND dt.UPDATE_TIMESTAMP > ?" ;
sql = sql + " AND dt.DEVICE_ID = gd.DEVICE_ID AND dt.UPDATE_TIMESTAMP > ?" ;
}
}
//Add the query for device-type
//Add the query for device-type
if ( deviceType ! = null & & ! deviceType . isEmpty ( ) ) {
if ( deviceType ! = null & & ! deviceType . isEmpty ( ) ) {
sql = sql + " AND t.NAME = ?" ;
sql = sql + " AND t.NAME = ?" ;
isDeviceTypeProvided = true ;
isDeviceTypeProvided = true ;
}
}
sql = sql + " ) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? " ;
sql = sql + " ) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? " ;
//Add the query for ownership
//Add the query for ownership
if ( ownership ! = null & & ! ownership . isEmpty ( ) ) {
if ( ownership ! = null & & ! ownership . isEmpty ( ) ) {
sql = sql + " AND e.OWNERSHIP = ?" ;
sql = sql + " AND e.OWNERSHIP = ?" ;
@ -252,66 +256,57 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
sql = sql + " AND e.OWNER LIKE ?" ;
sql = sql + " AND e.OWNER LIKE ?" ;
isOwnerPatternProvided = true ;
isOwnerPatternProvided = true ;
}
}
//Add the query for status
if ( statusList ! = null & & ! statusList . isEmpty ( ) ) {
if ( status ! = null & & ! status . isEmpty ( ) ) {
sql + = buildStatusQuery ( statusList ) ;
sql = sql + " AND e.STATUS = ?" ;
isStatusProvided = true ;
isStatusProvided = true ;
}
}
//Add the query for exclude status
if ( excludeStatus ! = null & & ! excludeStatus . isEmpty ( ) ) {
sql = sql + " AND e.STATUS != ?" ;
isExcludeStatusProvided = true ;
}
sql = sql + " LIMIT ?,?" ;
sql = sql + " LIMIT ?,?" ;
stmt = conn . prepareStatement ( sql ) ;
try ( PreparedStatement stmt = conn . prepareStatement ( sql ) ) {
int paramIdx = 1 ;
stmt . setInt ( 1 , groupId ) ;
stmt . setInt ( paramIdx + + , groupId ) ;
stmt . setInt ( 2 , tenantId ) ;
stmt . setInt ( paramIdx + + , tenantId ) ;
if ( isDeviceNameProvided ) {
int paramIdx = 3 ;
stmt . setString ( paramIdx + + , deviceName + "%" ) ;
if ( isDeviceNameProvided ) {
}
stmt . setString ( paramIdx + + , deviceName + "%" ) ;
if ( isSinceProvided ) {
}
stmt . setLong ( paramIdx + + , since . getTime ( ) ) ;
if ( isSinceProvided ) {
}
stmt . setLong ( paramIdx + + , since . getTime ( ) ) ;
if ( isDeviceTypeProvided ) {
}
stmt . setString ( paramIdx + + , deviceType ) ;
if ( isDeviceTypeProvided ) {
}
stmt . setString ( paramIdx + + , deviceType ) ;
stmt . setInt ( paramIdx + + , tenantId ) ;
}
if ( isOwnershipProvided ) {
stmt . setString ( paramIdx + + , ownership ) ;
stmt . setInt ( paramIdx + + , tenantId ) ;
}
if ( isOwnershipProvided ) {
if ( isOwnerProvided ) {
stmt . setString ( paramIdx + + , ownership ) ;
stmt . setString ( paramIdx + + , owner ) ;
}
} else if ( isOwnerPatternProvided ) {
if ( isOwnerProvided ) {
stmt . setString ( paramIdx + + , ownerPattern + "%" ) ;
stmt . setString ( paramIdx + + , owner ) ;
}
} else if ( isOwnerPatternProvided ) {
if ( isStatusProvided ) {
stmt . setString ( paramIdx + + , ownerPattern + "%" ) ;
for ( String status : statusList ) {
}
stmt . setString ( paramIdx + + , status ) ;
if ( isStatusProvided ) {
}
stmt . setString ( paramIdx + + , status ) ;
}
}
stmt . setInt ( paramIdx + + , request . getStartIndex ( ) ) ;
if ( isExcludeStatusProvided ) {
stmt . setInt ( paramIdx , request . getRowCount ( ) ) ;
stmt . setString ( paramIdx + + , excludeStatus ) ;
}
stmt . setInt ( paramIdx + + , request . getStartIndex ( ) ) ;
stmt . setInt ( paramIdx , request . getRowCount ( ) ) ;
rs = stmt . executeQuery ( ) ;
try ( ResultSet rs = stmt . executeQuery ( ) ) {
devices = new ArrayList < > ( ) ;
devices = new ArrayList < > ( ) ;
while ( rs . next ( ) ) {
while ( rs . next ( ) ) {
Device device = DeviceManagementDAOUtil . loadDevice ( rs ) ;
Device device = DeviceManagementDAOUtil . loadDevice ( rs ) ;
devices . add ( device ) ;
devices . add ( device ) ;
}
return devices ;
}
}
}
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw new DeviceManagementDAOException ( "Error occurred while retrieving information of" +
String msg = "Error occurred while retrieving information of" +
" devices belonging to group : " + groupId , e ) ;
" devices belonging to group : " + groupId ;
} finally {
log . error ( msg , e ) ;
DeviceManagementDAOUtil . cleanupResources ( stmt , rs ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
}
}
return devices ;
}
}
@Override
@Override
@ -421,46 +416,81 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
@Override
@Override
public List < Device > getDevicesByStatus ( PaginationRequest request , int tenantId )
public List < Device > getDevicesByStatus ( PaginationRequest request , int tenantId )
throws DeviceManagementDAOException {
throws DeviceManagementDAOException {
Connection conn ;
PreparedStatement stmt = null ;
List < Device > devices = new ArrayList < > ( ) ;
List < Device > devices = new ArrayList < > ( ) ;
List < String > statusList = request . getStatusList ( ) ;
try {
try {
conn = this . getConnection ( ) ;
Connection conn = getConnection ( ) ;
String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, t.NAME AS DEVICE_TYPE, " +
String sql = "SELECT d.ID AS DEVICE_ID, " +
"d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
"d.DESCRIPTION, " +
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " +
"d.NAME AS DEVICE_NAME, " +
"e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e " +
"t.NAME AS DEVICE_TYPE, " +
"WHERE TENANT_ID = ? AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t " +
"d.DEVICE_IDENTIFICATION, " +
"WHERE DEVICE_ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? LIMIT ?,?" ;
"e.OWNER, " +
stmt = conn . prepareStatement ( sql ) ;
"e.OWNERSHIP, " +
stmt . setInt ( 1 , tenantId ) ;
"e.STATUS, " +
stmt . setString ( 2 , request . getStatus ( ) ) ;
"e.DATE_OF_LAST_UPDATE, " +
stmt . setInt ( 3 , tenantId ) ;
"e.DATE_OF_ENROLMENT, " +
stmt . setInt ( 4 , request . getStartIndex ( ) ) ;
"e.ID AS ENROLMENT_ID " +
stmt . setInt ( 5 , request . getRowCount ( ) ) ;
"FROM " +
ResultSet rs = stmt . executeQuery ( ) ;
"(SELECT e.ID, " +
"e.DEVICE_ID, " +
"e.OWNER, " +
"e.OWNERSHIP, " +
"e.STATUS, " +
"e.DATE_OF_ENROLMENT, " +
"e.DATE_OF_LAST_UPDATE, " +
"e.ID AS ENROLMENT_ID " +
"FROM DM_ENROLMENT e " +
"WHERE TENANT_ID = ?" ;
if ( statusList = = null | | statusList . isEmpty ( ) ) {
String msg = "Error occurred while fetching the list of devices. Status List can't " +
"be null or empty" ;
log . error ( msg ) ;
throw new DeviceManagementDAOException ( msg ) ;
}
sql + = buildStatusQuery ( statusList ) ;
sql + = ") e, " +
"DM_DEVICE d, " +
"DM_DEVICE_TYPE t " +
"WHERE DEVICE_ID = e.DEVICE_ID " +
"AND d.DEVICE_TYPE_ID = t.ID " +
"AND d.TENANT_ID = ? " +
"LIMIT ?,?" ;
while ( rs . next ( ) ) {
try ( PreparedStatement stmt = conn . prepareStatement ( sql ) ) {
Device device = DeviceManagementDAOUtil . loadDevice ( rs ) ;
int paramIdx = 1 ;
devices . add ( device ) ;
stmt . setInt ( paramIdx + + , tenantId ) ;
for ( String status : statusList ) {
stmt . setString ( paramIdx + + , status ) ;
}
stmt . setInt ( paramIdx + + , tenantId ) ;
stmt . setInt ( paramIdx + + , request . getStartIndex ( ) ) ;
stmt . setInt ( paramIdx , request . getRowCount ( ) ) ;
try ( ResultSet rs = stmt . executeQuery ( ) ) {
while ( rs . next ( ) ) {
Device device = DeviceManagementDAOUtil . loadDevice ( rs ) ;
devices . add ( device ) ;
}
return devices ;
}
}
}
} catch ( SQLException e ) {
} catch ( SQLException e ) {
String msg = "Error occurred while fetching the list of devices that matches to status " +
String msg = "Error occurred while fetching the list of devices that matches to status " +
"'" + request . getStatus ( ) + "'" ;
request . getStatusList ( ) . toString ( ) ;
log . error ( msg , e ) ;
log . error ( msg , e ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
} finally {
DeviceManagementDAOUtil . cleanupResources ( stmt , null ) ;
}
}
return devices ;
}
}
@Override
@Override
public List < Device > getDevicesByDuration ( PaginationRequest request , List < String > statusList , int tenantId ,
public List < Device > getDevicesByDuration ( PaginationRequest request , int tenantId ,
String fromDate , String toDate )
String fromDate , String toDate )
throws DeviceManagementDAOException {
throws DeviceManagementDAOException {
List < Device > devices ;
List < Device > devices ;
String ownership = request . getOwnership ( ) ;
String ownership = request . getOwnership ( ) ;
List < String > statusList = request . getStatusList ( ) ;
boolean isStatusProvided = false ;
boolean isStatusProvided = false ;
String sql = "SELECT " +
String sql = "SELECT " +
@ -479,20 +509,13 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
"d.DEVICE_TYPE_ID = t.ID AND " +
"d.DEVICE_TYPE_ID = t.ID AND " +
"e.TENANT_ID = ? AND " +
"e.TENANT_ID = ? AND " +
"e.DATE_OF_ENROLMENT BETWEEN ? AND ?" ;
"e.DATE_OF_ENROLMENT BETWEEN ? AND ?" ;
if ( statusList ! = null & & ! statusList . isEmpty ( ) ) {
//Add the query for status
sql + = buildStatusQuery ( statusList ) ;
StringBuilder sqlBuilder = new StringBuilder ( sql ) ;
isStatusProvided = buildStatusQuery ( statusList , sqlBuilder ) ;
sql = sqlBuilder . toString ( ) ;
if ( statusList ! = null & & ! statusList . isEmpty ( ) ) {
isStatusProvided = true ;
isStatusProvided = true ;
}
}
if ( ownership ! = null ) {
if ( ownership ! = null ) {
sql = sql + " AND e.OWNERSHIP = ?" ;
sql = sql + " AND e.OWNERSHIP = ?" ;
}
}
sql = sql + " LIMIT ?,?" ;
sql = sql + " LIMIT ?,?" ;
try ( Connection conn = this . getConnection ( ) ;
try ( Connection conn = this . getConnection ( ) ;
@ -511,70 +534,74 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
}
stmt . setInt ( paramIdx + + , request . getStartIndex ( ) ) ;
stmt . setInt ( paramIdx + + , request . getStartIndex ( ) ) ;
stmt . setInt ( paramIdx , request . getRowCount ( ) ) ;
stmt . setInt ( paramIdx , request . getRowCount ( ) ) ;
try ( ResultSet rs = stmt . executeQuery ( ) ) {
try ( ResultSet rs = stmt . executeQuery ( ) ) {
devices = new ArrayList < > ( ) ;
devices = new ArrayList < > ( ) ;
while ( rs . next ( ) ) {
while ( rs . next ( ) ) {
Device device = DeviceManagementDAOUtil . loadDevice ( rs ) ;
Device device = DeviceManagementDAOUtil . loadDevice ( rs ) ;
devices . add ( device ) ;
devices . add ( device ) ;
}
}
return devices ;
}
}
} catch ( SQLException e ) {
} catch ( SQLException e ) {
String msg = "Error occurred while retrieving information of all " +
String msg = "Error occurred while retrieving information of all " +
"registered devices under tenant id " + tenantId ;
"registered devices under tenant id " + tenantId ;
log . error ( msg , e ) ;
log . error ( msg , e ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
}
}
return devices ;
}
}
@Override
@Override
public int getDevicesByDurationCount ( List < String > statusList , String ownership , String fromDate , String toDate , int tenantId ) throws DeviceManagementDAOException {
public int getDevicesByDurationCount (
List < String > statusList , String ownership , String fromDate , String toDate , int tenantId )
throws DeviceManagementDAOException {
int deviceCount = 0 ;
int deviceCount = 0 ;
boolean isStatusProvided ;
boolean isStatusProvided = false ;
String sql = "SELECT " +
"COUNT(d.ID) AS DEVICE_COUNT " +
"FROM DM_DEVICE AS d , DM_ENROLMENT AS e , DM_DEVICE_TYPE AS t " +
"WHERE d.ID = e.DEVICE_ID AND " +
"d.DEVICE_TYPE_ID = t.ID AND " +
"e.TENANT_ID = ? AND " +
"e.DATE_OF_ENROLMENT BETWEEN ? AND ?" ;
//Add the query for status
String sql = "SELECT " +
StringBuilder sqlBuilder = new StringBuilder ( sql ) ;
"COUNT(d.ID) AS DEVICE_COUNT " +
isStatusProvided = buildStatusQuery ( statusList , sqlBuilder ) ;
"FROM DM_DEVICE AS d , " +
sql = sqlBuilder . toString ( ) ;
"DM_ENROLMENT AS e , " +
"DM_DEVICE_TYPE AS t " +
"WHERE d.ID = e.DEVICE_ID " +
"AND d.DEVICE_TYPE_ID = t.ID " +
"AND e.TENANT_ID = ? " +
"AND e.DATE_OF_ENROLMENT BETWEEN ? AND ?" ;
if ( statusList ! = null & & ! statusList . isEmpty ( ) ) {
sql + = buildStatusQuery ( statusList ) ;
isStatusProvided = true ;
}
if ( ownership ! = null ) {
sql = sql + " AND e.OWNERSHIP = ?" ;
}
try ( Connection conn = this . getConnection ( ) ;
PreparedStatement stmt = conn . prepareStatement ( sql ) ) {
int paramIdx = 1 ;
stmt . setInt ( paramIdx + + , tenantId ) ;
stmt . setString ( paramIdx + + , fromDate ) ;
stmt . setString ( paramIdx + + , toDate ) ;
if ( isStatusProvided ) {
for ( String status : statusList ) {
stmt . setString ( paramIdx + + , status ) ;
}
}
if ( ownership ! = null ) {
if ( ownership ! = null ) {
sql = sql + " AND e.OWNERSHIP = ?" ;
s tmt. setString ( paramIdx , ownership ) ;
}
}
try ( Connection conn = this . getConnection ( ) ;
try ( ResultSet rs = stmt . executeQuery ( ) ) {
PreparedStatement stmt = conn . prepareStatement ( sql ) ) {
if ( rs . next ( ) ) {
int paramIdx = 1 ;
deviceCount = rs . getInt ( "DEVICE_COUNT" ) ;
stmt . setInt ( paramIdx + + , tenantId ) ;
stmt . setString ( paramIdx + + , fromDate ) ;
stmt . setString ( paramIdx + + , toDate ) ;
if ( isStatusProvided ) {
for ( String status : statusList ) {
stmt . setString ( paramIdx + + , status ) ;
}
}
if ( ownership ! = null ) {
stmt . setString ( paramIdx + + , ownership ) ;
}
try ( ResultSet rs = stmt . executeQuery ( ) ) {
if ( rs . next ( ) ) {
deviceCount = rs . getInt ( "DEVICE_COUNT" ) ;
}
}
}
} catch ( SQLException e ) {
return deviceCount ;
String msg = "Error occurred while retrieving information of all " +
"registered devices under tenant id " + tenantId ;
log . error ( msg , e ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
}
}
return deviceCount ;
} catch ( SQLException e ) {
String msg = "Error occurred while retrieving information of all " +
"registered devices under tenant id " + tenantId ;
log . error ( msg , e ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
}
}
}
@Override
@Override
@ -583,7 +610,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
throws DeviceManagementDAOException {
throws DeviceManagementDAOException {
List < Count > countList = new ArrayList < > ( ) ;
List < Count > countList = new ArrayList < > ( ) ;
String ownership = request . getOwnership ( ) ;
String ownership = request . getOwnership ( ) ;
boolean isStatusProvided ;
boolean isStatusProvided = false ;
String sql =
String sql =
"SELECT " +
"SELECT " +
@ -597,9 +624,10 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
"BETWEEN ? AND ? " ;
"BETWEEN ? AND ? " ;
//Add the query for status
//Add the query for status
StringBuilder sqlBuilder = new StringBuilder ( sql ) ;
if ( statusList ! = null & & ! statusList . isEmpty ( ) ) {
isStatusProvided = buildStatusQuery ( statusList , sqlBuilder ) ;
sql + = buildStatusQuery ( statusList ) ;
sql = sqlBuilder . toString ( ) ;
isStatusProvided = true ;
}
if ( ownership ! = null ) {
if ( ownership ! = null ) {
sql = sql + " AND e.OWNERSHIP = ?" ;
sql = sql + " AND e.OWNERSHIP = ?" ;
@ -644,23 +672,6 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
return countList ;
return countList ;
}
}
protected boolean buildStatusQuery ( List < String > statusList , StringBuilder sqlBuilder ) {
if ( statusList ! = null & & ! statusList . isEmpty ( ) & & ! statusList . get ( 0 ) . isEmpty ( ) ) {
sqlBuilder . append ( " AND e.STATUS IN(" ) ;
for ( int i = 0 ; i < statusList . size ( ) ; i + + ) {
sqlBuilder . append ( "?" ) ;
if ( i ! = statusList . size ( ) - 1 ) {
sqlBuilder . append ( "," ) ;
}
}
sqlBuilder . append ( ")" ) ;
return true ;
} else {
return false ;
}
}
/ * *
/ * *
* Get the list of devices that matches with the given device name and ( or ) device type .
* Get the list of devices that matches with the given device name and ( or ) device type .
*
*