-
-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
204 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
backend/data/src/main/kotlin/io/tolgee/component/ActivityHolderProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package io.tolgee.component | ||
|
||
import io.tolgee.activity.ActivityHolder | ||
import jakarta.annotation.PreDestroy | ||
import org.springframework.beans.factory.support.ScopeNotActiveException | ||
import org.springframework.context.ApplicationContext | ||
import org.springframework.stereotype.Component | ||
import org.springframework.transaction.support.TransactionSynchronization | ||
import org.springframework.transaction.support.TransactionSynchronizationManager | ||
|
||
/** | ||
* Class providing Activity Holder, while caching it in ThreadLocal. | ||
* I also registers a transaction synchronization, which clears the ThreadLocal after transaction | ||
* is completed, this enables us to be safe even when running activities in main thred. | ||
*/ | ||
@Component | ||
class ActivityHolderProvider(private val applicationContext: ApplicationContext) { | ||
private val threadLocal = ThreadLocal<ActivityHolder?>() | ||
|
||
fun getActivityHolder(): ActivityHolder { | ||
// Get the activity holder from ThreadLocal. | ||
var activityHolder = threadLocal.get() | ||
if (activityHolder == null) { | ||
// If no activity holder exists for this thread, fetch one and store it in ThreadLocal. | ||
activityHolder = fetchActivityHolder() | ||
threadLocal.set(activityHolder) | ||
} | ||
return activityHolder | ||
} | ||
|
||
private fun fetchActivityHolder(): ActivityHolder { | ||
return try { | ||
applicationContext.getBean("requestActivityHolder", ActivityHolder::class.java).also { | ||
it.activityRevision | ||
} | ||
} catch (e: ScopeNotActiveException) { | ||
val transactionActivityHolder = | ||
applicationContext.getBean("transactionActivityHolder", ActivityHolder::class.java) | ||
if (TransactionSynchronizationManager.isSynchronizationActive()) { | ||
TransactionSynchronizationManager.registerSynchronization( | ||
object : TransactionSynchronization { | ||
override fun afterCompletion(status: Int) { | ||
clearThreadLocal() | ||
} | ||
} | ||
) | ||
return transactionActivityHolder | ||
} | ||
|
||
throw IllegalStateException("Transaction synchronization is not active.") | ||
} | ||
} | ||
|
||
@PreDestroy | ||
fun clearThreadLocal() { | ||
threadLocal.remove() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
backend/data/src/main/kotlin/io/tolgee/service/dataImport/ImportDeleteService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package io.tolgee.service.dataImport | ||
|
||
import jakarta.persistence.EntityManager | ||
import org.hibernate.Session | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class ImportDeleteService( | ||
private val entityManager: EntityManager | ||
) { | ||
fun deleteImport(importId: Long) { | ||
entityManager.unwrap(Session::class.java).doWork { connection -> | ||
deleteImportTranslations(connection, importId) | ||
deleteImportLanguages(connection, importId) | ||
deleteImportKeyMetaTags(connection, importId) | ||
deleteImportKeyMetaComments(connection, importId) | ||
deleteImportKeyMetaCodeReferences(connection, importId) | ||
deleteImportKeyMeta(connection, importId) | ||
deleteImportKeys(connection, importId) | ||
deleteImportFileIssueParams(connection, importId) | ||
deleteImportFileIssues(connection, importId) | ||
deleteImportFiles(connection, importId) | ||
deleteTheImport(connection, importId) | ||
} | ||
} | ||
|
||
fun executeUpdate(connection: java.sql.Connection, query: String, importId: Long) { | ||
@Suppress("SqlSourceToSinkFlow") | ||
connection.prepareStatement(query).use { statement -> | ||
statement.setLong(1, importId) | ||
statement.executeUpdate() | ||
} | ||
} | ||
|
||
fun deleteImportTranslations(connection: java.sql.Connection, importId: Long) { | ||
val query = | ||
"delete from import_translation " + | ||
"where id in (" + | ||
"select it.id from import_translation it " + | ||
"join import_language il on il.id = it.language_id " + | ||
"join import_file if on il.file_id = if.id where if.import_id = ?)" | ||
executeUpdate(connection, query, importId) | ||
} | ||
|
||
fun deleteImportLanguages(connection: java.sql.Connection, importId: Long) { | ||
val query = | ||
"delete from import_language " + | ||
"where id in (select il.id from import_language il " + | ||
"join import_file if on il.file_id = if.id where if.import_id = ?)" | ||
executeUpdate(connection, query, importId) | ||
} | ||
|
||
fun deleteImportKeys(connection: java.sql.Connection, importId: Long) { | ||
val query = | ||
"delete from import_key " + | ||
"where id in (select ik.id from import_key ik " + | ||
"join import_file if on ik.file_id = if.id where if.import_id = ?)" | ||
executeUpdate(connection, query, importId) | ||
} | ||
|
||
fun deleteImportKeyMetaTags(connection: java.sql.Connection, importId: Long) { | ||
val query = | ||
"delete from key_meta_tags " + | ||
"where key_metas_id in (select ikm.id from key_meta ikm " + | ||
"join import_key ik on ikm.import_key_id = ik.id " + | ||
"join import_file if on ik.file_id = if.id where if.import_id = ?)" | ||
executeUpdate(connection, query, importId) | ||
} | ||
|
||
fun deleteImportKeyMetaComments(connection: java.sql.Connection, importId: Long) { | ||
val query = | ||
"delete from key_comment " + | ||
"where key_meta_id in (select ikm.id from key_meta ikm " + | ||
"join import_key ik on ikm.import_key_id = ik.id " + | ||
"join import_file if on ik.file_id = if.id where if.import_id = ?)" | ||
executeUpdate(connection, query, importId) | ||
} | ||
|
||
fun deleteImportKeyMetaCodeReferences(connection: java.sql.Connection, importId: Long) { | ||
val query = | ||
"delete from key_code_reference " + | ||
"where key_meta_id in (select ikm.id from key_meta ikm " + | ||
"join import_key ik on ikm.import_key_id = ik.id " + | ||
"join import_file if on ik.file_id = if.id where if.import_id = ?)" | ||
executeUpdate(connection, query, importId) | ||
} | ||
|
||
fun deleteImportKeyMeta(connection: java.sql.Connection, importId: Long) { | ||
val query = | ||
"delete from key_meta " + | ||
"where id in (select ikm.id from key_meta ikm " + | ||
"join import_key ik on ikm.import_key_id = ik.id " + | ||
"join import_file if on ik.file_id = if.id where if.import_id = ?)" | ||
executeUpdate(connection, query, importId) | ||
} | ||
|
||
fun deleteImportFileIssueParams(connection: java.sql.Connection, importId: Long) { | ||
val query = | ||
"delete from import_file_issue_param " + | ||
"where import_file_issue_param.issue_id in (select ifi.id from import_file_issue ifi " + | ||
"join import_file if on ifi.file_id = if.id where if.import_id = ?)" | ||
executeUpdate(connection, query, importId) | ||
} | ||
|
||
fun deleteImportFileIssues(connection: java.sql.Connection, importId: Long) { | ||
val query = | ||
"delete from import_file_issue " + | ||
"where id in (select ifi.id from import_file_issue ifi " + | ||
"join import_file if on ifi.file_id = if.id where if.import_id = ?)" | ||
executeUpdate(connection, query, importId) | ||
} | ||
|
||
fun deleteImportFiles(connection: java.sql.Connection, importId: Long) { | ||
val query = "delete from import_file " + | ||
"where id in (select if.id from import_file if where if.import_id = ?)" | ||
executeUpdate(connection, query, importId) | ||
} | ||
|
||
fun deleteTheImport(connection: java.sql.Connection, importId: Long) { | ||
val query = "delete from import where id = ?" | ||
executeUpdate(connection, query, importId) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters