|
|
@ -0,0 +1,88 @@ |
|
|
|
package com.jesuspinar.booksearch; |
|
|
|
|
|
|
|
import com.jesuspinar.booksearch.model.Book; |
|
|
|
import com.jesuspinar.booksearch.service.IBookQueryService; |
|
|
|
import com.jesuspinar.booksearch.service.IBookService; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Collection; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
@Component |
|
|
|
public class Run { |
|
|
|
|
|
|
|
@Autowired |
|
|
|
IBookService iBookService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
IBookQueryService iBookQueryService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
Help help; |
|
|
|
|
|
|
|
public void run(String[] args) { |
|
|
|
|
|
|
|
if (args.length < 1) { |
|
|
|
System.out.println("Error de sintaxis"); |
|
|
|
System.out.println(help.getHelp()); |
|
|
|
} else if (args.length == 1) { |
|
|
|
switch (args[0].toLowerCase()) { |
|
|
|
case "-lg": |
|
|
|
iBookService.findAllGenres().forEach(System.out::println); |
|
|
|
break; |
|
|
|
case "-h": |
|
|
|
System.out.println(help.getHelp()); |
|
|
|
break; |
|
|
|
default: |
|
|
|
System.out.println("Error de sintaxis"); |
|
|
|
System.out.println(help.getHelp()); |
|
|
|
|
|
|
|
} |
|
|
|
} else if (args.length % 2 != 0) { |
|
|
|
System.out.println("Error de sintaxis"); |
|
|
|
System.out.println(help.getHelp()); |
|
|
|
} else if (args.length > 8) { |
|
|
|
System.out.println("Error de sintaxis"); |
|
|
|
System.out.println(help.getHelp()); |
|
|
|
} else { |
|
|
|
List<String[]> argumentos = new ArrayList<>(); |
|
|
|
|
|
|
|
for (int i = 0; i < args.length; i += 2) { |
|
|
|
argumentos.add(new String[]{args[i], args[i + 1]}); |
|
|
|
} |
|
|
|
|
|
|
|
boolean error = false; |
|
|
|
|
|
|
|
for (String[] argumento : argumentos) { |
|
|
|
switch (argumento[0].toLowerCase()) { |
|
|
|
case "-ag": |
|
|
|
iBookQueryService.anyGenre(argumento[1].split(",")); |
|
|
|
break; |
|
|
|
case "-tg": |
|
|
|
iBookQueryService.allGenres(argumento[1].split(",")); |
|
|
|
break; |
|
|
|
case "-t": |
|
|
|
iBookQueryService.anyTitle(argumento[1]); |
|
|
|
break; |
|
|
|
default: |
|
|
|
error = true; |
|
|
|
System.out.println("Error de sintaxis"); |
|
|
|
System.out.println(help.getHelp()); |
|
|
|
} |
|
|
|
} |
|
|
|
if (!error) { |
|
|
|
Collection<Book> result = iBookQueryService.exec(); |
|
|
|
System.out.printf("%s\t%-35s\t%-22s\t%s\n", "ID", "Título", "Autor", "Género"); |
|
|
|
if (result != null) { |
|
|
|
result.forEach(book -> System.out.printf("%s\t%-35s\t%-22s\t%s\n", |
|
|
|
book.getId(), book.getTitle(), book.getAuthors(), |
|
|
|
book.getGenre())); |
|
|
|
} else { |
|
|
|
System.out.println("No hay libros que cumplan esos criterios."); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |