| | | 1 | | using System.Text.Json; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | using Pomodoro.Web.Models; |
| | | 5 | | using Pomodoro.Web.Services.Repositories; |
| | | 6 | | |
| | | 7 | | namespace Pomodoro.Web.Services; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Service for exporting and importing application data |
| | | 11 | | /// </summary> |
| | | 12 | | public class ExportService : IExportService |
| | | 13 | | { |
| | | 14 | | private readonly IActivityRepository _activityRepository; |
| | | 15 | | private readonly ITaskRepository _taskRepository; |
| | | 16 | | private readonly ISettingsRepository _settingsRepository; |
| | | 17 | | private readonly ILogger<ExportService> _logger; |
| | | 18 | | |
| | 40 | 19 | | public ExportService( |
| | 40 | 20 | | IActivityRepository activityRepository, |
| | 40 | 21 | | ITaskRepository taskRepository, |
| | 40 | 22 | | ISettingsRepository settingsRepository, |
| | 40 | 23 | | ILogger<ExportService> logger) |
| | 40 | 24 | | { |
| | 40 | 25 | | _activityRepository = activityRepository; |
| | 40 | 26 | | _taskRepository = taskRepository; |
| | 40 | 27 | | _settingsRepository = settingsRepository; |
| | 40 | 28 | | _logger = logger; |
| | 40 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Exports all data to JSON format |
| | | 33 | | /// </summary> |
| | | 34 | | public async Task<string> ExportToJsonAsync() |
| | 7 | 35 | | { |
| | | 36 | | try |
| | 7 | 37 | | { |
| | | 38 | | // Get all data from IndexedDB |
| | 7 | 39 | | var activities = await _activityRepository.GetAllAsync(); |
| | 5 | 40 | | var tasks = await _taskRepository.GetAllAsync(); |
| | 5 | 41 | | var settings = await _settingsRepository.GetAsync(); |
| | | 42 | | |
| | | 43 | | // Create export data structure |
| | 5 | 44 | | var exportData = new |
| | 5 | 45 | | { |
| | 5 | 46 | | Version = 1, |
| | 5 | 47 | | ExportDate = DateTime.UtcNow, |
| | 5 | 48 | | Settings = settings, |
| | 5 | 49 | | Activities = activities, |
| | 5 | 50 | | Tasks = tasks |
| | 5 | 51 | | }; |
| | | 52 | | |
| | 5 | 53 | | var json = JsonSerializer.Serialize(exportData, new JsonSerializerOptions |
| | 5 | 54 | | { |
| | 5 | 55 | | WriteIndented = true, |
| | 5 | 56 | | PropertyNamingPolicy = JsonNamingPolicy.CamelCase |
| | 5 | 57 | | }); |
| | | 58 | | |
| | 5 | 59 | | _logger.LogInformation(Constants.Messages.LogExportJsonFormat, activities.Count, tasks.Count); |
| | 5 | 60 | | return json; |
| | | 61 | | } |
| | 2 | 62 | | catch (Exception ex) |
| | 2 | 63 | | { |
| | 2 | 64 | | _logger.LogError(ex, Constants.Messages.LogExportJsonFailed); |
| | 2 | 65 | | throw; |
| | | 66 | | } |
| | 5 | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Imports data from JSON format with duplicate detection and validation |
| | | 71 | | /// </summary> |
| | | 72 | | public async Task<ImportResult> ImportFromJsonAsync(string jsonData) |
| | 27 | 73 | | { |
| | | 74 | | try |
| | 27 | 75 | | { |
| | | 76 | | // Validate input |
| | 27 | 77 | | var validationResult = ValidateJsonInput(jsonData); |
| | 27 | 78 | | if (!validationResult.IsValid) |
| | 3 | 79 | | { |
| | 3 | 80 | | return validationResult.Result; |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | // Parse JSON |
| | 24 | 84 | | var parseResult = await ParseJsonDataAsync(jsonData); |
| | 24 | 85 | | if (!parseResult.IsValid) |
| | 5 | 86 | | { |
| | 5 | 87 | | return parseResult.Result; |
| | | 88 | | } |
| | | 89 | | |
| | 19 | 90 | | var importData = parseResult.ImportData!; |
| | | 91 | | |
| | | 92 | | // Load existing data for duplicate detection |
| | 19 | 93 | | var existingData = await LoadExistingDataAsync(); |
| | | 94 | | |
| | | 95 | | // Import settings |
| | 18 | 96 | | var settingsImported = await ImportSettingsAsync(importData.Settings); |
| | | 97 | | |
| | | 98 | | // Import tasks with duplicate detection |
| | 18 | 99 | | var taskImportResult = await ImportTasksAsync(importData.Tasks, existingData); |
| | | 100 | | |
| | | 101 | | // Import activities with duplicate detection |
| | 18 | 102 | | var activityImportResult = await ImportActivitiesAsync(importData.Activities, existingData, taskImportResult |
| | | 103 | | |
| | 18 | 104 | | _logger.LogInformation( |
| | 18 | 105 | | "Import completed: {ActivitiesImported} activities imported, {ActivitiesSkipped} activities skipped, " + |
| | 18 | 106 | | "{TasksImported} tasks imported, {TasksSkipped} tasks skipped", |
| | 18 | 107 | | activityImportResult.ActivitiesImported, activityImportResult.ActivitiesSkipped, |
| | 18 | 108 | | taskImportResult.TasksImported, taskImportResult.TasksSkipped); |
| | | 109 | | |
| | 18 | 110 | | return ImportResult.Succeeded( |
| | 18 | 111 | | activityImportResult.ActivitiesImported, |
| | 18 | 112 | | activityImportResult.ActivitiesSkipped, |
| | 18 | 113 | | taskImportResult.TasksImported, |
| | 18 | 114 | | taskImportResult.TasksSkipped, |
| | 18 | 115 | | settingsImported); |
| | | 116 | | } |
| | 1 | 117 | | catch (Exception ex) |
| | 1 | 118 | | { |
| | 1 | 119 | | _logger.LogError(ex, Constants.Messages.LogImportJsonFailed); |
| | 1 | 120 | | return ImportResult.Failed(Constants.Messages.ImportErrorFailed); |
| | | 121 | | } |
| | 27 | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Validates JSON input data |
| | | 126 | | /// </summary> |
| | | 127 | | private (bool IsValid, ImportResult Result) ValidateJsonInput(string jsonData) |
| | 27 | 128 | | { |
| | 27 | 129 | | if (string.IsNullOrWhiteSpace(jsonData)) |
| | 3 | 130 | | { |
| | 3 | 131 | | _logger.LogWarning(Constants.Messages.LogImportJsonInvalid); |
| | 3 | 132 | | return (false, ImportResult.Failed(Constants.Messages.ImportErrorEmptyFile)); |
| | | 133 | | } |
| | | 134 | | |
| | 24 | 135 | | return (true, ImportResult.Succeeded(0, 0, 0, 0, false)); |
| | 27 | 136 | | } |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// Parses JSON data and validates structure |
| | | 140 | | /// </summary> |
| | | 141 | | private async Task<(bool IsValid, ImportResult Result, ExportData? ImportData)> ParseJsonDataAsync(string jsonData) |
| | 24 | 142 | | { |
| | 24 | 143 | | var options = new JsonSerializerOptions |
| | 24 | 144 | | { |
| | 24 | 145 | | PropertyNameCaseInsensitive = true, |
| | 24 | 146 | | MaxDepth = 64 |
| | 24 | 147 | | }; |
| | | 148 | | |
| | | 149 | | ExportData? importData; |
| | | 150 | | try |
| | 24 | 151 | | { |
| | 24 | 152 | | importData = JsonSerializer.Deserialize<ExportData>(jsonData, options); |
| | 23 | 153 | | } |
| | 1 | 154 | | catch (JsonException ex) |
| | 1 | 155 | | { |
| | 1 | 156 | | _logger.LogError(ex, Constants.Messages.LogImportJsonParseFailed); |
| | 1 | 157 | | return (false, ImportResult.Failed(Constants.Messages.ImportErrorInvalidJson), null); |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | // Validate parsed data structure |
| | 23 | 161 | | if (importData == null) |
| | 1 | 162 | | { |
| | 1 | 163 | | _logger.LogWarning(Constants.Messages.LogImportJsonInvalid); |
| | 1 | 164 | | return (false, ImportResult.Failed(Constants.Messages.ImportErrorInvalidFormat), null); |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | // Validate required fields |
| | 22 | 168 | | if (importData.Version <= 0) |
| | 3 | 169 | | { |
| | 3 | 170 | | _logger.LogWarning("Import file has invalid version: {Version}", importData.Version); |
| | 3 | 171 | | return (false, ImportResult.Failed(Constants.Messages.ImportErrorInvalidVersion), null); |
| | | 172 | | } |
| | | 173 | | |
| | 19 | 174 | | return (true, ImportResult.Succeeded(0, 0, 0, 0, false), importData); |
| | 24 | 175 | | } |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// Loads existing data for duplicate detection |
| | | 179 | | /// </summary> |
| | | 180 | | private async Task<ExistingData> LoadExistingDataAsync() |
| | 19 | 181 | | { |
| | 19 | 182 | | var existingActivities = await _activityRepository.GetAllAsync(); |
| | 18 | 183 | | var existingTasks = await _taskRepository.GetAllAsync(); |
| | | 184 | | |
| | | 185 | | // Create lookup sets for efficient duplicate detection |
| | 18 | 186 | | var activityLookup = existingActivities |
| | 3 | 187 | | .Select(a => new ActivityKey(a.Type, a.CompletedAt, a.DurationMinutes, a.TaskName)) |
| | 18 | 188 | | .ToHashSet(); |
| | | 189 | | |
| | 18 | 190 | | var taskLookup = existingTasks |
| | 4 | 191 | | .Select(t => new TaskKey(t.Name, t.CreatedAt)) |
| | 18 | 192 | | .ToHashSet(); |
| | | 193 | | |
| | | 194 | | // Build dictionary for O(1) task ID lookups during activity import |
| | 26 | 195 | | var existingTaskById = existingTasks.ToDictionary(t => t.Id, t => t); |
| | | 196 | | |
| | | 197 | | // Build dictionary for O(1) task lookups by key (Name + CreatedAt) for duplicate mapping |
| | | 198 | | // Use GroupBy to handle potential duplicates gracefully (take first occurrence) |
| | 18 | 199 | | var existingTaskByKey = existingTasks |
| | 4 | 200 | | .GroupBy(t => new TaskKey(t.Name, t.CreatedAt)) |
| | 26 | 201 | | .ToDictionary(g => g.Key, g => g.First()); |
| | | 202 | | |
| | 18 | 203 | | return new ExistingData |
| | 18 | 204 | | { |
| | 18 | 205 | | Activities = existingActivities, |
| | 18 | 206 | | Tasks = existingTasks, |
| | 18 | 207 | | ActivityLookup = activityLookup, |
| | 18 | 208 | | TaskLookup = taskLookup, |
| | 18 | 209 | | TaskById = existingTaskById, |
| | 18 | 210 | | TaskByKey = existingTaskByKey |
| | 18 | 211 | | }; |
| | 18 | 212 | | } |
| | | 213 | | |
| | | 214 | | /// <summary> |
| | | 215 | | /// Imports settings |
| | | 216 | | /// </summary> |
| | | 217 | | private async Task<bool> ImportSettingsAsync(TimerSettings? settings) |
| | 18 | 218 | | { |
| | 18 | 219 | | if (settings != null) |
| | 4 | 220 | | { |
| | 4 | 221 | | await _settingsRepository.SaveAsync(settings); |
| | 4 | 222 | | return true; |
| | | 223 | | } |
| | | 224 | | |
| | 14 | 225 | | return false; |
| | 18 | 226 | | } |
| | | 227 | | |
| | | 228 | | /// <summary> |
| | | 229 | | /// Imports tasks with duplicate detection |
| | | 230 | | /// </summary> |
| | | 231 | | private async Task<TaskImportResult> ImportTasksAsync(List<TaskItem>? tasks, ExistingData existingData) |
| | 18 | 232 | | { |
| | 18 | 233 | | var tasksImported = 0; |
| | 18 | 234 | | var tasksSkipped = 0; |
| | 18 | 235 | | var taskIdMapping = new Dictionary<Guid, Guid>(); |
| | | 236 | | |
| | 18 | 237 | | if (tasks != null) |
| | 18 | 238 | | { |
| | 88 | 239 | | foreach (var task in tasks) |
| | 17 | 240 | | { |
| | 17 | 241 | | var key = new TaskKey(task.Name, task.CreatedAt); |
| | | 242 | | |
| | 17 | 243 | | if (existingData.TaskLookup.Contains(key)) |
| | 5 | 244 | | { |
| | | 245 | | // Duplicate found - skip but still map the ID for activity references |
| | | 246 | | // Use O(1) dictionary lookup instead of O(n) FirstOrDefault |
| | 5 | 247 | | if (existingData.TaskByKey.TryGetValue(key, out var existingTask) && task.Id != Guid.Empty) |
| | 2 | 248 | | { |
| | 2 | 249 | | taskIdMapping[task.Id] = existingTask.Id; |
| | 2 | 250 | | } |
| | 3 | 251 | | else if (task.Id != Guid.Empty) |
| | 2 | 252 | | { |
| | | 253 | | // Edge case: taskLookup contained the key but dictionary lookup failed (data inconsistency) |
| | 2 | 254 | | _logger.LogWarning("Duplicate task found but couldn't map ID {TaskId} for task {Name}", task.Id, |
| | 2 | 255 | | } |
| | 5 | 256 | | tasksSkipped++; |
| | 5 | 257 | | _logger.LogDebug("Skipping duplicate task: {Name} created at {CreatedAt}", task.Name, task.CreatedAt |
| | 5 | 258 | | } |
| | | 259 | | else |
| | 12 | 260 | | { |
| | | 261 | | // Not a duplicate - create new record with new ID |
| | 12 | 262 | | var newId = Guid.NewGuid(); |
| | 12 | 263 | | var importedTask = new TaskItem |
| | 12 | 264 | | { |
| | 12 | 265 | | Id = newId, |
| | 12 | 266 | | Name = task.Name, |
| | 12 | 267 | | PomodoroCount = task.PomodoroCount, |
| | 12 | 268 | | TotalFocusMinutes = task.TotalFocusMinutes, |
| | 12 | 269 | | IsCompleted = task.IsCompleted, |
| | 12 | 270 | | CreatedAt = task.CreatedAt, |
| | 12 | 271 | | LastWorkedOn = task.LastWorkedOn, |
| | 12 | 272 | | IsDeleted = task.IsDeleted, |
| | 12 | 273 | | DeletedAt = task.DeletedAt |
| | 12 | 274 | | }; |
| | 12 | 275 | | await _taskRepository.SaveAsync(importedTask); |
| | | 276 | | |
| | | 277 | | // Map old task ID to new task ID for activity references |
| | 12 | 278 | | if (task.Id != Guid.Empty) |
| | 12 | 279 | | { |
| | 12 | 280 | | taskIdMapping[task.Id] = newId; |
| | 12 | 281 | | } |
| | | 282 | | |
| | 12 | 283 | | tasksImported++; |
| | 12 | 284 | | existingData.TaskLookup.Add(key); // Add to lookup to catch duplicates within the same import file |
| | 12 | 285 | | } |
| | 17 | 286 | | } |
| | 18 | 287 | | } |
| | | 288 | | |
| | 18 | 289 | | return new TaskImportResult |
| | 18 | 290 | | { |
| | 18 | 291 | | TasksImported = tasksImported, |
| | 18 | 292 | | TasksSkipped = tasksSkipped, |
| | 18 | 293 | | TaskIdMapping = taskIdMapping |
| | 18 | 294 | | }; |
| | 18 | 295 | | } |
| | | 296 | | |
| | | 297 | | /// <summary> |
| | | 298 | | /// Imports activities with duplicate detection |
| | | 299 | | /// </summary> |
| | | 300 | | private async Task<ActivityImportResult> ImportActivitiesAsync(List<ActivityRecord>? activities, ExistingData existi |
| | 18 | 301 | | { |
| | 18 | 302 | | var activitiesImported = 0; |
| | 18 | 303 | | var activitiesSkipped = 0; |
| | | 304 | | |
| | 18 | 305 | | if (activities != null) |
| | 18 | 306 | | { |
| | 82 | 307 | | foreach (var activity in activities) |
| | 14 | 308 | | { |
| | 14 | 309 | | var key = new ActivityKey(activity.Type, activity.CompletedAt, activity.DurationMinutes, activity.TaskNa |
| | | 310 | | |
| | 14 | 311 | | if (existingData.ActivityLookup.Contains(key)) |
| | 4 | 312 | | { |
| | | 313 | | // Duplicate found - skip |
| | 4 | 314 | | activitiesSkipped++; |
| | 4 | 315 | | _logger.LogDebug("Skipping duplicate activity: {Type} at {CompletedAt}", activity.Type, activity.Com |
| | 4 | 316 | | } |
| | | 317 | | else |
| | 10 | 318 | | { |
| | | 319 | | // Map the old TaskId to the new TaskId if it exists |
| | 10 | 320 | | Guid? newTaskId = null; |
| | 10 | 321 | | if (activity.TaskId.HasValue && activity.TaskId.Value != Guid.Empty) |
| | 4 | 322 | | { |
| | 4 | 323 | | if (taskIdMapping.TryGetValue(activity.TaskId.Value, out var mappedId)) |
| | 2 | 324 | | { |
| | 2 | 325 | | newTaskId = mappedId; |
| | 2 | 326 | | } |
| | 2 | 327 | | else if (existingData.TaskById.TryGetValue(activity.TaskId.Value, out var existingTask)) |
| | 1 | 328 | | { |
| | | 329 | | // Task wasn't in the import but exists in the database, keep the reference |
| | 1 | 330 | | newTaskId = activity.TaskId.Value; |
| | 1 | 331 | | } |
| | | 332 | | // else: Task doesn't exist, leave TaskId as null |
| | 4 | 333 | | } |
| | | 334 | | |
| | | 335 | | // Not a duplicate - create new record with new ID |
| | 10 | 336 | | var importedActivity = new ActivityRecord |
| | 10 | 337 | | { |
| | 10 | 338 | | Id = Guid.NewGuid(), |
| | 10 | 339 | | Type = activity.Type, |
| | 10 | 340 | | TaskId = newTaskId, |
| | 10 | 341 | | TaskName = activity.TaskName, |
| | 10 | 342 | | CompletedAt = activity.CompletedAt, |
| | 10 | 343 | | DurationMinutes = activity.DurationMinutes, |
| | 10 | 344 | | WasCompleted = activity.WasCompleted |
| | 10 | 345 | | }; |
| | 10 | 346 | | await _activityRepository.SaveAsync(importedActivity); |
| | 10 | 347 | | activitiesImported++; |
| | 10 | 348 | | existingData.ActivityLookup.Add(key); // Add to lookup to catch duplicates within the same import fi |
| | 10 | 349 | | } |
| | 14 | 350 | | } |
| | 18 | 351 | | } |
| | | 352 | | |
| | 18 | 353 | | return new ActivityImportResult |
| | 18 | 354 | | { |
| | 18 | 355 | | ActivitiesImported = activitiesImported, |
| | 18 | 356 | | ActivitiesSkipped = activitiesSkipped |
| | 18 | 357 | | }; |
| | 18 | 358 | | } |
| | | 359 | | |
| | | 360 | | /// <summary> |
| | | 361 | | /// Clears all application data |
| | | 362 | | /// </summary> |
| | | 363 | | public async Task ClearAllDataAsync() |
| | 6 | 364 | | { |
| | | 365 | | try |
| | 6 | 366 | | { |
| | 6 | 367 | | var activityCount = await _activityRepository.GetCountAsync(); |
| | 4 | 368 | | var taskCount = await _taskRepository.GetCountAsync(); |
| | | 369 | | |
| | 4 | 370 | | await _activityRepository.ClearAllAsync(); |
| | 4 | 371 | | await _taskRepository.ClearAllAsync(); |
| | | 372 | | |
| | | 373 | | // Clear settings by resetting to defaults |
| | 4 | 374 | | var defaultSettings = new TimerSettings(); |
| | 4 | 375 | | await _settingsRepository.SaveAsync(defaultSettings); |
| | | 376 | | |
| | 4 | 377 | | _logger.LogInformation(Constants.Messages.LogClearDataSuccess, activityCount, taskCount); |
| | 4 | 378 | | } |
| | 2 | 379 | | catch (Exception ex) |
| | 2 | 380 | | { |
| | 2 | 381 | | _logger.LogError(ex, Constants.Messages.LogClearDataFailed); |
| | 2 | 382 | | throw; |
| | | 383 | | } |
| | 4 | 384 | | } |
| | | 385 | | |
| | | 386 | | #region Export Data Model |
| | | 387 | | |
| | | 388 | | /// <summary> |
| | | 389 | | /// Data structure for JSON export/import |
| | | 390 | | /// </summary> |
| | | 391 | | private class ExportData |
| | | 392 | | { |
| | 46 | 393 | | public int Version { get; set; } |
| | 21 | 394 | | public DateTime ExportDate { get; set; } |
| | 39 | 395 | | public TimerSettings? Settings { get; set; } |
| | 39 | 396 | | public List<ActivityRecord>? Activities { get; set; } |
| | 39 | 397 | | public List<TaskItem>? Tasks { get; set; } |
| | | 398 | | } |
| | | 399 | | |
| | | 400 | | /// <summary> |
| | | 401 | | /// Container for existing data used during import |
| | | 402 | | /// </summary> |
| | | 403 | | private class ExistingData |
| | | 404 | | { |
| | 36 | 405 | | public List<ActivityRecord> Activities { get; set; } = new(); |
| | 36 | 406 | | public List<TaskItem> Tasks { get; set; } = new(); |
| | 60 | 407 | | public HashSet<ActivityKey> ActivityLookup { get; set; } = new(); |
| | 65 | 408 | | public HashSet<TaskKey> TaskLookup { get; set; } = new(); |
| | 38 | 409 | | public Dictionary<Guid, TaskItem> TaskById { get; set; } = new(); |
| | 41 | 410 | | public Dictionary<TaskKey, TaskItem> TaskByKey { get; set; } = new(); |
| | | 411 | | } |
| | | 412 | | |
| | | 413 | | /// <summary> |
| | | 414 | | /// Result of task import operation |
| | | 415 | | /// </summary> |
| | | 416 | | private class TaskImportResult |
| | | 417 | | { |
| | 54 | 418 | | public int TasksImported { get; set; } |
| | 54 | 419 | | public int TasksSkipped { get; set; } |
| | 54 | 420 | | public Dictionary<Guid, Guid> TaskIdMapping { get; set; } = new(); |
| | | 421 | | } |
| | | 422 | | |
| | | 423 | | /// <summary> |
| | | 424 | | /// Result of activity import operation |
| | | 425 | | /// </summary> |
| | | 426 | | private class ActivityImportResult |
| | | 427 | | { |
| | 54 | 428 | | public int ActivitiesImported { get; set; } |
| | 54 | 429 | | public int ActivitiesSkipped { get; set; } |
| | | 430 | | } |
| | | 431 | | |
| | | 432 | | #endregion |
| | | 433 | | } |