Skip to content

Commit

Permalink
Improve detecting savepoints for blank forms
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed May 15, 2024
1 parent 65155df commit 0e0d01f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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))
}
}
Expand Down

0 comments on commit 0e0d01f

Please sign in to comment.