package hello.config.data;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import hello.dao.GreetingService;
import hello.entities.Greeting;

@Component
@Profile({ "default", "test" })
public class InitialDataLoader implements ApplicationListener<ContextRefreshedEvent> {

	private final static Logger log = LoggerFactory.getLogger(InitialDataLoader.class);

	private final static String GREETING = "Hello, %s!";

	@Autowired
	private GreetingService greetingService;


	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {

		if (greetingService.count() > 0) {
			log.info("Database already populated. Skipping data initialization.");
			return;
		}

		Greeting greeting = new Greeting();
		greeting.setTemplate(GREETING);

		greetingService.save(greeting);

		log.info("Added greeting (" + greeting.getId() + "): " + greeting.getTemplate());
	}
}

@@ -682,32 +682,53 @@
 ring
- GREETING = %22Hello, %25s!%22
+%5B%5D TEMPLATES = %7B %22Hello, %25s!%22, %22Howdy, %25s!%22 %7D
 ;%0A%0A%09
@@ -984,16 +984,55 @@
 n;%0A%09%09%7D%0A%0A
+%09%09for (String template : TEMPLATES) %7B%0A%09
 %09%09Greeti
@@ -1063,16 +1063,17 @@
 ng();%0A%09%09
+%09
 greeting
@@ -1089,20 +1089,21 @@
 ate(
-GREETING
+template
 );%0A%0A
+%09
 %09%09gr
@@ -1132,17 +1132,17 @@
 eting);%0A
-%0A
+%09
 %09%09log.in
@@ -1217,13 +1217,17 @@
 ate());%0A
+%09%09%7D%0A
 %09%7D%0A%7D%0A

Add another greeting to the initial data.