diff --git a/src/main/kotlin/org/wycliffeassociates/otter/jvm/persistence/database/daos/ResourceMetadataDao.kt b/src/main/kotlin/org/wycliffeassociates/otter/jvm/persistence/database/daos/ResourceMetadataDao.kt index 422b404f..b8861d1b 100644 --- a/src/main/kotlin/org/wycliffeassociates/otter/jvm/persistence/database/daos/ResourceMetadataDao.kt +++ b/src/main/kotlin/org/wycliffeassociates/otter/jvm/persistence/database/daos/ResourceMetadataDao.kt @@ -119,6 +119,29 @@ class ResourceMetadataDao( identifier: String, creator: String, derivedFromFk: Int?, + relaxCreatorIfNoMatch: Boolean = true, + dsl: DSLContext = instanceDsl + ): ResourceMetadataEntity? { + fun flv(_creator: String?) = fetchLatestVersion( + creator = _creator, + languageSlug = languageSlug, + identifier = identifier, + derivedFromFk = derivedFromFk, + dsl = dsl + ) + + return flv(creator) + ?: if (relaxCreatorIfNoMatch) { + flv(null) + } else { + null + } + } + + private fun fetchLatestVersion(languageSlug: String, + identifier: String, + creator: String?, + derivedFromFk: Int?, dsl: DSLContext = instanceDsl ) : ResourceMetadataEntity? { return dsl @@ -127,7 +150,7 @@ class ResourceMetadataDao( .on(DUBLIN_CORE_ENTITY.LANGUAGE_FK.eq(LANGUAGE_ENTITY.ID))) .where(LANGUAGE_ENTITY.SLUG.eq(languageSlug)) .and(DUBLIN_CORE_ENTITY.IDENTIFIER.eq(identifier)) - .and(DUBLIN_CORE_ENTITY.CREATOR.eq(creator)) + .run { creator?.let { and(DUBLIN_CORE_ENTITY.CREATOR.eq(creator)) } ?: this } .and(derivedFromFk?.let(DUBLIN_CORE_ENTITY.DERIVEDFROM_FK::eq) ?: DUBLIN_CORE_ENTITY.DERIVEDFROM_FK.isNull) .orderBy(DUBLIN_CORE_ENTITY.VERSION.desc()) diff --git a/src/main/kotlin/org/wycliffeassociates/otter/jvm/persistence/repositories/CollectionRepository.kt b/src/main/kotlin/org/wycliffeassociates/otter/jvm/persistence/repositories/CollectionRepository.kt index 9fd277fc..0e05a8f7 100644 --- a/src/main/kotlin/org/wycliffeassociates/otter/jvm/persistence/repositories/CollectionRepository.kt +++ b/src/main/kotlin/org/wycliffeassociates/otter/jvm/persistence/repositories/CollectionRepository.kt @@ -12,6 +12,7 @@ import org.jooq.impl.DSL.field import org.jooq.impl.DSL.value import org.wycliffeassociates.otter.common.data.model.Collection import org.wycliffeassociates.otter.common.data.model.Language +import org.wycliffeassociates.otter.common.data.model.MimeType import org.wycliffeassociates.otter.common.data.model.ResourceMetadata import org.wycliffeassociates.otter.common.domain.mapper.mapToMetadata import org.wycliffeassociates.otter.common.persistence.IDirectoryProvider @@ -201,7 +202,7 @@ class CollectionRepository( } creator = "otter" version = metadata.version - format = "text/usfm" + format = MimeType.USFM.norm subject = metadata.subject type = "book" title = metadata.title diff --git a/src/main/kotlin/org/wycliffeassociates/otter/jvm/persistence/repositories/ResourceContainerRepository.kt b/src/main/kotlin/org/wycliffeassociates/otter/jvm/persistence/repositories/ResourceContainerRepository.kt index 1bd55391..9fbde346 100644 --- a/src/main/kotlin/org/wycliffeassociates/otter/jvm/persistence/repositories/ResourceContainerRepository.kt +++ b/src/main/kotlin/org/wycliffeassociates/otter/jvm/persistence/repositories/ResourceContainerRepository.kt @@ -7,9 +7,10 @@ import org.jooq.DSLContext import org.jooq.Record3 import org.jooq.Select import org.jooq.impl.DSL -import org.wycliffeassociates.otter.common.collections.tree.Tree -import org.wycliffeassociates.otter.common.collections.tree.TreeNode +import org.wycliffeassociates.otter.common.collections.tree.OtterTree +import org.wycliffeassociates.otter.common.collections.tree.OtterTreeNode import org.wycliffeassociates.otter.common.data.model.Collection +import org.wycliffeassociates.otter.common.data.model.CollectionOrContent import org.wycliffeassociates.otter.common.data.model.Content import org.wycliffeassociates.otter.common.data.model.ContentType import org.wycliffeassociates.otter.common.domain.mapper.mapToMetadata @@ -42,7 +43,7 @@ class ResourceContainerRepository( override fun importResourceContainer( rc: ResourceContainer, - rcTree: Tree, + rcTree: OtterTree, languageSlug: String ): Single { val dublinCore = rc.manifest.dublinCore @@ -83,9 +84,15 @@ class ResourceContainerRepository( ): List { val relatedIds = mutableListOf() relations.forEach { relation -> - val parts = relation.split('/') - // NOTE: We look for derivedFromFk=null since we are looking for the original resource container - resourceMetadataDao.fetchLatestVersion(parts[0], parts[1], creator, null, dsl) + val (languageSlug, identifier) = relation.split('/') + resourceMetadataDao.fetchLatestVersion( + languageSlug = languageSlug, + identifier = identifier, + creator = creator, + relaxCreatorIfNoMatch = true, + derivedFromFk = null, // derivedFromFk=null since we are looking for the original resource container + dsl = dsl + ) ?.let { relatedDublinCore -> resourceMetadataDao.addLink(dublinCoreFk, relatedDublinCore.id, dsl) relatedIds.add(relatedDublinCore.id) @@ -103,7 +110,7 @@ class ResourceContainerRepository( private val helpContentTypes = listOf(ContentType.TITLE, ContentType.BODY) private val dublinCoreIdDslVal = DSL.`val`(dublinCoreId) - fun import(node: TreeNode) { + fun import(node: OtterTreeNode) { importCollection(null, node) relatedBundleDublinCoreId @@ -130,7 +137,7 @@ class ResourceContainerRepository( return collectionDao.insert(entity, dsl) } - private fun importCollection(parentId: Int?, node: TreeNode): Int? { + private fun importCollection(parentId: Int?, node: OtterTreeNode): Int? { val collectionId = (node.value as Collection).let { collection -> when (relatedBundleDublinCoreId) { null -> addCollection(collection, parentId) @@ -141,7 +148,7 @@ class ResourceContainerRepository( } } - val children = (node as? Tree)?.children + val children = (node as? OtterTree)?.children if (children != null) { if (collectionId != null) { val contents = children.filter { it.value is Content } @@ -161,11 +168,11 @@ class ResourceContainerRepository( return collectionId } - private fun importContent(parentId: Int, nodes: List) { + private fun importContent(parentId: Int, nodes: List>) { val entities = nodes - .mapNotNull { (it.value as? Content) } + .mapNotNull { it.value as? Content } .map { contentMapper.mapToEntity(it).apply { collectionFk = parentId } } - contentDao.insertNoReturn(*entities.toTypedArray()) + if (entities.isNotEmpty()) contentDao.insertNoReturn(*entities.toTypedArray()) } private fun linkVerseResources(parentCollectionId: Int) {