From 0e0d01f49cdd0086e5dc05df9209ffd6b054e96d Mon Sep 17 00:00:00 2001 From: Grzegorz Orczykowski Date: Wed, 15 May 2024 13:49:23 +0200 Subject: [PATCH] Improve detecting savepoints for blank forms --- .../initialization/SavepointsImporter.kt | 10 ++++------ .../initialization/SavepointsImporterTest.kt | 18 +++++++++--------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/collect_app/src/main/java/org/odk/collect/android/application/initialization/SavepointsImporter.kt b/collect_app/src/main/java/org/odk/collect/android/application/initialization/SavepointsImporter.kt index e4e1872196c..b6af7527cd1 100644 --- a/collect_app/src/main/java/org/odk/collect/android/application/initialization/SavepointsImporter.kt +++ b/collect_app/src/main/java/org/odk/collect/android/application/initialization/SavepointsImporter.kt @@ -49,18 +49,16 @@ class SavepointsImporter( } private fun importSavepointsThatBelongToBlankForms(formsRepository: FormsRepository, savepointsRepository: SavepointsRepository, cacheDir: File, instancesDir: File) { - formsRepository.all.sortedByDescending { form -> form.date }.forEach { form -> + formsRepository.all.forEach { form -> if (!form.isDeleted) { val formFileName = File(form.formFilePath).name.substringBeforeLast(".xml") cacheDir.listFiles { file -> - file.name.startsWith("${formFileName}_") && file.name.endsWith(".xml.save") + val match = """${formFileName}_(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})(.xml.save)""".toRegex().matchEntire(file.name) + match != null }?.forEach { savepointFile -> if (savepointFile.lastModified() > form.date) { - val alreadyUsed = savepointsRepository.getAll().any { savepoint -> savepoint.savepointFilePath == savepointFile.absolutePath } - if (!alreadyUsed) { - savepointsRepository.save(Savepoint(form.dbId, null, savepointFile.absolutePath, File(instancesDir, "$formFileName/$formFileName.xml").absolutePath)) - } + savepointsRepository.save(Savepoint(form.dbId, null, savepointFile.absolutePath, File(instancesDir, "$formFileName/$formFileName.xml").absolutePath)) } } } diff --git a/collect_app/src/test/java/org/odk/collect/android/application/initialization/SavepointsImporterTest.kt b/collect_app/src/test/java/org/odk/collect/android/application/initialization/SavepointsImporterTest.kt index 590541d65ca..fb86b6f99d1 100644 --- a/collect_app/src/test/java/org/odk/collect/android/application/initialization/SavepointsImporterTest.kt +++ b/collect_app/src/test/java/org/odk/collect/android/application/initialization/SavepointsImporterTest.kt @@ -53,7 +53,7 @@ class SavepointsImporterTest { createBlankForm(project, "sampleForm", "1", date = System.currentTimeMillis() + TimeInMs.ONE_HOUR) // create savepoints - createSavepointFile(project, "sampleForm_${System.currentTimeMillis()}.xml") + createSavepointFile(project, "sampleForm_2024-04-10_01-35-41.xml") // trigger importing savepointsImporter.run() @@ -69,7 +69,7 @@ class SavepointsImporterTest { createBlankForm(project, "sampleForm", "1", deleted = true) // create savepoints - createSavepointFile(project, "sampleForm_${System.currentTimeMillis()}.xml") + createSavepointFile(project, "sampleForm_2024-04-10_01-35-41.xml") // trigger importing savepointsImporter.run() @@ -89,8 +89,8 @@ class SavepointsImporterTest { val blankForm2 = createBlankForm(project, form2Name, "2") // create savepoints - val savepointFile1 = createSavepointFile(project, "${form1Name}_${System.currentTimeMillis()}.xml") - val savepointFile2 = createSavepointFile(project, "${form2Name}_${System.currentTimeMillis()}.xml") + val savepointFile1 = createSavepointFile(project, "${form1Name}_2024-04-10_01-35-41.xml") + val savepointFile2 = createSavepointFile(project, "${form2Name}_2024-04-10_01-35-42.xml") // trigger importing savepointsImporter.run() @@ -114,8 +114,8 @@ class SavepointsImporterTest { val blankForm2 = createBlankForm(project, form2Name, "1", "2", date = 2) // create savepoints - val savepointFile1 = createSavepointFile(project, "${form1Name}_${System.currentTimeMillis()}.xml") - val savepointFile2 = createSavepointFile(project, "${form2Name}_${System.currentTimeMillis()}.xml") + val savepointFile1 = createSavepointFile(project, "${form1Name}_2024-04-10_01-35-41.xml") + val savepointFile2 = createSavepointFile(project, "${form2Name}_2024-04-10_01-35-42.xml") // trigger importing savepointsImporter.run() @@ -126,7 +126,7 @@ class SavepointsImporterTest { Savepoint(blankForm1.dbId, null, savepointFile1.absolutePath, "${projectDependencyProvider.storagePathProvider.getOdkDirPath(StorageSubdirectory.INSTANCES, project.uuid)}/$form1Name/$form1Name.xml") val expectedSavepoint2 = Savepoint(blankForm2.dbId, null, savepointFile2.absolutePath, "${projectDependencyProvider.storagePathProvider.getOdkDirPath(StorageSubdirectory.INSTANCES, project.uuid)}/$form2Name/$form2Name.xml") - assertThat(savepoints, contains(expectedSavepoint2, expectedSavepoint1)) + assertThat(savepoints, contains(expectedSavepoint1, expectedSavepoint2)) } @Test @@ -257,9 +257,9 @@ class SavepointsImporterTest { return projectDependencyProvider.instancesRepository.save(InstanceFixtures.instance(displayName = formName, form = form, lastStatusChangeDate = lastStatusChangeDate, deletedDate = deletedDate)) } - private fun createSavepointFile(project: Project.Saved, instanceName: String): File { + private fun createSavepointFile(project: Project.Saved, fileName: String): File { val cacheDir = File(projectDependencyProvider.storagePathProvider.getOdkDirPath(StorageSubdirectory.CACHE, project.uuid)) - return File(cacheDir, "$instanceName.save").also { + return File(cacheDir, "$fileName.save").also { it.writeText(RandomString.randomString(10)) } }