@ -1486,6 +1486,146 @@ public class GenericOperationDAOImpl implements OperationDAO {
return operations ;
}
@Override
public List < ? extends Operation > getOperationsForDeviceByDeviceIdentifier ( DeviceIdentifier deviceId , PaginationRequest request )
throws OperationManagementDAOException {
Operation operation ;
List < Operation > operations = new ArrayList < > ( ) ;
String createdTo = null ;
String createdFrom = null ;
DateFormat simple = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss.SSS" ) ;
boolean isCreatedDayProvided = false ;
boolean isUpdatedDayProvided = false ; //updated day = received day
boolean isOperationCodeProvided = false ;
boolean isStatusProvided = false ;
if ( request . getOperationLogFilters ( ) . getCreatedDayFrom ( ) ! = null ) {
createdFrom = simple . format ( request . getOperationLogFilters ( ) . getCreatedDayFrom ( ) ) ;
}
if ( request . getOperationLogFilters ( ) . getCreatedDayTo ( ) ! = null ) {
createdTo = simple . format ( request . getOperationLogFilters ( ) . getCreatedDayTo ( ) ) ;
}
Long updatedFrom = request . getOperationLogFilters ( ) . getUpdatedDayFrom ( ) ;
Long updatedTo = request . getOperationLogFilters ( ) . getUpdatedDayTo ( ) ;
List < String > operationCode = request . getOperationLogFilters ( ) . getOperationCode ( ) ;
List < String > status = request . getOperationLogFilters ( ) . getStatus ( ) ;
StringBuilder sql = new StringBuilder ( "SELECT " +
"o.ID, " +
"TYPE, " +
"o.CREATED_TIMESTAMP, " +
"o.RECEIVED_TIMESTAMP, " +
"o.OPERATION_CODE, " +
"o.INITIATED_BY, " +
"om.STATUS, " +
"om.ID AS OM_MAPPING_ID, " +
"om.UPDATED_TIMESTAMP " +
"FROM " +
"DM_OPERATION o " +
"INNER JOIN " +
"(SELECT dm.OPERATION_ID, " +
"dm.ID, " +
"dm.STATUS, " +
"dm.UPDATED_TIMESTAMP " +
"FROM " +
"DM_ENROLMENT_OP_MAPPING dm " +
"WHERE " +
"dm.DEVICE_IDENTIFICATION = ?" ) ;
if ( updatedFrom ! = null & & updatedFrom ! = 0 & & updatedTo ! = null & & updatedTo ! = 0 ) {
sql . append ( " AND dm.UPDATED_TIMESTAMP BETWEEN ? AND ?" ) ;
isUpdatedDayProvided = true ;
}
sql . append ( ") om ON o.ID = om.OPERATION_ID " ) ;
if ( createdFrom ! = null & & ! createdFrom . isEmpty ( ) & & createdTo ! = null & & ! createdTo . isEmpty ( ) ) {
sql . append ( " WHERE o.CREATED_TIMESTAMP BETWEEN ? AND ?" ) ;
isCreatedDayProvided = true ;
}
if ( ( isCreatedDayProvided ) & & ( status ! = null & & ! status . isEmpty ( ) ) ) {
int size = status . size ( ) ;
sql . append ( " AND (om.STATUS = ? " ) ;
for ( int i = 0 ; i < size - 1 ; i + + ) {
sql . append ( " OR om.STATUS = ?" ) ;
}
sql . append ( ")" ) ;
isStatusProvided = true ;
} else if ( ( ! isCreatedDayProvided ) & & ( status ! = null & & ! status . isEmpty ( ) ) ) {
int size = status . size ( ) ;
sql . append ( " WHERE (om.STATUS = ? " ) ;
for ( int i = 0 ; i < size - 1 ; i + + ) {
sql . append ( " OR om.STATUS = ?" ) ;
}
sql . append ( ")" ) ;
isStatusProvided = true ;
}
if ( ( isCreatedDayProvided | | isStatusProvided ) & & ( operationCode ! = null & & ! operationCode . isEmpty ( ) ) ) {
int size = operationCode . size ( ) ;
sql . append ( " AND (o.OPERATION_CODE = ? " ) ;
for ( int i = 0 ; i < size - 1 ; i + + ) {
sql . append ( " OR o.OPERATION_CODE = ?" ) ;
}
sql . append ( ")" ) ;
isOperationCodeProvided = true ;
} else if ( ( ! isCreatedDayProvided & & ! isStatusProvided ) & & ( operationCode ! = null & & ! operationCode . isEmpty ( ) ) ) {
int size = operationCode . size ( ) ;
sql . append ( " WHERE (o.OPERATION_CODE = ? " ) ;
for ( int i = 0 ; i < size - 1 ; i + + ) {
sql . append ( " OR o.OPERATION_CODE = ?" ) ;
}
sql . append ( ")" ) ;
isOperationCodeProvided = true ;
}
sql . append ( " ORDER BY o.CREATED_TIMESTAMP DESC LIMIT ?,?" ) ;
try {
Connection conn = OperationManagementDAOFactory . getConnection ( ) ;
try ( PreparedStatement stmt = conn . prepareStatement ( sql . toString ( ) ) ) {
int paramIndex = 1 ;
stmt . setString ( paramIndex + + , deviceId . getId ( ) ) ;
if ( isUpdatedDayProvided ) {
stmt . setLong ( paramIndex + + , updatedFrom ) ;
stmt . setLong ( paramIndex + + , updatedTo ) ;
}
if ( isCreatedDayProvided ) {
stmt . setString ( paramIndex + + , createdFrom ) ;
stmt . setString ( paramIndex + + , createdTo ) ;
}
if ( isStatusProvided ) {
for ( String s : status ) {
stmt . setString ( paramIndex + + , s ) ;
}
}
if ( isOperationCodeProvided ) {
for ( String s : operationCode ) {
stmt . setString ( paramIndex + + , s ) ;
}
}
stmt . setInt ( paramIndex + + , request . getStartIndex ( ) ) ;
stmt . setInt ( paramIndex , request . getRowCount ( ) ) ;
try ( ResultSet rs = stmt . executeQuery ( ) ) {
while ( rs . next ( ) ) {
operation = new Operation ( ) ;
operation . setId ( rs . getInt ( "ID" ) ) ;
operation . setType ( Operation . Type . valueOf ( rs . getString ( "TYPE" ) ) ) ;
operation . setCreatedTimeStamp ( new Timestamp ( rs . getLong ( "CREATED_TIMESTAMP" ) * 1000L ) . toString ( ) ) ;
if ( rs . getLong ( "UPDATED_TIMESTAMP" ) = = 0 ) {
operation . setReceivedTimeStamp ( "" ) ;
} else {
operation . setReceivedTimeStamp (
new Timestamp ( ( rs . getLong ( "UPDATED_TIMESTAMP" ) * 1000 ) ) . toString ( ) ) ;
}
operation . setCode ( rs . getString ( "OPERATION_CODE" ) ) ;
operation . setInitiatedBy ( rs . getString ( "INITIATED_BY" ) ) ;
operation . setStatus ( Operation . Status . valueOf ( rs . getString ( "STATUS" ) ) ) ;
OperationDAOUtil . setActivityId ( operation , rs . getInt ( "ID" ) ) ;
operations . add ( operation ) ;
}
}
}
} catch ( SQLException e ) {
throw new OperationManagementDAOException ( "SQL error occurred while retrieving the operation " +
"available for the device'" + deviceId + "' with status '" , e ) ;
}
return operations ;
}
@Override
public int getOperationCountForDevice ( int enrolmentId , PaginationRequest request )
throws OperationManagementDAOException {
@ -1592,6 +1732,112 @@ public class GenericOperationDAOImpl implements OperationDAO {
return 0 ;
}
@Override
public int getOperationCountForDeviceWithDeviceIdentifier ( DeviceIdentifier deviceId , PaginationRequest request )
throws OperationManagementDAOException {
String createdTo = null ;
String createdFrom = null ;
DateFormat simple = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss.SSS" ) ;
if ( request . getOperationLogFilters ( ) . getCreatedDayFrom ( ) ! = null ) {
createdFrom = simple . format ( request . getOperationLogFilters ( ) . getCreatedDayFrom ( ) ) ;
}
if ( request . getOperationLogFilters ( ) . getCreatedDayTo ( ) ! = null ) {
createdTo = simple . format ( request . getOperationLogFilters ( ) . getCreatedDayTo ( ) ) ;
}
Long updatedFrom = request . getOperationLogFilters ( ) . getUpdatedDayFrom ( ) ;
Long updatedTo = request . getOperationLogFilters ( ) . getUpdatedDayTo ( ) ;
List < String > operationCodes = request . getOperationLogFilters ( ) . getOperationCode ( ) ;
List < String > status = request . getOperationLogFilters ( ) . getStatus ( ) ;
boolean isCreatedDayProvided = false ;
boolean isUpdatedDayProvided = false ;
boolean isOperationCodeProvided = false ;
boolean isStatusProvided = false ;
String sql = "SELECT "
+ "COUNT(o.ID) AS OPERATION_COUNT "
+ "FROM "
+ "DM_OPERATION o "
+ "INNER JOIN "
+ "(SELECT dm.OPERATION_ID, "
+ "dm.ID, "
+ "dm.STATUS, "
+ "dm.UPDATED_TIMESTAMP "
+ "FROM "
+ "DM_ENROLMENT_OP_MAPPING dm "
+ "WHERE "
+ "dm.DEVICE_IDENTIFICATION = ?" ;
if ( updatedFrom ! = null & & updatedFrom ! = 0 & & updatedTo ! = null & & updatedTo ! = 0 ) {
sql + = " AND dm.UPDATED_TIMESTAMP BETWEEN ? AND ?" ;
isUpdatedDayProvided = true ;
}
sql + = ") om ON o.ID = om.OPERATION_ID " ;
if ( createdFrom ! = null & & ! createdFrom . isEmpty ( ) & & createdTo ! = null & & ! createdTo . isEmpty ( ) ) {
sql + = " WHERE o.CREATED_TIMESTAMP BETWEEN ? AND ?" ;
isCreatedDayProvided = true ;
}
if ( status ! = null & & ! status . isEmpty ( ) ) {
if ( isCreatedDayProvided ) {
sql + = " AND (om.STATUS = ? " ;
} else {
sql + = " WHERE (om.STATUS = ? " ;
}
sql = IntStream . range ( 0 , status . size ( ) - 1 ) . mapToObj ( i - > " OR om.STATUS = ?" )
. collect ( Collectors . joining ( "" , sql , "" ) ) ;
sql + = ")" ;
isStatusProvided = true ;
}
if ( operationCodes ! = null & & ! operationCodes . isEmpty ( ) ) {
if ( isCreatedDayProvided | | isStatusProvided ) {
sql + = " AND (o.OPERATION_CODE = ? " ;
} else {
sql + = " WHERE (o.OPERATION_CODE = ? " ;
}
sql = IntStream . range ( 0 , operationCodes . size ( ) - 1 ) . mapToObj ( i - > " OR o.OPERATION_CODE = ?" )
. collect ( Collectors . joining ( "" , sql , "" ) ) ;
sql + = ")" ;
isOperationCodeProvided = true ;
}
try {
Connection conn = OperationManagementDAOFactory . getConnection ( ) ;
try ( PreparedStatement stmt = conn . prepareStatement ( sql ) ) {
int paramIndex = 1 ;
stmt . setString ( paramIndex + + , deviceId . getId ( ) ) ;
if ( isUpdatedDayProvided ) {
stmt . setLong ( paramIndex + + , updatedFrom ) ;
stmt . setLong ( paramIndex + + , updatedTo ) ;
}
if ( isCreatedDayProvided ) {
stmt . setString ( paramIndex + + , createdFrom ) ;
stmt . setString ( paramIndex + + , createdTo ) ;
}
if ( isStatusProvided ) {
for ( String s : status ) {
stmt . setString ( paramIndex + + , s ) ;
}
}
if ( isOperationCodeProvided ) {
for ( String s : operationCodes ) {
stmt . setString ( paramIndex + + , s ) ;
}
}
try ( ResultSet rs = stmt . executeQuery ( ) ) {
if ( rs . next ( ) ) {
return rs . getInt ( "OPERATION_COUNT" ) ;
}
}
}
} catch ( SQLException e ) {
String msg = "SQL error occurred while retrieving the operation count of the device" + deviceId
+ " for search query" ;
log . error ( msg , e ) ;
throw new OperationManagementDAOException ( msg , e ) ;
}
return 0 ;
}
@Override
public Operation getNextOperation ( int enrolmentId , Operation . Status status ) throws OperationManagementDAOException {
PreparedStatement stmt = null ;