Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: import resolvable force override #2599

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import io.tolgee.ProjectAuthControllerTest
import io.tolgee.development.testDataBuilder.data.ResolvableImportTestData
import io.tolgee.dtos.request.ImageUploadInfoDto
import io.tolgee.fixtures.andAssertThatJson
import io.tolgee.fixtures.andIsBadRequest
import io.tolgee.fixtures.andIsForbidden
import io.tolgee.fixtures.andIsOk
import io.tolgee.fixtures.node
import io.tolgee.fixtures.*
import io.tolgee.testing.annotations.ProjectJWTAuthTestMethod
import io.tolgee.testing.assert
import io.tolgee.util.generateImage
Expand Down Expand Up @@ -38,17 +34,19 @@ class KeyControllerResolvableImportTest : ProjectAuthControllerTest("/v2/project
projectSupplier = { testData.projectBuilder.self }
userAccount = testData.user
uploadedImageId =
imageUploadService.store(
generateImage(),
userAccount!!,
ImageUploadInfoDto(location = "My cool frame"),
).id
imageUploadService
.store(
generateImage(),
userAccount!!,
ImageUploadInfoDto(location = "My cool frame"),
).id
uploadedImageId2 =
imageUploadService.store(
generateImage(),
testData.viewOnlyUser,
ImageUploadInfoDto(location = "My cool frame"),
).id
imageUploadService
.store(
generateImage(),
testData.viewOnlyUser,
ImageUploadInfoDto(location = "My cool frame"),
).id
}

@Test
Expand Down Expand Up @@ -364,19 +362,69 @@ class KeyControllerResolvableImportTest : ProjectAuthControllerTest("/v2/project
).andIsOk
}

@ProjectJWTAuthTestMethod
@Test
fun `option force-override works`() {
performProjectAuthPost(
"keys/import-resolvable",
mapOf(
"keys" to
listOf(
mapOf(
"name" to "key-1",
"namespace" to "namespace-1",
"translations" to
mapOf(
"de" to
mapOf(
"text" to "new",
"resolution" to "FORCE_OVERRIDE",
),
"en" to
mapOf(
"text" to "new",
"resolution" to "FORCE_OVERRIDE",
),
),
),
mapOf(
"name" to "key-2",
"namespace" to "namespace-1",
"translations" to
mapOf(
"en" to
mapOf(
"text" to "existing translation",
"resolution" to "FORCE_OVERRIDE",
),
),
),
),
),
).andIsOk

executeInNewTransaction {
assertTranslationText("namespace-1", "key-1", "de", "new")
assertTranslationText("namespace-1", "key-1", "en", "new")
assertTranslationText("namespace-1", "key-2", "en", "existing translation")
}
}

