feat(content): 관리자 번역 후보 선택과 필드별 승인 보호를 구현한다
This commit is contained in:
@@ -9,15 +9,17 @@ import kr.co.vividnext.sodalive.aws.cloudfront.AudioContentCloudFront
|
||||
import kr.co.vividnext.sodalive.aws.s3.S3Uploader
|
||||
import kr.co.vividnext.sodalive.common.SodaException
|
||||
import kr.co.vividnext.sodalive.content.main.tab.GetContentMainTabItem
|
||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationEvent
|
||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType
|
||||
import kr.co.vividnext.sodalive.utils.generateFileName
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationSourceInvalidationService
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.application.toContentSourceSnapshot
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.context.ApplicationEventPublisher
|
||||
import org.springframework.data.domain.Pageable
|
||||
import org.springframework.data.repository.findByIdOrNull
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.PlatformTransactionManager
|
||||
import org.springframework.transaction.TransactionDefinition
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import org.springframework.transaction.support.TransactionTemplate
|
||||
import org.springframework.web.multipart.MultipartFile
|
||||
|
||||
@Service
|
||||
@@ -29,10 +31,14 @@ class AdminContentService(
|
||||
private val contentMainTabRepository: AdminContentMainTabRepository,
|
||||
private val objectMapper: ObjectMapper,
|
||||
private val s3Uploader: S3Uploader,
|
||||
private val applicationEventPublisher: ApplicationEventPublisher,
|
||||
private val sourceInvalidation: ContentTranslationSourceInvalidationService,
|
||||
transactionManager: PlatformTransactionManager,
|
||||
@Value("\${cloud.aws.s3.bucket}")
|
||||
private val bucket: String
|
||||
) {
|
||||
private val uploadTransaction = TransactionTemplate(transactionManager).apply {
|
||||
propagationBehavior = TransactionDefinition.PROPAGATION_NOT_SUPPORTED
|
||||
}
|
||||
fun getAudioContentList(status: ContentReleaseStatus, pageable: Pageable): GetAdminContentListResponse {
|
||||
val totalCount = repository.getAudioContentTotalCount(status = status)
|
||||
val audioContentAndThemeList = repository.getAudioContentList(
|
||||
@@ -99,25 +105,31 @@ class AdminContentService(
|
||||
fun updateAudioContent(coverImage: MultipartFile?, requestString: String) {
|
||||
val requestNode = objectMapper.readTree(requestString)
|
||||
val request = objectMapper.treeToValue(requestNode, UpdateAdminContentRequest::class.java)
|
||||
val audioContent = repository.findByIdOrNull(id = request.id)
|
||||
?: throw SodaException(messageKey = "admin.content.not_found")
|
||||
val audioContent = repository.findByIdOrNull(request.id) ?: throw SodaException(messageKey = "admin.content.not_found")
|
||||
|
||||
if (coverImage != null) {
|
||||
val uploadedCover = if (coverImage != null) {
|
||||
val metadata = ObjectMetadata()
|
||||
metadata.contentLength = coverImage.size
|
||||
|
||||
val fileName = generateFileName(prefix = "${request.id}-cover")
|
||||
val imagePath = s3Uploader.upload(
|
||||
inputStream = coverImage.inputStream,
|
||||
bucket = bucket,
|
||||
filePath = "audio_content_cover/${request.id}/$fileName",
|
||||
metadata = metadata
|
||||
)
|
||||
audioContent.coverImage = imagePath
|
||||
uploadTransaction.execute {
|
||||
s3Uploader.upload(
|
||||
inputStream = coverImage.inputStream,
|
||||
bucket = bucket,
|
||||
filePath = "audio_content_cover/${request.id}/$fileName",
|
||||
metadata = metadata
|
||||
)
|
||||
}
|
||||
} else if (request.isDefaultCoverImage) {
|
||||
audioContent.coverImage = "profile/default_profile.png"
|
||||
"profile/default_profile.png"
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
sourceInvalidation.lock(audioContent)
|
||||
val before = audioContent.toContentSourceSnapshot()
|
||||
if (uploadedCover != null) audioContent.coverImage = uploadedCover
|
||||
|
||||
if (request.isActive != null) {
|
||||
if (!request.isActive) {
|
||||
audioContent.releaseDate = null
|
||||
@@ -163,15 +175,7 @@ class AdminContentService(
|
||||
audioContent.settlementRatio = request.settlementRatio
|
||||
}
|
||||
|
||||
if (request.title != null || request.detail != null) {
|
||||
applicationEventPublisher.publishEvent(
|
||||
LanguageTranslationEvent(
|
||||
id = request.id,
|
||||
targetType = LanguageTranslationTargetType.CONTENT,
|
||||
waitTransactionCommit = true
|
||||
)
|
||||
)
|
||||
}
|
||||
sourceInvalidation.invalidateChanged(audioContent, before)
|
||||
}
|
||||
|
||||
fun getContentMainTabList(): List<GetContentMainTabItem> {
|
||||
|
||||
+35
-36
@@ -8,7 +8,11 @@ import kr.co.vividnext.sodalive.i18n.translation.ResourceTranslationJobScheduler
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationLanguages
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationMemoryService
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationReadModelMaterializer
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationSourceField
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationLanguageService
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationSourceInvalidationService
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationWriteService
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.application.toContentSourceSnapshot
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceSnapshot
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.PlatformTransactionManager
|
||||
@@ -29,7 +33,9 @@ data class AudioContentDetailTranslationResult(
|
||||
class AudioContentDetailTranslationService(
|
||||
private val repository: AudioContentRepository,
|
||||
private val translations: ContentTranslationRepository,
|
||||
private val detector: LanguageDetectionCacheService,
|
||||
private val languageService: ContentTranslationLanguageService,
|
||||
private val sourceInvalidation: ContentTranslationSourceInvalidationService,
|
||||
private val writer: ContentTranslationWriteService,
|
||||
private val memories: TranslationMemoryService,
|
||||
private val materializer: TranslationReadModelMaterializer,
|
||||
private val scheduler: ResourceTranslationJobScheduler,
|
||||
@@ -44,34 +50,32 @@ class AudioContentDetailTranslationService(
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
fun resolve(contentId: Long, targetLanguage: String): AudioContentDetailTranslationResult {
|
||||
var source = transaction.execute { snapshot(contentId) } ?: return AudioContentDetailTranslationResult(null)
|
||||
if (source.languageCode.isNullOrBlank()) {
|
||||
val beforeDetection = source
|
||||
if (source.sourceLanguage.isNullOrBlank()) {
|
||||
try {
|
||||
val detected = detector.detectWithCache(source.query)?.detectedLanguage
|
||||
?: return AudioContentDetailTranslationResult(source.languageCode, detectionQuery = source.query)
|
||||
source = transaction.execute {
|
||||
val content = entityManager.find(AudioContent::class.java, contentId, LockModeType.PESSIMISTIC_WRITE)
|
||||
?: return@execute null
|
||||
val current = snapshot(contentId) ?: return@execute null
|
||||
if (current == beforeDetection) content.languageCode = detected
|
||||
snapshot(contentId)
|
||||
} ?: return AudioContentDetailTranslationResult(null)
|
||||
source = languageService.detectMissing(contentId) ?: return AudioContentDetailTranslationResult(null)
|
||||
} catch (ex: Exception) {
|
||||
log.warn("Content detail language detection failed. contentId={}, error={}", contentId, ex.message)
|
||||
return AudioContentDetailTranslationResult(source.languageCode, detectionQuery = source.query)
|
||||
return AudioContentDetailTranslationResult(source.sourceLanguage, detectionQuery = source.detectionQuery)
|
||||
}
|
||||
}
|
||||
val language = source.languageCode
|
||||
if (language.isNullOrBlank()) return AudioContentDetailTranslationResult(language, detectionQuery = source.query)
|
||||
val target = targetLanguage.lowercase()
|
||||
val existing = transaction.execute {
|
||||
val content = writer.lockContent(contentId) ?: return@execute null
|
||||
sourceInvalidation.invalidateStale(content)
|
||||
source = content.toContentSourceSnapshot()
|
||||
translations.findByContentIdAndLocale(contentId, target)?.let {
|
||||
entityManager.refresh(it, LockModeType.PESSIMISTIC_WRITE)
|
||||
it.renderedPayload
|
||||
}
|
||||
}
|
||||
val language = source.sourceLanguage
|
||||
if (language.isNullOrBlank()) return AudioContentDetailTranslationResult(language, detectionQuery = source.detectionQuery)
|
||||
if (
|
||||
!TranslationLanguages.isSupported(language) || !TranslationLanguages.isSupported(target) ||
|
||||
language.lowercase() == target
|
||||
) {
|
||||
return AudioContentDetailTranslationResult(language)
|
||||
}
|
||||
|
||||
val existing = transaction.execute { translations.findByContentIdAndLocale(contentId, target)?.renderedPayload }
|
||||
if (existing != null) {
|
||||
maintainRetries(contentId, target, scheduleMissing = false)
|
||||
return result(language, existing)
|
||||
@@ -79,10 +83,19 @@ class AudioContentDetailTranslationService(
|
||||
|
||||
return try {
|
||||
source.fields.forEach { field ->
|
||||
memories.ensureMemory(LanguageTranslationTargetType.CONTENT, field.fieldKey, field.sourceText, language, target)
|
||||
val approved = transaction.execute { writer.hasCurrentApproval(contentId, target, field.fieldKey) } == true
|
||||
if (!approved) {
|
||||
memories.ensureMemory(
|
||||
LanguageTranslationTargetType.CONTENT,
|
||||
field.fieldKey,
|
||||
field.sourceText,
|
||||
language,
|
||||
target
|
||||
)
|
||||
}
|
||||
}
|
||||
val payload = transaction.execute {
|
||||
entityManager.find(AudioContent::class.java, contentId, LockModeType.PESSIMISTIC_WRITE)
|
||||
writer.lockContent(contentId)
|
||||
check(snapshot(contentId) == source) { "content translation source changed" }
|
||||
check(materializer.materialize(LanguageTranslationTargetType.CONTENT, contentId, target)) {
|
||||
"content translation is incomplete"
|
||||
@@ -125,20 +138,6 @@ class AudioContentDetailTranslationService(
|
||||
TranslatedContent(payload.title, payload.detail, payload.tags)
|
||||
)
|
||||
|
||||
private fun snapshot(contentId: Long): DetailSource? {
|
||||
val content = repository.findById(contentId).orElse(null) ?: return null
|
||||
val tags = content.audioContentHashTags.filter { it.isActive }.mapNotNull { it.hashTag?.tag }
|
||||
return DetailSource(
|
||||
content.languageCode,
|
||||
listOf(
|
||||
TranslationSourceField("title", content.title),
|
||||
TranslationSourceField("detail", content.detail),
|
||||
TranslationSourceField("tags", tags.joinToString(","))
|
||||
),
|
||||
listOf(content.title.trim(), content.detail.trim(), tags.joinToString(" ").trim())
|
||||
.filter { it.isNotBlank() }.joinToString(" ")
|
||||
)
|
||||
}
|
||||
|
||||
private data class DetailSource(val languageCode: String?, val fields: List<TranslationSourceField>, val query: String)
|
||||
private fun snapshot(contentId: Long): ContentSourceSnapshot? =
|
||||
repository.findById(contentId).orElse(null)?.toContentSourceSnapshot()
|
||||
}
|
||||
|
||||
@@ -38,6 +38,8 @@ import kr.co.vividnext.sodalive.member.MemberRepository
|
||||
import kr.co.vividnext.sodalive.member.block.BlockMemberRepository
|
||||
import kr.co.vividnext.sodalive.member.contentpreference.MemberContentPreferenceService
|
||||
import kr.co.vividnext.sodalive.utils.generateFileName
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationSourceInvalidationService
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.application.toContentSourceSnapshot
|
||||
import kr.co.vividnext.sodalive.v2.creatorname.application.CreatorLocalizedNameService
|
||||
import kr.co.vividnext.sodalive.v2.home.following.application.HomeFollowingNewsPublishService
|
||||
import kr.co.vividnext.sodalive.v2.recommendation.application.CreatorContentViewHistoryService
|
||||
@@ -97,6 +99,7 @@ class AudioContentService(
|
||||
private val homeFollowingNewsPublishService: HomeFollowingNewsPublishService,
|
||||
private val memberContentPreferenceService: MemberContentPreferenceService,
|
||||
private val subtitleJobService: SubtitleJobService,
|
||||
private val sourceInvalidation: ContentTranslationSourceInvalidationService,
|
||||
transactionManager: PlatformTransactionManager,
|
||||
|
||||
@Value("\${cloud.aws.s3.content-bucket}")
|
||||
@@ -112,6 +115,9 @@ class AudioContentService(
|
||||
private val registrationTransaction = TransactionTemplate(transactionManager).apply {
|
||||
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
||||
}
|
||||
private val uploadTransaction = TransactionTemplate(transactionManager).apply {
|
||||
propagationBehavior = TransactionDefinition.PROPAGATION_NOT_SUPPORTED
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun audioContentLike(request: PutAudioContentLikeRequest, member: Member): PutAudioContentLikeResponse {
|
||||
@@ -145,13 +151,7 @@ class AudioContentService(
|
||||
val audioContent = repository.findByIdAndCreatorId(request.contentId, member.id!!)
|
||||
?: throw SodaException(messageKey = "content.error.invalid_content_retry")
|
||||
|
||||
if (request.title != null) audioContent.title = request.title
|
||||
if (request.detail != null) audioContent.detail = request.detail
|
||||
if (request.isPointAvailable != null) audioContent.isPointAvailable = request.isPointAvailable
|
||||
audioContent.isCommentAvailable = request.isCommentAvailable
|
||||
audioContent.isAdult = request.isAdult
|
||||
|
||||
if (coverImage != null) {
|
||||
val uploadedCover = if (coverImage != null) {
|
||||
val metadata = ObjectMetadata()
|
||||
metadata.contentLength = coverImage.size
|
||||
|
||||
@@ -159,16 +159,28 @@ class AudioContentService(
|
||||
val coverImageFileName = generateFileName(prefix = "${audioContent.id}-cover")
|
||||
|
||||
// 커버 이미지 업로드
|
||||
val coverImagePath = s3Uploader.upload(
|
||||
inputStream = coverImage.inputStream,
|
||||
bucket = coverImageBucket,
|
||||
filePath = "audio_content_cover/${audioContent.id}/$coverImageFileName",
|
||||
metadata = metadata
|
||||
)
|
||||
|
||||
audioContent.coverImage = coverImagePath
|
||||
uploadTransaction.execute {
|
||||
s3Uploader.upload(
|
||||
inputStream = coverImage.inputStream,
|
||||
bucket = coverImageBucket,
|
||||
filePath = "audio_content_cover/${audioContent.id}/$coverImageFileName",
|
||||
metadata = metadata
|
||||
)
|
||||
}
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
sourceInvalidation.lock(audioContent)
|
||||
if (audioContent.member?.id != member.id) throw SodaException(messageKey = "content.error.invalid_content_retry")
|
||||
val before = audioContent.toContentSourceSnapshot()
|
||||
if (request.title != null) audioContent.title = request.title
|
||||
if (request.detail != null) audioContent.detail = request.detail
|
||||
if (request.isPointAvailable != null) audioContent.isPointAvailable = request.isPointAvailable
|
||||
audioContent.isCommentAvailable = request.isCommentAvailable
|
||||
audioContent.isAdult = request.isAdult
|
||||
if (uploadedCover != null) audioContent.coverImage = uploadedCover
|
||||
|
||||
if (!request.tags.isNullOrBlank()) {
|
||||
val normalizedTags = request.tags
|
||||
.replace("#", " #")
|
||||
@@ -201,22 +213,18 @@ class AudioContentService(
|
||||
audioContentHashTag
|
||||
}
|
||||
|
||||
audioContent.audioContentHashTags.addAll(newAudioContentHashTagList)
|
||||
val added = newAudioContentHashTagList.filter { it !in audioContent.audioContentHashTags }
|
||||
audioContent.audioContentHashTags.addAll(added)
|
||||
}
|
||||
|
||||
applicationEventPublisher.publishEvent(
|
||||
LanguageTranslationEvent(
|
||||
id = request.contentId,
|
||||
targetType = LanguageTranslationTargetType.CONTENT,
|
||||
waitTransactionCommit = true
|
||||
)
|
||||
)
|
||||
sourceInvalidation.invalidateChanged(audioContent, before)
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun deleteAudioContent(audioContentId: Long, member: Member) {
|
||||
val audioContent = repository.findByIdAndCreatorId(audioContentId, member.id!!)
|
||||
?: throw SodaException(messageKey = "content.error.invalid_content_retry")
|
||||
sourceInvalidation.lock(audioContent)
|
||||
if (audioContent.member?.id != member.id) throw SodaException(messageKey = "content.error.invalid_content_retry")
|
||||
|
||||
val pinContent = pinContentRepository.findByContentIdAndMemberId(
|
||||
contentId = audioContentId,
|
||||
@@ -412,13 +420,8 @@ class AudioContentService(
|
||||
registrationTransaction.executeWithoutResult {
|
||||
// 언어 코드가 지정되지 않은 경우, 파파고 언어 감지 API를 통해 비동기로 언어를 식별한다.
|
||||
if (audioContent.languageCode.isNullOrBlank()) {
|
||||
val papagoQuery = listOf(
|
||||
request.title.trim(),
|
||||
request.detail.trim(),
|
||||
request.tags.trim()
|
||||
)
|
||||
.filter { it.isNotBlank() }
|
||||
.joinToString(" ")
|
||||
val papagoQuery = repository.findByIdOrNull(requireNotNull(audioContent.id))
|
||||
?.toContentSourceSnapshot()?.detectionQuery ?: return@executeWithoutResult
|
||||
|
||||
applicationEventPublisher.publishEvent(
|
||||
LanguageDetectEvent(
|
||||
@@ -505,6 +508,7 @@ class AudioContentService(
|
||||
|
||||
val audioContent = repository.findByIdOrNull(contentId)
|
||||
?: throw SodaException(messageKey = "common.error.invalid_request")
|
||||
sourceInvalidation.lock(audioContent)
|
||||
|
||||
audioContent.content = content
|
||||
audioContent.duration = duration
|
||||
@@ -556,7 +560,14 @@ class AudioContentService(
|
||||
fun releaseContent() {
|
||||
val notReleasedAudioContent = repository.getNotReleaseContent()
|
||||
|
||||
for (audioContent in notReleasedAudioContent) {
|
||||
for (audioContent in notReleasedAudioContent.sortedBy { it.id }) {
|
||||
sourceInvalidation.lock(audioContent)
|
||||
if (
|
||||
audioContent.isActive || audioContent.releaseDate == null || audioContent.releaseDate!! > LocalDateTime.now() ||
|
||||
audioContent.duration == null
|
||||
) {
|
||||
continue
|
||||
}
|
||||
audioContent.isActive = true
|
||||
|
||||
applicationEventPublisher.publishEvent(
|
||||
|
||||
@@ -11,17 +11,21 @@ import kr.co.vividnext.sodalive.explorer.profile.creatorCommunity.CreatorCommuni
|
||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType
|
||||
import kr.co.vividnext.sodalive.i18n.translation.ResourceTranslationJobScheduler
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationLanguages.getTranslatableLanguageCodes
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationLanguageService
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.community.translation.application.CreatorCommunityTranslationService
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.data.repository.findByIdOrNull
|
||||
import org.springframework.scheduling.annotation.Async
|
||||
import org.springframework.stereotype.Component
|
||||
import org.springframework.transaction.PlatformTransactionManager
|
||||
import org.springframework.transaction.TransactionDefinition
|
||||
import org.springframework.transaction.annotation.Propagation
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import org.springframework.transaction.event.TransactionPhase
|
||||
import org.springframework.transaction.event.TransactionalEventListener
|
||||
import org.springframework.transaction.support.TransactionSynchronization
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager
|
||||
import org.springframework.transaction.support.TransactionTemplate
|
||||
import javax.persistence.EntityManager
|
||||
import javax.persistence.LockModeType
|
||||
|
||||
@@ -51,7 +55,6 @@ class LanguageDetectEvent(
|
||||
|
||||
@Component
|
||||
class LanguageDetectListener(
|
||||
private val audioContentRepository: AudioContentRepository,
|
||||
private val audioContentCommentRepository: AudioContentCommentRepository,
|
||||
private val chatCharacterRepository: ChatCharacterRepository,
|
||||
private val characterCommentRepository: CharacterCommentRepository,
|
||||
@@ -63,30 +66,41 @@ class LanguageDetectListener(
|
||||
private val languageDetectionCacheService: LanguageDetectionCacheService,
|
||||
private val creatorCommunityTranslationService: CreatorCommunityTranslationService,
|
||||
private val entityManager: EntityManager,
|
||||
private val resourceTranslationJobScheduler: ResourceTranslationJobScheduler
|
||||
private val resourceTranslationJobScheduler: ResourceTranslationJobScheduler,
|
||||
private val contentLanguageService: ContentTranslationLanguageService,
|
||||
transactionManager: PlatformTransactionManager
|
||||
) {
|
||||
|
||||
private val log = LoggerFactory.getLogger(LanguageDetectListener::class.java)
|
||||
private val transaction = TransactionTemplate(transactionManager).apply {
|
||||
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
||||
}
|
||||
|
||||
@Async
|
||||
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
fun detectLanguage(event: LanguageDetectEvent) {
|
||||
if (event.query.isBlank()) {
|
||||
log.debug("[LanguageDetect] query is blank. Skip language detection. targetId={}", event.id)
|
||||
return
|
||||
}
|
||||
|
||||
when (event.targetType) {
|
||||
LanguageDetectTargetType.CONTENT -> handleContentLanguageDetect(event)
|
||||
LanguageDetectTargetType.COMMENT -> handleCommentLanguageDetect(event)
|
||||
LanguageDetectTargetType.CHARACTER -> handleCharacterLanguageDetect(event)
|
||||
LanguageDetectTargetType.CHARACTER_COMMENT -> handleCharacterCommentLanguageDetect(event)
|
||||
LanguageDetectTargetType.CREATOR_CHEERS -> handleCreatorCheersLanguageDetect(event)
|
||||
LanguageDetectTargetType.SERIES -> handleSeriesLanguageDetect(event)
|
||||
LanguageDetectTargetType.ORIGINAL_WORK -> handleOriginalWorkLanguageDetect(event)
|
||||
LanguageDetectTargetType.CREATOR_CONTENT_CATEGORY -> handleCreatorContentCategoryLanguageDetect(event)
|
||||
LanguageDetectTargetType.CREATOR_COMMUNITY -> handleCreatorCommunityLanguageDetect(event)
|
||||
if (event.targetType == LanguageDetectTargetType.CONTENT) {
|
||||
contentLanguageService.detectMissing(event.id, event.query)
|
||||
return
|
||||
}
|
||||
transaction.executeWithoutResult {
|
||||
when (event.targetType) {
|
||||
LanguageDetectTargetType.CONTENT -> error("CONTENT detection uses the source guard")
|
||||
LanguageDetectTargetType.COMMENT -> handleCommentLanguageDetect(event)
|
||||
LanguageDetectTargetType.CHARACTER -> handleCharacterLanguageDetect(event)
|
||||
LanguageDetectTargetType.CHARACTER_COMMENT -> handleCharacterCommentLanguageDetect(event)
|
||||
LanguageDetectTargetType.CREATOR_CHEERS -> handleCreatorCheersLanguageDetect(event)
|
||||
LanguageDetectTargetType.SERIES -> handleSeriesLanguageDetect(event)
|
||||
LanguageDetectTargetType.ORIGINAL_WORK -> handleOriginalWorkLanguageDetect(event)
|
||||
LanguageDetectTargetType.CREATOR_CONTENT_CATEGORY -> handleCreatorContentCategoryLanguageDetect(event)
|
||||
LanguageDetectTargetType.CREATOR_COMMUNITY -> handleCreatorCommunityLanguageDetect(event)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,44 +140,6 @@ class LanguageDetectListener(
|
||||
)
|
||||
}
|
||||
|
||||
private fun handleContentLanguageDetect(event: LanguageDetectEvent) {
|
||||
val contentId = event.id
|
||||
|
||||
val audioContent = audioContentRepository.findById(contentId).orElse(null)
|
||||
if (audioContent == null) {
|
||||
log.warn("[LanguageDetect] AudioContent not found. contentId={}", contentId)
|
||||
return
|
||||
}
|
||||
|
||||
// 이미 언어 코드가 설정된 경우 호출하지 않음
|
||||
if (!audioContent.languageCode.isNullOrBlank()) {
|
||||
log.debug(
|
||||
"[LanguageDetect] languageCode already set. Skip language detection. contentId={}, languageCode={}",
|
||||
contentId,
|
||||
audioContent.languageCode
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
val langCode = detectLanguageCode(event) ?: return
|
||||
|
||||
audioContent.languageCode = langCode
|
||||
|
||||
// REQUIRES_NEW 트랜잭션 내에서 변경 내용을 저장한다.
|
||||
audioContentRepository.save(audioContent)
|
||||
|
||||
resourceTranslationJobScheduler.scheduleResourceTranslations(
|
||||
resourceType = LanguageTranslationTargetType.CONTENT,
|
||||
resourceId = contentId
|
||||
)
|
||||
|
||||
log.info(
|
||||
"[LanguageDetect] languageCode updated. contentId={}, langCode={}",
|
||||
contentId,
|
||||
langCode
|
||||
)
|
||||
}
|
||||
|
||||
private fun handleCommentLanguageDetect(event: LanguageDetectEvent) {
|
||||
val commentId = event.id
|
||||
|
||||
|
||||
@@ -4,29 +4,48 @@ import kr.co.vividnext.sodalive.i18n.translation.DetectedLanguage
|
||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageDetectionProvider
|
||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageProviderProperties
|
||||
import kr.co.vividnext.sodalive.i18n.translation.SourceTextNormalizer
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationCallControl
|
||||
import kr.co.vividnext.sodalive.i18n.translation.translationCallStop
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.PlatformTransactionManager
|
||||
import org.springframework.transaction.TransactionDefinition
|
||||
import org.springframework.transaction.annotation.Propagation
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import org.springframework.transaction.support.TransactionTemplate
|
||||
|
||||
@Service
|
||||
class LanguageDetectionCacheService(
|
||||
private val languageDetectionResultRepository: LanguageDetectionResultRepository,
|
||||
providers: List<LanguageDetectionProvider>,
|
||||
properties: LanguageProviderProperties
|
||||
properties: LanguageProviderProperties,
|
||||
transactionManager: PlatformTransactionManager
|
||||
) {
|
||||
private val providers = properties.orderedProviders(properties.detectionOrder, providers) { it.providerId }
|
||||
private val transaction = TransactionTemplate(transactionManager).apply {
|
||||
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun detectWithCache(query: String): DetectedLanguage? {
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
fun detectWithCache(query: String): DetectedLanguage? = detect(query, null)
|
||||
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
fun detectWithCache(query: String, authorizeDispatch: () -> Boolean): DetectedLanguage? {
|
||||
return detect(query, TranslationCallControl(authorizeDispatch))
|
||||
}
|
||||
|
||||
private fun detect(query: String, control: TranslationCallControl?): DetectedLanguage? {
|
||||
val normalizedQuery = SourceTextNormalizer.normalize(query)
|
||||
if (normalizedQuery.isBlank()) return null
|
||||
|
||||
val sourceHash = SourceTextNormalizer.hash(normalizedQuery)
|
||||
providers.forEach { provider ->
|
||||
val cached = languageDetectionResultRepository.findBySourceHashAndProviderAndNormalizationVersion(
|
||||
sourceHash = sourceHash,
|
||||
provider = provider.providerId.code,
|
||||
normalizationVersion = SourceTextNormalizer.NORMALIZATION_VERSION
|
||||
)
|
||||
val cached = transaction.execute {
|
||||
languageDetectionResultRepository.findBySourceHashAndProviderAndNormalizationVersion(
|
||||
sourceHash = sourceHash,
|
||||
provider = provider.providerId.code,
|
||||
normalizationVersion = SourceTextNormalizer.NORMALIZATION_VERSION
|
||||
)
|
||||
}
|
||||
if (cached != null && cached.detectedLanguage.isNotBlank()) {
|
||||
return DetectedLanguage(cached.detectedLanguage, provider.providerId)
|
||||
}
|
||||
@@ -34,26 +53,30 @@ class LanguageDetectionCacheService(
|
||||
|
||||
providers.forEach { provider ->
|
||||
val detectedLanguage = try {
|
||||
provider.detect(query)
|
||||
} catch (_: Exception) {
|
||||
control?.beforeCall()
|
||||
if (control == null) provider.detect(query) else provider.detect(query, control)
|
||||
} catch (error: Exception) {
|
||||
if (error.translationCallStop() != null) return null
|
||||
null
|
||||
}?.takeIf { it.isNotBlank() }?.lowercase() ?: return@forEach
|
||||
languageDetectionResultRepository.insertIfAbsent(
|
||||
sourceHash = sourceHash,
|
||||
sourceTextSample = normalizedQuery.take(MAX_SAMPLE_LENGTH),
|
||||
detectedLanguage = detectedLanguage,
|
||||
provider = provider.providerId.code,
|
||||
confidence = null,
|
||||
normalizationVersion = SourceTextNormalizer.NORMALIZATION_VERSION
|
||||
)
|
||||
val winner = requireNotNull(
|
||||
languageDetectionResultRepository.findDetectedLanguageForUpdate(
|
||||
val winner = transaction.execute {
|
||||
languageDetectionResultRepository.insertIfAbsent(
|
||||
sourceHash = sourceHash,
|
||||
sourceTextSample = normalizedQuery.take(MAX_SAMPLE_LENGTH),
|
||||
detectedLanguage = detectedLanguage,
|
||||
provider = provider.providerId.code,
|
||||
confidence = null,
|
||||
normalizationVersion = SourceTextNormalizer.NORMALIZATION_VERSION
|
||||
)
|
||||
)
|
||||
return DetectedLanguage(winner, provider.providerId)
|
||||
requireNotNull(
|
||||
languageDetectionResultRepository.findDetectedLanguageForUpdate(
|
||||
sourceHash = sourceHash,
|
||||
provider = provider.providerId.code,
|
||||
normalizationVersion = SourceTextNormalizer.NORMALIZATION_VERSION
|
||||
)
|
||||
)
|
||||
}
|
||||
return DetectedLanguage(requireNotNull(winner), provider.providerId)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
+3
@@ -9,6 +9,9 @@ import javax.persistence.LockModeType
|
||||
interface ContentTranslationRepository : JpaRepository<ContentTranslation, Long> {
|
||||
fun findByContentIdAndLocale(contentId: Long, locale: String): ContentTranslation?
|
||||
|
||||
@Lock(LockModeType.PESSIMISTIC_WRITE)
|
||||
fun findByContentId(contentId: Long): List<ContentTranslation>
|
||||
|
||||
@Lock(LockModeType.PESSIMISTIC_WRITE)
|
||||
@Query("select t from ContentTranslation t where t.contentId = :contentId and t.locale = :locale")
|
||||
fun findByContentIdAndLocaleForUpdate(
|
||||
|
||||
+29
-22
@@ -10,15 +10,17 @@ import kr.co.vividnext.sodalive.content.ContentPriceChangeLogRepository
|
||||
import kr.co.vividnext.sodalive.content.hashtag.AudioContentHashTag
|
||||
import kr.co.vividnext.sodalive.content.hashtag.HashTag
|
||||
import kr.co.vividnext.sodalive.content.hashtag.HashTagRepository
|
||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationEvent
|
||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType
|
||||
import kr.co.vividnext.sodalive.member.Member
|
||||
import kr.co.vividnext.sodalive.utils.generateFileName
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationSourceInvalidationService
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.application.toContentSourceSnapshot
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.context.ApplicationEventPublisher
|
||||
import org.springframework.data.domain.Pageable
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.PlatformTransactionManager
|
||||
import org.springframework.transaction.TransactionDefinition
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import org.springframework.transaction.support.TransactionTemplate
|
||||
import org.springframework.web.multipart.MultipartFile
|
||||
|
||||
@Service
|
||||
@@ -30,7 +32,8 @@ class CreatorAdminContentService(
|
||||
private val objectMapper: ObjectMapper,
|
||||
private val s3Uploader: S3Uploader,
|
||||
|
||||
private val applicationEventPublisher: ApplicationEventPublisher,
|
||||
private val sourceInvalidation: ContentTranslationSourceInvalidationService,
|
||||
transactionManager: PlatformTransactionManager,
|
||||
|
||||
@Value("\${cloud.aws.s3.bucket}")
|
||||
private val bucket: String,
|
||||
@@ -38,6 +41,9 @@ class CreatorAdminContentService(
|
||||
@Value("\${cloud.aws.cloud-front.host}")
|
||||
private val coverImageHost: String
|
||||
) {
|
||||
private val uploadTransaction = TransactionTemplate(transactionManager).apply {
|
||||
propagationBehavior = TransactionDefinition.PROPAGATION_NOT_SUPPORTED
|
||||
}
|
||||
fun getAudioContentList(pageable: Pageable, member: Member): GetCreatorAdminContentListResponse {
|
||||
val totalCount = repository.getAudioContentTotalCount(memberId = member.id!!)
|
||||
val audioContentAndThemeList = repository.getAudioContentList(
|
||||
@@ -115,20 +121,28 @@ class CreatorAdminContentService(
|
||||
val audioContent = repository.getAudioContent(memberId = member.id!!, audioContentId = request.id)
|
||||
?: throw SodaException(messageKey = "creator.admin.content.invalid_content")
|
||||
|
||||
if (coverImage != null) {
|
||||
val uploadedCover = if (coverImage != null) {
|
||||
val metadata = ObjectMetadata()
|
||||
metadata.contentLength = coverImage.size
|
||||
|
||||
val fileName = generateFileName()
|
||||
val imagePath = s3Uploader.upload(
|
||||
inputStream = coverImage.inputStream,
|
||||
bucket = bucket,
|
||||
filePath = "audio_content_cover/${request.id}/$fileName",
|
||||
metadata = metadata
|
||||
)
|
||||
audioContent.coverImage = imagePath
|
||||
uploadTransaction.execute {
|
||||
s3Uploader.upload(
|
||||
inputStream = coverImage.inputStream,
|
||||
bucket = bucket,
|
||||
filePath = "audio_content_cover/${request.id}/$fileName",
|
||||
metadata = metadata
|
||||
)
|
||||
}
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
sourceInvalidation.lock(audioContent)
|
||||
if (audioContent.member?.id != member.id) throw SodaException(messageKey = "creator.admin.content.invalid_content")
|
||||
val before = audioContent.toContentSourceSnapshot()
|
||||
if (uploadedCover != null) audioContent.coverImage = uploadedCover
|
||||
|
||||
if (request.isActive != null) {
|
||||
if (!request.isActive) {
|
||||
audioContent.releaseDate = null
|
||||
@@ -198,17 +212,10 @@ class CreatorAdminContentService(
|
||||
audioContentHashTag
|
||||
}
|
||||
|
||||
audioContent.audioContentHashTags.addAll(newAudioContentHashTagList)
|
||||
val added = newAudioContentHashTagList.filter { it !in audioContent.audioContentHashTags }
|
||||
audioContent.audioContentHashTags.addAll(added)
|
||||
}
|
||||
|
||||
if (request.title != null || request.detail != null || !request.tags.isNullOrBlank()) {
|
||||
applicationEventPublisher.publishEvent(
|
||||
LanguageTranslationEvent(
|
||||
id = request.id,
|
||||
targetType = LanguageTranslationTargetType.CONTENT,
|
||||
waitTransactionCommit = true
|
||||
)
|
||||
)
|
||||
}
|
||||
sourceInvalidation.invalidateChanged(audioContent, before)
|
||||
}
|
||||
}
|
||||
|
||||
+7
@@ -24,6 +24,8 @@ import org.springframework.data.repository.findByIdOrNull
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import org.springframework.web.multipart.MultipartFile
|
||||
import javax.persistence.EntityManager
|
||||
import javax.persistence.LockModeType
|
||||
|
||||
@Service
|
||||
@Transactional(readOnly = true)
|
||||
@@ -32,6 +34,7 @@ class CreatorAdminContentSeriesService(
|
||||
private val genreRepository: CreatorAdminContentSeriesGenreRepository,
|
||||
private val hashTagRepository: HashTagRepository,
|
||||
private val audioContentRepository: AudioContentRepository,
|
||||
private val entityManager: EntityManager,
|
||||
|
||||
private val s3Uploader: S3Uploader,
|
||||
private val objectMapper: ObjectMapper,
|
||||
@@ -275,6 +278,8 @@ class CreatorAdminContentSeriesService(
|
||||
fun addingContentToTheSeries(request: AddingContentToTheSeriesRequest, memberId: Long) {
|
||||
val series = repository.findByIdAndCreatorId(id = request.seriesId, creatorId = memberId)
|
||||
?: throw SodaException(messageKey = "creator.admin.series.invalid_access")
|
||||
entityManager.refresh(series, LockModeType.PESSIMISTIC_WRITE)
|
||||
if (series.member?.id != memberId) throw SodaException(messageKey = "creator.admin.series.invalid_access")
|
||||
|
||||
val seriesContentList = mutableListOf<SeriesContent>()
|
||||
|
||||
@@ -299,6 +304,8 @@ class CreatorAdminContentSeriesService(
|
||||
fun removeContentInTheSeries(request: RemoveContentToTheSeriesRequest, memberId: Long) {
|
||||
val series = repository.findByIdAndCreatorId(id = request.seriesId, creatorId = memberId)
|
||||
?: throw SodaException(messageKey = "creator.admin.series.invalid_access")
|
||||
entityManager.refresh(series, LockModeType.PESSIMISTIC_WRITE)
|
||||
if (series.member?.id != memberId) throw SodaException(messageKey = "creator.admin.series.invalid_access")
|
||||
|
||||
series.contentList.removeIf { it.content!!.id == request.contentId }
|
||||
}
|
||||
|
||||
@@ -40,6 +40,56 @@ class SodaMessageSource {
|
||||
Lang.EN to "Invalid request.",
|
||||
Lang.JA to "無効なリクエストです。"
|
||||
),
|
||||
"translation.selection.not_found" to mapOf(
|
||||
Lang.KO to "번역 요청 또는 대상을 찾을 수 없습니다.",
|
||||
Lang.EN to "Translation request or scope was not found.",
|
||||
Lang.JA to "翻訳リクエストまたは対象が見つかりません。"
|
||||
),
|
||||
"translation.selection.invalid_grouping" to mapOf(
|
||||
Lang.KO to "제목 그룹의 구성원과 원문 구간을 확인해 주세요.",
|
||||
Lang.EN to "Check title group members and source ranges.",
|
||||
Lang.JA to "タイトルグループのメンバーと原文の範囲を確認してください。"
|
||||
),
|
||||
"translation.selection.revision_conflict" to mapOf(
|
||||
Lang.KO to "번역 요청이 변경되었습니다. 최신 상태를 조회해 주세요.",
|
||||
Lang.EN to "The translation request changed. Reload its current state.",
|
||||
Lang.JA to "翻訳リクエストが変更されました。最新の状態を取得してください。"
|
||||
),
|
||||
"translation.selection.idempotency_conflict" to mapOf(
|
||||
Lang.KO to "같은 요청 식별자를 다른 내용에 사용할 수 없습니다.",
|
||||
Lang.EN to "The same request identifier cannot be used with a different body.",
|
||||
Lang.JA to "同じリクエスト識別子を異なる内容には使用できません。"
|
||||
),
|
||||
"translation.selection.closed" to mapOf(
|
||||
Lang.KO to "이미 종료된 번역 요청입니다.",
|
||||
Lang.EN to "The translation request is already closed.",
|
||||
Lang.JA to "翻訳リクエストは既に終了しています。"
|
||||
),
|
||||
"translation.selection.not_ready" to mapOf(
|
||||
Lang.KO to "현재 번역 요청 상태에서는 수행할 수 없습니다.",
|
||||
Lang.EN to "This operation is unavailable in the current translation request state.",
|
||||
Lang.JA to "現在の翻訳リクエストの状態では実行できません。"
|
||||
),
|
||||
"translation.selection.candidate_not_selectable" to mapOf(
|
||||
Lang.KO to "선택할 수 없는 번역 후보입니다. 요청과 필드, 언어를 확인해 주세요.",
|
||||
Lang.EN to "This translation candidate cannot be selected. Check its request, field and language.",
|
||||
Lang.JA to "選択できない翻訳候補です。リクエスト、フィールド、言語を確認してください。"
|
||||
),
|
||||
"translation.selection.incomplete_title_group" to mapOf(
|
||||
Lang.KO to "공통 제목과 모든 구성원의 개별 구간 후보를 선택해 주세요.",
|
||||
Lang.EN to "Select the common title and residual choices for every group member.",
|
||||
Lang.JA to "共通タイトルと全メンバーの個別部分の候補を選択してください。"
|
||||
),
|
||||
"translation.selection.source_changed" to mapOf(
|
||||
Lang.KO to "선택한 필드의 원문이 변경되었습니다. 새 번역 요청을 만들어 주세요.",
|
||||
Lang.EN to "The selected source field changed. Create a new translation request.",
|
||||
Lang.JA to "選択したフィールドの原文が変更されました。新しい翻訳リクエストを作成してください。"
|
||||
),
|
||||
"translation.selection.membership_changed" to mapOf(
|
||||
Lang.KO to "시리즈의 활성 구성원이 변경되었습니다. 새 번역 요청을 만들어 주세요.",
|
||||
Lang.EN to "The active series membership changed. Create a new translation request.",
|
||||
Lang.JA to "シリーズの有効なメンバーが変更されました。新しい翻訳リクエストを作成してください。"
|
||||
),
|
||||
"chat.error.room_not_found" to mapOf(
|
||||
Lang.KO to "채팅방을 찾을 수 없습니다.",
|
||||
Lang.EN to "Chat room not found.",
|
||||
|
||||
@@ -21,8 +21,8 @@ enum class TranslationJobStatus {
|
||||
name = "translation_job",
|
||||
uniqueConstraints = [
|
||||
UniqueConstraint(
|
||||
name = "uk_translation_job_resource_field_target_hash",
|
||||
columnNames = ["resource_type", "resource_id", "field_key", "target_language", "source_hash"]
|
||||
name = "uk_translation_job_resource_field_target_hash_language",
|
||||
columnNames = ["resource_type", "resource_id", "field_key", "target_language", "source_hash", "source_language"]
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
+32
-6
@@ -1,20 +1,44 @@
|
||||
package kr.co.vividnext.sodalive.i18n.translation
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository
|
||||
import org.springframework.data.jpa.repository.Lock
|
||||
import org.springframework.data.jpa.repository.Modifying
|
||||
import org.springframework.data.jpa.repository.Query
|
||||
import org.springframework.data.repository.query.Param
|
||||
import java.time.LocalDateTime
|
||||
import javax.persistence.LockModeType
|
||||
|
||||
interface TranslationJobRepository : JpaRepository<TranslationJob, Long> {
|
||||
fun findByResourceTypeAndResourceIdAndFieldKeyAndTargetLanguageAndSourceHash(
|
||||
fun findByResourceTypeAndResourceIdAndFieldKeyAndTargetLanguageAndSourceHashAndSourceLanguage(
|
||||
resourceType: LanguageTranslationTargetType,
|
||||
resourceId: Long,
|
||||
fieldKey: String,
|
||||
targetLanguage: String,
|
||||
sourceHash: String
|
||||
sourceHash: String,
|
||||
sourceLanguage: String
|
||||
): TranslationJob?
|
||||
|
||||
@Lock(LockModeType.PESSIMISTIC_WRITE)
|
||||
@Query(
|
||||
"""
|
||||
select j from TranslationJob j
|
||||
where j.resourceType = kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType.CONTENT
|
||||
and j.resourceId = :contentId
|
||||
and j.fieldKey = :fieldKey
|
||||
and j.targetLanguage = :targetLanguage
|
||||
and j.sourceHash = :sourceHash
|
||||
and j.sourceLanguage = :sourceLanguage
|
||||
order by j.id
|
||||
"""
|
||||
)
|
||||
fun findContentFieldJobsForUpdate(
|
||||
@Param("contentId") contentId: Long,
|
||||
@Param("fieldKey") fieldKey: String,
|
||||
@Param("targetLanguage") targetLanguage: String,
|
||||
@Param("sourceHash") sourceHash: String,
|
||||
@Param("sourceLanguage") sourceLanguage: String
|
||||
): List<TranslationJob>
|
||||
|
||||
fun findByResourceTypeAndResourceIdInAndTargetLanguageAndStatus(
|
||||
resourceType: LanguageTranslationTargetType,
|
||||
resourceIds: Collection<Long>,
|
||||
@@ -61,11 +85,10 @@ interface TranslationJobRepository : JpaRepository<TranslationJob, Long> {
|
||||
j.nextRetryAt = :now
|
||||
where j.id = :jobId
|
||||
and j.resourceType = kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType.CONTENT
|
||||
and j.fieldKey = 'tags'
|
||||
and j.status = kr.co.vividnext.sodalive.i18n.translation.TranslationJobStatus.COMPLETED
|
||||
"""
|
||||
)
|
||||
fun restartCompletedContentTagsJob(@Param("jobId") jobId: Long, @Param("now") now: LocalDateTime): Int
|
||||
fun restartCompletedContentFieldJob(@Param("jobId") jobId: Long, @Param("now") now: LocalDateTime): Int
|
||||
|
||||
@Query(
|
||||
"""
|
||||
@@ -75,6 +98,7 @@ interface TranslationJobRepository : JpaRepository<TranslationJob, Long> {
|
||||
and j.fieldKey = :fieldKey
|
||||
and j.targetLanguage = :targetLanguage
|
||||
and j.sourceHash = :sourceHash
|
||||
and j.sourceLanguage = :sourceLanguage
|
||||
and j.status in (kr.co.vividnext.sodalive.i18n.translation.TranslationJobStatus.PENDING, kr.co.vividnext.sodalive.i18n.translation.TranslationJobStatus.RUNNING)
|
||||
"""
|
||||
)
|
||||
@@ -83,7 +107,8 @@ interface TranslationJobRepository : JpaRepository<TranslationJob, Long> {
|
||||
@Param("resourceId") resourceId: Long,
|
||||
@Param("fieldKey") fieldKey: String,
|
||||
@Param("targetLanguage") targetLanguage: String,
|
||||
@Param("sourceHash") sourceHash: String
|
||||
@Param("sourceHash") sourceHash: String,
|
||||
@Param("sourceLanguage") sourceLanguage: String
|
||||
): TranslationJob?
|
||||
|
||||
@Query(
|
||||
@@ -91,7 +116,7 @@ interface TranslationJobRepository : JpaRepository<TranslationJob, Long> {
|
||||
select j.id
|
||||
from translation_job j
|
||||
join (
|
||||
select resource_type, resource_id, target_language
|
||||
select resource_type, resource_id, target_language, source_language
|
||||
from translation_job
|
||||
where status = 'PENDING'
|
||||
and next_retry_at <= :now
|
||||
@@ -100,6 +125,7 @@ interface TranslationJobRepository : JpaRepository<TranslationJob, Long> {
|
||||
) g on j.resource_type = g.resource_type
|
||||
and j.resource_id = g.resource_id
|
||||
and j.target_language = g.target_language
|
||||
and j.source_language = g.source_language
|
||||
where j.status = 'PENDING'
|
||||
and j.next_retry_at <= :now
|
||||
order by j.created_at asc
|
||||
|
||||
+38
-12
@@ -4,12 +4,15 @@ import kr.co.vividnext.sodalive.content.translation.ContentTranslationRepository
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import java.time.LocalDateTime
|
||||
import javax.persistence.EntityManager
|
||||
import javax.persistence.LockModeType
|
||||
|
||||
@Service
|
||||
class TranslationJobScheduler(
|
||||
private val translationJobRepository: TranslationJobRepository,
|
||||
private val translationCoordinator: TranslationCoordinator,
|
||||
private val contentTranslationRepository: ContentTranslationRepository
|
||||
private val contentTranslationRepository: ContentTranslationRepository,
|
||||
private val entityManager: EntityManager
|
||||
) {
|
||||
@Transactional
|
||||
fun scheduleMissingTranslation(
|
||||
@@ -18,7 +21,8 @@ class TranslationJobScheduler(
|
||||
fieldKey: String,
|
||||
sourceText: String,
|
||||
sourceLanguage: String,
|
||||
targetLanguage: String
|
||||
targetLanguage: String,
|
||||
refreshCompletedContentField: Boolean = false
|
||||
) {
|
||||
val normalizedText = SourceTextNormalizer.normalize(sourceText)
|
||||
if (normalizedText.isBlank()) return
|
||||
@@ -30,16 +34,38 @@ class TranslationJobScheduler(
|
||||
if (normalizedSourceLanguage == normalizedTargetLanguage) return
|
||||
|
||||
val sourceHash = SourceTextNormalizer.hash(normalizedText)
|
||||
val existingJob = translationJobRepository.findByResourceTypeAndResourceIdAndFieldKeyAndTargetLanguageAndSourceHash(
|
||||
resourceType = resourceType,
|
||||
resourceId = resourceId,
|
||||
fieldKey = fieldKey,
|
||||
targetLanguage = normalizedTargetLanguage,
|
||||
sourceHash = sourceHash
|
||||
)
|
||||
val forceContentRefresh = resourceType == LanguageTranslationTargetType.CONTENT && refreshCompletedContentField
|
||||
val existingJob = if (forceContentRefresh) {
|
||||
translationJobRepository.findContentFieldJobsForUpdate(
|
||||
resourceId,
|
||||
fieldKey,
|
||||
normalizedTargetLanguage,
|
||||
sourceHash,
|
||||
normalizedSourceLanguage
|
||||
).singleOrNull()?.also { entityManager.refresh(it, LockModeType.PESSIMISTIC_WRITE) }
|
||||
} else {
|
||||
translationJobRepository.findByResourceTypeAndResourceIdAndFieldKeyAndTargetLanguageAndSourceHashAndSourceLanguage(
|
||||
resourceType = resourceType,
|
||||
resourceId = resourceId,
|
||||
fieldKey = fieldKey,
|
||||
targetLanguage = normalizedTargetLanguage,
|
||||
sourceHash = sourceHash,
|
||||
sourceLanguage = normalizedSourceLanguage
|
||||
)
|
||||
}
|
||||
if (existingJob != null) {
|
||||
if (existingJob.sourceLanguage != normalizedSourceLanguage) return
|
||||
|
||||
if (forceContentRefresh) {
|
||||
when (existingJob.status) {
|
||||
TranslationJobStatus.FAILED, TranslationJobStatus.COMPLETED -> {
|
||||
existingJob.status = TranslationJobStatus.PENDING
|
||||
existingJob.retryCount = 0
|
||||
existingJob.lastErrorMessage = null
|
||||
existingJob.nextRetryAt = LocalDateTime.now()
|
||||
}
|
||||
TranslationJobStatus.PENDING, TranslationJobStatus.RUNNING -> Unit
|
||||
}
|
||||
return
|
||||
}
|
||||
when (existingJob.status) {
|
||||
TranslationJobStatus.FAILED ->
|
||||
translationJobRepository.restartFailedJob(requireNotNull(existingJob.id), LocalDateTime.now())
|
||||
@@ -63,7 +89,7 @@ class TranslationJobScheduler(
|
||||
)
|
||||
if (cached != null && cached.translatedText == renderedTags) return
|
||||
|
||||
translationJobRepository.restartCompletedContentTagsJob(requireNotNull(existingJob.id), LocalDateTime.now())
|
||||
translationJobRepository.restartCompletedContentFieldJob(requireNotNull(existingJob.id), LocalDateTime.now())
|
||||
}
|
||||
TranslationJobStatus.PENDING, TranslationJobStatus.RUNNING -> Unit
|
||||
}
|
||||
|
||||
@@ -33,31 +33,47 @@ class TranslationJobWorker(
|
||||
val failedJobs = mutableListOf<Pair<TranslationJob, Exception>>()
|
||||
jobs.forEach { job ->
|
||||
try {
|
||||
translationMemoryService.ensureMemory(
|
||||
job.resourceType,
|
||||
job.fieldKey,
|
||||
job.sourceText,
|
||||
job.sourceLanguage,
|
||||
job.targetLanguage
|
||||
)
|
||||
if (job.resourceType != LanguageTranslationTargetType.CONTENT || !materializer.hasCurrentContentApproval(job)) {
|
||||
translationMemoryService.ensureMemory(
|
||||
job.resourceType,
|
||||
job.fieldKey,
|
||||
job.sourceText,
|
||||
job.sourceLanguage,
|
||||
job.targetLanguage
|
||||
)
|
||||
}
|
||||
succeededJobs.add(job)
|
||||
} catch (ex: Exception) {
|
||||
failedJobs.add(job to ex)
|
||||
}
|
||||
}
|
||||
|
||||
if (failedJobs.isNotEmpty()) {
|
||||
if (failedJobs.isNotEmpty() && firstJob.resourceType != LanguageTranslationTargetType.CONTENT) {
|
||||
succeededJobs.forEach { completeJob(it.id!!) }
|
||||
failedJobs.forEach { (job, ex) -> failJob(job.id!!, ex) }
|
||||
return true
|
||||
}
|
||||
|
||||
try {
|
||||
materializer.materialize(firstJob.resourceType, firstJob.resourceId, firstJob.targetLanguage)
|
||||
succeededJobs.forEach { completeJob(it.id!!) }
|
||||
} catch (ex: Exception) {
|
||||
succeededJobs.forEach { failJob(it.id!!, ex) }
|
||||
if (succeededJobs.isNotEmpty()) {
|
||||
try {
|
||||
transactionTemplate.executeWithoutResult {
|
||||
val materialized = if (firstJob.resourceType == LanguageTranslationTargetType.CONTENT) {
|
||||
materializer.materializeContentFields(
|
||||
firstJob.resourceId,
|
||||
firstJob.targetLanguage,
|
||||
succeededJobs.map { it.fieldKey }.toSet()
|
||||
)
|
||||
} else {
|
||||
materializer.materialize(firstJob.resourceType, firstJob.resourceId, firstJob.targetLanguage)
|
||||
}
|
||||
check(materialized) { "translation materialization is incomplete" }
|
||||
succeededJobs.forEach { completeJob(it.id!!) }
|
||||
}
|
||||
} catch (ex: Exception) {
|
||||
succeededJobs.forEach { failJob(it.id!!, ex) }
|
||||
}
|
||||
}
|
||||
failedJobs.forEach { (job, ex) -> failJob(job.id!!, ex) }
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
+22
-26
@@ -6,7 +6,6 @@ import kr.co.vividnext.sodalive.chat.character.translate.AiCharacterTranslationR
|
||||
import kr.co.vividnext.sodalive.chat.original.translation.OriginalWorkTranslation
|
||||
import kr.co.vividnext.sodalive.chat.original.translation.OriginalWorkTranslationPayload
|
||||
import kr.co.vividnext.sodalive.chat.original.translation.OriginalWorkTranslationRepository
|
||||
import kr.co.vividnext.sodalive.content.AudioContent
|
||||
import kr.co.vividnext.sodalive.content.category.CategoryTranslation
|
||||
import kr.co.vividnext.sodalive.content.category.CategoryTranslationRepository
|
||||
import kr.co.vividnext.sodalive.content.series.translation.SeriesGenreTranslation
|
||||
@@ -16,11 +15,10 @@ import kr.co.vividnext.sodalive.content.series.translation.SeriesTranslationPayl
|
||||
import kr.co.vividnext.sodalive.content.series.translation.SeriesTranslationRepository
|
||||
import kr.co.vividnext.sodalive.content.theme.translation.ContentThemeTranslation
|
||||
import kr.co.vividnext.sodalive.content.theme.translation.ContentThemeTranslationRepository
|
||||
import kr.co.vividnext.sodalive.content.translation.ContentTranslation
|
||||
import kr.co.vividnext.sodalive.content.translation.ContentTranslationPayload
|
||||
import kr.co.vividnext.sodalive.content.translation.ContentTranslationRepository
|
||||
import kr.co.vividnext.sodalive.creator.admin.content.series.Series
|
||||
import kr.co.vividnext.sodalive.explorer.profile.creatorCommunity.CreatorCommunityRepository
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationWriteService
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.community.translation.adapter.out.persistence.CreatorCommunityTranslation
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.community.translation.adapter.out.persistence.CreatorCommunityTranslationRepository
|
||||
import org.springframework.stereotype.Service
|
||||
@@ -32,7 +30,6 @@ import javax.persistence.LockModeType
|
||||
class TranslationReadModelMaterializer(
|
||||
private val sourceExtractor: TranslationSourceExtractor,
|
||||
private val translationCoordinator: TranslationCoordinator,
|
||||
private val contentTranslationRepository: ContentTranslationRepository,
|
||||
private val aiCharacterTranslationRepository: AiCharacterTranslationRepository,
|
||||
private val contentThemeTranslationRepository: ContentThemeTranslationRepository,
|
||||
private val seriesTranslationRepository: SeriesTranslationRepository,
|
||||
@@ -41,15 +38,32 @@ class TranslationReadModelMaterializer(
|
||||
private val categoryTranslationRepository: CategoryTranslationRepository,
|
||||
private val creatorCommunityRepository: CreatorCommunityRepository,
|
||||
private val creatorCommunityTranslationRepository: CreatorCommunityTranslationRepository,
|
||||
private val entityManager: EntityManager
|
||||
private val entityManager: EntityManager,
|
||||
private val contentWriter: ContentTranslationWriteService
|
||||
) {
|
||||
@Transactional
|
||||
fun hasCurrentContentApproval(job: TranslationJob): Boolean = contentWriter.hasCurrentApproval(
|
||||
job.resourceId,
|
||||
job.targetLanguage,
|
||||
job.fieldKey
|
||||
)
|
||||
|
||||
@Transactional
|
||||
fun materializeContentFields(resourceId: Long, targetLanguage: String, fieldKeys: Set<String>): Boolean =
|
||||
contentWriter.materialize(
|
||||
resourceId,
|
||||
targetLanguage,
|
||||
fieldKeys.map { key -> ContentSourceFieldKey.values().single { it.key == key } }.toSet()
|
||||
)
|
||||
|
||||
@Transactional
|
||||
fun materialize(resourceType: LanguageTranslationTargetType, resourceId: Long, targetLanguage: String): Boolean {
|
||||
if (resourceType == LanguageTranslationTargetType.CONTENT) return contentWriter.materialize(resourceId, targetLanguage)
|
||||
val source = sourceExtractor.extract(resourceType, resourceId) ?: return false
|
||||
val translations = resolveTranslatedFields(source, targetLanguage.lowercase()) ?: return false
|
||||
|
||||
when (resourceType) {
|
||||
LanguageTranslationTargetType.CONTENT -> upsertContent(resourceId, targetLanguage, translations)
|
||||
LanguageTranslationTargetType.CONTENT -> error("CONTENT uses the locked field writer")
|
||||
LanguageTranslationTargetType.CHARACTER -> upsertCharacter(resourceId, targetLanguage, translations)
|
||||
LanguageTranslationTargetType.CONTENT_THEME -> upsertContentTheme(resourceId, targetLanguage, translations)
|
||||
LanguageTranslationTargetType.SERIES -> upsertSeries(resourceId, targetLanguage, translations)
|
||||
@@ -76,9 +90,7 @@ class TranslationReadModelMaterializer(
|
||||
val memory = translationCoordinator.findCached(
|
||||
sourceHash = SourceTextNormalizer.hash(normalizedText),
|
||||
sourceLanguage = source.sourceLanguage.lowercase(),
|
||||
targetLanguage = targetLanguage,
|
||||
rejectUnchangedAwsResult = source.resourceType == LanguageTranslationTargetType.CONTENT &&
|
||||
field.fieldKey == "tags"
|
||||
targetLanguage = targetLanguage
|
||||
) ?: return null
|
||||
|
||||
result[field.fieldKey] = memory.translatedText
|
||||
@@ -86,22 +98,6 @@ class TranslationReadModelMaterializer(
|
||||
return result
|
||||
}
|
||||
|
||||
private fun upsertContent(resourceId: Long, targetLanguage: String, translations: Map<String, String>) {
|
||||
val payload = ContentTranslationPayload(
|
||||
title = translations["title"].orEmpty(),
|
||||
detail = translations["detail"].orEmpty(),
|
||||
tags = translations["tags"].orEmpty()
|
||||
)
|
||||
entityManager.find(AudioContent::class.java, resourceId, LockModeType.PESSIMISTIC_WRITE)
|
||||
val existing = contentTranslationRepository.findByContentIdAndLocaleForUpdate(resourceId, targetLanguage)
|
||||
if (existing == null) {
|
||||
contentTranslationRepository.save(ContentTranslation(resourceId, targetLanguage, payload))
|
||||
} else {
|
||||
existing.renderedPayload = payload
|
||||
contentTranslationRepository.save(existing)
|
||||
}
|
||||
}
|
||||
|
||||
private fun upsertCharacter(resourceId: Long, targetLanguage: String, translations: Map<String, String>) {
|
||||
val payload = AiCharacterTranslationRenderedPayload(
|
||||
name = translations["name"].orEmpty(),
|
||||
|
||||
+4
-10
@@ -8,6 +8,7 @@ import kr.co.vividnext.sodalive.content.AudioContentRepository
|
||||
import kr.co.vividnext.sodalive.content.category.CategoryRepository
|
||||
import kr.co.vividnext.sodalive.content.theme.AudioContentThemeQueryRepository
|
||||
import kr.co.vividnext.sodalive.explorer.profile.creatorCommunity.CreatorCommunityRepository
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.application.toContentSourceSnapshot
|
||||
import org.springframework.data.repository.findByIdOrNull
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
@@ -50,20 +51,13 @@ class TranslationSourceExtractor(
|
||||
|
||||
private fun extractContent(resourceId: Long): TranslationSource? {
|
||||
val content = audioContentRepository.findByIdOrNull(resourceId) ?: return null
|
||||
val sourceLanguage = content.languageCode?.takeIf { it.isNotBlank() } ?: return null
|
||||
val tags = content.audioContentHashTags
|
||||
.filter { it.isActive }
|
||||
.mapNotNull { it.hashTag?.tag }
|
||||
.joinToString(",")
|
||||
val snapshot = content.toContentSourceSnapshot()
|
||||
val sourceLanguage = snapshot.sourceLanguage?.takeIf { it.isNotBlank() } ?: return null
|
||||
return TranslationSource(
|
||||
resourceType = LanguageTranslationTargetType.CONTENT,
|
||||
resourceId = resourceId,
|
||||
sourceLanguage = sourceLanguage,
|
||||
fields = listOf(
|
||||
TranslationSourceField("title", content.title),
|
||||
TranslationSourceField("detail", content.detail),
|
||||
TranslationSourceField("tags", tags)
|
||||
)
|
||||
fields = snapshot.fields
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.`in`.web
|
||||
|
||||
import kr.co.vividnext.sodalive.common.ApiResponse
|
||||
import kr.co.vividnext.sodalive.member.Member
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.application.TranslationSelectionApplyService
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.application.TranslationSelectionCandidateQueryService
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.application.TranslationSelectionContentsService
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.application.TranslationSelectionGenerationService
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.application.TranslationSelectionGroupingService
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.application.TranslationSelectionRequestService
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionError
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.ApplyTranslationSelectionRequest
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.CreateTranslationSelectionRequest
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.GenerateTranslationSelectionRequest
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.ReviewTranslationSelectionGroupingRequest
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.response
|
||||
import org.springframework.security.access.prepost.PreAuthorize
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.PutMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RequestParam
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/admin/audio-content/translation-requests")
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
class AdminTranslationSelectionController(
|
||||
private val requests: TranslationSelectionRequestService,
|
||||
private val grouping: TranslationSelectionGroupingService,
|
||||
private val contents: TranslationSelectionContentsService,
|
||||
private val generation: TranslationSelectionGenerationService,
|
||||
private val candidates: TranslationSelectionCandidateQueryService,
|
||||
private val applyService: TranslationSelectionApplyService
|
||||
) {
|
||||
@PostMapping
|
||||
fun create(
|
||||
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?,
|
||||
@RequestBody body: CreateTranslationSelectionRequest
|
||||
) = ApiResponse.ok(
|
||||
requests.create(
|
||||
member?.id ?: throw TranslationSelectionError.INVALID_REQUEST.exception(),
|
||||
body.clientRequestId,
|
||||
body.scope
|
||||
).response()
|
||||
)
|
||||
|
||||
@GetMapping("/{requestId}")
|
||||
fun summary(@PathVariable requestId: Long) = ApiResponse.ok(requests.get(requestId).response())
|
||||
|
||||
@GetMapping("/{requestId}/contents")
|
||||
fun contents(
|
||||
@PathVariable requestId: Long,
|
||||
@RequestParam(required = false) afterContentId: Long?,
|
||||
@RequestParam(defaultValue = "50") size: Int
|
||||
) = ApiResponse.ok(contents.contents(requestId, afterContentId, size))
|
||||
|
||||
@GetMapping("/{requestId}/grouping")
|
||||
fun grouping(@PathVariable requestId: Long) = ApiResponse.ok(grouping.get(requestId))
|
||||
|
||||
@PutMapping("/{requestId}/grouping")
|
||||
fun review(@PathVariable requestId: Long, @RequestBody body: ReviewTranslationSelectionGroupingRequest) =
|
||||
ApiResponse.ok(grouping.review(requestId, body))
|
||||
|
||||
@PostMapping("/{requestId}/cancel")
|
||||
fun cancel(@PathVariable requestId: Long) = ApiResponse.ok(requests.cancel(requestId).response())
|
||||
|
||||
@PostMapping("/{requestId}/generate")
|
||||
fun generate(@PathVariable requestId: Long, @RequestBody body: GenerateTranslationSelectionRequest) =
|
||||
ApiResponse.ok(generation.generate(requestId, body.expectedRevision).response())
|
||||
|
||||
@GetMapping("/{requestId}/candidates")
|
||||
fun candidates(
|
||||
@PathVariable requestId: Long,
|
||||
@RequestParam(required = false) afterCandidateId: Long?,
|
||||
@RequestParam(defaultValue = "50") size: Int
|
||||
) = ApiResponse.ok(candidates.page(requestId, afterCandidateId, size))
|
||||
|
||||
@PostMapping("/{requestId}/apply")
|
||||
fun apply(
|
||||
@PathVariable requestId: Long,
|
||||
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?,
|
||||
@RequestBody body: ApplyTranslationSelectionRequest
|
||||
) = ApiResponse.ok(
|
||||
applyService.apply(requestId, member?.id ?: throw TranslationSelectionError.INVALID_REQUEST.exception(), body)
|
||||
)
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.`in`.web
|
||||
|
||||
import kr.co.vividnext.sodalive.common.SodaExceptionHandler
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionError
|
||||
import org.springframework.core.Ordered
|
||||
import org.springframework.core.annotation.Order
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice
|
||||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException
|
||||
|
||||
@RestControllerAdvice(assignableTypes = [AdminTranslationSelectionController::class])
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
class TranslationSelectionInputExceptionHandler(private val handler: SodaExceptionHandler) {
|
||||
@ExceptionHandler(HttpMessageNotReadableException::class, MethodArgumentTypeMismatchException::class)
|
||||
fun invalidInput() = handler.handleSodaException(TranslationSelectionError.INVALID_REQUEST.exception())
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence
|
||||
|
||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||
import com.fasterxml.jackson.module.kotlin.readValue
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ApprovalProvenance
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||
import java.time.LocalDateTime
|
||||
import javax.persistence.AttributeConverter
|
||||
import javax.persistence.Column
|
||||
import javax.persistence.Convert
|
||||
import javax.persistence.Converter
|
||||
import javax.persistence.Entity
|
||||
import javax.persistence.EnumType
|
||||
import javax.persistence.Enumerated
|
||||
import javax.persistence.GeneratedValue
|
||||
import javax.persistence.GenerationType
|
||||
import javax.persistence.Id
|
||||
import javax.persistence.Table
|
||||
import javax.persistence.UniqueConstraint
|
||||
|
||||
@Entity
|
||||
@Table(
|
||||
name = "content_translation_override",
|
||||
uniqueConstraints = [
|
||||
UniqueConstraint(name = "uk_content_translation_override_field", columnNames = ["content_id", "locale", "field_key"])
|
||||
]
|
||||
)
|
||||
class ContentTranslationOverrideEntity(
|
||||
@Column(name = "content_id", nullable = false)
|
||||
val contentId: Long,
|
||||
@Column(nullable = false, length = 10)
|
||||
val locale: String,
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(name = "field_key", nullable = false, length = 10)
|
||||
val fieldKey: ContentSourceFieldKey,
|
||||
@Column(nullable = false, length = 67)
|
||||
var sourceFingerprint: String,
|
||||
@Column(nullable = false, columnDefinition = "text")
|
||||
var approvedText: String,
|
||||
@Convert(converter = ApprovalProvenanceConverter::class)
|
||||
@Column(nullable = false, columnDefinition = "text")
|
||||
var provenance: ApprovalProvenance,
|
||||
@Column(nullable = false, columnDefinition = "timestamp")
|
||||
var approvedAt: LocalDateTime,
|
||||
@Column(nullable = false, columnDefinition = "timestamp")
|
||||
val createdAt: LocalDateTime = approvedAt,
|
||||
@Column(nullable = false, columnDefinition = "timestamp")
|
||||
var updatedAt: LocalDateTime = approvedAt
|
||||
) {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
var id: Long? = null
|
||||
}
|
||||
|
||||
@Converter
|
||||
class ApprovalProvenanceConverter : AttributeConverter<ApprovalProvenance, String> {
|
||||
override fun convertToDatabaseColumn(attribute: ApprovalProvenance): String = mapper.writeValueAsString(attribute)
|
||||
override fun convertToEntityAttribute(dbData: String): ApprovalProvenance = mapper.readValue(dbData)
|
||||
|
||||
companion object {
|
||||
private val mapper = jacksonObjectMapper()
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository
|
||||
import org.springframework.data.jpa.repository.Lock
|
||||
import javax.persistence.LockModeType
|
||||
|
||||
interface ContentTranslationOverrideRepository : JpaRepository<ContentTranslationOverrideEntity, Long> {
|
||||
@Lock(LockModeType.PESSIMISTIC_WRITE)
|
||||
fun findByContentId(contentId: Long): List<ContentTranslationOverrideEntity>
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence
|
||||
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentFieldApproval
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentTranslationOverride
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.port.ContentTranslationOverrideStore
|
||||
import org.springframework.stereotype.Component
|
||||
import javax.persistence.EntityManager
|
||||
import javax.persistence.LockModeType
|
||||
|
||||
@Component
|
||||
class JpaContentTranslationOverrideStore(
|
||||
private val repository: ContentTranslationOverrideRepository,
|
||||
private val entityManager: EntityManager
|
||||
) : ContentTranslationOverrideStore {
|
||||
override fun findByContentId(contentId: Long): List<ContentTranslationOverride> = rows(contentId).map {
|
||||
ContentTranslationOverride(
|
||||
it.contentId,
|
||||
it.locale,
|
||||
ContentFieldApproval(it.fieldKey, it.sourceFingerprint, it.approvedText, it.provenance),
|
||||
it.approvedAt
|
||||
)
|
||||
}
|
||||
|
||||
override fun save(override: ContentTranslationOverride) {
|
||||
val approval = override.approval
|
||||
val existing = rows(override.contentId).singleOrNull { it.locale == override.locale && it.fieldKey == approval.field }
|
||||
if (existing == null) {
|
||||
repository.save(
|
||||
ContentTranslationOverrideEntity(
|
||||
override.contentId,
|
||||
override.locale,
|
||||
approval.field,
|
||||
approval.sourceFingerprint,
|
||||
approval.translatedText,
|
||||
approval.provenance,
|
||||
override.approvedAt
|
||||
)
|
||||
)
|
||||
} else {
|
||||
existing.sourceFingerprint = approval.sourceFingerprint
|
||||
existing.approvedText = approval.translatedText
|
||||
existing.provenance = approval.provenance
|
||||
existing.approvedAt = override.approvedAt
|
||||
existing.updatedAt = override.approvedAt
|
||||
}
|
||||
}
|
||||
|
||||
override fun deleteFields(contentId: Long, fields: Set<ContentSourceFieldKey>, locale: String?) {
|
||||
rows(contentId).filter { it.fieldKey in fields && (locale == null || it.locale == locale) }.forEach(repository::delete)
|
||||
}
|
||||
|
||||
private fun rows(contentId: Long): List<ContentTranslationOverrideEntity> = repository.findByContentId(contentId).onEach {
|
||||
entityManager.refresh(it, LockModeType.PESSIMISTIC_WRITE)
|
||||
}
|
||||
}
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence
|
||||
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ApprovalProvider
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.SelectionCandidateProgress
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidate
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateStatus
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationCandidateStore
|
||||
import org.springframework.stereotype.Component
|
||||
import java.time.LocalDateTime
|
||||
import javax.persistence.EntityManager
|
||||
import javax.persistence.LockModeType
|
||||
|
||||
@Component
|
||||
class JpaTranslationCandidateStore(private val entityManager: EntityManager) : TranslationCandidateStore {
|
||||
override fun save(candidate: TranslationCandidate): TranslationCandidate {
|
||||
val row = if (candidate.candidateId == 0L) {
|
||||
TranslationCandidateEntity(
|
||||
entityManager.getReference(TranslationSelectionRequestEntity::class.java, candidate.requestId),
|
||||
candidate.unit.key, candidate.unit, candidate.provider.id, candidate.provider.version, candidate.targetLanguage,
|
||||
candidate.status, candidate.translatedValues, candidate.failureCode, candidate.attempt, candidate.leaseUntil,
|
||||
candidate.fence, candidate.createdAt, candidate.updatedAt, candidate.terminalAt, candidate.linkedCandidateId
|
||||
).also { entityManager.persist(it) }
|
||||
} else {
|
||||
entityManager.getReference(TranslationCandidateEntity::class.java, candidate.candidateId).apply {
|
||||
status = candidate.status
|
||||
translatedValues = candidate.translatedValues
|
||||
failureCode = candidate.failureCode
|
||||
attempt = candidate.attempt
|
||||
leaseUntil = candidate.leaseUntil
|
||||
fence = candidate.fence
|
||||
updatedAt = candidate.updatedAt
|
||||
terminalAt = candidate.terminalAt
|
||||
}
|
||||
}
|
||||
entityManager.flush()
|
||||
return row.domain()
|
||||
}
|
||||
|
||||
override fun nextRequest(now: LocalDateTime): Long? = entityManager.createQuery(
|
||||
"select c.request.id from TranslationCandidateEntity c where c.request.status = :generating and " +
|
||||
"c.linkedCandidateId is null and (c.status = :pending or (c.status = :running and c.leaseUntil <= :now)) " +
|
||||
"order by c.id",
|
||||
java.lang.Long::class.java
|
||||
).setParameter("generating", TranslationSelectionStatus.GENERATING)
|
||||
.setParameter("pending", TranslationCandidateStatus.PENDING).setParameter("running", TranslationCandidateStatus.RUNNING)
|
||||
.setParameter("now", now)
|
||||
.setMaxResults(1).resultList.firstOrNull()?.toLong()
|
||||
|
||||
override fun lockNext(requestId: Long, now: LocalDateTime): TranslationCandidate? = entityManager.createQuery(
|
||||
"select c from TranslationCandidateEntity c where c.request.id = :requestId and " +
|
||||
"c.linkedCandidateId is null and (c.status = :pending or (c.status = :running and c.leaseUntil <= :now)) " +
|
||||
"order by c.id",
|
||||
TranslationCandidateEntity::class.java
|
||||
).setParameter("requestId", requestId).setParameter("pending", TranslationCandidateStatus.PENDING)
|
||||
.setParameter("running", TranslationCandidateStatus.RUNNING).setParameter("now", now)
|
||||
.setLockMode(LockModeType.PESSIMISTIC_WRITE).setMaxResults(1).resultList.firstOrNull()
|
||||
?.let { lock(requireNotNull(it.id)) }
|
||||
|
||||
override fun find(candidateId: Long): TranslationCandidate? =
|
||||
entityManager.find(TranslationCandidateEntity::class.java, candidateId)?.domain()
|
||||
|
||||
override fun lock(candidateId: Long): TranslationCandidate? {
|
||||
val row = entityManager.find(TranslationCandidateEntity::class.java, candidateId, LockModeType.PESSIMISTIC_WRITE)
|
||||
?: return null
|
||||
entityManager.refresh(row, LockModeType.PESSIMISTIC_WRITE)
|
||||
return row.domain()
|
||||
}
|
||||
|
||||
override fun page(requestId: Long, afterId: Long, limit: Int): List<TranslationCandidate> = entityManager.createQuery(
|
||||
"select c from TranslationCandidateEntity c where c.request.id = :requestId and c.id > :afterId order by c.id",
|
||||
TranslationCandidateEntity::class.java
|
||||
).setParameter("requestId", requestId).setParameter("afterId", afterId).setMaxResults(limit).resultList.map { it.domain() }
|
||||
|
||||
override fun progress(requestId: Long): SelectionCandidateProgress {
|
||||
val counts = entityManager.createQuery(
|
||||
"select new kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence.TranslationCandidateCount" +
|
||||
"(c.status, count(c)) from TranslationCandidateEntity c where c.request.id = :requestId group by c.status",
|
||||
TranslationCandidateCount::class.java
|
||||
).setParameter("requestId", requestId).resultList.associate { it.status to it.count.toInt() }
|
||||
val succeeded = counts[TranslationCandidateStatus.SUCCEEDED] ?: 0
|
||||
val failed = counts[TranslationCandidateStatus.FAILED] ?: 0
|
||||
return SelectionCandidateProgress(
|
||||
counts.values.sum(),
|
||||
succeeded + failed + (counts[TranslationCandidateStatus.DISCARDED] ?: 0),
|
||||
succeeded,
|
||||
failed
|
||||
)
|
||||
}
|
||||
|
||||
override fun discard(requestId: Long, now: LocalDateTime) {
|
||||
discardUnselected(requestId, emptySet(), now)
|
||||
}
|
||||
|
||||
override fun discardUnselected(requestId: Long, selectedIds: Set<Long>, now: LocalDateTime): Int {
|
||||
val rows = entityManager.createQuery(
|
||||
"select c from TranslationCandidateEntity c where c.request.id = :requestId order by c.id",
|
||||
TranslationCandidateEntity::class.java
|
||||
).setParameter("requestId", requestId).setLockMode(LockModeType.PESSIMISTIC_WRITE).resultList
|
||||
val discarded = rows.filter { it.id !in selectedIds }
|
||||
discarded.forEach {
|
||||
it.status = TranslationCandidateStatus.DISCARDED
|
||||
it.translatedValues = null
|
||||
it.failureCode = null
|
||||
it.leaseUntil = null
|
||||
it.fence++
|
||||
it.updatedAt = now
|
||||
it.terminalAt = now
|
||||
}
|
||||
entityManager.flush()
|
||||
return discarded.size
|
||||
}
|
||||
|
||||
override fun completeLinked(source: TranslationCandidate) {
|
||||
val rows = entityManager.createQuery(
|
||||
"select c from TranslationCandidateEntity c where c.request.id = :requestId " +
|
||||
"and c.linkedCandidateId = :sourceId order by c.id",
|
||||
TranslationCandidateEntity::class.java
|
||||
).setParameter("requestId", source.requestId).setParameter("sourceId", source.candidateId)
|
||||
.setLockMode(LockModeType.PESSIMISTIC_WRITE).resultList
|
||||
rows.forEach {
|
||||
it.status = source.status
|
||||
it.translatedValues = source.translatedValues
|
||||
it.failureCode = source.failureCode
|
||||
it.updatedAt = source.updatedAt
|
||||
it.terminalAt = source.terminalAt
|
||||
}
|
||||
entityManager.flush()
|
||||
}
|
||||
|
||||
private fun TranslationCandidateEntity.domain() = TranslationCandidate(
|
||||
requireNotNull(id), requireNotNull(request.id), unit, ApprovalProvider(providerId, providerVersion), targetLanguage,
|
||||
status, translatedValues, failureCode, attempt, leaseUntil, fence, createdAt, updatedAt, terminalAt, linkedCandidateId
|
||||
)
|
||||
}
|
||||
|
||||
data class TranslationCandidateCount(val status: TranslationCandidateStatus, val count: Long)
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence
|
||||
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionRequest
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationCandidateStore
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationSelectionStore
|
||||
import org.springframework.stereotype.Component
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneOffset
|
||||
import java.util.UUID
|
||||
import javax.persistence.EntityManager
|
||||
import javax.persistence.LockModeType
|
||||
|
||||
@Component
|
||||
class JpaTranslationSelectionStore(
|
||||
private val repository: TranslationSelectionRequestRepository,
|
||||
private val entityManager: EntityManager,
|
||||
private val candidates: TranslationCandidateStore
|
||||
) : TranslationSelectionStore {
|
||||
override fun find(requestId: Long): TranslationSelectionRequest? = repository.findById(requestId).orElse(null)?.domain()
|
||||
|
||||
override fun findCreation(adminId: Long, clientRequestId: UUID): TranslationSelectionRequest? =
|
||||
repository.findByCreatedByAndClientRequestId(adminId, clientRequestId.toString())?.domain()
|
||||
|
||||
override fun lock(requestId: Long): TranslationSelectionRequest? {
|
||||
val row = entityManager.find(TranslationSelectionRequestEntity::class.java, requestId, LockModeType.PESSIMISTIC_WRITE)
|
||||
?: return null
|
||||
entityManager.refresh(row, LockModeType.PESSIMISTIC_WRITE)
|
||||
return row.domain()
|
||||
}
|
||||
|
||||
override fun nextPreparation(now: LocalDateTime): TranslationSelectionRequest? = entityManager.createQuery(
|
||||
"select r from TranslationSelectionRequestEntity r where r.status = :status " +
|
||||
"and (r.preparationLeaseUntil is null or r.preparationLeaseUntil <= :now) order by r.id",
|
||||
TranslationSelectionRequestEntity::class.java
|
||||
).setParameter("status", TranslationSelectionStatus.PREPARING).setParameter("now", now)
|
||||
.setLockMode(LockModeType.PESSIMISTIC_WRITE).setMaxResults(1).resultList.firstOrNull()
|
||||
?.let { lock(requireNotNull(it.id)) }
|
||||
|
||||
override fun save(request: TranslationSelectionRequest): TranslationSelectionRequest {
|
||||
val row = if (request.requestId == 0L) {
|
||||
TranslationSelectionRequestEntity(
|
||||
request.createdBy, request.clientRequestId.toString(), request.bodyHash, request.scope,
|
||||
request.status, request.revision, request.snapshot, request.preparationAttempt,
|
||||
request.preparationLeaseUntil, request.preparationFence, request.errorCode, request.createdAt, request.updatedAt
|
||||
)
|
||||
} else {
|
||||
entityManager.getReference(TranslationSelectionRequestEntity::class.java, request.requestId).apply {
|
||||
status = request.status
|
||||
revision = request.revision
|
||||
snapshot = request.snapshot
|
||||
preparationAttempt = request.preparationAttempt
|
||||
preparationLeaseUntil = request.preparationLeaseUntil
|
||||
preparationFence = request.preparationFence
|
||||
errorCode = request.errorCode
|
||||
updatedAt = request.updatedAt
|
||||
}
|
||||
}
|
||||
row.acceptedGenerateRevision = request.acceptedGenerateRevision
|
||||
row.applyOperationId = request.applyOperationId?.toString()
|
||||
row.applyBodyHash = request.applyBodyHash
|
||||
row.applyReceipt = request.applyReceipt
|
||||
row.appliedAt = request.applyReceipt?.appliedAt?.atOffset(ZoneOffset.UTC)?.toLocalDateTime()
|
||||
return repository.saveAndFlush(row).domain()
|
||||
}
|
||||
|
||||
private fun TranslationSelectionRequestEntity.domain() = TranslationSelectionRequest(
|
||||
requireNotNull(id), createdBy, UUID.fromString(clientRequestId), bodyHash, scope, status, revision, snapshot,
|
||||
preparationAttempt, preparationLeaseUntil, preparationFence, errorCode, createdAt, updatedAt,
|
||||
acceptedGenerateRevision, candidates.progress(requireNotNull(id)), applyOperationId?.let(UUID::fromString),
|
||||
applyBodyHash, applyReceipt
|
||||
)
|
||||
}
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence
|
||||
|
||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||
import com.fasterxml.jackson.module.kotlin.readValue
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateFailure
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateStatus
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationGenerationUnit
|
||||
import org.springframework.data.jpa.repository.JpaRepository
|
||||
import java.time.LocalDateTime
|
||||
import javax.persistence.AttributeConverter
|
||||
import javax.persistence.Column
|
||||
import javax.persistence.Convert
|
||||
import javax.persistence.Converter
|
||||
import javax.persistence.Entity
|
||||
import javax.persistence.EnumType
|
||||
import javax.persistence.Enumerated
|
||||
import javax.persistence.FetchType
|
||||
import javax.persistence.GeneratedValue
|
||||
import javax.persistence.GenerationType
|
||||
import javax.persistence.Id
|
||||
import javax.persistence.Index
|
||||
import javax.persistence.JoinColumn
|
||||
import javax.persistence.ManyToOne
|
||||
import javax.persistence.Table
|
||||
import javax.persistence.UniqueConstraint
|
||||
|
||||
@Entity
|
||||
@Table(
|
||||
name = "translation_candidate",
|
||||
uniqueConstraints = [
|
||||
UniqueConstraint(
|
||||
name = "uk_translation_candidate_unit",
|
||||
columnNames = ["request_id", "unit_key", "provider_id", "target_language"]
|
||||
)
|
||||
],
|
||||
indexes = [Index(name = "ix_translation_candidate_claim", columnList = "request_id,status,lease_until,id")]
|
||||
)
|
||||
class TranslationCandidateEntity(
|
||||
@ManyToOne(fetch = FetchType.LAZY, optional = false)
|
||||
@JoinColumn(name = "request_id", nullable = false)
|
||||
val request: TranslationSelectionRequestEntity,
|
||||
@Column(name = "unit_key", nullable = false, length = 160) val unitKey: String,
|
||||
@Convert(converter = TranslationGenerationUnitConverter::class)
|
||||
@Column(nullable = false, columnDefinition = "longtext")
|
||||
val unit: TranslationGenerationUnit,
|
||||
@Column(name = "provider_id", nullable = false, length = 20) val providerId: String,
|
||||
@Column(nullable = false, length = 80) val providerVersion: String,
|
||||
@Column(name = "target_language", nullable = false, length = 10) val targetLanguage: String,
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(nullable = false, length = 16)
|
||||
var status: TranslationCandidateStatus,
|
||||
@Convert(converter = CandidateValuesConverter::class)
|
||||
@Column(columnDefinition = "longtext")
|
||||
var translatedValues: List<String>?,
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(length = 40)
|
||||
var failureCode: TranslationCandidateFailure?,
|
||||
@Column(nullable = false) var attempt: Int,
|
||||
@Column(name = "lease_until", columnDefinition = "timestamp") var leaseUntil: LocalDateTime?,
|
||||
@Column(nullable = false) var fence: Long,
|
||||
@Column(nullable = false, columnDefinition = "timestamp") val createdAt: LocalDateTime,
|
||||
@Column(nullable = false, columnDefinition = "timestamp") var updatedAt: LocalDateTime,
|
||||
@Column(columnDefinition = "timestamp") var terminalAt: LocalDateTime?,
|
||||
val linkedCandidateId: Long?
|
||||
) {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
var id: Long? = null
|
||||
}
|
||||
|
||||
interface TranslationCandidateRepository : JpaRepository<TranslationCandidateEntity, Long>
|
||||
|
||||
@Converter
|
||||
class TranslationGenerationUnitConverter : AttributeConverter<TranslationGenerationUnit, String> {
|
||||
override fun convertToDatabaseColumn(attribute: TranslationGenerationUnit): String = mapper.writeValueAsString(attribute)
|
||||
override fun convertToEntityAttribute(dbData: String): TranslationGenerationUnit = mapper.readValue(dbData)
|
||||
companion object { private val mapper = jacksonObjectMapper() }
|
||||
}
|
||||
|
||||
@Converter
|
||||
class CandidateValuesConverter : AttributeConverter<List<String>?, String?> {
|
||||
override fun convertToDatabaseColumn(attribute: List<String>?): String? = attribute?.let { mapper.writeValueAsString(it) }
|
||||
override fun convertToEntityAttribute(dbData: String?): List<String>? = dbData?.let { mapper.readValue(it) }
|
||||
companion object { private val mapper = jacksonObjectMapper() }
|
||||
}
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence
|
||||
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
|
||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||
import com.fasterxml.jackson.module.kotlin.readValue
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.SelectionRequestSnapshot
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionError
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionScope
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionApplyReceipt
|
||||
import java.time.LocalDateTime
|
||||
import javax.persistence.AttributeConverter
|
||||
import javax.persistence.Column
|
||||
import javax.persistence.Convert
|
||||
import javax.persistence.Converter
|
||||
import javax.persistence.Entity
|
||||
import javax.persistence.EnumType
|
||||
import javax.persistence.Enumerated
|
||||
import javax.persistence.GeneratedValue
|
||||
import javax.persistence.GenerationType
|
||||
import javax.persistence.Id
|
||||
import javax.persistence.Index
|
||||
import javax.persistence.Table
|
||||
import javax.persistence.UniqueConstraint
|
||||
|
||||
@Entity
|
||||
@Table(
|
||||
name = "translation_selection_request",
|
||||
uniqueConstraints = [
|
||||
UniqueConstraint(name = "uk_translation_selection_creation", columnNames = ["created_by", "client_request_id"])
|
||||
],
|
||||
indexes = [Index(name = "ix_translation_selection_prepare", columnList = "status,preparation_lease_until,id")]
|
||||
)
|
||||
class TranslationSelectionRequestEntity(
|
||||
@Column(name = "created_by", nullable = false) val createdBy: Long,
|
||||
@Column(name = "client_request_id", nullable = false, length = 36) val clientRequestId: String,
|
||||
@Column(nullable = false, length = 64) val bodyHash: String,
|
||||
@Convert(converter = SelectionScopeConverter::class)
|
||||
@Column(nullable = false, columnDefinition = "text")
|
||||
val scope: TranslationSelectionScope,
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(nullable = false, length = 24)
|
||||
var status: TranslationSelectionStatus,
|
||||
@Column(nullable = false) var revision: Long,
|
||||
@Convert(converter = SelectionSnapshotConverter::class)
|
||||
@Column(nullable = false, columnDefinition = "longtext")
|
||||
var snapshot: SelectionRequestSnapshot,
|
||||
@Column(nullable = false) var preparationAttempt: Int,
|
||||
@Column(name = "preparation_lease_until", columnDefinition = "timestamp") var preparationLeaseUntil: LocalDateTime?,
|
||||
@Column(nullable = false) var preparationFence: Long,
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(length = 40)
|
||||
var errorCode: TranslationSelectionError?,
|
||||
@Column(nullable = false, columnDefinition = "timestamp") val createdAt: LocalDateTime,
|
||||
@Column(nullable = false, columnDefinition = "timestamp") var updatedAt: LocalDateTime
|
||||
) {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
var id: Long? = null
|
||||
|
||||
var acceptedGenerateRevision: Long? = null
|
||||
|
||||
@Column(length = 36)
|
||||
var applyOperationId: String? = null
|
||||
|
||||
@Column(length = 64)
|
||||
var applyBodyHash: String? = null
|
||||
|
||||
@Column(columnDefinition = "longtext")
|
||||
@Convert(converter = SelectionReceiptConverter::class)
|
||||
var applyReceipt: TranslationSelectionApplyReceipt? = null
|
||||
|
||||
@Column(columnDefinition = "timestamp")
|
||||
var appliedAt: LocalDateTime? = null
|
||||
}
|
||||
|
||||
@Converter
|
||||
class SelectionReceiptConverter : AttributeConverter<TranslationSelectionApplyReceipt?, String?> {
|
||||
override fun convertToDatabaseColumn(attribute: TranslationSelectionApplyReceipt?): String? =
|
||||
attribute?.let { mapper.writeValueAsString(it) }
|
||||
override fun convertToEntityAttribute(dbData: String?): TranslationSelectionApplyReceipt? =
|
||||
dbData?.let { mapper.readValue(it) }
|
||||
companion object { private val mapper = jacksonObjectMapper().registerModule(JavaTimeModule()) }
|
||||
}
|
||||
|
||||
@Converter
|
||||
class SelectionScopeConverter : AttributeConverter<TranslationSelectionScope, String> {
|
||||
override fun convertToDatabaseColumn(attribute: TranslationSelectionScope): String = mapper.writeValueAsString(attribute)
|
||||
override fun convertToEntityAttribute(dbData: String): TranslationSelectionScope = mapper.readValue(dbData)
|
||||
companion object { private val mapper = jacksonObjectMapper() }
|
||||
}
|
||||
|
||||
@Converter
|
||||
class SelectionSnapshotConverter : AttributeConverter<SelectionRequestSnapshot, String> {
|
||||
override fun convertToDatabaseColumn(attribute: SelectionRequestSnapshot): String = mapper.writeValueAsString(attribute)
|
||||
override fun convertToEntityAttribute(dbData: String): SelectionRequestSnapshot = mapper.readValue(dbData)
|
||||
companion object { private val mapper = jacksonObjectMapper() }
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository
|
||||
|
||||
interface TranslationSelectionRequestRepository : JpaRepository<TranslationSelectionRequestEntity, Long> {
|
||||
fun findByCreatedByAndClientRequestId(createdBy: Long, clientRequestId: String): TranslationSelectionRequestEntity?
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.scheduler
|
||||
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.application.TranslationSelectionPreparationService
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.application.TranslationSelectionWorkerService
|
||||
import org.redisson.api.RedissonClient
|
||||
import org.springframework.scheduling.annotation.Scheduled
|
||||
import org.springframework.stereotype.Component
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
@Component
|
||||
class TranslationSelectionWorker(
|
||||
private val preparation: TranslationSelectionPreparationService,
|
||||
private val candidates: TranslationSelectionWorkerService,
|
||||
private val redisson: RedissonClient
|
||||
) {
|
||||
@Scheduled(fixedDelayString = "\${sodalive.translation-selection.fixed-delay-ms:1000}")
|
||||
fun runPendingRequests() {
|
||||
val lock = redisson.getLock("lock:translation-selection")
|
||||
if (!lock.tryLock(0, 180, TimeUnit.SECONDS)) return
|
||||
try {
|
||||
try {
|
||||
candidates.runNext()
|
||||
} finally {
|
||||
preparation.prepareNext()
|
||||
}
|
||||
} finally {
|
||||
if (lock.isHeldByCurrentThread) lock.unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||
|
||||
import kr.co.vividnext.sodalive.content.AudioContentRepository
|
||||
import kr.co.vividnext.sodalive.content.LanguageDetectionCacheService
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceSnapshot
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.PlatformTransactionManager
|
||||
import org.springframework.transaction.TransactionDefinition
|
||||
import org.springframework.transaction.annotation.Propagation
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import org.springframework.transaction.support.TransactionTemplate
|
||||
|
||||
@Service
|
||||
class ContentTranslationLanguageService(
|
||||
private val repository: AudioContentRepository,
|
||||
private val detector: LanguageDetectionCacheService,
|
||||
private val writer: ContentTranslationWriteService,
|
||||
private val invalidation: ContentTranslationSourceInvalidationService,
|
||||
transactionManager: PlatformTransactionManager
|
||||
) {
|
||||
private val transaction = TransactionTemplate(transactionManager).apply {
|
||||
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
fun detectMissing(contentId: Long, expectedQuery: String? = null): ContentSourceSnapshot? {
|
||||
val before = transaction.execute {
|
||||
repository.findById(contentId).orElse(null)?.toContentSourceSnapshot()
|
||||
} ?: return null
|
||||
if (!before.sourceLanguage.isNullOrBlank() || (expectedQuery != null && before.detectionQuery != expectedQuery)) {
|
||||
return before
|
||||
}
|
||||
val detected = detector.detectWithCache(before.detectionQuery)?.detectedLanguage ?: return before
|
||||
return transaction.execute {
|
||||
val content = writer.lockContent(contentId) ?: return@execute null
|
||||
val current = content.toContentSourceSnapshot()
|
||||
if (current == before) {
|
||||
content.languageCode = detected
|
||||
invalidation.invalidateChanged(content, current)
|
||||
}
|
||||
content.toContentSourceSnapshot()
|
||||
}
|
||||
}
|
||||
}
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||
|
||||
import kr.co.vividnext.sodalive.content.AudioContent
|
||||
import kr.co.vividnext.sodalive.content.translation.ContentTranslationPayload
|
||||
import kr.co.vividnext.sodalive.content.translation.ContentTranslationRepository
|
||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationJobScheduler
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationLanguages
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceSnapshot
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.port.ContentTranslationOverrideStore
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import javax.persistence.EntityManager
|
||||
import javax.persistence.LockModeType
|
||||
|
||||
@Service
|
||||
class ContentTranslationSourceInvalidationService(
|
||||
private val writer: ContentTranslationWriteService,
|
||||
private val overrides: ContentTranslationOverrideStore,
|
||||
private val translations: ContentTranslationRepository,
|
||||
private val scheduler: TranslationJobScheduler,
|
||||
private val entityManager: EntityManager
|
||||
) {
|
||||
@Transactional
|
||||
fun lock(content: AudioContent) = writer.lockContent(content)
|
||||
|
||||
@Transactional
|
||||
fun invalidateChanged(content: AudioContent, before: ContentSourceSnapshot) {
|
||||
val after = content.toContentSourceSnapshot()
|
||||
val changed = ContentSourceFieldKey.values().filter { before.fingerprint(it) != after.fingerprint(it) }.toSet()
|
||||
if (changed.isEmpty()) return
|
||||
val contentId = requireNotNull(content.id)
|
||||
overrides.deleteFields(contentId, changed)
|
||||
fallback(contentId, after, changed)
|
||||
schedule(contentId, after, changed)
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun invalidateStale(content: AudioContent) {
|
||||
val contentId = requireNotNull(content.id)
|
||||
val source = content.toContentSourceSnapshot()
|
||||
val stale = overrides.findByContentId(contentId).filter {
|
||||
it.approval.sourceFingerprint != source.fingerprint(it.approval.field)
|
||||
}
|
||||
stale.groupBy { it.locale }.forEach { (locale, rows) ->
|
||||
val fields = rows.map { it.approval.field }.toSet()
|
||||
overrides.deleteFields(contentId, fields, locale)
|
||||
fallback(contentId, source, fields, locale)
|
||||
schedule(contentId, source, fields)
|
||||
}
|
||||
}
|
||||
|
||||
private fun fallback(
|
||||
contentId: Long,
|
||||
source: ContentSourceSnapshot,
|
||||
changed: Set<ContentSourceFieldKey>,
|
||||
locale: String? = null
|
||||
) {
|
||||
translations.findByContentId(contentId).filter { locale == null || it.locale == locale }.forEach { row ->
|
||||
entityManager.refresh(row, LockModeType.PESSIMISTIC_WRITE)
|
||||
val previous = row.renderedPayload
|
||||
row.renderedPayload = ContentTranslationPayload(
|
||||
if (ContentSourceFieldKey.TITLE in changed) source.title else previous.title,
|
||||
if (ContentSourceFieldKey.DETAIL in changed) source.detail else previous.detail,
|
||||
if (ContentSourceFieldKey.TAGS in changed) source.tags.joinToString(",") else previous.tags
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun schedule(contentId: Long, source: ContentSourceSnapshot, changed: Set<ContentSourceFieldKey>) {
|
||||
val language = source.sourceLanguage?.takeIf { TranslationLanguages.isSupported(it) } ?: return
|
||||
TranslationLanguages.getTranslatableLanguageCodes(language).forEach { target ->
|
||||
source.fields.filter { field -> changed.any { it.key == field.fieldKey } }.forEach { field ->
|
||||
scheduler.scheduleMissingTranslation(
|
||||
LanguageTranslationTargetType.CONTENT,
|
||||
contentId,
|
||||
field.fieldKey,
|
||||
field.sourceText,
|
||||
language,
|
||||
target,
|
||||
refreshCompletedContentField = true
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||
|
||||
import kr.co.vividnext.sodalive.content.AudioContent
|
||||
import kr.co.vividnext.sodalive.creator.admin.content.series.Series
|
||||
import kr.co.vividnext.sodalive.creator.admin.content.series.SeriesContent
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceSnapshot
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionScope
|
||||
import org.springframework.stereotype.Service
|
||||
import javax.persistence.EntityManager
|
||||
import javax.persistence.LockModeType
|
||||
|
||||
fun AudioContent.toContentSourceSnapshot(): ContentSourceSnapshot = ContentSourceSnapshot(
|
||||
sourceLanguage = languageCode,
|
||||
title = title,
|
||||
detail = detail,
|
||||
tags = audioContentHashTags
|
||||
.filter { it.isActive }
|
||||
.sortedWith(compareBy(nullsLast()) { it.id })
|
||||
.mapNotNull { it.hashTag?.tag }
|
||||
)
|
||||
|
||||
@Service
|
||||
class ContentTranslationSourceService(
|
||||
private val entityManager: EntityManager,
|
||||
private val writer: ContentTranslationWriteService
|
||||
) {
|
||||
// Caller owns the request transaction; membership mutation uses the same Series parent lock.
|
||||
fun lockScope(scope: TranslationSelectionScope): List<AudioContent>? = when (scope) {
|
||||
is TranslationSelectionScope.Content -> writer.lockContent(scope.contentId)?.let { listOf(it) }
|
||||
is TranslationSelectionScope.Series -> {
|
||||
val series = entityManager.find(Series::class.java, scope.seriesId, LockModeType.PESSIMISTIC_WRITE)
|
||||
if (series == null) {
|
||||
null
|
||||
} else {
|
||||
entityManager.refresh(series, LockModeType.PESSIMISTIC_WRITE)
|
||||
if (!series.isActive) {
|
||||
null
|
||||
} else {
|
||||
linkedContentIds(scope.seriesId, true).mapNotNull(writer::lockContent).filter { it.isActive }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun linkedContentIds(seriesId: Long, lock: Boolean = false): List<Long> {
|
||||
val query = entityManager.createQuery(
|
||||
"select s from SeriesContent s where s.series.id = :id order by s.content.id",
|
||||
SeriesContent::class.java
|
||||
).setParameter("id", seriesId)
|
||||
if (lock) query.setLockMode(LockModeType.PESSIMISTIC_WRITE)
|
||||
return query.resultList.onEach { if (lock) entityManager.refresh(it, LockModeType.PESSIMISTIC_WRITE) }
|
||||
.map { requireNotNull(it.content?.id) }.distinct().sorted()
|
||||
}
|
||||
}
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||
|
||||
import kr.co.vividnext.sodalive.common.SodaException
|
||||
import kr.co.vividnext.sodalive.content.AudioContent
|
||||
import kr.co.vividnext.sodalive.content.translation.ContentTranslation
|
||||
import kr.co.vividnext.sodalive.content.translation.ContentTranslationPayload
|
||||
import kr.co.vividnext.sodalive.content.translation.ContentTranslationRepository
|
||||
import kr.co.vividnext.sodalive.i18n.translation.SourceTextNormalizer
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationCoordinator
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentFieldApproval
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceSnapshot
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentTranslationOverride
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.port.ContentTranslationOverrideStore
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneOffset
|
||||
import java.time.temporal.ChronoUnit
|
||||
import javax.persistence.EntityManager
|
||||
import javax.persistence.LockModeType
|
||||
|
||||
@Service
|
||||
class ContentTranslationWriteService(
|
||||
private val entityManager: EntityManager,
|
||||
private val translations: ContentTranslationRepository,
|
||||
private val overrides: ContentTranslationOverrideStore,
|
||||
private val coordinator: TranslationCoordinator,
|
||||
private val nowProvider: () -> LocalDateTime = { LocalDateTime.now(ZoneOffset.UTC) }
|
||||
) {
|
||||
@Transactional
|
||||
fun lockContent(contentId: Long): AudioContent? {
|
||||
val content = entityManager.find(AudioContent::class.java, contentId, LockModeType.PESSIMISTIC_WRITE) ?: return null
|
||||
lockContent(content)
|
||||
return content
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun lockContent(content: AudioContent) {
|
||||
entityManager.refresh(content, LockModeType.PESSIMISTIC_WRITE)
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun hasCurrentApproval(contentId: Long, locale: String, fieldKey: String): Boolean {
|
||||
val source = lockContent(contentId)?.toContentSourceSnapshot() ?: return false
|
||||
return overrides.findByContentId(contentId).any {
|
||||
it.locale == locale.lowercase() && it.approval.field.key == fieldKey &&
|
||||
it.approval.sourceFingerprint == source.fingerprint(it.approval.field)
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun approve(contentId: Long, locale: String, approvals: List<ContentFieldApproval>): List<ContentTranslationOverride> =
|
||||
approve(contentId, locale, approvals, nowProvider())
|
||||
|
||||
@Transactional
|
||||
fun approve(
|
||||
contentId: Long,
|
||||
locale: String,
|
||||
approvals: List<ContentFieldApproval>,
|
||||
approvedAt: LocalDateTime
|
||||
): List<ContentTranslationOverride> {
|
||||
val content = lockContent(contentId) ?: throw SodaException(messageKey = "admin.content.not_found")
|
||||
val source = content.toContentSourceSnapshot()
|
||||
if (approvals.any { it.sourceFingerprint != source.fingerprint(it.field) }) {
|
||||
throw SodaException(messageKey = "common.error.invalid_request", errorProperty = "SOURCE_CHANGED")
|
||||
}
|
||||
require(approvals.isNotEmpty() && approvals.map { it.field }.distinct().size == approvals.size)
|
||||
val target = locale.lowercase()
|
||||
val now = approvedAt.truncatedTo(ChronoUnit.SECONDS)
|
||||
val approved = approvals.map { ContentTranslationOverride(contentId, target, it, now) }
|
||||
approved.forEach(overrides::save)
|
||||
merge(contentId, target, source, approvals.associate { it.field to it.translatedText })
|
||||
return approved
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun materialize(contentId: Long, locale: String): Boolean =
|
||||
materialize(contentId, locale, ContentSourceFieldKey.values().toSet())
|
||||
|
||||
@Transactional
|
||||
fun materialize(contentId: Long, locale: String, fields: Set<ContentSourceFieldKey>): Boolean {
|
||||
val content = lockContent(contentId) ?: return false
|
||||
val source = content.toContentSourceSnapshot()
|
||||
val language = source.sourceLanguage?.takeIf { it.isNotBlank() } ?: return false
|
||||
val target = locale.lowercase()
|
||||
val selected = if (translations.findByContentIdAndLocaleForUpdate(contentId, target) == null) {
|
||||
ContentSourceFieldKey.values().toSet()
|
||||
} else {
|
||||
fields
|
||||
}
|
||||
val approved = overrides.findByContentId(contentId).filter {
|
||||
it.locale == target && it.approval.sourceFingerprint == source.fingerprint(it.approval.field)
|
||||
}.associate { it.approval.field to it.approval.translatedText }
|
||||
val values = mutableMapOf<ContentSourceFieldKey, String>()
|
||||
selected.forEach { field ->
|
||||
val raw = source.fields.single { it.fieldKey == field.key }.sourceText
|
||||
values[field] = approved[field] ?: if (SourceTextNormalizer.normalize(raw).isBlank()) {
|
||||
""
|
||||
} else {
|
||||
coordinator.findCached(
|
||||
SourceTextNormalizer.hash(raw),
|
||||
language.lowercase(),
|
||||
target,
|
||||
rejectUnchangedAwsResult = field == ContentSourceFieldKey.TAGS
|
||||
)?.translatedText ?: return false
|
||||
}
|
||||
}
|
||||
merge(contentId, target, source, values)
|
||||
return true
|
||||
}
|
||||
|
||||
private fun merge(
|
||||
contentId: Long,
|
||||
locale: String,
|
||||
source: ContentSourceSnapshot,
|
||||
values: Map<ContentSourceFieldKey, String>
|
||||
) {
|
||||
val existing = translations.findByContentIdAndLocaleForUpdate(contentId, locale)
|
||||
if (existing != null) entityManager.refresh(existing, LockModeType.PESSIMISTIC_WRITE)
|
||||
val previous = existing?.renderedPayload ?: ContentTranslationPayload(
|
||||
source.title,
|
||||
source.detail,
|
||||
source.tags.joinToString(",")
|
||||
)
|
||||
val payload = ContentTranslationPayload(
|
||||
values[ContentSourceFieldKey.TITLE] ?: previous.title,
|
||||
values[ContentSourceFieldKey.DETAIL] ?: previous.detail,
|
||||
values[ContentSourceFieldKey.TAGS] ?: previous.tags
|
||||
)
|
||||
if (existing == null) {
|
||||
translations.save(ContentTranslation(contentId, locale, payload))
|
||||
} else {
|
||||
existing.renderedPayload = payload
|
||||
}
|
||||
}
|
||||
}
|
||||
+203
@@ -0,0 +1,203 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationLanguages
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ApprovalProvenance
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentFieldApproval
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceSnapshot
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidate
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateStatus
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationGenerationUnit
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionError
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionRequest
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationUnitKind
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.ApplyTranslationSelectionRequest
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionAppliedField
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionApplyReceipt
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionChoice
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionField
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationCandidateStore
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationSelectionStore
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneOffset
|
||||
import java.time.temporal.ChronoUnit
|
||||
|
||||
@Service
|
||||
class TranslationSelectionApplyService(
|
||||
private val requests: TranslationSelectionStore,
|
||||
private val candidates: TranslationCandidateStore,
|
||||
private val sources: ContentTranslationSourceService,
|
||||
private val writer: ContentTranslationWriteService,
|
||||
private val nowProvider: () -> LocalDateTime = { LocalDateTime.now(ZoneOffset.UTC) }
|
||||
) {
|
||||
@Transactional
|
||||
fun apply(requestId: Long, adminId: Long, body: ApplyTranslationSelectionRequest): TranslationSelectionApplyReceipt {
|
||||
val request = requests.lock(requestId) ?: throw TranslationSelectionError.RESOURCE_NOT_FOUND.exception()
|
||||
val hash = body.canonicalHash()
|
||||
if (adminId <= 0) throw TranslationSelectionError.INVALID_REQUEST.exception()
|
||||
if (request.applyOperationId == body.operationId) {
|
||||
if (request.applyBodyHash != hash) throw TranslationSelectionError.IDEMPOTENCY_CONFLICT.exception()
|
||||
return requireNotNull(request.applyReceipt)
|
||||
}
|
||||
if (request.status.isTerminal()) throw TranslationSelectionError.REQUEST_CLOSED.exception()
|
||||
if (request.status != TranslationSelectionStatus.READY) throw TranslationSelectionError.REQUEST_NOT_READY.exception()
|
||||
if (request.revision != body.expectedRevision) throw TranslationSelectionError.REVISION_CONFLICT.exception()
|
||||
if (body.selections.isEmpty()) throw TranslationSelectionError.INVALID_REQUEST.exception()
|
||||
val current = sources.lockScope(request.scope).orEmpty()
|
||||
.associate { requireNotNull(it.id) to it.toContentSourceSnapshot() }
|
||||
if (body.selections.any { it is TranslationSelectionChoice.TitleGroup } &&
|
||||
current.keys != request.snapshot.memberContentIds.toSet()
|
||||
) {
|
||||
throw TranslationSelectionError.MEMBERSHIP_CHANGED.exception()
|
||||
}
|
||||
val fields = body.selections.flatMap { choice ->
|
||||
when (choice) {
|
||||
is TranslationSelectionChoice.Field -> listOf(field(request, choice))
|
||||
is TranslationSelectionChoice.TitleGroup -> group(request, choice)
|
||||
}
|
||||
}.sortedWith(compareBy({ it.contentId }, { it.targetLanguage }, { it.field.key }))
|
||||
if (fields.map { Triple(it.contentId, it.field, it.targetLanguage) }.distinct().size != fields.size) {
|
||||
throw TranslationSelectionError.INVALID_REQUEST.exception()
|
||||
}
|
||||
fields.forEach { field -> validateSource(request, field, current[field.contentId]) }
|
||||
val now = nowProvider().truncatedTo(ChronoUnit.SECONDS)
|
||||
fields.groupBy { it.contentId to it.targetLanguage }.forEach { (target, selected) ->
|
||||
writer.approve(
|
||||
target.first,
|
||||
target.second,
|
||||
selected.map {
|
||||
val key = ContentSourceFieldKey.values().single { key -> key.key == it.field.key }
|
||||
ContentFieldApproval(
|
||||
key,
|
||||
current.getValue(it.contentId).fingerprint(key),
|
||||
it.translatedValue,
|
||||
ApprovalProvenance(requestId, it.candidateIds, it.providers, adminId)
|
||||
)
|
||||
},
|
||||
now
|
||||
)
|
||||
}
|
||||
val discarded = candidates.discardUnselected(requestId, fields.flatMap { it.candidateIds }.toSet(), now)
|
||||
val receipt = TranslationSelectionApplyReceipt(
|
||||
requestId,
|
||||
body.operationId,
|
||||
TranslationSelectionStatus.APPLIED,
|
||||
now.toInstant(ZoneOffset.UTC),
|
||||
fields,
|
||||
discarded
|
||||
)
|
||||
requests.save(
|
||||
request.copy(
|
||||
status = TranslationSelectionStatus.APPLIED,
|
||||
revision = request.revision + 1,
|
||||
updatedAt = now,
|
||||
applyOperationId = body.operationId,
|
||||
applyBodyHash = hash,
|
||||
applyReceipt = receipt
|
||||
)
|
||||
)
|
||||
return receipt
|
||||
}
|
||||
|
||||
private fun field(
|
||||
request: TranslationSelectionRequest,
|
||||
choice: TranslationSelectionChoice.Field
|
||||
): TranslationSelectionAppliedField {
|
||||
if (choice.contentId <= 0 || choice.candidateId <= 0 ||
|
||||
(
|
||||
choice.field == TranslationSelectionField.TITLE &&
|
||||
request.snapshot.grouping?.groups.orEmpty().any { choice.contentId in it.memberContentIds }
|
||||
)
|
||||
) {
|
||||
throw TranslationSelectionError.INVALID_REQUEST.exception()
|
||||
}
|
||||
val unit = request.snapshot.generationUnits.singleOrNull {
|
||||
it.kind == TranslationUnitKind.FIELD && it.contentId == choice.contentId && it.field?.key == choice.field.key
|
||||
} ?: throw TranslationSelectionError.CANDIDATE_NOT_SELECTABLE.exception()
|
||||
val selected = candidate(request, choice.candidateId, unit to choice.targetLanguage)
|
||||
return TranslationSelectionAppliedField(
|
||||
choice.contentId,
|
||||
choice.field,
|
||||
choice.targetLanguage,
|
||||
requireNotNull(selected.translatedValues).single(),
|
||||
listOf(selected.candidateId),
|
||||
listOf(selected.provider)
|
||||
)
|
||||
}
|
||||
|
||||
private fun group(
|
||||
request: TranslationSelectionRequest,
|
||||
choice: TranslationSelectionChoice.TitleGroup
|
||||
): List<TranslationSelectionAppliedField> {
|
||||
val grouping = requireNotNull(request.snapshot.grouping)
|
||||
val group = grouping.groups.singleOrNull { it.groupId == choice.groupId }
|
||||
?: throw TranslationSelectionError.INCOMPLETE_TITLE_GROUP.exception()
|
||||
if (choice.residualChoices.map { it.contentId }.sorted() != group.memberContentIds.sorted()) {
|
||||
throw TranslationSelectionError.INCOMPLETE_TITLE_GROUP.exception()
|
||||
}
|
||||
val commonUnit = request.snapshot.generationUnits.single {
|
||||
it.kind == TranslationUnitKind.GROUP_COMMON && it.groupId == group.groupId
|
||||
}
|
||||
val common = candidate(request, choice.commonCandidateId, commonUnit to choice.targetLanguage)
|
||||
return choice.residualChoices.map { residualChoice ->
|
||||
val template = grouping.templates.single { it.contentId == residualChoice.contentId }.template()
|
||||
val residual = if (template.residualInputs.isEmpty()) {
|
||||
if (residualChoice.candidateId != null) throw TranslationSelectionError.INCOMPLETE_TITLE_GROUP.exception()
|
||||
null
|
||||
} else {
|
||||
val id = residualChoice.candidateId ?: throw TranslationSelectionError.INCOMPLETE_TITLE_GROUP.exception()
|
||||
val residualUnit = request.snapshot.generationUnits.single {
|
||||
it.kind == TranslationUnitKind.TITLE_RESIDUAL && it.groupId == group.groupId &&
|
||||
it.contentId == residualChoice.contentId
|
||||
}
|
||||
candidate(request, id, residualUnit to choice.targetLanguage)
|
||||
}
|
||||
val contributing = listOfNotNull(common, residual)
|
||||
TranslationSelectionAppliedField(
|
||||
residualChoice.contentId,
|
||||
TranslationSelectionField.TITLE,
|
||||
choice.targetLanguage,
|
||||
template.render(requireNotNull(common.translatedValues), residual?.translatedValues.orEmpty()),
|
||||
contributing.map { it.candidateId },
|
||||
contributing.map { it.provider }.distinct()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun candidate(
|
||||
request: TranslationSelectionRequest,
|
||||
candidateId: Long,
|
||||
target: Pair<TranslationGenerationUnit, String>
|
||||
): TranslationCandidate {
|
||||
val (unit, language) = target
|
||||
if (candidateId <= 0 || language !in TranslationLanguages.getTranslatableLanguageCodes(null)) {
|
||||
throw TranslationSelectionError.INVALID_REQUEST.exception()
|
||||
}
|
||||
val candidate = candidates.lock(candidateId) ?: throw TranslationSelectionError.CANDIDATE_NOT_SELECTABLE.exception()
|
||||
if (request.acceptedGenerateRevision == null || candidate.requestId != request.requestId ||
|
||||
candidate.status != TranslationCandidateStatus.SUCCEEDED || candidate.unit != unit ||
|
||||
candidate.provider !in request.snapshot.providerManifest || candidate.targetLanguage != language ||
|
||||
language !in request.snapshot.targetLanguages ||
|
||||
language !in TranslationLanguages.getTranslatableLanguageCodes(unit.sourceLanguage) ||
|
||||
candidate.translatedValues?.size != unit.inputValues.size || candidate.translatedValues.any { it.isBlank() }
|
||||
) {
|
||||
throw TranslationSelectionError.CANDIDATE_NOT_SELECTABLE.exception()
|
||||
}
|
||||
return candidate
|
||||
}
|
||||
|
||||
private fun validateSource(
|
||||
request: TranslationSelectionRequest,
|
||||
field: TranslationSelectionAppliedField,
|
||||
current: ContentSourceSnapshot?
|
||||
) {
|
||||
val frozen = request.snapshot.contents.singleOrNull { it.contentId == field.contentId }
|
||||
val key = ContentSourceFieldKey.values().single { it.key == field.field.key }
|
||||
if (current == null || frozen == null || current.fingerprint(key) != frozen.source().fingerprint(key)) {
|
||||
throw TranslationSelectionError.SOURCE_CHANGED.exception()
|
||||
}
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionError
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationCandidatesResponse
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.response
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationCandidateStore
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationSelectionStore
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
|
||||
@Service
|
||||
class TranslationSelectionCandidateQueryService(
|
||||
private val requests: TranslationSelectionStore,
|
||||
private val candidates: TranslationCandidateStore
|
||||
) {
|
||||
@Transactional(readOnly = true)
|
||||
fun page(requestId: Long, afterCandidateId: Long?, size: Int): TranslationCandidatesResponse {
|
||||
if (size !in 1..100 || (afterCandidateId != null && afterCandidateId <= 0)) {
|
||||
throw TranslationSelectionError.INVALID_REQUEST.exception()
|
||||
}
|
||||
val request = requests.find(requestId) ?: throw TranslationSelectionError.RESOURCE_NOT_FOUND.exception()
|
||||
if (request.status in setOf(
|
||||
TranslationSelectionStatus.PREPARING,
|
||||
TranslationSelectionStatus.REVIEW_REQUIRED,
|
||||
TranslationSelectionStatus.READY_TO_GENERATE
|
||||
)
|
||||
) {
|
||||
throw TranslationSelectionError.REQUEST_NOT_READY.exception()
|
||||
}
|
||||
val page = candidates.page(requestId, afterCandidateId ?: 0, size + 1)
|
||||
val items = page.take(size)
|
||||
return TranslationCandidatesResponse(
|
||||
items.map { it.response() },
|
||||
if (page.size > size) items.last().candidateId else null
|
||||
)
|
||||
}
|
||||
}
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||
|
||||
import kr.co.vividnext.sodalive.content.translation.ContentTranslationRepository
|
||||
import kr.co.vividnext.sodalive.creator.admin.content.series.Series
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationLanguages
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.SelectionContentSource
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.SelectionFieldValues
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionError
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionScope
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.selectionFingerprints
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.SelectionApprovalOrigins
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.SelectionApprovalProvenances
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionApprovalOrigin
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionApprovalProvenance
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionContentResponse
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionContentsResponse
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionCurrentTranslation
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionField
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.port.ContentTranslationOverrideStore
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationSelectionStore
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import java.time.ZoneOffset
|
||||
import javax.persistence.EntityManager
|
||||
import javax.persistence.LockModeType
|
||||
|
||||
@Service
|
||||
class TranslationSelectionContentsService(
|
||||
private val store: TranslationSelectionStore,
|
||||
private val sources: ContentTranslationSourceService,
|
||||
private val writer: ContentTranslationWriteService,
|
||||
private val translations: ContentTranslationRepository,
|
||||
private val overrides: ContentTranslationOverrideStore,
|
||||
private val entityManager: EntityManager
|
||||
) {
|
||||
@Transactional
|
||||
fun contents(requestId: Long, afterContentId: Long?, size: Int): TranslationSelectionContentsResponse {
|
||||
if (size !in 1..100 || (afterContentId != null && afterContentId <= 0)) {
|
||||
throw TranslationSelectionError.INVALID_REQUEST.exception()
|
||||
}
|
||||
val request = store.find(requestId) ?: throw TranslationSelectionError.RESOURCE_NOT_FOUND.exception()
|
||||
val page = request.snapshot.contents.filter { it.contentId > (afterContentId ?: 0) }
|
||||
.sortedBy { it.contentId }.take(size + 1)
|
||||
val linked = when (val scope = request.scope) {
|
||||
is TranslationSelectionScope.Content -> null
|
||||
is TranslationSelectionScope.Series -> if (entityManager.find(Series::class.java, scope.seriesId)?.isActive == true) {
|
||||
sources.linkedContentIds(scope.seriesId).toSet()
|
||||
} else {
|
||||
emptySet()
|
||||
}
|
||||
}
|
||||
val items = page.take(size).map { source -> item(source, linked) }
|
||||
return TranslationSelectionContentsResponse(items, if (page.size > size) items.last().contentId else null)
|
||||
}
|
||||
|
||||
private fun item(source: SelectionContentSource, linked: Set<Long>?): TranslationSelectionContentResponse {
|
||||
val content = writer.lockContent(source.contentId)
|
||||
val current = content?.toContentSourceSnapshot()
|
||||
val active = content?.isActive == true
|
||||
val approved = if (current == null) {
|
||||
emptyList()
|
||||
} else {
|
||||
overrides.findByContentId(source.contentId).filter {
|
||||
it.approval.sourceFingerprint == current.fingerprint(it.approval.field)
|
||||
}
|
||||
}
|
||||
val currentTranslations = TranslationLanguages.getTranslatableLanguageCodes(source.sourceLanguage).map { locale ->
|
||||
val row = translations.findByContentIdAndLocaleForUpdate(source.contentId, locale)
|
||||
if (row != null) entityManager.refresh(row, LockModeType.PESSIMISTIC_WRITE)
|
||||
val fallback = current ?: source.source()
|
||||
val fields = row?.renderedPayload?.let { SelectionFieldValues(it.title, it.detail, it.tags) }
|
||||
?: SelectionFieldValues(fallback.title, fallback.detail, fallback.tags.joinToString(","))
|
||||
val provenance = ContentSourceFieldKey.values().associateWith { field ->
|
||||
approved.singleOrNull { it.locale == locale && it.approval.field == field }?.let {
|
||||
val origin = it.approval.provenance
|
||||
TranslationSelectionApprovalProvenance(
|
||||
origin.requestId,
|
||||
origin.candidateIds,
|
||||
origin.providers,
|
||||
origin.approvedBy,
|
||||
it.approvedAt.toInstant(ZoneOffset.UTC)
|
||||
)
|
||||
}
|
||||
}
|
||||
fun origin(field: ContentSourceFieldKey) = if (provenance[field] == null) {
|
||||
TranslationSelectionApprovalOrigin.UNKNOWN
|
||||
} else {
|
||||
TranslationSelectionApprovalOrigin.ADMIN
|
||||
}
|
||||
TranslationSelectionCurrentTranslation(
|
||||
locale,
|
||||
fields,
|
||||
SelectionApprovalOrigins(
|
||||
origin(ContentSourceFieldKey.TITLE),
|
||||
origin(ContentSourceFieldKey.DETAIL),
|
||||
origin(ContentSourceFieldKey.TAGS)
|
||||
),
|
||||
SelectionApprovalProvenances(
|
||||
provenance[ContentSourceFieldKey.TITLE],
|
||||
provenance[ContentSourceFieldKey.DETAIL],
|
||||
provenance[ContentSourceFieldKey.TAGS]
|
||||
)
|
||||
)
|
||||
}
|
||||
return TranslationSelectionContentResponse(
|
||||
source.contentId,
|
||||
active,
|
||||
content != null && (linked == null || (active && source.contentId in linked)),
|
||||
requireNotNull(source.sourceLanguage),
|
||||
SelectionFieldValues(source.title, source.detail, source.tags.joinToString(",")),
|
||||
source.sourceFingerprints,
|
||||
current?.selectionFingerprints(),
|
||||
source.source().fields.filter { it.sourceText.isBlank() }.map { field ->
|
||||
TranslationSelectionField.values().single { it.key == field.fieldKey }
|
||||
},
|
||||
currentTranslations
|
||||
)
|
||||
}
|
||||
}
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationLanguages
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationProvider
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ApprovalProvider
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TitleSegmentKind
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidate
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationGenerationUnit
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionError
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionRequest
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationUnitKind
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationCandidateStore
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationSelectionStore
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneOffset
|
||||
import java.time.temporal.ChronoUnit
|
||||
|
||||
@Service
|
||||
class TranslationSelectionGenerationService(
|
||||
private val requests: TranslationSelectionStore,
|
||||
private val candidates: TranslationCandidateStore,
|
||||
private val providers: List<TranslationProvider>,
|
||||
private val nowProvider: () -> LocalDateTime = { LocalDateTime.now(ZoneOffset.UTC) }
|
||||
) {
|
||||
@Transactional
|
||||
fun generate(requestId: Long, expectedRevision: Long): TranslationSelectionRequest {
|
||||
val request = requests.lock(requestId) ?: throw TranslationSelectionError.RESOURCE_NOT_FOUND.exception()
|
||||
if (request.acceptedGenerateRevision == expectedRevision) return request
|
||||
if (request.status.isTerminal()) throw TranslationSelectionError.REQUEST_CLOSED.exception()
|
||||
if (!request.snapshot.reviewed) throw TranslationSelectionError.REVIEW_REQUIRED.exception()
|
||||
if (request.revision != expectedRevision) throw TranslationSelectionError.REVISION_CONFLICT.exception()
|
||||
if (request.status != TranslationSelectionStatus.READY_TO_GENERATE) {
|
||||
throw TranslationSelectionError.REQUEST_NOT_READY.exception()
|
||||
}
|
||||
val now = nowProvider().truncatedTo(ChronoUnit.SECONDS)
|
||||
val units = units(request)
|
||||
val manifest = providers.map { ApprovalProvider(it.providerId.code, it.providerVersion) }.sortedBy { it.id }
|
||||
val inputs = mutableMapOf<ProviderInput, Long>()
|
||||
units.forEach { unit ->
|
||||
manifest.forEach { provider ->
|
||||
TranslationLanguages.getTranslatableLanguageCodes(unit.sourceLanguage).forEach { language ->
|
||||
val input = ProviderInput(provider, language, unit.sourceLanguage, unit.kind, unit.field, unit.inputValues)
|
||||
val candidate = candidates.save(
|
||||
TranslationCandidate(
|
||||
requestId = requestId,
|
||||
unit = unit,
|
||||
provider = provider,
|
||||
targetLanguage = language,
|
||||
createdAt = now,
|
||||
linkedCandidateId = inputs[input]
|
||||
)
|
||||
)
|
||||
inputs.putIfAbsent(input, candidate.candidateId)
|
||||
}
|
||||
}
|
||||
}
|
||||
return requests.save(
|
||||
request.copy(
|
||||
status = if (candidates.progress(requestId).total == 0) {
|
||||
TranslationSelectionStatus.READY
|
||||
} else {
|
||||
TranslationSelectionStatus.GENERATING
|
||||
},
|
||||
revision = request.revision + 1,
|
||||
acceptedGenerateRevision = expectedRevision,
|
||||
updatedAt = now,
|
||||
snapshot = request.snapshot.copy(generationUnits = units, providerManifest = manifest)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
private data class ProviderInput(
|
||||
val provider: ApprovalProvider,
|
||||
val target: String,
|
||||
val source: String,
|
||||
val kind: TranslationUnitKind,
|
||||
val field: ContentSourceFieldKey?,
|
||||
val values: List<String>
|
||||
)
|
||||
|
||||
private fun units(request: TranslationSelectionRequest): List<TranslationGenerationUnit> {
|
||||
val grouping = requireNotNull(request.snapshot.grouping)
|
||||
val grouped = grouping.groups.flatMap { it.memberContentIds }.toSet()
|
||||
val fields = request.snapshot.contents.flatMap { content ->
|
||||
content.source().fields.filter {
|
||||
it.sourceText.isNotBlank() && (it.fieldKey != "title" || content.contentId !in grouped)
|
||||
}.map {
|
||||
TranslationGenerationUnit(
|
||||
"field:${content.contentId}:${it.fieldKey}",
|
||||
TranslationUnitKind.FIELD,
|
||||
requireNotNull(content.sourceLanguage),
|
||||
listOf(it.sourceText),
|
||||
content.contentId,
|
||||
ContentSourceFieldKey.values().single { field -> field.key == it.fieldKey }
|
||||
)
|
||||
}
|
||||
}
|
||||
val common = grouping.groups.mapIndexed { index, group ->
|
||||
TranslationGenerationUnit(
|
||||
"common:$index",
|
||||
TranslationUnitKind.GROUP_COMMON,
|
||||
group.sourceLanguage,
|
||||
group.sharedParts.map { it.sourceText },
|
||||
groupId = group.groupId
|
||||
)
|
||||
}
|
||||
val residuals = grouping.templates.mapNotNull { template ->
|
||||
val values = template.segments.filter { it.kind == TitleSegmentKind.RESIDUAL }.map { it.sourceText }
|
||||
if (values.isEmpty()) {
|
||||
null
|
||||
} else {
|
||||
TranslationGenerationUnit(
|
||||
"residual:${template.contentId}",
|
||||
TranslationUnitKind.TITLE_RESIDUAL,
|
||||
requireNotNull(request.snapshot.contents.single { it.contentId == template.contentId }.sourceLanguage),
|
||||
values,
|
||||
template.contentId,
|
||||
groupId = template.groupId
|
||||
)
|
||||
}
|
||||
}
|
||||
return fields + common + residuals
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TitleGroupingReview
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionError
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionRequest
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.groupingInputs
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.selectionSnapshot
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.ReviewTranslationSelectionGroupingRequest
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionGroupingResponse
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationSelectionStore
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneOffset
|
||||
import java.time.temporal.ChronoUnit
|
||||
|
||||
@Service
|
||||
class TranslationSelectionGroupingService(private val store: TranslationSelectionStore) {
|
||||
@Transactional(readOnly = true)
|
||||
fun get(requestId: Long): TranslationSelectionGroupingResponse =
|
||||
response(store.find(requestId) ?: throw TranslationSelectionError.RESOURCE_NOT_FOUND.exception())
|
||||
|
||||
@Transactional
|
||||
fun review(requestId: Long, body: ReviewTranslationSelectionGroupingRequest): TranslationSelectionGroupingResponse {
|
||||
val request = store.lock(requestId) ?: throw TranslationSelectionError.RESOURCE_NOT_FOUND.exception()
|
||||
if (request.status.isTerminal()) throw TranslationSelectionError.REQUEST_CLOSED.exception()
|
||||
if (request.status !in setOf(TranslationSelectionStatus.REVIEW_REQUIRED, TranslationSelectionStatus.READY_TO_GENERATE)) {
|
||||
throw TranslationSelectionError.REQUEST_NOT_READY.exception()
|
||||
}
|
||||
if (body.expectedRevision != request.revision) throw TranslationSelectionError.REVISION_CONFLICT.exception()
|
||||
val grouping = try {
|
||||
TitleGroupingReview(body.groups, body.individualContentIds).validate(request.snapshot.contents.groupingInputs())
|
||||
} catch (_: IllegalArgumentException) {
|
||||
throw TranslationSelectionError.INVALID_GROUPING.exception()
|
||||
}
|
||||
return response(
|
||||
store.save(
|
||||
request.copy(
|
||||
status = TranslationSelectionStatus.READY_TO_GENERATE,
|
||||
revision = request.revision + 1,
|
||||
snapshot = request.snapshot.copy(grouping = grouping.selectionSnapshot(), reviewed = true),
|
||||
updatedAt = LocalDateTime.now(ZoneOffset.UTC).truncatedTo(ChronoUnit.SECONDS)
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
private fun response(request: TranslationSelectionRequest): TranslationSelectionGroupingResponse {
|
||||
val grouping = request.snapshot.grouping ?: throw TranslationSelectionError.REQUEST_NOT_READY.exception()
|
||||
return TranslationSelectionGroupingResponse(
|
||||
request.requestId,
|
||||
request.revision,
|
||||
request.snapshot.reviewed,
|
||||
grouping.groups,
|
||||
grouping.individualContentIds,
|
||||
grouping.templates
|
||||
)
|
||||
}
|
||||
}
|
||||
+179
@@ -0,0 +1,179 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||
|
||||
import kr.co.vividnext.sodalive.content.AudioContent
|
||||
import kr.co.vividnext.sodalive.content.LanguageDetectionCacheService
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationLanguages
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationProvider
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ApprovalProvider
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.SelectionContentSource
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TitleGroupingAnalyzer
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationPreparationClaim
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionError
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionRequest
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionScope
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.groupingInputs
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.selectionFingerprints
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.selectionSnapshot
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationSelectionStore
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.PlatformTransactionManager
|
||||
import org.springframework.transaction.TransactionDefinition
|
||||
import org.springframework.transaction.annotation.Propagation
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import org.springframework.transaction.support.TransactionTemplate
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneOffset
|
||||
import java.time.temporal.ChronoUnit
|
||||
|
||||
@Service
|
||||
class TranslationSelectionPreparationService(
|
||||
private val store: TranslationSelectionStore,
|
||||
private val sources: ContentTranslationSourceService,
|
||||
private val detector: LanguageDetectionCacheService,
|
||||
private val providers: List<TranslationProvider>,
|
||||
transactionManager: PlatformTransactionManager,
|
||||
private val nowProvider: () -> LocalDateTime = { LocalDateTime.now(ZoneOffset.UTC) }
|
||||
) {
|
||||
private val transaction = TransactionTemplate(transactionManager).apply {
|
||||
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
fun prepareNext(): Boolean {
|
||||
val claim = claimNext() ?: return false
|
||||
processPrepare(claim)
|
||||
return true
|
||||
}
|
||||
|
||||
fun claimNext(): TranslationPreparationClaim? = transaction.execute {
|
||||
val now = now()
|
||||
val request = store.nextPreparation(now) ?: return@execute null
|
||||
if (request.preparationAttempt >= 2) {
|
||||
fail(request, TranslationSelectionError.PREPARATION_INTERRUPTED)
|
||||
return@execute null
|
||||
}
|
||||
val current = currentSources(request) ?: return@execute null
|
||||
val baseline = request.snapshot.preparationSources ?: current.map { it.selectionSource() }
|
||||
val claimed = store.save(
|
||||
request.copy(
|
||||
preparationAttempt = request.preparationAttempt + 1,
|
||||
preparationFence = request.preparationFence + 1,
|
||||
preparationLeaseUntil = now.plusSeconds(60),
|
||||
updatedAt = now,
|
||||
snapshot = request.snapshot.copy(preparationSources = baseline)
|
||||
)
|
||||
)
|
||||
TranslationPreparationClaim(claimed.requestId, claimed.preparationFence, baseline)
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
fun processPrepare(claim: TranslationPreparationClaim) {
|
||||
val languages = mutableMapOf<Long, String>()
|
||||
for (source in claim.sources) {
|
||||
if (transaction.execute { checkedCurrent(claim) != null } != true) return
|
||||
val language = source.sourceLanguage?.takeIf { it.isNotBlank() }
|
||||
?: detector.detectWithCache(source.source().detectionQuery) {
|
||||
transaction.execute { checkedCurrent(claim) != null } == true
|
||||
}?.detectedLanguage
|
||||
val accepted = transaction.execute {
|
||||
val (request) = checkedCurrent(claim) ?: return@execute false
|
||||
val error = when {
|
||||
language.isNullOrBlank() -> TranslationSelectionError.LANGUAGE_DETECTION_FAILED
|
||||
!TranslationLanguages.isSupported(language) -> TranslationSelectionError.UNSUPPORTED_SOURCE_LANGUAGE
|
||||
else -> null
|
||||
}
|
||||
if (error != null) fail(request, error)
|
||||
error == null
|
||||
}
|
||||
if (accepted != true) return
|
||||
languages[source.contentId] = requireNotNull(language)
|
||||
}
|
||||
transaction.executeWithoutResult {
|
||||
val (request, current) = checkedCurrent(claim) ?: return@executeWithoutResult
|
||||
current.forEach { content ->
|
||||
if (content.languageCode.isNullOrBlank()) content.languageCode = languages.getValue(requireNotNull(content.id))
|
||||
}
|
||||
val contents = current.map { it.selectionSource() }
|
||||
val proposed = TitleGroupingAnalyzer().analyze(contents.groupingInputs())
|
||||
store.save(
|
||||
request.copy(
|
||||
status = when (request.scope) {
|
||||
is TranslationSelectionScope.Content -> TranslationSelectionStatus.READY_TO_GENERATE
|
||||
is TranslationSelectionScope.Series -> TranslationSelectionStatus.REVIEW_REQUIRED
|
||||
},
|
||||
preparationLeaseUntil = null,
|
||||
updatedAt = now(),
|
||||
snapshot = request.snapshot.copy(
|
||||
contents = contents,
|
||||
grouping = proposed.selectionSnapshot(),
|
||||
reviewed = request.scope is TranslationSelectionScope.Content,
|
||||
providerManifest = providers.map { ApprovalProvider(it.providerId.code, it.providerVersion) }
|
||||
.sortedBy { it.id },
|
||||
targetLanguages = contents.flatMap {
|
||||
TranslationLanguages.getTranslatableLanguageCodes(it.sourceLanguage)
|
||||
}.distinct()
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkedCurrent(claim: TranslationPreparationClaim): Pair<TranslationSelectionRequest, List<AudioContent>>? {
|
||||
val request = store.lock(claim.requestId) ?: return null
|
||||
val now = now()
|
||||
if (request.status != TranslationSelectionStatus.PREPARING || request.preparationFence != claim.fence ||
|
||||
request.preparationLeaseUntil?.isAfter(now) != true
|
||||
) {
|
||||
return null
|
||||
}
|
||||
val current = currentSources(request) ?: return null
|
||||
val renewed = store.save(request.copy(preparationLeaseUntil = now.plusSeconds(60), updatedAt = now))
|
||||
return renewed to current
|
||||
}
|
||||
|
||||
private fun currentSources(request: TranslationSelectionRequest): List<AudioContent>? {
|
||||
val current = sources.lockScope(request.scope)
|
||||
val error = when {
|
||||
current == null -> when (request.scope) {
|
||||
is TranslationSelectionScope.Content -> TranslationSelectionError.SOURCE_CHANGED
|
||||
is TranslationSelectionScope.Series -> TranslationSelectionError.MEMBERSHIP_CHANGED
|
||||
}
|
||||
current.map { it.id } != request.snapshot.memberContentIds -> TranslationSelectionError.MEMBERSHIP_CHANGED
|
||||
request.snapshot.preparationSources?.let { it != current.map { content -> content.selectionSource() } } == true ->
|
||||
TranslationSelectionError.SOURCE_CHANGED
|
||||
else -> null
|
||||
}
|
||||
if (error != null) {
|
||||
fail(request, error)
|
||||
return null
|
||||
}
|
||||
return current
|
||||
}
|
||||
|
||||
private fun fail(request: TranslationSelectionRequest, error: TranslationSelectionError) {
|
||||
store.save(
|
||||
request.copy(
|
||||
status = TranslationSelectionStatus.FAILED,
|
||||
errorCode = error,
|
||||
preparationLeaseUntil = null,
|
||||
preparationFence = request.preparationFence + 1,
|
||||
updatedAt = now()
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
private fun AudioContent.selectionSource(): SelectionContentSource {
|
||||
val source = toContentSourceSnapshot()
|
||||
return SelectionContentSource(
|
||||
requireNotNull(id),
|
||||
source.sourceLanguage,
|
||||
source.title,
|
||||
source.detail,
|
||||
source.tags,
|
||||
source.selectionFingerprints()
|
||||
)
|
||||
}
|
||||
|
||||
private fun now(): LocalDateTime = nowProvider().truncatedTo(ChronoUnit.SECONDS)
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.SelectionRequestSnapshot
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionError
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionRequest
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionScope
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationCandidateStore
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationSelectionStore
|
||||
import org.springframework.dao.DataIntegrityViolationException
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.PlatformTransactionManager
|
||||
import org.springframework.transaction.TransactionDefinition
|
||||
import org.springframework.transaction.annotation.Propagation
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import org.springframework.transaction.support.TransactionTemplate
|
||||
import java.security.MessageDigest
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneOffset
|
||||
import java.time.temporal.ChronoUnit
|
||||
import java.util.UUID
|
||||
|
||||
@Service
|
||||
class TranslationSelectionRequestService(
|
||||
private val store: TranslationSelectionStore,
|
||||
private val sources: ContentTranslationSourceService,
|
||||
transactionManager: PlatformTransactionManager,
|
||||
private val candidates: TranslationCandidateStore,
|
||||
private val nowProvider: () -> LocalDateTime = { LocalDateTime.now(ZoneOffset.UTC) }
|
||||
) {
|
||||
private val transaction = TransactionTemplate(transactionManager).apply {
|
||||
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
fun create(adminId: Long, clientRequestId: UUID, scope: TranslationSelectionScope): TranslationSelectionRequest {
|
||||
if (adminId <= 0 || scope.resourceId() <= 0) throw TranslationSelectionError.INVALID_REQUEST.exception()
|
||||
val hash = MessageDigest.getInstance("SHA-256").digest(scope.canonicalBody().toByteArray(Charsets.UTF_8))
|
||||
.joinToString("") { "%02x".format(it) }
|
||||
fun replay(): TranslationSelectionRequest? = transaction.execute { store.findCreation(adminId, clientRequestId) }?.also {
|
||||
if (it.bodyHash != hash) throw TranslationSelectionError.IDEMPOTENCY_CONFLICT.exception()
|
||||
}
|
||||
replay()?.let { return it }
|
||||
try {
|
||||
return requireNotNull(
|
||||
transaction.execute {
|
||||
val members = sources.lockScope(scope) ?: throw TranslationSelectionError.RESOURCE_NOT_FOUND.exception()
|
||||
val now = nowProvider().truncatedTo(ChronoUnit.SECONDS)
|
||||
store.save(
|
||||
TranslationSelectionRequest(
|
||||
createdBy = adminId,
|
||||
clientRequestId = clientRequestId,
|
||||
bodyHash = hash,
|
||||
scope = scope,
|
||||
snapshot = SelectionRequestSnapshot(
|
||||
memberContentIds = members.map { requireNotNull(it.id) }.sorted()
|
||||
),
|
||||
status = if (members.isEmpty()) {
|
||||
TranslationSelectionStatus.FAILED
|
||||
} else {
|
||||
TranslationSelectionStatus.PREPARING
|
||||
},
|
||||
errorCode = if (members.isEmpty()) TranslationSelectionError.EMPTY_SCOPE else null,
|
||||
createdAt = now
|
||||
)
|
||||
)
|
||||
}
|
||||
)
|
||||
} catch (collision: DataIntegrityViolationException) {
|
||||
// The failed insert transaction is already rolled back before reading the winning unique row.
|
||||
return replay() ?: throw collision
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
fun get(requestId: Long): TranslationSelectionRequest =
|
||||
store.find(requestId) ?: throw TranslationSelectionError.RESOURCE_NOT_FOUND.exception()
|
||||
|
||||
@Transactional
|
||||
fun cancel(requestId: Long): TranslationSelectionRequest {
|
||||
val request = store.lock(requestId) ?: throw TranslationSelectionError.RESOURCE_NOT_FOUND.exception()
|
||||
if (request.status == TranslationSelectionStatus.CANCELLED) return request
|
||||
if (request.status.isTerminal()) throw TranslationSelectionError.REQUEST_CLOSED.exception()
|
||||
val now = nowProvider().truncatedTo(ChronoUnit.SECONDS)
|
||||
candidates.discard(requestId, now)
|
||||
return store.save(
|
||||
request.copy(
|
||||
status = TranslationSelectionStatus.CANCELLED,
|
||||
preparationLeaseUntil = null,
|
||||
preparationFence = request.preparationFence + 1,
|
||||
revision = request.revision + 1,
|
||||
updatedAt = now
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
+169
@@ -0,0 +1,169 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||
|
||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageProviderId
|
||||
import kr.co.vividnext.sodalive.i18n.translation.SourceTextNormalizer
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslateRequest
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationCallControl
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationCallStopReason
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationProvider
|
||||
import kr.co.vividnext.sodalive.i18n.translation.translationCallStop
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidate
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateClaim
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateFailure
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateStatus
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionRequest
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationCandidateStore
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationSelectionStore
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.PlatformTransactionManager
|
||||
import org.springframework.transaction.TransactionDefinition
|
||||
import org.springframework.transaction.annotation.Propagation
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import org.springframework.transaction.support.TransactionTemplate
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneOffset
|
||||
import java.time.temporal.ChronoUnit
|
||||
|
||||
@Service
|
||||
class TranslationSelectionWorkerService(
|
||||
private val requests: TranslationSelectionStore,
|
||||
private val candidates: TranslationCandidateStore,
|
||||
private val providers: List<TranslationProvider>,
|
||||
transactionManager: PlatformTransactionManager,
|
||||
private val nowProvider: () -> LocalDateTime = { LocalDateTime.now(ZoneOffset.UTC) }
|
||||
) {
|
||||
private val transaction = TransactionTemplate(transactionManager).apply {
|
||||
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
||||
isolationLevel = TransactionDefinition.ISOLATION_READ_COMMITTED
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
fun runNext(): Boolean {
|
||||
val claim = claimNext() ?: return false
|
||||
process(claim)
|
||||
return true
|
||||
}
|
||||
|
||||
fun claimNext(): TranslationCandidateClaim? = transaction.execute {
|
||||
val now = now()
|
||||
val requestId = candidates.nextRequest(now) ?: return@execute null
|
||||
val request = requests.lock(requestId) ?: return@execute null
|
||||
if (request.status != TranslationSelectionStatus.GENERATING) return@execute null
|
||||
val candidate = candidates.lockNext(requestId, now) ?: return@execute null
|
||||
if (candidate.attempt >= 2) {
|
||||
val failed = candidates.save(
|
||||
candidate.copy(
|
||||
status = TranslationCandidateStatus.FAILED,
|
||||
failureCode = TranslationCandidateFailure.EXECUTION_INTERRUPTED,
|
||||
fence = candidate.fence + 1,
|
||||
leaseUntil = null,
|
||||
updatedAt = now,
|
||||
terminalAt = now
|
||||
)
|
||||
)
|
||||
candidates.completeLinked(failed)
|
||||
finishIfReady(request)
|
||||
return@execute null
|
||||
}
|
||||
val claimed = candidates.save(
|
||||
candidate.copy(
|
||||
status = TranslationCandidateStatus.RUNNING,
|
||||
attempt = candidate.attempt + 1,
|
||||
fence = candidate.fence + 1,
|
||||
leaseUntil = now.plusSeconds(60),
|
||||
updatedAt = now
|
||||
)
|
||||
)
|
||||
TranslationCandidateClaim(claimed, request.revision)
|
||||
}
|
||||
|
||||
fun authorize(claim: TranslationCandidateClaim): Boolean = transaction.execute {
|
||||
val (_, candidate) = current(claim) ?: return@execute false
|
||||
val now = now()
|
||||
candidates.save(candidate.copy(leaseUntil = now.plusSeconds(60), updatedAt = now))
|
||||
true
|
||||
} == true
|
||||
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
fun process(claim: TranslationCandidateClaim) {
|
||||
val candidate = claim.candidate
|
||||
val provider = providers.singleOrNull {
|
||||
it.providerId.code == candidate.provider.id && it.providerVersion == candidate.provider.version
|
||||
}
|
||||
if (provider == null) {
|
||||
publish(claim, null, TranslationCandidateFailure.PROVIDER_CHANGED)
|
||||
return
|
||||
}
|
||||
var values: List<String>? = null
|
||||
val failure = try {
|
||||
val control = TranslationCallControl({ authorize(claim) })
|
||||
control.beforeCall()
|
||||
values = provider.translate(
|
||||
TranslateRequest(
|
||||
candidate.unit.inputValues,
|
||||
candidate.unit.sourceLanguage,
|
||||
candidate.targetLanguage,
|
||||
candidate.unit.field == ContentSourceFieldKey.TAGS,
|
||||
control
|
||||
)
|
||||
).translatedText
|
||||
control.remainingTimeout()
|
||||
when {
|
||||
values.size != candidate.unit.inputValues.size || values.any { it.isBlank() } ->
|
||||
TranslationCandidateFailure.INVALID_PROVIDER_RESULT
|
||||
candidate.unit.field == ContentSourceFieldKey.TAGS && provider.providerId == LanguageProviderId.AWS &&
|
||||
candidate.unit.inputValues.zip(values).any { (source, translated) ->
|
||||
SourceTextNormalizer.hasSameCommaSeparatedText(source, translated)
|
||||
} -> TranslationCandidateFailure.INVALID_PROVIDER_RESULT
|
||||
else -> null
|
||||
}
|
||||
} catch (error: Exception) {
|
||||
if (error.translationCallStop()?.reason == TranslationCallStopReason.OWNERSHIP_LOST) return
|
||||
// Raw provider exceptions never cross the candidate persistence/API boundary.
|
||||
TranslationCandidateFailure.PROVIDER_FAILED
|
||||
}
|
||||
publish(claim, if (failure == null) values else null, failure)
|
||||
}
|
||||
|
||||
fun publish(claim: TranslationCandidateClaim, values: List<String>?, failure: TranslationCandidateFailure?): Boolean =
|
||||
transaction.execute {
|
||||
val (request, candidate) = current(claim) ?: return@execute false
|
||||
val now = now()
|
||||
val completed = candidates.save(
|
||||
candidate.copy(
|
||||
status = if (failure == null) TranslationCandidateStatus.SUCCEEDED else TranslationCandidateStatus.FAILED,
|
||||
translatedValues = values,
|
||||
failureCode = failure,
|
||||
leaseUntil = null,
|
||||
updatedAt = now,
|
||||
terminalAt = now
|
||||
)
|
||||
)
|
||||
candidates.completeLinked(completed)
|
||||
finishIfReady(request)
|
||||
true
|
||||
} == true
|
||||
|
||||
private fun current(claim: TranslationCandidateClaim): Pair<TranslationSelectionRequest, TranslationCandidate>? {
|
||||
val request = requests.lock(claim.candidate.requestId) ?: return null
|
||||
if (request.status != TranslationSelectionStatus.GENERATING || request.revision != claim.generationRevision) return null
|
||||
val candidate = candidates.lock(claim.candidate.candidateId) ?: return null
|
||||
if (candidate.status != TranslationCandidateStatus.RUNNING || candidate.fence != claim.candidate.fence ||
|
||||
candidate.leaseUntil?.isAfter(now()) != true
|
||||
) {
|
||||
return null
|
||||
}
|
||||
return request to candidate
|
||||
}
|
||||
|
||||
private fun finishIfReady(request: TranslationSelectionRequest) {
|
||||
val progress = candidates.progress(request.requestId)
|
||||
if (progress.total == progress.terminal) {
|
||||
requests.save(request.copy(status = TranslationSelectionStatus.READY, updatedAt = now()))
|
||||
}
|
||||
}
|
||||
|
||||
private fun now(): LocalDateTime = nowProvider().truncatedTo(ChronoUnit.SECONDS)
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.domain
|
||||
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationSourceField
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.DataOutputStream
|
||||
import java.security.MessageDigest
|
||||
|
||||
enum class ContentSourceFieldKey(val key: String) {
|
||||
TITLE("title"), DETAIL("detail"), TAGS("tags")
|
||||
}
|
||||
|
||||
class ContentSourceSnapshot(
|
||||
val sourceLanguage: String?,
|
||||
val title: String,
|
||||
val detail: String,
|
||||
tags: List<String>
|
||||
) {
|
||||
val tags: List<String> = java.util.List.copyOf(tags)
|
||||
val fields: List<TranslationSourceField> = java.util.List.of(
|
||||
TranslationSourceField(ContentSourceFieldKey.TITLE.key, title),
|
||||
TranslationSourceField(ContentSourceFieldKey.DETAIL.key, detail),
|
||||
TranslationSourceField(ContentSourceFieldKey.TAGS.key, this.tags.joinToString(","))
|
||||
)
|
||||
val detectionQuery: String = listOf(title.trim(), detail.trim(), this.tags.joinToString(" ").trim())
|
||||
.filter { it.isNotBlank() }.joinToString(" ")
|
||||
|
||||
fun fingerprint(field: ContentSourceFieldKey): String {
|
||||
val values = when (field) {
|
||||
ContentSourceFieldKey.TITLE -> listOf(title)
|
||||
ContentSourceFieldKey.DETAIL -> listOf(detail)
|
||||
ContentSourceFieldKey.TAGS -> tags
|
||||
}
|
||||
val bytes = ByteArrayOutputStream()
|
||||
DataOutputStream(bytes).use { output ->
|
||||
output.writeFrame("content-source-v1")
|
||||
output.writeFrame(sourceLanguage)
|
||||
output.writeFrame(field.key)
|
||||
output.writeInt(values.size)
|
||||
values.forEach { output.writeFrame(it) }
|
||||
}
|
||||
val hash = MessageDigest.getInstance("SHA-256").digest(bytes.toByteArray())
|
||||
.joinToString("") { "%02x".format(it) }
|
||||
return "v1:$hash"
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean = other is ContentSourceSnapshot &&
|
||||
sourceLanguage == other.sourceLanguage && title == other.title && detail == other.detail && tags == other.tags
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = sourceLanguage?.hashCode() ?: 0
|
||||
result = 31 * result + title.hashCode()
|
||||
result = 31 * result + detail.hashCode()
|
||||
return 31 * result + tags.hashCode()
|
||||
}
|
||||
|
||||
private fun DataOutputStream.writeFrame(value: String?) {
|
||||
if (value == null) {
|
||||
writeInt(-1)
|
||||
} else {
|
||||
val bytes = value.toByteArray(Charsets.UTF_8)
|
||||
writeInt(bytes.size)
|
||||
write(bytes)
|
||||
}
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.domain
|
||||
|
||||
import java.time.LocalDateTime
|
||||
|
||||
data class ApprovalProvider(val id: String, val version: String)
|
||||
|
||||
data class ApprovalProvenance(
|
||||
val requestId: Long,
|
||||
val candidateIds: List<Long>,
|
||||
val providers: List<ApprovalProvider>,
|
||||
val approvedBy: Long
|
||||
)
|
||||
|
||||
data class ContentFieldApproval(
|
||||
val field: ContentSourceFieldKey,
|
||||
val sourceFingerprint: String,
|
||||
val translatedText: String,
|
||||
val provenance: ApprovalProvenance
|
||||
)
|
||||
|
||||
data class ContentTranslationOverride(
|
||||
val contentId: Long,
|
||||
val locale: String,
|
||||
val approval: ContentFieldApproval,
|
||||
val approvedAt: LocalDateTime
|
||||
)
|
||||
+152
@@ -0,0 +1,152 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.domain
|
||||
|
||||
import java.util.regex.Pattern
|
||||
|
||||
class TitleGroupingAnalyzer {
|
||||
fun analyze(inputs: List<TitleGroupingInput>): TitleGrouping {
|
||||
require(inputs.map { it.contentId }.distinct().size == inputs.size) { "Duplicate content ID" }
|
||||
val candidates = inputs.sortedBy { it.contentId }.groupBy { it.sourceLanguage }.values.flatMap { members ->
|
||||
val texts = members.associate { it.contentId to GraphemeTitle(it.title) }
|
||||
val repeated = mutableSetOf<String>()
|
||||
members.forEachIndexed { index, left ->
|
||||
members.drop(index + 1).forEach { right ->
|
||||
scan(texts.getValue(left.contentId), texts.getValue(right.contentId), repeated)
|
||||
}
|
||||
}
|
||||
repeated.mapNotNull { text ->
|
||||
val occurrences = members.mapNotNull { member ->
|
||||
texts.getValue(member.contentId).uniqueOccurrence(member.contentId, text)
|
||||
}
|
||||
if (occurrences.size < 2) null else Candidate(text, occurrences)
|
||||
}
|
||||
}.sortedWith { left, right ->
|
||||
compareValues(right.savings, left.savings).takeIf { it != 0 }
|
||||
?: compareValues(right.length, left.length).takeIf { it != 0 }
|
||||
?: compareCodePoints(left.text, right.text).takeIf { it != 0 }
|
||||
?: compareMemberIds(left.occurrences, right.occurrences)
|
||||
}
|
||||
val assigned = mutableSetOf<Long>()
|
||||
val groups = mutableListOf<ReviewedTitleGroup>()
|
||||
var partNumber = 0
|
||||
candidates.forEach { seed ->
|
||||
val memberIds = seed.occurrences.map { it.contentId }.filter { it !in assigned }
|
||||
if (memberIds.size >= 2) {
|
||||
val parts = mutableListOf<ReviewedSharedPart>()
|
||||
candidates.forEach { candidate ->
|
||||
val occurrences = candidate.occurrences.filter { it.contentId in memberIds }
|
||||
if (occurrences.size == memberIds.size && occurrences.all { occurrence ->
|
||||
parts.flatMap { it.occurrences }.none { existing -> existing.overlaps(occurrence) }
|
||||
}
|
||||
) {
|
||||
parts.add(ReviewedSharedPart("p-${++partNumber}", occurrences))
|
||||
}
|
||||
}
|
||||
groups.add(ReviewedTitleGroup("g-${groups.size + 1}", memberIds, parts))
|
||||
assigned.addAll(memberIds)
|
||||
}
|
||||
}
|
||||
return TitleGroupingReview(groups, inputs.map { it.contentId }.filter { it !in assigned }).validate(inputs)
|
||||
}
|
||||
|
||||
// Scan matching diagonals without retaining a quadratic longest-common-substring matrix.
|
||||
private fun scan(left: GraphemeTitle, right: GraphemeTitle, repeated: MutableSet<String>) {
|
||||
for (diagonal in 1 - right.graphemes.size until left.graphemes.size) {
|
||||
var i = maxOf(0, diagonal)
|
||||
var j = maxOf(0, -diagonal)
|
||||
while (i < left.graphemes.size && j < right.graphemes.size) {
|
||||
if (left.graphemes[i] != right.graphemes[j]) {
|
||||
i++
|
||||
j++
|
||||
continue
|
||||
}
|
||||
val initialI = i
|
||||
val initialJ = j
|
||||
while (i < left.graphemes.size && j < right.graphemes.size && left.graphemes[i] == right.graphemes[j]) {
|
||||
i++
|
||||
j++
|
||||
}
|
||||
var start = initialI
|
||||
var end = i
|
||||
while (start < end &&
|
||||
(!left.lexicalBoundary(start) || !right.lexicalBoundary(initialJ + start - initialI))
|
||||
) start++
|
||||
while (end > start && (!left.lexicalBoundary(end) || !right.lexicalBoundary(initialJ + end - initialI))) end--
|
||||
val text = left.substring(start, end)
|
||||
if (text.codePoints().filter { Character.isLetter(it) }.count() >= 2) repeated.add(text)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private data class Candidate(val text: String, val occurrences: List<TitleOccurrence>) {
|
||||
val length: Int = text.codePointCount(0, text.length)
|
||||
val savings: Long = length.toLong() * (occurrences.size - 1)
|
||||
}
|
||||
|
||||
private fun compareCodePoints(left: String, right: String): Int {
|
||||
val a = left.codePoints().iterator()
|
||||
val b = right.codePoints().iterator()
|
||||
while (a.hasNext() && b.hasNext()) {
|
||||
val comparison = a.nextInt().compareTo(b.nextInt())
|
||||
if (comparison != 0) return comparison
|
||||
}
|
||||
return a.hasNext().compareTo(b.hasNext())
|
||||
}
|
||||
|
||||
private fun compareMemberIds(left: List<TitleOccurrence>, right: List<TitleOccurrence>): Int {
|
||||
left.zip(right).forEach { (a, b) ->
|
||||
val comparison = a.contentId.compareTo(b.contentId)
|
||||
if (comparison != 0) return comparison
|
||||
}
|
||||
return left.size.compareTo(right.size)
|
||||
}
|
||||
}
|
||||
|
||||
internal class GraphemeTitle(private val raw: String) {
|
||||
val graphemes: List<String> = GRAPHEME.matcher(raw).results().map { it.group() }.toList()
|
||||
private val charOffsets = IntArray(graphemes.size + 1)
|
||||
val codePointOffsets = IntArray(graphemes.size + 1)
|
||||
|
||||
init {
|
||||
graphemes.forEachIndexed { index, text ->
|
||||
charOffsets[index + 1] = charOffsets[index] + text.length
|
||||
codePointOffsets[index + 1] = codePointOffsets[index] + text.codePointCount(0, text.length)
|
||||
}
|
||||
}
|
||||
|
||||
fun substring(start: Int, end: Int): String = raw.substring(charOffsets[start], charOffsets[end])
|
||||
|
||||
fun slice(occurrence: TitleOccurrence): String {
|
||||
val start = codePointOffsets.binarySearch(occurrence.startCodePoint)
|
||||
val end = codePointOffsets.binarySearch(occurrence.endCodePoint)
|
||||
require(start >= 0 && end > start) { "Range must be nonempty and follow grapheme boundaries" }
|
||||
return substring(start, end)
|
||||
}
|
||||
|
||||
fun lexicalBoundary(index: Int): Boolean = index == 0 || index == graphemes.size ||
|
||||
!isWord(graphemes[index - 1]) || !isWord(graphemes[index])
|
||||
|
||||
fun uniqueOccurrence(contentId: Long, text: String): TitleOccurrence? {
|
||||
var from = 0
|
||||
var found: TitleOccurrence? = null
|
||||
while (from <= raw.length - text.length) {
|
||||
val position = raw.indexOf(text, from)
|
||||
if (position < 0) break
|
||||
val start = charOffsets.binarySearch(position)
|
||||
val end = charOffsets.binarySearch(position + text.length)
|
||||
if (start >= 0 && end >= 0 && lexicalBoundary(start) && lexicalBoundary(end)) {
|
||||
if (found != null) return null
|
||||
found = TitleOccurrence(contentId, codePointOffsets[start], codePointOffsets[end])
|
||||
}
|
||||
from = position + 1
|
||||
}
|
||||
return found
|
||||
}
|
||||
|
||||
private fun isWord(text: String): Boolean = text.codePoints().anyMatch {
|
||||
Character.isLetterOrDigit(it) || Character.getType(it) == Character.CONNECTOR_PUNCTUATION.toInt()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val GRAPHEME = Pattern.compile("\\X")
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.domain
|
||||
|
||||
import java.time.LocalDateTime
|
||||
|
||||
enum class TranslationCandidateStatus { PENDING, RUNNING, SUCCEEDED, FAILED, DISCARDED }
|
||||
enum class TranslationCandidateFailure { PROVIDER_FAILED, PROVIDER_CHANGED, INVALID_PROVIDER_RESULT, EXECUTION_INTERRUPTED }
|
||||
enum class TranslationUnitKind { FIELD, GROUP_COMMON, TITLE_RESIDUAL }
|
||||
|
||||
data class TranslationGenerationUnit(
|
||||
val key: String,
|
||||
val kind: TranslationUnitKind,
|
||||
val sourceLanguage: String,
|
||||
val inputValues: List<String>,
|
||||
val contentId: Long? = null,
|
||||
val field: ContentSourceFieldKey? = null,
|
||||
val groupId: String? = null
|
||||
)
|
||||
|
||||
data class TranslationCandidate(
|
||||
val candidateId: Long = 0,
|
||||
val requestId: Long,
|
||||
val unit: TranslationGenerationUnit,
|
||||
val provider: ApprovalProvider,
|
||||
val targetLanguage: String,
|
||||
val status: TranslationCandidateStatus = TranslationCandidateStatus.PENDING,
|
||||
val translatedValues: List<String>? = null,
|
||||
val failureCode: TranslationCandidateFailure? = null,
|
||||
val attempt: Int = 0,
|
||||
val leaseUntil: LocalDateTime? = null,
|
||||
val fence: Long = 0,
|
||||
val createdAt: LocalDateTime,
|
||||
val updatedAt: LocalDateTime = createdAt,
|
||||
val terminalAt: LocalDateTime? = null,
|
||||
val linkedCandidateId: Long? = null
|
||||
)
|
||||
|
||||
data class TranslationCandidateClaim(val candidate: TranslationCandidate, val generationRevision: Long)
|
||||
data class SelectionCandidateProgress(val total: Int = 0, val terminal: Int = 0, val succeeded: Int = 0, val failed: Int = 0)
|
||||
+152
@@ -0,0 +1,152 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.domain
|
||||
|
||||
data class TitleGroupingInput(val contentId: Long, val sourceLanguage: String, val title: String)
|
||||
data class TitleOccurrence(val contentId: Long, val startCodePoint: Int, val endCodePoint: Int)
|
||||
data class ReviewedSharedPart(val partId: String, val occurrences: List<TitleOccurrence>)
|
||||
data class ReviewedTitleGroup(val groupId: String, val memberContentIds: List<Long>, val sharedParts: List<ReviewedSharedPart>)
|
||||
data class TitleGroupingReview(val groups: List<ReviewedTitleGroup>, val individualContentIds: List<Long>) {
|
||||
fun validate(inputs: List<TitleGroupingInput>): TitleGrouping {
|
||||
val sources = inputs.associateBy { it.contentId }
|
||||
require(sources.size == inputs.size) { "Duplicate content ID" }
|
||||
val assigned = groups.flatMap { it.memberContentIds } + individualContentIds
|
||||
require(assigned.size == sources.size && assigned.toSet() == sources.keys) { "Scope must be covered exactly once" }
|
||||
require(groups.map { it.groupId }.distinct().size == groups.size && groups.all { it.groupId.isNotBlank() }) {
|
||||
"Group IDs must be nonempty and unique"
|
||||
}
|
||||
val parts = groups.flatMap { it.sharedParts }
|
||||
require(parts.map { it.partId }.distinct().size == parts.size && parts.all { it.partId.isNotBlank() }) {
|
||||
"Part IDs must be nonempty and unique"
|
||||
}
|
||||
val titles = inputs.associate { it.contentId to GraphemeTitle(it.title) }
|
||||
val validated = groups.map { group ->
|
||||
val memberIds = group.memberContentIds.sorted()
|
||||
require(memberIds.size >= 2 && group.sharedParts.isNotEmpty()) { "Group needs two members and a shared part" }
|
||||
val languages = memberIds.map { sources.getValue(it).sourceLanguage }.distinct()
|
||||
require(languages.size == 1) { "Group languages must match" }
|
||||
val shared = group.sharedParts.map { part ->
|
||||
require(part.occurrences.map { it.contentId }.sorted() == memberIds) {
|
||||
"Part must cover every member exactly once"
|
||||
}
|
||||
val occurrences = part.occurrences.sortedBy { it.contentId }
|
||||
val texts = occurrences.map { titles.getValue(it.contentId).slice(it) }
|
||||
require(texts.distinct().size == 1) { "Shared raw text must match exactly" }
|
||||
TitleSharedPart(part.partId, texts.first(), java.util.List.copyOf(occurrences))
|
||||
}
|
||||
memberIds.forEach { contentId ->
|
||||
val ranges = shared.flatMap { it.occurrences }.filter { it.contentId == contentId }.sortedBy { it.startCodePoint }
|
||||
require(ranges.zipWithNext().none { (left, right) -> left.overlaps(right) }) { "Shared parts overlap" }
|
||||
}
|
||||
TitleGroup(group.groupId, languages.single(), java.util.List.copyOf(memberIds), java.util.List.copyOf(shared))
|
||||
}
|
||||
val templates = validated.flatMap { group ->
|
||||
group.memberContentIds.map { contentId -> template(sources.getValue(contentId), group, titles.getValue(contentId)) }
|
||||
}.sortedBy { it.contentId }
|
||||
return TitleGrouping(
|
||||
java.util.List.copyOf(validated),
|
||||
java.util.List.copyOf(individualContentIds.sorted()),
|
||||
java.util.List.copyOf(templates)
|
||||
)
|
||||
}
|
||||
|
||||
private fun template(input: TitleGroupingInput, group: TitleGroup, title: GraphemeTitle): TitleTemplate {
|
||||
val segments = mutableListOf<TitleSegment>()
|
||||
var residualIndex = 0
|
||||
var cursor = 0
|
||||
val outerDelimiters = Regex("[\\p{javaWhitespace}\\p{Z}\\p{P}<>]+")
|
||||
fun appendGap(endCodePoint: Int) {
|
||||
val start = title.codePointOffsets.binarySearch(cursor)
|
||||
val end = title.codePointOffsets.binarySearch(endCodePoint)
|
||||
val hasLetters = (start until end).any { index ->
|
||||
title.graphemes[index].codePoints().anyMatch { Character.isLetter(it) }
|
||||
}
|
||||
var residualStart = start
|
||||
var residualEnd = end
|
||||
if (hasLetters) {
|
||||
while (residualStart < residualEnd && outerDelimiters.matches(title.graphemes[residualStart])) residualStart++
|
||||
while (residualEnd > residualStart && outerDelimiters.matches(title.graphemes[residualEnd - 1])) residualEnd--
|
||||
}
|
||||
val boundaries = if (hasLetters) listOf(start, residualStart, residualEnd, end) else listOf(start, end)
|
||||
boundaries.zipWithNext().filter { (from, to) -> from < to }.forEach { (from, to) ->
|
||||
val translatable = hasLetters && from == residualStart && to == residualEnd
|
||||
segments.add(
|
||||
TitleSegment(
|
||||
"s-${segments.size + 1}",
|
||||
if (translatable) TitleSegmentKind.RESIDUAL else TitleSegmentKind.LITERAL,
|
||||
title.codePointOffsets[from],
|
||||
title.codePointOffsets[to],
|
||||
title.substring(from, to),
|
||||
null,
|
||||
if (translatable) residualIndex++ else null
|
||||
)
|
||||
)
|
||||
}
|
||||
cursor = endCodePoint
|
||||
}
|
||||
val shared = group.sharedParts.mapIndexed { index, part ->
|
||||
Triple(index, part, part.occurrences.single { it.contentId == input.contentId })
|
||||
}.sortedBy { it.third.startCodePoint }
|
||||
shared.forEach { (valueIndex, part, occurrence) ->
|
||||
appendGap(occurrence.startCodePoint)
|
||||
segments.add(
|
||||
TitleSegment(
|
||||
"s-${segments.size + 1}",
|
||||
TitleSegmentKind.SHARED,
|
||||
occurrence.startCodePoint,
|
||||
occurrence.endCodePoint,
|
||||
part.sourceText,
|
||||
part.partId,
|
||||
valueIndex
|
||||
)
|
||||
)
|
||||
cursor = occurrence.endCodePoint
|
||||
}
|
||||
appendGap(title.codePointOffsets.last())
|
||||
return TitleTemplate(
|
||||
input.contentId,
|
||||
group.groupId,
|
||||
ContentSourceSnapshot(input.sourceLanguage, input.title, "", emptyList()).fingerprint(ContentSourceFieldKey.TITLE),
|
||||
java.util.List.copyOf(segments)
|
||||
)
|
||||
}
|
||||
}
|
||||
data class TitleSharedPart(val partId: String, val sourceText: String, val occurrences: List<TitleOccurrence>)
|
||||
data class TitleGroup(
|
||||
val groupId: String,
|
||||
val sourceLanguage: String,
|
||||
val memberContentIds: List<Long>,
|
||||
val sharedParts: List<TitleSharedPart>
|
||||
)
|
||||
data class TitleGrouping(val groups: List<TitleGroup>, val individualContentIds: List<Long>, val templates: List<TitleTemplate>)
|
||||
enum class TitleSegmentKind { LITERAL, SHARED, RESIDUAL }
|
||||
data class TitleSegment(
|
||||
val segmentId: String,
|
||||
val kind: TitleSegmentKind,
|
||||
val startCodePoint: Int,
|
||||
val endCodePoint: Int,
|
||||
val sourceText: String,
|
||||
val partId: String?,
|
||||
val valueIndex: Int?
|
||||
)
|
||||
data class TitleTemplate(
|
||||
val contentId: Long,
|
||||
val groupId: String,
|
||||
val sourceFingerprint: String,
|
||||
val segments: List<TitleSegment>
|
||||
) {
|
||||
val residualInputs: List<String> get() = segments.filter { it.kind == TitleSegmentKind.RESIDUAL }.map { it.sourceText }
|
||||
|
||||
fun render(sharedValues: List<String>, residualValues: List<String>): String {
|
||||
require(sharedValues.size == segments.count { it.kind == TitleSegmentKind.SHARED }) { "Shared value count differs" }
|
||||
require(residualValues.size == residualInputs.size) { "Residual value count differs" }
|
||||
return segments.joinToString("") { segment ->
|
||||
when (segment.kind) {
|
||||
TitleSegmentKind.LITERAL -> segment.sourceText
|
||||
TitleSegmentKind.SHARED -> sharedValues[requireNotNull(segment.valueIndex)]
|
||||
TitleSegmentKind.RESIDUAL -> residualValues[requireNotNull(segment.valueIndex)]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal fun TitleOccurrence.overlaps(other: TitleOccurrence): Boolean = contentId == other.contentId &&
|
||||
startCodePoint < other.endCodePoint && other.startCodePoint < endCodePoint
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.domain
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonSubTypes
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo
|
||||
import kr.co.vividnext.sodalive.common.SodaException
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionApplyReceipt
|
||||
import java.time.LocalDateTime
|
||||
import java.util.UUID
|
||||
|
||||
enum class TranslationSelectionStatus {
|
||||
PREPARING, REVIEW_REQUIRED, READY_TO_GENERATE, GENERATING, READY, APPLIED, CANCELLED, FAILED;
|
||||
|
||||
fun isTerminal(): Boolean = this in setOf(APPLIED, CANCELLED, FAILED)
|
||||
}
|
||||
|
||||
enum class TranslationSelectionError {
|
||||
INVALID_REQUEST, RESOURCE_NOT_FOUND, EMPTY_SCOPE, UNSUPPORTED_SOURCE_LANGUAGE, LANGUAGE_DETECTION_FAILED,
|
||||
PREPARATION_INTERRUPTED, INVALID_GROUPING, REVISION_CONFLICT, REQUEST_NOT_READY, REQUEST_CLOSED, REVIEW_REQUIRED,
|
||||
SOURCE_CHANGED, MEMBERSHIP_CHANGED, IDEMPOTENCY_CONFLICT, CANDIDATE_NOT_SELECTABLE, INCOMPLETE_TITLE_GROUP;
|
||||
|
||||
fun exception(): SodaException = SodaException(
|
||||
messageKey = when (this) {
|
||||
RESOURCE_NOT_FOUND -> "translation.selection.not_found"
|
||||
INVALID_GROUPING -> "translation.selection.invalid_grouping"
|
||||
REVISION_CONFLICT -> "translation.selection.revision_conflict"
|
||||
IDEMPOTENCY_CONFLICT -> "translation.selection.idempotency_conflict"
|
||||
REQUEST_CLOSED -> "translation.selection.closed"
|
||||
REQUEST_NOT_READY -> "translation.selection.not_ready"
|
||||
CANDIDATE_NOT_SELECTABLE -> "translation.selection.candidate_not_selectable"
|
||||
INCOMPLETE_TITLE_GROUP -> "translation.selection.incomplete_title_group"
|
||||
SOURCE_CHANGED -> "translation.selection.source_changed"
|
||||
MEMBERSHIP_CHANGED -> "translation.selection.membership_changed"
|
||||
else -> "common.error.invalid_request"
|
||||
},
|
||||
errorProperty = name
|
||||
)
|
||||
}
|
||||
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
|
||||
@JsonSubTypes(
|
||||
JsonSubTypes.Type(value = TranslationSelectionScope.Content::class, name = "CONTENT"),
|
||||
JsonSubTypes.Type(value = TranslationSelectionScope.Series::class, name = "SERIES")
|
||||
)
|
||||
sealed class TranslationSelectionScope {
|
||||
data class Content(val contentId: Long) : TranslationSelectionScope()
|
||||
data class Series(val seriesId: Long) : TranslationSelectionScope()
|
||||
|
||||
fun resourceId(): Long = when (this) {
|
||||
is Content -> contentId
|
||||
is Series -> seriesId
|
||||
}
|
||||
|
||||
fun canonicalBody(): String = when (this) {
|
||||
is Content -> "v1:CONTENT:$contentId"
|
||||
is Series -> "v1:SERIES:$seriesId"
|
||||
}
|
||||
}
|
||||
|
||||
// Only stored source fields are serialized; shared snapshot's derived getters are never persisted.
|
||||
data class SelectionContentSource(
|
||||
val contentId: Long,
|
||||
val sourceLanguage: String?,
|
||||
val title: String,
|
||||
val detail: String,
|
||||
val tags: List<String>,
|
||||
val sourceFingerprints: SelectionFieldValues
|
||||
) {
|
||||
fun source(): ContentSourceSnapshot = ContentSourceSnapshot(sourceLanguage, title, detail, tags)
|
||||
}
|
||||
|
||||
data class SelectionFieldValues(val title: String, val detail: String, val tags: String)
|
||||
|
||||
fun ContentSourceSnapshot.selectionFingerprints() = SelectionFieldValues(
|
||||
fingerprint(ContentSourceFieldKey.TITLE),
|
||||
fingerprint(ContentSourceFieldKey.DETAIL),
|
||||
fingerprint(ContentSourceFieldKey.TAGS)
|
||||
)
|
||||
|
||||
data class SelectionTitleTemplate(
|
||||
val contentId: Long,
|
||||
val groupId: String,
|
||||
val sourceFingerprint: String,
|
||||
val segments: List<TitleSegment>
|
||||
) {
|
||||
fun template(): TitleTemplate = TitleTemplate(contentId, groupId, sourceFingerprint, segments)
|
||||
}
|
||||
|
||||
data class SelectionGroupingSnapshot(
|
||||
val groups: List<TitleGroup>,
|
||||
val individualContentIds: List<Long>,
|
||||
val templates: List<SelectionTitleTemplate>
|
||||
)
|
||||
|
||||
fun TitleGrouping.selectionSnapshot() = SelectionGroupingSnapshot(
|
||||
groups,
|
||||
individualContentIds,
|
||||
templates.map { SelectionTitleTemplate(it.contentId, it.groupId, it.sourceFingerprint, it.segments) }
|
||||
)
|
||||
|
||||
data class SelectionRequestSnapshot(
|
||||
val version: Int = 1,
|
||||
val memberContentIds: List<Long>,
|
||||
val preparationSources: List<SelectionContentSource>? = null,
|
||||
val contents: List<SelectionContentSource> = emptyList(),
|
||||
val grouping: SelectionGroupingSnapshot? = null,
|
||||
val reviewed: Boolean = false,
|
||||
val providerManifest: List<ApprovalProvider> = emptyList(),
|
||||
val targetLanguages: List<String> = emptyList(),
|
||||
val generationUnits: List<TranslationGenerationUnit> = emptyList()
|
||||
)
|
||||
|
||||
data class TranslationSelectionRequest(
|
||||
val requestId: Long = 0,
|
||||
val createdBy: Long,
|
||||
val clientRequestId: UUID,
|
||||
val bodyHash: String,
|
||||
val scope: TranslationSelectionScope,
|
||||
val status: TranslationSelectionStatus = TranslationSelectionStatus.PREPARING,
|
||||
val revision: Long = 1,
|
||||
val snapshot: SelectionRequestSnapshot,
|
||||
val preparationAttempt: Int = 0,
|
||||
val preparationLeaseUntil: LocalDateTime? = null,
|
||||
val preparationFence: Long = 0,
|
||||
val errorCode: TranslationSelectionError? = null,
|
||||
val createdAt: LocalDateTime,
|
||||
val updatedAt: LocalDateTime = createdAt,
|
||||
val acceptedGenerateRevision: Long? = null,
|
||||
val progress: SelectionCandidateProgress = SelectionCandidateProgress(),
|
||||
val applyOperationId: UUID? = null,
|
||||
val applyBodyHash: String? = null,
|
||||
val applyReceipt: TranslationSelectionApplyReceipt? = null
|
||||
)
|
||||
|
||||
data class TranslationPreparationClaim(val requestId: Long, val fence: Long, val sources: List<SelectionContentSource>)
|
||||
|
||||
fun List<SelectionContentSource>.groupingInputs(): List<TitleGroupingInput> = map {
|
||||
TitleGroupingInput(it.contentId, requireNotNull(it.sourceLanguage), it.title)
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.dto
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonSubTypes
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo
|
||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||
import java.security.MessageDigest
|
||||
import java.util.UUID
|
||||
|
||||
data class ApplyTranslationSelectionRequest(
|
||||
val operationId: UUID,
|
||||
val expectedRevision: Long,
|
||||
val selections: List<TranslationSelectionChoice>
|
||||
) {
|
||||
fun canonicalHash(): String {
|
||||
val mapper = jacksonObjectMapper()
|
||||
val choices = selections.map { choice ->
|
||||
when (choice) {
|
||||
is TranslationSelectionChoice.Field -> choice
|
||||
is TranslationSelectionChoice.TitleGroup -> choice.copy(
|
||||
residualChoices = choice.residualChoices.sortedBy { it.contentId }
|
||||
)
|
||||
}
|
||||
}.sortedBy { mapper.writeValueAsString(it) }
|
||||
val canonical = mapper.writeValueAsBytes(copy(selections = choices))
|
||||
return MessageDigest.getInstance("SHA-256").digest(canonical).joinToString("") { "%02x".format(it) }
|
||||
}
|
||||
}
|
||||
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "kind")
|
||||
@JsonSubTypes(
|
||||
JsonSubTypes.Type(value = TranslationSelectionChoice.Field::class, name = "FIELD"),
|
||||
JsonSubTypes.Type(value = TranslationSelectionChoice.TitleGroup::class, name = "TITLE_GROUP")
|
||||
)
|
||||
sealed class TranslationSelectionChoice {
|
||||
data class Field(
|
||||
val contentId: Long,
|
||||
val field: TranslationSelectionField,
|
||||
val targetLanguage: String,
|
||||
val candidateId: Long
|
||||
) : TranslationSelectionChoice()
|
||||
|
||||
data class TitleGroup(
|
||||
val groupId: String,
|
||||
val targetLanguage: String,
|
||||
val commonCandidateId: Long,
|
||||
val residualChoices: List<TranslationSelectionResidualChoice>
|
||||
) : TranslationSelectionChoice()
|
||||
}
|
||||
|
||||
data class TranslationSelectionResidualChoice(val contentId: Long, val candidateId: Long?)
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.dto
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ApprovalProvider
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidate
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateFailure
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateStatus
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationUnitKind
|
||||
|
||||
data class GenerateTranslationSelectionRequest(val expectedRevision: Long)
|
||||
data class TranslationCandidatesResponse(val items: List<TranslationCandidateResponse>, val nextAfterCandidateId: Long?)
|
||||
data class TranslationCandidateResponse(
|
||||
val candidateId: Long,
|
||||
val unit: TranslationCandidateUnitResponse,
|
||||
val provider: ApprovalProvider,
|
||||
val targetLanguage: String,
|
||||
val inputValues: List<String>,
|
||||
val status: TranslationCandidateStatus,
|
||||
val translatedValues: List<String>?,
|
||||
val failureCode: TranslationCandidateFailure?
|
||||
)
|
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
data class TranslationCandidateUnitResponse(
|
||||
val kind: TranslationUnitKind,
|
||||
val contentId: Long?,
|
||||
val field: String?,
|
||||
val groupId: String?
|
||||
)
|
||||
|
||||
fun TranslationCandidate.response() = TranslationCandidateResponse(
|
||||
candidateId,
|
||||
TranslationCandidateUnitResponse(unit.kind, unit.contentId, unit.field?.key, unit.groupId),
|
||||
provider,
|
||||
targetLanguage,
|
||||
unit.inputValues,
|
||||
status,
|
||||
translatedValues,
|
||||
failureCode
|
||||
)
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.dto
|
||||
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ReviewedTitleGroup
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionScope
|
||||
import java.util.UUID
|
||||
|
||||
data class CreateTranslationSelectionRequest(val clientRequestId: UUID, val scope: TranslationSelectionScope)
|
||||
|
||||
data class ReviewTranslationSelectionGroupingRequest(
|
||||
val expectedRevision: Long,
|
||||
val groups: List<ReviewedTitleGroup>,
|
||||
val individualContentIds: List<Long>
|
||||
)
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.dto
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude
|
||||
import com.fasterxml.jackson.annotation.JsonValue
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ApprovalProvider
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.SelectionFieldValues
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.SelectionTitleTemplate
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TitleGroup
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionError
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionRequest
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionScope
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||
import java.time.Instant
|
||||
import java.time.ZoneOffset
|
||||
import java.util.UUID
|
||||
|
||||
data class TranslationSelectionRequestResponse(
|
||||
val requestId: Long,
|
||||
val scope: TranslationSelectionScope,
|
||||
val status: TranslationSelectionStatus,
|
||||
val revision: Long,
|
||||
val createdAt: Instant,
|
||||
val providerManifest: List<ApprovalProvider>,
|
||||
val targetLanguages: List<String>,
|
||||
val contentCount: Int,
|
||||
val progress: TranslationSelectionProgress,
|
||||
val errorCode: TranslationSelectionError?,
|
||||
val applyReceipt: TranslationSelectionApplyReceipt?
|
||||
)
|
||||
|
||||
data class TranslationSelectionProgress(val total: Int = 0, val terminal: Int = 0, val succeeded: Int = 0, val failed: Int = 0)
|
||||
data class TranslationSelectionApplyReceipt(
|
||||
val requestId: Long,
|
||||
val operationId: UUID,
|
||||
val status: TranslationSelectionStatus,
|
||||
val appliedAt: Instant,
|
||||
val appliedFields: List<TranslationSelectionAppliedField>,
|
||||
val discardedCandidateCount: Int
|
||||
)
|
||||
data class TranslationSelectionAppliedField(
|
||||
val contentId: Long,
|
||||
val field: TranslationSelectionField,
|
||||
val targetLanguage: String,
|
||||
val translatedValue: String,
|
||||
val candidateIds: List<Long>,
|
||||
val providers: List<ApprovalProvider>
|
||||
)
|
||||
|
||||
fun TranslationSelectionRequest.response() = TranslationSelectionRequestResponse(
|
||||
requestId, scope, status, revision, createdAt.toInstant(ZoneOffset.UTC), snapshot.providerManifest,
|
||||
snapshot.targetLanguages, snapshot.contents.size,
|
||||
TranslationSelectionProgress(progress.total, progress.terminal, progress.succeeded, progress.failed), errorCode, applyReceipt
|
||||
)
|
||||
|
||||
enum class TranslationSelectionField(@get:JsonValue val key: String) { TITLE("title"), DETAIL("detail"), TAGS("tags") }
|
||||
enum class TranslationSelectionApprovalOrigin { ADMIN, UNKNOWN }
|
||||
enum class TitleIndexUnit { UNICODE_CODE_POINT }
|
||||
|
||||
data class TranslationSelectionGroupingResponse(
|
||||
val requestId: Long,
|
||||
val revision: Long,
|
||||
val reviewed: Boolean,
|
||||
val groups: List<TitleGroup>,
|
||||
val individualContentIds: List<Long>,
|
||||
val templates: List<SelectionTitleTemplate>,
|
||||
val indexUnit: TitleIndexUnit = TitleIndexUnit.UNICODE_CODE_POINT
|
||||
)
|
||||
|
||||
data class TranslationSelectionContentsResponse(
|
||||
val items: List<TranslationSelectionContentResponse>,
|
||||
val nextAfterContentId: Long?
|
||||
)
|
||||
|
||||
data class TranslationSelectionContentResponse(
|
||||
val contentId: Long,
|
||||
val active: Boolean,
|
||||
val eligible: Boolean,
|
||||
val sourceLanguage: String,
|
||||
val sourceFields: SelectionFieldValues,
|
||||
val sourceFingerprints: SelectionFieldValues,
|
||||
val currentSourceFingerprints: SelectionFieldValues?,
|
||||
val emptyFields: List<TranslationSelectionField>,
|
||||
val currentTranslations: List<TranslationSelectionCurrentTranslation>
|
||||
)
|
||||
|
||||
data class TranslationSelectionCurrentTranslation(
|
||||
val targetLanguage: String,
|
||||
val fields: SelectionFieldValues,
|
||||
val approvalOrigins: SelectionApprovalOrigins,
|
||||
val approvalProvenance: SelectionApprovalProvenances
|
||||
)
|
||||
|
||||
data class SelectionApprovalOrigins(
|
||||
val title: TranslationSelectionApprovalOrigin,
|
||||
val detail: TranslationSelectionApprovalOrigin,
|
||||
val tags: TranslationSelectionApprovalOrigin
|
||||
)
|
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
data class SelectionApprovalProvenances(
|
||||
val title: TranslationSelectionApprovalProvenance?,
|
||||
val detail: TranslationSelectionApprovalProvenance?,
|
||||
val tags: TranslationSelectionApprovalProvenance?
|
||||
)
|
||||
|
||||
data class TranslationSelectionApprovalProvenance(
|
||||
val requestId: Long,
|
||||
val candidateIds: List<Long>,
|
||||
val providers: List<ApprovalProvider>,
|
||||
val approvedBy: Long,
|
||||
val approvedAt: Instant
|
||||
)
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.port
|
||||
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentTranslationOverride
|
||||
|
||||
interface ContentTranslationOverrideStore {
|
||||
fun findByContentId(contentId: Long): List<ContentTranslationOverride>
|
||||
fun save(override: ContentTranslationOverride)
|
||||
fun deleteFields(contentId: Long, fields: Set<ContentSourceFieldKey>, locale: String? = null)
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.port
|
||||
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.SelectionCandidateProgress
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidate
|
||||
import java.time.LocalDateTime
|
||||
|
||||
interface TranslationCandidateStore {
|
||||
fun save(candidate: TranslationCandidate): TranslationCandidate
|
||||
fun nextRequest(now: LocalDateTime): Long?
|
||||
fun lockNext(requestId: Long, now: LocalDateTime): TranslationCandidate?
|
||||
fun lock(candidateId: Long): TranslationCandidate?
|
||||
fun find(candidateId: Long): TranslationCandidate?
|
||||
fun page(requestId: Long, afterId: Long, limit: Int): List<TranslationCandidate>
|
||||
fun progress(requestId: Long): SelectionCandidateProgress
|
||||
fun completeLinked(source: TranslationCandidate)
|
||||
fun discard(requestId: Long, now: LocalDateTime)
|
||||
fun discardUnselected(requestId: Long, selectedIds: Set<Long>, now: LocalDateTime): Int
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package kr.co.vividnext.sodalive.v2.contenttranslation.port
|
||||
|
||||
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionRequest
|
||||
import java.time.LocalDateTime
|
||||
import java.util.UUID
|
||||
|
||||
interface TranslationSelectionStore {
|
||||
fun find(requestId: Long): TranslationSelectionRequest?
|
||||
fun findCreation(adminId: Long, clientRequestId: UUID): TranslationSelectionRequest?
|
||||
fun lock(requestId: Long): TranslationSelectionRequest?
|
||||
fun nextPreparation(now: LocalDateTime): TranslationSelectionRequest?
|
||||
fun save(request: TranslationSelectionRequest): TranslationSelectionRequest
|
||||
}
|
||||
Reference in New Issue
Block a user