From e5c34a56c218efdd6ba2d3a6da2af4ee80cd78a9 Mon Sep 17 00:00:00 2001 From: Ulisses Lima Date: Tue, 22 Mar 2022 19:27:49 -0300 Subject: [PATCH] https://jira.baeldung.com/browse/BAEL-5403 Editor Review changes: * reverted SpringBootPersistenceApplication.java * created SpringBootJsonConvertFileApplication.java in article package: com.baeldung.boot.json.convertfile --- .../SpringBootPersistenceApplication.java | 49 +--------------- .../SpringBootJsonConvertFileApplication.java | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+), 48 deletions(-) create mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/json/convertfile/SpringBootJsonConvertFileApplication.java diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/SpringBootPersistenceApplication.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/SpringBootPersistenceApplication.java index bbda5823f8..2dff3f37df 100644 --- a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/SpringBootPersistenceApplication.java +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/SpringBootPersistenceApplication.java @@ -1,60 +1,13 @@ package com.baeldung; -import java.io.File; -import java.util.ArrayList; -import java.util.List; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.ApplicationArguments; -import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.ApplicationContext; - -import com.baeldung.boot.json.convertfile.ImportUtils; -import com.baeldung.boot.json.convertfile.service.ImportJsonService; @SpringBootApplication -public class SpringBootPersistenceApplication implements ApplicationRunner { - private Logger log = LogManager.getLogger(this.getClass()); - private static final String RESOURCE_PREFIX = "classpath:"; - - @Autowired - private ApplicationContext context; +public class SpringBootPersistenceApplication { public static void main(String ... args) { SpringApplication.run(SpringBootPersistenceApplication.class, args); } - @Override - public void run(ApplicationArguments args) throws Exception { - if (args.containsOption("import")) { - if (!args.containsOption("collection")) - throw new IllegalArgumentException("required option: --collection with collection name when using --import"); - - String collection = args.getOptionValues("collection") - .get(0); - - List sources = args.getOptionValues("import"); - for (String source : sources) { - List jsonLines = new ArrayList<>(); - if (source.startsWith(RESOURCE_PREFIX)) { - String resource = source.substring(RESOURCE_PREFIX.length()); - jsonLines = ImportUtils.linesFromResource(resource); - } else { - jsonLines = ImportUtils.lines(new File(source)); - } - - if (jsonLines == null || jsonLines.isEmpty()) { - log.warn(source + " - no input to import"); - } else { - ImportJsonService importService = context.getBean(ImportJsonService.class); - String result = importService.importTo(collection, jsonLines); - log.info(source + " - import result: " + result); - } - } - } - } } diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/json/convertfile/SpringBootJsonConvertFileApplication.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/json/convertfile/SpringBootJsonConvertFileApplication.java new file mode 100644 index 0000000000..40613c056b --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/json/convertfile/SpringBootJsonConvertFileApplication.java @@ -0,0 +1,58 @@ +package com.baeldung.boot.json.convertfile; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import com.baeldung.boot.json.convertfile.service.ImportJsonService; + +@SpringBootApplication +public class SpringBootJsonConvertFileApplication implements ApplicationRunner { + private Logger log = LogManager.getLogger(this.getClass()); + private static final String RESOURCE_PREFIX = "classpath:"; + + @Autowired + private ImportJsonService importService; + + public static void main(String ... args) { + SpringApplication.run(SpringBootJsonConvertFileApplication.class, args); + } + + @Override + public void run(ApplicationArguments args) throws Exception { + if (args.containsOption("import")) { + if (!args.containsOption("collection")) + throw new IllegalArgumentException("required option: --collection with collection name when using --import"); + + String collection = args.getOptionValues("collection") + .get(0); + + List sources = args.getOptionValues("import"); + for (String source : sources) { + List jsonLines = new ArrayList<>(); + if (source.startsWith(RESOURCE_PREFIX)) { + String resource = source.substring(RESOURCE_PREFIX.length()); + jsonLines = ImportUtils.linesFromResource(resource); + } else { + jsonLines = ImportUtils.lines(new File(source)); + } + + if (jsonLines == null || jsonLines.isEmpty()) { + log.warn(source + " - no input to import"); + } else { + // ImportJsonService importService = context.getBean(ImportJsonService.class); + String result = importService.importTo(collection, jsonLines); + log.info(source + " - import result: " + result); + } + } + } + } +}