fun assertTranslationText(
namespace: String?,
keyName: String,
languageTag: String,
expectedText: String,
) {
projectService.get(testData.projectBuilder.self.id)
projectService
.get(testData.projectBuilder.self.id)
.keys
.find { it.name == keyName && it.namespace?.name == namespace }!!
.translations
.find { it.language.tag == languageTag }!!
.text
.assert.isEqualTo(
.assert
.isEqualTo(
expectedText,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ enum class ImportTranslationResolution {
KEEP,
OVERRIDE,
NEW,
FORCE_OVERRIDE,
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class ImportTranslationResolvableDto(

- KEEP: Translation is not changed
- OVERRIDE: Translation is overridden
- NEW: New translation is created)
- NEW: New translation is created
- FORCE_OVERRIDE: Translation is updated, created or kept.
""",
example = "OVERRIDE",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.tolgee.service.key

import io.tolgee.constants.Message
import io.tolgee.dtos.KeyImportResolvableResult
import io.tolgee.dtos.cacheable.LanguageDto
import io.tolgee.dtos.request.ScreenshotInfoDto
import io.tolgee.dtos.request.translation.importKeysResolvable.ImportKeysResolvableItemDto
import io.tolgee.dtos.request.translation.importKeysResolvable.ImportTranslationResolution
Expand All @@ -11,11 +12,7 @@ import io.tolgee.exceptions.NotFoundException
import io.tolgee.exceptions.PermissionException
import io.tolgee.formats.convertToIcuPlurals
import io.tolgee.formats.convertToPluralIfAnyIsPlural
import io.tolgee.model.Language
import io.tolgee.model.Project
import io.tolgee.model.Project_
import io.tolgee.model.Screenshot
import io.tolgee.model.UploadedImage
import io.tolgee.model.*
import io.tolgee.model.enums.Scope
import io.tolgee.model.key.Key
import io.tolgee.model.key.Key_
Expand Down Expand Up @@ -91,6 +88,18 @@ class ResolvingKeyImporter(
}
}

if (resolvable.resolution == ImportTranslationResolution.FORCE_OVERRIDE) {
addOrUpdateTranslation(
key,
language,
resolvable.text,
translationsToModify::add,
isNew,
existingTranslation,
)
return@translations
}

if (isEmpty || (!isNew && resolvable.resolution == ImportTranslationResolution.OVERRIDE)) {
translationsToModify.add(TranslationToModify(existingTranslation!!, resolvable.text, false))
return@translations
Expand All @@ -116,6 +125,28 @@ class ResolvingKeyImporter(
return result
}

private fun addOrUpdateTranslation(
key: Key,
language: LanguageDto,
translationText: String,
addToList: (TranslationToModify) -> Boolean,
new: Boolean,
existingTranslation: Translation?,
) {
if (new) {
val translation =
Translation(translationText).apply {
this.key = key
this.language = entityManager.getReference(Language::class.java, language.id)
}
addToList(TranslationToModify(translation, translationText, true))
} else {
if (existingTranslation != null) {
JanCizmar marked this conversation as resolved.
Show resolved Hide resolved
addToList(TranslationToModify(existingTranslation, translationText, false))
}
}
}

private fun handlePluralizationAndSave(
isExistingKeyPlural: Boolean,
translationsToModify: MutableList<TranslationToModify>,
Expand Down Expand Up @@ -180,10 +211,11 @@ class ResolvingKeyImporter(
val locations = images.map { it.location }

val allReferences =
screenshotService.getKeyScreenshotReferences(
importedKeys,
locations,
).toMutableList()
screenshotService
.getKeyScreenshotReferences(
importedKeys,
locations,
).toMutableList()

val referencesToDelete = mutableListOf<KeyScreenshotReference>()

Expand Down Expand Up @@ -253,6 +285,10 @@ class ResolvingKeyImporter(
key: Key,
languageTag: String,
): Boolean {
if (resolvable.resolution == ImportTranslationResolution.FORCE_OVERRIDE) {
return false
}

if (translationExists && resolvable.resolution == ImportTranslationResolution.NEW) {
errors.add(
listOf(Message.TRANSLATION_EXISTS.code, key.namespace?.name, key.name, languageTag),
Expand Down Expand Up @@ -308,12 +344,13 @@ class ResolvingKeyImporter(
val namespaceJoin: Join<Key, Namespace> = root.fetch(Key_.namespace, JoinType.LEFT) as Join<Key, Namespace>

val predicates =
keys.map { (namespace, name) ->
cb.and(
cb.equal(root.get(Key_.name), name),
cb.equalNullable(namespaceJoin.get(Namespace_.name), namespace),
)
}.toTypedArray()
keys
.map { (namespace, name) ->
cb.and(
cb.equal(root.get(Key_.name), name),
cb.equalNullable(namespaceJoin.get(Namespace_.name), namespace),
)
}.toTypedArray()

val projectIdPath = root.get(Key_.project).get(Project_.id)

Expand All @@ -323,10 +360,12 @@ class ResolvingKeyImporter(
}

private val existingKeys by lazy {
this.getAllByNamespaceAndName(
projectId = projectEntity.id,
keys = keysToImport.map { it.namespace to it.name },
).associateBy { (it.namespace?.name to it.name) }.toMutableMap()
this
.getAllByNamespaceAndName(
projectId = projectEntity.id,
keys = keysToImport.map { it.namespace to it.name },
).associateBy { (it.namespace?.name to it.name) }
.toMutableMap()
}

private val languages by lazy {
Expand All @@ -335,16 +374,19 @@ class ResolvingKeyImporter(
}

private val keyLanguagesMap by lazy {
keysToImport.mapNotNull {
val key = existingKeys[it.namespace to it.name] ?: return@mapNotNull null
val keyLanguages = it.translations.keys.mapNotNull { tag -> languages[tag] }
key to keyLanguages
}.toMap()
keysToImport
.mapNotNull {
val key = existingKeys[it.namespace to it.name] ?: return@mapNotNull null
val keyLanguages = it.translations.keys.mapNotNull { tag -> languages[tag] }
key to keyLanguages
}.toMap()
}

private val existingTranslations by lazy {
translationService.get(keyLanguagesMap)
.groupBy { it.key.namespace?.name to it.key.name }.map { (key, translations) ->
translationService
.get(keyLanguagesMap)
.groupBy { it.key.namespace?.name to it.key.name }
.map { (key, translations) ->
key to translations.associateBy { it.language.tag }
}.toMap()
}
Expand Down
Loading