-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(kotlin): migration of the DatabaseService to Kotlin
- Loading branch information
1 parent
787910f
commit ac03d29
Showing
9 changed files
with
218 additions
and
208 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
3 changes: 1 addition & 2 deletions
3
backend/src/main/java/lan/dk/podcastserver/controller/task/DatabaseController.java
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
2 changes: 1 addition & 1 deletion
2
backend/src/main/java/lan/dk/podcastserver/scheduled/DatabaseScheduled.java
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
84 changes: 0 additions & 84 deletions
84
backend/src/main/java/lan/dk/podcastserver/service/DatabaseService.java
This file was deleted.
Oops, something went wrong.
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
77 changes: 77 additions & 0 deletions
77
backend/src/main/kotlin/com/github/davinkevin/podcastserver/service/DatabaseService.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,77 @@ | ||
package com.github.davinkevin.podcastserver.service | ||
|
||
import lan.dk.podcastserver.service.properties.Backup | ||
import org.rauschig.jarchivelib.ArchiveFormat | ||
import org.rauschig.jarchivelib.ArchiverFactory | ||
import org.rauschig.jarchivelib.CompressionType | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
import java.io.IOException | ||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import java.time.ZonedDateTime | ||
import java.time.format.DateTimeFormatterBuilder | ||
import java.time.temporal.ChronoField.* | ||
import javax.persistence.EntityManager | ||
|
||
/** | ||
* Created by kevin on 28/03/2016 for Podcast Server | ||
*/ | ||
@Service | ||
@ConditionalOnProperty("podcastserver.backup.enabled") | ||
class DatabaseService(val backup: Backup, val em: EntityManager) { | ||
|
||
private val log = LoggerFactory.getLogger(this.javaClass.name)!! | ||
|
||
@Transactional | ||
fun backupWithDefault(): Path { | ||
log.info("Doing backup operation") | ||
val result = backup(this.backup.location, this.backup.binary) | ||
log.info("End of backup operation") | ||
return result | ||
} | ||
|
||
@Transactional | ||
@Throws(IOException::class) | ||
fun backup(destinationDirectory: Path, isBinary: Boolean = false): Path { | ||
|
||
if (!Files.isDirectory(destinationDirectory)) { | ||
log.error("The path {} is not a directory, can't be use for backup", destinationDirectory.toString()) | ||
return destinationDirectory | ||
} | ||
|
||
val backupFile = destinationDirectory.toAbsolutePath().resolve("podcast-server-" + ZonedDateTime.now().format(formatter) + if (isBinary) "" else ".sql") | ||
|
||
// Simpler way to execute query via JPA, ExecuteUpdate not allowed here | ||
em.createNativeQuery(generateQuery(isBinary, backupFile)).resultList | ||
|
||
|
||
archiver.create(backupFile.fileName.toString(), backupFile.parent.toFile(), backupFile.toFile()) | ||
Files.deleteIfExists(backupFile) | ||
|
||
return backupFile.resolveSibling("${backupFile.fileName}.tar.gz") | ||
} | ||
|
||
private fun generateQuery(isBinary: Boolean, backupFile: Path) = | ||
if (isBinary) "BACKUP TO '$backupFile'" | ||
else "SCRIPT TO '$backupFile'" | ||
|
||
companion object { | ||
|
||
private val archiver = ArchiverFactory.createArchiver(ArchiveFormat.TAR, CompressionType.GZIP) | ||
private val formatter = DateTimeFormatterBuilder() | ||
.appendValue(YEAR, 4) | ||
.appendLiteral("-") | ||
.appendValue(MONTH_OF_YEAR, 2) | ||
.appendLiteral("-") | ||
.appendValue(DAY_OF_MONTH, 2) | ||
.appendLiteral("-") | ||
.appendValue(HOUR_OF_DAY, 2) | ||
.appendLiteral("-") | ||
.appendValue(MINUTE_OF_HOUR, 2) | ||
.toFormatter() | ||
} | ||
} | ||
|
2 changes: 1 addition & 1 deletion
2
backend/src/test/java/lan/dk/podcastserver/scheduled/DatabaseScheduledTest.java
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
117 changes: 0 additions & 117 deletions
117
backend/src/test/java/lan/dk/podcastserver/service/DatabaseServiceTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.