From 002fb9e8fd332cf065b4884335cc5dbbb6b44628 Mon Sep 17 00:00:00 2001 From: Maxim Date: Fri, 26 Apr 2024 17:48:41 +0400 Subject: [PATCH] Use Psalm instead of Chapter as chapter title for book of Psalms (#1132) * Use book name instead of chapter in chapter title * Use book name instead of chapter in chapter title (updated) * refactored ChapterLabel --- .../common/data/primitives/ChapterLabel.kt | 11 ++++++ .../common/data/primitives/ContentLabel.kt | 13 ++++--- .../project/VersificationTreeBuilder.kt | 12 +++---- .../project/markdown/MarkdownProjectReader.kt | 9 +++-- .../projects/DatabaseEnvironment.kt | 3 +- .../integrationtest/projects/TestRcImport.kt | 3 +- .../persistence/database/DatabaseMigrator.kt | 34 +++++++++++++++++-- .../ui/narration/NarrationViewModel.kt | 2 +- .../src/main/resources/Messages_en.properties | 1 + 9 files changed, 67 insertions(+), 21 deletions(-) create mode 100644 common/src/main/kotlin/org/wycliffeassociates/otter/common/data/primitives/ChapterLabel.kt diff --git a/common/src/main/kotlin/org/wycliffeassociates/otter/common/data/primitives/ChapterLabel.kt b/common/src/main/kotlin/org/wycliffeassociates/otter/common/data/primitives/ChapterLabel.kt new file mode 100644 index 0000000000..a5e36f85d1 --- /dev/null +++ b/common/src/main/kotlin/org/wycliffeassociates/otter/common/data/primitives/ChapterLabel.kt @@ -0,0 +1,11 @@ +package org.wycliffeassociates.otter.common.data.primitives + +const val PSALMS_SLUG = "psa" + +object ChapterLabel { + fun of(bookSlug: String): String { + return if (bookSlug.startsWith(PSALMS_SLUG)) { + ContentLabel.PSALM.value + } else ContentLabel.CHAPTER.value + } +} \ No newline at end of file diff --git a/common/src/main/kotlin/org/wycliffeassociates/otter/common/data/primitives/ContentLabel.kt b/common/src/main/kotlin/org/wycliffeassociates/otter/common/data/primitives/ContentLabel.kt index e1b92754e8..4fcf305150 100644 --- a/common/src/main/kotlin/org/wycliffeassociates/otter/common/data/primitives/ContentLabel.kt +++ b/common/src/main/kotlin/org/wycliffeassociates/otter/common/data/primitives/ContentLabel.kt @@ -26,16 +26,19 @@ package org.wycliffeassociates.otter.common.data.primitives ) enum class ContentLabel(val value: String, val type: ContentType) { CHAPTER("chapter", ContentType.META), + PSALM("psalm", ContentType.META), VERSE("verse", ContentType.TEXT), HELP_TITLE("title", ContentType.TITLE), HELP_BODY("body", ContentType.BODY); companion object { - fun of(contentType: ContentType) = when (contentType) { - ContentType.META -> CHAPTER - ContentType.TEXT -> VERSE - ContentType.TITLE -> HELP_TITLE - ContentType.BODY -> HELP_BODY + fun of(contentType: ContentType, bookSlug: String? = null): ContentLabel { + return when (contentType) { + ContentType.META -> if (bookSlug?.startsWith(PSALMS_SLUG) == true) PSALM else CHAPTER + ContentType.TEXT -> VERSE + ContentType.TITLE -> HELP_TITLE + ContentType.BODY -> HELP_BODY + } } } } diff --git a/common/src/main/kotlin/org/wycliffeassociates/otter/common/domain/resourcecontainer/project/VersificationTreeBuilder.kt b/common/src/main/kotlin/org/wycliffeassociates/otter/common/domain/resourcecontainer/project/VersificationTreeBuilder.kt index 6bf9daf609..ff9621e414 100644 --- a/common/src/main/kotlin/org/wycliffeassociates/otter/common/domain/resourcecontainer/project/VersificationTreeBuilder.kt +++ b/common/src/main/kotlin/org/wycliffeassociates/otter/common/domain/resourcecontainer/project/VersificationTreeBuilder.kt @@ -18,18 +18,12 @@ */ package org.wycliffeassociates.otter.common.domain.resourcecontainer.project -import com.fasterxml.jackson.annotation.JsonInclude -import com.fasterxml.jackson.core.JsonFactory -import com.fasterxml.jackson.databind.ObjectMapper -import com.fasterxml.jackson.module.kotlin.KotlinModule import org.wycliffeassociates.otter.common.collections.OtterTree import org.wycliffeassociates.otter.common.collections.OtterTreeNode import org.wycliffeassociates.otter.common.data.primitives.* import org.wycliffeassociates.otter.common.data.primitives.Collection import org.wycliffeassociates.otter.common.domain.resourcecontainer.toCollection -import org.wycliffeassociates.otter.common.domain.versification.ParatextVersification import org.wycliffeassociates.otter.common.domain.versification.Versification -import org.wycliffeassociates.otter.common.persistence.IDirectoryProvider import org.wycliffeassociates.otter.common.persistence.repositories.IVersificationRepository import org.wycliffeassociates.resourcecontainer.ResourceContainer import org.wycliffeassociates.resourcecontainer.entity.Project @@ -47,7 +41,7 @@ class VersificationTreeBuilder @Inject constructor( } private fun getVersification(container: ResourceContainer): Versification? { - var versificationCode = container.manifest.projects.firstOrNull()?.versification ?: return null + val versificationCode = container.manifest.projects.firstOrNull()?.versification ?: return null if (versificationCode == "") return null return versificationRepository.getVersification(versificationCode).blockingGet() } @@ -73,11 +67,13 @@ class VersificationTreeBuilder @Inject constructor( ) val projectTree = OtterTree(project.toCollection()) val chapters = versification.getChaptersInBook(project.identifier) + val chapterLabel = ChapterLabel.of(project.identifier) + for (i in 1..chapters) { val chapterCollection = Collection( sort = i, slug = "${project.identifier}_${i}", - labelKey = "chapter", + labelKey = chapterLabel, titleKey = "$i", resourceContainer = null ) diff --git a/common/src/main/kotlin/org/wycliffeassociates/otter/common/domain/resourcecontainer/project/markdown/MarkdownProjectReader.kt b/common/src/main/kotlin/org/wycliffeassociates/otter/common/domain/resourcecontainer/project/markdown/MarkdownProjectReader.kt index a7b6e196c4..613662c2a1 100644 --- a/common/src/main/kotlin/org/wycliffeassociates/otter/common/domain/resourcecontainer/project/markdown/MarkdownProjectReader.kt +++ b/common/src/main/kotlin/org/wycliffeassociates/otter/common/domain/resourcecontainer/project/markdown/MarkdownProjectReader.kt @@ -20,6 +20,7 @@ package org.wycliffeassociates.otter.common.domain.resourcecontainer.project.mar import org.wycliffeassociates.otter.common.collections.OtterTree import org.wycliffeassociates.otter.common.collections.OtterTreeNode +import org.wycliffeassociates.otter.common.data.primitives.ChapterLabel import org.wycliffeassociates.otter.common.data.primitives.Collection import org.wycliffeassociates.otter.common.data.primitives.CollectionOrContent import org.wycliffeassociates.otter.common.data.primitives.Content @@ -43,7 +44,6 @@ import java.util.ArrayDeque private val extensions = Regex(".+\\.(md|mkdn?|mdown|markdown)$", RegexOption.IGNORE_CASE) private const val PRIMARY_COLLECTION_KEY = "project" -private const val SECONDARY_COLLECTION_KEY = "chapter" class MarkdownProjectReader(private val isHelp: Boolean) : IProjectReader { private val collectionForEachFile = !isHelp @@ -135,7 +135,7 @@ class MarkdownProjectReader(private val isHelp: Boolean) : IProjectReader { ) = Collection( sort = fileToSort(file), slug = fileToSlug(file = file, projectRoot = projectRoot, project = project), - labelKey = SECONDARY_COLLECTION_KEY, + labelKey = ChapterLabel.of(fileToSlug(file = file, projectRoot = projectRoot, project = project)), titleKey = simplifyTitle(file.nameWithoutExtension), resourceContainer = null ) @@ -220,7 +220,10 @@ class MarkdownProjectReader(private val isHelp: Boolean) : IProjectReader { if (isHelp) return this.children.forEach { (it as? OtterTree)?.addMetaContents() } val labelKey = (this.value as? Collection)?.labelKey - if (labelKey == SECONDARY_COLLECTION_KEY) { + val expectedLabelKey = (this.value as? Collection)?.slug?.let { + ChapterLabel.of(it) + } + if (labelKey == expectedLabelKey) { addChild(chapterMetaNode(this)) } } diff --git a/jvm/workbookapp/src/integration-test/kotlin/integrationtest/projects/DatabaseEnvironment.kt b/jvm/workbookapp/src/integration-test/kotlin/integrationtest/projects/DatabaseEnvironment.kt index 8865bad39a..31b69e72a4 100644 --- a/jvm/workbookapp/src/integration-test/kotlin/integrationtest/projects/DatabaseEnvironment.kt +++ b/jvm/workbookapp/src/integration-test/kotlin/integrationtest/projects/DatabaseEnvironment.kt @@ -22,6 +22,7 @@ import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.annotation.JsonPropertyOrder import jooq.Tables.CONTENT_DERIVATIVE import org.junit.Assert +import org.wycliffeassociates.otter.common.data.primitives.ChapterLabel import org.wycliffeassociates.otter.common.data.primitives.Collection import org.wycliffeassociates.otter.common.data.primitives.Language import org.wycliffeassociates.otter.common.domain.collections.CreateProject @@ -136,7 +137,7 @@ class DatabaseEnvironment @Inject constructor( Assert.assertNotNull("Retrieving resource container info", rc) chapter.forEach { (slug, verseCount) -> - val entity = db.collectionDao.fetch(containerId = rc!!.id, label = "chapter", slug = slug) + val entity = db.collectionDao.fetch(containerId = rc!!.id, label = ChapterLabel.of(slug), slug = slug) Assert.assertNotNull("Retrieving chapter $slug", entity) val content = db.contentDao.fetchByCollectionId(entity!!.id) diff --git a/jvm/workbookapp/src/integration-test/kotlin/integrationtest/projects/TestRcImport.kt b/jvm/workbookapp/src/integration-test/kotlin/integrationtest/projects/TestRcImport.kt index 2dc990f5ce..1d18f0d393 100644 --- a/jvm/workbookapp/src/integration-test/kotlin/integrationtest/projects/TestRcImport.kt +++ b/jvm/workbookapp/src/integration-test/kotlin/integrationtest/projects/TestRcImport.kt @@ -170,7 +170,8 @@ class TestRcImport { "ulb", CollectionDescriptor(label = "bundle", slug = "ulb"), CollectionDescriptor(label = "project", slug = "gen"), - CollectionDescriptor(label = "chapter", slug = "gen_1") + CollectionDescriptor(label = "chapter", slug = "gen_1"), + CollectionDescriptor(label = "psalm", slug = "psa_1") ) } diff --git a/jvm/workbookapp/src/main/kotlin/org/wycliffeassociates/otter/jvm/workbookapp/persistence/database/DatabaseMigrator.kt b/jvm/workbookapp/src/main/kotlin/org/wycliffeassociates/otter/jvm/workbookapp/persistence/database/DatabaseMigrator.kt index 747f8138f5..e79909746d 100644 --- a/jvm/workbookapp/src/main/kotlin/org/wycliffeassociates/otter/jvm/workbookapp/persistence/database/DatabaseMigrator.kt +++ b/jvm/workbookapp/src/main/kotlin/org/wycliffeassociates/otter/jvm/workbookapp/persistence/database/DatabaseMigrator.kt @@ -24,13 +24,14 @@ import org.jooq.exception.DataAccessException import org.jooq.impl.DSL import org.jooq.impl.SQLDataType import org.slf4j.LoggerFactory -import org.wycliffeassociates.otter.assets.initialization.InitializeProjects +import org.wycliffeassociates.otter.common.data.primitives.ContentLabel +import org.wycliffeassociates.otter.common.data.primitives.PSALMS_SLUG import org.wycliffeassociates.otter.common.persistence.IDirectoryProvider import org.wycliffeassociates.otter.common.utils.SELECTED_TAKES_FROM_DB import java.io.File import org.wycliffeassociates.otter.common.data.primitives.CheckingStatus as CheckingStatusEnum -const val SCHEMA_VERSION = 13 +const val SCHEMA_VERSION = 14 const val DATABASE_INSTALLABLE_NAME = "DATABASE" class DatabaseMigrator( @@ -57,6 +58,7 @@ class DatabaseMigrator( currentVersion = migrate10to11(dsl, currentVersion) currentVersion = migrate11to12(dsl, currentVersion) currentVersion = migrate12to13(dsl, currentVersion) + currentVersion = migrate13to14(dsl, currentVersion) updateDatabaseVersion(dsl, currentVersion) } } @@ -473,6 +475,34 @@ class DatabaseMigrator( } } + /** + * Version 14 + * Renames 'chapter' labels of Psalms collections to 'psalm'. + */ + private fun migrate13to14(dsl: DSLContext, current: Int): Int { + return if (current < 14) { + try { + dsl + .update(CollectionEntity.COLLECTION_ENTITY) + .set(CollectionEntity.COLLECTION_ENTITY.LABEL, ContentLabel.PSALM.value) + .where( + CollectionEntity.COLLECTION_ENTITY.LABEL.eq(ContentLabel.CHAPTER.value) + .and(CollectionEntity.COLLECTION_ENTITY.SLUG.startsWith(PSALMS_SLUG)) + ) + .execute() + } catch (e: DataAccessException) { + // Exception is thrown because the column might already exist but an existence check cannot + // be performed in sqlite. + logger.error("Error in while migrating database from version 13 to 14", e) + return 13 + } + logger.info("Updated database from version 13 to 14") + 14 + } else { + current + } + } + private fun createWorkbookTypeTable(dsl: DSLContext) { dsl .createTableIfNotExists( diff --git a/jvm/workbookapp/src/main/kotlin/org/wycliffeassociates/otter/jvm/workbookapp/ui/narration/NarrationViewModel.kt b/jvm/workbookapp/src/main/kotlin/org/wycliffeassociates/otter/jvm/workbookapp/ui/narration/NarrationViewModel.kt index 30e1e005af..feb4f5d6d0 100644 --- a/jvm/workbookapp/src/main/kotlin/org/wycliffeassociates/otter/jvm/workbookapp/ui/narration/NarrationViewModel.kt +++ b/jvm/workbookapp/src/main/kotlin/org/wycliffeassociates/otter/jvm/workbookapp/ui/narration/NarrationViewModel.kt @@ -498,7 +498,7 @@ class NarrationViewModel : ViewModel() { chapterTitleProperty.set( MessageFormat.format( messages["chapterTitle"], - messages["chapter"], + messages[chapter.label], chapter.title ) ) diff --git a/jvm/workbookapp/src/main/resources/Messages_en.properties b/jvm/workbookapp/src/main/resources/Messages_en.properties index 4090796793..e6b8445d98 100644 --- a/jvm/workbookapp/src/main/resources/Messages_en.properties +++ b/jvm/workbookapp/src/main/resources/Messages_en.properties @@ -388,6 +388,7 @@ recording = Recording take = Take takes = Takes chapter = Chapter +psalm = Psalm chunk = Chunk verse = Verse oldTestament = Old Testament