Skip to content

Commit

Permalink
Allow importing OBS markdown files (#461)
Browse files Browse the repository at this point in the history
* Allow importing OBS markdown files.

MimeType enum instead of string literals.
Ignore creator if unable to match help RCs otherwise.
Unhide the Tree generics.
More type safety.

* Narrow down a few more OtterTree<Any> params.
  • Loading branch information
aunger authored and jsarabia committed Jul 1, 2019
1 parent 63024db commit a7adc0e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -42,7 +43,7 @@ class ResourceContainerRepository(

override fun importResourceContainer(
rc: ResourceContainer,
rcTree: Tree,
rcTree: OtterTree<CollectionOrContent>,
languageSlug: String
): Single<ImportResult> {
val dublinCore = rc.manifest.dublinCore
Expand Down Expand Up @@ -83,9 +84,15 @@ class ResourceContainerRepository(
): List<Int> {
val relatedIds = mutableListOf<Int>()
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)
Expand All @@ -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<CollectionOrContent>) {
importCollection(null, node)

relatedBundleDublinCoreId
Expand All @@ -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<CollectionOrContent>): Int? {
val collectionId = (node.value as Collection).let { collection ->
when (relatedBundleDublinCoreId) {
null -> addCollection(collection, parentId)
Expand All @@ -141,7 +148,7 @@ class ResourceContainerRepository(
}
}

val children = (node as? Tree)?.children
val children = (node as? OtterTree<CollectionOrContent>)?.children
if (children != null) {
if (collectionId != null) {
val contents = children.filter { it.value is Content }
Expand All @@ -161,11 +168,11 @@ class ResourceContainerRepository(
return collectionId
}

private fun importContent(parentId: Int, nodes: List<TreeNode>) {
private fun importContent(parentId: Int, nodes: List<OtterTreeNode<CollectionOrContent>>) {
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) {
Expand Down

0 comments on commit a7adc0e

Please sign in to comment.