Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,33 @@ public void deletePreRegRecords(@NonNull String jobId, @NonNull MasterDataSyncPi
}
}

@Override
public void deleteRegistrationPackets(@NonNull String jobId, @NonNull MasterDataSyncPigeon.Result<Boolean> result) {
try {
packetService.deleteRegistrationPackets();
masterDataService.logLastSyncCompletionDateTime(jobId);
Toast.makeText(context, "Deleted Registration packets", Toast.LENGTH_LONG).show();
result.success(true);
} catch (Exception e) {
Log.e(TAG, "Failed to delete registration packets", e);
result.error(e);
Toast.makeText(context, "Failed to delete Registration packets", Toast.LENGTH_LONG).show();
}
}

@Override
public void syncPacketStatus(@NonNull String jobId, @NonNull MasterDataSyncPigeon.Result<Boolean> result) {
try {
packetService.syncAllPacketStatus();
masterDataService.logLastSyncCompletionDateTime(jobId);
Log.i(TAG, "Packet status sync job completed");
result.success(true);
} catch (Exception e) {
Log.e(TAG, "Failed to sync packet status", e);
result.error(e);
}
}

@Override
public void getLastSyncTimeByJobId(@NonNull String jobId, @NonNull MasterDataSyncPigeon.Result<String> result) {
int syncJobId = jobManagerService.generateJobServiceId(jobId);
Expand Down Expand Up @@ -473,6 +500,12 @@ public void executeJobByApiName(String jobApiName, Context context) {
preRegistrationDataSyncService.fetchAndDeleteRecords();
masterDataService.logLastSyncCompletionDateTime(jobId);
break;

case "registrationDeletionJob":
packetService.deleteRegistrationPackets();
masterDataService.logLastSyncCompletionDateTime(jobId);
Log.i(TAG, "Registration packet deletion job completed");
break;
default:
Log.w(getClass().getSimpleName(), "Unknown job: " + jobApiName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public class RegistrationConstants {
public static final String RIGHT = "Right";
public static final String LEFT = "Left";
public static final String PRE_REG_DELETION_CONFIGURED_DAYS = "mosip.registration.pre_reg_deletion_configured_days";
public static final String REG_DELETION_CONFIGURED_DAYS = "mosip.registration.reg_deletion_configured_days";
public static final String PRE_REG_DELETE_SUCCESS = "PRE_REG_DELETE_SUCCESS";
public static final String PRE_REG_DELETE_FAILURE = "PRE_REG_DELETE_FAILURE";
public static final String JOB_TRIGGER_POINT_USER = "User";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public interface RegistrationDao {
@Query("SELECT COUNT(*) FROM registration where client_status in (:syncedStatus, :approvedStatus)")
int findRegistrationCountBySyncedStatusAndApprovedStatus(String syncedStatus, String approvedStatus);


@Query("SELECT COUNT(*) FROM registration where client_status in (:syncedStatus)")
int findRegistrationCountBySyncedStatus(String syncedStatus);

Expand All @@ -70,4 +69,10 @@ public interface RegistrationDao {
@Query("SELECT COUNT(*) FROM registration where client_status in ('APPROVED', 'REJECTED')")
int findApprovedAndRejectedRegistrationCount();

@Query("SELECT * FROM registration where server_status IN (:serverStatuses) AND cr_dtimes < :timestamp")
List<Registration> findByServerStatusAndCrDtimeBefore(List<String> serverStatuses, long timestamp);

// Update server status with timestamp (for status sync)
@Query("UPDATE registration SET server_status = :serverStatus, server_status_dtimes = :timestamp WHERE packet_id = :packetId")
void updateServerStatusWithTimestamp(String packetId, String serverStatus, long timestamp);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.mosip.registration.clientmanager.jobs;

import android.annotation.SuppressLint;
import android.util.Log;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import dagger.android.AndroidInjection;
import io.mosip.registration.clientmanager.spi.PacketService;

/**
* Job for deleting old processed registration packets
* Runs periodically to clean up packets that have been successfully processed
* by the server and are older than the configured number of days.
*
* @since 1.0.0
*/
@SuppressLint("SpecifyJobSchedulerIdRange")
public class RegistrationDeletionJob extends SyncJobServiceBase {

private static final String TAG = RegistrationDeletionJob.class.getSimpleName();

@Inject
PacketService packetService;

public RegistrationDeletionJob() {
configureBuilder();
}

@Override
public void onCreate() {
super.onCreate();
AndroidInjection.inject(this);
}

@Override
public boolean triggerJob(int jobId) {
Log.d(TAG, TAG + " Started");
try {
packetService.deleteRegistrationPackets();
long timeStampInSeconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
logJobTransaction(jobId, timeStampInSeconds);
Log.d(TAG, TAG + " Completed successfully");
return true;
} catch (Exception e) {
Log.e(TAG, TAG + " failed", e);
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,15 @@ public void updateSupervisorReview(String packetId, String supervisorStatus, Str
public int getYetToExportCount() {
return this.registrationDao.findApprovedAndRejectedRegistrationCount();
}

public List<Registration> findByServerStatusAndCrDtimeBefore(List<String> serverStatuses, long timestamp) {
return this.registrationDao.findByServerStatusAndCrDtimeBefore(serverStatuses, timestamp);
}

/**
* Update server status with timestamp
*/
public void updateServerStatusWithTimestamp(String packetId, String serverStatus, long timestamp) {
this.registrationDao.updateServerStatusWithTimestamp(packetId, serverStatus, timestamp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.mosip.registration.clientmanager.jobs.ConfigDataSyncJob;
import io.mosip.registration.clientmanager.jobs.DeleteAuditLogsJob;
import io.mosip.registration.clientmanager.jobs.PacketStatusSyncJob;
import io.mosip.registration.clientmanager.jobs.RegistrationDeletionJob;
import io.mosip.registration.clientmanager.repository.SyncJobDefRepository;
import io.mosip.registration.clientmanager.spi.JobManagerService;
import io.mosip.registration.clientmanager.spi.JobTransactionService;
Expand Down Expand Up @@ -195,7 +196,6 @@ public int generateJobServiceId(String syncJobDefId) {
}
}


private Class<?> getJobServiceImplClass(String jobAPIName) {
switch (jobAPIName) {
case "packetSyncStatusJob":
Expand All @@ -204,6 +204,8 @@ private Class<?> getJobServiceImplClass(String jobAPIName) {
return ConfigDataSyncJob.class;
case "deleteAuditLogsJob":
return DeleteAuditLogsJob.class;
case "registrationDeletionJob":
return RegistrationDeletionJob.class;
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import javax.inject.Inject;
Expand Down Expand Up @@ -305,6 +306,7 @@ public void syncAllPacketStatus() {
} else {
packet.setPacketId(reg.getPacketId());
}
packets.add(packet);
}
packetStatusRequest.setRequest(packets);

Expand All @@ -317,10 +319,11 @@ public void onResponse(Call<PacketStatusResponse> call, Response<PacketStatusRes
int packetSyncSuccess = 0;

if (packetStatusList != null && packetStatusList.size() > 0) {
long currentTimestamp = System.currentTimeMillis();
for (PacketStatusDto packetStatus : packetStatusList) {
PacketStatusUpdateDto updateDto = new PacketStatusUpdateDto(packetStatus.getRegistrationId() != null ? packetStatus.getRegistrationId() : packetStatus.getPacketId(), packetStatus.getStatusCode());
registrationRepository.updateStatus(updateDto.getRegistrationId(), updateDto.getStatusCode(),
PacketClientStatus.UPLOADED.name());
// Update server status with timestamp
registrationRepository.updateServerStatusWithTimestamp(updateDto.getRegistrationId(), updateDto.getStatusCode(), currentTimestamp);
packetSyncSuccess++;
}
}
Expand Down Expand Up @@ -444,4 +447,60 @@ public boolean isMaxPacketCountLimitReached() {
return false;
}
}

@Override
public void deleteRegistrationPackets() {
Log.i(TAG, "Starting registration packet deletion job");
try {
String configuredDays = globalParamRepository
.getCachedStringGlobalParam(RegistrationConstants.REG_DELETION_CONFIGURED_DAYS);
int days = Integer.parseInt(configuredDays);
Comment thread
SachinPremkumar marked this conversation as resolved.

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -days);
long cutoffTime = cal.getTimeInMillis();

List<String> serverStatuses = Arrays.asList(
PacketServerStatus.PROCESSED.name(),
PacketServerStatus.ACCEPTED.name());

List<Registration> registrations = registrationRepository.findByServerStatusAndCrDtimeBefore(serverStatuses,
cutoffTime);

if (registrations != null && !registrations.isEmpty()) {
for (Registration registration : registrations) {
delete(registration);
}
} else {
Log.i(TAG, "No registrations found to delete");
}
} catch (NumberFormatException | NullPointerException ex) {
Log.e(TAG, "Invalid or missing REG_DELETION_CONFIGURED_DAYS configuration", ex);
} catch (Exception e) {
Log.e(TAG, "Error during registration packet deletion", e);
}
}

private void delete(Registration registration) {
try {
String filePath = registration.getFilePath();
if (filePath != null) {
File zipFile = new File(filePath);

// Delete ZIP packet file
if (zipFile.exists()) {
if (zipFile.delete()) {
Log.i(TAG, "Deleted zip file: " + filePath);
} else {
Log.e(TAG, "Failed to delete zip file: " + filePath);
}
}
}

// Delete database record
registrationRepository.deleteRegistration(registration.getPacketId());
} catch (Exception e) {
Log.e(TAG, "Error deleting registration: " + registration.getPacketId(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,6 @@ public interface PacketService {
* @return true if maximum packet count limit is reached, otherwise false.
*/
boolean isMaxPacketCountLimitReached();

void deleteRegistrationPackets();
}
28 changes: 28 additions & 0 deletions lib/platform_android/sync_response_service_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,34 @@ class SyncResponseServiceImpl implements SyncResponseService {
}
}

@override
Future<bool> deleteRegistrationPackets(String jobId) async {
try {
final deleteResponse = await SyncApi().deleteRegistrationPackets(jobId);
return deleteResponse;
} on PlatformException catch (e) {
debugPrint('deleteRegistrationPackets PlatformException: ${e.message}');
return false;
} catch (e) {
debugPrint('deleteRegistrationPackets failed: $e');
return false;
}
}

@override
Future<bool> syncPacketStatus(String jobId) async {
try {
final syncResponse = await SyncApi().syncPacketStatus(jobId);
return syncResponse;
} on PlatformException catch (e) {
debugPrint('syncPacketStatus PlatformException: ${e.message}');
return false;
} catch (e) {
debugPrint('syncPacketStatus failed: $e');
return false;
}
}

@override
Future<String> getLastSyncTimeByJobId(String jobId) async{
try {
Expand Down
2 changes: 2 additions & 0 deletions lib/platform_spi/sync_response_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ abstract class SyncResponseService {
Future<bool> getSyncAndUploadInProgressStatus();
Future<bool> deleteAuditLogs(String jobId);
Future<bool> deletePreRegRecords(String jobId);
Future<bool> deleteRegistrationPackets(String jobId);
Future<bool> syncPacketStatus(String jobId);

Future<List<String?>> getActiveSyncJobs();
Future<String> getLastSyncTimeByJobId(String jobId);
Expand Down
8 changes: 7 additions & 1 deletion lib/ui/settings/widgets/scheduled_jobs_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:registration_client/utils/sync_job_def.dart';
import '../../../provider/sync_provider.dart';

// Dart equivalent of the Java PACKET_JOBS constant
const List<String> PACKET_JOBS = ['RPS_J00006', 'RSJ_J00014', 'PUJ_J00017', 'PVS_J00015'];
const List<String> PACKET_JOBS = ['RPS_J00006', 'RSJ_J00014', 'PUJ_J00017'];

class ScheduledJobsSettings extends StatelessWidget {
const ScheduledJobsSettings({
Expand Down Expand Up @@ -167,6 +167,12 @@ class _JobCardState extends State<_JobCard> {
case 'preRegistrationPacketDeletionJob':
await service.deletePreRegRecords(jobId ?? '');
break;
case 'registrationDeletionJob':
await service.deleteRegistrationPackets(jobId ?? '');
break;
case 'packetSyncStatusJob':
await service.syncPacketStatus(jobId ?? '');
break;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
default:
debugPrint('No handler for sync job: $apiName');
return;
Expand Down
4 changes: 4 additions & 0 deletions pigeon/master_data_sync.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ abstract class SyncApi {
@async
bool deletePreRegRecords(String jobId);
@async
bool deleteRegistrationPackets(String jobId);
@async
bool syncPacketStatus(String jobId);
@async
String getLastSyncTimeByJobId(String jobId);
@async
String getNextSyncTimeByJobId(String jobId);
Expand Down