package org.keyboardplaying.demo.injector;

import org.keyboardplaying.demo.people.PeopleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Scanner;

/**
 * The command-line interface to control the injector.
 *
 * @author Cyrille Chopelet
 */
@Component
public class CommandLineInterface implements ApplicationContextAware, CommandLineRunner {

    private ApplicationContext context;

    @Autowired
    private PeopleRepository repository;

    @Autowired
    private PersonGenerator personGenerator;

    @Override
    public void setApplicationContext(ApplicationContext context) {
        this.context = context;
    }

    @Override
    public void run(String... args) throws Exception {
        final Scanner scanner = new Scanner(System.in);

        loop:
        while (true) {
            System.out.println("What would you like to do: [c]lear database, [i]nject data or e[x]it injector?");

            String command = scanner.next();
            switch (command.toLowerCase().charAt(0)) {
                case 'c':
                    repository.deleteAll();
                    System.out.println("Repository has been cleaned");
                    break;

                case 'i':
                    injectPeople(scanner);
                    break;

                case 'x':
                    exitApplication(scanner);
                    break loop;

                default:
                    System.out.println(String.format("Command <%s> is invalid", command));
            }
        }
    }

    private void injectPeople(Scanner scanner) {
        System.out.println("How many people would you like to inject into database?");
        try {
            int max = scanner.nextInt();
            for (int i = 0; i < max; ++i) {
                try {
                    repository.save(personGenerator.randomPerson());
                } catch (Exception e) {
                    e.printStackTrace();
                    System.err.println("Skipping record, going on");
                }
                if (i % 1000 == 0) {
                    System.out.println();
                } else if (i % 100 == 0) {
                    System.out.print("|");
                } else if (i % 10 == 0) {
                    System.out.print(".");
                }
            }
        } catch (NumberFormatException e) {
            System.out.println("Please provide an integer value.");
            injectPeople(scanner);
        }
    }

    private void exitApplication(Scanner scanner) {
        scanner.close();
        SpringApplication.exit(this.context);
    }
}
@@ -424,16 +424,52 @@
 onent;%0A%0A
+import javax.annotation.PreDestroy;%0A
 import j
@@ -683,16 +683,68 @@
 nner %7B%0A%0A
+    private final Scanner scanner = new Scanner();%0A%0A
     priv
@@ -894,16 +894,93 @@
 rator;%0A%0A
+    @PreDestroy%0A    public void tearDown() %7B%0A        scanner.close();%0A    %7D%0A%0A
     @Ove
@@ -1165,79 +1165,8 @@
 n %7B%0A
-        final Scanner scanner = new Scanner(System.in);%0A%0A        loop:%0A
     
@@ -1619,39 +1619,32 @@
    injectPeople(
-scanner
 );%0A             
@@ -1720,23 +1720,16 @@
 ication(
-scanner
 );%0A     
@@ -1752,13 +1752,8 @@
 reak
- loop
 ;%0A%0A 
@@ -1928,31 +1928,16 @@
 tPeople(
-Scanner scanner
 ) %7B%0A    
@@ -2796,23 +2796,16 @@
 tPeople(
-scanner
 );%0A     
@@ -2853,51 +2853,11 @@
 ion(
-Scanner scanner) %7B%0A        scanner.close();
+) %7B
 %0A   

Fix some SonarQube issues