1616package com.embabel.movie.populate
1717
1818import com.embabel.common.util.RandomFromFileMessageGenerator
19- import com.embabel.movie.domain.MovieBuff
20- import com.embabel.movie.domain.MovieService
21- import com.embabel.movie.domain.StreamingService
22- import com.embabel.movie.domain.StreamingServiceRepository
19+ import com.embabel.movie.domain.*
20+ import org.slf4j.LoggerFactory
2321import org.springframework.boot.ApplicationArguments
2422import org.springframework.boot.ApplicationRunner
2523import org.springframework.stereotype.Component
24+ import org.springframework.stereotype.Service
2625import org.springframework.transaction.annotation.Transactional
2726
28- @Component
29- class StartupDataInitializer (
27+ @Service
28+ class DataPopulatorService (
3029 private val movieService : MovieService ,
30+ private val movieBuffRepository : MovieBuffRepository ,
3131 private val streamingServiceRepository : StreamingServiceRepository ,
32- ) : ApplicationRunner {
32+ ) {
33+ private val logger = LoggerFactory .getLogger(DataPopulatorService ::class .java)
3334
3435 @Transactional
35- override fun run (args : ApplicationArguments ? ) {
36- // Skip if data already exists
37- if (streamingServiceRepository.count() > 0 ) {
38- return
39- }
36+ fun populate () {
37+ try {
38+ logger.info(
39+ " {} streaming services, {} movie buffs found in database." ,
40+ streamingServiceRepository.count(),
41+ movieBuffRepository.count(),
42+ )
43+ // Skip if data already exists
44+ if (streamingServiceRepository.count() > 0 || movieBuffRepository.count() > 0 ) {
45+ return
46+ }
4047
41- val netflix = StreamingService (" Netflix" , " https://www.netflix.com" )
42- streamingServiceRepository.save(netflix)
43- val stan = StreamingService (" Stan" , " https://www.stan.com.au" )
44- streamingServiceRepository.save(stan)
45- val disneyPlus = StreamingService (" Disney+" , " https://www.disneyplus.com" )
46- streamingServiceRepository.save(disneyPlus)
48+ logger.info(" Populating initial data..." )
4749
48- val name = " Rod"
49- if (movieService.findMovieBuffByName(name) == null ) {
50- val rod = MovieBuff (
51- username = " Rod" ,
52- displayName = " Rod Johnson" ,
53- email = " johnsonroda@gmail.com" ,
54- hobbies = listOf (" Travel" , " Skiing" , " Chess" , " Hiking" , " Reading" ),
55- countryCode = " au" ,
56- about = """
57- Rod is an Australian man who has a PhD in Musicology and
58- has a career as a software engineer, author and tech entrepreneur.
59- He is widely traveled and has lived in California and the UK
60- before returning to Sydney.
61- """ .trimIndent(),
62- streamingServices = mutableListOf (netflix, stan, disneyPlus),
63- movieLikes = " Complex plots, film noir" ,
64- movieDislikes = " Predictable endings, formulaic blockbusters, anime" ,
65- )
66- loadRatings(rod, " movie/rod_ratings.tsv" )
67- movieService.save(rod)
50+ val netflix = StreamingService (" Netflix" , " https://www.netflix.com" )
51+ streamingServiceRepository.save(netflix)
52+ val stan = StreamingService (" Stan" , " https://www.stan.com.au" )
53+ streamingServiceRepository.save(stan)
54+ val disneyPlus = StreamingService (" Disney+" , " https://www.disneyplus.com" )
55+ streamingServiceRepository.save(disneyPlus)
56+
57+ val name = " Rod"
58+ if (movieService.findMovieBuffByName(name) == null ) {
59+ val rod = MovieBuff (
60+ username = " Rod" ,
61+ displayName = " Rod Johnson" ,
62+ email = " johnsonroda@gmail.com" ,
63+ hobbies = listOf (" Travel" , " Skiing" , " Chess" , " Hiking" , " Reading" ),
64+ countryCode = " au" ,
65+ about = """
66+ Rod is an Australian man who has a PhD in Musicology and
67+ has a career as a software engineer, author and tech entrepreneur.
68+ He is widely traveled and has lived in California and the UK
69+ before returning to Sydney.
70+ """ .trimIndent(),
71+ streamingServices = mutableListOf (netflix, stan, disneyPlus),
72+ movieLikes = " Complex plots, film noir" ,
73+ movieDislikes = " Predictable endings, formulaic blockbusters, anime" ,
74+ )
75+ loadRatings(rod, " movie/rod_ratings.tsv" )
76+ movieService.save(rod)
77+ }
78+ logger.info(" Data population completed successfully" )
79+ } catch (e: Exception ) {
80+ logger.error(" Data population failed - transaction will rollback" , e)
81+ throw e
6882 }
6983 }
7084
71-
7285 fun loadRatings (movieBuff : MovieBuff , tsv : String ) {
7386 addRatings(
7487 movieBuff,
@@ -77,7 +90,6 @@ class StartupDataInitializer(
7790 )
7891 }
7992
80-
8193 fun addRatings (
8294 movieBuff : MovieBuff ,
8395 inputs : List <String >,
@@ -90,7 +102,21 @@ class StartupDataInitializer(
90102 // Last element is the rating, everything else is the title
91103 val rating = parts.last().toInt()
92104 val title = parts.dropLast(1 ).joinToString(" " )
93- movieService.rate(movieBuff, title, rating)
105+ try {
106+ movieService.rate(movieBuff, title, rating)
107+ } catch (e: Exception ) {
108+ logger.warn(" Failed to rate movie '{}' with rating {}: {}" , title, rating, e.message)
109+ }
94110 }
95111 }
96- }
112+ }
113+
114+ @Component
115+ class StartupDataInitializer (
116+ private val dataPopulatorService : DataPopulatorService ,
117+ ) : ApplicationRunner {
118+
119+ override fun run (args : ApplicationArguments ? ) {
120+ dataPopulatorService.populate()
121+ }
122+ }
0 commit comments