Skip to content

Commit

Permalink
refactor(kotlin): migration of the DatabaseService to Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
davinkevin committed Sep 25, 2018
1 parent 787910f commit ac03d29
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 208 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Podcast-Server

Back-end : [![Codacy Badge](https://api.codacy.com/project/badge/Grade/2030290b1c2145f6878e9ad7811c542e)](https://www.codacy.com/app/davin-kevin/Podcast-Server?utm_source=github.com&utm_medium=referral&utm_content=davinkevin/Podcast-Server&utm_campaign=Badge_Grade) [![Coverage Status](https://coveralls.io/repos/davinkevin/Podcast-Server/badge.svg?branch=master)](https://coveralls.io/r/davinkevin/Podcast-Server?branch=master)

Migration : ![Java stage](https://badgen.net/badge/Java/90%25/orange) ![Kotlin stage](https://badgen.net/badge/Kotlin/10%25/purple)
Migration : ![Java stage](https://badgen.net/badge/Java/89%25/orange) ![Kotlin stage](https://badgen.net/badge/Kotlin/11%25/purple)

Front-end : [![Code Climate](https://codeclimate.com/github/davinkevin/Podcast-Server/badges/gpa.svg)](https://codeclimate.com/github/davinkevin/Podcast-Server)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package lan.dk.podcastserver.controller.task;

import lan.dk.podcastserver.service.DatabaseService;
import com.github.davinkevin.podcastserver.service.DatabaseService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package lan.dk.podcastserver.scheduled;

import lan.dk.podcastserver.service.DatabaseService;
import com.github.davinkevin.podcastserver.service.DatabaseService;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package lan.dk.podcastserver.service.properties;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.boot.context.properties.ConfigurationProperties;
Expand All @@ -10,11 +9,23 @@
/**
* Created by kevin on 12/04/2016 for Podcast Server
*/
@Getter @Setter
@Setter
@Accessors(chain = true)
@ConfigurationProperties("podcastserver.backup")
public class Backup {
private Path location;
private Boolean binary = false;
private String cron = "0 0 4 * * *";

public Path getLocation() {
return this.location;
}

public Boolean getBinary() {
return this.binary;
}

public String getCron() {
return this.cron;
}
}
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()
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package lan.dk.podcastserver.scheduled;

import lan.dk.podcastserver.service.DatabaseService;
import com.github.davinkevin.podcastserver.service.DatabaseService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
Expand Down

This file was deleted.

Loading

0 comments on commit ac03d29

Please sign in to comment.